Docs · Leokani-tts

Leokani for JavaScript

Speech in the browser or in Node 20+, on any CPU.

Install

Download the package and the model, then install the package:

curl -O https://milliwattlabs.com/models/leokani-tts/downloads/leokani-1.1.0.tgz
curl -O https://milliwattlabs.com/models/leokani-tts/demo/leokani-tts-fp16.onnx
npm i ./leokani-1.1.0.tgz

Browser

Serve this page next to node_modules/ and the model:

<button id="speak">Speak</button>
<script type="module">
  import { Leokani, play } from './node_modules/leokani/src/index.mjs';

  const tts = await Leokani.load({ modelUrl: './leokani-tts-fp16.onnx' });
  document.querySelector('#speak').onclick = () => play(tts.stream('Hello from your browser.'));
</script>

With Vite or webpack, import from 'leokani' instead.

Node

import { writeFile } from 'node:fs/promises';
import { Leokani, toWav } from 'leokani';

const tts = await Leokani.load({ modelUrl: './leokani-tts-fp16.onnx' });
const { samples } = await tts.speak('Hello from Node.');
await writeFile('hello.wav', toWav(samples));
await tts.dispose();

For about 4× the speed, also run npm i onnxruntime-node. Leokani uses it when it's installed.

Voice

One English voice. speed sets the pace: 1 is normal, 0.85 a little slower, 1.2 a little faster.

const { samples } = await tts.speak('A little slower.', { speed: 0.85 });

Audio out

  • tts.speak(text) gives { samples, sampleRate }: mono Float32Array at 24 kHz.
  • tts.stream(text) gives the same audio one sentence at a time, each as soon as it's ready.
  • toWav(samples) gives the bytes of a WAV file.
  • play(audio) plays a clip or a stream in the browser. Call it from a click.
import { toWav } from 'leokani';

const { samples } = await tts.speak('Save me.');
const url = URL.createObjectURL(new Blob([toWav(samples)], { type: 'audio/wav' }));

Leokani-tts is CC BY-NC 4.0. For a commercial license or a custom voice, email hello@milliwattlabs.com.