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: harden incremental parakeet previews

altic-dev1ce13665f5ee
diff --git a/Sources/Fluid/Services/ASRService.swift b/Sources/Fluid/Services/ASRService.swift
index 90e75bd7..35fd26d8 100644
--- a/Sources/Fluid/Services/ASRService.swift
+++ b/Sources/Fluid/Services/ASRService.swift
@@ -4674,8 +4674,25 @@ final class ASRService: ObservableObject {
return
}
- // Thread-safe copy of the data
- let chunk = self.audioBuffer.getPrefix(currentSampleCount)
+ // Once FluidAudio has an incremental session, copy only newly captured PCM.
+ // Other providers and the first incremental preview still receive the full prefix.
+ #if arch(arm64)
+ let incrementalProvider = self.transcriptionProvider as? FluidAudioProvider
+ let incrementalDeltaStart = incrementalProvider?.incrementalPreviewDeltaStart(
+ totalSampleCount: currentSampleCount
+ )
+ #else
+ let incrementalDeltaStart: Int? = nil
+ #endif
+ let chunk: [Float]
+ if let incrementalDeltaStart {
+ chunk = self.audioBuffer.getRange(
+ startingAt: incrementalDeltaStart,
+ count: currentSampleCount - incrementalDeltaStart
+ )
+ } else {
+ chunk = self.audioBuffer.getPrefix(currentSampleCount)
+ }
// Validate chunk is not empty (defensive check)
guard !chunk.isEmpty else {
@@ -4689,15 +4706,50 @@ final class ASRService: ObservableObject {
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)")
+ let newSamples = max(0, currentSampleCount - self.benchmarkLastChunkSampleCount)
+ self.benchmarkLastChunkSampleCount = currentSampleCount
+ let audioMilliseconds = Int((Double(currentSampleCount) / 16_000.0 * 1000).rounded())
+ self.benchmarkLog(
+ "chunk_start index=\(chunkIndex) ageMs=\(chunkAgeMs) samples=\(currentSampleCount) " +
+ "inputSamples=\(chunk.count) newSamples=\(newSamples) audioMs=\(audioMilliseconds) " +
+ "provider=\(self.transcriptionProvider.name)"
+ )
do {
- DebugLogger.shared.debug("Streaming chunk starting transcription (samples: \(chunk.count)) using \(self.transcriptionProvider.name)", source: "ASRService")
- let result = try await transcriptionExecutor.run { [provider = self.transcriptionProvider] in
+ DebugLogger.shared.debug("Streaming chunk starting transcription (samples: \(currentSampleCount), input: \(chunk.count)) using \(self.transcriptionProvider.name)", source: "ASRService")
+ let result: ASRTranscriptionResult
+ #if arch(arm64)
+ if incrementalDeltaStart != nil, let incrementalProvider {
+ do {
+ result = try await self.transcriptionExecutor.run {
+ try await incrementalProvider.transcribeStreamingDelta(
+ chunk,
+ totalSampleCount: currentSampleCount
+ )
+ }
+ } catch {
+ if Task.isCancelled || error is CancellationError {
+ throw CancellationError()
+ }
+ DebugLogger.shared.warning(
+ "Incremental delta preview failed; retrying with the full prefix",
+ source: "ASRService"
+ )
+ let fullPrefix = self.audioBuffer.getPrefix(currentSampleCount)
+ result = try await self.transcriptionExecutor.run { [provider = self.transcriptionProvider] in
+ try await provider.transcribeStreaming(fullPrefix)
+ }
+ }
+ } else {
+ result = try await self.transcriptionExecutor.run { [provider = self.transcriptionProvider] in
+ try await provider.transcribeStreaming(chunk)
+ }
+ }
+ #else
+ result = try await self.transcriptionExecutor.run { [provider = self.transcriptionProvider] in
try await provider.transcribeStreaming(chunk)
}
+ #endif
let duration = Date().timeIntervalSince(startTime)
DebugLogger.shared.debug(
@@ -4710,7 +4762,7 @@ final class ASRService: ObservableObject {
)
self.recordWordBoostHitIfAny(transcribedText: newText)
self.benchmarkCompletedStreamingChunks += 1
- self.lastProcessedSampleCount = chunk.count
+ self.lastProcessedSampleCount = currentSampleCount
// Mark first transcription as complete to clear loading state
if !self.hasCompletedFirstTranscription {
@@ -4730,11 +4782,11 @@ 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 rtf = duration / (Double(currentSampleCount) / 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))"
+ "samples=\(currentSampleCount) inputSamples=\(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
@@ -4748,7 +4800,7 @@ final class ASRService: ObservableObject {
}
} catch {
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.benchmarkLog("chunk_fail index=\(chunkIndex) elapsedMs=\(self.elapsedMilliseconds(since: startedAt)) samples=\(currentSampleCount) inputSamples=\(chunk.count) error=\(error.localizedDescription)")
self.skipNextChunk = true
}
}
diff --git a/Sources/Fluid/Services/FluidAudioProvider.swift b/Sources/Fluid/Services/FluidAudioProvider.swift
index 8020acf6..4c7f610d 100644
--- a/Sources/Fluid/Services/FluidAudioProvider.swift
+++ b/Sources/Fluid/Services/FluidAudioProvider.swift
@@ -252,6 +252,75 @@ final class FluidAudioProvider: TranscriptionProvider {
return ASRTranscriptionResult(text: result.text, confidence: result.confidence)
}
+ /// Returns the first sample not yet accepted by the active incremental session.
+ /// Callers can use this to copy only newly captured PCM instead of the full
+ /// growing recording on every live-preview tick.
+ func incrementalPreviewDeltaStart(totalSampleCount: Int) -> Int? {
+ Self.incrementalPreviewDeltaRange(
+ enabled: SettingsStore.shared.experimentalParakeetUnifiedFinalEnabled,
+ hasSession: self.incrementalSession != nil,
+ acceptedSampleCount: self.incrementalAcceptedSampleCount,
+ totalSampleCount: totalSampleCount
+ )?.lowerBound
+ }
+
+ static func incrementalPreviewDeltaRange(
+ enabled: Bool,
+ hasSession: Bool,
+ acceptedSampleCount: Int,
+ totalSampleCount: Int
+ ) -> Range<Int>? {
+ guard enabled,
+ hasSession,
+ acceptedSampleCount > self.incrementalChunkingThresholdSamples,
+ totalSampleCount > acceptedSampleCount
+ else { return nil }
+ return acceptedSampleCount..<totalSampleCount
+ }
+
+ /// Advances an existing incremental session with only the newly captured PCM.
+ /// Any failure invalidates the session; the caller should retry through
+ /// `transcribeStreaming(_:)` with the full prefix so normal fallback remains intact.
+ func transcribeStreamingDelta(
+ _ newSamples: [Float],
+ totalSampleCount: Int
+ ) async throws -> ASRTranscriptionResult {
+ guard let session = self.incrementalSession,
+ self.incrementalAcceptedSampleCount + newSamples.count == totalSampleCount
+ else {
+ self.resetIncrementalSession()
+ throw NSError(
+ domain: "FluidAudioProvider",
+ code: -4,
+ userInfo: [NSLocalizedDescriptionKey: "Incremental preview delta no longer matches the recording"]
+ )
+ }
+
+ let startedAt = Date().timeIntervalSince1970
+ do {
+ try await session.append(newSamples)
+ self.incrementalAcceptedSampleCount = totalSampleCount
+ let result = try await session.preview()
+ let text = result.text.trimmingCharacters(in: .whitespacesAndNewlines)
+ self.latestStreamingPreviewText = text
+ self.latestStreamingPreviewSampleCount = totalSampleCount
+ self.latestStreamingPreviewFinishedAt = Date().timeIntervalSince1970
+ self.logStreamingBenchmark(
+ sampleCount: totalSampleCount,
+ text: text,
+ startedAt: startedAt,
+ inputSampleCount: newSamples.count
+ )
+ return ASRTranscriptionResult(text: result.text, confidence: result.confidence)
+ } catch {
+ self.resetIncrementalSession()
+ if Task.isCancelled || error is CancellationError {
+ throw CancellationError()
+ }
+ throw error
+ }
+ }
+
func transcribeDictionaryTraining(_ samples: [Float]) async throws -> ASRTranscriptionResult {
guard let manager = self.streamingAsrManager else {
throw NSError(
@@ -587,6 +656,25 @@ final class FluidAudioProvider: TranscriptionProvider {
)
}
+ private func logStreamingBenchmark(
+ sampleCount: Int,
+ text: String,
+ startedAt: TimeInterval,
+ inputSampleCount: Int
+ ) {
+ let elapsedMs = Int(((Date().timeIntervalSince1970 - startedAt) * 1000).rounded())
+ let audioMs = Int((Double(sampleCount) / 16_000.0 * 1000).rounded())
+ let rtf = audioMs > 0 ? Double(elapsedMs) / Double(audioMs) : 0
+ DebugLogger.shared.info(
+ """
+ ASR_BENCH provider_streaming_delta_done samples=\(sampleCount) inputSamples=\(inputSampleCount) \
+ audioMs=\(audioMs) elapsedMs=\(elapsedMs) textChars=\(text.count) \
+ rtf=\(String(format: "%.3f", rtf))
+ """,
+ source: "ASRBenchmark"
+ )
+ }
+
func modelsExistOnDisk() -> Bool {
let selectedModel = self.modelOverride ?? SettingsStore.shared.selectedSpeechModel
switch selectedModel {
diff --git a/Sources/Fluid/UI/SettingsView.swift b/Sources/Fluid/UI/SettingsView.swift
index 9545a196..80e0d233 100644
--- a/Sources/Fluid/UI/SettingsView.swift
+++ b/Sources/Fluid/UI/SettingsView.swift
@@ -2914,6 +2914,7 @@ private extension SettingsView {
)
.textFieldStyle(.roundedBorder)
.frame(width: 170)
+ .accessibilityLabel("Spoken Send phrase")
}
HStack(alignment: .center) {
@@ -2938,6 +2939,7 @@ private extension SettingsView {
}
.pickerStyle(.menu)
.frame(width: 170, alignment: .trailing)
+ .accessibilityLabel("Spoken Send command")
}
}
.padding(.leading, 12)
diff --git a/Tests/FluidDictationIntegrationTests/HotkeyShortcutTests.swift b/Tests/FluidDictationIntegrationTests/HotkeyShortcutTests.swift
index a263cd05..c0ad8e91 100644
--- a/Tests/FluidDictationIntegrationTests/HotkeyShortcutTests.swift
+++ b/Tests/FluidDictationIntegrationTests/HotkeyShortcutTests.swift
@@ -170,6 +170,43 @@ final class HotkeyShortcutTests: XCTestCase {
XCTAssertNil(legacyBackup.settings.experimentalParakeetUnifiedFinalEnabled)
}
+ @MainActor
+ func testIncrementalParakeetCopiesOnlyTheUnacceptedTailAfterSessionStarts() {
+ XCTAssertEqual(
+ FluidAudioProvider.incrementalPreviewDeltaRange(
+ enabled: true,
+ hasSession: true,
+ acceptedSampleCount: 320_000,
+ totalSampleCount: 330_000
+ ),
+ 320_000..<330_000
+ )
+ XCTAssertNil(
+ FluidAudioProvider.incrementalPreviewDeltaRange(
+ enabled: false,
+ hasSession: true,
+ acceptedSampleCount: 320_000,
+ totalSampleCount: 330_000
+ )
+ )
+ XCTAssertNil(
+ FluidAudioProvider.incrementalPreviewDeltaRange(
+ enabled: true,
+ hasSession: false,
+ acceptedSampleCount: 320_000,
+ totalSampleCount: 330_000
+ )
+ )
+ XCTAssertNil(
+ FluidAudioProvider.incrementalPreviewDeltaRange(
+ enabled: true,
+ hasSession: true,
+ acceptedSampleCount: 240_000,
+ totalSampleCount: 250_000
+ )
+ )
+ }
+
@MainActor
func testLegacySystemModeBackupQueuesMicrophonePriorityMigration() async throws {
let document = await BackupService.shared.makeBackupDocument()
diff --git a/Tests/FluidDictationIntegrationTests/SpokenSendTests.swift b/Tests/FluidDictationIntegrationTests/SpokenSendTests.swift
index 0428e7d5..658c2690 100644
--- a/Tests/FluidDictationIntegrationTests/SpokenSendTests.swift
+++ b/Tests/FluidDictationIntegrationTests/SpokenSendTests.swift
@@ -2,6 +2,7 @@ import CoreGraphics
@testable import FluidVoice_Debug
import XCTest
+@MainActor
final class SpokenSendTests: XCTestCase {
func testDisabledFeatureLeavesTextUntouched() {
XCTAssertEqual(