How to extract sound object to a mono byte array in AS3

有些话、适合烂在心里 提交于 2019-12-25 03:25:14

问题


I'm trying to prepend the byte array of a sound object to a captured microphone sound byte array.

It works, but the extracted sound object get pichted down and doubled in length. I guess this is because the byte array of the sound object is in stereo, while the mic byte array is in mono.

I have this:

sound.extract(myByteArray, extract);

myByteArray now contains the stereo data. How can I turn this to mono (I'm new to ByteArrays).

UPDATE:

Here's a working solution:

existingByte.position = 0;
var mono : ByteArray = new ByteArray();
while(existingByte.bytesAvailable) {
    var left : Number = existingByte.readFloat();
    mono.writeFloat(left);
    existingByte.position +=4;
}

回答1:


Just pick a channel to extract. I think ByteArray is interleaved so if you pick all odd bytes its the left channel, if you pick all even bytes it's the right channel.

var mono : ByteArray = new ByteArray();
for( var i : int = 0; i < raw.length; i+=2 ) {
    var left : int = raw[i];
    var right : int = raw[i+1];

    var mixed : int = left * 0.5 + right * 0.5;
    if( pickLeft ) {
      mono.writeByte( left );
    } else if( pickRight ) {
      mono.writeByte( right );
    } else {
      mono.writeByte( mixed );
    }
}


来源:https://stackoverflow.com/questions/8912631/how-to-extract-sound-object-to-a-mono-byte-array-in-as3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!