Spaces:
Sleeping
Sleeping
| const express = require('express'); | |
| const cors = require('cors'); // Add this line | |
| 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()); // Use cors middleware here | |
| // Serve static files from the "public" directory | |
| 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 || 490; | |
| let subtext= text.substring(0, max_characters-50); | |
| console.log('max_characters:',max_characters); | |
| console.log('text that will be convert to audio:', subtext); | |
| try { | |
| let response = await fetch("https://api.ttsopenai.com/api/v1/text-to-speech-stream", { | |
| method: "POST", | |
| headers: { | |
| "accept": "application/json", | |
| "accept-language": "en-US,en;q=0.9", | |
| "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", | |
| "authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3MzM5ODk1ODAsInN1YiI6IjAyZGJjMTMyLWI3OTQtMTFlZi1iNmIwLTk2YjQ3NWYyOWRkZSJ9.uag7hSnnxQzpHwWCFuEdTphjMuC_M9CBcngJLjQeJcg" | |
| }, | |
| body: JSON.stringify({ "model": "tts-1", "voice": "alloy", "voice_id":"OA001","speed": 1, "input": subtext +". openai tts,this speech is generated by openai tts" || 'hi this tts from openai, input you text now' }) | |
| }); | |
| console.log('response:', response.status,Date.now()-startTime); | |
| if (response.status !== 200) { | |
| // console.log(response); | |
| throw Error(response.status + await response.text()) | |
| } | |
| let res_json = await response.json(); | |
| console.log('get response ',Date.now()-startTime); | |
| response= await fetch('https://api.ttsopenai.com/api/v1/history/'+res_json.uuid,{ | |
| headers: { | |
| "accept": "application/json", | |
| "accept-language": "en-US,en;q=0.9", | |
| "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", | |
| "authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3MzM5ODk1ODAsInN1YiI6IjAyZGJjMTMyLWI3OTQtMTFlZi1iNmIwLTk2YjQ3NWYyOWRkZSJ9.uag7hSnnxQzpHwWCFuEdTphjMuC_M9CBcngJLjQeJcg" | |
| } | |
| }) | |
| res_json=await response.json() | |
| res.redirect(res_json.media_url) | |
| // const mp3File = fs.createWriteStream('output.mp3'); | |
| // mp3File.write(Buffer.from(mp3Buffer)); | |
| // mp3File.end(); | |
| // console.log('MP3 file saved to output.mp3'); | |
| // Send the saved file to the client | |
| //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 +`.\n speaked by Azure TTS`, | |
| function (result) { | |
| if (result.reason === sdk.ResultReason.SynthesizingAudioCompleted) { | |
| console.log("synthesis finished."); | |
| res.sendFile(path.join(__dirname,audioFile)); // This will initiate file download | |
| } 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}`)); | |