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

iOS设备上回声消除的例子

 

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

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

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

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

     
    1. AVAudioSession* session = [AVAudioSession sharedInstance];

    2. [session overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:nil];

    3. [session setActive:YES error:nil];

  2. 初始化一个AUGraph,创建一个AUNode,并将之添加到graph上。一般来说,沟通麦克风/扬声器的AUNode,其类型应该是RemoteIO,但是RemoteIO不带回声消除功能,VoiceProcessingIO类型的才带。

     
    1. AudioComponentDescription inputcd = {0};

    2. inputcd.componentType = kAudioUnitType_Output;

    3. //inputcd.componentSubType = kAudioUnitSubType_RemoteIO;

    4. //we can access the system's echo cancellation by using kAudioUnitSubType_VoiceProcessingIO subtype

    5. inputcd.componentSubType = kAudioUnitSubType_VoiceProcessingIO;

    6. 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格式);配置回调函数。

     
    1. //Open input of the bus 1(input mic)

    2. UInt32 enableFlag = 1;

    3. CheckError(AudioUnitSetProperty(myStruct->remoteIOUnit,

    4. kAudioOutputUnitProperty_EnableIO,

    5. kAudioUnitScope_Input,

    6. 1,

    7. &enableFlag,

    8. sizeof(enableFlag)),

    9. "Open input of bus 1 failed");

    10.  
    11. //Open output of bus 0(output speaker)

    12. CheckError(AudioUnitSetProperty(myStruct->remoteIOUnit,

    13. kAudioOutputUnitProperty_EnableIO,

    14. kAudioUnitScope_Output,

    15. 0,

    16. &enableFlag,

    17. sizeof(enableFlag)),

    18. "Open output of bus 0 failed");

    19.  
    20. //Set up stream format for input and output

    21. streamFormat.mFormatID = kAudioFormatLinearPCM;

    22. streamFormat.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;

    23. streamFormat.mSampleRate = 44100;

    24. streamFormat.mFramesPerPacket = 1;

    25. streamFormat.mBytesPerFrame = 2;

    26. streamFormat.mBytesPerPacket = 2;

    27. streamFormat.mBitsPerChannel = 16;

    28. streamFormat.mChannelsPerFrame = 1;

    29.  
    30. CheckError(AudioUnitSetProperty(myStruct->remoteIOUnit,

    31. kAudioUnitProperty_StreamFormat,

    32. kAudioUnitScope_Input,

    33. 0,

    34. &streamFormat,

    35. sizeof(streamFormat)),

    36. "kAudioUnitProperty_StreamFormat of bus 0 failed");

    37.  
    38. CheckError(AudioUnitSetProperty(myStruct->remoteIOUnit,

    39. kAudioUnitProperty_StreamFormat,

    40. kAudioUnitScope_Output,

    41. 1,

    42. &streamFormat,

    43. sizeof(streamFormat)),

    44. "kAudioUnitProperty_StreamFormat of bus 1 failed");

    45.  
    46. //Set up input callback

    47. AURenderCallbackStruct input;

    48. input.inputProc = InputCallback;

    49. input.inputProcRefCon = myStruct;

    50. CheckError(AudioUnitSetProperty(myStruct->remoteIOUnit,

    51. kAudioUnitProperty_SetRenderCallback,

    52. kAudioUnitScope_Global,

    53. 0,//input mic

    54. &input,

    55. sizeof(input)),

    56. "kAudioUnitProperty_SetRenderCallback failed");

  4. 在回调函数inputCallback中,用AudioUnitRender() 函数获取麦克风的声音,存在一个bufferList中。这个bufferList是一个ring结构,存储最新的声音,然后播放旧声音。这样,声音的输入和输出之间,就有了0.5s(可调节)左右的延迟,形成了明显的回声。

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

     
    1. UInt32 echoCancellation;

    2. UInt32 size = sizeof(echoCancellation);

    3. CheckError(AudioUnitGetProperty(myStruct.remoteIOUnit,

    4. kAUVoiceIOProperty_BypassVoiceProcessing,

    5. kAudioUnitScope_Global,

    6. 0,

    7. &echoCancellation,

    8. &size),

    9. "kAUVoiceIOProperty_BypassVoiceProcessing failed");

  6. 现在可以开始graph了:

     
    1. CheckError(AUGraphInitialize(graph),

    2. "AUGraphInitialize failed");

    3. CheckError(AUGraphStart(graph),

    4. "AUGraphStart failed");

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

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

 

 

 

工业上的声音处理中,回声消除是一个重要的话题,重要性不亚于噪声消除、人声放大、自动增益等,尤其是在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];
    
    • 1
    • 2
    • 3
  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 subtype
    inputcd.componentSubType = kAudioUnitSubType_VoiceProcessingIO;
    inputcd.componentManufacturer = kAudioUnitManufacturer_Apple;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
  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 output
    streamFormat.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 callback
    AURenderCallbackStruct input;
    input.inputProc = InputCallback;
    input.inputProcRefCon = myStruct;
    CheckError(AudioUnitSetProperty(myStruct->remoteIOUnit,
                                kAudioUnitProperty_SetRenderCallback,
                                kAudioUnitScope_Global,
                                0,//input mic
                                &input,
                                sizeof(input)),
           "kAudioUnitProperty_SetRenderCallback failed");
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
  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");
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
  6. 现在可以开始graph了:

    CheckError(AUGraphInitialize(graph),
           "AUGraphInitialize failed");
    CheckError(AUGraphStart(graph),
           "AUGraphStart failed");
    
    • 1
    • 2
    • 3
    • 4

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

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

 

iOS 音频-AVAudioSession
iOS音频掌柜-- AVAudioSession

在WebRTC应用中,AudioUnit 使用的是Voice-Processing I / O unit (subtype kAudioUnitSubType_VoiceProcessingIO),使用内置的aec和agc等功能, AVAudioSession状态:
Category = AVAudioSessionCategoryPlayAndRecord,Mode = AVAudioSessionModeVoiceChat,无须显式的setMode,使用 Voice-Processing I / O unit 会自动切换为 VoiceChat,至于Options依实际业务情景需求设置。


  1. Category 切换成 AVAudioSessionCategoryPlayback:
    那么AudioUnit record cb 有回调,但是获取到的是静音数据。
  2. 使用AVAudioPlayer播放音频文件,音量非常低:
    可以通过调整mode解决, 播放音频文件时设置为 AVAudioSessionModeDefault,待播放完毕之后再设置为 AVAudioSessionModeVoiceChat。但是这个缺点是,切换成 default mode 后 失去了回音消除功能了。
    然而,像狼人杀的场景,需要一直播放背景音乐,又需要对话过程中回音消除,那么就需要好好的维护好 mode 和 option了。

 

0人点赞

 

日记本

 



作者:暴走大牙
链接:https://www.jianshu.com/p/5addd678c64e
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值