Clicking/distortion 混合两个音频缓冲区时
Clicking/distortion when mixing two audio buffers
我正在开发一个需要同步声音的 android 音频应用程序。
我正在尝试合并两个声音缓冲器,但在大幅度时出现失真。
这就是我正在做的:
for(int i=0;i<data2.length;i+=2)
{
short buf1a = data[i+1];
short buf2a = data[i];
buf1a = (short)((buf1a & 0xff) << 8);
buf2a = (short) (buf2a & 0xff);
short buf1b = data2[i+1];
short buf2b = data2[i];
buf1b = (short) ((buf1b & 0xff) << 8);
buf2b = (short) (buf2b & 0xff);
short buf1c = (short) (buf1a + buf1b);
short buf2c = (short) (buf2a + buf2b);
short res = (short) (buf1c + buf2c);
int res2 = res/2;
res = (short)res2;
data3[i]=(byte)res;
data3[i+1]=(byte)(res>>8);
}
缓冲区使用以下方式播放:
AudioTrack at = new AudioTrack(STREAM_MUSIC,44100,CHANNEL_OUT_MONO,ENCODING_PCM_16BIT,dataSize,MODE_STATIC);
at.write(data3,0,data3.length);
int frames = data3.length/2; //2 bytes per frame.
Log.d(TAG,"this is data length: "+data3.length);
Log.d(TAG,"this is assumed frame number:"+frames);
at.setLoopPoints(0,frames,3);
at.play();
我几乎完全按照此处概述的程序进行操作: .
缓冲区 data 和 data2 包含我要混合的 wav 文件中的数据。使用 AudioTrack 单独播放它们时,它们听起来不错。此外,除了 "high-amplitude-clicks" 之外,混音听起来还不错。
我假设问题是短值对于最大振幅来说太大了,但我不知道为什么,因为我除以 2。真的很高兴有任何想法。
更新:我将混音输出为 wav 并在 Audacity 中查看。
下面的顶部波形是我与点击的混合。底部的波形是 Audacity 混合两个 wav 并且当波形触及图表的 'roof'/'floor' 时在我的混音中没有 clicks.The 的点击。
我的混音在这些地方似乎有更宽的峰值。仍然没有解决这个问题。
更新 2:
这就是问题区域近距离观察的样子。它看起来在 Audacity 版本中它被切割成一个 highest/lowest 值(floor/roof),但在我的版本中它似乎跳到另一边并且 'finish' 它的曲线在那里。
我通过检查大数字是否突然改变符号设法摆脱了剪辑。下面添加在 for 循环末尾的代码似乎可以完成这项工作,并给出类似于上述问题中大胆图的结果。
if(res>10000) //Avoid 'normal' cases where amplitude shifts from f.ex. 4 to -2, which we want to keep.
{
if((res*resPrevious)<0) //If the sign has changed suddenly for a large number, use the previous number.
{
Log.d(TAG,"res:"+res+"");
res = resPrevious;
}
}
if(res<-10000)
{
if((res*resPrevious)<0) //If the sign has changed suddenly for a large number, use the previous number.
{
res = resPrevious;
}
}
resPrevious=res;
data3[i] = (byte) res;
data3[i + 1] = (byte) (res >> 8);
你的代码启发了我,最终我遇到了同样的问题。您将 16 字节短 buf1a
、buf1b
、buf2a
、buf2b
值一个一个地添加到另一个,然后将结果转换为 16 字节短 buf1c
和 buf2c
。当加法结果小于 -32,768
或大于 32,767
a loss conversation occours 因为它超过了 short
数据类型容量...
Oracle 文档摘录:
A narrowing primitive conversion may lose information about the overall magnitude of a numeric value and may also lose precision and range.
short buf1c = (short) (buf1a + buf1b);
short buf2c = (short) (buf2a + buf2b);
我的简单修改:)。这完美无任何失真:
int buf1c = (buf1a + buf1b)/2;
int buf2c = (buf2a + buf2b)/2;
我正在开发一个需要同步声音的 android 音频应用程序。
我正在尝试合并两个声音缓冲器,但在大幅度时出现失真。 这就是我正在做的:
for(int i=0;i<data2.length;i+=2)
{
short buf1a = data[i+1];
short buf2a = data[i];
buf1a = (short)((buf1a & 0xff) << 8);
buf2a = (short) (buf2a & 0xff);
short buf1b = data2[i+1];
short buf2b = data2[i];
buf1b = (short) ((buf1b & 0xff) << 8);
buf2b = (short) (buf2b & 0xff);
short buf1c = (short) (buf1a + buf1b);
short buf2c = (short) (buf2a + buf2b);
short res = (short) (buf1c + buf2c);
int res2 = res/2;
res = (short)res2;
data3[i]=(byte)res;
data3[i+1]=(byte)(res>>8);
}
缓冲区使用以下方式播放:
AudioTrack at = new AudioTrack(STREAM_MUSIC,44100,CHANNEL_OUT_MONO,ENCODING_PCM_16BIT,dataSize,MODE_STATIC);
at.write(data3,0,data3.length);
int frames = data3.length/2; //2 bytes per frame.
Log.d(TAG,"this is data length: "+data3.length);
Log.d(TAG,"this is assumed frame number:"+frames);
at.setLoopPoints(0,frames,3);
at.play();
我几乎完全按照此处概述的程序进行操作:
缓冲区 data 和 data2 包含我要混合的 wav 文件中的数据。使用 AudioTrack 单独播放它们时,它们听起来不错。此外,除了 "high-amplitude-clicks" 之外,混音听起来还不错。
我假设问题是短值对于最大振幅来说太大了,但我不知道为什么,因为我除以 2。真的很高兴有任何想法。
更新:我将混音输出为 wav 并在 Audacity 中查看。
下面的顶部波形是我与点击的混合。底部的波形是 Audacity 混合两个 wav 并且当波形触及图表的 'roof'/'floor' 时在我的混音中没有 clicks.The 的点击。
我的混音在这些地方似乎有更宽的峰值。仍然没有解决这个问题。
我通过检查大数字是否突然改变符号设法摆脱了剪辑。下面添加在 for 循环末尾的代码似乎可以完成这项工作,并给出类似于上述问题中大胆图的结果。
if(res>10000) //Avoid 'normal' cases where amplitude shifts from f.ex. 4 to -2, which we want to keep.
{
if((res*resPrevious)<0) //If the sign has changed suddenly for a large number, use the previous number.
{
Log.d(TAG,"res:"+res+"");
res = resPrevious;
}
}
if(res<-10000)
{
if((res*resPrevious)<0) //If the sign has changed suddenly for a large number, use the previous number.
{
res = resPrevious;
}
}
resPrevious=res;
data3[i] = (byte) res;
data3[i + 1] = (byte) (res >> 8);
你的代码启发了我,最终我遇到了同样的问题。您将 16 字节短 buf1a
、buf1b
、buf2a
、buf2b
值一个一个地添加到另一个,然后将结果转换为 16 字节短 buf1c
和 buf2c
。当加法结果小于 -32,768
或大于 32,767
a loss conversation occours 因为它超过了 short
数据类型容量...
Oracle 文档摘录:
A narrowing primitive conversion may lose information about the overall magnitude of a numeric value and may also lose precision and range.
short buf1c = (short) (buf1a + buf1b);
short buf2c = (short) (buf2a + buf2b);
我的简单修改:)。这完美无任何失真:
int buf1c = (buf1a + buf1b)/2;
int buf2c = (buf2a + buf2b)/2;