@@ -32,13 +32,16 @@ final class AppState: ObservableObject {
3232 @ Published var ollamaURL = " http://localhost:11434 "
3333 @ Published var ollamaModels : [ String ] = [ ]
3434 @ Published var ollamaStatus = " "
35+ @ Published var normalizePrompt = TextNormalizer . defaultInstruction
36+ @ Published var normalizing = false
3537
3638 // Runtime
3739 enum Phase : Equatable { case idle , synthesizing , playing , paused }
3840 @ Published var phase : Phase = . idle
3941 @ Published var progress : Double = 0 // 0…1 across all chunks
4042 @ Published var currentText = " " // text being spoken (overlay label)
41- @ Published var inputText = " " // mirrored into the Generate tab
43+ @ Published var inputText = " " // original (top panel / source text)
44+ @ Published var scriptText = " " // TTS-friendly script actually sent to the engine (bottom panel)
4245 @ Published var chunkIndex = 0 // 1-based chunk being played
4346 @ Published var chunkCount = 0
4447 @ Published var statusText = " "
@@ -115,15 +118,9 @@ final class AppState: ObservableObject {
115118 player . start ( expected : chunks . count )
116119 chunkCount = chunks . count ; chunkIndex = 0
117120
118- let ident = backendIdentity , vid = voiceId , mid = modelId
119- // Cache namespace includes normalization so normalized/raw audio don't collide.
120- let sHash = settingsHash + ( normalizeEnabled ? " |norm: \( ollamaModel ) " : " " )
121+ let ident = backendIdentity , vid = voiceId , mid = modelId , sHash = settingsHash
121122 let vName = voiceName , ext = audioExt
122123 let voice = VoiceConfig ( voiceId : vid , modelId : mid , settingsHash : sHash )
123- let normalizer : TextNormalizer ? = normalizeEnabled
124- ? TextNormalizer ( baseURL : URL ( string : ollamaURL ) ?? URL ( string : " http://localhost:11434 " ) ! ,
125- model : ollamaModel )
126- : nil
127124
128125 task = Task { [ weak self ] in
129126 guard let self else { return }
@@ -141,20 +138,14 @@ final class AppState: ObservableObject {
141138 // "synthesizing" only while waiting for the FIRST chunk
142139 if ! startedPlaying {
143140 self . phase = . synthesizing
144- self . statusText = normalizer != nil ? " 정규화· 합성 중…" : " 합성 중… "
141+ if self . statusText . isEmpty { self . statusText = " 합성 중… " }
145142 }
146143 if backend == nil { backend = self . makeBackend ( ) }
147144 guard let b = backend else {
148145 self . phase = . idle ; self . statusText = " 키/백엔드 미설정 " ; return
149146 }
150- // TTS-friendly normalization (falls back to original on failure).
151- var ttsText = chunk
152- if let normalizer {
153- ttsText = ( try? await normalizer . normalize ( chunk ) ) ?? chunk
154- try Task . checkCancellation ( )
155- }
156147 var data = Data ( )
157- for try await c in b . stream ( segment : ttsText , voice : voice ) {
148+ for try await c in b . stream ( segment : chunk , voice : voice ) {
158149 try Task . checkCancellation ( ) ; data . append ( c )
159150 }
160151 if self . useCache {
@@ -185,6 +176,51 @@ final class AppState: ObservableObject {
185176 }
186177 }
187178
179+ // MARK: - Script (TTS-friendly text)
180+
181+ /// Build the TTS script from inputText: normalize per paragraph via the LLM
182+ /// when enabled, otherwise pass the original through. Returns the script and
183+ /// fills `scriptText` (the bottom panel).
184+ @ discardableResult
185+ func prepareScript ( ) async -> String {
186+ let src = inputText . trimmingCharacters ( in : . whitespacesAndNewlines )
187+ guard ! src . isEmpty else { scriptText = " " ; return " " }
188+ guard normalizeEnabled else { scriptText = src ; return src }
189+
190+ normalizing = true
191+ let norm = TextNormalizer (
192+ baseURL : URL ( string : ollamaURL ) ?? URL ( string : " http://localhost:11434 " ) ! ,
193+ model : ollamaModel , instruction : normalizePrompt )
194+ var out : [ String ] = [ ]
195+ for p in TextSplitter . paragraphs ( src , maxChars : maxChunkChars ) {
196+ out . append ( ( try? await norm . normalize ( p ) ) ?? p )
197+ }
198+ let script = out . joined ( separator : " \n " )
199+ scriptText = script
200+ normalizing = false
201+ return script
202+ }
203+
204+ /// Generate-tab "재생": speak the script (or the original if it is empty).
205+ func speakScript ( ) {
206+ let s = scriptText . trimmingCharacters ( in : . whitespacesAndNewlines )
207+ synthesize ( s . isEmpty ? inputText : scriptText )
208+ }
209+
210+ /// Services entry: set the source, build the script, then speak it.
211+ func speakSelected ( _ text : String ) {
212+ task ? . cancel ( ) ; player . stop ( )
213+ inputText = text
214+ scriptText = " "
215+ phase = . synthesizing
216+ statusText = normalizeEnabled ? " 대본 생성 중… " : " 합성 중… "
217+ Task { [ weak self ] in
218+ guard let self else { return }
219+ let script = await self . prepareScript ( )
220+ self . synthesize ( script )
221+ }
222+ }
223+
188224 func replay ( _ e : HistoryEntry ) {
189225 task ? . cancel ( ) ; player . stop ( )
190226 guard let url = cache . fileURL ( forKey : e . id ) else { statusText = " 오디오 파일 없음 " ; return }
@@ -257,6 +293,7 @@ final class AppState: ObservableObject {
257293 " backend " : backendKind . rawValue , " useCache " : useCache , " localBaseURL " : localBaseURL ,
258294 " maxChunkChars " : maxChunkChars ,
259295 " normalize " : normalizeEnabled , " ollamaModel " : ollamaModel , " ollamaURL " : ollamaURL ,
296+ " normalizePrompt " : normalizePrompt ,
260297 " stability " : voiceSettings . stability , " similarity " : voiceSettings . similarityBoost ,
261298 " style " : voiceSettings . style , " speakerBoost " : voiceSettings . useSpeakerBoost ,
262299 ]
@@ -278,6 +315,7 @@ final class AppState: ObservableObject {
278315 normalizeEnabled = o [ " normalize " ] as? Bool ?? normalizeEnabled
279316 ollamaModel = o [ " ollamaModel " ] as? String ?? ollamaModel
280317 ollamaURL = o [ " ollamaURL " ] as? String ?? ollamaURL
318+ normalizePrompt = o [ " normalizePrompt " ] as? String ?? normalizePrompt
281319 voiceSettings . stability = o [ " stability " ] as? Double ?? voiceSettings . stability
282320 voiceSettings . similarityBoost = o [ " similarity " ] as? Double ?? voiceSettings . similarityBoost
283321 voiceSettings . style = o [ " style " ] as? Double ?? voiceSettings . style
0 commit comments