Generation and streaming
Initialize
const tts = await KittenTTS.create(undefined, (progress, info) => {
if (info?.stage === 'cached') {
console.log('Assets are already cached');
} else {
console.log(`Setup ${Math.round(progress * 100)}%`);
}
});
The setup phase resolves configuration, prepares the phonemizer, downloads or loads model assets, reads voices.npz, and creates the ONNX session.
Generate
const result = await tts.generate(
'Small models can still have a big voice.',
KittenVoice.Luna,
1.05,
);
console.log(result.samples); // Float32Array, mono PCM
console.log(result.sampleRate); // 24000
console.log(result.duration);
console.log(result.wordTimings);
console.log(result.wavData()); // Uint8Array
console.log(result.wavBase64());
Stream long text
for await (const chunk of tts.generateStreaming(
articleText,
KittenVoice.Jasper,
1.0,
)) {
await tts.play(chunk);
}
Streaming splits long input into sentence groups and yields a complete KittenTTSResult for each group. It reduces time to first audio but does not expose partial samples from a single inference call.
Lifecycle
Reuse an initialized KittenTTS instance across generations. Call dispose() when the owning screen, service, or worker is permanently finished with it so ONNX and phonemizer resources can be released.
try {
const result = await tts.generate(text);
consume(result);
} finally {
await tts.dispose();
}