- (void) initAudioGraph {
OSStatus status;
NewAUGraph(&inputAudioGraph);
AUNode ioNode;
AudioComponentDescription desc;
desc.componentType = kAudioUnitType_Output;
desc.componentSubType = kAudioUnitSubType_HALOutput;
desc.componentFlags = 0;
desc.componentFlagsMask = 0;
desc.componentManufacturer = kAudioUnitManufacturer_Apple;
status = AUGraphAddNode(inputAudioGraph, &desc, &ioNode);
NSLog(@"Add node %i",status);
status = AUGraphOpen(inputAudioGraph);
NSLog(@"Open graph %i",status);
status = AUGraphNodeInfo(inputAudioGraph, ioNode, NULL, &inputAudioUnit);
NSLog(@"Node info %i",status);
BOOL enable = true;
status = AudioUnitSetProperty(inputAudioUnit,
kAudioOutputUnitProperty_EnableIO,
kAudioUnitScope_Input,
1, &enable, sizeof(enable));
NSLog(@"Status (enable input): %i",(int)status);
enable = false;
status = AudioUnitSetProperty(inputAudioUnit,
kAudioOutputUnitProperty_EnableIO,
kAudioUnitScope_Output,
0, &enable, sizeof(enable));
NSLog(@"Status (enable output): %i",(int)status);
AudioDeviceID input_device_id = kAudioObjectUnknown;
AudioObjectPropertyAddress property_address = {
kAudioHardwarePropertyDefaultInputDevice,
kAudioObjectPropertyScopeGlobal,
kAudioObjectPropertyElementMaster
};
int statusCounter = 0;
UInt32 input_device_id_size = sizeof(input_device_id);
status = AudioObjectGetPropertyData(kAudioObjectSystemObject, &property_address, 0, NULL, &input_device_id_size, &input_device_id);
NSLog(@"Get default device status: %i -- %i",status,input_device_id);
status = AudioUnitSetProperty(inputAudioUnit, kAudioOutputUnitProperty_CurrentDevice, kAudioUnitScope_Global, 0, &input_device_id, sizeof(input_device_id));
NSLog(@"%i Status (set current device): %i",statusCounter++,status);
/*
AudioDeviceID input_device_id = sourceID;
connectedInputDeviceID = input_device_id;
int statusCounter = 0;
status = AudioUnitSetProperty(inputAudioUnit, kAudioOutputUnitProperty_CurrentDevice, kAudioUnitScope_Global, 0, &input_device_id, sizeof(input_device_id));
NSLog(@"%i Status (set current device): %i",statusCounter++,status);
*/
inputAudioFormat.mSampleRate= 44100.0f;
inputAudioFormat.mFormatID= kAudioFormatLinearPCM;
inputAudioFormat.mFormatFlags= kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
inputAudioFormat.mFramesPerPacket= 1;
inputAudioFormat.mChannelsPerFrame= 1;
inputAudioFormat.mBitsPerChannel= 16;
inputAudioFormat.mBytesPerPacket= 2;
inputAudioFormat.mBytesPerFrame= 2;
status = AudioUnitSetProperty(inputAudioUnit,
kAudioUnitProperty_StreamFormat,
kAudioUnitScope_Output, 1,
&inputAudioFormat, sizeof(inputAudioFormat));
NSLog(@"%i Status (format): %i",statusCounter++,status);
status = AUGraphInitialize(inputAudioGraph);
NSLog(@"Init input graph: %i",status);
CAShow(inputAudioGraph);
}