وویس تایپینگ سانی اوکس مک
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

339 lines
14 KiB

import Foundation
import AppKit
import AVFoundation
import CoreMedia
public final class AudioRecorder: NSObject, AVCaptureAudioDataOutputSampleBufferDelegate {
private var captureSession: AVCaptureSession?
private var audioOutput: AVCaptureAudioDataOutput?
private var audioConverter: AVAudioConverter?
private let targetFormat: AVAudioFormat
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 captureQueue = DispatchQueue(label: "com.soniox.audiocapture", qos: .userInteractive)
private let sessionQueue = DispatchQueue(label: "com.soniox.audiosession", qos: .userInteractive)
// Pre-allocated reusable buffers to eliminate 50Hz per-frame heap allocations
private var reusableSrcBuffer: AVAudioPCMBuffer?
private var reusableOutBuffer: AVAudioPCMBuffer?
public private(set) var isRecording = false
public var onAudioLevelUpdate: ((Float) -> Void)?
public var onAudioChunkAvailable: ((Data) -> Void)?
public override init() {
self.targetFormat = AVAudioFormat(commonFormat: .pcmFormatInt16, sampleRate: 16000, channels: 1, interleaved: false)!
super.init()
setupLifecycleObservers()
}
private func setupLifecycleObservers() {
NSWorkspace.shared.notificationCenter.addObserver(forName: NSWorkspace.willSleepNotification, object: nil, queue: .main) { [weak self] _ in
print("AudioRecorder: Mac going to sleep. Resetting audio hardware session.")
self?.resetSession()
}
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...")
self?.resetSession()
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
try? self?.ensureSessionRunning()
}
}
NotificationCenter.default.addObserver(forName: .AVCaptureSessionRuntimeError, object: nil, queue: .main) { [weak self] notif in
print("AudioRecorder: AVCaptureSessionRuntimeError received. Resetting capture graph:", notif)
self?.resetSession()
}
NotificationCenter.default.addObserver(forName: .AVCaptureSessionWasInterrupted, object: nil, queue: .main) { [weak self] _ in
print("AudioRecorder: AVCaptureSessionWasInterrupted received. Resetting session.")
self?.resetSession()
}
}
public func resetSession() {
lock.lock()
isRecording = false
let session = captureSession
captureSession = nil
audioOutput = nil
audioConverter = nil
reusableSrcBuffer = nil
reusableOutBuffer = nil
pcmBuffer.removeAll(keepingCapacity: true)
preRollBuffer.removeAll(keepingCapacity: true)
lock.unlock()
if let session = session {
sessionQueue.async {
if session.isRunning {
session.stopRunning()
}
}
}
print("AudioRecorder: Hardware capture session reset.")
}
public func requestMicrophonePermission(completion: @escaping (Bool) -> Void) {
switch AVCaptureDevice.authorizationStatus(for: .audio) {
case .authorized:
completion(true)
case .notDetermined:
AVCaptureDevice.requestAccess(for: .audio) { granted in
DispatchQueue.main.async {
completion(granted)
}
}
case .denied, .restricted:
completion(false)
@unknown default:
completion(false)
}
}
public static func availableAudioDevices() -> [AVCaptureDevice] {
var types: [AVCaptureDevice.DeviceType] = []
if #available(macOS 14.0, *) {
types = [.microphone, .external]
} else {
types = [.builtInMicrophone, .externalUnknown]
}
let session = AVCaptureDevice.DiscoverySession(deviceTypes: types, mediaType: .audio, position: .unspecified)
return session.devices
}
private func resolveCaptureDevice() -> AVCaptureDevice? {
let all = AudioRecorder.availableAudioDevices()
if let prefID = SonioxSettings.shared.preferredAudioDeviceID,
let matched = all.first(where: { $0.uniqueID == prefID }) {
return matched
}
return AVCaptureDevice.default(for: .audio) ?? all.first
}
public func ensureSessionConfigured() throws {
try ensureSessionRunning()
}
public func ensureSessionRunning() throws {
lock.lock()
if let session = captureSession, session.isRunning {
lock.unlock()
return
}
lock.unlock()
guard let device = resolveCaptureDevice() else {
throw NSError(domain: "AudioRecorder", code: 1, userInfo: [NSLocalizedDescriptionKey: "No microphone device found"])
}
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 session = captureSession
lock.unlock()
if let session = session, !session.isRunning {
session.startRunning()
print("AudioRecorder: Pre-warmed hot audio session running for device:", device.localizedName)
}
}
public func startRecording() throws {
lock.lock()
if isRecording {
lock.unlock()
return
}
isRecording = true
let preRoll = preRollBuffer
preRollBuffer.removeAll(keepingCapacity: true)
pcmBuffer.removeAll(keepingCapacity: true)
pcmBuffer.append(preRoll)
let session = captureSession
lock.unlock()
if let session = session, !session.isRunning {
sessionQueue.async {
if !session.isRunning {
session.startRunning()
}
}
} else if session == nil {
sessionQueue.async { [weak self] in
try? self?.ensureSessionRunning()
}
}
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) {
// Audio converter runs continuously to maintain 300ms rolling pre-roll ring buffer!
guard let formatDesc = CMSampleBufferGetFormatDescription(sampleBuffer) else { return }
let srcFormat = AVAudioFormat(cmAudioFormatDescription: formatDesc)
if audioConverter == nil || audioConverter?.inputFormat != srcFormat {
audioConverter = AVAudioConverter(from: srcFormat, to: targetFormat)
reusableSrcBuffer = nil
reusableOutBuffer = nil
}
guard let converter = self.audioConverter else { return }
guard let blockBuffer = CMSampleBufferGetDataBuffer(sampleBuffer) else { return }
let numSamples = CMSampleBufferGetNumSamples(sampleBuffer)
guard numSamples > 0 else { return }
let requiredSrcCap = AVAudioFrameCount(numSamples)
if reusableSrcBuffer == nil || reusableSrcBuffer!.frameCapacity < requiredSrcCap || reusableSrcBuffer!.format != srcFormat {
reusableSrcBuffer = AVAudioPCMBuffer(pcmFormat: srcFormat, frameCapacity: max(requiredSrcCap, 4096))
}
guard let srcBuffer = reusableSrcBuffer else { return }
srcBuffer.frameLength = requiredSrcCap
let bytesPerFrame = Int(srcFormat.streamDescription.pointee.mBytesPerFrame)
let totalBlockLen = CMBlockBufferGetDataLength(blockBuffer)
guard bytesPerFrame > 0, totalBlockLen > 0 else { return }
if srcFormat.isInterleaved {
let abl = srcBuffer.mutableAudioBufferList
if let dst = abl.pointee.mBuffers.mData {
let bytesToCopy = min(totalBlockLen, Int(requiredSrcCap) * bytesPerFrame)
guard CMBlockBufferCopyDataBytes(blockBuffer, atOffset: 0, dataLength: bytesToCopy, destination: dst) == kCMBlockBufferNoErr else { return }
abl.pointee.mBuffers.mDataByteSize = UInt32(bytesToCopy)
}
} else {
let chCount = Int(srcFormat.channelCount)
let planeBytes = Int(requiredSrcCap) * bytesPerFrame
for ch in 0..<chCount {
let offset = ch * planeBytes
if offset >= totalBlockLen { break }
let copyLen = min(planeBytes, totalBlockLen - offset)
if let floatChannel = srcBuffer.floatChannelData?[ch] {
_ = CMBlockBufferCopyDataBytes(blockBuffer, atOffset: offset, dataLength: copyLen, destination: floatChannel)
} else if let int16Channel = srcBuffer.int16ChannelData?[ch] {
_ = CMBlockBufferCopyDataBytes(blockBuffer, atOffset: offset, dataLength: copyLen, destination: int16Channel)
} else if let int32Channel = srcBuffer.int32ChannelData?[ch] {
_ = CMBlockBufferCopyDataBytes(blockBuffer, atOffset: offset, dataLength: copyLen, destination: int32Channel)
}
}
}
let outCapacity = AVAudioFrameCount(Double(numSamples) * (16000.0 / srcFormat.sampleRate)) + 128
if reusableOutBuffer == nil || reusableOutBuffer!.frameCapacity < outCapacity {
reusableOutBuffer = AVAudioPCMBuffer(pcmFormat: targetFormat, frameCapacity: max(outCapacity, 2048))
}
guard let outBuffer = reusableOutBuffer else { return }
outBuffer.frameLength = 0
var error: NSError?
var haveData = true
let status = converter.convert(to: outBuffer, error: &error) { inNumPackets, outStatus in
if haveData {
haveData = false
outStatus.pointee = .haveData
return srcBuffer
} else {
outStatus.pointee = .noDataNow
return nil
}
}
if status == .haveData || status == .inputRanDry {
let frameLen = Int(outBuffer.frameLength)
if frameLen > 0, let int16Ptr = outBuffer.int16ChannelData?[0] {
let bytesCount = frameLen * MemoryLayout<Int16>.size
let data = Data(bytes: int16Ptr, count: bytesCount)
lock.lock()
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()
}
}
}
}
/// Non-blocking drain of the serial capture queue (keeps audio engine hot for 0ms next capture)
public func stopRecording() -> Data {
lock.lock()
if !isRecording {
let data = pcmBuffer
lock.unlock()
return data
}
lock.unlock()
// Synchronize with serial captureQueue (<1ms) so any in-flight sampleBuffer finishes cleanly
captureQueue.sync {
self.lock.lock()
self.isRecording = false
self.preRollBuffer.removeAll(keepingCapacity: true)
self.lock.unlock()
}
lock.lock()
let data = pcmBuffer
lock.unlock()
print("AudioRecorder: Stopped. Audio engine kept hot for 0ms delay. Total PCM captured: \(data.count) bytes.")
return data
}
/// Immediate abort (e.g. Escape key pressed) — discards buffered audio immediately
public func abortRecording() {
lock.lock()
isRecording = false
pcmBuffer.removeAll(keepingCapacity: true)
preRollBuffer.removeAll(keepingCapacity: true)
lock.unlock()
print("AudioRecorder: Aborted immediately.")
}
}