|
|
|
@ -3,6 +3,12 @@ import AppKit |
|
|
|
import AVFoundation |
|
|
|
import CoreMedia |
|
|
|
|
|
|
|
/// Professional Demand-Driven Audio Recorder (v3.1): |
|
|
|
/// - Pre-configures the AVCaptureSession graph WITHOUT activating mic hardware while idle |
|
|
|
/// (macOS orange microphone indicator stays OFF, 0% idle CPU, Bluetooth AirPods stay in stereo A2DP). |
|
|
|
/// - Starts capture immediately on `.userInteractive` queue when recording begins (~25–45ms attack). |
|
|
|
/// - Shuts down mic hardware cleanly immediately after dictation (or after a 15s burst cooldown if selected). |
|
|
|
/// - Uses `CMBlockBufferCopyDataBytes` for safe multi-channel (stereo/mono) & segmented buffer capture. |
|
|
|
public final class AudioRecorder: NSObject, AVCaptureAudioDataOutputSampleBufferDelegate { |
|
|
|
private var captureSession: AVCaptureSession? |
|
|
|
private var audioOutput: AVCaptureAudioDataOutput? |
|
|
|
@ -11,7 +17,10 @@ public final class AudioRecorder: NSObject, AVCaptureAudioDataOutputSampleBuffer |
|
|
|
|
|
|
|
private var pcmBuffer = Data() |
|
|
|
private var preRollBuffer = Data() |
|
|
|
private let preRollByteLimit = 9600 // 300ms rolling pre-roll at 16kHz 16-bit mono (32,000 bytes/sec) |
|
|
|
private let preRollByteLimit = 9600 // 300ms rolling buffer used only during optional 15s warm cooldown |
|
|
|
private var idleStopWorkItem: DispatchWorkItem? |
|
|
|
private var didFireHardwareReadyForSession = false |
|
|
|
|
|
|
|
private let lock = NSLock() |
|
|
|
private let captureQueue = DispatchQueue(label: "com.soniox.audiocapture", qos: .userInteractive) |
|
|
|
private let sessionQueue = DispatchQueue(label: "com.soniox.audiosession", qos: .userInteractive) |
|
|
|
@ -23,6 +32,7 @@ public final class AudioRecorder: NSObject, AVCaptureAudioDataOutputSampleBuffer |
|
|
|
public private(set) var isRecording = false |
|
|
|
public var onAudioLevelUpdate: ((Float) -> Void)? |
|
|
|
public var onAudioChunkAvailable: ((Data) -> Void)? |
|
|
|
public var onHardwareCapturingReady: (() -> Void)? |
|
|
|
|
|
|
|
public override init() { |
|
|
|
self.targetFormat = AVAudioFormat(commonFormat: .pcmFormatInt16, sampleRate: 16000, channels: 1, interleaved: false)! |
|
|
|
@ -37,10 +47,10 @@ public final class AudioRecorder: NSObject, AVCaptureAudioDataOutputSampleBuffer |
|
|
|
} |
|
|
|
|
|
|
|
NSWorkspace.shared.notificationCenter.addObserver(forName: NSWorkspace.didWakeNotification, object: nil, queue: .main) { [weak self] _ in |
|
|
|
print("AudioRecorder: Mac woke from sleep. Resetting and priming audio session in 1.0s...") |
|
|
|
print("AudioRecorder: Mac woke from sleep. Pre-configuring idle audio graph (mic stays OFF)...") |
|
|
|
self?.resetSession() |
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) { |
|
|
|
try? self?.ensureSessionRunning() |
|
|
|
try? self?.ensureSessionConfigured() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@ -58,6 +68,9 @@ public final class AudioRecorder: NSObject, AVCaptureAudioDataOutputSampleBuffer |
|
|
|
public func resetSession() { |
|
|
|
lock.lock() |
|
|
|
isRecording = false |
|
|
|
didFireHardwareReadyForSession = false |
|
|
|
idleStopWorkItem?.cancel() |
|
|
|
idleStopWorkItem = nil |
|
|
|
let session = captureSession |
|
|
|
captureSession = nil |
|
|
|
audioOutput = nil |
|
|
|
@ -75,7 +88,7 @@ public final class AudioRecorder: NSObject, AVCaptureAudioDataOutputSampleBuffer |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
print("AudioRecorder: Hardware capture session reset.") |
|
|
|
print("AudioRecorder: Hardware capture session reset (mic OFF).") |
|
|
|
} |
|
|
|
|
|
|
|
public func requestMicrophonePermission(completion: @escaping (Bool) -> Void) { |
|
|
|
@ -114,14 +127,12 @@ public final class AudioRecorder: NSObject, AVCaptureAudioDataOutputSampleBuffer |
|
|
|
} |
|
|
|
return AVCaptureDevice.default(for: .audio) ?? all.first |
|
|
|
} |
|
|
|
|
|
|
|
public func ensureSessionConfigured() throws { |
|
|
|
try ensureSessionRunning() |
|
|
|
} |
|
|
|
|
|
|
|
public func ensureSessionRunning() throws { |
|
|
|
/// Pre-wires the AVCaptureSession graph WITHOUT activating the microphone hardware! |
|
|
|
/// Keeps macOS orange mic indicator OFF and CPU at 0% while making `startRunning()` ultra-fast. |
|
|
|
public func ensureSessionConfigured() throws { |
|
|
|
lock.lock() |
|
|
|
if let session = captureSession, session.isRunning { |
|
|
|
if captureSession != nil { |
|
|
|
lock.unlock() |
|
|
|
return |
|
|
|
} |
|
|
|
@ -131,29 +142,24 @@ public final class AudioRecorder: NSObject, AVCaptureAudioDataOutputSampleBuffer |
|
|
|
throw NSError(domain: "AudioRecorder", code: 1, userInfo: [NSLocalizedDescriptionKey: "No microphone device found"]) |
|
|
|
} |
|
|
|
|
|
|
|
let session = AVCaptureSession() |
|
|
|
let input = try AVCaptureDeviceInput(device: device) |
|
|
|
if session.canAddInput(input) { |
|
|
|
session.addInput(input) |
|
|
|
} |
|
|
|
let output = AVCaptureAudioDataOutput() |
|
|
|
output.setSampleBufferDelegate(self, queue: captureQueue) |
|
|
|
if session.canAddOutput(output) { |
|
|
|
session.addOutput(output) |
|
|
|
} |
|
|
|
|
|
|
|
lock.lock() |
|
|
|
if captureSession == nil { |
|
|
|
let session = AVCaptureSession() |
|
|
|
let input = try AVCaptureDeviceInput(device: device) |
|
|
|
if session.canAddInput(input) { |
|
|
|
session.addInput(input) |
|
|
|
} |
|
|
|
let output = AVCaptureAudioDataOutput() |
|
|
|
output.setSampleBufferDelegate(self, queue: captureQueue) |
|
|
|
if session.canAddOutput(output) { |
|
|
|
session.addOutput(output) |
|
|
|
} |
|
|
|
if self.captureSession == nil { |
|
|
|
self.captureSession = session |
|
|
|
self.audioOutput = output |
|
|
|
print("AudioRecorder: Capture session graph configured for device:", device.localizedName) |
|
|
|
} |
|
|
|
let session = captureSession |
|
|
|
lock.unlock() |
|
|
|
|
|
|
|
if let session = session, !session.isRunning { |
|
|
|
session.startRunning() |
|
|
|
print("AudioRecorder: Pre-warmed hot audio session running for device:", device.localizedName) |
|
|
|
} |
|
|
|
print("AudioRecorder: Idle capture graph pre-configured for '\(device.localizedName)' (mic hardware OFF).") |
|
|
|
} |
|
|
|
|
|
|
|
public func startRecording() throws { |
|
|
|
@ -162,35 +168,61 @@ public final class AudioRecorder: NSObject, AVCaptureAudioDataOutputSampleBuffer |
|
|
|
lock.unlock() |
|
|
|
return |
|
|
|
} |
|
|
|
isRecording = true |
|
|
|
let preRoll = preRollBuffer |
|
|
|
idleStopWorkItem?.cancel() |
|
|
|
idleStopWorkItem = nil |
|
|
|
lock.unlock() |
|
|
|
|
|
|
|
try ensureSessionConfigured() |
|
|
|
|
|
|
|
lock.lock() |
|
|
|
let session = captureSession |
|
|
|
let alreadyHot = session?.isRunning ?? false |
|
|
|
let preRoll = alreadyHot ? preRollBuffer : Data() |
|
|
|
preRollBuffer.removeAll(keepingCapacity: true) |
|
|
|
pcmBuffer.removeAll(keepingCapacity: true) |
|
|
|
pcmBuffer.append(preRoll) |
|
|
|
let session = captureSession |
|
|
|
isRecording = true |
|
|
|
didFireHardwareReadyForSession = alreadyHot |
|
|
|
lock.unlock() |
|
|
|
|
|
|
|
if let session = session, !session.isRunning { |
|
|
|
if let session = session, !alreadyHot { |
|
|
|
sessionQueue.async { |
|
|
|
if !session.isRunning { |
|
|
|
session.startRunning() |
|
|
|
} |
|
|
|
} |
|
|
|
} else if session == nil { |
|
|
|
sessionQueue.async { [weak self] in |
|
|
|
try? self?.ensureSessionRunning() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if !preRoll.isEmpty { |
|
|
|
onAudioChunkAvailable?(preRoll) |
|
|
|
if alreadyHot { |
|
|
|
if !preRoll.isEmpty { |
|
|
|
onAudioChunkAvailable?(preRoll) |
|
|
|
} |
|
|
|
DispatchQueue.main.async { [weak self] in |
|
|
|
self?.onHardwareCapturingReady?() |
|
|
|
} |
|
|
|
print("AudioRecorder: Recording started on warm mic (pre-roll: \(preRoll.count) bytes).") |
|
|
|
} else { |
|
|
|
print("AudioRecorder: Starting mic hardware on demand (0% idle mode).") |
|
|
|
} |
|
|
|
let preRollMs = Int(Double(preRoll.count) / 32.0) |
|
|
|
print("AudioRecorder: Instant 0ms recording started (hot mic, pre-roll: \(preRoll.count) bytes / \(preRollMs)ms).") |
|
|
|
} |
|
|
|
|
|
|
|
public func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) { |
|
|
|
// Audio converter runs continuously to maintain 300ms rolling pre-roll ring buffer! |
|
|
|
lock.lock() |
|
|
|
let active = isRecording |
|
|
|
let keepWarm = (SonioxSettings.shared.micStandbyMode == .warmCooldown15s) && (idleStopWorkItem != nil) |
|
|
|
let shouldNotifyReady = active && !didFireHardwareReadyForSession |
|
|
|
if shouldNotifyReady { |
|
|
|
didFireHardwareReadyForSession = true |
|
|
|
} |
|
|
|
lock.unlock() |
|
|
|
|
|
|
|
if shouldNotifyReady { |
|
|
|
DispatchQueue.main.async { [weak self] in |
|
|
|
self?.onHardwareCapturingReady?() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
guard active || keepWarm else { return } |
|
|
|
|
|
|
|
guard let formatDesc = CMSampleBufferGetFormatDescription(sampleBuffer) else { return } |
|
|
|
let srcFormat = AVAudioFormat(cmAudioFormatDescription: formatDesc) |
|
|
|
@ -272,36 +304,68 @@ public final class AudioRecorder: NSObject, AVCaptureAudioDataOutputSampleBuffer |
|
|
|
pcmBuffer.append(data) |
|
|
|
lock.unlock() |
|
|
|
|
|
|
|
// Stream live audio chunk to WebSocket immediately |
|
|
|
onAudioChunkAvailable?(data) |
|
|
|
|
|
|
|
// Calculate decibel loudness for perceptual, high-responsiveness audio meter |
|
|
|
var sumSquare: Float = 0 |
|
|
|
for i in 0..<frameLen { |
|
|
|
let sample = Float(int16Ptr[i]) / 32768.0 |
|
|
|
sumSquare += sample * sample |
|
|
|
} |
|
|
|
let rms = sqrt(sumSquare / Float(frameLen)) |
|
|
|
// Convert to decibels (-50 dB noise floor to -12 dB peak speech) |
|
|
|
let db = 20.0 * log10(max(rms, 0.00001)) |
|
|
|
let normalizedLevel = min(max((db + 48.0) / 36.0, 0.0), 1.0) |
|
|
|
|
|
|
|
DispatchQueue.main.async { [weak self] in |
|
|
|
self?.onAudioLevelUpdate?(normalizedLevel) |
|
|
|
} |
|
|
|
} else { |
|
|
|
// Continuous 300ms rolling pre-roll ring buffer (9,600 bytes at 16kHz Int16 mono) |
|
|
|
} else if keepWarm { |
|
|
|
preRollBuffer.append(data) |
|
|
|
if preRollBuffer.count > preRollByteLimit { |
|
|
|
preRollBuffer.removeFirst(preRollBuffer.count - preRollByteLimit) |
|
|
|
} |
|
|
|
lock.unlock() |
|
|
|
} else { |
|
|
|
lock.unlock() |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/// Non-blocking drain of the serial capture queue (keeps audio engine hot for 0ms next capture) |
|
|
|
/// Schedules clean microphone hardware shutdown so the macOS orange mic indicator turns OFF when idle. |
|
|
|
private func scheduleHardwareShutdown(delaySeconds: TimeInterval) { |
|
|
|
lock.lock() |
|
|
|
idleStopWorkItem?.cancel() |
|
|
|
let work = DispatchWorkItem { [weak self] in |
|
|
|
guard let self = self else { return } |
|
|
|
self.lock.lock() |
|
|
|
let stillIdle = !self.isRecording |
|
|
|
let session = self.captureSession |
|
|
|
if stillIdle { |
|
|
|
self.idleStopWorkItem = nil |
|
|
|
self.preRollBuffer.removeAll(keepingCapacity: true) |
|
|
|
} |
|
|
|
self.lock.unlock() |
|
|
|
|
|
|
|
if stillIdle, let session = session { |
|
|
|
self.sessionQueue.async { |
|
|
|
if session.isRunning { |
|
|
|
session.stopRunning() |
|
|
|
print("AudioRecorder: Microphone hardware turned OFF (idle — orange dot cleared).") |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
idleStopWorkItem = work |
|
|
|
lock.unlock() |
|
|
|
|
|
|
|
if delaySeconds <= 0.0 { |
|
|
|
work.perform() |
|
|
|
} else { |
|
|
|
DispatchQueue.global(qos: .utility).asyncAfter(deadline: .now() + delaySeconds, execute: work) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/// Non-blocking drain of serial captureQueue + clean demand-driven mic hardware shutdown. |
|
|
|
public func stopRecording() -> Data { |
|
|
|
lock.lock() |
|
|
|
if !isRecording { |
|
|
|
@ -311,10 +375,10 @@ public final class AudioRecorder: NSObject, AVCaptureAudioDataOutputSampleBuffer |
|
|
|
} |
|
|
|
lock.unlock() |
|
|
|
|
|
|
|
// Synchronize with serial captureQueue (<1ms) so any in-flight sampleBuffer finishes cleanly |
|
|
|
captureQueue.sync { |
|
|
|
self.lock.lock() |
|
|
|
self.isRecording = false |
|
|
|
self.didFireHardwareReadyForSession = false |
|
|
|
self.preRollBuffer.removeAll(keepingCapacity: true) |
|
|
|
self.lock.unlock() |
|
|
|
} |
|
|
|
@ -323,17 +387,22 @@ public final class AudioRecorder: NSObject, AVCaptureAudioDataOutputSampleBuffer |
|
|
|
let data = pcmBuffer |
|
|
|
lock.unlock() |
|
|
|
|
|
|
|
print("AudioRecorder: Stopped. Audio engine kept hot for 0ms delay. Total PCM captured: \(data.count) bytes.") |
|
|
|
let cooldown: TimeInterval = (SonioxSettings.shared.micStandbyMode == .warmCooldown15s) ? 15.0 : 0.0 |
|
|
|
scheduleHardwareShutdown(delaySeconds: cooldown) |
|
|
|
print("AudioRecorder: Stopped (cooldown: \(Int(cooldown))s). Total PCM captured: \(data.count) bytes.") |
|
|
|
return data |
|
|
|
} |
|
|
|
|
|
|
|
/// Immediate abort (e.g. Escape key pressed) — discards buffered audio immediately |
|
|
|
/// Immediate abort (Escape key or short tap) — discards audio and turns mic hardware OFF immediately. |
|
|
|
public func abortRecording() { |
|
|
|
lock.lock() |
|
|
|
isRecording = false |
|
|
|
didFireHardwareReadyForSession = false |
|
|
|
pcmBuffer.removeAll(keepingCapacity: true) |
|
|
|
preRollBuffer.removeAll(keepingCapacity: true) |
|
|
|
lock.unlock() |
|
|
|
print("AudioRecorder: Aborted immediately.") |
|
|
|
|
|
|
|
scheduleHardwareShutdown(delaySeconds: 0.0) |
|
|
|
print("AudioRecorder: Aborted immediately (mic OFF).") |
|
|
|
} |
|
|
|
} |