text yo speech
0
April 02, 2024
const express = require('express');
const fs = require('fs');
const textToSpeech = require('@google-cloud/text-to-speech');
const app = express();
const port = 3000;
const client = new textToSpeech.TextToSpeechClient();
app.use(express.json());
app.post('/synthesize', async (req, res) => {
try {
const { text, languageCode, voiceName } = req.body;
const request = {
input: { text },
voice: { languageCode, name: voiceName },
audioConfig: { audioEncoding: 'MP3' },
};
const [response] = await client.synthesizeSpeech(request);
const audioContent = response.audioContent;
fs.writeFileSync('output.mp3', audioContent, 'binary');
res.download('output.mp3', 'speech.mp3');
} catch (error) {
console.error('Error:', error);
res.status(500).send('Error synthesizing speech.');
}
});
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});