Text-to-speech · Synthesize speech from text1 / 5

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.

Questions

Question 1 of 3

After a textToSpeech() call with stream: false, what does result.buffer contain?

Question 2 of 3

Which of the following is a bundled Supertonic voice?

Question 3 of 3

What is the SDK's default value for ttsSpeed if you leave it unset?

index.ts
Loading editor...

Run your code, check your answer, or ask a question. It all shows up here.