|
|
|
@ -328,9 +328,9 @@ public final class SonioxLiveSession { |
|
|
|
lock.unlock() |
|
|
|
|
|
|
|
print("SonioxLiveSession: Sending finalize packet...") |
|
|
|
// 1. Send trailing 600ms (19200 bytes at 16kHz 16-bit PCM) silence frame |
|
|
|
// 1. Send trailing 300ms (9600 bytes at 16kHz 16-bit PCM) silence frame |
|
|
|
// so acoustic CTC model cleanly flushes and resolves trailing fast speech syllables |
|
|
|
let silenceChunk = Data(count: 19200) |
|
|
|
let silenceChunk = Data(count: 9600) |
|
|
|
let silenceMsg = URLSessionWebSocketTask.Message.data(silenceChunk) |
|
|
|
task.send(silenceMsg) { _ in } |
|
|
|
|
|
|
|
@ -493,16 +493,41 @@ public final class SonioxLiveSession { |
|
|
|
}() |
|
|
|
let isTerminatingMessage = gotFin || sessionEnded || sessionDone || isFinished || hasRawFinished |
|
|
|
|
|
|
|
// Emit live streaming deltas STRICTLY from Soniox-finalized tokens (is_final == true) |
|
|
|
// and ONLY during active speech, NEVER on terminal finalize packets! |
|
|
|
// When terminating, the final text is safely delivered to completeWithCurrentText() for full normalization |
|
|
|
// and atomic insertion, completely eliminating dual-paste race conditions! |
|
|
|
// Emit live streaming deltas in real time as the user speaks! |
|
|
|
// Soniox batches `is_final: true` tokens only every 6-8 seconds, which would make live typing feel lagging and out of sync. |
|
|
|
// However, completed words in `combined` (with a 1-word acoustic lookahead holdback) are 100% stable and appear within ~150ms! |
|
|
|
// We stream `fullCommitted` PLUS any completed words in `fullNonFinal` except the trailing word currently being uttered. |
|
|
|
var deltaToEmit: String? = nil |
|
|
|
if !triggerDetected && !isTerminatingMessage && !self.isFinalizing && fullCommitted.count > streamedPrefix.count { |
|
|
|
if fullCommitted.hasPrefix(streamedPrefix) { |
|
|
|
let delta = String(fullCommitted.dropFirst(streamedPrefix.count)) |
|
|
|
streamedPrefix = fullCommitted |
|
|
|
deltaToEmit = delta |
|
|
|
if !triggerDetected && !isTerminatingMessage && !self.isFinalizing { |
|
|
|
var stableCandidate = fullCommitted |
|
|
|
if let lastSpaceRange = fullNonFinal.rangeOfCharacter(from: .whitespacesAndNewlines, options: .backwards) { |
|
|
|
let completedNonFinal = String(fullNonFinal[..<lastSpaceRange.upperBound]) |
|
|
|
let trailingToken = String(fullNonFinal[lastSpaceRange.upperBound...]).trimmingCharacters(in: .whitespacesAndNewlines) |
|
|
|
if !trailingToken.isEmpty { |
|
|
|
// At least one subsequent word has started, so `completedNonFinal` has full acoustic lookahead |
|
|
|
stableCandidate = fullCommitted + completedNonFinal |
|
|
|
} else { |
|
|
|
// Trailing space at the very end of `fullNonFinal`: hold back the last word until the next word begins |
|
|
|
let trimmedNF = completedNonFinal.trimmingCharacters(in: .whitespacesAndNewlines) |
|
|
|
if let prevSpaceRange = trimmedNF.rangeOfCharacter(from: .whitespacesAndNewlines, options: .backwards) { |
|
|
|
stableCandidate = fullCommitted + String(trimmedNF[..<prevSpaceRange.upperBound]) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if stableCandidate.count > streamedPrefix.count { |
|
|
|
if stableCandidate.hasPrefix(streamedPrefix) { |
|
|
|
let delta = String(stableCandidate.dropFirst(streamedPrefix.count)) |
|
|
|
streamedPrefix = stableCandidate |
|
|
|
deltaToEmit = delta |
|
|
|
} else { |
|
|
|
let common = stableCandidate.commonPrefix(with: streamedPrefix) |
|
|
|
if stableCandidate.count > common.count { |
|
|
|
let delta = String(stableCandidate.dropFirst(common.count)) |
|
|
|
streamedPrefix = stableCandidate |
|
|
|
deltaToEmit = delta |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
lock.unlock() |
|
|
|
@ -962,19 +987,13 @@ public final class SonioxLiveSession { |
|
|
|
var cleaned: [String] = [] |
|
|
|
|
|
|
|
let stopWords: Set<String> = ["sex", "from", "no", "oh", "you", "all", "know", "that", "god", "loves", "men", "for", "day", "night", "mankind", "wanted", "moments"] |
|
|
|
let preserveWords: Set<String> = [ |
|
|
|
"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", |
|
|
|
"tab", "slash", "account", "telegram", "ide", "code", "chatgpt", "claude", |
|
|
|
"github", "git", "firefox", "chrome", "safari", "figma", "terminal", |
|
|
|
"notes", "music", "finder", "settings", "status", "cancel", "new", "close", "reopen", "search" |
|
|
|
] |
|
|
|
|
|
|
|
if faRatio >= 0.25 { |
|
|
|
for w in words { |
|
|
|
if matches(enRegex, in: w) && !matches(faRegex, in: w) { |
|
|
|
let cleanW = w.lowercased().trimmingCharacters(in: punctCharSet) |
|
|
|
// Strip ONLY known acoustic noise hallucinations; preserve legitimate English technical terms (Soniox, Voice, Sync, Mac, Push, Talk, API, etc.) |
|
|
|
if stopWords.contains(cleanW) { continue } |
|
|
|
if faRatio >= 0.70 && !preserveWords.contains(cleanW) { continue } |
|
|
|
} |
|
|
|
cleaned.append(w) |
|
|
|
} |
|
|
|
|