2

I'm trying to do active noise cancellation in iOS using remote I/O. I have been able to get the audio buffer in 8.24 fixed point format and put it in the speaker as well. Right now I'm trying to capture a sinusoidal wave through microphone (using onlinetonegenerator.com) and reversing the magnitude in each frame I'm getting through the callback. Here goes my code:

static OSStatus PerformThru(
                        void                        *inRefCon, 
                        AudioUnitRenderActionFlags  *ioActionFlags, 
                        const AudioTimeStamp        *inTimeStamp, 
                        UInt32                      inBusNumber,
                        UInt32                      inNumberFrames, 
                        AudioBufferList             *ioData) {

 AppDelegate *THIS = (AppDelegate *)inRefCon;
 OSStatus err = AudioUnitRender(THIS->rioUnit, ioActionFlags, inTimeStamp, 1, inNumberFrames, ioData);

 for(int i = 0; i < ioData->mNumberBuffers; ++i) {
     Float32 *flptr = (Float32 *)ioData->mBuffers[i].mData;       
     for(int j = 0; j < inNumberFrames; ++j) {
         *flptr *= -1.;   // inverting the buffer value
         flptr++;
     }
 }

 return err;
}

But the output tone doesn't seem to create a destructive interference. I'm sensing that when I'm running the app there is a periodic change of amplitude but it is not canceling the input sound.

I think there might be 2 more factors:

  1. Latency in generating the output stream from microphone stream
  2. Difference of amplitude between original sound and generated sound

Any idea how can I take care of these issues in AudioUnit?

Thanks a lot!

Michael
  • 57,169
  • 9
  • 80
  • 125
rushafi
  • 863
  • 10
  • 19

1 Answers1

0

It seems you use kAudioUnitSubType_RemoteIO. Did you try to use kAudioUnitSubType_VoiceProcessingIO? May be it will help.

This audio unit does signal processing on the incoming audio (taking out any of the audio that is played from the device at a given time from the incoming audio).

  • Yes I'm using kAudioUnitSubType_RemoteIO. I didn't use kAudioUnitSubType_VoiceProcessingIO because I don't actually want to clean up the noises in the incoming audio. Rather I want to generate a 180 degree out of phase sound wave. But I'll will give a try with kAudioUnitSubType_VoiceProcessingIO. Thanks for your answer. – rushafi Feb 18 '14 at 05:29
  • I tried kAudioUnitSubType_VoiceProcessingIO, it destroyed the voice, broken and robotic sounding. I do not recommend. – trampster Jul 15 '20 at 07:19