kAudioUnitType_Output, componentSubType: kAudioUnitSubType_RemoteIO, componentManufacturer: kAudioUnitManufacturer_Apple, componentFlags: 0, componentFlagsMask: 0 ) guard let audioComponent = AudioComponentFindNext(nil, &audioComponentDescription) else { print("Failed to find audio component") return } var status = AudioComponentInstanceNew(audioComponent, &audioUnit) guard status == noErr else { print("Failed to create audio unit: \(status)") return } var oneFlag: UInt32 = 1 status = AudioUnitSetProperty(audioUnit!, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Input, 1, &oneFlag, UInt32(MemoryLayout<UInt32>.size)) guard status == noErr else { print("Failed to enable audio input: \(status)") return } status = AudioUnitSetProperty(audioUnit!, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 1, &audioFormat, UInt32(MemoryLayout<AudioStreamBasicDescription>.size)) guard status == noErr else { print("Failed to set stream format: \(status)") return } var callbackStruct = AURenderCallbackStruct( inputProc: recordingCallback, inputProcRefCon: Unmanaged.passUnretained(self).toOpaque() ) status = AudioUnitSetProperty(audioUnit!, kAudioOutputUnitProperty_SetInputCallback, kAudioUnitScope_Global, 0, &callbackStruct, UInt32(MemoryLayout<AURenderCallbackStruct>.size)) guard status == noErr else { print("Failed to set recording callback: \(status)") return } print("Audio unit setup completed") }