title: Audio capture in Mac OS X
body: <p>I am developing an audio capture application. How to capture audio on Mac OS X using 8 kHz sampling rate with single chanel using Audio Unit API ?</p>
  
  <p>This is the code I have tried.</p>
  
  <pre><code>    Component                   component;
  ComponentDescription        description;
  OSStatus    err = noErr;
  UInt32  param;
  AURenderCallbackStruct  callback;
  
  description.componentType = kAudioUnitType_Output;
  description.componentSubType = kAudioUnitSubType_HALOutput;
  description.componentManufacturer = kAudioUnitManufacturer_Apple;
  description.componentFlags = 0;
  description.componentFlagsMask = 0;
  if(component = FindNextComponent(NULL, &amp;description))
  {
      err = OpenAComponent(component, &amp;fAudioUnit);
      if(err != noErr)
      {
          fAudioUnit = NULL;
          return err;
      }
  }
  param = 1;
  err = AudioUnitSetProperty(fAudioUnit, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Input, 1, &amp;param, sizeof(UInt32));
  if(err == noErr)
  {
      param = 0;
      err = AudioUnitSetProperty(fAudioUnit, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Output, 0, &amp;param, sizeof(UInt32));
  }
  param = sizeof(AudioDeviceID);
  err = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultInputDevice, &amp;param, &amp;fInputDeviceID);
  if(err != noErr)
  {
      fprintf(stderr, "failed to get default input device\n");
      return err;
  }
  err = AudioUnitSetProperty(fAudioUnit, kAudioOutputUnitProperty_CurrentDevice, kAudioUnitScope_Global, 0, &amp;fInputDeviceID, sizeof(AudioDeviceID));
  if(err != noErr)
  {
      fprintf(stderr, "failed to set AU input device\n");
      return err;
  }
  callback.inputProc = AudioInputProc; 
  callback.inputProcRefCon = NULL;
  err = AudioUnitSetProperty(fAudioUnit, kAudioOutputUnitProperty_SetInputCallback, kAudioUnitScope_Global, 0, &amp;callback, sizeof(AURenderCallbackStruct));
  param = sizeof(AudioStreamBasicDescription);
  err = AudioUnitGetProperty(fAudioUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 1, &amp;fDeviceFormat, &amp;param);
  if(err != noErr)
  {
      printf("failed to get input device ASBD\n");
      return err;
  }
  fDeviceFormat.mSampleRate = 8000.0;
  fDeviceFormat.mChannelsPerFrame = 1;
  fDeviceFormat.mBitsPerChannel = 16;
  fDeviceFormat.mFormatID = kAudioFormatLinearPCM;
  fDeviceFormat.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked;
  err = AudioUnitSetProperty(fAudioUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 1, &amp;fDeviceFormat, sizeof(AudioStreamBasicDescription));
  if(err != noErr)
  {
      printf( "failed to set input device ASBD= %4.4s\n",(char *)&amp;err);
      if(err == kAudioUnitErr_FormatNotSupported)
      {
          printf("kAudioUnitErr_FormatNotSupported\n");
      }
      return err;
  }
  err = AudioUnitGetProperty(fAudioUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 1, &amp;fDeviceFormat, &amp;param);
  if(err != noErr)
  {
      printf( "failed to get input device ASBD\n");
      return err;
  }   
  param = sizeof(UInt32);
  err = AudioUnitGetProperty(fAudioUnit, kAudioDevicePropertyBufferFrameSize, kAudioUnitScope_Global, 0, &amp;fAudioSamples, &amp;param);
  if(err != noErr)
  {
      fprintf(stderr, "failed to get audio sample size\n");
      return err;
  }
  err = AudioUnitInitialize(fAudioUnit);
  if(err != noErr)
  {
      fprintf(stderr, "failed to initialize AU\n");
      return err;
  }
  </code></pre>
  
  <p>Here I am unable to change the sample rate and bit per samples from 32 to 16. Please any one help me to do this.</p>
  
  <p>Thanks &amp; Regards.</p>