Search code examples
flashactionscript-3audiopcm

how to use loadPCMFromByteArray?


has anyone been able to use the new Sound method loadPCMFromByteArray in Flash 11 successfully? I just end up hearing repeating ticking sound whenever I use it.

And also is there a way to set the depth (bits) in that method? it seems that this loadPCMFromByteArray only understands 32bit PCM data.

I've looked for examples but I've found no working examples

Here is my sample code, please note that I'm continously adding data to the soundBA (ByteArray object) and clearing it after passing it to the loadPCMFromByteArray method, also I'm just forced to do a check (8192 * 4) since it really seems that the loadPCMFromByeArray method only understands 32bit audio samples, how can I set it to read 8-bit and 16-bit samples?:

if (soundBA.bytesAvailable >= (8192 * 4 * channels))
{ 
    soundPlayer.loadPCMFromByteArray(soundBA, soundBA.bytesAvailable / 4, "float", false, 22050);
    soundChannel = soundPlayer.play();
    soundBA.clear();
}

Solution

  • I'm looking for the same thing as you but I already have a part of the answer. Before trying to use "loadPCMFromByteArray" function, I was using as3wavsound lib which uses an audio manipulating library called "sazameki". This last library gives you ways to transcode 8 or 16 bits to a vector of Numbers. You just have to take that vector and write it into a ByteArray with:

    var someByteArray:ByteArray = new ByteArray();
    for (....){
      someByteArray.writeFloat(number);
    }
    

    then give it "loadPCMFromByteArray" function. You should have an error because your position on the ByteArray is not good. So reset it. Your final code should look like that:

    var flexSound:Sound = new Sound();
    var someByteArray:ByteArray = new ByteArray();
    for (....){
        someByteArray.writeFloat(number);
    }
    someByteArray.position = 0;
    flexSound.loadPCMFromByteArray(someByteArray, length);
    flexSound.play();
    

    I hope this could help you.