title: how to setup Effect Audio Unit in iOS
body: <p>My task is to play an audio file that is saved locally in documents directory, apply audio effect in that audio file using Effect Audio Unit and save a new audio file in documents directory with that effect.
  Here is my code that i have written so far, but its not working. Effects are not being applied in the audio. Please suggest me what is wrong in this code ?? Thanks in advance..</p>
  
  <pre><code>- (void) setUpAudioUnits
  {
  OSStatus setupErr = noErr;
  
  // describe unit
  AudioComponentDescription audioCompDesc;
  audioCompDesc.componentType = kAudioUnitType_Output;
  audioCompDesc.componentSubType = kAudioUnitSubType_RemoteIO;
  audioCompDesc.componentManufacturer = kAudioUnitManufacturer_Apple;
  audioCompDesc.componentFlags = 0;
  audioCompDesc.componentFlagsMask = 0;
  
  // get rio unit from audio component manager
  AudioComponent rioComponent = AudioComponentFindNext(NULL, &amp;audioCompDesc);
  setupErr = AudioComponentInstanceNew(rioComponent, &amp;remoteIOUnit);
  NSAssert (setupErr == noErr, @"Couldn't get RIO unit instance");
  
  // set up the rio unit for playback
  UInt32 oneFlag = 1;
  AudioUnitElement outputElement = 0;
  setupErr = 
  AudioUnitSetProperty (remoteIOUnit,
                        kAudioOutputUnitProperty_EnableIO,
                        kAudioUnitScope_Output,
                        outputElement,
                        &amp;oneFlag,
                        sizeof(oneFlag));
  NSAssert (setupErr == noErr, @"Couldn't enable RIO output");
  
  // enable rio input
  AudioUnitElement inputElement = 1;
  
  // setup an asbd in the iphone canonical format
  AudioStreamBasicDescription myASBD;
  memset (&amp;myASBD, 0, sizeof (myASBD));
  // myASBD.mSampleRate = 44100;
  myASBD.mSampleRate = hardwareSampleRate;
  myASBD.mFormatID = kAudioFormatLinearPCM;
  myASBD.mFormatFlags = kAudioFormatFlagsCanonical;
  myASBD.mBytesPerPacket = 4;
  myASBD.mFramesPerPacket = 1;
  myASBD.mBytesPerFrame = 4;
  myASBD.mChannelsPerFrame = 2;
  myASBD.mBitsPerChannel = 16;
  
  /*
   // set format for output (bus 0) on rio's input scope
   */
  setupErr =
  AudioUnitSetProperty (remoteIOUnit,
                        kAudioUnitProperty_StreamFormat,
                        kAudioUnitScope_Input,
                        outputElement,
                        &amp;myASBD,
                        sizeof (myASBD));
  NSAssert (setupErr == noErr, @"Couldn't set ASBD for RIO on input scope / bus 0");
  
  
  // song must be an LPCM file, preferably in caf container
  // to convert, use /usr/bin/afconvert, like this:
  //  /usr/bin/afconvert --data LEI16 Girlfriend.m4a song.caf
  
  // read in the entire audio file (NOT recommended)
  // better to use a ring buffer: thread or timer fills, render callback drains
  NSURL *songURL = [NSURL fileURLWithPath:
                    [[NSBundle mainBundle] pathForResource: @"song"
                                                    ofType: @"caf"]];
  AudioFileID songFile;
  setupErr = AudioFileOpenURL((CFURLRef) songURL,
                              kAudioFileReadPermission,
                              0,
                              &amp;songFile);
  NSAssert (setupErr == noErr, @"Couldn't open audio file");
  
  UInt64 audioDataByteCount;
  UInt32 audioDataByteCountSize = sizeof (audioDataByteCount);
  setupErr = AudioFileGetProperty(songFile,
                                  kAudioFilePropertyAudioDataByteCount,
                                  &amp;audioDataByteCountSize,
                                  &amp;audioDataByteCount);
  NSAssert (setupErr == noErr, @"Couldn't get size property");
  
  musicPlaybackState.audioData = malloc (audioDataByteCount);
  musicPlaybackState.audioDataByteCount = audioDataByteCount;
  musicPlaybackState.samplePtr = musicPlaybackState.audioData;
  
  NSLog (@"reading %qu bytes from file", audioDataByteCount);
  UInt32 bytesRead = audioDataByteCount;
  setupErr = AudioFileReadBytes(songFile,
                                false,
                                0,
                                &amp;bytesRead,
                                musicPlaybackState.audioData);
  NSAssert (setupErr == noErr, @"Couldn't read audio data");
  NSLog (@"read %d bytes from file", bytesRead);
  
  AudioStreamBasicDescription fileASBD;
  UInt32 asbdSize = sizeof (fileASBD);
  setupErr = AudioFileGetProperty(songFile,
                                  kAudioFilePropertyDataFormat,
                                  &amp;asbdSize,
                                  &amp;fileASBD);
  NSAssert (setupErr == noErr, @"Couldn't get file asbd");
  
  ExtAudioFileCreateWithURL(outputFileURL,
                            kAudioFileCAFType,
                            &amp;fileASBD,
                            nil,
                            kAudioFileFlags_EraseFile,
                            &amp;musicPlaybackState.extAudioFile);
  
  // get the mixer unit
  AudioComponentDescription mixerDesc;
  mixerDesc.componentType = kAudioUnitType_Effect;
  mixerDesc.componentSubType = kAudioUnitSubType_Delay;
  mixerDesc.componentManufacturer = kAudioUnitManufacturer_Apple;
  mixerDesc.componentFlags = 0;
  mixerDesc.componentFlagsMask = 0;
  
  // get mixer unit from audio component manager
  AudioComponent mixerComponent = AudioComponentFindNext(NULL, &amp;mixerDesc);
  setupErr = AudioComponentInstanceNew(mixerComponent, &amp;mixerUnit);
  NSAssert (setupErr == noErr, @"Couldn't get mixer unit instance");
  
  // set up connections and callbacks
  
  // connect mixer bus 0 input to robot voice render callback
  effectState.rioUnit = remoteIOUnit;
  effectState.sineFrequency = 23;
  effectState.sinePhase = 0;
  effectState.asbd = myASBD;
  
  // connect mixer bus 1 input to music player callback
  
  AURenderCallbackStruct musicPlayerCallbackStruct;
  musicPlayerCallbackStruct.inputProc = MusicPlayerCallback; // callback function
  musicPlayerCallbackStruct.inputProcRefCon = &amp;musicPlaybackState;
  
  setupErr = 
  AudioUnitSetProperty(mixerUnit, 
                       kAudioUnitProperty_SetRenderCallback,
                       kAudioUnitScope_Global,
                       outputElement,
                       &amp;musicPlayerCallbackStruct,
                       sizeof (musicPlayerCallbackStruct));
  NSAssert (setupErr == noErr, @"Couldn't set mixer render callback on bus 1");
  
  // direct connect mixer to output
  AudioUnitConnection connection;
  connection.sourceAudioUnit = mixerUnit;
  connection.sourceOutputNumber = outputElement;
  connection.destInputNumber = outputElement;
  
  setupErr = 
  AudioUnitSetProperty(remoteIOUnit, 
                       kAudioUnitProperty_MakeConnection,
                       kAudioUnitScope_Input,
                       outputElement,
                       &amp;connection,
                       sizeof (connection));
  NSAssert (setupErr == noErr, @"Couldn't set mixer-to-RIO connection");
  
  setupErr = AudioUnitInitialize(mixerUnit);
  NSAssert (setupErr == noErr, @"Couldn't initialize mixer unit");
  
  setupErr =  AudioUnitInitialize(remoteIOUnit);
  NSAssert (setupErr == noErr, @"Couldn't initialize RIO unit");
  
      setupErr = AudioOutputUnitStart (remoteIOUnit);
   }
  </code></pre>