zoukankan      html  css  js  c++  java
  • 苹果ios音频的回声消除处理

    https://blog.csdn.net/h156144206/article/details/52503664

    工业上的声音处理中,回声消除是一个重要的话题,重要性不亚于噪声消除、人声放大、自动增益等,尤其是在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");

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

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

  • 相关阅读:
    maven完成构建后,eclipse导入运行maven web
    maven构建java项目的过程【完全】
    maven配置【转载】
    iOS - 移动设备防丢失App
    iOS
    iOS
    iOS
    iOS
    iOS
    iOS
  • 原文地址:https://www.cnblogs.com/itlover2013/p/14414247.html
Copyright © 2011-2022 走看看