我需要生成2个不同频率的正弦波音,并在Android上以立体声模式分别播放到右声道和左声道。
这是我的代码:
int sample;
double sampleRate;
double duration;
double time;
double f1;
double f2;
double amplitude1;
double amplitude2;
double sineWave1;
double sineWave2;
float[] buffer1;
float[] buffer2;
byte[] byteBuffer1;
byte[] byteBuffer2;
byte[] byteBufferFinal;
int bufferIndex;
short x;
short y;
AudioTrack audioTrack;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
sampleRate = 44100.0;
duration = 20.0;
f1 = 440.0;
amplitude1= 1;
f2 = 444.0;
amplitude2 = 0;
buffer1 = new float[(int)(duration*sampleRate)];
buffer2 = new float[(int)(duration*sampleRate)];
for(sample = 0; sample < buffer1.length; sample ++){
time = sample / sampleRate;
buffer1[sample] = (float)(amplitude1*Math.sin(2*Math.PI*f1*time));
buffer2[sample] = (float)(amplitude2*Math.sin(2*Math.PI*f2*time));
}
byteBuffer1 = new byte[buffer1.length*2]; //two bytes per audio frame, 16 bits
for(int i = 0, bufferIndex=0; i < byteBuffer1.length; i++){
x = (short) (buffer1[bufferIndex++]*32767.0); // [2^16 - 1]/2 = 32767.0
byteBuffer1[i] = (byte) x; // low byte
byteBuffer1[++i] = (byte) (x >>> 8); // high byte
}
byteBuffer2 = new byte[buffer2.length*2];
for(int j = 0, bufferIndex=0; j < byteBuffer2.length; j++){
y = (short) (buffer2[bufferIndex++]*32767.0);
byteBuffer2[j] = (byte) y; // low byte
byteBuffer2[++j] = (byte) (y >>> 8); // high byte
}
byteBufferFinal = new byte[byteBuffer1.length*2];
//LL RR LL RR LL RR
for(int k = 0, index = 0; index < byteBufferFinal.length - 4; k=k+2){
byteBufferFinal[index] = byteBuffer1[k]; // LEFT {0,1/4,5/8,9/12,13;...}
byteBufferFinal[index+1] = byteBuffer1[k+1];
index = index + 2;
byteBufferFinal[index] = byteBuffer2[k]; // RIGHT {2,3/6,7/10,11;...}
byteBufferFinal[index+1] = byteBuffer2[k+1];
index = index + 2;
}
audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
(int) sampleRate,AudioFormat.CHANNEL_OUT_STEREO,
AudioFormat.ENCODING_PCM_16BIT,byteBufferFinal.length,
AudioTrack.MODE_STATIC);
audioTrack.write(byteBufferFinal, 0, byteBufferFinal.length);
audioTrack.play();
}
输出缓冲区(byteBufferFinal)具有16位格式:LL RR LL RR LL RR LL(每个字符为1个字节)
我的代码不能很好地分配左右声道中的声音。我证明它将一个振幅放在“1”而另一个放在“0”中,这样两个声道中的一个没有声音,但都发出声音
我的代码出了什么问题?
答案 0 :(得分:0)
我尝试使用API 18,Eclipse Kepler,在我的Samsung S4上运行,它运行良好。右声道静音,左声道播放440Hz正弦波。
我在阅读代码时注意到的唯一错误是播放持续时间是它应该的1/2:行“byteBufferFinal = new byte [buffer1.length * 2];”应该是“byteBufferFinal = new byte [byteBuffer1.length * 2];”
可悲的是,问题可能只是您的音频线或扬声器:音频插头可能没有完全插入电话插孔,两个扬声器都可以播放一个频道。