title: AudioUnit Input Samples
body: <p>So I am having some trouble here with my AudioUnit taking in data from microphone/line-in in iOS. I am able to set everything up to what I think is okay and it is calling my recordingCallback, but the data that I am getting out of the buffer is not correct. It always returns exactly the same thing, which is mostly zeros and random large numbers. Does anyone know what could be causing this. My code is as follows. </p>
  
  <p><strong>Setting up Audio Unit</strong></p>
  
  <pre><code>OSStatus status;
  
  // Describe audio component
  AudioComponentDescription desc;
  desc.componentType = kAudioUnitType_Output;
  desc.componentSubType = kAudioUnitSubType_RemoteIO;
  desc.componentFlags = 0;
  desc.componentFlagsMask = 0;
  desc.componentManufacturer = kAudioUnitManufacturer_Apple;
  
  // Get component
  AudioComponent inputComponent = AudioComponentFindNext(NULL, &amp;desc);
  status = AudioComponentInstanceNew(inputComponent, &amp;audioUnit);
  
  // Enable IO for recording
  UInt32 flag = 1;
  status = AudioUnitSetProperty(audioUnit, 
                                kAudioOutputUnitProperty_EnableIO, 
                                kAudioUnitScope_Input, 
                                kInputBusNumber,
                                &amp;flag, 
                                sizeof(flag));
  // Disable playback IO
  flag = 0;
  status = AudioUnitSetProperty(audioUnit, 
                                kAudioOutputUnitProperty_EnableIO, 
                                kAudioUnitScope_Output, 
                                kOutputBusNumber,
                                &amp;flag, 
                                sizeof(flag));
  
  // Describe format
  AudioStreamBasicDescription audioFormat;
  audioFormat.mSampleRate         = 44100.00;
  audioFormat.mFormatID           = kAudioFormatLinearPCM;
  audioFormat.mFormatFlags        = kAudioFormatFlagsNativeFloatPacked |kAudioFormatFlagIsNonInterleaved;
  audioFormat.mFramesPerPacket    = 1;
  audioFormat.mChannelsPerFrame   = 1;
  audioFormat.mBitsPerChannel     = 32;
  audioFormat.mBytesPerPacket     = 4;
  audioFormat.mBytesPerFrame      = 4;
  
  // Apply format
  status = AudioUnitSetProperty(audioUnit, 
                                kAudioUnitProperty_StreamFormat, 
                                kAudioUnitScope_Output, 
                                kInputBusNumber, 
                                &amp;audioFormat, 
                                sizeof(audioFormat));
  
  // Set input callback
  AURenderCallbackStruct callbackStruct;
  callbackStruct.inputProc = recordingCallback;
  callbackStruct.inputProcRefCon = (__bridge void*)self;
  status = AudioUnitSetProperty(audioUnit, 
                                kAudioOutputUnitProperty_SetInputCallback, 
                                kAudioUnitScope_Global, 
                                kInputBusNumber, 
                                &amp;callbackStruct, 
                                sizeof(callbackStruct));
  status = AudioUnitInitialize(audioUnit);
  </code></pre>
  
  <p><strong>Input Callback</strong></p>
  
  <pre><code>static OSStatus recordingCallback(void *inRefCon, 
                                    AudioUnitRenderActionFlags *ioActionFlags, 
                                    const AudioTimeStamp *inTimeStamp, 
                                    UInt32 inBusNumber, 
                                    UInt32 inNumberFrames, 
                                    AudioBufferList *ioData) {
  
      AudioBufferList bufferList;
      bufferList.mNumberBuffers = 1;
      bufferList.mBuffers[0].mDataByteSize = 4;
      bufferList.mBuffers[0].mNumberChannels = 1;
      bufferList.mBuffers[0].mData = malloc(sizeof(float)*inNumberFrames); //
      InputAudio *input = (__bridge InputAudio*)inRefCon;
  
      OSStatus status;
  
      status = AudioUnitRender([input audioUnit], 
                               ioActionFlags, 
                               inTimeStamp, 
                               inBusNumber, 
                               inNumberFrames, 
                               &amp;bufferList);
  
      float* result = (float*)&amp;bufferList.mBuffers[0].mData;
  
      if (input-&gt;counter == 5) {
          for (int i = 0;i&lt;inNumberFrames;i++) {
              printf("%f ",result[i]);
          }
      }
      input-&gt;counter++;
      return noErr;
  }
  </code></pre>
  
  <p>Anyone ever encounter a similar problem or see something clearly wrong in my code. Thanks in advance for any help!</p>
  
  <p>I am basing all of it off of <a href="http://atastypixel.com/blog/using-remoteio-audio-unit/" rel="nofollow">Michael Tysons Core Audio RemoteIO code</a></p>