苹果ios音频的回声消除处理

来源:互联网 发布:西安金象网络 编辑:程序博客网 时间:2024/04/30 19:45

工业上的声音处理中,回声消除是一个重要的话题,重要性不亚于噪声消除、人声放大、自动增益等,尤其是在VoIP功能上,回声消除是每一个做VoIP功能团队的必修课。QQ、Skype等等,回声消除的效果是一个重要的考查指标。

具体的回声消除算法比较复杂,我现在还没有研究的很明白。简单来说,就是在即将播放出来的声音中,将回声的那部分减去。其中一个关键,是如何估计回声大小,这需要用到自适应算法。研究不透,多说无益。有兴趣的同学可以一起学习。

Apple在Core Audio中提供了回声消除的接口,我写了一个测试APP,测试了其效果。链接:https://github.com/lixing123/iOSEchoCancellation 
下面讲一下如何实现。

  1. 将声音输出route到speaker,这样声音比较大,回声明显:

    AVAudioSession* session = [AVAudioSession sharedInstance];[session overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:nil];[session setActive:YES error:nil];
  2. 初始化一个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;
  3. 配置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");
  4. 在回调函数inputCallback中,用AudioUnitRender() 函数获取麦克风的声音,存在一个bufferList中。这个bufferList是一个ring结构,存储最新的声音,然后播放旧声音。这样,声音的输入和输出之间,就有了0.5s(可调节)左右的延迟,形成了明显的回声。

  5. 给回声消除添加一个开关。VoiceProcessingIO有一个属性可用来打开/关闭回声消除功能:kAUVoiceIOProperty_BypassVoiceProcessing

    UInt32 echoCancellation;UInt32 size = sizeof(echoCancellation);CheckError(AudioUnitGetProperty(myStruct.remoteIOUnit,                            kAUVoiceIOProperty_BypassVoiceProcessing,                            kAudioUnitScope_Global,                            0,                            &echoCancellation,                            &size),       "kAUVoiceIOProperty_BypassVoiceProcessing failed");
  6. 现在可以开始graph了:

    CheckError(AUGraphInitialize(graph),       "AUGraphInitialize failed");CheckError(AUGraphStart(graph),       "AUGraphStart failed");

    在示例中,有一个简单的开关按钮,可以明显感觉到打开/关闭回声消除的区别。

    在实测中,打开回声消除功能时,仍然能听到一点点的回声,不过很小,一般情况下足够使用了。

0 0
原创粉丝点击