Examples of echo cancellation on iOS devices

Source: Internet
Author: User

In industrial sound processing, echo cancellation is an important topic, as much as noise elimination, vocal amplification, automatic gain, etc., especially in VoIP functions, echo cancellation is a required course for every VoIP function team. QQ, Skype and so on, the effect of echo cancellation is an important test indicator.

The specific echo cancellation algorithm is more complex, and I have not studied it very clearly. In short, the part of the ECHO is subtracted from the sound that is about to be played. One of the keys is how to estimate the echo size, which requires an adaptive algorithm. Research is not enough, it is useless to say. Students who are interested can study together.

Apple provides an echo-cancellation interface in core audio, and I've written a test app to test its effects. Links: https://github.com/lixing123/iOSEchoCancellation
Here's how it's implemented.

  1. The sound output route to speaker, so that the sound is larger, the echo is obvious:

    AVAudioSession* session = [AVAudioSession sharedInstance];[session overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:nil];[session setActive:YES error:nil];
  2. Initializes a augraph, creates a aunode, and adds it to the graph. Generally speaking, the aunode of the microphone/speaker, the type should be Remoteio, but Remoteio without the echo cancellation function, Voiceprocessingio type of tape.

    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. Configure the properties of the AudioUnit to open the connection to the microphone/speaker (This is more difficult to understand, you can refer to the Apple documentation: https://developer.apple.com/library/ios/documentation/ musicaudio/conceptual/audiounithostingguide_ios/usingspecificaudiounits/ usingspecificaudiounits.html), and configure client data format (linear PCM format only); Configure callback functions.

    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, &enablefla G, sizeof (Enableflag)), "Open output of bus 0 failed");//set up stream format for input a nd 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 Inpu T 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. In the callback function Inputcallback, a function is used to AudioUnitRender() get the sound of the microphone, which exists in a bufferlist. This bufferlist is a ring structure that stores the latest sound and then plays the old sound. In this way, there is a 0.5s (adjustable) delay between the input and output of the sound, resulting in a noticeable echo.

  5. Add a switch to echo cancellation. The Voiceprocessingio has an attribute that can be used to turn echo cancellation on/off:kAUVoiceIOProperty_BypassVoiceProcessing

    UInt32 echoCancellation;UInt32 size = sizeof(echoCancellation);CheckError(AudioUnitGetProperty(myStruct.remoteIOUnit,                            kAUVoiceIOProperty_BypassVoiceProcessing,                            kAudioUnitScope_Global,                            0,                            &echoCancellation,                            &size),       "kAUVoiceIOProperty_BypassVoiceProcessing failed");
  6. Now it's time to start graph:

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

    In the example, there is a simple switch button that can visibly feel the difference between turning on/off echo cancellation.

    In the actual measurement, when the echo cancellation function is turned on, a little echo can still be heard, but it is small enough to be used in general.

Examples of echo cancellation on iOS devices

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.