Skip to content

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

perf(asr): benchmark dictation latency

altic-devaa2846cb4866
diff --git a/Sources/Fluid/Services/ASRService.swift b/Sources/Fluid/Services/ASRService.swift
index 984d5117..ee7adb8e 100644
--- a/Sources/Fluid/Services/ASRService.swift
+++ b/Sources/Fluid/Services/ASRService.swift
@@ -267,6 +267,15 @@ final class ASRService: ObservableObject {
)
}
+ private func elapsedMilliseconds(since start: TimeInterval?) -> Int {
+ guard let start else { return -1 }
+ return Int(((Date().timeIntervalSince1970 - start) * 1000).rounded())
+ }
+
+ private func benchmarkLog(_ message: String) {
+ DebugLogger.shared.info("ASR_BENCH session=\(self.benchmarkSessionID) \(message)", source: "ASRBenchmark")
+ }
+
private func streamingChunkErrorCategory(for error: Error) -> String {
if error is CancellationError {
return "cancelled"
@@ -496,6 +505,11 @@ final class ASRService: ObservableObject {
private var isProcessingChunk: Bool = false
private var skipNextChunk: Bool = false
private var previousFullTranscription: String = ""
+ private var benchmarkSessionID: Int = 0
+ private var benchmarkRecordingStartedAt: TimeInterval?
+ private var benchmarkStreamingChunkIndex: Int = 0
+ private var benchmarkCompletedStreamingChunks: Int = 0
+ private var benchmarkLastChunkSampleCount: Int = 0
private let streamingChunkAnalyticsSuccessSampleRate: Int = 50
private let streamingChunkFailureMinIntervalSeconds: TimeInterval = 15
private var streamingChunkAnalyticsSuccessCount: Int = 0
@@ -766,10 +780,17 @@ final class ASRService: ObservableObject {
self.lastProcessedSampleCount = 0
self.isProcessingChunk = false
self.skipNextChunk = false
+ self.benchmarkSessionID += 1
+ self.benchmarkRecordingStartedAt = Date().timeIntervalSince1970
+ self.benchmarkStreamingChunkIndex = 0
+ self.benchmarkCompletedStreamingChunks = 0
+ self.benchmarkLastChunkSampleCount = 0
self.streamingChunkAnalyticsSuccessCount = 0
self.lastStreamingChunkFailureAnalyticsAt = nil
self.audioCapturePipeline.setRecordingEnabled(true)
self.refreshWordBoostStatus()
+ let dims = self.currentTranscriptionAnalyticsDimensions()
+ self.benchmarkLog("recording_start model=\(dims.model) provider=\(dims.provider) supportsStreaming=\(SettingsStore.shared.selectedSpeechModel.supportsStreaming)")
DebugLogger.shared.debug("✅ Buffers cleared", source: "ASRService")
self.isStarting = true
@@ -813,6 +834,7 @@ final class ASRService: ObservableObject {
let model = SettingsStore.shared.selectedSpeechModel
if model.supportsStreaming {
DebugLogger.shared.debug("📡 Starting streaming transcription...", source: "ASRService")
+ self.benchmarkLog("streaming_timer_start intervalMs=\(Int((self.streamingChunkDurationSeconds * 1000).rounded())) minSamples=\(self.minimumStreamingPreviewSamples)")
self.startStreamingTranscription()
} else {
DebugLogger.shared.debug("⏸️ Skipping streaming - model '\(model.displayName)' does not support real-time chunk processing", source: "ASRService")
@@ -883,6 +905,8 @@ final class ASRService: ObservableObject {
/// Check debug logs for detailed error information.
func stop() async -> String {
DebugLogger.shared.info("🛑 STOP() called - beginning shutdown sequence", source: "ASRService")
+ let stopStartedAt = Date().timeIntervalSince1970
+ self.benchmarkLog("stop_start ageMs=\(self.elapsedMilliseconds(since: self.benchmarkRecordingStartedAt)) bufferedSamples=\(self.audioBuffer.count)")
guard self.isRunning else {
DebugLogger.shared.warning("⚠️ STOP() - not running, returning empty string", source: "ASRService")
@@ -931,7 +955,9 @@ final class ASRService: ObservableObject {
// CRITICAL FIX: Await completion of streaming task AND any pending transcriptions
// This prevents use-after-free crashes (EXC_BAD_ACCESS) when clearing buffer
DebugLogger.shared.debug("⏳ Awaiting stopStreamingTimerAndAwait()...", source: "ASRService")
+ let streamingStopStartedAt = Date().timeIntervalSince1970
await self.stopStreamingTimerAndAwait()
+ self.benchmarkLog("stop_streaming_wait elapsedMs=\(self.elapsedMilliseconds(since: streamingStopStartedAt))")
DebugLogger.shared.debug("✅ stopStreamingTimerAndAwait() completed", source: "ASRService")
self.isProcessingChunk = false
@@ -944,6 +970,7 @@ final class ASRService: ObservableObject {
// Thread-safe copy of recorded audio
var pcm = self.audioBuffer.getAll()
self.audioBuffer.clear()
+ self.benchmarkLog("stop_audio_drained samples=\(pcm.count) audioMs=\(Int((Double(pcm.count) / 16_000.0 * 1000).rounded()))")
// Drop recordings with no audio at all — nothing to transcribe.
guard !pcm.isEmpty else {
@@ -959,6 +986,7 @@ final class ASRService: ObservableObject {
await MediaPlaybackService.shared.resumeIfWePaused(true)
DebugLogger.shared.info("🎵 Resumed system media after empty audio", source: "ASRService")
}
+ self.benchmarkLog("stop_end result=empty totalMs=\(self.elapsedMilliseconds(since: stopStartedAt)) reason=no_audio")
return ""
}
@@ -978,29 +1006,42 @@ final class ASRService: ObservableObject {
}
do {
- DebugLogger.shared.debug("🔍 Calling ensureAsrReady()...", source: "ASRService")
- try await self.ensureAsrReady()
- DebugLogger.shared.debug("✅ ensureAsrReady() completed", source: "ASRService")
+ var provider = self.transcriptionProvider
+ let ensureStartedAt = Date().timeIntervalSince1970
+ if self.isAsrReady, provider.isReady {
+ self.benchmarkLog("stop_ensure_ready skipped=true elapsedMs=0")
+ } else {
+ DebugLogger.shared.debug("🔍 Calling ensureAsrReady()...", source: "ASRService")
+ try await self.ensureAsrReady()
+ provider = self.transcriptionProvider
+ self.benchmarkLog("stop_ensure_ready skipped=false elapsedMs=\(self.elapsedMilliseconds(since: ensureStartedAt))")
+ DebugLogger.shared.debug("✅ ensureAsrReady() completed", source: "ASRService")
+ }
- guard self.transcriptionProvider.isReady else {
+ guard provider.isReady else {
DebugLogger.shared.error("Transcription provider is not ready", source: "ASRService")
// Resume media playback if we paused it
if shouldResumeMedia {
await MediaPlaybackService.shared.resumeIfWePaused(true)
DebugLogger.shared.info("🎵 Resumed system media after provider not ready", source: "ASRService")
}
+ self.benchmarkLog("stop_end result=empty totalMs=\(self.elapsedMilliseconds(since: stopStartedAt)) reason=provider_not_ready")
return ""
}
DebugLogger.shared.debug("Starting transcription with \(pcm.count) samples (\(Float(pcm.count) / 16_000.0) seconds)", source: "ASRService")
- DebugLogger.shared.debug("stop(): starting full transcription (samples: \(pcm.count)) using \(self.transcriptionProvider.name)", source: "ASRService")
+ DebugLogger.shared.debug("stop(): starting full transcription (samples: \(pcm.count)) using \(provider.name)", source: "ASRService")
DebugLogger.shared.debug("📝 Starting transcription executor...", source: "ASRService")
- let result = try await transcriptionExecutor.run { [provider = self.transcriptionProvider] in
+ let finalStartedAt = Date().timeIntervalSince1970
+ let result = try await transcriptionExecutor.run { [provider] in
DebugLogger.shared.debug("📝 Inside transcription executor closure", source: "ASRService")
let transcriptionResult = try await provider.transcribeFinal(pcm)
DebugLogger.shared.debug("📝 Transcription provider returned", source: "ASRService")
return transcriptionResult
}
+ let finalElapsedMs = self.elapsedMilliseconds(since: finalStartedAt)
+ let finalAudioSeconds = Double(pcm.count) / 16_000.0
+ let finalRTF = finalAudioSeconds > 0 ? (Double(finalElapsedMs) / 1000.0) / finalAudioSeconds : 0
DebugLogger.shared.debug("✅ Transcription executor completed", source: "ASRService")
DebugLogger.shared.debug("stop(): full transcription finished", source: "ASRService")
DebugLogger.shared.debug(
@@ -1008,9 +1049,14 @@ final class ASRService: ObservableObject {
source: "ASRService"
)
DebugLogger.shared.info(
- "Final ASR result | provider=\(self.transcriptionProvider.name) | samples=\(pcm.count) | textChars=\(result.text.trimmingCharacters(in: .whitespacesAndNewlines).count) | confidence=\(result.confidence)",
+ "Final ASR result | provider=\(provider.name) | samples=\(pcm.count) | textChars=\(result.text.trimmingCharacters(in: .whitespacesAndNewlines).count) | confidence=\(result.confidence)",
source: "ASRService"
)
+ let finalTextChars = result.text.trimmingCharacters(in: .whitespacesAndNewlines).count
+ self.benchmarkLog(
+ "final_done elapsedMs=\(finalElapsedMs) samples=\(pcm.count) audioMs=\(Int((finalAudioSeconds * 1000).rounded())) " +
+ "textChars=\(finalTextChars) rtf=\(String(format: "%.3f", finalRTF)) streamedChunks=\(self.benchmarkCompletedStreamingChunks)"
+ )
// Mark first transcription as complete to clear loading state
if !self.hasCompletedFirstTranscription {
@@ -1025,6 +1071,7 @@ final class ASRService: ObservableObject {
let cleanedText = ASRService.applyCustomDictionary(ASRService.removeFillerWords(result.text))
self.recordWordBoostHitIfAny(transcribedText: cleanedText)
DebugLogger.shared.debug("After post-processing: '\(cleanedText)'", source: "ASRService")
+ self.benchmarkLog("stop_end result=success totalMs=\(self.elapsedMilliseconds(since: stopStartedAt)) recordingAgeMs=\(self.elapsedMilliseconds(since: self.benchmarkRecordingStartedAt)) cleanedChars=\(cleanedText.count)")
// Resume media playback if we paused it
if shouldResumeMedia {
@@ -1061,6 +1108,7 @@ final class ASRService: ObservableObject {
DebugLogger.shared.info("🎵 Resumed system media after transcription failure", source: "ASRService")
}
+ self.benchmarkLog("stop_end result=error totalMs=\(self.elapsedMilliseconds(since: stopStartedAt)) error=\(error.localizedDescription)")
return ""
}
}
@@ -2399,16 +2447,18 @@ final class ASRService: ObservableObject {
/// This prevents race conditions where the buffer is cleared while
/// a transcription task is still running.
private func stopStreamingTimerAndAwait() async {
- guard let task = self.streamingTask else { return }
+ guard let task = self.streamingTask else {
+ self.benchmarkLog("streaming_timer_stop no_task=true")
+ return
+ }
+ let startedAt = Date().timeIntervalSince1970
+ self.benchmarkLog("streaming_timer_stop begin")
task.cancel()
// Wait for the task to actually finish - this is critical!
// The task may be in the middle of processStreamingChunk()
_ = await task.result
self.streamingTask = nil
-
- // Also cancel and await any pending transcription in the executor
- // This prevents use-after-free when we clear the buffer
- await self.transcriptionExecutor.cancelAndAwaitPending()
+ self.benchmarkLog("streaming_timer_stop end elapsedMs=\(self.elapsedMilliseconds(since: startedAt)) completedChunks=\(self.benchmarkCompletedStreamingChunks)")
}
/// Legacy sync version for cases where we can't await (e.g., stopWithoutTranscription)
@@ -2460,21 +2510,29 @@ final class ASRService: ObservableObject {
@MainActor
private func processStreamingChunk() async {
guard self.isRunning else { return }
+ self.benchmarkStreamingChunkIndex += 1
+ let chunkIndex = self.benchmarkStreamingChunkIndex
+ let chunkAgeMs = self.elapsedMilliseconds(since: self.benchmarkRecordingStartedAt)
// Skip if already processing to prevent queue buildup
guard !self.isProcessingChunk else {
DebugLogger.shared.debug("⚠️ Skipping chunk - previous transcription still in progress", source: "ASRService")
+ self.benchmarkLog("chunk_skip index=\(chunkIndex) reason=busy ageMs=\(chunkAgeMs)")
self.skipNextChunk = true
return
}
if self.skipNextChunk {
DebugLogger.shared.debug("⚠️ Skipping chunk for ANE recovery", source: "ASRService")
+ self.benchmarkLog("chunk_skip index=\(chunkIndex) reason=recovery ageMs=\(chunkAgeMs)")
self.skipNextChunk = false
return
}
- guard self.isAsrReady, self.transcriptionProvider.isReady else { return }
+ guard self.isAsrReady, self.transcriptionProvider.isReady else {
+ self.benchmarkLog("chunk_skip index=\(chunkIndex) reason=not_ready ageMs=\(chunkAgeMs) isAsrReady=\(self.isAsrReady) providerReady=\(self.transcriptionProvider.isReady)")
+ return
+ }
// Thread-safe count check
let currentSampleCount = self.audioBuffer.count
@@ -2487,6 +2545,7 @@ final class ASRService: ObservableObject {
"Waiting for more audio data (\(currentSampleCount)/\(minSamples) samples)",
source: "ASRService"
)
+ self.benchmarkLog("chunk_wait index=\(chunkIndex) ageMs=\(chunkAgeMs) samples=\(currentSampleCount) minSamples=\(minSamples)")
}
return
}
@@ -2497,6 +2556,7 @@ final class ASRService: ObservableObject {
// Validate chunk is not empty (defensive check)
guard !chunk.isEmpty else {
DebugLogger.shared.warning("Audio buffer returned empty chunk despite count > 0. Skipping transcription.", source: "ASRService")
+ self.benchmarkLog("chunk_skip index=\(chunkIndex) reason=empty ageMs=\(chunkAgeMs)")
return
}
@@ -2504,6 +2564,10 @@ final class ASRService: ObservableObject {
defer { isProcessingChunk = false }
let startTime = Date()
+ let startedAt = startTime.timeIntervalSince1970
+ let newSamples = max(0, chunk.count - self.benchmarkLastChunkSampleCount)
+ self.benchmarkLastChunkSampleCount = chunk.count
+ self.benchmarkLog("chunk_start index=\(chunkIndex) ageMs=\(chunkAgeMs) samples=\(chunk.count) newSamples=\(newSamples) audioMs=\(Int((Double(chunk.count) / 16_000.0 * 1000).rounded())) provider=\(self.transcriptionProvider.name)")
do {
DebugLogger.shared.debug("Streaming chunk starting transcription (samples: \(chunk.count)) using \(self.transcriptionProvider.name)", source: "ASRService")
@@ -2525,6 +2589,8 @@ final class ASRService: ObservableObject {
let rawText = result.text.trimmingCharacters(in: .whitespacesAndNewlines)
let newText = ASRService.applyCustomDictionary(ASRService.removeFillerWords(rawText))
self.recordWordBoostHitIfAny(transcribedText: newText)
+ self.benchmarkCompletedStreamingChunks += 1
+ self.lastProcessedSampleCount = chunk.count
// Mark first transcription as complete to clear loading state
if !self.hasCompletedFirstTranscription {
@@ -2543,6 +2609,12 @@ final class ASRService: ObservableObject {
DebugLogger.shared.debug("✅ Streaming: '\(updatedText)' (\(String(format: "%.2f", duration))s)", source: "ASRService")
}
+ let rtf = chunk.isEmpty ? 0 : duration / (Double(chunk.count) / 16_000.0)
+ let chunkDoneAgeMs = self.elapsedMilliseconds(since: self.benchmarkRecordingStartedAt)
+ self.benchmarkLog(
+ "chunk_done index=\(chunkIndex) elapsedMs=\(self.elapsedMilliseconds(since: startedAt)) ageMs=\(chunkDoneAgeMs) " +
+ "samples=\(chunk.count) rawChars=\(rawText.count) cleanedChars=\(newText.count) rtf=\(String(format: "%.3f", rtf))"
+ )
// If transcription takes longer than the interval, skip next to prevent queue buildup
// This allows slower machines to still work without overwhelming the system
@@ -2563,6 +2635,7 @@ final class ASRService: ObservableObject {
error: error
)
DebugLogger.shared.error("❌ Streaming failed: \(error)", source: "ASRService")
+ self.benchmarkLog("chunk_fail index=\(chunkIndex) elapsedMs=\(self.elapsedMilliseconds(since: startedAt)) samples=\(chunk.count) error=\(error.localizedDescription)")
self.skipNextChunk = true
}
}
diff --git a/Sources/Fluid/Services/FluidAudioProvider.swift b/Sources/Fluid/Services/FluidAudioProvider.swift
index 5274f5fe..cca32e6b 100644
--- a/Sources/Fluid/Services/FluidAudioProvider.swift
+++ b/Sources/Fluid/Services/FluidAudioProvider.swift
@@ -165,7 +165,19 @@ final class FluidAudioProvider: TranscriptionProvider {
)
}
+ let startedAt = Date().timeIntervalSince1970
let result = try await manager.transcribe(samples, source: AudioSource.microphone)
+ let elapsedMs = Int(((Date().timeIntervalSince1970 - startedAt) * 1000).rounded())
+ let audioMs = Int((Double(samples.count) / 16_000.0 * 1000).rounded())
+ let rtf = audioMs > 0 ? Double(elapsedMs) / Double(audioMs) : 0
+ DebugLogger.shared.info(
+ """
+ ASR_BENCH provider_streaming_done samples=\(samples.count) audioMs=\(audioMs) \
+ elapsedMs=\(elapsedMs) textChars=\(result.text.trimmingCharacters(in: .whitespacesAndNewlines).count) \
+ rtf=\(String(format: "%.3f", rtf))
+ """,
+ source: "ASRBenchmark"
+ )
return ASRTranscriptionResult(text: result.text, confidence: result.confidence)
}
@@ -181,7 +193,9 @@ final class FluidAudioProvider: TranscriptionProvider {
// If the boosted final manager fails, fall back to the unboosted streaming
// manager so the user still gets a transcription (just without CTC rescoring).
do {
+ let startedAt = Date().timeIntervalSince1970
let result = try await manager.transcribe(samples, source: AudioSource.microphone)
+ self.logFinalBenchmark(samples: samples, text: result.text, startedAt: startedAt, usedFallback: false)
return ASRTranscriptionResult(text: result.text, confidence: result.confidence)
} catch {
guard let fallback = self.streamingAsrManager, fallback !== manager else {
@@ -191,11 +205,27 @@ final class FluidAudioProvider: TranscriptionProvider {
"FluidAudioProvider: Boosted final transcription failed (\(error.localizedDescription)), retrying without vocab boost",
source: "FluidAudioProvider"
)
+ let startedAt = Date().timeIntervalSince1970
let result = try await fallback.transcribe(samples, source: AudioSource.microphone)
+ self.logFinalBenchmark(samples: samples, text: result.text, startedAt: startedAt, usedFallback: true)
return ASRTranscriptionResult(text: result.text, confidence: result.confidence)
}
}
+ private func logFinalBenchmark(samples: [Float], text: String, startedAt: TimeInterval, usedFallback: Bool) {
+ let elapsedMs = Int(((Date().timeIntervalSince1970 - startedAt) * 1000).rounded())
+ let audioMs = Int((Double(samples.count) / 16_000.0 * 1000).rounded())
+ let rtf = audioMs > 0 ? Double(elapsedMs) / Double(audioMs) : 0
+ DebugLogger.shared.info(
+ """
+ ASR_BENCH provider_final_done samples=\(samples.count) audioMs=\(audioMs) \
+ elapsedMs=\(elapsedMs) textChars=\(text.trimmingCharacters(in: .whitespacesAndNewlines).count) \
+ rtf=\(String(format: "%.3f", rtf)) fallback=\(usedFallback)
+ """,
+ source: "ASRBenchmark"
+ )
+ }
+
func modelsExistOnDisk() -> Bool {
let baseCacheDir = AsrModels.defaultCacheDirectory().deletingLastPathComponent()
let selectedModel = self.modelOverride ?? SettingsStore.shared.selectedSpeechModel