|
|
const express = require('express'); |
|
|
const cors = require('cors'); |
|
|
const app = express(); |
|
|
const sdk = require("microsoft-cognitiveservices-speech-sdk"); |
|
|
const path = require('path'); |
|
|
const fs = require('fs') |
|
|
|
|
|
let MAX_CHAR=parseInt(process.env.MAX_CHAR) |
|
|
|
|
|
console.log('MAX_CHAR:',MAX_CHAR); |
|
|
|
|
|
|
|
|
app.use(cors()); |
|
|
|
|
|
|
|
|
app.use(express.static(path.join(__dirname, 'public'))); |
|
|
|
|
|
app.get('/tts/:text', async (req, res) => { |
|
|
let startTime=Date.now(); |
|
|
console.log('start /tts ' + startTime); |
|
|
let text = req?.params?.text || 'test'; |
|
|
let max_characters=MAX_CHAR || 498; |
|
|
let subtext= text.substring(0, max_characters); |
|
|
|
|
|
console.log('max_characters:',max_characters); |
|
|
console.log('text that will be convert to audio:', subtext); |
|
|
|
|
|
|
|
|
try { |
|
|
const response = await fetch("https://api.ttsopenai.com/api/v1/public/text-to-speech-stream", { |
|
|
method: "POST", |
|
|
headers: { |
|
|
"accept": "application/json", |
|
|
"accept-language": "en-US,en;q=0.9", |
|
|
"authorization": "", |
|
|
"cache-control": "no-cache", "content-type": "application/json", "pragma": "no-cache", "sec-ch-ua": "\"Google Chrome\";v=\"123\", \"Not:A-Brand\";v=\"8\", \"Chromium\";v=\"123\"", "sec-ch-ua-mobile": "?0", "sec-ch-ua-platform": "\"Linux\"", "sec-fetch-dest": "empty", "sec-fetch-mode": "cors", "sec-fetch-site": "same-site", |
|
|
"Referer": "https://ttsopenai.com/", |
|
|
"Referrer-Policy": "strict-origin-when-cross-origin" |
|
|
}, |
|
|
body: JSON.stringify({ "model": "tts-1", "voice": "alloy", "voice_id":"OA001","speed": 1, "input": subtext }) |
|
|
}); |
|
|
console.log('response:', response.status,Date.now()-startTime); |
|
|
if (response.status !== 200) { |
|
|
|
|
|
throw Error(response.status + await response.text()) |
|
|
} |
|
|
const mp3Buffer = await response.arrayBuffer(); |
|
|
console.log('gt mp3 buffer ',Date.now()-startTime); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
res.set('Content-Type', 'audio/mpeg'); |
|
|
res.send(Buffer.from(mp3Buffer)) |
|
|
console.log('sent output.mp3 ',Date.now()-startTime); |
|
|
|
|
|
return; |
|
|
} catch (error) { |
|
|
console.error(error, 'try azure tts'); |
|
|
} |
|
|
console.log('redirct to auretts'); |
|
|
res.redirect('/atts'+text); |
|
|
}); |
|
|
|
|
|
app.get('/atts/:text', (req, res) => { |
|
|
const text = req?.params?.text || 'test'; |
|
|
const voice = req?.query?.voicename || "de-DE-SeraphinaMultilingualNeural"; |
|
|
let audioFile = "audio.mp3"; |
|
|
let speechConfig = sdk.SpeechConfig.fromSubscription(process.env.SPEECH_KEY, process.env.SPEECH_REGION); |
|
|
let audioConfig = sdk.AudioConfig.fromAudioFileOutput(audioFile); |
|
|
speechConfig.speechSynthesisOutputFormat = sdk.SpeechSynthesisOutputFormat.Audio24Khz160KBitRateMonoMp3; |
|
|
speechConfig.speechSynthesisVoiceName = voice; |
|
|
let synthesizer = new sdk.SpeechSynthesizer(speechConfig, audioConfig); |
|
|
|
|
|
|
|
|
console.log('/attss',text); |
|
|
|
|
|
|
|
|
|
|
|
synthesizer.speakTextAsync(text, |
|
|
function (result) { |
|
|
if (result.reason === sdk.ResultReason.SynthesizingAudioCompleted) { |
|
|
console.log("synthesis finished."); |
|
|
res.sendFile(path.join(__dirname,audioFile)); |
|
|
} else { |
|
|
console.error("Speech synthesis canceled, " + result.errorDetails + |
|
|
"\nDid you set the speech resource key and region values?"); |
|
|
} |
|
|
synthesizer.close(); |
|
|
synthesizer = null; |
|
|
}, |
|
|
function (err) { |
|
|
console.trace("err - " + err); |
|
|
synthesizer.close(); |
|
|
synthesizer = null; |
|
|
}); |
|
|
console.log("Now synthesizing to: " + audioFile); |
|
|
}); |
|
|
|
|
|
|
|
|
const port = process.env.PORT || 7860; |
|
|
|
|
|
app.listen(port, () => console.log(`Server is running on port ${port}`)); |
|
|
|