Skip to content

Commit

Permalink
use node-edge-tts
Browse files Browse the repository at this point in the history
  • Loading branch information
SchneeHertz committed Aug 31, 2023
1 parent f5a4c26 commit 1413ef6
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 12 deletions.
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const lancedb = require('vectordb')
const { STORE_PATH, LOG_PATH, AUDIO_PATH } = require('./utils/initFile.js')
const { getStore, setStore } = require('./modules/store.js')
const { getSpeechText } = require('./modules/whisper.js')
const { EdgeTTS } = require('./modules/edge-tts.js')
const { EdgeTTS } = require('node-edge-tts')
const { openaiChatStream, openaiEmbedding, azureOpenaiChatStream, azureOpenaiEmbedding } = require('./modules/common.js')
const { functionAction, functionInfo, functionList } = require('./modules/functions.js')
const { config: {
Expand Down Expand Up @@ -60,7 +60,8 @@ let tts = new EdgeTTS({
voice: SpeechSynthesisVoiceName,
lang: 'zh-CN',
outputFormat: 'audio-24khz-96kbitrate-mono-mp3',
proxy: proxyString
proxy: proxyString,
saveSubtitles: true
})

let mainWindow
Expand Down
27 changes: 17 additions & 10 deletions modules/edge-tts.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
const { randomBytes } = require('node:crypto')
const fs = require('node:fs')
const { writeFileSync, createWriteStream } = require('node:fs')
const { WebSocket } = require('ws')
const { HttpsProxyAgent } = require('https-proxy-agent')


class EdgeTTS {

voice
lang
outputFormat
proxy
saveSubtitles

constructor ({
voice = 'zh-CN-XiaoyiNeural',
lang = 'zh-CN',
outputFormat = 'audio-24khz-48kbitrate-mono-mp3',
saveSubtitles = false,
proxy
}) {
this.voice = voice
this.lang = lang
this.outputFormat = outputFormat
this.saveSubtitles = saveSubtitles
this.proxy = proxy
}

Expand All @@ -37,11 +42,11 @@ class EdgeTTS {
"context": {
"synthesis": {
"audio": {
"metadataoptions": {
"sentenceBoundaryEnabled": "false",
"wordBoundaryEnabled": "true"
},
"outputFormat": "${this.outputFormat}"
"metadataoptions": {
"sentenceBoundaryEnabled": "false",
"wordBoundaryEnabled": "true"
},
"outputFormat": "${this.outputFormat}"
}
}
}
Expand Down Expand Up @@ -73,13 +78,13 @@ class EdgeTTS {
}
cue.part = fullPart
})
fs.writeFileSync(subPath, JSON.stringify(subFile, null, ' '), { encoding: 'utf-8' })
writeFileSync(subPath, JSON.stringify(subFile, null, ' '), { encoding: 'utf-8' })
}

async ttsPromise (text, audioPath) {
const _wsConnect = await this._connectWebSocket()
return await new Promise((resolve, reject) => {
let audioStream = fs.createWriteStream(audioPath)
let audioStream = createWriteStream(audioPath)
let subFile = []
_wsConnect.on('message', async (data, isBinary) => {
if (isBinary) {
Expand All @@ -91,7 +96,9 @@ class EdgeTTS {
let message = data.toString()
if (message.includes('Path:turn.end')) {
audioStream.end()
this._saveSubFile(subFile, text, audioPath)
if (this.saveSubtitles) {
this._saveSubFile(subFile, text, audioPath)
}
resolve()
} else if (message.includes('Path:audio.metadata')) {
let splitTexts = message.split('\r\n')
Expand All @@ -108,7 +115,7 @@ class EdgeTTS {
_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="${this.lang}">
<voice name="${this.voice}">
${text}
${text}
</voice>
</speak>`)
})
Expand Down
19 changes: 19 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"js-tiktoken": "^1.0.7",
"lodash": "^4.17.21",
"nanoid": "^3.3.6",
"node-edge-tts": "^1.1.3",
"openai": "^4.3.0",
"quickjs-emscripten": "^0.23.0",
"sound-play": "^1.1.0",
Expand Down

0 comments on commit 1413ef6

Please sign in to comment.