|
| 1 | +import Foundation |
| 2 | +import AVFoundation |
| 3 | + |
| 4 | +// Speaks an assistant reply out loud, one sentence-sized chunk at a time. |
| 5 | +// |
| 6 | +// Chunking matters for two reasons: synthesis latency scales with input length (a whole |
| 7 | +// reply would take many seconds before the first sound), and a reply that is still |
| 8 | +// streaming has no "whole" to send yet. So text is cut into chunks, each chunk is |
| 9 | +// synthesized to an MP3 while the previous one plays, and `append` lets the caller keep |
| 10 | +// feeding text as tokens arrive. |
| 11 | +// |
| 12 | +// Everything here is main-thread only: CurlFetcher delivers its completion on the main |
| 13 | +// queue and AVAudioPlayer's delegate callbacks also land there, so no locking is needed. |
| 14 | +final class TTSPlayer: NSObject, AVAudioPlayerDelegate { |
| 15 | + static let shared = TTSPlayer() |
| 16 | + |
| 17 | + // Don't synthesize a chunk shorter than this unless the text is finished — a 3-word |
| 18 | + // request costs a full round trip and sounds clipped. |
| 19 | + private static let minChunk = 80 |
| 20 | + // Hard ceiling so a wall of text with no punctuation still gets spoken promptly. |
| 21 | + private static let maxChunk = 500 |
| 22 | + // How many synthesized chunks to keep queued ahead of the one playing. |
| 23 | + private static let readyAhead = 2 |
| 24 | + |
| 25 | + // Called whenever the speaking message changes, so the UI can retitle its buttons. |
| 26 | + var onStateChange: (() -> Void)? |
| 27 | + var onError: ((String) -> Void)? |
| 28 | + |
| 29 | + // Identifies the message being spoken ("<conversationID>:<row>"); nil = idle. |
| 30 | + private(set) var speakingKey: String? |
| 31 | + |
| 32 | + private var full: [Character] = [] |
| 33 | + private var cursor = 0 // how much of `full` has been cut into chunks |
| 34 | + private var pending: [String] = [] // cut, not yet synthesized |
| 35 | + private var ready: [String] = [] // synthesized file paths, not yet played |
| 36 | + private var isSynthesizing = false |
| 37 | + private var streamComplete = false |
| 38 | + private var player: AVAudioPlayer? |
| 39 | + |
| 40 | + // Bumped by stop(); in-flight synthesis callbacks compare against it and bail out. |
| 41 | + private var generation = 0 |
| 42 | + |
| 43 | + private static let cacheDir: String = { |
| 44 | + let docs = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] |
| 45 | + let dir = (docs as NSString).appendingPathComponent("tts") |
| 46 | + try? FileManager.default.createDirectory(atPath: dir, withIntermediateDirectories: true, attributes: nil) |
| 47 | + return dir |
| 48 | + }() |
| 49 | + |
| 50 | + private override init() { super.init() } |
| 51 | + |
| 52 | + // MARK: - Public API |
| 53 | + |
| 54 | + func isSpeaking(key: String) -> Bool { |
| 55 | + return speakingKey == key |
| 56 | + } |
| 57 | + |
| 58 | + // Starts speaking `text`. If it belongs to a reply that is still streaming, pass |
| 59 | + // isComplete: false and keep calling append() as more text arrives. |
| 60 | + func speak(key: String, text: String, isComplete: Bool) { |
| 61 | + stop() |
| 62 | + speakingKey = key |
| 63 | + full = Array(text) |
| 64 | + streamComplete = isComplete |
| 65 | + activateSession() |
| 66 | + drain() |
| 67 | + pump() |
| 68 | + onStateChange?() |
| 69 | + } |
| 70 | + |
| 71 | + // Feeds the latest full text of the message being spoken. Ignored for other messages. |
| 72 | + func append(key: String, fullText: String) { |
| 73 | + guard speakingKey == key, !streamComplete else { return } |
| 74 | + full = Array(fullText) |
| 75 | + drain() |
| 76 | + pump() |
| 77 | + } |
| 78 | + |
| 79 | + // Marks the streaming reply as finished; flushes whatever tail is left. |
| 80 | + func finish(key: String) { |
| 81 | + guard speakingKey == key else { return } |
| 82 | + streamComplete = true |
| 83 | + drain() |
| 84 | + pump() |
| 85 | + } |
| 86 | + |
| 87 | + func stop() { |
| 88 | + generation += 1 |
| 89 | + player?.stop() |
| 90 | + player = nil |
| 91 | + speakingKey = nil |
| 92 | + full = [] |
| 93 | + cursor = 0 |
| 94 | + pending = [] |
| 95 | + ready = [] |
| 96 | + isSynthesizing = false |
| 97 | + streamComplete = false |
| 98 | + onStateChange?() |
| 99 | + } |
| 100 | + |
| 101 | + // MARK: - Chunking |
| 102 | + |
| 103 | + // Cuts as much of the un-chunked tail of `full` into speakable chunks as it can. |
| 104 | + private func drain() { |
| 105 | + while cursor < full.count { |
| 106 | + let remaining = full.count - cursor |
| 107 | + var end = -1 |
| 108 | + |
| 109 | + // Prefer a sentence boundary at least minChunk in. |
| 110 | + var i = cursor + TTSPlayer.minChunk |
| 111 | + while i < full.count && i < cursor + TTSPlayer.maxChunk { |
| 112 | + let c = full[i] |
| 113 | + if c == "." || c == "!" || c == "?" || c == "\n" { |
| 114 | + end = i |
| 115 | + break |
| 116 | + } |
| 117 | + i += 1 |
| 118 | + } |
| 119 | + |
| 120 | + if end < 0 && remaining >= TTSPlayer.maxChunk { |
| 121 | + // No punctuation in range — break on the last space instead of mid-word. |
| 122 | + var j = cursor + TTSPlayer.maxChunk - 1 |
| 123 | + while j > cursor + TTSPlayer.minChunk && full[j] != " " { j -= 1 } |
| 124 | + end = j |
| 125 | + } |
| 126 | + |
| 127 | + if end < 0 { |
| 128 | + // Not enough text yet. Once the stream is done, speak the tail as-is. |
| 129 | + if streamComplete && remaining > 0 { end = full.count - 1 } else { return } |
| 130 | + } |
| 131 | + |
| 132 | + let chunk = String(full[cursor...end]).trimmingCharacters(in: .whitespacesAndNewlines) |
| 133 | + cursor = end + 1 |
| 134 | + if !chunk.isEmpty { pending.append(chunk) } |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + // MARK: - Pipeline |
| 139 | + |
| 140 | + private func pump() { |
| 141 | + pumpSynthesis() |
| 142 | + pumpPlayback() |
| 143 | + checkDone() |
| 144 | + } |
| 145 | + |
| 146 | + private func pumpSynthesis() { |
| 147 | + guard !isSynthesizing, !pending.isEmpty, ready.count < TTSPlayer.readyAhead else { return } |
| 148 | + |
| 149 | + let model = Settings.ttsModel |
| 150 | + let voice = Settings.ttsVoice |
| 151 | + let chunk = pending.removeFirst() |
| 152 | + let path = cachePath(for: chunk, model: model, voice: voice) |
| 153 | + |
| 154 | + if FileManager.default.fileExists(atPath: path) { |
| 155 | + ready.append(path) |
| 156 | + pumpSynthesis() |
| 157 | + pumpPlayback() |
| 158 | + return |
| 159 | + } |
| 160 | + |
| 161 | + isSynthesizing = true |
| 162 | + let gen = generation |
| 163 | + TTSAPI.synthesize(text: chunk, model: model, voice: voice, apiKey: Settings.apiKey) { [weak self] data, error in |
| 164 | + guard let self = self, gen == self.generation else { return } |
| 165 | + self.isSynthesizing = false |
| 166 | + if let data = data, (data as NSData).write(toFile: path, atomically: true) { |
| 167 | + self.ready.append(path) |
| 168 | + } else { |
| 169 | + // One failed chunk shouldn't kill the rest of the reply — report and skip. |
| 170 | + self.onError?(error ?? "Speech synthesis failed.") |
| 171 | + } |
| 172 | + self.pump() |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + private func pumpPlayback() { |
| 177 | + guard player == nil, !ready.isEmpty else { return } |
| 178 | + let path = ready.removeFirst() |
| 179 | + do { |
| 180 | + let p = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: path)) |
| 181 | + p.delegate = self |
| 182 | + player = p |
| 183 | + p.prepareToPlay() |
| 184 | + if !p.play() { |
| 185 | + player = nil |
| 186 | + onError?("Could not play synthesized audio.") |
| 187 | + } |
| 188 | + } catch { |
| 189 | + player = nil |
| 190 | + onError?("Could not open synthesized audio.") |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + // Speaking is over only when the stream ended AND the whole pipeline has drained. |
| 195 | + private func checkDone() { |
| 196 | + guard speakingKey != nil, streamComplete, player == nil, |
| 197 | + ready.isEmpty, pending.isEmpty, !isSynthesizing, cursor >= full.count else { return } |
| 198 | + stop() |
| 199 | + } |
| 200 | + |
| 201 | + func audioPlayerDidFinishPlaying(_ p: AVAudioPlayer, successfully flag: Bool) { |
| 202 | + guard p === player else { return } |
| 203 | + player = nil |
| 204 | + pump() |
| 205 | + } |
| 206 | + |
| 207 | + func audioPlayerDecodeErrorDidOccur(_ p: AVAudioPlayer, error: Error?) { |
| 208 | + guard p === player else { return } |
| 209 | + player = nil |
| 210 | + pump() |
| 211 | + } |
| 212 | + |
| 213 | + // MARK: - Cache |
| 214 | + |
| 215 | + private func activateSession() { |
| 216 | + let session = AVAudioSession.sharedInstance() |
| 217 | + try? session.setCategory(.playback) |
| 218 | + try? session.setActive(true) |
| 219 | + } |
| 220 | + |
| 221 | + // Content-addressed so identical text is never synthesized (or billed) twice. |
| 222 | + private func cachePath(for text: String, model: String, voice: String) -> String { |
| 223 | + let key = "\(model)|\(voice)|\(text)" |
| 224 | + let name = String(format: "%08x-%d.mp3", TTSPlayer.hash(key), key.utf8.count) |
| 225 | + return (TTSPlayer.cacheDir as NSString).appendingPathComponent(name) |
| 226 | + } |
| 227 | + |
| 228 | + // djb2 — CommonCrypto isn't bridged here and a collision only costs a wrong cache hit, |
| 229 | + // which the length suffix already makes vanishingly unlikely. |
| 230 | + private static func hash(_ s: String) -> UInt32 { |
| 231 | + var h: UInt32 = 5381 |
| 232 | + for b in s.utf8 { h = (h &* 33) &+ UInt32(b) } |
| 233 | + return h |
| 234 | + } |
| 235 | +} |
0 commit comments