#I am using the latest node js package and trying to convert speech to text but I am getting this: T
1 messages · Page 1 of 1 (latest)
Based on the information provided, it seems like you're trying to use a function called textToSpeechStream which is not recognized. In the content provided, there is no mention of a function named textToSpeechStream.
However, there is a function called text_to_speech_stream in Python and a method to convert text to speech using websockets in Node.js. Here's how you can use it:
app.ws('/call/connection', (ws: WebSocket) => {
ws.on('message', async (data: string) => {
const message: {
event: string;
start?: { streamSid: string; callSid: string };
} = JSON.parse(data);
if (message.event === 'start' && message.start) {
const streamSid = message.start.streamSid;
const response = await elevenlabs.textToSpeech.convert(voiceId, {
model_id: 'eleven_turbo_v2_5',
output_format: outputFormat,
text,
});
}
});
});
In this code snippet, we're listening for messages on a websocket connection. When we receive a start message, we call the convert method of the textToSpeech object from the ElevenLabs SDK to convert the text to speech.
Please ensure that you're using the correct function or method name as per your SDK's documentation. If you're still facing issues, it would be helpful to check the version of the SDK you're using and ensure it supports the function you're trying to use.
This is my code:
const { ElevenLabsClient } = require("elevenlabs");
const dotenv = require("dotenv");
dotenv.config();
const ELEVENLABS_API_KEY = process.env.ELEVENLABS_API_KEY;
const VOICE_ID = process.env.ELEVENLABS_VOICE_ID;
if (!ELEVENLABS_API_KEY) {
throw new Error("ELEVENLABS_API_KEY is not set in .env file");
}
if (!VOICE_ID) {
throw new Error("ELEVENLABS_VOICE_ID is not set in .env file");
}
const client = new ElevenLabsClient(ELEVENLABS_API_KEY);
async function generateSpeech(text) {
try {
console.log('Generating speech for:', text);
const audioStream = await client.textToSpeechStream({
textInput: text,
voiceId: VOICE_ID,
modelId: "eleven_monolingual_v1",
voice_settings: {
stability: 0.5,
similarity_boost: 0.5
}
});
// Convert stream to buffer
const chunks = [];
for await (const chunk of audioStream) {
chunks.push(chunk);
}
const audioBuffer = Buffer.concat(chunks);
console.log('Speech generated successfully');
return audioBuffer;
} catch (error) {
console.error('Error generating speech:', error);
throw error;
}
}
module.exports = { generateSpeech };