Repository Public
1// ORIX DJ2// File: Packages/ORIXDJCore/Sources/ORIXDJEngine/AudioOutput.swift3// Description: Core Audio output: device discovery, selection, sample rate and buffer size, the AUHAL render callback4// that pulls the RenderCore, and recovery from device changes.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import AudioToolbox9import CoreAudio10import Foundation1112public struct AudioDeviceInfo: Identifiable, Equatable, Sendable {13 public var id: AudioDeviceID14 public var uid: String15 public var name: String16 public var manufacturer: String17 public var outputChannels: Int18 public var sampleRates: [Double]19 public var currentSampleRate: Double20 public var bufferFrameRange: ClosedRange<Int>21 public var bufferFrames: Int22 /// Device latency plus safety offset, in frames (excludes the buffer itself).23 public var latencyFrames: Int24 public var transport: String25 public var isDefault: Bool26}2728public enum AudioOutputError: Error, LocalizedError, Equatable {29 case noDevice30 case coreAudio(String, OSStatus)3132 public var errorDescription: String? {33 switch self {34 case .noDevice: "No audio output device is available."35 case let .coreAudio(what, status): "Core Audio failed to \(what) (error \(status))."36 }37 }38}3940public enum AudioDevices {41 private static func address(_ selector: AudioObjectPropertySelector, _ scope: AudioObjectPropertyScope = kAudioObjectPropertyScopeGlobal) -> AudioObjectPropertyAddress {42 AudioObjectPropertyAddress(mSelector: selector, mScope: scope, mElement: kAudioObjectPropertyElementMain)43 }4445 private static func value<T>(_ id: AudioObjectID, _ selector: AudioObjectPropertySelector, _ scope: AudioObjectPropertyScope = kAudioObjectPropertyScopeGlobal, default d: T) -> T {46 var addr = address(selector, scope)47 var size = UInt32(MemoryLayout<T>.size)48 var v = d49 let status = withUnsafeMutablePointer(to: &v) { AudioObjectGetPropertyData(id, &addr, 0, nil, &size, $0) }50 return status == noErr ? v : d51 }5253 private static func string(_ id: AudioObjectID, _ selector: AudioObjectPropertySelector) -> String {54 var addr = address(selector)55 var size = UInt32(MemoryLayout<Unmanaged<CFString>?>.size)56 var ref: Unmanaged<CFString>?57 let status = withUnsafeMutablePointer(to: &ref) { AudioObjectGetPropertyData(id, &addr, 0, nil, &size, $0) }58 guard status == noErr, let ref else { return "" }59 return ref.takeRetainedValue() as String60 }6162 private static func array<T>(_ id: AudioObjectID, _ selector: AudioObjectPropertySelector, _ scope: AudioObjectPropertyScope = kAudioObjectPropertyScopeGlobal, of _: T.Type) -> [T] {63 var addr = address(selector, scope)64 var size: UInt32 = 065 guard AudioObjectGetPropertyDataSize(id, &addr, 0, nil, &size) == noErr, size > 0 else { return [] }66 let count = Int(size) / MemoryLayout<T>.stride67 let buffer = UnsafeMutablePointer<T>.allocate(capacity: max(1, count))68 defer { buffer.deallocate() }69 guard AudioObjectGetPropertyData(id, &addr, 0, nil, &size, buffer) == noErr else { return [] }70 return Array(UnsafeBufferPointer(start: buffer, count: Int(size) / MemoryLayout<T>.stride))71 }7273 public static var defaultOutputID: AudioDeviceID? {74 let id: AudioDeviceID = value(AudioObjectID(kAudioObjectSystemObject), kAudioHardwarePropertyDefaultOutputDevice, default: 0)75 return id == 0 ? nil : id76 }7778 static func outputChannelCount(_ id: AudioDeviceID) -> Int {79 var addr = address(kAudioDevicePropertyStreamConfiguration, kAudioObjectPropertyScopeOutput)80 var size: UInt32 = 081 guard AudioObjectGetPropertyDataSize(id, &addr, 0, nil, &size) == noErr, size > 0 else { return 0 }82 let raw = UnsafeMutableRawPointer.allocate(byteCount: Int(size), alignment: MemoryLayout<AudioBufferList>.alignment)83 defer { raw.deallocate() }84 guard AudioObjectGetPropertyData(id, &addr, 0, nil, &size, raw) == noErr else { return 0 }85 let list = UnsafeMutableAudioBufferListPointer(raw.assumingMemoryBound(to: AudioBufferList.self))86 return list.reduce(0) { $0 + Int($1.mNumberChannels) }87 }8889 private static func transportName(_ t: UInt32) -> String {90 switch t {91 case kAudioDeviceTransportTypeBuiltIn: "Built-in"92 case kAudioDeviceTransportTypeUSB: "USB"93 case kAudioDeviceTransportTypeBluetooth, kAudioDeviceTransportTypeBluetoothLE: "Bluetooth"94 case kAudioDeviceTransportTypeHDMI: "HDMI"95 case kAudioDeviceTransportTypeDisplayPort: "DisplayPort"96 case kAudioDeviceTransportTypeAirPlay: "AirPlay"97 case kAudioDeviceTransportTypeThunderbolt: "Thunderbolt"98 case kAudioDeviceTransportTypeAggregate: "Aggregate"99 case kAudioDeviceTransportTypeVirtual: "Virtual"100 default: "Other"101 }102 }103104 public static func info(_ id: AudioDeviceID) -> AudioDeviceInfo? {105 let channels = outputChannelCount(id)106 guard channels > 0 else { return nil }107 let ranges = array(id, kAudioDevicePropertyAvailableNominalSampleRates, of: AudioValueRange.self)108 var rates: [Double] = []109 for candidate in [44100.0, 48000, 88200, 96000] where ranges.contains(where: { $0.mMinimum <= candidate && candidate <= $0.mMaximum }) { rates.append(candidate) }110 let bufferRange: AudioValueRange = value(id, kAudioDevicePropertyBufferFrameSizeRange, default: AudioValueRange(mMinimum: 64, mMaximum: 4096))111 let latency: UInt32 = value(id, kAudioDevicePropertyLatency, kAudioObjectPropertyScopeOutput, default: 0)112 let safety: UInt32 = value(id, kAudioDevicePropertySafetyOffset, kAudioObjectPropertyScopeOutput, default: 0)113 let streamLatency: UInt32 = array(id, kAudioDevicePropertyStreams, kAudioObjectPropertyScopeOutput, of: AudioStreamID.self).first.map { value($0, kAudioStreamPropertyLatency, default: UInt32(0)) } ?? 0114 return AudioDeviceInfo(115 id: id, uid: string(id, kAudioDevicePropertyDeviceUID), name: string(id, kAudioObjectPropertyName), manufacturer: string(id, kAudioObjectPropertyManufacturer),116 outputChannels: channels, sampleRates: rates, currentSampleRate: value(id, kAudioDevicePropertyNominalSampleRate, default: Float64(0)),117 bufferFrameRange: Int(bufferRange.mMinimum) ... max(Int(bufferRange.mMinimum), Int(bufferRange.mMaximum)),118 bufferFrames: Int(value(id, kAudioDevicePropertyBufferFrameSize, default: UInt32(512))),119 latencyFrames: Int(latency + safety + streamLatency), transport: transportName(value(id, kAudioDevicePropertyTransportType, default: UInt32(0))),120 isDefault: id == defaultOutputID121 )122 }123124 public static func outputs() -> [AudioDeviceInfo] {125 array(AudioObjectID(kAudioObjectSystemObject), kAudioHardwarePropertyDevices, of: AudioDeviceID.self).compactMap(info)126 }127128 static func setSampleRate(_ id: AudioDeviceID, _ rate: Double) -> Bool {129 var addr = address(kAudioDevicePropertyNominalSampleRate)130 var r = rate131 guard AudioObjectSetPropertyData(id, &addr, 0, nil, UInt32(MemoryLayout<Float64>.size), &r) == noErr else { return false }132 // The change is asynchronous; wait briefly for it to take effect.133 for _ in 0 ..< 40 {134 if abs(value(id, kAudioDevicePropertyNominalSampleRate, default: Float64(0)) - rate) < 1 { return true }135 usleep(25000)136 }137 return false138 }139140 static func setBufferFrames(_ id: AudioDeviceID, _ frames: Int) -> Bool {141 var addr = address(kAudioDevicePropertyBufferFrameSize)142 var f = UInt32(frames)143 return AudioObjectSetPropertyData(id, &addr, 0, nil, UInt32(MemoryLayout<UInt32>.size), &f) == noErr144 }145146 /// Calls `handler` (on an arbitrary queue) when devices appear, disappear, or the default output changes.147 public static func observeChanges(_ handler: @escaping @Sendable () -> Void) {148 let queue = DispatchQueue(label: "ai.spboucher.orix.dj.devices")149 for selector in [kAudioHardwarePropertyDevices, kAudioHardwarePropertyDefaultOutputDevice] {150 var addr = address(selector)151 AudioObjectAddPropertyListenerBlock(AudioObjectID(kAudioObjectSystemObject), &addr, queue) { _, _ in handler() }152 }153 }154}155156/// Owns the AUHAL output unit that drives a RenderCore.157final class AudioOutput: @unchecked Sendable {158 private var unit: AudioUnit?159 private(set) var core: RenderCore?160 private(set) var device: AudioDeviceInfo?161 private(set) var channelCount = 2162 private(set) var isRunning = false163164 private static let renderCallback: AURenderCallback = { refCon, _, _, _, frames, ioData in165 guard let ioData else { return noErr }166 let core = Unmanaged<RenderCore>.fromOpaque(refCon).takeUnretainedValue()167 let list = UnsafeMutableAudioBufferListPointer(ioData)168 // Non-interleaved float: one buffer per channel. Up to 64 channels are mapped without allocating.169 withUnsafeTemporaryAllocation(of: UnsafeMutablePointer<Float>?.self, capacity: 64) { pointers in170 let n = min(list.count, 64)171 for i in 0 ..< n { pointers[i] = list[i].mData?.assumingMemoryBound(to: Float.self) }172 core.render(frameCount: Int(frames), channels: UnsafePointer(pointers.baseAddress!), channelCount: n)173 }174 return noErr175 }176177 /// Opens `deviceID` (or the default output) and starts rendering `core`. The core must have been created at the device's sample rate.178 func start(core: RenderCore, deviceID: AudioDeviceID?, bufferFrames: Int) throws {179 stop()180 guard let id = deviceID ?? AudioDevices.defaultOutputID, var info = AudioDevices.info(id) else { throw AudioOutputError.noDevice }181 if abs(info.currentSampleRate - core.sampleRate) > 1 {182 guard AudioDevices.setSampleRate(id, core.sampleRate) else { throw AudioOutputError.coreAudio("set the sample rate to \(Int(core.sampleRate)) Hz", -1) }183 }184 let frames = min(max(bufferFrames, info.bufferFrameRange.lowerBound), min(info.bufferFrameRange.upperBound, RenderCore.maxFrames))185 _ = AudioDevices.setBufferFrames(id, frames)186 info = AudioDevices.info(id) ?? info187188 var desc = AudioComponentDescription(componentType: kAudioUnitType_Output, componentSubType: kAudioUnitSubType_HALOutput, componentManufacturer: kAudioUnitManufacturer_Apple, componentFlags: 0, componentFlagsMask: 0)189 guard let component = AudioComponentFindNext(nil, &desc) else { throw AudioOutputError.coreAudio("find the output unit", -1) }190 var newUnit: AudioUnit?191 var status = AudioComponentInstanceNew(component, &newUnit)192 guard status == noErr, let au = newUnit else { throw AudioOutputError.coreAudio("create the output unit", status) }193 func check(_ s: OSStatus, _ what: String) throws {194 if s != noErr { AudioComponentInstanceDispose(au); throw AudioOutputError.coreAudio(what, s) }195 }196 var dev = id197 status = AudioUnitSetProperty(au, kAudioOutputUnitProperty_CurrentDevice, kAudioUnitScope_Global, 0, &dev, UInt32(MemoryLayout<AudioDeviceID>.size))198 try check(status, "select the device")199 let channels = min(info.outputChannels, 64)200 var format = AudioStreamBasicDescription(201 mSampleRate: core.sampleRate, mFormatID: kAudioFormatLinearPCM,202 mFormatFlags: kAudioFormatFlagIsFloat | kAudioFormatFlagIsPacked | kAudioFormatFlagIsNonInterleaved | kAudioFormatFlagsNativeEndian,203 mBytesPerPacket: 4, mFramesPerPacket: 1, mBytesPerFrame: 4, mChannelsPerFrame: UInt32(channels), mBitsPerChannel: 32, mReserved: 0204 )205 status = AudioUnitSetProperty(au, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 0, &format, UInt32(MemoryLayout<AudioStreamBasicDescription>.size))206 try check(status, "set the stream format")207 var maxSlice = UInt32(RenderCore.maxFrames)208 AudioUnitSetProperty(au, kAudioUnitProperty_MaximumFramesPerSlice, kAudioUnitScope_Global, 0, &maxSlice, UInt32(MemoryLayout<UInt32>.size))209 var callback = AURenderCallbackStruct(inputProc: Self.renderCallback, inputProcRefCon: Unmanaged.passUnretained(core).toOpaque())210 status = AudioUnitSetProperty(au, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Input, 0, &callback, UInt32(MemoryLayout<AURenderCallbackStruct>.size))211 try check(status, "install the render callback")212 try check(AudioUnitInitialize(au), "initialise the output unit")213 status = AudioOutputUnitStart(au)214 if status != noErr { AudioUnitUninitialize(au); AudioComponentInstanceDispose(au); throw AudioOutputError.coreAudio("start the device", status) }215 unit = au; self.core = core; device = info; channelCount = channels; isRunning = true216 }217218 func stop() {219 guard let au = unit else { return }220 AudioOutputUnitStop(au); AudioUnitUninitialize(au); AudioComponentInstanceDispose(au)221 unit = nil; isRunning = false; core = nil222 }223224 deinit { stop() }225}226