1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
//! Text-preprocessing hook for the TTS synthesis pipeline.
//!
//! Ports mlx-audio-swift's [`TextProcessor`][swift-tp] protocol — the
//! *interface* a per-model phonemizer / normalizer plugs into.
//!
//! ## What lives here
//!
//! - [`TextProcessor`] — the trait, a 1:1 of the swift protocol (a
//! [`prepare`][TextProcessor::prepare] hook + a
//! [`process`][TextProcessor::process] method that takes natural-language
//! text and an optional language hint, returns the per-model expected
//! string).
//! - [`BasicTextProcessor`] — a no-G2P default impl that runs the three
//! normalization passes most TTS frontends share — Unicode NFC
//! ([`unicode-normalization`][un] is the only added dep) → ASCII
//! lowercase → whitespace collapse — and returns the normalized text.
//! Suitable for any TTS model that wants raw normalized text (not
//! phonemized IPA). Per-model phonemizers (Misaki G2P, eSpeak adapter, …)
//! implement [`TextProcessor`] directly without going through
//! [`BasicTextProcessor`].
//!
//! [swift-tp]: https://github.com/Blaizzy/mlx-audio-swift/blob/main/Sources/MLXAudioTTS/TextProcessor.swift
//! [un]: https://docs.rs/unicode-normalization
use UnicodeNormalization;
use crateResult;
/// Text-preprocessing hook for the TTS synthesis pipeline.
///
/// A 1:1 port of mlx-audio-swift's [`TextProcessor`][swift-tp] protocol —
/// the *interface*, not a concrete phonemizer. Some TTS models (kokoro,
/// kitten-tts) require phonemized IPA input rather than raw text; a
/// [`TextProcessor`] converts natural-language text into the format the
/// target model expects.
///
/// Phonemization / G2P itself is **model-specific** and out of scope per
/// the project's no per-model arch porting rule — mlxrs ships the
/// hook, not a Misaki/eSpeak G2P implementation. A per-model crate
/// implements [`TextProcessor`] (e.g. a Misaki G2P adapter) and the model's
/// [`crate::audio::tts::model::TtsModel::synthesize_segment`] runs it; the
/// [`crate::audio::tts::generate::tts_generate`] driver itself never
/// phonemizes — it passes segment text through unchanged.
///
/// Why a separate hook trait rather than a method on
/// [`TtsModel`](crate::audio::tts::model::TtsModel): mlx-audio-swift keeps
/// `TextProcessor` distinct from `SpeechGenerationModel` precisely so one
/// G2P adapter can be shared across several models, and so a caller can
/// *inject* a custom processor at load time (the swift `loadModel(...,
/// textProcessor:)` parameter). mlxrs mirrors that separation — per the
/// mirror-reference-structure rule, the reference's two distinct
/// protocols stay two distinct traits.
///
/// [swift-tp]: https://github.com/Blaizzy/mlx-audio-swift/blob/main/Sources/MLXAudioTTS/TextProcessor.swift
/// A no-G2P default [`TextProcessor`] that applies the three normalization
/// passes most TTS text frontends share — Unicode NFC composition,
/// lowercase folding, and whitespace collapse — and returns the
/// normalized text.
///
/// Suitable for any TTS model that wants raw normalized text (not
/// phonemized IPA); a per-model phonemizer (Misaki G2P, eSpeak adapter)
/// implements [`TextProcessor`] directly without composing
/// [`BasicTextProcessor`] (it does no phonemization).
///
/// The three passes (mirroring the Swift TTS-frontend convention):
///
/// 1. **Unicode NFC** — composes combining marks into precomposed forms so
/// `"café"` (with a combining acute accent) and `"café"` (with a
/// precomposed é) collide on the same string.
/// 2. **Lowercase** — uses Rust's [`str::to_lowercase`] (Unicode-aware,
/// matching Swift's `String.lowercased()`).
/// 3. **Whitespace collapse** — runs of Unicode whitespace (any
/// [`char::is_whitespace`]) collapse to a single ASCII space, and
/// leading / trailing whitespace is trimmed.
///
/// `language` is ignored — this processor does not branch on locale.
;
/// Collapse runs of Unicode whitespace to a single ASCII space and trim
/// leading / trailing whitespace. `" hello \t world "` → `"hello world"`.