Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- - (id) init {
- self = [super init];
- sizeof(allowBluetoothInput), &allowBluetoothInput);
- // You can adjust the latency of RemoteIO (and, in fact, any other audio framework) by setting the kAudioSessionProperty_PreferredHardwareIOBufferDuration property
- float aBufferLength = 0.005; // In seconds
- AudioSessionSetProperty(kAudioSessionProperty_PreferredHardwareIOBufferDuration, sizeof(aBufferLength), &aBufferLength);
- 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;
- // set line in as preferred
- AVAudioSession *audioSession = [AVAudioSession sharedInstance];
- NSString *preferredPortType = AVAudioSessionPortLineIn;
- for (AVAudioSessionPortDescription *desc in audioSession.availableInputs) {
- if ([desc.portType isEqualToString: AVAudioSessionPortLineIn] ||
- // [desc.portType isEqualToString: AVAudioSessionPortBuiltInMic] ||
- [desc.portType isEqualToString: AVAudioSessionPortHeadsetMic])
- {
- [audioSession setPreferredInput:desc error:nil];
- }
- }
- // 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);
- // Enable IO for playback
- status = AudioUnitSetProperty(audioUnit,
- kAudioOutputUnitProperty_EnableIO,
- kAudioUnitScope_Output,
- kOutputBus,
- &flag,
- sizeof(flag));
- checkStatus(status);
- // Describe format
- AudioStreamBasicDescription audioFormat;
- audioFormat.mSampleRate = 44100.00;
- audioFormat.mFormatID = kAudioFormatLinearPCM; // kAudioFormatMPEG4AAC_ELD
- audioFormat.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
- audioFormat.mFramesPerPacket = 1;
- audioFormat.mChannelsPerFrame = 1;
- audioFormat.mBitsPerChannel = 16;
- audioFormat.mBytesPerPacket = 2;
- audioFormat.mBytesPerFrame = 2;
- // Apply format
- status = AudioUnitSetProperty(audioUnit,
- kAudioUnitProperty_StreamFormat,
- kAudioUnitScope_Output,
- kInputBus,
- &audioFormat,
- sizeof(audioFormat));
- checkStatus(status);
- status = AudioUnitSetProperty(audioUnit,
- kAudioUnitProperty_StreamFormat,
- kAudioUnitScope_Input,
- kOutputBus,
- &audioFormat,
- sizeof(audioFormat));
- 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);
- // Set output callback
- callbackStruct.inputProc = playbackCallback;
- callbackStruct.inputProcRefCon = self;
- status = AudioUnitSetProperty(audioUnit,
- kAudioUnitProperty_SetRenderCallback,
- kAudioUnitScope_Global,
- kOutputBus,
- &callbackStruct,
- sizeof(callbackStruct));
- checkStatus(status);
- // Disable buffer allocation for the recorder (optional - do this if we want to pass in our own)
- flag = 0;
- status = AudioUnitSetProperty(audioUnit,
- kAudioUnitProperty_ShouldAllocateBuffer,
- kAudioUnitScope_Output,
- kInputBus,
- &flag,
- sizeof(flag));
- // Allocate our own buffers (1 channel, 16 bits per sample, thus 16 bits per frame, thus 2 bytes per frame).
- // Practice learns the buffers used contain 512 frames, if this changes it will be fixed in processAudio.
- tempBuffer.mNumberChannels = 1;
- tempBuffer.mDataByteSize = 512 * 2;
- tempBuffer.mData = malloc( 512 * 2 );
- [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionAllowBluetoothA2DP error:NULL];
- // Initialise
- AudioSessionSetActive(true);
- NSTimeInterval outLat = [[AVAudioSession sharedInstance] outputLatency];
- NSTimeInterval inLat = [[AVAudioSession sharedInstance] inputLatency];
- status = AudioUnitInitialize(audioUnit);
- checkStatus(status);
- return self;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement