@ -35,10 +35,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
@ -61,10 +67,46 @@ 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 {
lock . lock ( )
defer { lock . unlock ( ) }
@ -256,22 +298,197 @@ public final class SonioxLiveSession {
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
// / 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 > 48 000 && 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 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 : " & " )
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 )
}
}
@ -281,15 +498,40 @@ public final class SonioxLiveSession {
lock . unlock ( )
return
}
if ! isConnected && initialAudioBuffer . count < 80 {
initialAudioBuffer . append ( data )
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
@ -297,15 +539,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 {
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
@ -320,13 +566,21 @@ 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
@ -403,14 +657,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 ) " )
}
}
}
@ -465,21 +720,40 @@ 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
}
}
}
@ -492,27 +766,16 @@ public final class SonioxLiveSession {
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 !
// S o n i o x b a t c h e s ` i s _ f i n a l : t r u e ` t o k e n s o n l y e v e r y 6 - 8 s e c o n d s , w h i c h w o u l d m a k e l i v e t y p i n g f e e l l a g g i n g a n d o u t o f s y n c .
// H o w e v e r , 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 ) a r e 1 0 0 % s t a b l e a n d a p p e a r w i t h i n ~ 1 5 0 m s !
// W e s t r e a m ` f u l l C o m m i t t e d ` P L U S a n y c o m p l e t e d w o r d s i n ` f u l l N o n F i n a l ` e x c e p t t h e t r a i l i n g w o r d c u r r e n t l y b e i n g u t t e r e d .
// 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 && ! 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 {
// A t l e a s t o n e s u b s e q u e n t w o r d h a s s t a r t e d , s o ` c o m p l e t e d N o n F i n a l ` h a s f u l l a c o u s t i c l o o k a h e a d
stableCandidate = fullCommitted + completedNonFinal
} else {
// T r a i l i n g s p a c e a t t h e v e r y e n d o f ` f u l l N o n F i n a l ` : h o l d b a c k t h e l a s t w o r d u n t i l t h e n e x t w o r d b e g i n s
let trimmedNF = completedNonFinal . trimmingCharacters ( in : . whitespacesAndNewlines )
if let prevSpaceRange = trimmedNF . rangeOfCharacter ( from : . whitespacesAndNewlines , options : . backwards ) {
stableCandidate = fullCommitted + String ( trimmedNF [ . . < prevSpaceRange . upperBound ] )
}
}
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 {
@ -521,9 +784,10 @@ public final class SonioxLiveSession {
streamedPrefix = stableCandidate
deltaToEmit = delta
} else {
let common = stableCandidate . commonPrefix ( with : streamedPrefix )
if stableCandidate . count > common . count {
let delta = String ( stableCandidate . dropFirst ( common . count ) )
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
}
@ -549,7 +813,12 @@ public final class SonioxLiveSession {
}
if isTerminatingMessage {
completeWithCurrentText ( )
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
@ -560,7 +829,14 @@ public final class SonioxLiveSession {
return raw . contains ( " \" finished \" :true " ) || raw . contains ( " \" finished \" : true " )
} ( )
if sessionEnded || sessionDone || isFinished || hasRawFinished {
completeWithCurrentText ( )
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 ( )
}
}
}
}
@ -569,7 +845,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 )
}
@ -580,10 +864,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
@ -597,7 +894,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 ) )
@ -613,7 +912,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
@ -656,6 +958,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
@ -761,7 +1185,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