|
|
@ -10,6 +10,8 @@ public final class AudioRecorder: NSObject, AVCaptureAudioDataOutputSampleBuffer |
|
|
private let targetFormat: AVAudioFormat |
|
|
private let targetFormat: AVAudioFormat |
|
|
|
|
|
|
|
|
private var pcmBuffer = Data() |
|
|
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 lock = NSLock() |
|
|
private let lock = NSLock() |
|
|
private let captureQueue = DispatchQueue(label: "com.soniox.audiocapture", qos: .userInteractive) |
|
|
private let captureQueue = DispatchQueue(label: "com.soniox.audiocapture", qos: .userInteractive) |
|
|
private let sessionQueue = DispatchQueue(label: "com.soniox.audiosession", qos: .userInteractive) |
|
|
private let sessionQueue = DispatchQueue(label: "com.soniox.audiosession", qos: .userInteractive) |
|
|
@ -38,7 +40,7 @@ public final class AudioRecorder: NSObject, AVCaptureAudioDataOutputSampleBuffer |
|
|
print("AudioRecorder: Mac woke from sleep. Resetting and priming audio session in 1.0s...") |
|
|
print("AudioRecorder: Mac woke from sleep. Resetting and priming audio session in 1.0s...") |
|
|
self?.resetSession() |
|
|
self?.resetSession() |
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) { |
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) { |
|
|
try? self?.ensureSessionConfigured() |
|
|
|
|
|
|
|
|
try? self?.ensureSessionRunning() |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -62,6 +64,8 @@ public final class AudioRecorder: NSObject, AVCaptureAudioDataOutputSampleBuffer |
|
|
audioConverter = nil |
|
|
audioConverter = nil |
|
|
reusableSrcBuffer = nil |
|
|
reusableSrcBuffer = nil |
|
|
reusableOutBuffer = nil |
|
|
reusableOutBuffer = nil |
|
|
|
|
|
pcmBuffer.removeAll(keepingCapacity: true) |
|
|
|
|
|
preRollBuffer.removeAll(keepingCapacity: true) |
|
|
lock.unlock() |
|
|
lock.unlock() |
|
|
|
|
|
|
|
|
if let session = session { |
|
|
if let session = session { |
|
|
@ -112,25 +116,44 @@ public final class AudioRecorder: NSObject, AVCaptureAudioDataOutputSampleBuffer |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public func ensureSessionConfigured() throws { |
|
|
public func ensureSessionConfigured() throws { |
|
|
if captureSession != nil { |
|
|
|
|
|
|
|
|
try ensureSessionRunning() |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public func ensureSessionRunning() throws { |
|
|
|
|
|
lock.lock() |
|
|
|
|
|
if let session = captureSession, session.isRunning { |
|
|
|
|
|
lock.unlock() |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
|
|
|
lock.unlock() |
|
|
|
|
|
|
|
|
guard let device = resolveCaptureDevice() else { |
|
|
guard let device = resolveCaptureDevice() else { |
|
|
throw NSError(domain: "AudioRecorder", code: 1, userInfo: [NSLocalizedDescriptionKey: "No microphone device found"]) |
|
|
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) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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) |
|
|
|
|
|
} |
|
|
|
|
|
self.captureSession = session |
|
|
|
|
|
self.audioOutput = output |
|
|
|
|
|
print("AudioRecorder: Capture session graph configured for device:", device.localizedName) |
|
|
} |
|
|
} |
|
|
let output = AVCaptureAudioDataOutput() |
|
|
|
|
|
output.setSampleBufferDelegate(self, queue: captureQueue) |
|
|
|
|
|
if session.canAddOutput(output) { |
|
|
|
|
|
session.addOutput(output) |
|
|
|
|
|
|
|
|
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) |
|
|
} |
|
|
} |
|
|
self.captureSession = session |
|
|
|
|
|
self.audioOutput = output |
|
|
|
|
|
print("AudioRecorder: Capture session graph configured for device:", device.localizedName) |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public func startRecording() throws { |
|
|
public func startRecording() throws { |
|
|
@ -139,27 +162,35 @@ public final class AudioRecorder: NSObject, AVCaptureAudioDataOutputSampleBuffer |
|
|
lock.unlock() |
|
|
lock.unlock() |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
|
|
|
isRecording = true |
|
|
|
|
|
let preRoll = preRollBuffer |
|
|
|
|
|
preRollBuffer.removeAll(keepingCapacity: true) |
|
|
pcmBuffer.removeAll(keepingCapacity: true) |
|
|
pcmBuffer.removeAll(keepingCapacity: true) |
|
|
try ensureSessionConfigured() |
|
|
|
|
|
|
|
|
pcmBuffer.append(preRoll) |
|
|
let session = captureSession |
|
|
let session = captureSession |
|
|
isRecording = true |
|
|
|
|
|
lock.unlock() |
|
|
lock.unlock() |
|
|
|
|
|
|
|
|
if let session = session { |
|
|
|
|
|
|
|
|
if let session = session, !session.isRunning { |
|
|
sessionQueue.async { |
|
|
sessionQueue.async { |
|
|
if !session.isRunning { |
|
|
if !session.isRunning { |
|
|
session.startRunning() |
|
|
session.startRunning() |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
} else if session == nil { |
|
|
|
|
|
sessionQueue.async { [weak self] in |
|
|
|
|
|
try? self?.ensureSessionRunning() |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
print("AudioRecorder: Fast recording started (mic hardware ON).") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if !preRoll.isEmpty { |
|
|
|
|
|
onAudioChunkAvailable?(preRoll) |
|
|
|
|
|
} |
|
|
|
|
|
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) { |
|
|
public func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) { |
|
|
lock.lock() |
|
|
|
|
|
let active = isRecording |
|
|
|
|
|
lock.unlock() |
|
|
|
|
|
guard active else { return } |
|
|
|
|
|
|
|
|
// Audio converter runs continuously to maintain 300ms rolling pre-roll ring buffer! |
|
|
|
|
|
|
|
|
guard let formatDesc = CMSampleBufferGetFormatDescription(sampleBuffer) else { return } |
|
|
guard let formatDesc = CMSampleBufferGetFormatDescription(sampleBuffer) else { return } |
|
|
let srcFormat = AVAudioFormat(cmAudioFormatDescription: formatDesc) |
|
|
let srcFormat = AVAudioFormat(cmAudioFormatDescription: formatDesc) |
|
|
@ -237,35 +268,40 @@ public final class AudioRecorder: NSObject, AVCaptureAudioDataOutputSampleBuffer |
|
|
let data = Data(bytes: int16Ptr, count: bytesCount) |
|
|
let data = Data(bytes: int16Ptr, count: bytesCount) |
|
|
|
|
|
|
|
|
lock.lock() |
|
|
lock.lock() |
|
|
guard isRecording else { |
|
|
|
|
|
|
|
|
if isRecording { |
|
|
|
|
|
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) |
|
|
|
|
|
preRollBuffer.append(data) |
|
|
|
|
|
if preRollBuffer.count > preRollByteLimit { |
|
|
|
|
|
preRollBuffer.removeFirst(preRollBuffer.count - preRollByteLimit) |
|
|
|
|
|
} |
|
|
lock.unlock() |
|
|
lock.unlock() |
|
|
return |
|
|
|
|
|
} |
|
|
|
|
|
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) |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/// Non-blocking drain of the serial capture queue + async hardware shutdown |
|
|
|
|
|
|
|
|
/// Non-blocking drain of the serial capture queue (keeps audio engine hot for 0ms next capture) |
|
|
public func stopRecording() -> Data { |
|
|
public func stopRecording() -> Data { |
|
|
lock.lock() |
|
|
lock.lock() |
|
|
if !isRecording { |
|
|
if !isRecording { |
|
|
@ -275,28 +311,19 @@ public final class AudioRecorder: NSObject, AVCaptureAudioDataOutputSampleBuffer |
|
|
} |
|
|
} |
|
|
lock.unlock() |
|
|
lock.unlock() |
|
|
|
|
|
|
|
|
// Synchronize with serial captureQueue (<1ms) so any in-flight sampleBuffer finishes cleanly without 60ms usleep! |
|
|
|
|
|
|
|
|
// Synchronize with serial captureQueue (<1ms) so any in-flight sampleBuffer finishes cleanly |
|
|
captureQueue.sync { |
|
|
captureQueue.sync { |
|
|
self.lock.lock() |
|
|
self.lock.lock() |
|
|
self.isRecording = false |
|
|
self.isRecording = false |
|
|
|
|
|
self.preRollBuffer.removeAll(keepingCapacity: true) |
|
|
self.lock.unlock() |
|
|
self.lock.unlock() |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
lock.lock() |
|
|
lock.lock() |
|
|
let data = pcmBuffer |
|
|
let data = pcmBuffer |
|
|
let session = captureSession |
|
|
|
|
|
lock.unlock() |
|
|
lock.unlock() |
|
|
|
|
|
|
|
|
// Stop hardware capture asynchronously on background queue so main thread & WebSocket finalize have 0ms blocking |
|
|
|
|
|
if let session = session { |
|
|
|
|
|
sessionQueue.async { |
|
|
|
|
|
if session.isRunning { |
|
|
|
|
|
session.stopRunning() |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
print("AudioRecorder: Stopped. Microphone hardware OFF. Total PCM captured: \(data.count) bytes.") |
|
|
|
|
|
|
|
|
print("AudioRecorder: Stopped. Audio engine kept hot for 0ms delay. Total PCM captured: \(data.count) bytes.") |
|
|
return data |
|
|
return data |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -305,16 +332,8 @@ public final class AudioRecorder: NSObject, AVCaptureAudioDataOutputSampleBuffer |
|
|
lock.lock() |
|
|
lock.lock() |
|
|
isRecording = false |
|
|
isRecording = false |
|
|
pcmBuffer.removeAll(keepingCapacity: true) |
|
|
pcmBuffer.removeAll(keepingCapacity: true) |
|
|
let session = captureSession |
|
|
|
|
|
|
|
|
preRollBuffer.removeAll(keepingCapacity: true) |
|
|
lock.unlock() |
|
|
lock.unlock() |
|
|
|
|
|
|
|
|
if let session = session { |
|
|
|
|
|
sessionQueue.async { |
|
|
|
|
|
if session.isRunning { |
|
|
|
|
|
session.stopRunning() |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
print("AudioRecorder: Aborted immediately.") |
|
|
print("AudioRecorder: Aborted immediately.") |
|
|
} |
|
|
} |
|
|
} |
|
|
} |