-
Notifications
You must be signed in to change notification settings - Fork 100
/
Copy pathindex.js
107 lines (82 loc) · 3.18 KB
/
index.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
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");
import Express from "express";
const app = Express();
import speechApi from "./api/tts.js" // 导入 speechApi 函数
import path from 'path';
const __dirname = path.resolve();
app.set('x-powered-by', false)
app.use(Express.json())
app.use(Express.urlencoded({ extended: false }))
app.use(Express.static(__dirname+'/public'));
app.get("/audio", async (req, res) => {
try {
const { voice, rate, pitch, text,voiceStyle } = req.query;
const ssml = `<speak xmlns="http://www.w3.org/2001/10/synthesis" xmlns:mstts="http://www.w3.org/2001/mstts" xmlns:emo="http://www.w3.org/2009/10/emotionml" version="1.0" xml:lang="en-US">
<voice name="${voice}">
<mstts:express-as style="${voiceStyle}">
<prosody rate="${rate}%" pitch="${pitch}%">
${text}
</prosody>
</mstts:express-as>
</voice>
</speak>`;
// 调用 speechApi 函数,获取音频数据
const audioData = await speechApi(ssml);
const nowtime = new Date().getTime();
// 设置响应头为 audio/mp3
res.set("Content-Type", "audio/mpeg");
res.set("Content-Disposition", `attachment; filename=${nowtime}.mp3`);
// 将音频数据发送给客户端
res.send(audioData);
} catch (error) {
console.error("Failed to generate audio:",error.message);
const errorJson = {
error: error.message,
};
res.status(500).json(errorJson);
}
});
app.get("/sourcereader", async (req, res) => {
try{
const { voice, rate, pitch, voiceStyle } = req.query;
// const hostname = req.hostname;
const hostname = req.rawHeaders[1];
console.log(req.rawHeaders[1]);
const dataJson = {
"concurrentRate": "",//并发率
"contentType": "audio/mpeg",
"header": "",
"id": Date.now(),
"lastUpdateTime": Date.now(),
"loginCheckJs": "",
"loginUi": "",
"loginUrl": "",
"name": `Azure ${voice} ${voiceStyle} pitch: ${pitch} rate:${rate}`,
"url": `https://${hostname}/audio?text={{speakText}}&rate=${rate}&pitch=${pitch}&voice=${voice}&voiceStyle=${voiceStyle},{"method":"GET"}`,
}
res.send(dataJson);
}catch(error){
}
});
app.get("/legado", async (req, res) => {
try{
const { voice, rate, pitch, voiceStyle } = req.query;
// const hostname = req.hostname;
const hostname = req.rawHeaders[1];
console.log(req.rawHeaders[1]);
const dataJson = [{
"customOrder": 100,
"id": Date.now(),
"lastUpdateTime": Date.now(),
"name": ` ${voice} ${voiceStyle} pitch: ${pitch} rate:${rate}`,
"url": `https://${hostname}/audio?text={{speakText}}&rate=${rate}&pitch=${pitch}&voice=${voice}&voiceStyle=${voiceStyle},{"method":"GET"}`,
}]
res.send(dataJson);
}catch(error){
}
});
// 启动服务器,监听在指定端口上
const port = process.env.PORT || 3035;
app.listen(port, () => {
console.log('Start service success! listening port: http://127.0.0.1:' + port);
});