quarks
quarks

Reputation: 35276

Reading byte from floating point (Javascript)

I'm trying to get an audio data from a AS library, from the documentation the function is like this:

protected function audioData():String
        {
            var ret:String="";
            buffer.position = 0;                
            while (buffer.bytesAvailable > 0) 
            {
                ret += buffer.readFloat().toString() + ";";
            }
            return ret;
        }

In between my code and this library is another js that have this code:

 audioData: function(){
    return this.flashInterface().audioData().split(";");
  },

From my code I access this like:

  function getdata(){
    var data = Recorder.audioData();
    console.log("audioData: " + data);
  }

However, I tried to output the returned value to Firebug, I get a very long comma-separated list of of floating point values, how can I get back the byte[] buffer? What I mean by buffer is similar to Java, since I will be accessing the buffer from Java via JSNI.

Here's the sample log output (actual log is very long):

-0.00030517578125,0.00006103515625,0.00115966796875,0.00146484375,-0.00091552734375,-0.000946044921875,-0.001983642578125,-0.003997802734375,-0.005126953125,-0.00360107421875,-0.0032958984375,-0.004119873046875,-0.00433349609375,-0.0023193359375,-0.0008544921875,-0.003448486328125,-0.00347900390625,-0.0054931640625,-0.0067138671875,-0.005279541015625,-0.006072998046875,

I can't re-compile the AS that creates the output, for now what I can do is to interface to the SWF component in javascript and accept its floating point and convert it back to byte array. There's just too many errors in my AS project in FDT 5 IDE that I already need to do the mockup of my application.

I really want to recompile the AS library to fit the need however right now I just want to use it as it is.

Upvotes: 0

Views: 138

Answers (1)

Barış Uşaklı
Barış Uşaklı

Reputation: 13532

If you want to see the actual byte data in the byte array you can use the following :

protected function audioData():String
{
   var ret:String="";
   buffer.position = 0;                
   while (buffer.bytesAvailable > 0) 
   {
      ret += buffer.readByte().toString();
   }
   return ret;
}

AFAIK the ByteArray class in as3 is already a byte array(as the name suggests :)) you can access it's data using the [] operator, as in byteArray[0] will give you the first byte.

You should be able to send the byte array to a url on your server with a post request with something like this:

var request:URLRequest = new URLRequest ("http://someurl");
var loader: URLLoader = new URLLoader();
request.contentType = "application/octet-stream";
request.method = URLRequestMethod.POST;
request.data = byteArray;
loader.load(_request);

Upvotes: 1

Related Questions