Fastest and only macOS Dictation app with on-device STT and custom trained AI enhancement model. Windows, iOS testflight! Android, Linux waitlist open! A local Wispr Flow alternative. Follow https://x.com/fluidvoiceapp for more
Stability changes related to ASR Service
altic-devd08d13b05609
diff --git a/Sources/Fluid/Services/ASRService.swift b/Sources/Fluid/Services/ASRService.swiftindex 29afac60..f97906c0 100644--- a/Sources/Fluid/Services/ASRService.swift+++ b/Sources/Fluid/Services/ASRService.swift@@ -8,9 +8,19 @@ import AppKit/// Serializes all CoreML transcription operations to prevent concurrent access issues./// The actor ensures only one transcription runs at a time, preventing CoreML race conditions.+/// Serializes all CoreML transcription operations to prevent concurrent access issues.+/// This implementation enforces strict serialization (non-reentrant) using a task chain.private actor TranscriptionExecutor {- func run<T>(_ operation: @escaping () async throws -> T) async rethrows -> T {- try await operation()+ private var lastTask: Task<Void, Never>?++ func run<T>(_ operation: @escaping () async throws -> T) async throws -> T {+ let previous = lastTask+ let task = Task<T, Error> {+ _ = await previous?.result+ return try await operation()+ }+ lastTask = Task { _ = try? await task.value }+ return try await task.value}}@@ -90,7 +100,9 @@ final class ASRService: ObservableObjectvar asrManager: AsrManager?private var isRecordingWholeSession: Bool = false- private var recordedPCM: [Float] = []+ // Thread-safe buffer to prevent "Array mutation while enumerating" and memory corruption crashes+ // during long sessions where reallocation occurs frequently.+ private let audioBuffer = ThreadSafeAudioBuffer()// Streaming transcription state (no VAD)private var streamingTask: Task<Void, Never>?@@ -203,7 +215,7 @@ final class ASRService: ObservableObjectguard isRunning == false else { return }finalText.removeAll()- recordedPCM.removeAll()+ audioBuffer.clear(keepingCapacity: true) // specific optimization for restartpartialTranscription.removeAll()previousFullTranscription.removeAll()lastProcessedSampleCount = 0@@ -270,8 +282,8 @@ final class ASRService: ObservableObjectpreviousFullTranscription.removeAll()// Thread-safe copy of recorded audio- let pcm = recordedPCM- recordedPCM.removeAll()+ let pcm = audioBuffer.getAll()+ audioBuffer.clear()isRecordingWholeSession = falsedo@@ -317,7 +329,7 @@ final class ASRService: ObservableObjectremoveEngineTap()engine.stop()isRunning = false- recordedPCM.removeAll()+ audioBuffer.clear()isRecordingWholeSession = falsepartialTranscription.removeAll()previousFullTranscription.removeAll()@@ -421,7 +433,8 @@ final class ASRService: ObservableObjectlet mono16k = self.toMono16k(floatBuffer: buffer)if mono16k.isEmpty == false{- recordedPCM.append(contentsOf: mono16k)+ // Thread-safe append+ audioBuffer.append(mono16k)// Publish audio level for visualizationlet audioLevel = self.calculateAudioLevel(mono16k)@@ -788,12 +801,13 @@ final class ASRService: ObservableObjectguard isAsrReady, let manager = asrManager else { return }- let currentSampleCount = recordedPCM.count+ // Thread-safe count check+ let currentSampleCount = audioBuffer.countlet minSamples = 8000 // 0.5 second minimum for faster initial feedbackguard currentSampleCount >= minSamples else { return }- // Copy the audio data to prevent mutation during processing- let chunk = Array(recordedPCM[0..<currentSampleCount])+ // Thread-safe copy of the data+ let chunk = audioBuffer.getPrefix(currentSampleCount)isProcessingChunk = truedefer { isProcessingChunk = false }diff --git a/Sources/Fluid/Services/ThreadSafeAudioBuffer.swift b/Sources/Fluid/Services/ThreadSafeAudioBuffer.swiftnew file mode 100644index 00000000..9a630f8b--- /dev/null+++ b/Sources/Fluid/Services/ThreadSafeAudioBuffer.swift@@ -0,0 +1,45 @@++import Foundation++/// A thread-safe wrapper around a float array to prevent data races between+/// the audio engine (background thread) and the ASR service (main thread).+final class ThreadSafeAudioBuffer {+ private var buffer: [Float] = []+ private let lock = NSLock()++ /// Appends new samples to the buffer in a thread-safe manner+ func append(_ newSamples: [Float]) {+ lock.lock()+ defer { lock.unlock() }+ buffer.append(contentsOf: newSamples)+ }++ /// Clears the buffer, optionally keeping capacity to optimize for reuse+ func clear(keepingCapacity: Bool = false) {+ lock.lock()+ defer { lock.unlock() }+ buffer.removeAll(keepingCapacity: keepingCapacity)+ }++ /// Returns the current number of samples+ var count: Int {+ lock.lock()+ defer { lock.unlock() }+ return buffer.count+ }++ /// Returns a copy of the prefix of the buffer (thread-safe)+ func getPrefix(_ length: Int) -> [Float] {+ lock.lock()+ defer { lock.unlock() }+ let safeLength = min(length, buffer.count)+ return Array(buffer[0..<safeLength])+ }++ /// Returns a copy of the entire buffer+ func getAll() -> [Float] {+ lock.lock()+ defer { lock.unlock() }+ return buffer+ }+}