我想对我的录音应用增益(PCM 16bit)。为此,我有以下代码:
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];
}
但仍然听到奇怪的声音(噪音 + 像旧对讲机一样中断)。
任何帮助,将不胜感激,
谢谢。