Skip to content

Commit

Permalink
接入edge浏览器语音
Browse files Browse the repository at this point in the history
  • Loading branch information
v3ucn committed Jul 2, 2024
1 parent a647a37 commit 4143e42
Show file tree
Hide file tree
Showing 17 changed files with 4,060 additions and 5 deletions.
161 changes: 161 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ var ejs=require('ejs');
const path = require('path');
const app = express();
const multer = require('multer');
const { EdgeTTS } = require('node-edge-tts')


app.listen(3000, () => {
console.log("Application started and Listening on port 3000");
Expand Down Expand Up @@ -46,6 +48,42 @@ app.post('/upload', upload.single('image'), (req, res) => {
});



// edge_tts接口

app.get("/edge_tts", async (req, res) => {

var speaker = req.query.speaker || 'zh-CN-XiaoxiaoNeural';

var text = req.query.text || '你好哟,这里是测试';


const tts = new EdgeTTS({
voice: speaker
})

await tts.ttsPromise(text,"output.wav")


fs.readFile("output.wav", (err, data) => {
if (err) {
console.error("读取文件错误:", err);
res.status(500).send("服务器内部错误");
return;
}

// 将音频数据编码为 Base64
const base64Audio = Buffer.from(data).toString("base64");

// 将 Base64 编码的音频数据发送到前端
res.send({ audio: base64Audio });
});



});


// 修改json接口

app.get("/edit_config", (req, res) => {
Expand Down Expand Up @@ -209,3 +247,126 @@ app.get("/llm", (req, res) => {
app.get("/", (req, res) => {
res.render(__dirname + "/index");
});



// 文字转语音 edge页面操作
app.get("/tts_edge", (req, res) => {

var filePath = "./config.json"


// 同步地遍历目录并返回目录名
function getSubdirectories(dirPath) {
return new Promise((resolve, reject) => {
fs.readdir(dirPath, { withFileTypes: true }, (err, files) => {
if (err) {
return reject(err);
}

// 过滤出目录项
const directories = files
.filter(file => file.isDirectory())
.map(file => file.name);

resolve(directories);
});
});
}

// 指定目标目录路径
const targetDir = './models/';

var dis;

// 获取目标目录下的所有目录名
getSubdirectories(targetDir).then((directories) => {

dis = directories;
})
.catch((err) => {
console.error(err);
});

// 读取文件内容
fs.readFile(filePath, 'utf8', (err, data) => {
if (err) {
res.status(500).send('Error reading file,配置文件不存在');
} else {

console.log(data);

const jsonData = JSON.parse(data);
const modelPath = jsonData.model_path;

dis = JSON.stringify(dis);

res.render(__dirname + "/live2d_edge_tts",{model_path: modelPath,model_list:dis});
}
});




});


// 大模型 edge_tts
app.get("/llm_edge_tts", (req, res) => {

var filePath = "./config.json"


// 同步地遍历目录并返回目录名
function getSubdirectories(dirPath) {
return new Promise((resolve, reject) => {
fs.readdir(dirPath, { withFileTypes: true }, (err, files) => {
if (err) {
return reject(err);
}

// 过滤出目录项
const directories = files
.filter(file => file.isDirectory())
.map(file => file.name);

resolve(directories);
});
});
}

// 指定目标目录路径
const targetDir = './models/';

var dis;

// 获取目标目录下的所有目录名
getSubdirectories(targetDir).then((directories) => {

dis = directories;
})
.catch((err) => {
console.error(err);
});

// 读取文件内容
fs.readFile(filePath, 'utf8', (err, data) => {
if (err) {
res.status(500).send('Error reading file,配置文件不存在');
} else {

console.log(data);

const jsonData = JSON.parse(data);
const modelPath = jsonData.model_path;

dis = JSON.stringify(dis);

res.render(__dirname + "/live2d_llm_edge_tts",{model_path: modelPath,model_list:dis});
}
});




});
2 changes: 1 addition & 1 deletion config.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"model_path":"EVA RIN normal"}
{"model_path":"Nova - F"}
12 changes: 10 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,19 @@

<div style="margin-top: 50px; font-size: 18px;">

<a href="/tts">live2d数字人,文字转语音功能</a>
<a href="/tts">live2d数字人,文字转语音功能(gpt-sovits/bert-vits2)</a>

<br /><br />

<a href="/llm">live2d数字人,大语言模型聊天</a>
<a href="/tts_edge">live2d数字人,文字转语音功能,edge_tts,无须第三方tts</a>

<br /><br />

<a href="/llm">live2d数字人,大语言模型聊天(gpt-sovits/bert-vits2)</a>

<br /><br />

<a href="/llm_edge_tts">live2d数字人,大语言模型聊天(edge_tts,无须第三方tts)</a>

</div>

Expand Down
Loading

0 comments on commit 4143e42

Please sign in to comment.