From bec83259eddb3c803a9ff422cd4ec351abfa69af Mon Sep 17 00:00:00 2001 From: liugaowei Date: Tue, 21 Feb 2023 10:49:00 +0800 Subject: [PATCH] =?UTF-8?q?ws=E4=B8=AD=E5=8A=A0=E5=85=A5token?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 4 +- src/store/play.ts | 141 ++++++++++++++++++++++++--------------------- src/store/store.ts | 56 +++++++++++------- 3 files changed, 113 insertions(+), 88 deletions(-) diff --git a/package.json b/package.json index 8b979a8..1622d4d 100644 --- a/package.json +++ b/package.json @@ -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 ", "license": "MIT", "private": true, "scripts": { diff --git a/src/store/play.ts b/src/store/play.ts index 750f516..673bf42 100644 --- a/src/store/play.ts +++ b/src/store/play.ts @@ -5,7 +5,7 @@ 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: \"(.*?)\"/; @@ -13,7 +13,7 @@ async function getAuthToken() { if (reg.test(res.data)) { const token = RegExp.$1; - return token; + return "bearer " + token; } } @@ -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 = ` `; - } 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; diff --git a/src/store/store.ts b/src/store/store.ts index 374e128..4dceeac 100644 --- a/src/store/store.ts +++ b/src/store/store.ts @@ -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 @@ -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; - }); } } ); @@ -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);