iOS 怎么对声音滤波 放大

风Elen 2015-02-11 02:41:22
最近在进行声音采集和实时回放的开发,参数为16位单通道,但采集到的声音比较小,而且有噪音于是在回放函数
playbackCallback(void *inRefCon,
AudioUnitRenderActionFlags *ioActionFlags,
const AudioTimeStamp *inTimeStamp,
UInt32 inBusNumber,
UInt32 inNumberFrames,
AudioBufferList *ioData)
中对数据流进行了放大和滤波,得到的结果是如果只滤波或放大,效果明显,同时进行滤波和放大则声音完全变成了噪音。如果只滤波,得到的声音还是太小,只放大,则声音和噪音一起放大了,求指点

高通滤波函数
- (float)HighFilter:(float)data
{
if (m_HFParam>0)//highfilter
{
HFBuf[2]=HFBuf[1];
HFBuf[1]=HFBuf[0];

HFBuf[5]=HFBuf[4];
HFBuf[4]=HFBuf[3];
HFBuf[3]=data;
HFBuf[0]=HFBuf[3]*HFA0+HFBuf[4]*HFA1+HFBuf[5]*HFA2-HFBuf[1]*HFB1-HFBuf[2]*HFB2;
return HFBuf[0];
}
else
return data;
}

低通滤波函数
- (float)LowFilter:(float)data
{
if (m_LFParam>0)//lowfilter
{
LFBuf[3]=LFBuf[2];
LFBuf[2]=LFBuf[1];
LFBuf[1]=LFBuf[0];

LFBuf[7]=LFBuf[6];
LFBuf[6]=LFBuf[5];
LFBuf[5]=LFBuf[4];
LFBuf[4]=data;
LFBuf[0]=LFBuf[4]*LFA0+LFBuf[5]*LFA1+LFBuf[6]*LFA2+LFBuf[7]*LFA3-LFBuf[1]*LFB1-LFBuf[2]*LFB2-LFBuf[3]*LFB3;

return LFBuf[0];
}
else
return data;
}

放大函数
-(void)processBuffer: (AudioBufferList*) audioBufferList
{
AudioBuffer sourceBuffer = audioBufferList->mBuffers[0];

// we check here if the input data byte size has changed
if (audioBuffer.mDataByteSize != sourceBuffer.mDataByteSize) {
// clear old buffer
free(audioBuffer.mData);
// assing new byte size and allocate them on mData
audioBuffer.mDataByteSize = sourceBuffer.mDataByteSize;
audioBuffer.mData = malloc(sourceBuffer.mDataByteSize);
}

/**
Here we modify the raw data buffer now.
In my example this is a simple input volume gain.
iOS 5 has this on board now, but as example quite good.
*/
SInt16 *editBuffer = audioBufferList->mBuffers[0].mData;
//float *pointer = audioBufferList->mBuffers[0].mData;

for (int index = 0; index < audioBufferList->mBuffers[0].mDataByteSize/2;index++)
{
double sample = ((double)editBuffer[index]) / 32767.0;

sample = [self.filter HighFilter:sample];
sample = [self.filter LowFilter:sample];
sample *= 32767.0;

editBuffer[index] = (SInt16)sample;
}

// loop over every packet
for (int nb = 0; nb < (audioBufferList->mBuffers[0].mDataByteSize / 2); nb++) {

// we check if the gain has been modified to save resoures
if (gain != 0) {
// we need more accuracy in our calculation so we calculate with doubles
double gainSample = ((double)editBuffer[nb]) / 32767.0;

/*
at this point we multiply with our gain factor
we dont make a addition to prevent generation of sound where no sound is.

no noise
0*10=0

noise if zero
0+10=10
*/
gainSample *= gain;

/**
our signal range cant be higher or lesser -1.0/1.0
we prevent that the signal got outside our range
*/
gainSample = (gainSample < -1.0) ? -1.0 : (gainSample > 1.0) ? 1.0 : gainSample;

/*
This thing here is a little helper to shape our incoming wave.
The sound gets pretty warm and better and the noise is reduced a lot.
Feel free to outcomment this line and here again.

You can see here what happens here http://silentmatt.com/javascript-function-plotter/
Copy this to the command line and hit enter: plot y=(1.5*x)-0.5*x*x*x
*/

gainSample = (1.5 * gainSample) - 0.5 * gainSample * gainSample * gainSample;

// multiply the new signal back to short
gainSample = gainSample * 32767.0;

// write calculate sample back to the buffer
editBuffer[nb] = (SInt16)gainSample;
}
}
// copy incoming audio data to the audio buffer
memcpy(audioBuffer.mData, audioBufferList->mBuffers[0].mData, audioBufferList->mBuffers[0].mDataByteSize);
}
...全文
187 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

29,029

社区成员

发帖
与我相关
我的任务
社区描述
主要讨论与iOS相关的软件和技术
社区管理员
  • iOS
  • 大熊猫侯佩
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧