我正在尝试将 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){ }
我想做的是只从一个通道播放声音