Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

raw sound byteArray to float Array

I'm trying to convert the byteArray of a Sound Object to an array with floats. The Sound Object plays back fine & at full length, but the float Array i get from it is cut off (but sounds correct), so i must be doing something wrong in the conversion:

var s:Sound = mySound;
s.play(); // plays fine

var bytes:ByteArray = new ByteArray();
bytes.endian = Endian.LITTLE_ENDIAN;
s.extract(bytes, s.bytesTotal, 0);

var leftChannel:Array = new Array();
var rightChannel:Array = new Array();

bytes.position = 0;

while (bytes.bytesAvailable)
{
    leftChannel.push(bytes.readFloat());
    rightChannel.push(bytes.readFloat());
}

and this is what i get:

alt text

The top two channels are the original Sound Object. The lower two is the float Array Data. I aligned them so you can see that the beginning is cut off and obviously the length is incorrect.

Thanks for any answers...

like image 383
iddqd Avatar asked May 13 '26 07:05

iddqd


1 Answers

ok there were two problems:

  1. the mp3 file i was importing was somehow corrupt, that caused the beginning to be cut off
  2. the length i defined to extract was not correct, to find the full sound length use

var numTotalSamples:Number = int(s.length * 44.1); //assuming 44.1kHz sample rate

then:

s.extract(bytes, numTotalSamples, 0);

like image 150
iddqd Avatar answered May 16 '26 02:05

iddqd



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!