#Hello, I'm trying out the 'Convert text to speech (streaming)' typescript version that was in the i
1 messages · Page 1 of 1 (latest)
The code you've provided is correctly generating an audio stream from the text and concatenating the chunks into a single Buffer. However, it doesn't seem to be playing the audio, which might be why you're not hearing anything.
To play the audio, you'll need to write the Buffer to a file and then play that file. Here's an example of how you can do this using the fs module to write the file and a package like play-sound to play the audio:
import { ElevenLabsClient } from "elevenlabs";
import * as dotenv from "dotenv";
import * as fs from 'fs';
import * as player from 'play-sound';
dotenv.config();
const ELEVENLABS_API_KEY = process.env.ELEVENLABS_API_KEY;
if (!ELEVENLABS_API_KEY) {
throw new Error("Missing ELEVENLABS_API_KEY in environment variables");
}
const client = new ElevenLabsClient({
apiKey: ELEVENLABS_API_KEY,
});
export const createAudioStreamFromText = async (
text: string
): Promise<Buffer> => {
const audioStream = await client.generate({
voice: "Rachel",
model_id: "eleven_turbo_v2_5",
text,
});
const chunks: Buffer[] = [];
for await (const chunk of audioStream) {
chunks.push(chunk);
}
const content = Buffer.concat(chunks);
return content;
};
// Use the function
const audioBuffer = await createAudioStreamFromText('Hello, world!');
// Write the buffer to a file
fs.writeFileSync('output.mp3', audioBuffer);
// Play the audio file
player.play('output.mp3', (err) => {
if (err) throw err
});
This code will write the audio to a file named output.mp3 and then play that file. Please make sure to install the play-sound package using npm:
npm install play-sound
Please note that the play-sound package relies on command line audio players which need to be installed on your system. For example, on macOS, it uses the afplay command, which is installed by default. On other systems, you may need to install a command line audio player like mpg123.
Is there another way instead of using the play-sound package?
Yes, there are several ways to play audio in a Node.js application. One of the most common ways is to use the speaker package. Here's how you can modify your code to use speaker:
First, install the speaker package:
npm install speaker
Then, modify your code as follows:
import { ElevenLabsClient } from "elevenlabs";
import * as dotenv from "dotenv";
import Speaker from 'speaker';
dotenv.config();
const ELEVENLABS_API_KEY = process.env.ELEVENLABS_API_KEY;
if (!ELEVENLABS_API_KEY) {
throw new Error("Missing ELEVENLABS_API_KEY in environment variables");
}
const client = new ElevenLabsClient({
apiKey: ELEVENLABS_API_KEY,
});
export const createAudioStreamFromText = async (
text: string
): Promise<void> => {
const audioStream = await client.generate({
voice: "Rachel",
model_id: "eleven_turbo_v2_5",
text,
});
const speaker = new Speaker({
channels: 1, // 2 channels
bitDepth: 16, // 16-bit samples
sampleRate: 24000 // 44,100 Hz sample rate
});
audioStream.pipe(speaker);
};
This code will play the audio directly from the stream, without the need to collect all the chunks into a buffer first. Please note that the speaker package requires an audio backend to be installed on your system. On most systems, this should work out of the box, but on some systems, you might need to install additional software.
I don't want to install any npm packages.