views:

250

answers:

2

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...

+1  A: 

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);

iddqd
A: 

Hi Mate

How do cut off the sound using Script3.0 ?

uperumal