Java-android audioRecord-应用变化的增益
作者:互联网
我想将增益应用于录音(PCM 16位).为此,我有以下代码:
for (int i=0; i<buffer.length/2; i++)
{ // 16bit sample size
short curSample = getShort(buffer[i*2], buffer[i*2+1]);
if(rGain != 1){
//apply gain
curSample *= rGain;
//convert back from short sample that was "gained" to byte data
byte[] a = getByteFromShort(curSample);
buffer[i*2] = a[0];
buffer[i*2 + 1] = a[1];
}
如果像这样应用(将每个样本乘以分数),则播放时会停止播放(像旧的对讲机一样).是否有一些公式可以改变每个样本的增益因子?我假设样本范围内有一些maxValue和minValue(我猜是[-32768,32767]),并且在某些公式中使用这些值,我可以获得一个可变增益因子以应用于当前样本.
//编辑:
添加
if (curSample>32767) {curSample=32767;}
if (curSample<-32768) {curSample=-32768;}
完整方法
aRecorder.read(buffer, 0, buffer.length);
for (int i=0; i<buffer.length/2; i++)
{ // 16bit sample size
short curSample = getShort(buffer[i*2], buffer[i*2+1]);
if(rGain != 1){
//apply gain
curSample *= rGain;
if (curSample>32767) {curSample=32767;}
if (curSample<-32768) {curSample=-32768;}
//convert back from short sample that was "gained" to byte data
byte[] a = getByteFromShort(curSample);
buffer[i*2] = a[0];
buffer[i*2 + 1] = a[1];
}
但是仍然听到奇怪的声音(噪音像旧的对讲机一样中断).
任何帮助,将不胜感激,
谢谢.
解决方法:
您的源代码中还有另一个错误.以下行从-32768..32767创建样本值,这是s短变量的完整范围:
short curSample = getShort(buffer[i*2], buffer[i*2+1]);
现在,当您应用大于1的增益因子时,您会“溢出”简短格式:
curSample *= rGain;
这会在平滑信号中产生令人讨厌的裂纹,例如32767 * 1.5不是预期的49150,但是由于将“溢出”解释为-16386,因为您将结果再次分配给short变量.
因此这两行
if (curSample>32767) {curSample=32767;}
if (curSample<-32768) {curSample=-32768;}
不会更改任何内容,因为curSample永远不会大于32767或小于-32768.
为了避免这种情况,您必须使用一个临时的int变量:
short curSample = getShort(buffer[i*2], buffer[i*2+1]);
int temp = curSample * rGain;
if (temp>=32767)
curSample=32767;
else if (temp<=-32768)
curSample=-32768;
else
curSample=(short)temp;
标签:fft,audio-recording,audio,java,android 来源: https://codeday.me/bug/20191101/1982850.html