2

我正在尝试将 16 位单声道声音转换为立体声。声音存储为字节数组,因此据我了解,这意味着我一次复制两个字节。

在此处输入图像描述

我这样做对吗?我生成的代码改变了频率。

编辑:

我成功生成单音并将其存储在byte [] generatedSnd

播放单声道声音(工作):

    AudioTrack audioTrack = null;                                   // Get audio track
    try {
        audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
                sampleRate, AudioFormat.CHANNEL_OUT_MONO,
                AudioFormat.ENCODING_PCM_16BIT, (int)numSamples*2,
                AudioTrack.MODE_STATIC);
        audioTrack.setStereoVolume(0f, 1f);
        audioTrack.write(generatedSnd, 0, generatedSnd.length);     // Load the track
        audioTrack.play();                                          // Play the track
    }
    catch (Exception e){ }

转换为立体声:

    int monoByteArrayLength = generatedSnd.length;
    byte [] stereoGeneratedSnd = new byte[monoByteArrayLength * 2];

    stereoGeneratedSnd[0] = generatedSnd[0];
    stereoGeneratedSnd[2] = generatedSnd[0];

    for (int x=1; x<monoByteArrayLength; x+=2) {

        stereoGeneratedSnd[x*2-1] = generatedSnd[x];
        stereoGeneratedSnd[x*2+1] = generatedSnd[x];

        if (x+1 < monoByteArrayLength) {
            stereoGeneratedSnd[x*2] = generatedSnd[x+1];
            stereoGeneratedSnd[x*2+2] = generatedSnd[x+1];
        }
    }

    AudioTrack audioTrack = null;                                   // Get audio track
    try {
        audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
                sampleRate, AudioFormat.CHANNEL_OUT_STEREO,
                AudioFormat.ENCODING_PCM_16BIT, (int)numSamples*2,
                AudioTrack.MODE_STATIC);
        audioTrack.setStereoVolume(0f, 1f);
        audioTrack.write(stereoGeneratedSnd, 0, stereoGeneratedSnd.length);     // Load the track
        audioTrack.play();                                          // Play the track
    }
    catch (Exception e){ }

我想做的是只从一个通道播放声音

4

2 回答 2

5

一次复制两个字节是故意的吗?16bit PCM Wave 格式的立体声模式采用:

DATA[ ] : [1st byte from Chanel 1], [1st byte from Chanel 2], [2nd byte from Chanel 1], [2nd byte from Chanel 2]...

因此,如果您想将单声道转换为立体声,您的数组应该是:

Mono   : 0, 1, 2, 3 ...
Stereo : 0, 0, 1, 1, 2, 2, 3, 3 ...

如果你只想要一个频道

Stereo : 0, 0, 1, 0, 2, 0, 3, 0 ...
于 2015-03-30T06:48:51.923 回答
2

加倍算法的输出是0, 1, 2, 1, 2, 3, 4, 3, 4, 5, 0, 5.

一种更简单(且正确)的加倍方法是:

for (int i = 0; i < monoByteArrayLength; i += 2) {
    stereoGeneratedSnd[i*2+0] = generatedSnd[i];
    stereoGeneratedSnd[i*2+1] = generatedSnd[i+1];
    stereoGeneratedSnd[i*2+2] = generatedSnd[i];
    stereoGeneratedSnd[i*2+3] = generatedSnd[i+1];
}

输出:0, 1, 0, 1, 2, 3, 2, 3, 4, 5, 4, 5

于 2013-10-11T08:02:36.840 回答