NodeJS SDK Streaming

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.

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

For the most up-to-date features, including our newest models, make sure to get the latest version.

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({
  userId: "<YOUR_USER_ID>",
  apiKey: "<YOUR_API_KEY>",
});

// configure your stream
const streamingOptions = {
  // must use turbo for the best latency
  voiceEngine: "Play3.0",
  // 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, 48000 is default
  sampleRate: 48000,
  // 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

LLM + Audio Streaming with llama on Groq

Streaming audio to a local file

Streaming Options

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

ParameterTypeValuesDefaultDescription
voiceEnginestring'Play3.0''Play3.0'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 4800048000Sample rate for the output audio.
outputFormatstring'raw' | 'mp3' | 'wav' | 'ogg' | 'flac' | 'mulaw'mp3The format in which the output audio should be generated.
speednumberA number greater than 0 and less than or equal to 5.0.1.0Controls how fast the generated audio should be.
temperaturenumberA floating point number between 0, inclusive, and 2, inclusive.0.7Controls variance. Lower temperatures result in more predictable results. Higher temperatures allow each run to vary more, creating voices that sound less like the baseline.
textGuidancenumberA floating point number between 0 and 2.0.8This 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.
nullControls 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.

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.