Text-to-speech · Synthesize speech from text1 / 4
  1. 02
  2. 03
  3. 04

Synthesize speech from text

Example on GitHub(packages/sdk/examples/tts/supertonic.ts)

We're starting a new chapter on text-to-speech, and we're going to turn text into audio.

Text-to-speech runs the Supertonic engine on the supplied text and hands us back raw audio samples. The result is an Int16Array (one sample per array slot), 44100 Hz by default. We write it to disk as a WAV with a 44-byte header prepended.

Supertonic needs three knobs at first load: ttsEngine (backend), language, voice (F1/F2/M1/M2). All three are required. The first load with them set would look like the following:

const modelId = await loadModel({
  modelSrc: TTS_MULTILINGUAL_SUPERTONIC3_Q8_0,
  modelConfig: {
    ttsEngine: "supertonic",
    language: "en",
    voice: "F1",
  },
});

textToSpeech() is fire-and-await. Returns a 44.1 kHz mono Int16Array, the unit a WAV writer expects as follows:

const result = textToSpeech({
  modelId,
  text: "Hello, world.",
  inputType: "text",
  stream: false,
});
const audioBuffer = await result.buffer;
console.log(`▸ TTS complete. Total samples: ${audioBuffer.length}`);

To save it as a .wav file, we prepend a 44-byte header (see the supertonic example's createWav helper). The samples themselves are Int16Array values at the engine's sample rate.

Note: the ttsSpeed and ttsNumInferenceSteps fields in modelConfig trade latency against quality. ttsSpeed: 1.05 and ttsNumInferenceSteps: 5 are the defaults in the SDK and a good starting point.

Put it to the test

  1. Call loadModel with modelConfig.ttsEngine: "supertonic", a language, and a voice.
  2. Call textToSpeech({ modelId, text, inputType: "text", stream: false }) and await result.buffer.
  3. Log the buffer length.
index.ts

$ Run your code to see results

$