TTS: Kokoro (English)

Generate speech with the Kokoro English (v0.19) model. Kokoro is a high-quality TTS model with multiple speaker voices. It supports both synchronous and asynchronous generation.

For model documentation, see Kokoro English.

Source files

Synchronous generation

 1// Copyright (c)  2025  Xiaomi Corporation
 2//
 3// Text-to-speech with the Kokoro English model.
 4//
 5// Usage:
 6//   node tts_kokoro_en.js
 7//
 8const sherpa_onnx = require('sherpa-onnx-node');
 9
10function createOfflineTts() {
11  const config = {
12    model: {
13      kokoro: {
14        model: './kokoro-en-v0_19/model.onnx',
15        voices: './kokoro-en-v0_19/voices.bin',
16        tokens: './kokoro-en-v0_19/tokens.txt',
17        dataDir: './kokoro-en-v0_19/espeak-ng-data',
18      },
19      debug: true,
20      numThreads: 1,
21      provider: 'cpu',
22    },
23    maxNumSentences: 1,
24  };
25  return new sherpa_onnx.OfflineTts(config);
26}
27
28const tts = createOfflineTts();
29
30const text =
31    'Today as always, men fall into two groups: slaves and free men. Whoever does not have two-thirds of his day for himself, is a slave, whatever he may be: a statesman, a businessman, an official, or a scholar.';
32
33const generationConfig = new sherpa_onnx.GenerationConfig({
34  sid: 6,
35  speed: 1.0,
36  silenceScale: 0.2,
37});
38
39let start = Date.now();
40const audio = tts.generate({text, generationConfig});
41let stop = Date.now();
42const elapsed_seconds = (stop - start) / 1000;
43const duration = audio.samples.length / audio.sampleRate;
44const real_time_factor = elapsed_seconds / duration;
45console.log('Wave duration', duration.toFixed(3), 'seconds');
46console.log('Elapsed', elapsed_seconds.toFixed(3), 'seconds');
47console.log(
48    `RTF = ${elapsed_seconds.toFixed(3)}/${duration.toFixed(3)} =`,
49    real_time_factor.toFixed(3));
50
51const filename = 'test-kokoro-en.wav';
52sherpa_onnx.writeWave(
53    filename, {samples: audio.samples, sampleRate: audio.sampleRate});
54
55console.log(`Saved to ${filename}`);

Asynchronous generation

 1// Copyright (c)  2026  Xiaomi Corporation
 2//
 3// Asynchronous text-to-speech with the Kokoro English model.
 4//
 5// Usage:
 6//   node tts_kokoro_en_async.js
 7//
 8const sherpa_onnx = require('sherpa-onnx-node');
 9
10async function createOfflineTts() {
11  const config = {
12    model: {
13      kokoro: {
14        model: './kokoro-en-v0_19/model.onnx',
15        voices: './kokoro-en-v0_19/voices.bin',
16        tokens: './kokoro-en-v0_19/tokens.txt',
17        dataDir: './kokoro-en-v0_19/espeak-ng-data',
18      },
19      debug: false,
20      numThreads: 1,
21      provider: 'cpu',
22    },
23    maxNumSentences: 1,
24  };
25  return await sherpa_onnx.OfflineTts.createAsync(config);
26}
27
28async function main() {
29  const tts = await createOfflineTts();
30
31  const text =
32      'Today as always, men fall into two groups: slaves and free men. Whoever does not have two-thirds of his day for himself, is a slave, whatever he may be: a statesman, a businessman, an official, or a scholar.';
33
34  const generationConfig = new sherpa_onnx.GenerationConfig({
35    sid: 6,
36    speed: 1.0,
37    silenceScale: 0.2,
38  });
39
40  const start = Date.now();
41  const audio = await tts.generateAsync({
42    text,
43    enableExternalBuffer: true,
44    generationConfig,
45    onProgress: ({samples, progress}) => {
46      process.stdout.write(
47          `Progress: ${(progress * 100).toFixed(1)}%, ` +
48          `Samples: ${samples.length}\r`);
49      return 1;
50    },
51  });
52
53  console.log('');
54  const stop = Date.now();
55  const elapsed_seconds = (stop - start) / 1000;
56  const duration = audio.samples.length / audio.sampleRate;
57  const real_time_factor = elapsed_seconds / duration;
58  console.log('Wave duration', duration.toFixed(3), 'seconds');
59  console.log('Elapsed', elapsed_seconds.toFixed(3), 'seconds');
60  console.log(
61      `RTF = ${elapsed_seconds.toFixed(3)}/${duration.toFixed(3)} =`,
62      real_time_factor.toFixed(3));
63
64  const filename = 'test-kokoro-en-async.wav';
65  sherpa_onnx.writeWave(
66      filename, {samples: audio.samples, sampleRate: audio.sampleRate});
67  console.log(`Saved to ${filename}`);
68}
69
70main().catch((err) => {
71  console.error('Error:', err);
72});

How to run

  1. Install the package:

    npm install sherpa-onnx-node
    
  2. Download the model:

    curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/tts-models/kokoro-en-v0_19.tar.bz2
    tar xf kokoro-en-v0_19.tar.bz2
    rm kokoro-en-v0_19.tar.bz2
    
  3. Set the library path and run:

    # macOS
    export DYLD_LIBRARY_PATH=$(npm root)/sherpa-onnx-node/lib:$DYLD_LIBRARY_PATH
    
    # Linux
    export LD_LIBRARY_PATH=$(npm root)/sherpa-onnx-node/lib:$LD_LIBRARY_PATH
    
    # Choose one:
    node tts_kokoro_en.js
    node tts_kokoro_en_async.js
    

Notes

  • The config key is kokoro with fields: model, voices, tokens, dataDir.

  • sid selects the speaker voice (valid range depends on the model).

  • The sync API uses new sherpa_onnx.OfflineTts(config) and tts.generate({text, generationConfig}).

  • The async API uses OfflineTts.createAsync() and tts.generateAsync() with an onProgress callback.

  • For Chinese+English, see TTS: Kokoro (Chinese + English).