Repository navigation
Expand file tree
/
Copy pathTextToSpeechModule.ts
More file actions
228 lines (205 loc) · 6.82 KB
/
Copy pathTextToSpeechModule.ts
File metadata and controls
228 lines (205 loc) · 6.82 KB
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
220
221
222
223
224
225
226
227
228
import { RnExecutorchErrorCode } from '../../errors/ErrorCodes';
import { parseUnknownError, RnExecutorchError } from '../../errors/errorUtils';
import { ResourceFetcher } from '../../utils/ResourceFetcher';
import { ResourceSource } from '../../types/common';
import {
TextToSpeechModelConfig,
TextToSpeechModelSources,
TextToSpeechStreamingInput,
} from '../../types/tts';
import { Logger } from '../../common/Logger';
/**
* Module for Text to Speech (TTS) functionalities.
* @category Typescript API
*/
export class TextToSpeechModule {
private nativeModule: any;
private isStreaming: boolean = false;
private constructor(nativeModule: unknown) {
this.nativeModule = nativeModule;
}
/**
* Creates a Text to Speech instance.
* @param config - Configuration object containing model and voice sources.
* @param onDownloadProgress - Optional callback to monitor download progress, receiving a value between 0 and 1.
* @returns A Promise resolving to a `TextToSpeechModule` instance.
*/
static async fromModelName(
config: TextToSpeechModelConfig,
onDownloadProgress: (progress: number) => void = () => {}
): Promise<TextToSpeechModule> {
try {
const nativeModule = await TextToSpeechModule.loadKokoro(
config,
onDownloadProgress
);
return new TextToSpeechModule(nativeModule);
} catch (error) {
Logger.error('Load failed:', error);
throw parseUnknownError(error);
}
}
private static async loadKokoro(
config: TextToSpeechModelConfig,
onDownloadProgressCallback: (progress: number) => void
): Promise<unknown> {
const { model, voiceSource, phonemizerConfig } = config;
const kokoroModel = model as Extract<
TextToSpeechModelSources,
{ modelName: 'kokoro' }
>;
const sources: ResourceSource[] = [
kokoroModel.durationPredictorSource,
kokoroModel.synthesizerSource,
voiceSource,
];
// Since each of these args is optional, we need to handle the sources array in a dynamic way.
const taggerIdx = phonemizerConfig.taggerSource
? sources.push(phonemizerConfig.taggerSource) - 1
: -1;
const lexiconIdx = phonemizerConfig.lexiconSource
? sources.push(phonemizerConfig.lexiconSource) - 1
: -1;
const neuralModelIdx = phonemizerConfig.neuralModelSource
? sources.push(phonemizerConfig.neuralModelSource) - 1
: -1;
const paths = await ResourceFetcher.fetch(
onDownloadProgressCallback,
...sources
);
// Required fields
const [duration, synth, voice] = paths;
if (!duration || !synth || !voice) {
throw new RnExecutorchError(
RnExecutorchErrorCode.DownloadInterrupted,
'Kokoro: missing required model paths.'
);
}
// Optional fields
const tagger = paths[taggerIdx] ?? '';
const lexicon = paths[lexiconIdx] ?? '';
const neural = paths[neuralModelIdx] ?? '';
return await global.loadTextToSpeechKokoro(
phonemizerConfig.lang,
tagger,
lexicon,
neural,
duration,
synth,
voice
);
}
private ensureLoaded(methodName: string): void {
if (this.nativeModule == null)
throw new RnExecutorchError(
RnExecutorchErrorCode.ModuleNotLoaded,
`The model is currently not loaded. Please load the model before calling ${methodName}().`
);
}
/**
* Synthesizes the provided input (text or IPA phonemes) into speech.
* @param input - The input text or phonemes to be synthesized.
* @param speed - Playback speed multiplier (default: 1.0).
* @param phonemize - If true (default), treats input as text and converts it to phonemes.
* If false, input is treated as phonemes.
* @returns A promise resolving to the full audio waveform as a `Float32Array`.
*/
public async forward(
input: string,
speed: number = 1.0,
phonemize: boolean = true
): Promise<Float32Array> {
this.ensureLoaded('forward');
return await this.nativeModule.generate(input, speed, phonemize);
}
/**
* Starts a streaming synthesis session. Yields audio chunks as they are generated.
* @param input - Input object containing optional speed, phonemize flag and stopAutomatically flag.
* @yields An audio chunk generated during synthesis.
* @returns An async generator yielding Float32Array audio chunks.
*/
public async *stream({
speed = 1.0,
phonemize = true,
stopAutomatically = true,
}: TextToSpeechStreamingInput): AsyncGenerator<Float32Array> {
const queue: Float32Array[] = [];
let waiter: (() => void) | null = null;
let error: RnExecutorchError | undefined;
let nativeStreamFinished = false;
this.isStreaming = true;
const wake = () => {
waiter?.();
waiter = null;
};
(async () => {
try {
await this.nativeModule.stream(
(audio: number[]) => {
queue.push(new Float32Array(audio));
wake();
},
speed,
phonemize,
stopAutomatically
);
nativeStreamFinished = true;
wake();
} catch (e) {
error = parseUnknownError(e);
nativeStreamFinished = true;
wake();
}
})();
while (this.isStreaming) {
if (queue.length > 0) {
yield queue.shift()!;
if (nativeStreamFinished && queue.length === 0) {
return;
}
continue;
}
if (error) throw error;
if (nativeStreamFinished && queue.length === 0) return;
await new Promise<void>((r) => (waiter = r));
}
}
/**
* Inserts new content (text or IPA phonemes) into the buffer to be processed
* in streaming mode. Trailing un-terminated content sits in the buffer
* until {@link TextToSpeechModule.streamFlush} or `streamStop(false)`
* releases it.
* @param input - The text or phoneme fragment to append to the streaming buffer.
*/
public streamInsert(input: string): void {
this.nativeModule.streamInsert(input);
}
/**
* Force-partitions whatever is currently buffered, even without an
* end-of-sentence character. Call after the final `streamInsert` of an
* utterance to play out the trailing tail without ending the stream.
*/
public streamFlush(): void {
this.nativeModule.streamFlush();
}
/**
* Stops the streaming process if there is any ongoing.
* @param instant - If true, stops the streaming as soon as possible. Otherwise
* drains the current buffer (force-flushing any trailing
* un-terminated content) before stopping.
*/
public streamStop(instant: boolean = true): void {
this.nativeModule.streamStop(instant);
if (instant) {
this.isStreaming = false;
}
}
/**
* Unloads the model from memory.
*/
delete() {
if (this.nativeModule !== null) {
this.nativeModule.unload();
}
}
}