SPB Git

Repository Public

AudioSources.swift 13.8 KB 271 lines swift

1// SP Lecture Studio — LectureCapture2// File: Packages/LectureCapture/Sources/AudioSources.swift3// Description: Audio input abstraction (real microphone via AVAudioEngine, synthetic source for tests), input device listing, permissions.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import Foundation8import AVFoundation9#if os(macOS)10import CoreAudio11#else12import UIKit13#endif1415public enum AudioSourceEvent: Sendable, Equatable {16    /// The system took the audio input away (phone call, Siri, another app).17    case interruptionBegan18    case interruptionEnded(shouldResume: Bool)19    /// Input device or format changed (headset plugged, Bluetooth, sample rate); capture restarted on the new route.20    case routeChanged(description: String)21    case failed(String)22}2324/// A source of mono float audio buffers stamped with host time.25public protocol AudioSource: AnyObject {26    /// Called on an arbitrary queue with a mono Float32 buffer and the host time (seconds) of its first sample.27    var onBuffer: ((AVAudioPCMBuffer, TimeInterval) -> Void)? { get set }28    var onEvent: ((AudioSourceEvent) -> Void)? { get set }29    func start() throws30    func stop()31}3233public struct AudioInputDevice: Hashable, Sendable, Identifiable {34    public var id: String35    public var name: String36    public var isDefault: Bool37}3839public enum CapturePermission: Sendable { case granted, denied, undetermined }4041public enum CapturePermissions {42    public static func status(_ media: AVMediaType) -> CapturePermission {43        switch AVCaptureDevice.authorizationStatus(for: media) {44        case .authorized: .granted45        case .notDetermined: .undetermined46        default: .denied47        }48    }49    /// Asks only when undetermined (call just in time, when the user starts something that needs the device).50    public static func request(_ media: AVMediaType) async -> CapturePermission {51        switch status(media) {52        case .granted: return .granted53        case .denied: return .denied54        case .undetermined: return await AVCaptureDevice.requestAccess(for: media) ? .granted : .denied55        }56    }57}5859public enum AudioSourceError: Error, CustomStringConvertible {60    case noInput, engine(String)61    public var description: String {62        switch self { case .noInput: "no audio input is available"; case .engine(let m): "audio engine: \(m)" }63    }64}6566/// Microphone capture with AVAudioEngine. Buffers are downmixed to mono and delivered at the hardware sample rate.67public final class MicrophoneSource: AudioSource {68    public var onBuffer: ((AVAudioPCMBuffer, TimeInterval) -> Void)?69    public var onEvent: ((AudioSourceEvent) -> Void)?70    public var preferredDeviceID: String?71    /// Uses the system voice processing (echo cancellation / noise suppression) when true.72    public var voiceProcessing = false7374    private var engine = AVAudioEngine()75    private var observers: [NSObjectProtocol] = []76    private var running = false77    private var monoFormat: AVAudioFormat?7879    public init() {}80    deinit { stop() }8182    public static func availableInputs() -> [AudioInputDevice] {83        #if os(macOS)84        return MacAudioDevices.inputs()85        #else86        let session = AVAudioSession.sharedInstance()87        let current = session.currentRoute.inputs.first?.uid88        return (session.availableInputs ?? []).map { AudioInputDevice(id: $0.uid, name: $0.portName, isDefault: $0.uid == current) }89        #endif90    }9192    public func start() throws {93        #if os(iOS)94        let session = AVAudioSession.sharedInstance()95        do {96            try session.setCategory(.playAndRecord, mode: .default, options: [.defaultToSpeaker, .allowBluetoothHFP])97            if let id = preferredDeviceID, let port = session.availableInputs?.first(where: { $0.uid == id }) { try session.setPreferredInput(port) }98            try session.setActive(true)99        } catch { throw AudioSourceError.engine(error.localizedDescription) }100        #endif101        try startEngine()102        installObservers()103        running = true104    }105106    private func startEngine() throws {107        engine = AVAudioEngine()108        let input = engine.inputNode109        #if os(macOS)110        if let id = preferredDeviceID, let deviceID = MacAudioDevices.deviceID(forUID: id), let unit = input.audioUnit {111            var dev = deviceID112            AudioUnitSetProperty(unit, kAudioOutputUnitProperty_CurrentDevice, kAudioUnitScope_Global, 0, &dev, UInt32(MemoryLayout<AudioDeviceID>.size))113        }114        #endif115        if voiceProcessing { try? input.setVoiceProcessingEnabled(true) }116        let format = input.outputFormat(forBus: 0)117        guard format.sampleRate > 0, format.channelCount > 0 else { throw AudioSourceError.noInput }118        let mono = AVAudioFormat(commonFormat: .pcmFormatFloat32, sampleRate: format.sampleRate, channels: 1, interleaved: false)!119        monoFormat = mono120        input.installTap(onBus: 0, bufferSize: AVAudioFrameCount(format.sampleRate / 20), format: format) { [weak self] buffer, time in121            guard let self, let out = Self.downmix(buffer, to: mono) else { return }122            let host = time.isHostTimeValid ? AVAudioTime.seconds(forHostTime: time.hostTime) : RecordingClock.hostNow() - Double(buffer.frameLength) / format.sampleRate123            self.onBuffer?(out, host)124        }125        engine.prepare()126        do { try engine.start() } catch { input.removeTap(onBus: 0); throw AudioSourceError.engine(error.localizedDescription) }127    }128129    static func downmix(_ buffer: AVAudioPCMBuffer, to mono: AVAudioFormat) -> AVAudioPCMBuffer? {130        guard let src = buffer.floatChannelData, let out = AVAudioPCMBuffer(pcmFormat: mono, frameCapacity: buffer.frameLength) else { return nil }131        out.frameLength = buffer.frameLength132        let n = Int(buffer.frameLength), ch = Int(buffer.format.channelCount), dst = out.floatChannelData![0]133        if ch == 1 { dst.update(from: src[0], count: n) } else {134            let k = 1 / Float(ch)135            for i in 0..<n { var s: Float = 0; for c in 0..<ch { s += src[c][i] }; dst[i] = s * k }136        }137        return out138    }139140    private func installObservers() {141        removeObservers()142        let nc = NotificationCenter.default143        observers.append(nc.addObserver(forName: .AVAudioEngineConfigurationChange, object: nil, queue: nil) { [weak self] note in144            guard let self, self.running, (note.object as? AVAudioEngine) === self.engine else { return }145            // The hardware format changed: the engine stopped itself. Restart on the new route.146            self.engine.inputNode.removeTap(onBus: 0)147            do { try self.startEngine(); self.onEvent?(.routeChanged(description: Self.routeDescription())) }148            catch { self.onEvent?(.failed(String(describing: error))) }149        })150        #if os(iOS)151        let session = AVAudioSession.sharedInstance()152        observers.append(nc.addObserver(forName: AVAudioSession.interruptionNotification, object: session, queue: nil) { [weak self] note in153            guard let self, let raw = note.userInfo?[AVAudioSessionInterruptionTypeKey] as? UInt, let type = AVAudioSession.InterruptionType(rawValue: raw) else { return }154            if type == .began { self.onEvent?(.interruptionBegan) } else {155                let opts = AVAudioSession.InterruptionOptions(rawValue: note.userInfo?[AVAudioSessionInterruptionOptionKey] as? UInt ?? 0)156                try? session.setActive(true)157                if self.running, !self.engine.isRunning { try? self.engine.start() }158                self.onEvent?(.interruptionEnded(shouldResume: opts.contains(.shouldResume)))159            }160        })161        observers.append(nc.addObserver(forName: AVAudioSession.routeChangeNotification, object: session, queue: nil) { [weak self] note in162            guard let self, let raw = note.userInfo?[AVAudioSessionRouteChangeReasonKey] as? UInt, let reason = AVAudioSession.RouteChangeReason(rawValue: raw) else { return }163            if reason == .oldDeviceUnavailable || reason == .newDeviceAvailable { self.onEvent?(.routeChanged(description: Self.routeDescription())) }164        })165        observers.append(nc.addObserver(forName: AVAudioSession.mediaServicesWereResetNotification, object: session, queue: nil) { [weak self] _ in166            guard let self, self.running else { return }167            do { try self.startEngine(); self.onEvent?(.routeChanged(description: "media services reset")) } catch { self.onEvent?(.failed(String(describing: error))) }168        })169        #endif170    }171172    static func routeDescription() -> String {173        #if os(iOS)174        return AVAudioSession.sharedInstance().currentRoute.inputs.map(\.portName).joined(separator: ", ")175        #else176        return availableInputs().first(where: \.isDefault)?.name ?? ""177        #endif178    }179180    private func removeObservers() { observers.forEach(NotificationCenter.default.removeObserver); observers.removeAll() }181182    public func stop() {183        guard running else { return }184        running = false185        removeObservers()186        engine.inputNode.removeTap(onBus: 0)187        engine.stop()188        #if os(iOS)189        try? AVAudioSession.sharedInstance().setActive(false, options: [.notifyOthersOnDeactivation])190        #endif191    }192}193194/// Deterministic source for tests and simulations: the caller pushes generated audio, with explicit host times.195public final class SyntheticAudioSource: AudioSource {196    public var onBuffer: ((AVAudioPCMBuffer, TimeInterval) -> Void)?197    public var onEvent: ((AudioSourceEvent) -> Void)?198    public let sampleRate: Double199    public private(set) var isStarted = false200    public private(set) var hostTime: TimeInterval201    private var phase = 0.0202203    public init(sampleRate: Double = 48_000, hostStart: TimeInterval = 1000) { self.sampleRate = sampleRate; hostTime = hostStart }204    public func start() throws { isStarted = true }205    public func stop() { isStarted = false }206207    /// Delivers `seconds` of a sine tone (amplitude 0 = silence) in 50 ms buffers, advancing the synthetic host clock.208    public func push(seconds: Double, amplitude: Float = 0.2, hz: Double = 220, sampleRate overrideRate: Double? = nil) {209        let rate = overrideRate ?? sampleRate210        let format = AVAudioFormat(commonFormat: .pcmFormatFloat32, sampleRate: rate, channels: 1, interleaved: false)!211        var remaining = Int((seconds * rate).rounded())212        let block = Int(rate / 20)213        while remaining > 0 {214            let n = min(block, remaining)215            let buffer = AVAudioPCMBuffer(pcmFormat: format, frameCapacity: AVAudioFrameCount(n))!216            buffer.frameLength = AVAudioFrameCount(n)217            let p = buffer.floatChannelData![0]218            for i in 0..<n { p[i] = amplitude * Float(sin(phase)); phase += 2 * .pi * hz / rate }219            if isStarted { onBuffer?(buffer, hostTime) }220            hostTime += Double(n) / rate221            remaining -= n222        }223    }224    /// Lets host time pass without delivering audio (e.g. an interruption).225    public func advance(seconds: Double) { hostTime += seconds }226}227228#if os(macOS)229enum MacAudioDevices {230    static func allIDs() -> [AudioDeviceID] {231        var addr = AudioObjectPropertyAddress(mSelector: kAudioHardwarePropertyDevices, mScope: kAudioObjectPropertyScopeGlobal, mElement: kAudioObjectPropertyElementMain)232        var size: UInt32 = 0233        guard AudioObjectGetPropertyDataSize(AudioObjectID(kAudioObjectSystemObject), &addr, 0, nil, &size) == noErr else { return [] }234        var ids = [AudioDeviceID](repeating: 0, count: Int(size) / MemoryLayout<AudioDeviceID>.size)235        guard AudioObjectGetPropertyData(AudioObjectID(kAudioObjectSystemObject), &addr, 0, nil, &size, &ids) == noErr else { return [] }236        return ids237    }238    static func string(_ id: AudioDeviceID, _ selector: AudioObjectPropertySelector) -> String? {239        var addr = AudioObjectPropertyAddress(mSelector: selector, mScope: kAudioObjectPropertyScopeGlobal, mElement: kAudioObjectPropertyElementMain)240        var value: Unmanaged<CFString>?241        var size = UInt32(MemoryLayout<Unmanaged<CFString>?>.size)242        guard AudioObjectGetPropertyData(id, &addr, 0, nil, &size, &value) == noErr else { return nil }243        return value?.takeRetainedValue() as String?244    }245    static func inputChannels(_ id: AudioDeviceID) -> Int {246        var addr = AudioObjectPropertyAddress(mSelector: kAudioDevicePropertyStreamConfiguration, mScope: kAudioDevicePropertyScopeInput, mElement: kAudioObjectPropertyElementMain)247        var size: UInt32 = 0248        guard AudioObjectGetPropertyDataSize(id, &addr, 0, nil, &size) == noErr, size > 0 else { return 0 }249        let raw = UnsafeMutableRawPointer.allocate(byteCount: Int(size), alignment: MemoryLayout<AudioBufferList>.alignment)250        defer { raw.deallocate() }251        guard AudioObjectGetPropertyData(id, &addr, 0, nil, &size, raw) == noErr else { return 0 }252        let list = UnsafeMutableAudioBufferListPointer(raw.assumingMemoryBound(to: AudioBufferList.self))253        return list.reduce(0) { $0 + Int($1.mNumberChannels) }254    }255    static func defaultInput() -> AudioDeviceID {256        var addr = AudioObjectPropertyAddress(mSelector: kAudioHardwarePropertyDefaultInputDevice, mScope: kAudioObjectPropertyScopeGlobal, mElement: kAudioObjectPropertyElementMain)257        var id = AudioDeviceID(0); var size = UInt32(MemoryLayout<AudioDeviceID>.size)258        AudioObjectGetPropertyData(AudioObjectID(kAudioObjectSystemObject), &addr, 0, nil, &size, &id)259        return id260    }261    static func inputs() -> [AudioInputDevice] {262        let def = defaultInput()263        return allIDs().filter { inputChannels($0) > 0 }.compactMap { id in264            guard let uid = string(id, kAudioDevicePropertyDeviceUID), let name = string(id, kAudioObjectPropertyName) else { return nil }265            return AudioInputDevice(id: uid, name: name, isDefault: id == def)266        }267    }268    static func deviceID(forUID uid: String) -> AudioDeviceID? { allIDs().first { string($0, kAudioDevicePropertyDeviceUID) == uid } }269}270#endif271
Files main
Menu

Theme