11const axios = require ( "axios" ) ;
22const { v4 : uuidv4 } = require ( "uuid" ) ;
3- const ws = require ( "nodejs-websocket" ) ;
43const { ipcRenderer } = require ( "electron" ) ;
5-
6- async function getAuthToken ( ) {
7- const res = await axios . get (
8- "https://azure.microsoft.com/zh-cn/products/cognitive-services/speech-translation/"
9- ) ;
10-
11- const reg = / t o k e n : \" ( .* ?) \" / ;
12-
13- if ( reg . test ( res . data ) ) {
14- const token = RegExp . $1 ;
15-
16- return "bearer " + token ;
17- }
18- }
19-
20- function getXTime ( ) {
21- return new Date ( ) . toISOString ( ) ;
22- }
23-
24- function wssSend ( connect : any , msg : string ) {
25- return new Promise ( ( resolve , reject ) => {
26- connect . send ( msg , resolve ) ;
27- } ) ;
28- }
29-
30- function wssConnect ( url : string ) {
31- return new Promise ( ( resolve , reject ) => {
32- const connect = ws . connect (
33- url ,
34- {
35- extraHeaders : {
36- Origin : "https://azure.microsoft.com" ,
37- } ,
38- } ,
39- function ( ) {
40- resolve ( connect ) ;
41- }
42- ) ;
43- } ) ;
44- }
4+ const fs = require ( "fs" ) ;
455
466async function getTTSData (
477 inps : any ,
@@ -51,10 +11,9 @@ async function getTTSData(
5111 rate = 0 ,
5212 pitch = 0
5313) {
54- try {
55- let SSML = "" ;
56- if ( inps . activeIndex == "1" ) {
57- SSML = `
14+ let SSML = "" ;
15+ if ( inps . activeIndex == "1" ) {
16+ SSML = `
5817 <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">
5918 <voice name="${ voice } ">
6019 <mstts:express-as ${
@@ -67,82 +26,12 @@ async function getTTSData(
6726 </voice>
6827 </speak>
6928 ` ;
70- } else {
71- SSML = inps . inputValue ;
72- }
73- ipcRenderer . send ( "log.info" , SSML ) ;
74- console . log ( SSML ) ;
75-
76- console . log ( "获取Token..." ) ;
77- const Authorization = await getAuthToken ( ) ;
78- const XConnectionId = uuidv4 ( ) . toUpperCase ( ) . replaceAll ( "-" , "" ) ;
79-
80- ipcRenderer . send ( "log.info" , "创建webscoket连接..." ) ;
81- // const connect: any = await wssConnect(
82- // `wss://eastus.tts.speech.microsoft.com/cognitiveservices/websocket/v1?Authorization=${Authorization}&X-ConnectionId=${XConnectionId}`
83- // );
84- console . log ( "创建webscoket连接..." ) ;
85- console . log ( "Authorization:" , Authorization ) ;
86- console . log ( "XConnectionId:" , XConnectionId ) ;
87- const connect : any = await wssConnect (
88- `wss://eastus.api.speech.microsoft.com/cognitiveservices/websocket/v1?TrafficType=AzureDemo&Authorization=${ Authorization } &X-ConnectionId=${ XConnectionId } `
89- ) ;
90-
91- ipcRenderer . send ( "log.info" , "第1次上报..." ) ;
92- console . log ( "第1次上报..." ) ;
93- 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)"}}}}` ;
94- await wssSend ( connect , message_1 ) ;
95-
96- ipcRenderer . send ( "log.info" , "第2次上报..." ) ;
97- console . log ( "第2次上报..." ) ;
98- 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"}}}` ;
99- await wssSend ( connect , message_2 ) ;
100-
101- ipcRenderer . send ( "log.info" , "第3次上报..." ) ;
102- console . log ( "第3次上报..." ) ;
103- const message_3 = `Path: ssml\r\nX-RequestId: ${ XConnectionId } \r\nX-Timestamp: ${ getXTime ( ) } \r\nContent-Type: application/ssml+xml\r\n\r\n${ SSML } ` ;
104- await wssSend ( connect , message_3 ) ;
105-
106- return new Promise ( ( resolve , reject ) => {
107- let final_data = Buffer . alloc ( 0 ) ;
108- connect . on ( "text" , ( data : string | string [ ] ) => {
109- if ( data . indexOf ( "Path:turn.end" ) >= 0 ) {
110- ipcRenderer . send ( "log.info" , "已完成" ) ;
111- console . log ( "已完成" ) ;
112- connect . close ( ) ;
113- resolve ( final_data ) ;
114- }
115- } ) ;
116- connect . on (
117- "binary" ,
118- function ( response : {
119- on : ( arg0 : string , arg1 : { ( ) : void ; ( ) : void } ) => void ;
120- read : ( ) => any ;
121- } ) {
122- console . log ( "正在接收数据..." ) ;
123- let data = Buffer . alloc ( 0 ) ;
124- response . on ( "readable" , function ( ) {
125- const newData = response . read ( ) ;
126- if ( newData )
127- data = Buffer . concat (
128- [ data , newData ] ,
129- data . length + newData . length
130- ) ;
131- } ) ;
132- response . on ( "end" , function ( ) {
133- const index = data . toString ( ) . indexOf ( "Path:audio" ) + 10 ;
134- const cmbData = data . slice ( index + 2 ) ;
135- final_data = Buffer . concat ( [ final_data , cmbData ] ) ;
136- } ) ;
137- }
138- ) ;
139- connect . on ( "close" , function ( code : any , reason : any ) { } ) ;
140- } ) ;
141- } catch ( error ) {
142- console . log ( error ) ;
143- return new Promise ( ( resolve , reject ) => {
144- reject ( error ) ;
145- } ) ;
29+ } else {
30+ SSML = inps . inputValue ;
14631 }
32+ ipcRenderer . send ( "log.info" , SSML ) ;
33+ console . log ( SSML ) ;
34+ const result = await ipcRenderer . invoke ( "speech" , SSML ) ;
35+ return result ;
14736}
14837export default getTTSData ;
0 commit comments