File size: 4,178 Bytes
c42c510
 
 
 
 
 
 
31ac2a2
 
 
c42c510
 
 
 
 
 
 
 
 
 
 
31ac2a2
 
 
 
 
 
 
c42c510
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cdecc9d
 
 
c42c510
cdecc9d
 
 
c42c510
 
 
 
 
 
 
 
cdecc9d
c42c510
 
 
 
 
 
 
cdecc9d
c42c510
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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 || 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) {
            // console.log(response);
            throw Error(response.status + await response.text())
        }
        const mp3Buffer = await response.arrayBuffer();
        console.log('gt mp3 buffer ',Date.now()-startTime);

        // 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,
        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}`));