dsh-laivox-tts 0.7.25 → 0.7.26

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.en.md CHANGED
@@ -38,7 +38,7 @@ This installs the prebuilt package from npm—nothing is compiled locally and no
38
38
 
39
39
  ### Install from GitHub Release or offline package
40
40
 
41
- If npm is unavailable or you prefer pinning a release tarball, install via URL or download `dsh-laivox-tts.tgz` from [Releases](https://github.com/xiaolai181/dsh_laivox_tts/releases) (replace `latest/download` with `download/v0.7.25` to pin a version):
41
+ If npm is unavailable or you prefer pinning a release tarball, install via URL or download `dsh-laivox-tts.tgz` from [Releases](https://github.com/xiaolai181/dsh_laivox_tts/releases) (replace `latest/download` with `download/v0.7.26` to pin a version):
42
42
 
43
43
  ```sh
44
44
  dsh plugin --profile desktop add "https://github.com/xiaolai181/dsh_laivox_tts/releases/latest/download/dsh-laivox-tts.tgz"
package/README.md CHANGED
@@ -40,7 +40,7 @@ dsh plugin --profile desktop add dsh-laivox-tts
40
40
 
41
41
  ### GitHub Release 链接或离线安装包安装
42
42
 
43
- 如需锁定版本或无法直连 npm,可使用 Release 下载链接(将 `latest/download` 替换为 `download/v0.7.25` 可锁定版本),或从 [Releases](https://github.com/xiaolai181/dsh_laivox_tts/releases) 下载 `dsh-laivox-tts.tgz` 到本地后安装:
43
+ 如需锁定版本或无法直连 npm,可使用 Release 下载链接(将 `latest/download` 替换为 `download/v0.7.26` 可锁定版本),或从 [Releases](https://github.com/xiaolai181/dsh_laivox_tts/releases) 下载 `dsh-laivox-tts.tgz` 到本地后安装:
44
44
 
45
45
  ```sh
46
46
  dsh plugin --profile desktop add "https://github.com/xiaolai181/dsh_laivox_tts/releases/latest/download/dsh-laivox-tts.tgz"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-laivox-tts",
3
- "version": "0.7.25",
3
+ "version": "0.7.26",
4
4
  "private": false,
5
5
  "description": "把 TXT、EPUB、PDF 小说做成单人朗读或多角色有声书,也能一键朗读对话里的 AI 回答,支持本地书架与 MP3/WAV 导出。",
6
6
  "license": "MIT",
package/src/host/edge.mjs CHANGED
@@ -1,170 +1,162 @@
1
- import { spawn } from 'node:child_process'
2
- import { setTimeout as delay } from 'node:timers/promises'
3
- import { fileURLToPath } from 'node:url'
4
- import { hasSpeakable } from '../domain/segment.mjs'
5
-
6
- // Order matters: chooseVoices hands out the first unused voice of a matching
7
- // gender, so new voices are only ever appended. Appending leaves every role that
8
- // already had its own voice untouched. auto: false voices (regional accents)
9
- // are offered in the picker but never assigned automatically.
10
- // Added after 10/10 real synthesis runs each (5 simplified, 5
11
- // traditional); zh-TW-YunJheNeural failed 4/10 with truncated streams and is excluded.
12
- // Later voices are appended. Chinese automatic assignment still walks the
13
- // original zh-CN / zh-TW entries first, so roles that already had a voice keep it.
14
- export const edgeVoices = [
15
- { id: 'zh-CN-XiaoxiaoNeural', name: '晓晓', gender: 'female', age: 'adult', auto: true },
16
- { id: 'zh-CN-YunxiNeural', name: '云希', gender: 'male', age: 'adult', auto: true },
17
- { id: 'zh-CN-YunjianNeural', name: '云健', gender: 'male', age: 'adult', auto: true },
18
- { id: 'zh-CN-XiaoyiNeural', name: '晓伊', gender: 'female', age: 'adult', auto: true },
19
- { id: 'zh-TW-HsiaoChenNeural', name: '曉臻', gender: 'female', age: 'adult', auto: true },
20
- { id: 'zh-TW-HsiaoYuNeural', name: '曉雨', gender: 'female', age: 'adult', auto: true },
21
- { id: 'zh-CN-YunyangNeural', name: '云扬', gender: 'male', age: 'adult', auto: true },
22
- { id: 'zh-CN-YunxiaNeural', name: '云夏', gender: 'male', age: 'child', auto: true },
23
- { id: 'zh-CN-XiaohanNeural', name: '晓涵', gender: 'female', age: 'adult', auto: true },
24
- { id: 'zh-CN-XiaoxuanNeural', name: '晓萱', gender: 'female', age: 'adult', auto: true },
25
- { id: 'zh-CN-XiaoruiNeural', name: '晓睿', gender: 'female', age: 'adult', auto: true },
26
- { id: 'zh-CN-XiaoshuangNeural', name: '晓双', gender: 'female', age: 'child', auto: true },
27
- { id: 'zh-CN-YunfengNeural', name: '云枫', gender: 'male', age: 'adult', auto: true },
28
- { id: 'zh-CN-YunhaoNeural', name: '云皓', gender: 'male', age: 'adult', auto: true },
29
- { id: 'zh-CN-YunyeNeural', name: '云野', gender: 'male', age: 'adult', auto: true },
30
- { id: 'zh-CN-YunzeNeural', name: '云泽', gender: 'male', age: 'adult', auto: true },
31
- { id: 'en-US-AriaNeural', name: 'Aria(美式英语)', gender: 'female', age: 'adult', auto: true },
32
- { id: 'en-US-JennyNeural', name: 'Jenny(美式英语)', gender: 'female', age: 'adult', auto: true },
33
- { id: 'en-US-GuyNeural', name: 'Guy(美式英语)', gender: 'male', age: 'adult', auto: true },
34
- { id: 'en-US-DavisNeural', name: 'Davis(美式英语)', gender: 'male', age: 'adult', auto: true },
35
- { id: 'en-GB-SoniaNeural', name: 'Sonia(英式英语)', gender: 'female', age: 'adult', auto: true },
36
- { id: 'en-GB-RyanNeural', name: 'Ryan(英式英语)', gender: 'male', age: 'adult', auto: true },
37
- { id: 'en-US-AnaNeural', name: 'Ana(美式英语)', gender: 'female', age: 'child', auto: true },
38
- { id: 'zh-CN-liaoning-XiaobeiNeural', name: '晓北(东北口音)', gender: 'female', age: 'adult', auto: false },
39
- { id: 'zh-CN-shaanxi-XiaoniNeural', name: '晓妮(陕西口音)', gender: 'female', age: 'adult', auto: false },
40
- ]
41
-
42
- const workerFile = fileURLToPath(new URL('./edge-worker.mjs', import.meta.url))
43
- const DETAIL_LIMIT = 600
44
- const STDERR_LIMIT = 8 * 1024
45
-
46
- // Pauses between attempts. Three attempts in total ride out short network
47
- // blips before the job pauses with the last error.
48
- export const EDGE_RETRY_WAITS = [800, 2500]
49
-
50
- export async function retryEdge(once, signal, waits = EDGE_RETRY_WAITS, wait = delay) {
51
- for (let attempt = 0; ; attempt++) {
52
- signal.throwIfAborted()
53
- try {
54
- return await once()
55
- } catch (error) {
56
- signal.throwIfAborted()
57
- if (attempt >= waits.length) throw error
58
- await wait(waits[attempt], undefined, { signal })
59
- }
60
- }
61
- }
62
-
63
- export async function synthesizeEdge(input, signal, options = {}) {
64
- if (!edgeVoices.some(voice => voice.id === input.voiceId)) throw new Error('EDGE_INVALID_VOICE')
65
- if (![...input.text].length || [...input.text].length > 500) throw new Error('EDGE_INVALID_TEXT')
66
- return retryEdge(() => synthesizeOnce(input, signal, options), signal, edgeRetryWaits(input.text))
67
- }
68
-
69
- // Text with nothing to pronounce comes back empty on every attempt, so a second
70
- // try only costs another connection and another few seconds of the job.
71
- export function edgeRetryWaits(text) {
72
- return hasSpeakable(text) ? EDGE_RETRY_WAITS : []
73
- }
74
-
75
- const strip = value => String(value ?? '').replace(/\u001b\[[0-9;]*m/gu, '').replace(/\r/gu, '').trim()
76
-
77
- // Turn the child's stderr into one short line. The worker writes a JSON error
78
- // line for expected msedge-tts failures; crashes fall back to the last lines.
79
- // The reported code lets the parent pick a concrete error instead of always
80
- // calling it an outage.
81
- function edgeFailure(stderr) {
82
- const raw = strip(stderr)
83
- if (!raw) return { detail: '', code: null }
84
- const lines = raw.split('\n').map(line => line.trim()).filter(Boolean)
85
- for (let index = lines.length - 1; index >= 0; index--) {
86
- if (!lines[index].startsWith('{')) continue
87
- try {
88
- const value = JSON.parse(lines[index])
89
- if (value && typeof value.message === 'string' && value.message) {
90
- const name = typeof value.error === 'string' && value.error ? value.error + ': ' : ''
91
- const code = value.code ? ' (' + value.code + ')' : ''
92
- return {
93
- detail: (name + value.message + code).slice(0, DETAIL_LIMIT),
94
- code: typeof value.code === 'string' && value.code ? value.code : null,
95
- }
96
- }
97
- } catch { /* not the structured line */ }
98
- }
99
- return { detail: lines.slice(-4).join(' | ').slice(0, DETAIL_LIMIT), code: null }
100
- }
101
-
102
- export function edgeFailureDetail(stderr) {
103
- return edgeFailure(stderr).detail
104
- }
105
-
106
- const TIMING_LIMIT = 512 * 1024
107
-
108
- // Words as { o: start ms, d: duration ms, t: text } in speaking order; anything unreadable yields none.
109
- export function parseTiming(raw) {
110
- const line = String(raw ?? '').trim().split('\n').at(-1)
111
- if (!line) return []
112
- try {
113
- const words = JSON.parse(line)
114
- return Array.isArray(words) ? words.filter(word => Number.isFinite(word?.o) && Number.isFinite(word?.d) && typeof word?.t === 'string' && word.t) : []
115
- } catch { return [] }
116
- }
117
-
118
- export function synthesizeOnce(input, signal, { timeout = 23_000, command = process.execPath, args = [workerFile] } = {}) {
119
- return new Promise((resolve, reject) => {
120
- const child = spawn(command, args, {
121
- // fd 3 carries word timing from the worker (one JSON line).
122
- windowsHide: true, stdio: ['pipe', 'pipe', 'pipe', 'pipe'],
123
- env: { ...process.env, NODE_OPTIONS: '', ELECTRON_RUN_AS_NODE: '1' },
124
- })
125
- const chunks = []
126
- let size = 0
127
- let stderr = ''
128
- let timing = ''
129
- let failure
130
- function terminate(error) {
131
- failure ??= error
132
- child.kill()
133
- }
134
- const abort = () => terminate(signal.reason)
135
- const timer = setTimeout(() => terminate(new Error('EDGE_TIMEOUT')), timeout)
136
- signal.addEventListener('abort', abort, { once: true })
137
- if (signal.aborted) abort()
138
- child.stdout.on('data', chunk => {
139
- size += chunk.length
140
- if (size <= 8 * 1024 * 1024) chunks.push(chunk)
141
- else terminate(new Error('EDGE_AUDIO_TOO_LARGE'))
142
- })
143
- child.stderr.on('data', chunk => {
144
- if (stderr.length < STDERR_LIMIT) stderr += chunk.toString('utf8')
145
- })
146
- child.stdio[3]?.on('data', chunk => { if (timing.length < TIMING_LIMIT) timing += chunk.toString('utf8') })
147
- child.stdio[3]?.on('error', () => {})
148
- child.stdin.on('error', () => {})
149
- child.on('error', error => {
150
- failure ??= Object.assign(new Error('EDGE_WORKER_START_FAILED'), { detail: strip(error?.message ?? error) || null })
151
- })
152
- child.on('close', code => {
153
- clearTimeout(timer)
154
- signal.removeEventListener('abort', abort)
155
- if (!failure && code === 0 && size > 0) {
156
- // The audio stays a Buffer for every caller; timing rides along as a property.
157
- const audio = Buffer.concat(chunks)
158
- audio.marks = parseTiming(timing)
159
- resolve(audio)
160
- return
161
- }
162
- const { detail, code: reported } = edgeFailure(stderr)
163
- // An empty turn is its own failure: it is a content result, not an outage.
164
- const error = failure ?? (reported === 'EDGE_EMPTY_AUDIO' ? new Error('EDGE_EMPTY_AUDIO') : new Error('EDGE_UNAVAILABLE'))
165
- if (detail && !error.detail) error.detail = detail
166
- reject(error)
167
- })
168
- child.stdin.end(JSON.stringify(input))
169
- })
170
- }
1
+ import { spawn } from 'node:child_process'
2
+ import { setTimeout as delay } from 'node:timers/promises'
3
+ import { fileURLToPath } from 'node:url'
4
+ import { hasSpeakable } from '../domain/segment.mjs'
5
+
6
+ // Order matters: chooseVoices hands out the first unused voice of a matching
7
+ // gender, so new voices are only ever appended. Appending leaves every role that
8
+ // already had its own voice untouched. auto: false voices (regional accents)
9
+ // are offered in the picker but never assigned automatically.
10
+ // Use only IDs listed by the Edge Read Aloud voice directory.
11
+ // Added after 10/10 real synthesis runs each (5 simplified, 5
12
+ // traditional); zh-TW-YunJheNeural failed 4/10 with truncated streams and is excluded.
13
+ // Later voices are appended. Chinese automatic assignment still walks the
14
+ // original zh-CN / zh-TW entries first, so roles that already had a voice keep it.
15
+ export const edgeVoices = [
16
+ { id: 'zh-CN-XiaoxiaoNeural', name: '晓晓', gender: 'female', age: 'adult', auto: true },
17
+ { id: 'zh-CN-YunxiNeural', name: '云希', gender: 'male', age: 'adult', auto: true },
18
+ { id: 'zh-CN-YunjianNeural', name: '云健', gender: 'male', age: 'adult', auto: true },
19
+ { id: 'zh-CN-XiaoyiNeural', name: '晓伊', gender: 'female', age: 'adult', auto: true },
20
+ { id: 'zh-TW-HsiaoChenNeural', name: '曉臻', gender: 'female', age: 'adult', auto: true },
21
+ { id: 'zh-TW-HsiaoYuNeural', name: '曉雨', gender: 'female', age: 'adult', auto: true },
22
+ { id: 'zh-CN-YunyangNeural', name: '云扬', gender: 'male', age: 'adult', auto: true },
23
+ { id: 'zh-CN-YunxiaNeural', name: '云夏', gender: 'male', age: 'child', auto: true },
24
+ { id: 'en-US-AriaNeural', name: 'Aria(美式英语)', gender: 'female', age: 'adult', auto: true },
25
+ { id: 'en-US-JennyNeural', name: 'Jenny(美式英语)', gender: 'female', age: 'adult', auto: true },
26
+ { id: 'en-US-GuyNeural', name: 'Guy(美式英语)', gender: 'male', age: 'adult', auto: true },
27
+ { id: 'en-GB-SoniaNeural', name: 'Sonia(英式英语)', gender: 'female', age: 'adult', auto: true },
28
+ { id: 'en-GB-RyanNeural', name: 'Ryan(英式英语)', gender: 'male', age: 'adult', auto: true },
29
+ { id: 'en-US-AnaNeural', name: 'Ana(美式英语)', gender: 'female', age: 'child', auto: true },
30
+ { id: 'zh-CN-liaoning-XiaobeiNeural', name: '晓北(东北口音)', gender: 'female', age: 'adult', auto: false },
31
+ { id: 'zh-CN-shaanxi-XiaoniNeural', name: '晓妮(陕西口音)', gender: 'female', age: 'adult', auto: false },
32
+ ]
33
+
34
+ const workerFile = fileURLToPath(new URL('./edge-worker.mjs', import.meta.url))
35
+ const DETAIL_LIMIT = 600
36
+ const STDERR_LIMIT = 8 * 1024
37
+
38
+ // Pauses between attempts. Three attempts in total ride out short network
39
+ // blips before the job pauses with the last error.
40
+ export const EDGE_RETRY_WAITS = [800, 2500]
41
+
42
+ export async function retryEdge(once, signal, waits = EDGE_RETRY_WAITS, wait = delay) {
43
+ for (let attempt = 0; ; attempt++) {
44
+ signal.throwIfAborted()
45
+ try {
46
+ return await once()
47
+ } catch (error) {
48
+ signal.throwIfAborted()
49
+ if (attempt >= waits.length) throw error
50
+ await wait(waits[attempt], undefined, { signal })
51
+ }
52
+ }
53
+ }
54
+
55
+ export async function synthesizeEdge(input, signal, options = {}) {
56
+ if (!edgeVoices.some(voice => voice.id === input.voiceId)) throw new Error('EDGE_INVALID_VOICE')
57
+ if (![...input.text].length || [...input.text].length > 500) throw new Error('EDGE_INVALID_TEXT')
58
+ return retryEdge(() => synthesizeOnce(input, signal, options), signal, edgeRetryWaits(input.text))
59
+ }
60
+
61
+ // Text with nothing to pronounce comes back empty on every attempt, so a second
62
+ // try only costs another connection and another few seconds of the job.
63
+ export function edgeRetryWaits(text) {
64
+ return hasSpeakable(text) ? EDGE_RETRY_WAITS : []
65
+ }
66
+
67
+ const strip = value => String(value ?? '').replace(/\u001b\[[0-9;]*m/gu, '').replace(/\r/gu, '').trim()
68
+
69
+ // Turn the child's stderr into one short line. The worker writes a JSON error
70
+ // line for expected msedge-tts failures; crashes fall back to the last lines.
71
+ // The reported code lets the parent pick a concrete error instead of always
72
+ // calling it an outage.
73
+ function edgeFailure(stderr) {
74
+ const raw = strip(stderr)
75
+ if (!raw) return { detail: '', code: null }
76
+ const lines = raw.split('\n').map(line => line.trim()).filter(Boolean)
77
+ for (let index = lines.length - 1; index >= 0; index--) {
78
+ if (!lines[index].startsWith('{')) continue
79
+ try {
80
+ const value = JSON.parse(lines[index])
81
+ if (value && typeof value.message === 'string' && value.message) {
82
+ const name = typeof value.error === 'string' && value.error ? value.error + ': ' : ''
83
+ const code = value.code ? ' (' + value.code + ')' : ''
84
+ return {
85
+ detail: (name + value.message + code).slice(0, DETAIL_LIMIT),
86
+ code: typeof value.code === 'string' && value.code ? value.code : null,
87
+ }
88
+ }
89
+ } catch { /* not the structured line */ }
90
+ }
91
+ return { detail: lines.slice(-4).join(' | ').slice(0, DETAIL_LIMIT), code: null }
92
+ }
93
+
94
+ export function edgeFailureDetail(stderr) {
95
+ return edgeFailure(stderr).detail
96
+ }
97
+
98
+ const TIMING_LIMIT = 512 * 1024
99
+
100
+ // Words as { o: start ms, d: duration ms, t: text } in speaking order; anything unreadable yields none.
101
+ export function parseTiming(raw) {
102
+ const line = String(raw ?? '').trim().split('\n').at(-1)
103
+ if (!line) return []
104
+ try {
105
+ const words = JSON.parse(line)
106
+ return Array.isArray(words) ? words.filter(word => Number.isFinite(word?.o) && Number.isFinite(word?.d) && typeof word?.t === 'string' && word.t) : []
107
+ } catch { return [] }
108
+ }
109
+
110
+ export function synthesizeOnce(input, signal, { timeout = 23_000, command = process.execPath, args = [workerFile] } = {}) {
111
+ return new Promise((resolve, reject) => {
112
+ const child = spawn(command, args, {
113
+ // fd 3 carries word timing from the worker (one JSON line).
114
+ windowsHide: true, stdio: ['pipe', 'pipe', 'pipe', 'pipe'],
115
+ env: { ...process.env, NODE_OPTIONS: '', ELECTRON_RUN_AS_NODE: '1' },
116
+ })
117
+ const chunks = []
118
+ let size = 0
119
+ let stderr = ''
120
+ let timing = ''
121
+ let failure
122
+ function terminate(error) {
123
+ failure ??= error
124
+ child.kill()
125
+ }
126
+ const abort = () => terminate(signal.reason)
127
+ const timer = setTimeout(() => terminate(new Error('EDGE_TIMEOUT')), timeout)
128
+ signal.addEventListener('abort', abort, { once: true })
129
+ if (signal.aborted) abort()
130
+ child.stdout.on('data', chunk => {
131
+ size += chunk.length
132
+ if (size <= 8 * 1024 * 1024) chunks.push(chunk)
133
+ else terminate(new Error('EDGE_AUDIO_TOO_LARGE'))
134
+ })
135
+ child.stderr.on('data', chunk => {
136
+ if (stderr.length < STDERR_LIMIT) stderr += chunk.toString('utf8')
137
+ })
138
+ child.stdio[3]?.on('data', chunk => { if (timing.length < TIMING_LIMIT) timing += chunk.toString('utf8') })
139
+ child.stdio[3]?.on('error', () => {})
140
+ child.stdin.on('error', () => {})
141
+ child.on('error', error => {
142
+ failure ??= Object.assign(new Error('EDGE_WORKER_START_FAILED'), { detail: strip(error?.message ?? error) || null })
143
+ })
144
+ child.on('close', code => {
145
+ clearTimeout(timer)
146
+ signal.removeEventListener('abort', abort)
147
+ if (!failure && code === 0 && size > 0) {
148
+ // The audio stays a Buffer for every caller; timing rides along as a property.
149
+ const audio = Buffer.concat(chunks)
150
+ audio.marks = parseTiming(timing)
151
+ resolve(audio)
152
+ return
153
+ }
154
+ const { detail, code: reported } = edgeFailure(stderr)
155
+ // An empty turn is its own failure: it is a content result, not an outage.
156
+ const error = failure ?? (reported === 'EDGE_EMPTY_AUDIO' ? new Error('EDGE_EMPTY_AUDIO') : new Error('EDGE_UNAVAILABLE'))
157
+ if (detail && !error.detail) error.detail = detail
158
+ reject(error)
159
+ })
160
+ child.stdin.end(JSON.stringify(input))
161
+ })
162
+ }