我正在尝试将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){ }
我要做的是只从一个频道播放声音
答案 0 :(得分:4)
这是故意你一次复制两个字节吗? 16位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 ...
答案 1 :(得分:3)
倍增算法的输出为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