-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
43 lines (34 loc) · 1.39 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Load environment variables from .env file
require('dotenv').config({ path: './.env' });
// Imports the Google Cloud client library
const textToSpeech = require('@google-cloud/text-to-speech');
// Import other required libraries
const fs = require('fs');
const util = require('util');
const path = require('path');
// Import voice profiles
const voiceProfiles = require('./voiceProfiles');
// Creates a client
const client = new textToSpeech.TextToSpeechClient();
async function synthesizeSpeech(profile) {
// Read the SSML text from a file
const ssml = fs.readFileSync('input.ssml', 'utf8');
// Construct the request
const request = {
input: {ssml: ssml},
// Select the voice profile
voice: voiceProfiles[profile],
// Select the type of audio encoding
audioConfig: {audioEncoding: 'MP3'},
};
// Performs the text-to-speech request
const [response] = await client.synthesizeSpeech(request);
// Write the binary audio content to a local file
const writeFile = util.promisify(fs.writeFile);
const timestamp = new Date().toISOString().replace(/:/g, '-');
const outputFilePath = path.join('C:\\Users\\Fonck\\Music\\sound2text', `text2speech_${timestamp}.mp3`);
await writeFile(outputFilePath, response.audioContent, 'binary');
console.log(`Audio content written to file: ${outputFilePath}`);
}
// Use the male profile
synthesizeSpeech('masculina3').catch(console.error);