0

When a Mac is connected to an audio interface, I can use AVAudioEngine to obtain audio data from the MIC and OTG channels of the audio interface. Part of the code is as follows:

buffer = CircularBuffer<Float>(channelCount: 2, capacity: Int(framesPerSample!) * 512)
engine.setInputDevice(device)
format = AVAudioFormat(commonFormat: .pcmFormatFloat32, sampleRate: device.nominalSampleRate!, channels: 2, interleaved: false)!
engine.connect(engine.inputNode, to: engine.mainMixerNode, format: format)
let channels = device.channels(scope: .input)
var channelArray:Array<Int32> = Array(repeating: -1, count: channels.intValue)
if index % 2 == 0{
 channelArray[0] = index.int32Value
}else{
 channelArray[1] = index.int32Value
}
guard let audioUnit = engine.inputNode.audioUnit else {
 return
}
audioUnit.setPropertyChannelMap(&channelArray, .input, 1)
checkErr(AudioUnitAddRenderNotify(engine.inputNode.audioUnit!,
 renderCallback,
 Unmanaged.passUnretained(self).toOpaque()))
let renderCallback: AURenderCallback = {
 (inRefCon: UnsafeMutableRawPointer,
 ioActionFlags: UnsafeMutablePointer<AudioUnitRenderActionFlags>,
 inTimeStamp: UnsafePointer<AudioTimeStamp>,
 inBusNumber: UInt32,
 inNumberFrames: UInt32,
 ioData: UnsafeMutablePointer<AudioBufferList>?) -> OSStatus in
 
 if ioActionFlags.pointee == AudioUnitRenderActionFlags.unitRenderAction_PostRender {
 let inSelf = Unmanaged<Engine>.fromOpaque(inRefCon).takeUnretainedValue()
 
 let sampleTime = inTimeStamp.pointee.mSampleTime
 
 let start = sampleTime.int64Value
 let end = start + Int64(inNumberFrames)
 if inSelf.buffer.write(from: ioData!, start: start, end: end) != .noError {
 return noErr
 }
 inSelf.getVolumeIntensity(from: ioData, inNumberFrames: inNumberFrames)
 inSelf.lastSampleTime = sampleTime
 }
 
 return noErr
}

If the audio interface is used as an output device to play audio, How can I use AVAudioEngine to obtain audio data from each output channel of the audio interface?

Nick
6423 gold badges10 silver badges32 bronze badges
asked Dec 26, 2025 at 6:56
4
  • Are you asking how to capture the audio output from a different application? Commented Dec 26, 2025 at 7:05
  • @TimRoberts When using the audio device as an output device, directly obtain the audio from output channels 1 and 2 of the audio interface, rather than targeting a specific application. Commented Dec 26, 2025 at 7:15
  • If you want the output sent to the device, you can use a CoreAudio tap, see AudioHardwareCreateProcessTap(). Commented Dec 26, 2025 at 8:02
  • @GordonChilds For example, I have two playback software, A and B. When I open the software, A is set to play to channel 1 of the device, and B is set to play to channel 2. In this case, how can I obtain the audio data from output channel 1? Commented Dec 26, 2025 at 8:11

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.