Android的应用audioRecord-与收益的变化 [英] android audioRecord- apply gain with variation

查看:149
本文介绍了Android的应用audioRecord-与收益的变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想申请增益我录音(PCM 16位)。为此,我有以下的code:

I want to apply gain to my recordings(PCM 16bit). For this I have the following code:

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]),并在某些公式使用这些值我能到适用于当前的样品variated增益因子。

If applied like this(multiplying each sample with the fraction number), I get discontinues when playback(hearing like an old walkie-talkie). Is there some formula to vary my gain factor on each sample? I assume there is some maxValue and minValue for the range of samples (I guess [-32768, +32767]) and using these values in some formula I can get a variated gain factor to apply to the current sample.

//编辑:
添加

// added

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];
                        }

不过还是听到奇(噪声+中止像一个老对讲机)。

But still hears odd(noise + discontinues like an old walkie-talkie).

任何帮助将是AP preciated,

Any help would be appreciated,

感谢。

推荐答案

您在源另一个bug。下面一行从-32768..32767这是全方位的短篇小说变量创建样本值:

You have another bug in your source. The following line creates sample values from -32768..32767 which is the full range of s short variable:

short curSample = getShort(buffer[i*2], buffer[i*2+1]);

由于您立即应用的增益系数比磨碎1你溢出的格式:

curSample *= rGain;

这会产生讨厌的裂纹在光滑的信号,如例如32767 * 1.5是不是49150不如预期,但由于溢出是因为你再次分配结果到变量作为PTED间-16386 $ P $

This produces nasty cracks in the smooth signal, as e.g. 32767 * 1.5 is not 49150 as expected, but due to the "overflow" is interpreted as -16386 because you assign the result to a short variable again.

因此​​,两行

if (curSample>32767) {curSample=32767;}
if (curSample<-32768) {curSample=-32768;}

作为curSample大于32767 -32768比从未较大或较小

不会改变任何事情。

wouldn't change anything as curSample is never greater than 32767 or smaller than -32768.

要避免这一点,你必须使用一个临时 INT 变量:

To avoid this you have to use a temporary int variable:

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;

这篇关于Android的应用audioRecord-与收益的变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆