Skip to content

Commit

Permalink
ws中加入token
Browse files Browse the repository at this point in the history
  • Loading branch information
liugaowei committed Feb 21, 2023
1 parent f5d870e commit bec8325
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 88 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"name": "tts-vue",
"version": "1.8.7",
"version": "1.8.8",
"main": "dist/electron/main/index.js",
"description": "🎤 微软语音合成工具,使用 Electron + Vue + ElementPlus + Vite 构建。",
"author": "沫離Loker <806076693@qq.com>",
"author": "沫離Loker <loker80@qq.com>",
"license": "MIT",
"private": true,
"scripts": {
Expand Down
141 changes: 76 additions & 65 deletions src/store/play.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ const { ipcRenderer } = require("electron");

async function getAuthToken() {
const res = await axios.get(
"https://azure.microsoft.com/en-gb/services/cognitive-services/text-to-speech/"
"https://azure.microsoft.com/zh-cn/products/cognitive-services/speech-translation/"
);

const reg = /token: \"(.*?)\"/;

if (reg.test(res.data)) {
const token = RegExp.$1;

return token;
return "bearer " + token;
}
}

Expand Down Expand Up @@ -51,9 +51,10 @@ async function getTTSData(
rate = 0,
pitch = 0
) {
let SSML = "";
if (inps.activeIndex == "1") {
SSML = `
try {
let SSML = "";
if (inps.activeIndex == "1") {
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 ${
Expand All @@ -66,72 +67,82 @@ async function getTTSData(
</voice>
</speak>
`;
} else {
SSML = inps.inputValue;
}
ipcRenderer.send("log.info", SSML);
console.log(SSML);
} else {
SSML = inps.inputValue;
}
ipcRenderer.send("log.info", SSML);
console.log(SSML);

// console.log("获取Token...");
// const Authorization = await getAuthToken();
const XConnectionId = uuidv4().toUpperCase().replaceAll("-", "");
console.log("获取Token...");
const Authorization = await getAuthToken();
const XConnectionId = uuidv4().toUpperCase().replaceAll("-", "");

ipcRenderer.send("log.info", "创建webscoket连接...");
// const connect: any = await wssConnect(
// `wss://eastus.tts.speech.microsoft.com/cognitiveservices/websocket/v1?Authorization=${Authorization}&X-ConnectionId=${XConnectionId}`
// );
console.log("创建webscoket连接...");
console.log(XConnectionId);
const connect: any = await wssConnect(
`wss://eastus.api.speech.microsoft.com/cognitiveservices/websocket/v1?TrafficType=AzureDemo&Authorization=bearer%20undefined&X-ConnectionId=${XConnectionId}`
);
ipcRenderer.send("log.info", "创建webscoket连接...");
// const connect: any = await wssConnect(
// `wss://eastus.tts.speech.microsoft.com/cognitiveservices/websocket/v1?Authorization=${Authorization}&X-ConnectionId=${XConnectionId}`
// );
console.log("创建webscoket连接...");
console.log("Authorization:", Authorization);
console.log("XConnectionId:", XConnectionId);
const connect: any = await wssConnect(
`wss://eastus.api.speech.microsoft.com/cognitiveservices/websocket/v1?TrafficType=AzureDemo&Authorization=${Authorization}&X-ConnectionId=${XConnectionId}`
);

ipcRenderer.send("log.info", "第1次上报...");
console.log("第1次上报...");
const message_1 = `Path: speech.config\r\nX-RequestId: ${XConnectionId}\r\nX-Timestamp: ${getXTime()}\r\nContent-Type: application/json\r\n\r\n{"context":{"system":{"name":"SpeechSDK","version":"1.19.0","build":"JavaScript","lang":"JavaScript","os":{"platform":"Browser/Linux x86_64","name":"Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Firefox/78.0","version":"5.0 (X11)"}}}}`;
await wssSend(connect, message_1);
ipcRenderer.send("log.info", "第1次上报...");
console.log("第1次上报...");
const message_1 = `Path: speech.config\r\nX-RequestId: ${XConnectionId}\r\nX-Timestamp: ${getXTime()}\r\nContent-Type: application/json\r\n\r\n{"context":{"system":{"name":"SpeechSDK","version":"1.19.0","build":"JavaScript","lang":"JavaScript","os":{"platform":"Browser/Linux x86_64","name":"Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Firefox/78.0","version":"5.0 (X11)"}}}}`;
await wssSend(connect, message_1);

ipcRenderer.send("log.info", "第2次上报...");
console.log("第2次上报...");
const message_2 = `Path: synthesis.context\r\nX-RequestId: ${XConnectionId}\r\nX-Timestamp: ${getXTime()}\r\nContent-Type: application/json\r\n\r\n{"synthesis":{"audio":{"metadataOptions":{"sentenceBoundaryEnabled":false,"wordBoundaryEnabled":false},"outputFormat":"audio-24khz-160kbitrate-mono-mp3"}}}`;
await wssSend(connect, message_2);
ipcRenderer.send("log.info", "第2次上报...");
console.log("第2次上报...");
const message_2 = `Path: synthesis.context\r\nX-RequestId: ${XConnectionId}\r\nX-Timestamp: ${getXTime()}\r\nContent-Type: application/json\r\n\r\n{"synthesis":{"audio":{"metadataOptions":{"sentenceBoundaryEnabled":false,"wordBoundaryEnabled":false},"outputFormat":"audio-24khz-160kbitrate-mono-mp3"}}}`;
await wssSend(connect, message_2);

ipcRenderer.send("log.info", "第3次上报...");
console.log("第3次上报...");
const message_3 = `Path: ssml\r\nX-RequestId: ${XConnectionId}\r\nX-Timestamp: ${getXTime()}\r\nContent-Type: application/ssml+xml\r\n\r\n${SSML}`;
await wssSend(connect, message_3);
ipcRenderer.send("log.info", "第3次上报...");
console.log("第3次上报...");
const message_3 = `Path: ssml\r\nX-RequestId: ${XConnectionId}\r\nX-Timestamp: ${getXTime()}\r\nContent-Type: application/ssml+xml\r\n\r\n${SSML}`;
await wssSend(connect, message_3);

return new Promise((resolve, reject) => {
let final_data = Buffer.alloc(0);
connect.on("text", (data: string | string[]) => {
if (data.indexOf("Path:turn.end") >= 0) {
ipcRenderer.send("log.info", "已完成");
console.log("已完成");
connect.close();
resolve(final_data);
}
return new Promise((resolve, reject) => {
let final_data = Buffer.alloc(0);
connect.on("text", (data: string | string[]) => {
if (data.indexOf("Path:turn.end") >= 0) {
ipcRenderer.send("log.info", "已完成");
console.log("已完成");
connect.close();
resolve(final_data);
}
});
connect.on(
"binary",
function (response: {
on: (arg0: string, arg1: { (): void; (): void }) => void;
read: () => any;
}) {
console.log("正在接收数据...");
let data = Buffer.alloc(0);
response.on("readable", function () {
const newData = response.read();
if (newData)
data = Buffer.concat(
[data, newData],
data.length + newData.length
);
});
response.on("end", function () {
const index = data.toString().indexOf("Path:audio") + 10;
const cmbData = data.slice(index + 2);
final_data = Buffer.concat([final_data, cmbData]);
});
}
);
connect.on("close", function (code: any, reason: any) {});
});
connect.on(
"binary",
function (response: {
on: (arg0: string, arg1: { (): void; (): void }) => void;
read: () => any;
}) {
console.log("正在接收数据...");
let data = Buffer.alloc(0);
response.on("readable", function () {
const newData = response.read();
if (newData)
data = Buffer.concat([data, newData], data.length + newData.length);
});
response.on("end", function () {
const index = data.toString().indexOf("Path:audio") + 10;
const cmbData = data.slice(index + 2);
final_data = Buffer.concat([final_data, cmbData]);
});
}
);
connect.on("close", function (code: any, reason: any) {});
});
} catch (error) {
console.log(error);
return new Promise((resolve, reject) => {
reject(error);
});
}
}
export default getTTSData;
56 changes: 35 additions & 21 deletions src/store/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,12 +178,17 @@ export const useTtsStore = defineStore("ttsStore", {
this.formConfig.role,
(this.formConfig.speed - 1) * 100,
(this.formConfig.pitch - 1) * 50
).then((mp3buffer: any) => {
this.currMp3Buffer = mp3buffer;
const svlob = new Blob([mp3buffer]);
this.currMp3Url = URL.createObjectURL(svlob);
this.isLoading = false;
});
)
.then((mp3buffer: any) => {
this.currMp3Buffer = mp3buffer;
const svlob = new Blob([mp3buffer]);
this.currMp3Url = URL.createObjectURL(svlob);
this.isLoading = false;
})
.catch((err) => {
this.isLoading = false;
console.log(err);
});
}
ElMessage({
message: this.config.autoplay
Expand Down Expand Up @@ -284,16 +289,21 @@ export const useTtsStore = defineStore("ttsStore", {
this.formConfig.role,
(this.formConfig.speed - 1) * 100,
(this.formConfig.pitch - 1) * 50
).then((mp3buffer: any) => {
fs.writeFileSync(filePath, mp3buffer);
this.setDoneStatus(item.filePath);
ElMessage({
message: "成功,正在写入" + filePath,
type: "success",
duration: 2000,
)
.then((mp3buffer: any) => {
fs.writeFileSync(filePath, mp3buffer);
this.setDoneStatus(item.filePath);
ElMessage({
message: "成功,正在写入" + filePath,
type: "success",
duration: 2000,
});
this.isLoading = false;
})
.catch((err) => {
this.isLoading = false;
console.log(err);
});
this.isLoading = false;
});
}
}
);
Expand Down Expand Up @@ -336,12 +346,16 @@ export const useTtsStore = defineStore("ttsStore", {
this.formConfig.role,
(this.formConfig.speed - 1) * 100,
(this.formConfig.pitch - 1) * 50
).then((mp3buffer: any) => {
this.currMp3Buffer = mp3buffer;
const svlob = new Blob([mp3buffer]);
const sound = new Audio(URL.createObjectURL(svlob));
sound.play();
});
)
.then((mp3buffer: any) => {
this.currMp3Buffer = mp3buffer;
const svlob = new Blob([mp3buffer]);
const sound = new Audio(URL.createObjectURL(svlob));
sound.play();
})
.catch((err: any) => {
console.log(err);
});
},
showItemInFolder(filePath: string) {
ipcRenderer.send("showItemInFolder", filePath);
Expand Down

0 comments on commit bec8325

Please sign in to comment.