Realtime audio streaming using our nodejs SDK

Get started in 2 mins with our Nodejs SDK streaming. You can also try streaming using the Python SDK.

🚧

Model Version

For the lowest realtime latency, you must pass voiceEngine: "PlayHT2.0-turbo" when calling the nodejs sdk.

PlayHT2.0-turbo is the latest version of our Conversational Text to Voice model called Gargamel, and has improved quality and reliability especially with handling acronyms, emails, phone numbers, addresses, etc.

Get your Credentials

To use the HTTP API, you will need an API Key and a User ID; you can easily generate those, check this guide for a how-to.

Set Up

First, Install the Nodejs SDK:

npm install --save playht

Let's Stream

Let's stream some sentences and see how quickly we get that first chunk of data for each one. Heads up: you'll need your credentials for PlayHT. Swap out the placeholders in the code.

// import the playht SDK
import * as PlayHT from "playht";

// Initialize PlayHT API with your credentials
PlayHT.init({
  apiKey: "<YOUR_PLAY_HT_API_KEY>",
  userId: "<YOUR_PLAY_HT_USER_ID>",
});

// configure your stream
const streamingOptions = {
  // must use turbo for the best latency
  voiceEngine: "PlayHT2.0-turbo",
  // this voice id can be one of our prebuilt voices or your own voice clone id, refer to the`listVoices()` method for a list of supported voices.
  voiceId:
    "s3://voice-cloning-zero-shot/d9ff78ba-d016-47f6-b0ef-dd630f59414e/female-cs/manifest.json",
  // you can pass any value between 8000 and 48000, 24000 is default
  sampleRate: 44100,
  // the generated audio encoding, supports 'raw' | 'mp3' | 'wav' | 'ogg' | 'flac' | 'mulaw'
  outputFormat: 'mp3',
  // playback rate of generated speech
  speed: 1,
};

// start streaming!
const text = "Hey, this is Jennifer from Play. Please hold on a moment, let me just um pull up your details real quick."
const stream = await PlayHT.stream(text, streamingOptions);

stream.on("data", (chunk) => {
  // Do whatever you want with the stream, you could save it to a file, stream it in realtime to the browser or app, or to a telephony system
});

💡

Input Streaming

The .stream endpoint can accept text or any Readable stream as an input, so you can just pass to it the output coming from any LLM like chatGPT as you can see in this example.

Demos

Audio Input and Output Streaming with chatGPT

Stream audio to a local file

Streaming Options

The full list of options you can use to control the generated audio.

ParameterTypeValuesDescription
voiceEnginestring'PlayHT2.0-turbo' | 'PlayHT2.0' | 'PlayHT1.0' | 'Standard'
You must use "PlayHT2.0-turbo" for the lowest latency
Specifies the voice model to be used for speech synthesis.
voiceIdstring__Identifier for the voice to be used to synthesize the text. Refer to thelistVoices() method for a list of supported voices.
sampleRatenumberA number greater than or equal to 8000, and must be less than or equal to 48000Sample rate for the output audio.
outputFormatstring'raw' | 'mp3' | 'wav' | 'ogg' | 'flac' | 'mulaw'The format in which the output audio should be generated. Defaults to 'mp3'.
speednumberA number greater than 0 and less than or equal to 5.0.Controls how fast the generated audio should be.
temperaturenumberA floating point number between 0, inclusive, and 2, inclusive.Controls variance. Lower temperatures result in more predictable results. Higher temperatures allow each run to vary more, creating voices that sound less like the baseline.
voiceGuidancenumberA number between 1 and 6.Use lower numbers to reduce how unique your chosen voice will be compared to other voices. Higher numbers will maximize its individuality.
textGuidancenumberA floating point number between 0 and 2.This number influences how closely the generated speech adheres to the input text. Use lower values to create more fluid speech, but with a higher chance of deviating from the input text. Higher numbers will make the generated speech more accurate to the input text, ensuring that the words spoken align closely with the provided text.
seednumberAn integer number greater than or equal to 0.
If equal to null or not provided, a random seed will be used every time.
Controls the reproducibility of the generated audio. Assuming all other properties didn't change, a fixed seed will generate the exact same audio file given the same text and voiceId.
emotionstring 'female_happy' | 'female_sad' | 'female_angry' | 'female_fearful' | 'female_disgust' | 'female_surprised' | 'male_happy' | 'male_sad' | 'male_angry' | 'male_fearful' | 'male_disgust' | 'male_surprised'An emotion to be applied to the speech. Works for both stock or cloned voices.
It can result in less reliable results with more hallucinations
styleGuidancenumberNumber between 1 and 30.Use lower numbers to reduce how strong your chosen emotion will be. Higher numbers will create a very emotional performance.
Only applied when an emotion is selected.
It can result in less reliable results with more hallucinations

That is all you need to get started with realtime streaming through our Nodejs SDK. If you need support, reach out to us at [email protected] or join us on Discord.