views:

25

answers:

0

I've looked at aurioTouch but I wanted to do away with the UI and the playback function. Also looked at http://atastypixel.com/blog/using-remoteio-audio-unit/ but so far with no success.

Appreciate if someone can point out any issues in my code.

  // configure audio session
  sampleRate = 44100.0; // Hertz
  AVAudioSession *mySession = [AVAudioSession sharedInstance];
  [mySession setPreferredHardwareSampleRate: sampleRate error: nil];
  [mySession setCategory: AVAudioSessionCategoryPlayAndRecord error: nil];
  [mySession setActive: YES error: nil];
  sampleRate = [mySession currentHardwareSampleRate];

  Float32 preferredBufferSize = .005;
  AudioSessionSetProperty(kAudioSessionProperty_PreferredHardwareIOBufferDuration,  sizeof(preferredBufferSize), &preferredBufferSize);

  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, &desc);

  // Get audio units
  status = AudioComponentInstanceNew(inputComponent, &audioUnit);
  checkStatus(status);

  // Enable IO for recording
  UInt32 flag = 1;
  status = AudioUnitSetProperty(audioUnit, 
       kAudioOutputUnitProperty_EnableIO, 
       kAudioUnitScope_Input, 
       kInputBus,
       &flag, 
       sizeof(flag));
  checkStatus(status);

  // Describe format
  int bytesPerSample = sizeof (AudioUnitSampleType);
  AudioStreamBasicDescription inputStreamFormat = {0};
  inputStreamFormat.mFormatID    = kAudioFormatLinearPCM; 
  inputStreamFormat.mFormatFlags   = kAudioFormatFlagsAudioUnitCanonical;
  inputStreamFormat.mBytesPerPacket  = bytesPerSample; 
  inputStreamFormat.mBytesPerFrame  = bytesPerSample; 
  inputStreamFormat.mFramesPerPacket  = 1; 
  inputStreamFormat.mBitsPerChannel  = 8 * bytesPerSample;
  inputStreamFormat.mChannelsPerFrame  = 1;
  inputStreamFormat.mSampleRate   = sampleRate;

  // Apply format
  status = AudioUnitSetProperty(audioUnit, 
       kAudioUnitProperty_StreamFormat, 
       kAudioUnitScope_Output, 
       kInputBus, 
       &inputStreamFormat, 
       sizeof(inputStreamFormat));
  checkStatus(status);

  // Set input callback
  AURenderCallbackStruct callbackStruct;
  callbackStruct.inputProc = recordingCallback;
  callbackStruct.inputProcRefCon = self;
  status = AudioUnitSetProperty(audioUnit, 
       kAudioOutputUnitProperty_SetInputCallback, 
       kAudioUnitScope_Global, 
       kInputBus, 
       &callbackStruct, 
       sizeof(callbackStruct));
  checkStatus(status);

  // Setup
  UInt32 maxFPS; 
  size = sizeof(maxFPS);
  status = AudioUnitGetProperty(audioUnit, kAudioUnitProperty_MaximumFramesPerSlice, kAudioUnitScope_Global, 0, &maxFPS, &size);

  // Initialise
  status = AudioUnitInitialize(audioUnit);
  checkStatus(status);

  status = AudioOutputUnitStart(audioUnit);
  checkStatus(status);