@ -2,11 +2,22 @@ import Foundation
import AppKit
public final class SonioxLiveSession {
public static let primaryWsBase = " wss://translate.compare.soniox.com/compare/api/compare-websocket "
public static let fallbackWsBase = " wss://stt.compare.soniox.com/compare/api/compare-websocket "
public static let primaryWsBase = " wss://stt.newhorizonco.uk/ws/live-stt "
public static let fallbackWsBase = " wss://stt.newhorizonco.uk/ws/live-translate "
private static let apiKey = " nwh_live_Y54Zfil6MdYZEOu4IHlBzTWhGputnVbJ "
private static let userAgent = " Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 "
public static func origin ( for endpoint : String ) -> String {
return endpoint . contains ( " translate " ) ? " https://translate.compare.soniox.com " : " https://stt.compare.soniox.com "
return " https://stt.newhorizonco.uk "
}
private static func baseQuery ( withHints hints : [ String ] , targetLanguage : String ? ) -> String {
var parts : [ String ] = [ ]
let hintStr = hints . map { " language_hints= \( $0 ) " } . joined ( separator : " & " )
if ! hintStr . isEmpty { parts . append ( hintStr ) }
parts . append ( " key= \( apiKey ) " )
if let target = targetLanguage , ! target . isEmpty , target != " off " {
parts . append ( " target_language= \( target ) " )
}
return parts . joined ( separator : " & " )
}
// P r e - c o m p i l e d s t a t i c r e g e x e s f o r z e r o - a l l o c a t i o n h o t p a t h
@ -19,6 +30,9 @@ public final class SonioxLiveSession {
private static let punctCharSet = CharacterSet ( charactersIn : " .,!?:;،؛؟ \" '()[]{}«»-–— " )
public let targetLanguage : String ?
public let languageHints : [ String ]
private var isFallbackSwitched = false
private var initialAudioBuffer : [ Data ] = [ ]
public var isTranslationMode : Bool {
guard let target = targetLanguage , ! target . isEmpty , target != " off " else { return false }
return true
@ -32,10 +46,16 @@ public final class SonioxLiveSession {
private var isAborted = false
private let lock = NSLock ( )
// T r a n s c r i p t s & S t a l l t r a c k i n g
// T r a n s c r i p t s & S t a l l t r a c k i n g ( M u l t i - s e g m e n t c o n t i n u o u s s p e e c h + c h e c k p o i n t e d a u d i o b u f f e r )
private var archivedCommittedText : String = " "
private var committedFinalTokens : [ String ] = [ ]
private var currentNonFinalTokens : [ String ] = [ ]
private var streamedPrefix : String = " "
private var currentSegmentAudioBuffer = Data ( )
private var lastConfirmedWordAudioByteOffset : Int = 0
private var isReconnectingMidRecording = false
private var isFlushingReplayBuffer = false
private var segmentConnectTime : Date = Date ( )
private var sentAudioBytes : Int = 0
public private ( set ) var totalReceivedTokensCount : Int = 0
public private ( set ) var hasSilentStall : Bool = false
@ -58,8 +78,44 @@ public final class SonioxLiveSession {
isRecordingActive = active
if active {
lastDataTime = Date ( )
segmentConnectTime = Date ( )
}
lock . unlock ( )
}
// / T r u e w h e n a m i d - r e c o r d i n g d i s c o n n e c t / s t a l l o c c u r r e d a n d t h e r e i s > 0 . 3 5 s o f u n c o n f i r m e d t a i l a u d i o w a i t i n g t o b e t r a n s c r i b e d .
public var hasUntranscribedTailAudio : Bool {
lock . lock ( )
defer { lock . unlock ( ) }
let tailCount = max ( 0 , currentSegmentAudioBuffer . count - lastConfirmedWordAudioByteOffset )
let hadDrop = hasNetworkFailure || hasSilentStall || isReconnectingMidRecording || ! isConnected
return hadDrop && tailCount > 11200
}
// / R e t u r n s t h e a l r e a d y - c o n f i r m e d t r a n s c r i p t p r e f i x a c r o s s a l l c o m p l e t e d s e g m e n t s a n d f i n a l i z e d t o k e n s .
public func confirmedTranscriptPrefix ( ) -> String {
lock . lock ( )
let segCommitted = committedFinalTokens . joined ( ) . trimmingCharacters ( in : . whitespacesAndNewlines )
let rawPrefix : String
if archivedCommittedText . isEmpty {
rawPrefix = segCommitted
} else if segCommitted . isEmpty {
rawPrefix = archivedCommittedText
} else {
rawPrefix = archivedCommittedText + " " + segCommitted
}
lock . unlock ( )
return SonioxLiveSession . sanitizeText ( rawPrefix )
}
// / R e t u r n s t h e s l i c e o f r a w P C M a u d i o f r o m ` l a s t C o n f i r m e d W o r d A u d i o B y t e O f f s e t ` t o t h e e n d o f ` c u r r e n t S e g m e n t A u d i o B u f f e r ` .
public func untranscribedTailAudioData ( ) -> Data {
lock . lock ( )
defer { lock . unlock ( ) }
guard currentSegmentAudioBuffer . count > lastConfirmedWordAudioByteOffset else {
return Data ( )
}
return currentSegmentAudioBuffer . subdata ( in : lastConfirmedWordAudioByteOffset . . < currentSegmentAudioBuffer . count )
}
public var isReady : Bool {
@ -124,22 +180,12 @@ public final class SonioxLiveSession {
targetLanguage : String ? = nil ,
endpoint : String = SonioxLiveSession . primaryWsBase
) {
self . languageHints = languageHints
let effectiveTarget = targetLanguage ? ? ( SonioxSettings . shared . targetLanguage . isEnabled ? SonioxSettings . shared . targetLanguage . rawValue : nil )
self . targetLanguage = effectiveTarget
let hints = languageHints . map { " language_hints= \( $0 ) " } . joined ( separator : " & " )
var queryParams : [ String ] = [ ]
if ! hints . isEmpty {
queryParams . append ( hints )
}
queryParams . append ( " enable_speaker_diarization=false " )
queryParams . append ( " enable_language_identification=true " )
queryParams . append ( " enable_endpoint_detection=false " )
queryParams . append ( " providers=soniox " )
if let target = effectiveTarget , ! target . isEmpty , target != " off " {
queryParams . append ( " target_language= \( target ) " )
}
let urlStr = " \( endpoint ) ? " + queryParams . joined ( separator : " & " )
let queryStr = SonioxLiveSession . baseQuery ( withHints : languageHints , targetLanguage : effectiveTarget )
let urlStr = " \( endpoint ) ? \( queryStr ) "
guard let url = URL ( string : urlStr ) else { return }
var request = URLRequest ( url : url )
@ -164,49 +210,310 @@ public final class SonioxLiveSession {
if error = = nil && ! self . isClosed && ! self . isAborted {
self . isConnected = true
self . lastDataTime = Date ( )
self . initialAudioBuffer . removeAll ( )
self . lock . unlock ( )
self . onConnectionStateChanged ? ( true )
print ( " SonioxLiveSession: Connected successfully. " )
print ( " SonioxLiveSession: Connected successfully ( \( endpoint ) ) . " )
} else {
if let error = error {
print ( " SonioxLiveSession: Ping failed on \( endpoint ) : " , error )
}
if endpoint = = SonioxLiveSession . primaryWsBase && ! self . isClosed && ! self . isAborted && ! self . isFallbackSwitched {
self . lock . unlock ( )
self . switchToFallback ( )
return
}
self . hasNetworkFailure = true
self . lock . unlock ( )
self . onNetworkDegraded ? ( true )
if let error = error {
print ( " SonioxLiveSession: Ping failed: " , error )
}
}
}
startReceiving ( )
}
// / C a l l e d w h i l e u s e r i s a c t i v e l y s p e a k i n g t o d e t e c t h a l f - o p e n T C P s i l e n t s t a l l s
private func switchToFallback ( ) {
lock . lock ( )
guard ! isFallbackSwitched , ! isClosed , ! isAborted else {
lock . unlock ( )
return
}
isFallbackSwitched = true
print ( " SonioxLiveSession: ⚡️ Primary connection failed. Seamlessly switching to fallback endpoint: \( SonioxLiveSession . fallbackWsBase ) " )
webSocketTask ? . cancel ( with : . goingAway , reason : nil )
let endpoint = SonioxLiveSession . fallbackWsBase
let queryStr = SonioxLiveSession . baseQuery ( withHints : languageHints , targetLanguage : targetLanguage )
let urlStr = " \( endpoint ) ? \( queryStr ) "
guard let url = URL ( string : urlStr ) , let session = urlSession else {
hasNetworkFailure = true
lock . unlock ( )
onNetworkDegraded ? ( true )
return
}
var request = URLRequest ( url : url )
request . setValue ( SonioxLiveSession . userAgent , forHTTPHeaderField : " User-Agent " )
request . setValue ( SonioxLiveSession . origin ( for : endpoint ) , forHTTPHeaderField : " Origin " )
request . timeoutInterval = 10.0
let newTask = session . webSocketTask ( with : request )
self . webSocketTask = newTask
lock . unlock ( )
newTask . resume ( )
newTask . sendPing { [ weak self ] err in
guard let self = self else { return }
self . lock . lock ( )
if err = = nil && ! self . isClosed && ! self . isAborted {
self . isConnected = true
self . hasNetworkFailure = false
self . lastDataTime = Date ( )
let bufferedChunks = self . initialAudioBuffer
self . initialAudioBuffer . removeAll ( )
self . lock . unlock ( )
self . onConnectionStateChanged ? ( true )
print ( " SonioxLiveSession: ✅ Fallback endpoint connected! Flushing \( bufferedChunks . count ) buffered chunks... " )
for chunk in bufferedChunks {
newTask . send ( . data ( chunk ) ) { _ in }
}
} else {
self . hasNetworkFailure = true
self . lock . unlock ( )
self . onNetworkDegraded ? ( true )
if let err = err {
print ( " SonioxLiveSession: Fallback ping failed: " , err )
}
}
}
startReceiving ( )
}
// / C a l l e d w h i l e u s e r i s a c t i v e l y s p e a k i n g t o d e t e c t h a l f - o p e n T C P s i l e n t s t a l l s a n d a u t o - r e c o n n e c t s e a m l e s s l y
public func checkSilentStallWhileSpeaking ( ) {
lock . lock ( )
let connected = isConnected && ! isClosed && ! isAborted
let activeRec = isRecordingActive && ! isFinalizing && ! isReconnectingMidRecording
let sent = sentAudioBytes
let elapsedSinceData = Date ( ) . timeIntervalSince ( lastDataTime )
lock . unlock ( )
// I f > 1 . 5 s o f a u d i o ( 4 8 K B ) w a s s e n t w h i l e s p e a k i n g , b u t 0 f r a m e s a r r i v e d f o r > 3 . 0 s :
if connected && sent > 48000 && elapsedSinceData > 3.0 {
// I f > 1 . 0 s o f a u d i o ( 3 2 K B ) w a s s e n t w h i l e s p e a k i n g , b u t 0 f r a m e s a r r i v e d f o r > 3 . 5 s :
if connected && sent > 32 000 && elapsedSinceData > 3.5 {
lock . lock ( )
hasSilentStall = true
lock . unlock ( )
DispatchQueue . main . async { [ weak self ] in
self ? . onNetworkDegraded ? ( true )
}
if activeRec {
triggerMidRecordingReconnect ( cleanArchive : false , reason : " Silent TCP/STT stall ( \( String ( format : " %.1f " , elapsedSinceData ) ) s without frames while speaking) " )
}
}
}
// / S e a m l e s s l y a r c h i v e s c o n f i r m e d t r a n s c r i p t t o k e n s a n d r e c o n n e c t s a f r e s h W e b S o c k e t i n t h e b a c k g r o u n d
// / w h i l e t h e u s e r c o n t i n u e s h o l d i n g P u s h - T o - T a l k / s p e a k i n g f o r 5 – 1 0 + m i n u t e s !
public func triggerMidRecordingReconnect ( cleanArchive : Bool , reason : String ) {
lock . lock ( )
guard isRecordingActive , ! isFinalizing , ! isClosed , ! isAborted , ! isReconnectingMidRecording else {
lock . unlock ( )
return
}
isReconnectingMidRecording = true
isConnected = false
let segCommitted = committedFinalTokens . joined ( )
let segNonFinal = currentNonFinalTokens . joined ( )
let toArchive = cleanArchive ? ( segCommitted + segNonFinal ) : segCommitted
let cleanToArchive = toArchive . trimmingCharacters ( in : . whitespacesAndNewlines )
if ! cleanToArchive . isEmpty {
if archivedCommittedText . isEmpty {
archivedCommittedText = cleanToArchive
} else {
archivedCommittedText += " " + cleanToArchive
}
}
committedFinalTokens . removeAll ( )
currentNonFinalTokens . removeAll ( )
if cleanArchive {
currentSegmentAudioBuffer . removeAll ( keepingCapacity : true )
lastConfirmedWordAudioByteOffset = 0
} else if lastConfirmedWordAudioByteOffset > 0 && lastConfirmedWordAudioByteOffset <= currentSegmentAudioBuffer . count {
currentSegmentAudioBuffer . removeSubrange ( 0. . < lastConfirmedWordAudioByteOffset )
lastConfirmedWordAudioByteOffset = 0
}
if ! archivedCommittedText . isEmpty {
streamedPrefix = archivedCommittedText + " "
}
let bufferedBytes = currentSegmentAudioBuffer . count
let archivedCount = archivedCommittedText . count
let oldTask = webSocketTask
let oldSession = urlSession
webSocketTask = nil
urlSession = nil
lock . unlock ( )
print ( " SonioxLiveSession: 🔄 Mid-recording auto-reconnect triggered ( \( reason ) ). Archived \( archivedCount ) chars, buffering \( bufferedBytes ) bytes of unconfirmed audio... " )
oldTask ? . cancel ( with : . goingAway , reason : nil )
oldSession ? . invalidateAndCancel ( )
attemptMidRecordingConnect ( attempt : 1 )
}
private func attemptMidRecordingConnect ( attempt : Int ) {
lock . lock ( )
guard isRecordingActive , ! isFinalizing , ! isClosed , ! isAborted else {
isReconnectingMidRecording = false
lock . unlock ( )
return
}
let endpoint = ( attempt % 2 = = 1 ) ? SonioxLiveSession . primaryWsBase : SonioxLiveSession . fallbackWsBase
let queryStr = SonioxLiveSession . baseQuery ( withHints : languageHints , targetLanguage : targetLanguage )
let urlStr = " \( endpoint ) ? \( queryStr ) "
guard let url = URL ( string : urlStr ) else {
isReconnectingMidRecording = false
lock . unlock ( )
return
}
var request = URLRequest ( url : url )
request . setValue ( SonioxLiveSession . userAgent , forHTTPHeaderField : " User-Agent " )
request . setValue ( SonioxLiveSession . origin ( for : endpoint ) , forHTTPHeaderField : " Origin " )
request . timeoutInterval = 6.0
let config = URLSessionConfiguration . default
config . waitsForConnectivity = false
config . requestCachePolicy = . reloadIgnoringLocalCacheData
let newSession = URLSession ( configuration : config )
let newTask = newSession . webSocketTask ( with : request )
self . urlSession = newSession
self . webSocketTask = newTask
lock . unlock ( )
newTask . resume ( )
newTask . sendPing { [ weak self ] err in
guard let self = self else { return }
self . lock . lock ( )
if err = = nil && ! self . isClosed && ! self . isAborted && ! self . isFinalizing {
self . isConnected = true
self . hasNetworkFailure = false
self . hasSilentStall = false
self . isReconnectingMidRecording = false
self . isFlushingReplayBuffer = true
self . lastDataTime = Date ( )
self . segmentConnectTime = Date ( )
self . sentAudioBytes = 0
let replaySnapshot = self . currentSegmentAudioBuffer
self . lock . unlock ( )
self . onNetworkDegraded ? ( false )
self . onConnectionStateChanged ? ( true )
print ( " SonioxLiveSession: ✅ Mid-recording reconnect succeeded on attempt \( attempt ) ( \( endpoint ) )! Replaying \( replaySnapshot . count ) bytes of buffered speech... " )
self . startReceiving ( )
self . flushReplayBufferToSocket ( task : newTask , offset : 0 )
} else {
let shouldRetry = self . isRecordingActive && ! self . isFinalizing && ! self . isClosed && ! self . isAborted
self . webSocketTask = nil
self . urlSession = nil
self . lock . unlock ( )
newTask . cancel ( )
newSession . invalidateAndCancel ( )
if shouldRetry {
let delay = min ( 1.5 , 0.5 + Double ( min ( attempt , 4 ) ) * 0.25 )
DispatchQueue . global ( qos : . userInitiated ) . asyncAfter ( deadline : . now ( ) + delay ) { [ weak self ] in
self ? . attemptMidRecordingConnect ( attempt : attempt + 1 )
}
}
}
}
}
private func flushReplayBufferToSocket ( task : URLSessionWebSocketTask , offset : Int ) {
lock . lock ( )
guard isConnected , ! isClosed , ! isAborted , ! isFinalizing , webSocketTask = = = task else {
isFlushingReplayBuffer = false
lock . unlock ( )
return
}
let total = currentSegmentAudioBuffer . count
if offset >= total {
isFlushingReplayBuffer = false
lock . unlock ( )
return
}
let chunkSize = 32000 // 1 . 0 s c h u n k
let end = min ( offset + chunkSize , total )
let slice = currentSegmentAudioBuffer . subdata ( in : offset . . < end )
lock . unlock ( )
task . send ( . data ( slice ) ) { [ weak self ] err in
guard let self = self else { return }
if let err = err {
print ( " SonioxLiveSession: Error flushing replay buffer: " , err )
self . lock . lock ( )
self . isFlushingReplayBuffer = false
self . hasNetworkFailure = true
self . isConnected = false
let activeRec = self . isRecordingActive && ! self . isFinalizing && ! self . isClosed && ! self . isAborted
self . lock . unlock ( )
if activeRec {
self . triggerMidRecordingReconnect ( cleanArchive : false , reason : " Replay flush send error " )
}
return
}
self . lock . lock ( )
self . sentAudioBytes += slice . count
self . lock . unlock ( )
self . flushReplayBufferToSocket ( task : task , offset : end )
}
}
public func sendAudioChunk ( _ data : Data ) {
lock . lock ( )
guard ! isFinalizing , ! isClosed , ! isAborted , let task = webSocketTask else {
guard ! isFinalizing , ! isClosed , ! isAborted else {
lock . unlock ( )
return
}
currentSegmentAudioBuffer . append ( data )
if ! isConnected {
if initialAudioBuffer . count < 120 {
initialAudioBuffer . append ( data )
}
let shouldReconnect = isRecordingActive && hasNetworkFailure && ! isReconnectingMidRecording
lock . unlock ( )
if shouldReconnect {
triggerMidRecordingReconnect ( cleanArchive : false , reason : " Audio chunk arrived while disconnected " )
}
return
}
if isReconnectingMidRecording || isFlushingReplayBuffer {
lock . unlock ( )
return
}
guard let task = webSocketTask else {
lock . unlock ( )
return
}
// P r o a c t i v e s e g m e n t r o t a t i o n a t > 1 5 0 s w h e n a t a c l e a n c o m m i t t e d p a u s e b o u n d a r y s o s e r v e r n e v e r k i l l s l o n g 5 – 1 0 m s t r e a m s
let segAge = Date ( ) . timeIntervalSince ( segmentConnectTime )
let canRotateProactively = segAge > 150.0 && currentNonFinalTokens . isEmpty && ! committedFinalTokens . isEmpty
lock . unlock ( )
if canRotateProactively {
triggerMidRecordingReconnect ( cleanArchive : true , reason : " Proactive 150s continuous speech segment rollover " )
return
}
let t0 = Date ( )
let message = URLSessionWebSocketTask . Message . data ( data )
task . send ( message ) { [ weak self ] error in
@ -214,10 +521,19 @@ public final class SonioxLiveSession {
if let error = error {
print ( " SonioxLiveSession: Error streaming audio chunk: " , error )
self . lock . lock ( )
if ! self . isFallbackSwitched && ! self . isFinalizing && ! self . isClosed && ! self . isAborted && self . archivedCommittedText . isEmpty && self . committedFinalTokens . isEmpty {
self . lock . unlock ( )
self . switchToFallback ( )
return
}
self . hasNetworkFailure = true
self . isConnected = false
let activeRec = self . isRecordingActive && ! self . isFinalizing && ! self . isClosed && ! self . isAborted
self . lock . unlock ( )
self . onNetworkDegraded ? ( true )
if activeRec {
self . triggerMidRecordingReconnect ( cleanArchive : false , reason : " Send audio chunk error: \( error . localizedDescription ) " )
}
} else {
self . lock . lock ( )
self . sentAudioBytes += data . count
@ -232,15 +548,24 @@ public final class SonioxLiveSession {
public func finalizeStream ( ) {
lock . lock ( )
guard ! isFinalizing , ! isClosed , ! isAborted , let task = webSocketTask else {
guard ! isFinalizing , ! isClosed , ! isAborted else {
lock . unlock ( )
return
}
isFinalizing = true
let connected = isConnected && ! isReconnectingMidRecording
let task = webSocketTask
lock . unlock ( )
guard connected , let task = task else {
print ( " SonioxLiveSession: finalizeStream called while socket disconnected/reconnecting — completing immediately for tail recovery. " )
completeWithCurrentText ( )
return
}
print ( " SonioxLiveSession: Sending finalize packet... " )
// 1 . S e n d t r a i l i n g 3 0 0 m s ( 9 6 0 0 b y t e s a t 1 6 k H z 1 6 - b i t P C M ) s i l e n c e f r a m e
// s o a c o u s t i c C T C m o d e l c l e a n l y f l u s h e s a n d r e s o l v e s t r a i l i n g f a s t s p e e c h s y l l a b l e s
let silenceChunk = Data ( count : 9600 )
let silenceMsg = URLSessionWebSocketTask . Message . data ( silenceChunk )
task . send ( silenceMsg ) { _ in }
@ -314,14 +639,15 @@ public final class SonioxLiveSession {
self . lock . lock ( )
self . hasNetworkFailure = true
self . isConnected = false
let activeRecording = self . isRecordingActive
let activeRecording = self . isRecordingActive && ! self . isFinalizing && ! self . isClosed && ! self . isAborted
self . lock . unlock ( )
self . onNetworkDegraded ? ( true )
if ! activeRecording {
self . completeWithCurrentText ( )
} else {
print ( " SonioxLiveSession: Network dropped mid-recording. Audio is buffered for Smart Recovery upon key release. " )
print ( " SonioxLiveSession: Network dropped mid-recording. Triggering automatic live reconnect... " )
self . triggerMidRecordingReconnect ( cleanArchive : false , reason : " WebSocket receive error: \( error . localizedDescription ) " )
}
}
}
@ -376,37 +702,77 @@ public final class SonioxLiveSession {
totalReceivedTokensCount += tokenCountDelta
if ! newFinals . isEmpty {
committedFinalTokens . append ( contentsOf : newFinals )
// A d v a n c e c o n f i r m e d a u d i o c h e c k p o i n t i n ` c u r r e n t S e g m e n t A u d i o B u f f e r ` :
// K e e p a s a f e t y a c o u s t i c l o o k b a c k p r o p o r t i o n a l t o u n f i n a l i z e d w o r d s ( p l u s 0 . 8 s = 2 5 6 0 0 b y t e s )
let unfinalizedWords = newNonFinals . joined ( ) . components ( separatedBy : . whitespacesAndNewlines ) . filter { ! $0 . isEmpty } . count
let safetyLookbackBytes = max ( 25600 , ( unfinalizedWords + 1 ) * 16000 )
if currentSegmentAudioBuffer . count > safetyLookbackBytes {
lastConfirmedWordAudioByteOffset = max ( lastConfirmedWordAudioByteOffset , currentSegmentAudioBuffer . count - safetyLookbackBytes )
}
}
currentNonFinalTokens = newNonFinals
let fullCommitted = committedFinalTokens . joined ( )
let fullNonFinal = currentNonFinalTokens . joined ( )
let combined = fullCommitted + fullNonFinal
let segmentCombined = fullCommitted + fullNonFinal
let combined : String
if archivedCommittedText . isEmpty {
combined = segmentCombined
} else if segmentCombined . isEmpty {
combined = archivedCommittedText
} else {
let cleanSeg = segmentCombined . trimmingCharacters ( in : . whitespaces )
combined = archivedCommittedText + ( cleanSeg . isEmpty ? " " : ( " " + cleanSeg ) )
}
// C h e c k f o r i n - f l i g h t e n d t r i g g e r ( " a n d " , " ت م ا م " , " e n d " ) i n c o m b i n e d t e x t
// C h e c k f o r i n - f l i g h t e n d t r i g g e r ( " a n d " , " ت م ا م " , " e n d " ) O N L Y i n h a n d s - f r e e / t o g g l e m o d e a n d O N L Y o n f i n a l i z e d t o k e n s !
// N e v e r t r i g g e r a u t o - s t o p d u r i n g P u s h - T o - T a l k w h i l e u s e r i s p h y s i c a l l y h o l d i n g t h e h o t k e y !
var triggerDetected = false
let trimmedCombined = combined . trimmingCharacters ( in : . whitespacesAndNewlines )
let currentWords = trimmedCombined . components ( separatedBy : . whitespacesAndNewlines ) . filter { ! $0 . isEmpty }
if let lastRaw = currentWords . last {
let cleanLast = InputBoxManager . stripPunctuation ( lastRaw ) . lowercased ( )
if cleanLast = = " and " || cleanLast = = " تمام " || cleanLast = = " end " {
triggerDetected = true
if HotkeyManager . shared . currentMode != . pushToTalk && currentNonFinalTokens . isEmpty && ! newFinals . isEmpty {
let trimmedCombined = combined . trimmingCharacters ( in : . whitespacesAndNewlines )
let currentWords = trimmedCombined . components ( separatedBy : . whitespacesAndNewlines ) . filter { ! $0 . isEmpty }
if let lastRaw = currentWords . last {
let cleanLast = InputBoxManager . stripPunctuation ( lastRaw ) . lowercased ( )
if cleanLast = = " and " || cleanLast = = " تمام " || cleanLast = = " end " {
triggerDetected = true
}
}
}
// E m i t l i v e s t r e a m i n g d e l t a s S T R I C T L Y f r o m S o n i o x - f i n a l i z e d t o k e n s ( i s _ f i n a l = = t r u e ) :
// C h e c k i f t h i s m e s s a g e i n d i c a t e s e n d o f s e s s i o n o r f i n a l d e l i v e r y
let sessionEnded = json [ " session_ended " ] as ? Bool ? ? false
let sessionDone = ( json [ " type " ] as ? String ) = = " session_done "
let isFinished = json [ " finished " ] as ? Bool ? ? false
let hasRawFinished : Bool = {
guard let raw = json [ " raw " ] as ? String else { return false }
return raw . contains ( " \" finished \" :true " ) || raw . contains ( " \" finished \" : true " )
} ( )
let isTerminatingMessage = gotFin || sessionEnded || sessionDone || isFinished || hasRawFinished
let stillActivelyRecording = self . isRecordingActive && ! self . isFinalizing
// E m i t l i v e s t r e a m i n g d e l t a s i n r e a l t i m e a s t h e u s e r s p e a k s !
// W e s t r e a m c o m p l e t e d w o r d s i n ` c o m b i n e d ` w i t h a 1 - w o r d a c o u s t i c l o o k a h e a d h o l d b a c k s o e v e r y e m i t t e d c h u n k e n d s w i t h a t r a i l i n g s p a c e .
var deltaToEmit : String ? = nil
if ! triggerDetected && fullCommitted . count > streamedPrefix . count {
if fullCommitted . hasPrefix ( streamedPrefix ) {
let delta = String ( fullCommitted . dropFirst ( streamedPrefix . count ) )
streamedPrefix = fullCommitted
deltaToEmit = delta
} else {
let common = fullCommitted . commonPrefix ( with : streamedPrefix )
if fullCommitted . count > common . count {
let delta = String ( fullCommitted . dropFirst ( common . count ) )
streamedPrefix = fullCommitted
if ! triggerDetected && ! isTerminatingMessage && ! self . isFinalizing {
let rTrimmedCombined = combined . replacingOccurrences ( of : # " \ s+$ " # , with : " " , options : . regularExpression )
var stableCandidate = " "
if let lastSpaceRange = rTrimmedCombined . rangeOfCharacter ( from : . whitespacesAndNewlines , options : . backwards ) {
stableCandidate = String ( rTrimmedCombined [ . . < lastSpaceRange . upperBound ] )
}
if stableCandidate . count > streamedPrefix . count {
if stableCandidate . hasPrefix ( streamedPrefix ) {
let delta = String ( stableCandidate . dropFirst ( streamedPrefix . count ) )
streamedPrefix = stableCandidate
deltaToEmit = delta
} else {
let streamedWordsCount = streamedPrefix . components ( separatedBy : . whitespacesAndNewlines ) . filter { ! $0 . isEmpty } . count
let candidateWords = stableCandidate . components ( separatedBy : . whitespacesAndNewlines ) . filter { ! $0 . isEmpty }
if candidateWords . count > streamedWordsCount {
let delta = candidateWords . dropFirst ( streamedWordsCount ) . joined ( separator : " " ) + " "
streamedPrefix = stableCandidate
deltaToEmit = delta
}
}
}
}
@ -427,18 +793,33 @@ public final class SonioxLiveSession {
self ? . onPartialText ? ( combined )
}
}
}
let sessionEnded = json [ " session_ended " ] as ? Bool ? ? false
let sessionDone = ( json [ " type " ] as ? String ) = = " session_done "
let isFinished = json [ " finished " ] as ? Bool ? ? false
let hasRawFinished : Bool = {
guard let raw = json [ " raw " ] as ? String else { return false }
return raw . contains ( " \" finished \" :true " ) || raw . contains ( " \" finished \" : true " )
} ( )
if gotFin || sessionEnded || sessionDone || isFinished || hasRawFinished {
completeWithCurrentText ( )
if isTerminatingMessage {
if stillActivelyRecording {
print ( " SonioxLiveSession: Server ended WebSocket segment while user is still speaking — seamlessly rotating to fresh segment... " )
triggerMidRecordingReconnect ( cleanArchive : true , reason : " Server ended WebSocket segment while recording is still active " )
} else {
completeWithCurrentText ( )
}
}
} else {
let sessionEnded = json [ " session_ended " ] as ? Bool ? ? false
let sessionDone = ( json [ " type " ] as ? String ) = = " session_done "
let isFinished = json [ " finished " ] as ? Bool ? ? false
let hasRawFinished : Bool = {
guard let raw = json [ " raw " ] as ? String else { return false }
return raw . contains ( " \" finished \" :true " ) || raw . contains ( " \" finished \" : true " )
} ( )
if sessionEnded || sessionDone || isFinished || hasRawFinished {
lock . lock ( )
let stillActivelyRecording = self . isRecordingActive && ! self . isFinalizing
lock . unlock ( )
if stillActivelyRecording {
triggerMidRecordingReconnect ( cleanArchive : true , reason : " Server session_done while recording is still active " )
} else {
completeWithCurrentText ( )
}
}
}
}
@ -446,7 +827,15 @@ public final class SonioxLiveSession {
lock . lock ( )
let fullCommitted = committedFinalTokens . joined ( )
let fullNonFinal = currentNonFinalTokens . joined ( )
let rawCombined = fullCommitted . isEmpty ? fullNonFinal : ( fullCommitted + fullNonFinal )
let segCombined = fullCommitted . isEmpty ? fullNonFinal : ( fullCommitted + fullNonFinal )
let rawCombined : String
if archivedCommittedText . isEmpty {
rawCombined = segCombined
} else if segCombined . isEmpty {
rawCombined = archivedCommittedText
} else {
rawCombined = archivedCommittedText + " " + segCombined . trimmingCharacters ( in : . whitespaces )
}
lock . unlock ( )
return SonioxLiveSession . sanitizeText ( rawCombined )
}
@ -457,10 +846,23 @@ public final class SonioxLiveSession {
lock . unlock ( )
return
}
let tailCount = max ( 0 , currentSegmentAudioBuffer . count - lastConfirmedWordAudioByteOffset )
let hadDrop = hasNetworkFailure || hasSilentStall || isReconnectingMidRecording || ! isConnected
let needsTailRecovery = hadDrop && tailCount > 11200
isClosed = true
isReconnectingMidRecording = false
let fullCommitted = committedFinalTokens . joined ( )
let fullNonFinal = currentNonFinalTokens . joined ( )
let rawCombined = fullCommitted . isEmpty ? fullNonFinal : ( fullCommitted + fullNonFinal )
let segCombined = fullCommitted . isEmpty ? fullNonFinal : ( fullCommitted + fullNonFinal )
let rawCombined : String
if archivedCommittedText . isEmpty {
rawCombined = segCombined
} else if segCombined . isEmpty {
rawCombined = archivedCommittedText
} else {
rawCombined = archivedCommittedText + " " + segCombined . trimmingCharacters ( in : . whitespaces )
}
let cleaned = SonioxLiveSession . sanitizeText ( rawCombined )
let failureOccurred = hasNetworkFailure
@ -474,7 +876,9 @@ public final class SonioxLiveSession {
stopKeepAliveHeartbeat ( )
DispatchQueue . main . async {
if cleaned . isEmpty && failureOccurred {
if needsTailRecovery {
cb ? ( . failure ( NSError ( domain : " SonioxLiveSession " , code : - 103 , userInfo : [ NSLocalizedDescriptionKey : " Partial transcript with \( tailCount ) bytes of untranscribed tail audio " ] ) ) )
} else if cleaned . isEmpty && failureOccurred {
cb ? ( . failure ( NSError ( domain : " SonioxLiveSession " , code : - 101 , userInfo : [ NSLocalizedDescriptionKey : " WebSocket network failure with empty transcript " ] ) ) )
} else {
cb ? ( . success ( cleaned ) )
@ -490,6 +894,10 @@ public final class SonioxLiveSession {
isConnected = false
isRecordingActive = false
isFinalizing = false
isReconnectingMidRecording = false
isFlushingReplayBuffer = false
initialAudioBuffer . removeAll ( )
currentSegmentAudioBuffer . removeAll ( )
onPartialText = nil
onCommittedDelta = nil
onEndTriggerDetected = nil
@ -532,6 +940,128 @@ public final class SonioxLiveSession {
let durationSec = Double ( pcmData . count ) / 32000.0
print ( " SonioxLiveSession: Initiating Smart Burst Recovery: \( pcmData . count ) bytes ( \( String ( format : " %.1f " , durationSec ) ) s audio, target: \( effectiveTarget ? ? " orig " ) )... " )
// F o r m u l t i - m i n u t e r e c o r d i n g s ( > 6 0 s = 1 , 9 2 0 , 0 0 0 b y t e s ) , s p l i t i n t o ~ 6 0 s s i l e n c e - a l i g n e d c h u n k s
// s o i n d i v i d u a l W e b S o c k e t b u r s t c o n n e c t i o n s n e v e r h i t s e r v e r b u f f e r l i m i t s o r t i m e o u t s !
let maxSingleBurstBytes = 1_920_000
if pcmData . count > maxSingleBurstBytes {
let chunks = splitIntoSilenceAlignedChunks ( pcmData : pcmData , targetChunkBytes : maxSingleBurstBytes )
print ( " SonioxLiveSession: Splitting long recovery audio ( \( String ( format : " %.1f " , durationSec ) ) s) into \( chunks . count ) sequential chunks... " )
var recoveredParts : [ String ] = [ ]
func processChunk ( at index : Int ) {
if isCancelled ( ) {
completion ( . failure ( NSError ( domain : " SonioxLiveSession " , code : - 999 , userInfo : [ NSLocalizedDescriptionKey : " Cancelled by Escape " ] ) ) )
return
}
if index >= chunks . count {
let combinedText = recoveredParts . joined ( separator : " " ) . trimmingCharacters ( in : . whitespacesAndNewlines )
if ! combinedText . isEmpty {
completion ( . success ( combinedText ) )
} else {
completion ( . failure ( NSError ( domain : " SonioxLiveSession " , code : - 104 , userInfo : [ NSLocalizedDescriptionKey : " Empty chunked recovery result " ] ) ) )
}
return
}
DispatchQueue . main . async {
onStatusUpdate ? ( " PART \( index + 1 ) / \( chunks . count ) " )
}
retrySingleChunk (
pcmData : chunks [ index ] ,
languageHints : languageHints ,
effectiveTarget : effectiveTarget ,
isCancelled : isCancelled ,
onProgress : onProgress ,
onStatusUpdate : onStatusUpdate
) { result in
switch result {
case . success ( let partText ) :
let cleanPart = partText . trimmingCharacters ( in : . whitespacesAndNewlines )
if ! cleanPart . isEmpty {
recoveredParts . append ( cleanPart )
}
processChunk ( at : index + 1 )
case . failure ( let err ) :
if ! recoveredParts . isEmpty {
// S a l v a g e a l r e a d y - r e c o v e r e d c h u n k s i f a l a t e r c h u n k f a i l s
let partial = recoveredParts . joined ( separator : " " ) . trimmingCharacters ( in : . whitespacesAndNewlines )
completion ( . success ( partial ) )
} else {
completion ( . failure ( err ) )
}
}
}
}
processChunk ( at : 0 )
return
}
retrySingleChunk (
pcmData : pcmData ,
languageHints : languageHints ,
effectiveTarget : effectiveTarget ,
isCancelled : isCancelled ,
onProgress : onProgress ,
onStatusUpdate : onStatusUpdate ,
completion : completion
)
}
// / S p l i t s a m u l t i - m i n u t e 1 6 k H z 1 6 - b i t m o n o P C M b u f f e r a t l o w - e n e r g y s i l e n c e b o u n d a r i e s n e a r ` t a r g e t C h u n k B y t e s ` .
private static func splitIntoSilenceAlignedChunks ( pcmData : Data , targetChunkBytes : Int ) -> [ Data ] {
var chunks : [ Data ] = [ ]
var offset = 0
let total = pcmData . count
let searchWindowBytes = 160_000 // S e a r c h w i t h i n l a s t 5 . 0 s o f e a c h 6 0 s w i n d o w f o r l o w e s t e n e r g y p a u s e
let windowStepBytes = 3200 // 1 0 0 m s w i n d o w
while offset < total {
let remaining = total - offset
if remaining <= targetChunkBytes + 160_000 {
chunks . append ( pcmData . subdata ( in : offset . . < total ) )
break
}
var bestCut = offset + targetChunkBytes
let searchStart = max ( offset + targetChunkBytes - searchWindowBytes , offset + 64000 )
let searchEnd = min ( offset + targetChunkBytes + 32000 , total - windowStepBytes )
if searchEnd > searchStart {
var minEnergy : Float = . greatestFiniteMagnitude
pcmData . withUnsafeBytes { rawBuf in
guard let basePtr = rawBuf . baseAddress else { return }
var pos = searchStart & ~ 1 // a l i g n t o 2 - b y t e I n t 1 6 s a m p l e
while pos < searchEnd {
let sampleCount = windowStepBytes / 2
let ptr = basePtr . advanced ( by : pos ) . assumingMemoryBound ( to : Int16 . self )
var sumSq : Float = 0
for i in 0. . < sampleCount {
let s = Float ( ptr [ i ] )
sumSq += s * s
}
if sumSq < minEnergy {
minEnergy = sumSq
bestCut = pos + ( windowStepBytes / 2 )
}
pos += windowStepBytes
}
}
}
bestCut = bestCut & ~ 1
chunks . append ( pcmData . subdata ( in : offset . . < bestCut ) )
offset = bestCut
}
return chunks
}
private static func retrySingleChunk (
pcmData : Data ,
languageHints : [ String ] ,
effectiveTarget : String ? ,
isCancelled : @ escaping ( ) -> Bool ,
onProgress : ( ( String ) -> Void ) ? ,
onStatusUpdate : ( ( String ) -> Void ) ? ,
completion : @ escaping ( Result < String , Error > ) -> Void
) {
// M u l t i - s t a g e r e s i l i e n t r e t r y p i p e l i n e a c r o s s d u a l e n d p o i n t s w i t h a u t o m a t i c b a c k o f f :
// A t t e m p t 1 : P r i m a r y W S
// A t t e m p t 2 : F a l l b a c k W S
@ -614,19 +1144,8 @@ public final class SonioxLiveSession {
completion : @ escaping ( Result < String , Error > ) -> Void
) {
let isTranslationMode = targetLanguage != nil && ! targetLanguage ! . isEmpty && targetLanguage != " off "
let hints = languageHints . map { " language_hints= \( $0 ) " } . joined ( separator : " & " )
var queryParams : [ String ] = [ ]
if ! hints . isEmpty {
queryParams . append ( hints )
}
queryParams . append ( " enable_speaker_diarization=false " )
queryParams . append ( " enable_language_identification=true " )
queryParams . append ( " enable_endpoint_detection=false " )
queryParams . append ( " providers=soniox " )
if let target = targetLanguage , ! target . isEmpty , target != " off " {
queryParams . append ( " target_language= \( target ) " )
}
let urlStr = " \( endpoint ) ? " + queryParams . joined ( separator : " & " )
let queryStr = SonioxLiveSession . baseQuery ( withHints : languageHints , targetLanguage : targetLanguage )
let urlStr = " \( endpoint ) ? \( queryStr ) "
guard let url = URL ( string : urlStr ) else {
completion ( . failure ( NSError ( domain : " Soniox " , code : - 1 , userInfo : [ NSLocalizedDescriptionKey : " Invalid URL " ] ) ) )
return
@ -637,7 +1156,7 @@ public final class SonioxLiveSession {
request . setValue ( origin ( for : endpoint ) , forHTTPHeaderField : " Origin " )
let durationSec = Double ( pcmData . count ) / 32000.0
let watchdogTimeout = max ( 6.5 , min ( 2 5.0, durationSec * 0.60 + 5 .0 ) )
let watchdogTimeout = max ( 10.0 , min ( 6 5.0, durationSec * 0.90 + 10 .0 ) )
request . timeoutInterval = 5.0 // F a s t h a n d s h a k e t i m e o u t s o d e a d e n d p o i n t f a i l s o v e r i n < 5 s
let config = URLSessionConfiguration . ephemeral
@ -796,7 +1315,7 @@ public final class SonioxLiveSession {
if done || isCancelled ( ) { return }
if offset >= pcmData . count {
let silence = Data ( count : 64 00)
let silence = Data ( count : 192 00)
task . send ( . data ( silence ) ) { _ in
task . send ( . string ( " { \" type \" : \" finalize \" } " ) ) { _ in
task . send ( . string ( " END " ) ) { _ in }
@ -863,19 +1382,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 )
// S t r i p O N L Y k n o w n a c o u s t i c n o i s e h a l l u c i n a t i o n s ; p r e s e r v e l e g i t i m a t e E n g l i s h t e c h n i c a l t e r m s ( S o n i o x , V o i c e , S y n c , M a c , P u s h , T a l k , A P I , e t c . )
if stopWords . contains ( cleanW ) { continue }
if faRatio >= 0.70 && ! preserveWords . contains ( cleanW ) { continue }
}
cleaned . append ( w )
}
@ -994,12 +1507,25 @@ public final class SonioxSessionPool {
}
let age = Date ( ) . timeIntervalSince ( createdAt )
if let session = session , session . isReady && age < 55 .0 {
if let session = session , session . isReady && age < 60 .0 {
session . stopKeepAliveHeartbeat ( )
print ( " SonioxSessionPool: Acquired HOT prewarmed session (age: \( String ( format : " %.1f " , age ) ) s, 0ms connect delay!) " )
return session
}
// I f p r e w a r m e d s e s s i o n i s c o n n e c t i n g o r s l i g h t l y a g e d , w a i t u p t o 0 . 4 s f o r i t b e f o r e a b o r t i n g
if let session = session , ! session . isReady && age < 15.0 {
let tStart = Date ( )
while Date ( ) . timeIntervalSince ( tStart ) < 0.40 {
if session . isReady {
session . stopKeepAliveHeartbeat ( )
print ( " SonioxSessionPool: Acquired session after \( Int ( Date ( ) . timeIntervalSince ( tStart ) * 1000 ) ) ms connection wait. " )
return session
}
usleep ( 25000 ) // 2 5 m s
}
}
session ? . stopKeepAliveHeartbeat ( )
session ? . abort ( )