Skip to content

Commit

Permalink
update edge-tts
Browse files Browse the repository at this point in the history
  • Loading branch information
SchneeHertz committed Aug 29, 2023
1 parent a4b33fe commit 86609f7
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 30 deletions.
102 changes: 84 additions & 18 deletions modules/edge-tts.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,91 @@
const { spawn } = require('node:child_process')
const { config } = require('../utils/loadConfig.js')
// const { spawn } = require('node:child_process')
const { randomBytes } = require('node:crypto')
const fs = require('node:fs')
const { config: { SpeechSynthesisVoiceName, proxyString} } = require('../utils/loadConfig.js')
const { WebSocket } = require('ws')
const { HttpsProxyAgent } = require('https-proxy-agent')

const ttsPromise = (text, audioPath) => {
let vttPath = audioPath + '.vtt'
return new Promise((resolve, reject) => {
const spawned = spawn('edge-tts', [
'-v', config.SpeechSynthesisVoiceName,
'--text', text,
'--write-media', audioPath,
'--write-subtitles', vttPath,
'--proxy', config.proxyString
])
spawned.on('error', data => {
reject(data)
// const ttsPromise = (text, audioPath) => {
// let vttPath = audioPath + '.vtt'
// return new Promise((resolve, reject) => {
// const spawned = spawn('edge-tts', [
// '-v', SpeechSynthesisVoiceName,
// '--text', text,
// '--write-media', audioPath,
// '--write-subtitles', vttPath,
// '--proxy', proxyString
// ])
// spawned.on('error', data => {
// reject(data)
// })
// spawned.on('exit', code => {
// if (code === 0) {
// return resolve(vttPath)
// }
// return reject('edge-tts close code is ' + code)
// })
// })
// }

let wsConnect = {}
const connectWebSocket = async () => {
const wsConnect = new WebSocket(`wss://speech.platform.bing.com/consumer/speech/synthesize/readaloud/edge/v1?TrustedClientToken=6A5AA1D4EAFF4E9FB37E23D68491D6F4`, {
host: 'speech.platform.bing.com',
origin: 'chrome-extension://jdiccldimpdaibmpdkjnbmckianbfold',
headers: {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.66 Safari/537.36 Edg/103.0.1264.44',
},
agent: new HttpsProxyAgent(proxyString)
})
await new Promise((resolve, reject) => {
wsConnect.on('open', () => {
wsConnect.send(`Content-Type:application/json; charset=utf-8\r\nPath:speech.config\r\n\r\n
{
"context": {
"synthesis": {
"audio": {
"metadataoptions": {
"sentenceBoundaryEnabled": "false",
"wordBoundaryEnabled": "false"
},
"outputFormat": "audio-24khz-96kbitrate-mono-mp3"
}
}
}
}
`)
resolve()
})
spawned.on('exit', code => {
if (code === 0) {
return resolve(vttPath)
})
return wsConnect
}

const ttsPromise = async (text, audioPath) => {
if (wsConnect.readyState !== 1) {
wsConnect = await connectWebSocket()
}
return await new Promise((resolve, reject) => {
let requestId = randomBytes(16).toString('hex')
let queue = fs.createWriteStream(audioPath)
wsConnect.on('message', async (message, isBinary) => {
if (isBinary) {
const separator = 'Path:audio\r\n'
const index = message.indexOf(separator) + separator.length
const audioData = message.slice(index, message.length)
queue.write(audioData)
} else {
if (message.toString().includes('Path:turn.end')) {
queue.end()
resolve()
}
}
return reject('edge-tts close code is ' + code)
})
wsConnect.send(`X-RequestId:${requestId}\r\nContent-Type:application/ssml+xml\r\nPath:ssml\r\n\r\n
` + `<speak version="1.0" xmlns="http://www.w3.org/2001/10/synthesis" xmlns:mstts="https://www.w3.org/2001/mstts" xml:lang="zh-CN">
<voice name="${SpeechSynthesisVoiceName}">
${text}
</voice>
</speak>`)
})
}

Expand Down
43 changes: 35 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"name": "chat-xiuliu",
"private": true,
"version": "2.0.2",
"version": "2.0.3",
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview",
"start": "chcp 65001 && electron .",
"start": "chcp 65001 && electron --trace-warnings .",
"pack": "electron-builder --dir",
"dist": "electron-builder"
},
Expand Down Expand Up @@ -57,9 +57,10 @@
"js-tiktoken": "^1.0.7",
"lodash": "^4.17.21",
"nanoid": "^3.3.6",
"openai": "^4.0.0",
"openai": "^4.3.0",
"quickjs-emscripten": "^0.23.0",
"sound-play": "^1.1.0",
"vectordb": "^0.2.2"
"vectordb": "^0.2.2",
"ws": "^8.13.0"
}
}

0 comments on commit 86609f7

Please sign in to comment.