苹果ios音频的回声消除处理
来源:互联网 发布:西安金象网络 编辑:程序博客网 时间:2024/04/30 19:45
工业上的声音处理中,回声消除是一个重要的话题,重要性不亚于噪声消除、人声放大、自动增益等,尤其是在VoIP功能上,回声消除是每一个做VoIP功能团队的必修课。QQ、Skype等等,回声消除的效果是一个重要的考查指标。
具体的回声消除算法比较复杂,我现在还没有研究的很明白。简单来说,就是在即将播放出来的声音中,将回声的那部分减去。其中一个关键,是如何估计回声大小,这需要用到自适应算法。研究不透,多说无益。有兴趣的同学可以一起学习。
Apple在Core Audio中提供了回声消除的接口,我写了一个测试APP,测试了其效果。链接:https://github.com/lixing123/iOSEchoCancellation
下面讲一下如何实现。
将声音输出route到speaker,这样声音比较大,回声明显:
AVAudioSession* session = [AVAudioSession sharedInstance];[session overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:nil];[session setActive:YES error:nil];
初始化一个AUGraph,创建一个AUNode,并将之添加到graph上。一般来说,沟通麦克风/扬声器的AUNode,其类型应该是RemoteIO,但是RemoteIO不带回声消除功能,VoiceProcessingIO类型的才带。
AudioComponentDescription inputcd = {0};inputcd.componentType = kAudioUnitType_Output;//inputcd.componentSubType = kAudioUnitSubType_RemoteIO;//we can access the system's echo cancellation by using kAudioUnitSubType_VoiceProcessingIO subtypeinputcd.componentSubType = kAudioUnitSubType_VoiceProcessingIO;inputcd.componentManufacturer = kAudioUnitManufacturer_Apple;
配置AudioUnit的属性,打开与麦克风/扬声器的连接(这个比较难以理解,可以参考Apple文档:https://developer.apple.com/library/ios/documentation/MusicAudio/Conceptual/AudioUnitHostingGuide_iOS/UsingSpecificAudioUnits/UsingSpecificAudioUnits.html),并配置client data format(仅支持Linear PCM格式);配置回调函数。
//Open input of the bus 1(input mic)UInt32 enableFlag = 1;CheckError(AudioUnitSetProperty(myStruct->remoteIOUnit, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Input, 1, &enableFlag, sizeof(enableFlag)), "Open input of bus 1 failed");//Open output of bus 0(output speaker)CheckError(AudioUnitSetProperty(myStruct->remoteIOUnit, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Output, 0, &enableFlag, sizeof(enableFlag)), "Open output of bus 0 failed");//Set up stream format for input and outputstreamFormat.mFormatID = kAudioFormatLinearPCM;streamFormat.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;streamFormat.mSampleRate = 44100;streamFormat.mFramesPerPacket = 1;streamFormat.mBytesPerFrame = 2;streamFormat.mBytesPerPacket = 2;streamFormat.mBitsPerChannel = 16;streamFormat.mChannelsPerFrame = 1;CheckError(AudioUnitSetProperty(myStruct->remoteIOUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 0, &streamFormat, sizeof(streamFormat)), "kAudioUnitProperty_StreamFormat of bus 0 failed");CheckError(AudioUnitSetProperty(myStruct->remoteIOUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 1, &streamFormat, sizeof(streamFormat)), "kAudioUnitProperty_StreamFormat of bus 1 failed");//Set up input callbackAURenderCallbackStruct input;input.inputProc = InputCallback;input.inputProcRefCon = myStruct;CheckError(AudioUnitSetProperty(myStruct->remoteIOUnit, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Global, 0,//input mic &input, sizeof(input)), "kAudioUnitProperty_SetRenderCallback failed");
在回调函数inputCallback中,用
AudioUnitRender()
函数获取麦克风的声音,存在一个bufferList中。这个bufferList是一个ring结构,存储最新的声音,然后播放旧声音。这样,声音的输入和输出之间,就有了0.5s(可调节)左右的延迟,形成了明显的回声。给回声消除添加一个开关。VoiceProcessingIO有一个属性可用来打开/关闭回声消除功能:
kAUVoiceIOProperty_BypassVoiceProcessing
UInt32 echoCancellation;UInt32 size = sizeof(echoCancellation);CheckError(AudioUnitGetProperty(myStruct.remoteIOUnit, kAUVoiceIOProperty_BypassVoiceProcessing, kAudioUnitScope_Global, 0, &echoCancellation, &size), "kAUVoiceIOProperty_BypassVoiceProcessing failed");
现在可以开始graph了:
CheckError(AUGraphInitialize(graph), "AUGraphInitialize failed");CheckError(AUGraphStart(graph), "AUGraphStart failed");
在示例中,有一个简单的开关按钮,可以明显感觉到打开/关闭回声消除的区别。
在实测中,打开回声消除功能时,仍然能听到一点点的回声,不过很小,一般情况下足够使用了。
- 苹果ios音频的回声消除处理
- 音频处理之回声消除及调试经验
- iOS设备上回声消除的例子
- android、ios、windows等平台回声消除的解决方案
- speex 回声消除的用法
- speex 回声消除的用法
- 一种改进的回声消除
- 回声消除
- 回声消除
- 基于Speex的声学回声消除
- 回声消除的原理和应用场景
- speex 回声消除的用法【转】
- 即时通讯的语音回声消除技术
- 回声消除的原理和应用场景
- 基于Speex的声学回声消除
- 回声消除的原理和应用场景
- 回声消除技术
- 回声消除原理
- 数据类型---JavaScript
- html attributes properties
- 单例模式-基类单例。。用于其他单例的派生
- HDU1232-通畅工程
- POJ - 1016 - Prime Ring Problem(dfs)
- 苹果ios音频的回声消除处理
- [读书笔记]Android中Animation的Interpolator插值器详解(图文)
- h5环信保存离线的音频问题
- iOS开发-清理缓存功能的实现
- js打印顺序和倒序乘法口诀
- android 或者java中使用四舍五入的方法
- Codeforces Round #370 (Div. 2)
- chrome调试js的小技巧
- 使用 WSDL 2.0 描述 REST Web 服务