Repository navigation
Expand file tree
/
Copy pathMicEngine.swift
More file actions
209 lines (186 loc) · 9.77 KB
/
Copy pathMicEngine.swift
File metadata and controls
209 lines (186 loc) · 9.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import AppKit
import AudioToolbox
import CoreAudio
import Foundation
/// What the rack page needs from whichever engine it is editing.
protocol LiveEngine: AnyObject {
var inputPeak: Float { get }
var outputPeak: Float { get }
var sampleRate: Double { get }
func scopeSnapshot(into: UnsafeMutablePointer<Float>) -> Bool
func publish(_ settings: RackSettings)
}
extension SystemAudioEngine: LiveEngine {}
extension MicEngine: LiveEngine {}
// MARK: - Virtual microphone driver
/// The one piece of patchbay that touches the system: a passthrough loopback device
/// (BlackHole, GPLv3, built as "patchbay Mic") in /Library/Audio/Plug-Ins/HAL. Whatever
/// patchbay writes to its output stream appears on its input stream, so any app can
/// select "patchbay Mic" as its microphone and receive the processed signal.
enum VirtualMic {
/// Visible input-only device apps select as their microphone.
static let deviceUID = "patchbay Mic_UID"
/// Hidden output-only twin sharing the ring buffer; the engine writes here. Keeping the
/// mic's own input stream out of the aggregate means the engine can never read back
/// what it just wrote.
static let sinkUID = "patchbay Mic_2_UID"
static let installPath = "/Library/Audio/Plug-Ins/HAL/patchbayMic.driver"
static var bundledPath: String? { Bundle.main.path(forResource: "patchbayMic", ofType: "driver") }
static var installed: Bool { FileManager.default.fileExists(atPath: installPath) }
static func isVirtualMic(_ device: Device) -> Bool { device.uid == deviceUID || device.uid == sinkUID }
/// Copies the driver in and restarts coreaudiod. Asks for an administrator password.
/// Every engine must be stopped before this; coreaudiod's restart invalidates them all.
static func install(completion: @escaping (Result<Void, Error>) -> Void) {
guard let bundled = bundledPath else { completion(.failure(Failure("The driver is missing from this build."))); return }
privileged("rm -rf '\(installPath)' && cp -R '\(bundled)' '\(installPath)' && chown -R root:wheel '\(installPath)' && killall coreaudiod", completion: completion)
}
static func uninstall(completion: @escaping (Result<Void, Error>) -> Void) {
privileged("rm -rf '\(installPath)' && killall coreaudiod", completion: completion)
}
private static func privileged(_ command: String, completion: @escaping (Result<Void, Error>) -> Void) {
let escaped = command.replacingOccurrences(of: "\\", with: "\\\\").replacingOccurrences(of: "\"", with: "\\\"")
let source = "do shell script \"\(escaped)\" with administrator privileges"
DispatchQueue.global(qos: .userInitiated).async {
var error: NSDictionary?
NSAppleScript(source: source)?.executeAndReturnError(&error)
DispatchQueue.main.async {
if let error, let message = error[NSAppleScript.errorMessage] as? String {
// -128 is the user cancelling the password prompt.
if (error[NSAppleScript.errorNumber] as? Int) == -128 { completion(.failure(Failure("Cancelled."))) }
else { completion(.failure(Failure(message))) }
} else {
completion(.success(()))
}
}
}
}
struct Failure: LocalizedError {
let message: String
init(_ message: String) { self.message = message }
var errorDescription: String? { message }
}
}
// MARK: - Microphone engine
/// real microphone → chain → patchbay Mic. A private aggregate of the two devices runs one
/// IO proc that reads the mic's input buffers and writes the virtual device's output
/// buffers; `RealtimeDSP` does the rest. Nothing is muted and nothing needs proving: if
/// patchbay stops, the virtual mic simply falls silent and the real mic is untouched.
final class MicEngine {
private(set) var status: SystemAudioEngine.Status = .stopped {
didSet {
engineLog.info("mic status \(String(describing: self.status), privacy: .public)")
DispatchQueue.main.async { [status, weak self] in self?.onStatus?(status) }
}
}
var onStatus: ((SystemAudioEngine.Status) -> Void)?
var onNeedsRestart: (() -> Void)?
private(set) var sourceUID: String?
private(set) var sampleRate = 48_000.0
var inputPeak: Float { processor?.inputPeak ?? 0 }
var outputPeak: Float { processor?.outputPeak ?? 0 }
func scopeSnapshot(into: UnsafeMutablePointer<Float>) -> Bool {
guard let processor else { return false }
processor.scopeSnapshot(into: into)
return true
}
private var aggregateID = AudioObjectID(kAudioObjectUnknown)
private var ioProcID: AudioDeviceIOProcID?
private var processor: RealtimeDSP?
private var rateListener: AudioObjectPropertyListenerBlock?
private var settings = RackSettings(modules: [])
private var generation: UInt64 = 0
private var layoutGeneration: UInt64 = 0
private var lastLayoutKey = ""
deinit { stop() }
func start(source: Device, settings: RackSettings) {
engineLog.info("mic start from \(source.name, privacy: .public)")
stopResources()
self.settings = settings
sourceUID = source.uid
do {
try buildPipeline(source: source)
status = .running(device: source.name)
} catch {
stopResources()
status = .failed((error as? LocalizedError)?.errorDescription ?? error.localizedDescription)
}
}
func stop() {
guard aggregateID != kAudioObjectUnknown || sourceUID != nil else { return }
engineLog.info("mic stop")
stopResources()
sourceUID = nil
status = .stopped
}
func publish(_ settings: RackSettings) {
self.settings = settings
generation &+= 1
let key = settings.layoutKey
if key != lastLayoutKey { layoutGeneration &+= 1; lastLayoutKey = key }
processor?.publish(settings, generation: generation, layoutGeneration: layoutGeneration)
}
private func buildPipeline(source: Device) throws {
let description: [String: Any] = [
kAudioAggregateDeviceNameKey: "patchbay microphone",
kAudioAggregateDeviceUIDKey: "com.patchbay.mic.aggregate.\(UUID().uuidString)",
kAudioAggregateDeviceMainSubDeviceKey: source.uid,
kAudioAggregateDeviceIsPrivateKey: true,
kAudioAggregateDeviceIsStackedKey: false,
kAudioAggregateDeviceSubDeviceListKey: [
[kAudioSubDeviceUIDKey: source.uid],
[kAudioSubDeviceUIDKey: VirtualMic.sinkUID, kAudioSubDeviceDriftCompensationKey: true],
],
]
var newAggregateID = AudioObjectID(kAudioObjectUnknown)
try check(AudioHardwareCreateAggregateDevice(description as CFDictionary, &newAggregateID), "creating the microphone pipeline")
aggregateID = newAggregateID
var address = AudioObjectPropertyAddress(mSelector: kAudioDevicePropertyNominalSampleRate, mScope: kAudioObjectPropertyScopeGlobal, mElement: kAudioObjectPropertyElementMain)
var rate = Float64(0)
var size = UInt32(MemoryLayout<Float64>.size)
try check(AudioObjectGetPropertyData(newAggregateID, &address, 0, nil, &size, &rate), "reading the microphone sample rate")
sampleRate = rate
generation &+= 1
layoutGeneration &+= 1
lastLayoutKey = settings.layoutKey
guard let newProcessor = RealtimeDSP(settings: settings, sampleRate: sampleRate, generation: generation, layoutGeneration: layoutGeneration, proofOnly: false) else {
throw VirtualMic.Failure("allocating the realtime processor failed")
}
processor = newProcessor
var newIOProcID: AudioDeviceIOProcID?
let block: AudioDeviceIOBlock = { _, input, _, output, _ in newProcessor.render(input: input, output: output) }
try check(AudioDeviceCreateIOProcIDWithBlock(&newIOProcID, newAggregateID, nil, block), "creating the microphone callback")
ioProcID = newIOProcID
try check(AudioDeviceStart(newAggregateID, newIOProcID), "starting the microphone pipeline")
let listener: AudioObjectPropertyListenerBlock = { [weak self] _, _ in
DispatchQueue.main.async { [weak self] in
guard let self, self.aggregateID == newAggregateID else { return }
var current = Float64(0)
var s = UInt32(MemoryLayout<Float64>.size)
if AudioObjectGetPropertyData(newAggregateID, &address, 0, nil, &s, ¤t) == noErr, current != self.sampleRate {
self.onNeedsRestart?()
}
}
}
if AudioObjectAddPropertyListenerBlock(newAggregateID, &address, .main, listener) == noErr { rateListener = listener }
}
private func stopResources() {
if let rateListener, aggregateID != kAudioObjectUnknown {
var address = AudioObjectPropertyAddress(mSelector: kAudioDevicePropertyNominalSampleRate, mScope: kAudioObjectPropertyScopeGlobal, mElement: kAudioObjectPropertyElementMain)
AudioObjectRemovePropertyListenerBlock(aggregateID, &address, .main, rateListener)
}
rateListener = nil
if let ioProcID, aggregateID != kAudioObjectUnknown {
AudioDeviceStop(aggregateID, ioProcID)
AudioDeviceDestroyIOProcID(aggregateID, ioProcID)
}
ioProcID = nil
if aggregateID != kAudioObjectUnknown {
AudioHardwareDestroyAggregateDevice(aggregateID)
aggregateID = AudioObjectID(kAudioObjectUnknown)
}
processor = nil
}
private func check(_ status: OSStatus, _ operation: String) throws {
guard status == noErr else { throw VirtualMic.Failure("\(operation) failed (OSStatus \(status)).") }
}
}