gpt4 book ai didi

android - Android AudioTrack 混音示例代码

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:16:15 25 4
gpt4 key购买 nike

资源文件夹中有两个 PCM 声音文件。我使用输入流并将它们转换为字节数组。

然后我通过规范化处理它们并添加 music1 和 music2 并输出到字节数组输出。最后,放入输出数组并将其提供给 AudioTrack。

显然,我没有听到任何声音,有什么不对劲。

 private void mixSound() throws IOException {

InputStream in1=getResources().openRawResource(R.raw.cheerapp2);
InputStream in2=getResources().openRawResource(R.raw.buzzer2);

byte[] music1 = null;
music1= new byte[in1.available()];
music1=convertStreamToByteArray(in1);
in1.close();


byte[] music2 = null;
music2= new byte[in2.available()];
music2=convertStreamToByteArray(in2);
in2.close();

byte[] output = new byte[music1.length];

audioTrack.play();

for(int i=0; i < output.length; i++){

float samplef1 = music1[i] / 128.0f; // 2^7=128
float samplef2 = music2[i] / 128.0f;


float mixed = samplef1 + samplef2;
// reduce the volume a bit:
mixed *= 0.8;
// hard clipping
if (mixed > 1.0f) mixed = 1.0f;
if (mixed < -1.0f) mixed = -1.0f;
byte outputSample = (byte)(mixed * 128.0f);
output[i] = outputSample;
audioTrack.write(output, 0, i);
} //for loop


public static byte[] convertStreamToByteArray(InputStream is) throws IOException {



ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buff = new byte[10240];
int i = Integer.MAX_VALUE;
while ((i = is.read(buff, 0, buff.length)) > 0) {
baos.write(buff, 0, i);
}

return baos.toByteArray(); // be sure to close InputStream in calling function

}

最佳答案

我试过你的代码(用我自己的一些音频文件代替)。我像这样初始化了一个 AudioTrack 实例,希望这与您的做法类似:

AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, 44100, AudioFormat.CHANNEL_OUT_STEREO, AudioFormat.ENCODING_PCM_16BIT, 44100, AudioTrack.MODE_STREAM);

并尝试运行它。它发出高音调的声音,随着时间的推移变得越来越低。我检查了代码,问题是您在 mixSound() 方法的循环的每次迭代中都将整个输出字节数组写入 audioTrack。

线

 audioTrack.write(output, 0, i);

需要移出循环并更改为

 audioTrack.write(output, 0, output.length);

因此,您将两个文件混合到输出字节数组中,然后一次写入整个内容。

因此,工作 mixSound 方法的代码如下所示:

private void mixSound() throws IOException {
AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, 44100, AudioFormat.CHANNEL_OUT_STEREO, AudioFormat.ENCODING_PCM_16BIT, 44100, AudioTrack.MODE_STREAM);

InputStream in1=getResources().openRawResource(R.raw.track1);
InputStream in2=getResources().openRawResource(R.raw.track2);

byte[] music1 = null;
music1= new byte[in1.available()];
music1=convertStreamToByteArray(in1);
in1.close();


byte[] music2 = null;
music2= new byte[in2.available()];
music2=convertStreamToByteArray(in2);
in2.close();

byte[] output = new byte[music1.length];

audioTrack.play();

for(int i=0; i < output.length; i++){

float samplef1 = music1[i] / 128.0f; // 2^7=128
float samplef2 = music2[i] / 128.0f;


float mixed = samplef1 + samplef2;
// reduce the volume a bit:
mixed *= 0.8;
// hard clipping
if (mixed > 1.0f) mixed = 1.0f;

if (mixed < -1.0f) mixed = -1.0f;

byte outputSample = (byte)(mixed * 128.0f);
output[i] = outputSample;

} //for loop
audioTrack.write(output, 0, output.length);

}

关于android - Android AudioTrack 混音示例代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15106603/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com