android左右声道接反具体修正方法

原理:
对buffer的奇偶位数的数据进行对调
C++ tinyalsa 写法:
void AudioYusuStreamOut::swS2M(const void* buffer, size_t bytes)
{
uint32 Idx_i;
int16 PCM_M;
short* pbuffer=(short*)buffer;
//enable software stereo to mono
for(Idx_i = 0; Idx_i>1; Idx_i+=2)
{
PCM_M = pbuffer[Idx_i+1];
pbuffer[Idx_i+1]=pbuffer[Idx_i];
pbuffer[Idx_i]=PCM_M;
}
}

JAVA 写法:
/**
* 左右声道进行反转
* @param data
* @return
* 反转后的数据
*/
private byte[] getReversedData(byte[] data) {
byte[] reversed = new byte[data.length];
for (int i = 0; i < data.length - 3; i = i + 4) {
reversed[i] = data[i+2];
reversed[i+1] = data[i+3];
reversed[i+2] = data[i];
reversed[i+3] = data[i+1];
}
return reversed;
}

立体声拆分
现在需要把它拆分成左右声道来进行其他处理,可以通过如下方式来实现:

private void splitStereoPcm(byte[] data) {
    int monoLength = data.length / 2;
    byte[] leftData = new byte[monoLength];
    byte[] rightData = new byte[monoLength];
    for (int i = 0; i < monoLength; i++) {
        if (i % 2 == 0) {
            System.arraycopy(data, i * 2, leftData, i, 2);
        } else {
            System.arraycopy(data, i * 2, rightData, i - 1, 2);
        }
    }
    //TODO 使用leftData、rightData 进行其他处理
}

你可能感兴趣的:(android-audio)