Commit 310465d
feat!: allow passing pre-computed phonemes to Kokoro TTS
Right now if you want to use Kokoro TTS, you have to go through the
built-in phonemis G2P pipeline. There's no way around it. This PR adds
`generateFromPhonemes` / `streamFromPhonemes` methods that let you skip
phonemis and pass your own IPA phoneme strings directly to the synthesis
engine.
Why would you want this? A few reasons we've run into:
- phonemis doesn't handle every word well. Libraries like
[phonemizer](https://github.com/bootphon/phonemizer) (espeak-ng backend)
do better on edge cases, foreign words, etc.
- Custom lexicons. If you have domain-specific pronunciation (game
character names, medical terms), you probably want control over the G2P
step.
- Server-side G2P. Pre-compute phonemes on a server with a proper NLP
pipeline, send them to the device.
- Languages phonemis doesn't cover yet.
## What changed
The existing `generate()` / `stream()` methods now delegate to shared
internal helpers (`generateFromPhonemesImpl` /
`streamFromPhonemesImpl`). The new public methods call the same helpers
but skip the `phonemizer_.process()` step. No behavior change for
existing callers.
Changes across layers:
- C++ `Kokoro`: `generateFromPhonemes`, `streamFromPhonemes` + input
validation (empty string, invalid UTF-8)
- JSI `ModelHostObject`: exposes new methods
- `TextToSpeechModule`: `forwardFromPhonemes()`, `streamFromPhonemes()`
(shared `streamImpl` helper, no copy-paste)
- `useTextToSpeech` hook: same, with shared guard + streaming
orchestration
- Types: `TextToSpeechPhonemeInput`,
`TextToSpeechStreamingPhonemeInput`, `TextToSpeechStreamingCallbacks`
## Usage
```typescript
const tts = new TextToSpeechModule();
await tts.load(config);
// text path (unchanged -- goes through phonemis)
const audio = await tts.forward("Hello world");
// phoneme path (bypasses phonemis)
const audio = await tts.forwardFromPhonemes("həloʊ wɝːld");
// streaming
for await (const chunk of tts.streamFromPhonemes({ phonemes: "həloʊ wɝːld", speed: 1.0 })) {
playAudio(chunk);
}
```
## Test plan
- [ ] Existing `generate()` and `stream()` still work (refactor is
internal)
- [ ] `generateFromPhonemes()` with known Kokoro IPA strings
- [ ] `streamFromPhonemes()` produces same audio as `stream()` for
identical phonemes
- [ ] Multi-byte UTF-8 phoneme characters (ʊ, ɪ, ŋ, etc.)
- [ ] Empty string and invalid UTF-8 rejected with proper error
---------
Co-authored-by: IgorSwat <igorswat2002@o2.pl>1 parent f43d3c5 commit 310465d
File tree
9 files changed
+377
-98
lines changed- docs/docs
- 03-hooks/01-natural-language-processing
- 04-typescript-api/01-natural-language-processing
- packages/react-native-executorch
- common/rnexecutorch
- host_objects
- models/text_to_speech/kokoro
- src
- hooks/natural_language_processing
- modules/natural_language_processing
- types
9 files changed
+377
-98
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
127 | 127 | | |
128 | 128 | | |
129 | 129 | | |
| 130 | + | |
Lines changed: 55 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
82 | 82 | | |
83 | 83 | | |
84 | 84 | | |
85 | | - | |
| 85 | + | |
86 | 86 | | |
87 | | - | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
88 | 98 | | |
89 | 99 | | |
90 | | - | |
| 100 | + | |
91 | 101 | | |
92 | 102 | | |
93 | | - | |
94 | | - | |
95 | | - | |
96 | 103 | | |
97 | 104 | | |
98 | 105 | | |
| |||
185 | 192 | | |
186 | 193 | | |
187 | 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 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
188 | 237 | | |
189 | 238 | | |
190 | 239 | | |
| |||
Lines changed: 43 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
53 | 53 | | |
54 | 54 | | |
55 | 55 | | |
56 | | - | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
57 | 59 | | |
58 | 60 | | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
59 | 69 | | |
60 | 70 | | |
61 | | - | |
| 71 | + | |
62 | 72 | | |
63 | 73 | | |
64 | | - | |
65 | | - | |
66 | 74 | | |
67 | 75 | | |
68 | 76 | | |
| |||
135 | 143 | | |
136 | 144 | | |
137 | 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 | + | |
Lines changed: 8 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
171 | 171 | | |
172 | 172 | | |
173 | 173 | | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
174 | 182 | | |
175 | 183 | | |
176 | 184 | | |
| |||
Lines changed: 61 additions & 40 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
| 7 | + | |
7 | 8 | | |
8 | 9 | | |
9 | 10 | | |
| |||
73 | 74 | | |
74 | 75 | | |
75 | 76 | | |
76 | | - | |
77 | | - | |
78 | | - | |
79 | | - | |
80 | | - | |
81 | | - | |
82 | | - | |
83 | | - | |
84 | | - | |
85 | | - | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
86 | 80 | | |
87 | 81 | | |
88 | 82 | | |
| |||
98 | 92 | | |
99 | 93 | | |
100 | 94 | | |
101 | | - | |
102 | 95 | | |
103 | | - | |
| 96 | + | |
104 | 97 | | |
105 | 98 | | |
106 | | - | |
107 | | - | |
| 99 | + | |
| 100 | + | |
108 | 101 | | |
109 | 102 | | |
110 | 103 | | |
111 | 104 | | |
112 | 105 | | |
113 | | - | |
114 | | - | |
115 | | - | |
116 | | - | |
117 | | - | |
118 | | - | |
119 | | - | |
120 | | - | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
121 | 109 | | |
122 | 110 | | |
123 | 111 | | |
| |||
127 | 115 | | |
128 | 116 | | |
129 | 117 | | |
130 | | - | |
131 | 118 | | |
132 | 119 | | |
133 | | - | |
134 | | - | |
135 | | - | |
136 | | - | |
137 | | - | |
138 | | - | |
| 120 | + | |
139 | 121 | | |
140 | 122 | | |
141 | 123 | | |
142 | | - | |
143 | | - | |
144 | | - | |
145 | 124 | | |
146 | 125 | | |
147 | 126 | | |
| |||
151 | 130 | | |
152 | 131 | | |
153 | 132 | | |
154 | | - | |
| 133 | + | |
155 | 134 | | |
156 | 135 | | |
157 | 136 | | |
| |||
161 | 140 | | |
162 | 141 | | |
163 | 142 | | |
164 | | - | |
| 143 | + | |
165 | 144 | | |
166 | 145 | | |
167 | 146 | | |
168 | 147 | | |
169 | | - | |
170 | | - | |
171 | | - | |
172 | | - | |
173 | | - | |
| 148 | + | |
| 149 | + | |
174 | 150 | | |
175 | 151 | | |
176 | 152 | | |
177 | 153 | | |
178 | 154 | | |
179 | | - | |
180 | 155 | | |
181 | 156 | | |
182 | 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 | + | |
183 | 204 | | |
184 | 205 | | |
185 | 206 | | |
| |||
Lines changed: 17 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
27 | 27 | | |
28 | 28 | | |
29 | 29 | | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
30 | 37 | | |
31 | 38 | | |
32 | 39 | | |
33 | 40 | | |
34 | 41 | | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
35 | 46 | | |
36 | 47 | | |
37 | 48 | | |
| |||
42 | 53 | | |
43 | 54 | | |
44 | 55 | | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
45 | 62 | | |
46 | 63 | | |
47 | 64 | | |
| |||
0 commit comments