dsh-voice 0.6.1 → 0.8.0
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/CHANGELOG.md +18 -0
- package/README.en.md +26 -15
- package/README.md +26 -15
- package/docs/USAGE.en.md +84 -40
- package/docs/USAGE.md +84 -40
- package/lib/config.d.ts +44 -3
- package/lib/config.js +138 -9
- package/lib/edge-tts.d.ts +44 -0
- package/lib/edge-tts.js +305 -37
- package/lib/host-config.d.ts +1 -1
- package/lib/host-config.js +49 -20
- package/lib/index.d.ts +2 -0
- package/lib/index.js +2 -0
- package/lib/openai-tts.d.ts +29 -0
- package/lib/openai-tts.js +149 -0
- package/lib/paths.d.ts +4 -0
- package/lib/paths.js +21 -11
- package/lib/proxy-fetch.d.ts +6 -0
- package/lib/proxy-fetch.js +344 -4
- package/lib/stt.d.ts +43 -9
- package/lib/stt.js +164 -14
- package/lib/tools.d.ts +9 -2
- package/lib/tools.js +855 -81
- package/lib/tts-text.d.ts +48 -0
- package/lib/tts-text.js +368 -0
- package/lib/update-client.js +1 -1
- package/lib/voices.d.ts +24 -2
- package/lib/voices.js +179 -25
- package/locale/en.json +1 -1
- package/locale/zh.json +1 -1
- package/package.json +4 -3
- package/skills/voice-workflow/SKILL.md +96 -0
package/lib/edge-tts.js
CHANGED
|
@@ -6,11 +6,14 @@
|
|
|
6
6
|
* @module dsh-voice/edge-tts
|
|
7
7
|
*/
|
|
8
8
|
import { randomBytes, randomUUID } from 'node:crypto';
|
|
9
|
-
import { edgeTokenV2 } from './edge-drm-v2.js';
|
|
10
|
-
import { HttpsProxyAgent } from 'https-proxy-agent';
|
|
11
9
|
import WebSocket from 'ws';
|
|
10
|
+
import { edgeTokenV2 } from './edge-drm-v2.js';
|
|
11
|
+
import { getProxyWsAgent } from './proxy-fetch.js';
|
|
12
|
+
import { VOICES } from './voices.js';
|
|
12
13
|
const TRUSTED_CLIENT_TOKEN = '6A5AA1D4EAFF4E9FB37E23D68491D6F4';
|
|
13
14
|
const WS_BASE = 'wss://speech.platform.bing.com/consumer/speech/synthesize/readaloud/edge/v1';
|
|
15
|
+
const VOICES_LIST_URL = 'https://speech.platform.bing.com/consumer/speech/synthesize/readaloud/voices/list';
|
|
16
|
+
const EDGE_VOICES_CACHE_TTL_MS = 24 * 60 * 60 * 1000;
|
|
14
17
|
const OUTPUT_FORMAT = 'audio-24khz-48kbitrate-mono-mp3';
|
|
15
18
|
const SEC_MS_GEC_VERSION = '2-143.0.3650.75';
|
|
16
19
|
const WIN_EPOCH_SECONDS = 11644473600;
|
|
@@ -23,14 +26,30 @@ const WSS_HEADERS = {
|
|
|
23
26
|
'Sec-WebSocket-Version': '13',
|
|
24
27
|
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36 Edg/143.0.0.0',
|
|
25
28
|
};
|
|
29
|
+
/** 403 响应校准出的服务端时钟偏差(秒):serverSeconds - Date.now() / 1000。 */
|
|
30
|
+
let clockSkewSeconds = 0;
|
|
31
|
+
/** 重置时钟偏差缓存(测试专用)。 */
|
|
32
|
+
export function resetClockSkewForTest() {
|
|
33
|
+
clockSkewSeconds = 0;
|
|
34
|
+
}
|
|
35
|
+
/** 读取当前时钟偏差(秒)。 */
|
|
36
|
+
export function getClockSkewSeconds() {
|
|
37
|
+
return clockSkewSeconds;
|
|
38
|
+
}
|
|
39
|
+
/** 内部哨兵错误:收到 403 且已根据服务端 Date 头校准时钟后触发单次重试。 */
|
|
40
|
+
class ClockSkewRetrySignal extends Error {
|
|
41
|
+
constructor() {
|
|
42
|
+
super('clock-skew-retry');
|
|
43
|
+
}
|
|
44
|
+
}
|
|
26
45
|
/** 直连工厂(ws 库,Node 全版本可用)。 */
|
|
27
46
|
function directWebSocket(url, options) {
|
|
28
47
|
return new WebSocket(url, { headers: options.headers });
|
|
29
48
|
}
|
|
30
|
-
/**
|
|
49
|
+
/** 代理工厂:复用 proxy-fetch 缓存的 HTTP(S) / SOCKS5 Agent。 */
|
|
31
50
|
function proxyWebSocket(proxyUrl) {
|
|
32
|
-
const agent =
|
|
33
|
-
return (url, options) => new WebSocket(url, { headers: options.headers, agent });
|
|
51
|
+
const agent = getProxyWsAgent(proxyUrl);
|
|
52
|
+
return (url, options) => new WebSocket(url, { headers: options.headers, agent: agent });
|
|
34
53
|
}
|
|
35
54
|
/**
|
|
36
55
|
* 本地生成 Sec-MS-GEC v2 令牌(对齐 edge-tts 7.3.1)。
|
|
@@ -42,14 +61,37 @@ export function generateSecMsGec(nowSeconds = Date.now() / 1000) {
|
|
|
42
61
|
const raw = String(windowsTicks) + TRUSTED_CLIENT_TOKEN;
|
|
43
62
|
return edgeTokenV2(raw);
|
|
44
63
|
}
|
|
45
|
-
/** XML
|
|
64
|
+
/** 去除 XML 1.0 非法控制字符(保留 \t \n \r)。 */
|
|
65
|
+
function stripIllegalXmlChars(text) {
|
|
66
|
+
return text.replace(/[\x00-\x08\x0B\x0C\x0E-\x1F]/g, '');
|
|
67
|
+
}
|
|
68
|
+
/** XML 文本转义。 */
|
|
46
69
|
function escapeXml(text) {
|
|
47
|
-
return text
|
|
70
|
+
return stripIllegalXmlChars(text)
|
|
71
|
+
.replace(/&/g, '&')
|
|
72
|
+
.replace(/</g, '<')
|
|
73
|
+
.replace(/>/g, '>');
|
|
74
|
+
}
|
|
75
|
+
/** XML 属性转义(同时转义单双引号)。 */
|
|
76
|
+
function escapeXmlAttr(value) {
|
|
77
|
+
return escapeXml(value)
|
|
78
|
+
.replace(/'/g, ''')
|
|
79
|
+
.replace(/"/g, '"');
|
|
48
80
|
}
|
|
49
81
|
/** 生成 SSML。 */
|
|
50
82
|
export function buildSsml(options) {
|
|
51
|
-
const lang = /^[a-z]{2,3}(-[A-Z]{2})?/.exec(options.voice)?.[0] ?? 'zh-CN';
|
|
52
|
-
return "<speak version='1.0' xmlns='http://www.w3.org/2001/10/synthesis' xml:lang='"
|
|
83
|
+
const lang = /^[a-z]{2,3}(?:-[A-Za-z]{2,4})?(?:-[A-Z]{2})?/.exec(options.voice)?.[0] ?? 'zh-CN';
|
|
84
|
+
return "<speak version='1.0' xmlns='http://www.w3.org/2001/10/synthesis' xml:lang='"
|
|
85
|
+
+ escapeXmlAttr(lang)
|
|
86
|
+
+ "'><voice name='"
|
|
87
|
+
+ escapeXmlAttr(options.voice)
|
|
88
|
+
+ "'><prosody pitch='"
|
|
89
|
+
+ escapeXmlAttr(options.pitch)
|
|
90
|
+
+ "' rate='"
|
|
91
|
+
+ escapeXmlAttr(options.rate)
|
|
92
|
+
+ "' volume='+0%'>"
|
|
93
|
+
+ escapeXml(options.text)
|
|
94
|
+
+ '</prosody></voice></speak>';
|
|
53
95
|
}
|
|
54
96
|
/** 生成带时间戳与路径的协议消息头。 */
|
|
55
97
|
function protocolHeader(path, extra) {
|
|
@@ -59,26 +101,43 @@ function protocolHeader(path, extra) {
|
|
|
59
101
|
lines.push('Path:' + path);
|
|
60
102
|
return lines.join('\r\n') + '\r\n\r\n';
|
|
61
103
|
}
|
|
62
|
-
/**
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
const
|
|
68
|
-
if (
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
104
|
+
/** 从 metadata JSON 文本帧中提取句/词边界字幕。 */
|
|
105
|
+
function parseMetadataCues(data, cues) {
|
|
106
|
+
const sepIndex = data.indexOf('\r\n\r\n');
|
|
107
|
+
if (sepIndex === -1)
|
|
108
|
+
return;
|
|
109
|
+
const body = data.slice(sepIndex + 4).trim();
|
|
110
|
+
if (body === '')
|
|
111
|
+
return;
|
|
112
|
+
let parsed;
|
|
113
|
+
try {
|
|
114
|
+
parsed = JSON.parse(body);
|
|
115
|
+
}
|
|
116
|
+
catch {
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
for (const item of parsed.Metadata ?? []) {
|
|
120
|
+
if (item.Type !== 'SentenceBoundary' && item.Type !== 'WordBoundary')
|
|
121
|
+
continue;
|
|
122
|
+
const offset = Number(item.Data?.Offset ?? 0);
|
|
123
|
+
const duration = Number(item.Data?.Duration ?? 0);
|
|
124
|
+
const text = item.Data?.text?.Text ?? '';
|
|
125
|
+
if (!Number.isFinite(offset) || !Number.isFinite(duration) || text === '')
|
|
126
|
+
continue;
|
|
127
|
+
const startMs = Math.round(offset / 10000);
|
|
128
|
+
const endMs = Math.round((offset + duration) / 10000);
|
|
129
|
+
cues.push({ index: cues.length + 1, startMs, endMs, text });
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
async function runSynthesisAttempt(options, factory, nowSeconds, attemptTimeoutMs, totalTimeoutMs, abortSignal, canRetryOn403) {
|
|
133
|
+
const token = generateSecMsGec(nowSeconds);
|
|
76
134
|
// 对齐 edge-tts 当前协议:Sec-MS-GEC 走 URL 查询参数,MUID 走 header
|
|
77
135
|
const url = WS_BASE + '?TrustedClientToken=' + TRUSTED_CLIENT_TOKEN + '&ConnectionId=' + randomUUID().replace(/-/g, '') + '&Sec-MS-GEC=' + token + '&Sec-MS-GEC-Version=' + SEC_MS_GEC_VERSION;
|
|
78
136
|
const socket = factory(url, {
|
|
79
137
|
headers: { ...WSS_HEADERS, Cookie: 'muid=' + randomBytes(16).toString('hex').toUpperCase() + ';' },
|
|
80
138
|
});
|
|
81
139
|
const chunks = [];
|
|
140
|
+
const cues = [];
|
|
82
141
|
let done = false;
|
|
83
142
|
await new Promise((resolvePromise, reject) => {
|
|
84
143
|
const canListen = typeof abortSignal?.addEventListener === 'function';
|
|
@@ -119,24 +178,63 @@ export async function synthesizeSpeech(options, deps = {}, timeoutMs = 30000, si
|
|
|
119
178
|
catch { /* 忽略 */ }
|
|
120
179
|
};
|
|
121
180
|
timer = setTimeout(() => {
|
|
122
|
-
fail(new Error('语音合成超时(' +
|
|
181
|
+
fail(new Error('语音合成超时(' + totalTimeoutMs + ' 毫秒无完整音频),请重试或检查网络。'));
|
|
123
182
|
try {
|
|
124
183
|
socket.close();
|
|
125
184
|
}
|
|
126
185
|
catch { /* 忽略 */ }
|
|
127
|
-
},
|
|
186
|
+
}, attemptTimeoutMs);
|
|
128
187
|
if (canListen)
|
|
129
188
|
abortSignal.addEventListener('abort', onAbort, { once: true });
|
|
130
189
|
if (abortSignal?.aborted === true) {
|
|
131
190
|
onAbort();
|
|
132
191
|
return;
|
|
133
192
|
}
|
|
193
|
+
if (typeof socket.on === 'function') {
|
|
194
|
+
socket.on('unexpected-response', (_req, res) => {
|
|
195
|
+
const status = res?.statusCode;
|
|
196
|
+
if (status === 403 && canRetryOn403) {
|
|
197
|
+
const rawDate = res.headers?.date ?? res.headers?.Date;
|
|
198
|
+
const dateStr = Array.isArray(rawDate) ? rawDate[0] : rawDate;
|
|
199
|
+
const serverSeconds = typeof dateStr === 'string' ? Date.parse(dateStr) / 1000 : Number.NaN;
|
|
200
|
+
if (Number.isFinite(serverSeconds)) {
|
|
201
|
+
clockSkewSeconds = serverSeconds - Date.now() / 1000;
|
|
202
|
+
try {
|
|
203
|
+
res.resume?.();
|
|
204
|
+
}
|
|
205
|
+
catch { /* 忽略 */ }
|
|
206
|
+
fail(new ClockSkewRetrySignal());
|
|
207
|
+
try {
|
|
208
|
+
socket.close();
|
|
209
|
+
}
|
|
210
|
+
catch { /* 忽略 */ }
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
try {
|
|
215
|
+
res?.resume?.();
|
|
216
|
+
}
|
|
217
|
+
catch { /* 忽略 */ }
|
|
218
|
+
fail(new Error(status === 403
|
|
219
|
+
? 'edge-tts 服务端返回 HTTP 403(请求被拒绝或本机时钟偏差过大)。'
|
|
220
|
+
: 'edge-tts 握手失败(HTTP ' + String(status ?? 'unknown') + ')。'));
|
|
221
|
+
try {
|
|
222
|
+
socket.close();
|
|
223
|
+
}
|
|
224
|
+
catch { /* 忽略 */ }
|
|
225
|
+
});
|
|
226
|
+
}
|
|
134
227
|
socket.addEventListener('open', () => {
|
|
228
|
+
const sentenceBoundaryEnabled = options.boundary === 'sentence' ? 'true' : 'false';
|
|
229
|
+
const wordBoundaryEnabled = options.boundary === 'word' ? 'true' : 'false';
|
|
135
230
|
const config = protocolHeader('speech.config', { 'Content-Type': 'application/json; charset=utf-8' })
|
|
136
231
|
+ JSON.stringify({
|
|
137
232
|
context: {
|
|
138
233
|
synthesis: {
|
|
139
|
-
audio: {
|
|
234
|
+
audio: {
|
|
235
|
+
metadataoptions: { sentenceBoundaryEnabled, wordBoundaryEnabled },
|
|
236
|
+
outputFormat: OUTPUT_FORMAT,
|
|
237
|
+
},
|
|
140
238
|
},
|
|
141
239
|
},
|
|
142
240
|
});
|
|
@@ -152,6 +250,10 @@ export async function synthesizeSpeech(options, deps = {}, timeoutMs = 30000, si
|
|
|
152
250
|
socket.addEventListener('message', (event) => {
|
|
153
251
|
const data = event.data;
|
|
154
252
|
if (typeof data === 'string') {
|
|
253
|
+
if (data.includes('Path:audio.metadata')) {
|
|
254
|
+
parseMetadataCues(data, cues);
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
155
257
|
if (data.includes('Path:turn.end')) {
|
|
156
258
|
if (chunks.length === 0) {
|
|
157
259
|
fail(new Error('合成结束但没有收到音频数据(服务端可能拒绝了请求)。'));
|
|
@@ -163,32 +265,198 @@ export async function synthesizeSpeech(options, deps = {}, timeoutMs = 30000, si
|
|
|
163
265
|
}
|
|
164
266
|
return;
|
|
165
267
|
}
|
|
166
|
-
// 二进制帧:2 字节大端头长 + 头文本 +
|
|
268
|
+
// 二进制帧:2 字节大端头长 + 头文本 + 音频数据;只收 Path:audio 帧避免非音频头污染 MP3 流
|
|
167
269
|
const buffer = Buffer.isBuffer(data) ? data : Buffer.from(data);
|
|
168
270
|
if (buffer.length > 2) {
|
|
169
271
|
const headerLength = buffer.readUInt16BE(0);
|
|
170
272
|
if (buffer.length > 2 + headerLength) {
|
|
171
|
-
|
|
273
|
+
const header = buffer.subarray(2, 2 + headerLength).toString('utf8');
|
|
274
|
+
if (/(?:^|\r?\n)Path:audio(?:\r?\n|$)/.test(header)) {
|
|
275
|
+
chunks.push(buffer.subarray(2 + headerLength));
|
|
276
|
+
}
|
|
172
277
|
}
|
|
173
278
|
}
|
|
174
279
|
});
|
|
175
280
|
socket.addEventListener('error', () => {
|
|
176
|
-
fail(new Error('edge-tts WebSocket 连接失败。若网络需要特殊代理(梯子),请在
|
|
281
|
+
fail(new Error('edge-tts WebSocket 连接失败。若网络需要特殊代理(梯子),请在 DSH「设置 → 插件 → 语音」配置 proxyUrl(保存即生效)。'));
|
|
177
282
|
});
|
|
178
283
|
socket.addEventListener('close', () => {
|
|
179
284
|
if (!done) {
|
|
180
|
-
|
|
181
|
-
done = true;
|
|
182
|
-
finish();
|
|
183
|
-
}
|
|
184
|
-
else {
|
|
185
|
-
fail(new Error('edge-tts 连接在收到音频前关闭。'));
|
|
186
|
-
}
|
|
285
|
+
fail(new Error('edge-tts 连接在收到完整音频前关闭(未收到 turn.end)。'));
|
|
187
286
|
}
|
|
188
287
|
});
|
|
189
288
|
});
|
|
190
289
|
const audio = Buffer.concat(chunks);
|
|
191
290
|
if (audio.length === 0)
|
|
192
291
|
throw new Error('合成结果为空。');
|
|
193
|
-
return audio;
|
|
292
|
+
return { audio, cues };
|
|
293
|
+
}
|
|
294
|
+
/**
|
|
295
|
+
* 合成语音并可选返回句/词级边界字幕时间轴。
|
|
296
|
+
* 收到 HTTP 403 且响应带有效 Date 头时自动校准时钟偏差并单次重试。
|
|
297
|
+
* @throws 文本为空/超长 / 连接失败 / 超时 / 无音频数据时抛中文错误。
|
|
298
|
+
*/
|
|
299
|
+
export async function synthesizeSpeechWithMetadata(options, deps = {}, timeoutMs = 30000, signal) {
|
|
300
|
+
const abortSignal = signal ?? deps.signal;
|
|
301
|
+
if (abortSignal?.aborted === true)
|
|
302
|
+
throw abortSignal.reason;
|
|
303
|
+
if (options.text.trim() === '')
|
|
304
|
+
throw new Error('要合成的文本为空。');
|
|
305
|
+
if (options.text.length > 5000)
|
|
306
|
+
throw new Error('文本过长(超过 5000 字符),请分段合成。');
|
|
307
|
+
const factory = deps.webSocketFactory ?? (deps.proxyUrl !== undefined && deps.proxyUrl !== '' ? proxyWebSocket(deps.proxyUrl) : directWebSocket);
|
|
308
|
+
const startedAt = Date.now();
|
|
309
|
+
const now = () => (deps.nowSeconds ? deps.nowSeconds() : Date.now() / 1000 + clockSkewSeconds);
|
|
310
|
+
try {
|
|
311
|
+
return await runSynthesisAttempt(options, factory, now(), timeoutMs, timeoutMs, abortSignal, true);
|
|
312
|
+
}
|
|
313
|
+
catch (error) {
|
|
314
|
+
if (!(error instanceof ClockSkewRetrySignal))
|
|
315
|
+
throw error;
|
|
316
|
+
if (abortSignal?.aborted === true)
|
|
317
|
+
throw abortSignal.reason;
|
|
318
|
+
const remainingMs = timeoutMs - (Date.now() - startedAt);
|
|
319
|
+
if (remainingMs <= 0) {
|
|
320
|
+
throw new Error('语音合成超时(' + timeoutMs + ' 毫秒无完整音频),请重试或检查网络。');
|
|
321
|
+
}
|
|
322
|
+
return await runSynthesisAttempt(options, factory, now(), remainingMs, timeoutMs, abortSignal, false);
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
/**
|
|
326
|
+
* 合成语音,返回 MP3 字节。
|
|
327
|
+
* @throws 文本为空/超长 / 连接失败 / 超时 / 无音频数据时抛中文错误。
|
|
328
|
+
*/
|
|
329
|
+
export async function synthesizeSpeech(options, deps = {}, timeoutMs = 30000, signal) {
|
|
330
|
+
const result = await synthesizeSpeechWithMetadata(options, deps, timeoutMs, signal);
|
|
331
|
+
return result.audio;
|
|
332
|
+
}
|
|
333
|
+
let cachedEdgeVoices;
|
|
334
|
+
/** 重置 Edge 在线音色内存缓存(测试专用)。 */
|
|
335
|
+
export function resetEdgeVoicesCacheForTest() {
|
|
336
|
+
cachedEdgeVoices = undefined;
|
|
337
|
+
}
|
|
338
|
+
function throwIfAborted(signal) {
|
|
339
|
+
if (signal?.aborted === true)
|
|
340
|
+
throw signal.reason;
|
|
341
|
+
}
|
|
342
|
+
function raceAbortSignal(value, signal) {
|
|
343
|
+
const promise = Promise.resolve(value);
|
|
344
|
+
if (signal === undefined || typeof signal.addEventListener !== 'function')
|
|
345
|
+
return promise;
|
|
346
|
+
if (signal.aborted === true)
|
|
347
|
+
return Promise.reject(signal.reason);
|
|
348
|
+
return new Promise((resolvePromise, rejectPromise) => {
|
|
349
|
+
const onAbort = () => rejectPromise(signal.reason);
|
|
350
|
+
signal.addEventListener('abort', onAbort, { once: true });
|
|
351
|
+
promise.then((settled) => { signal.removeEventListener('abort', onAbort); resolvePromise(settled); }, (error) => { signal.removeEventListener('abort', onAbort); rejectPromise(error); });
|
|
352
|
+
});
|
|
353
|
+
}
|
|
354
|
+
function collectLowercaseTags(builtinTags, voiceTag) {
|
|
355
|
+
const tagSet = new Set();
|
|
356
|
+
const pushAll = (items) => {
|
|
357
|
+
if (!Array.isArray(items))
|
|
358
|
+
return;
|
|
359
|
+
for (const item of items) {
|
|
360
|
+
if (typeof item !== 'string')
|
|
361
|
+
continue;
|
|
362
|
+
const normalized = item.trim().toLowerCase();
|
|
363
|
+
if (normalized !== '')
|
|
364
|
+
tagSet.add(normalized);
|
|
365
|
+
}
|
|
366
|
+
};
|
|
367
|
+
pushAll(builtinTags);
|
|
368
|
+
pushAll(voiceTag?.ContentCategories);
|
|
369
|
+
pushAll(voiceTag?.VoicePersonalities);
|
|
370
|
+
return Array.from(tagSet);
|
|
371
|
+
}
|
|
372
|
+
/**
|
|
373
|
+
* 动态拉取微软 Edge 在线全量(300+)音色目录并缓存 24 小时。
|
|
374
|
+
* @throws 请求失败 / 重定向 / 响应异常 / 超时时抛中文错误。
|
|
375
|
+
*/
|
|
376
|
+
export async function fetchEdgeVoices(deps = {}, timeoutMs = 15000) {
|
|
377
|
+
throwIfAborted(deps.signal);
|
|
378
|
+
if (deps.forceRefresh !== true
|
|
379
|
+
&& cachedEdgeVoices !== undefined
|
|
380
|
+
&& Date.now() - cachedEdgeVoices.fetchedAtMs < EDGE_VOICES_CACHE_TTL_MS) {
|
|
381
|
+
return cachedEdgeVoices.voices;
|
|
382
|
+
}
|
|
383
|
+
const nowSec = deps.nowSeconds ? deps.nowSeconds() : Date.now() / 1000 + clockSkewSeconds;
|
|
384
|
+
const token = generateSecMsGec(nowSec);
|
|
385
|
+
const url = `${VOICES_LIST_URL}?trustedclienttoken=${TRUSTED_CLIENT_TOKEN}&Sec-MS-GEC=${token}&Sec-MS-GEC-Version=${SEC_MS_GEC_VERSION}`;
|
|
386
|
+
const fetchImpl = deps.fetchImpl ?? globalThis.fetch;
|
|
387
|
+
const timeoutSignal = AbortSignal.timeout(timeoutMs);
|
|
388
|
+
const requestSignal = deps.signal === undefined ? timeoutSignal : AbortSignal.any([timeoutSignal, deps.signal]);
|
|
389
|
+
let response;
|
|
390
|
+
try {
|
|
391
|
+
response = await raceAbortSignal(fetchImpl(url, {
|
|
392
|
+
method: 'GET',
|
|
393
|
+
redirect: 'manual',
|
|
394
|
+
headers: {
|
|
395
|
+
'User-Agent': WSS_HEADERS['User-Agent'],
|
|
396
|
+
Accept: 'application/json',
|
|
397
|
+
'Sec-MS-GEC': token,
|
|
398
|
+
'Sec-MS-GEC-Version': SEC_MS_GEC_VERSION,
|
|
399
|
+
},
|
|
400
|
+
signal: requestSignal,
|
|
401
|
+
}), requestSignal);
|
|
402
|
+
}
|
|
403
|
+
catch (error) {
|
|
404
|
+
throwIfAborted(deps.signal);
|
|
405
|
+
if (timeoutSignal.aborted)
|
|
406
|
+
throw new Error('拉取 Edge 在线音色列表超时,请重试或检查网络。');
|
|
407
|
+
throw new Error('拉取 Edge 在线音色列表失败:' + (error instanceof Error ? error.message : String(error)));
|
|
408
|
+
}
|
|
409
|
+
if (response.status >= 300 && response.status < 400) {
|
|
410
|
+
try {
|
|
411
|
+
await raceAbortSignal(response.body?.cancel(), requestSignal);
|
|
412
|
+
}
|
|
413
|
+
catch { /* 忽略 */ }
|
|
414
|
+
throwIfAborted(deps.signal);
|
|
415
|
+
throw new Error('Edge 在线音色列表返回重定向(HTTP ' + response.status + '),已阻止跳转。');
|
|
416
|
+
}
|
|
417
|
+
if (!response.ok) {
|
|
418
|
+
try {
|
|
419
|
+
await raceAbortSignal(response.body?.cancel(), requestSignal);
|
|
420
|
+
}
|
|
421
|
+
catch { /* 忽略 */ }
|
|
422
|
+
throwIfAborted(deps.signal);
|
|
423
|
+
throw new Error('拉取 Edge 在线音色列表失败:HTTP ' + response.status + '。');
|
|
424
|
+
}
|
|
425
|
+
let rawList;
|
|
426
|
+
try {
|
|
427
|
+
rawList = await raceAbortSignal(response.json(), requestSignal);
|
|
428
|
+
}
|
|
429
|
+
catch (error) {
|
|
430
|
+
throwIfAborted(deps.signal);
|
|
431
|
+
if (timeoutSignal.aborted)
|
|
432
|
+
throw new Error('拉取 Edge 在线音色列表超时,请重试或检查网络。');
|
|
433
|
+
throw new Error('Edge 在线音色列表响应不是合法 JSON:' + (error instanceof Error ? error.message : String(error)));
|
|
434
|
+
}
|
|
435
|
+
if (!Array.isArray(rawList) || rawList.length === 0) {
|
|
436
|
+
throw new Error('Edge 在线音色列表为空或格式异常。');
|
|
437
|
+
}
|
|
438
|
+
const builtinById = new Map(VOICES.map((voice) => [voice.id, voice]));
|
|
439
|
+
const mapped = [];
|
|
440
|
+
for (const entry of rawList) {
|
|
441
|
+
if (typeof entry !== 'object' || entry === null)
|
|
442
|
+
continue;
|
|
443
|
+
const item = entry;
|
|
444
|
+
if (typeof item.ShortName !== 'string' || item.ShortName.trim() === '')
|
|
445
|
+
continue;
|
|
446
|
+
const id = item.ShortName.trim();
|
|
447
|
+
const gender = item.Gender === 'Male' ? 'Male' : 'Female';
|
|
448
|
+
const builtin = builtinById.get(id);
|
|
449
|
+
const localeRaw = typeof item.Locale === 'string' && item.Locale.trim() !== ''
|
|
450
|
+
? item.Locale.trim()
|
|
451
|
+
: id.split('-').slice(0, 2).join('-');
|
|
452
|
+
const friendlyRaw = typeof item.FriendlyName === 'string' ? item.FriendlyName.trim() : '';
|
|
453
|
+
const locale = builtin !== undefined ? builtin.locale : `${localeRaw}(${friendlyRaw || id})`;
|
|
454
|
+
const tags = collectLowercaseTags(builtin?.tags, item.VoiceTag);
|
|
455
|
+
mapped.push({ id, gender, locale, tags });
|
|
456
|
+
}
|
|
457
|
+
if (mapped.length === 0) {
|
|
458
|
+
throw new Error('Edge 在线音色列表为空或格式异常。');
|
|
459
|
+
}
|
|
460
|
+
cachedEdgeVoices = { voices: mapped, fetchedAtMs: Date.now() };
|
|
461
|
+
return mapped;
|
|
194
462
|
}
|
package/lib/host-config.d.ts
CHANGED
|
@@ -103,7 +103,7 @@ export declare const z: {
|
|
|
103
103
|
};
|
|
104
104
|
/**
|
|
105
105
|
* 宿主原生设置表单 Schema。
|
|
106
|
-
* 标量字段不设 default,以便留空时自动回退内置预设与环境变量 DSH_VOICE_ASR_KEY。
|
|
106
|
+
* 标量字段不设 default,以便留空时自动回退内置预设与环境变量 DSH_VOICE_ASR_KEY / DSH_VOICE_TTS_KEY。
|
|
107
107
|
*/
|
|
108
108
|
export declare const Config: HostSchemaNode<VoiceConfig>;
|
|
109
109
|
/** 活引用视图:同时兼容 DSH >=0.1.7 的 { get() } 活引用与旧宿主/测试传入的普通对象。 */
|
package/lib/host-config.js
CHANGED
|
@@ -433,55 +433,84 @@ export const z = {
|
|
|
433
433
|
};
|
|
434
434
|
/**
|
|
435
435
|
* 宿主原生设置表单 Schema。
|
|
436
|
-
* 标量字段不设 default,以便留空时自动回退内置预设与环境变量 DSH_VOICE_ASR_KEY。
|
|
436
|
+
* 标量字段不设 default,以便留空时自动回退内置预设与环境变量 DSH_VOICE_ASR_KEY / DSH_VOICE_TTS_KEY。
|
|
437
437
|
*/
|
|
438
438
|
export const Config = z.object({
|
|
439
|
+
ttsEngine: z.union(['edge', 'openai', 'siliconflow', 'custom']).volatile()
|
|
440
|
+
.description('语音合成(TTS)引擎预设:edge(默认,零密钥)/ openai / siliconflow / custom'),
|
|
441
|
+
ttsBaseUrl: z.string().volatile()
|
|
442
|
+
.description('TTS 接口基地址(custom 模式必填;openai / siliconflow 留空使用官方预设,edge 模式忽略)'),
|
|
443
|
+
ttsApiKey: z.string().role('secret').volatile()
|
|
444
|
+
.description('TTS API 密钥(自动脱敏;留空时读取环境变量 DSH_VOICE_TTS_KEY,edge 与本机回环服务无需填写)'),
|
|
445
|
+
ttsModel: z.string().volatile()
|
|
446
|
+
.description('TTS 模型名(可选;openai 默认 gpt-4o-mini-tts,siliconflow 默认 FunAudioLLM/CosyVoice2-0.5B)'),
|
|
439
447
|
ttsVoice: z.string().volatile()
|
|
440
|
-
.description('默认朗读音色(默认 zh-CN-XiaoxiaoNeural
|
|
448
|
+
.description('默认朗读音色(默认 zh-CN-XiaoxiaoNeural,edge 模式无需 API 密钥即可合成 MP3)'),
|
|
441
449
|
ttsRate: z.string().volatile()
|
|
442
450
|
.description('默认语速(如 +0%、+15%、-10%)'),
|
|
443
451
|
ttsPitch: z.string().volatile()
|
|
444
452
|
.description('默认音调(如 +0Hz、+2Hz、-1Hz)'),
|
|
445
|
-
asrEngine: z.union(['groq', 'openai', 'custom']).volatile()
|
|
446
|
-
.description('语音转文字(ASR)服务预设:groq(默认)/ openai / custom'),
|
|
453
|
+
asrEngine: z.union(['groq', 'openai', 'siliconflow', 'custom']).volatile()
|
|
454
|
+
.description('语音转文字(ASR)服务预设:groq(默认)/ openai / siliconflow / custom'),
|
|
447
455
|
asrBaseUrl: z.string().volatile()
|
|
448
|
-
.description('ASR 接口地址(custom 模式必填,如 https://api.example.com/v1;groq / openai 留空使用官方预设)'),
|
|
456
|
+
.description('ASR 接口地址(custom 模式必填,如 https://api.example.com/v1;groq / openai / siliconflow 留空使用官方预设)'),
|
|
449
457
|
asrApiKey: z.string().role('secret').volatile()
|
|
450
|
-
.description('ASR API 密钥(自动脱敏;留空时读取环境变量 DSH_VOICE_ASR_KEY
|
|
458
|
+
.description('ASR API 密钥(自动脱敏;留空时读取环境变量 DSH_VOICE_ASR_KEY,本机回环服务可留空)'),
|
|
451
459
|
asrModel: z.string().volatile()
|
|
452
|
-
.description('ASR 模型名(可选;groq 默认 whisper-large-v3-turbo,openai 默认 whisper-1)'),
|
|
460
|
+
.description('ASR 模型名(可选;groq 默认 whisper-large-v3-turbo,openai 默认 whisper-1,siliconflow 默认 FunAudioLLM/SenseVoiceSmall)'),
|
|
453
461
|
proxyUrl: z.string().role('secret').volatile()
|
|
454
|
-
.description('
|
|
462
|
+
.description('网络代理地址(可选,支持 http(s):// 或 socks5(h)://,如 http://127.0.0.1:7890)'),
|
|
455
463
|
timeoutMs: z.number().min(5000).max(600000).step(1000).volatile()
|
|
456
464
|
.description('单次合成或转写超时毫秒数(5000–600000,默认 60000)'),
|
|
457
465
|
overwrite: z.boolean().volatile()
|
|
458
466
|
.description('同名输出文件是否直接覆盖(默认关闭,自动加序号防误盖)'),
|
|
467
|
+
customVoices: z.array(z.object({
|
|
468
|
+
name: z.string().required().description('自定义音色别名(如 老王、女主持、悬疑旁白)'),
|
|
469
|
+
voice: z.string().description('底层音色 ID 或混合表达式(可选,如 zh-CN-YunzeNeural、FunAudioLLM/CosyVoice2-0.5B:alex、zf_xiaobei*0.6+zf_xiaoni*0.4)'),
|
|
470
|
+
persona: z.string().description('继承的内置人设 ID(可选,如 late-night-radio)'),
|
|
471
|
+
rate: z.string().description('专属语速(可选,如 -8%、+10%)'),
|
|
472
|
+
pitch: z.string().description('专属音调(可选,如 -2Hz、+2Hz)'),
|
|
473
|
+
instructions: z.string().description('情绪/语气指令(可选,用于 gpt-4o-mini-tts 或 CosyVoice2,如“用低沉神秘的语气轻声讲述”)'),
|
|
474
|
+
referenceAudio: z.string().description('零样本克隆参考音频路径(可选,会话工作区内 5–10 秒干净人声 wav/mp3)'),
|
|
475
|
+
referenceText: z.string().description('参考音频对应的原始台词文本(可选,CosyVoice2 等克隆模型推荐填写)'),
|
|
476
|
+
})).collapse().volatile()
|
|
477
|
+
.description('自定义音色与零样本克隆别名列表(保存后立即生效)'),
|
|
459
478
|
}).i18n({
|
|
460
479
|
zh: {
|
|
461
|
-
$description: '零密钥 Edge
|
|
462
|
-
|
|
480
|
+
$description: '零密钥 Edge / OpenAI 兼容语音合成(TTS)与语音转文字(ASR)设置(保存立即生效)',
|
|
481
|
+
ttsEngine: '语音合成引擎预设(edge / openai / siliconflow / custom)',
|
|
482
|
+
ttsBaseUrl: 'TTS 接口基地址(custom 必填,需含 /v1;openai / siliconflow 可留空,edge 不需要)',
|
|
483
|
+
ttsApiKey: 'TTS API 密钥(脱敏保存;留空时读取环境变量 DSH_VOICE_TTS_KEY,edge 与本机服务免填)',
|
|
484
|
+
ttsModel: 'TTS 模型名称(可选;留空按引擎预设自动选择)',
|
|
485
|
+
ttsVoice: '默认朗读音色(如 zh-CN-XiaoxiaoNeural、en-US-AriaNeural;edge 模式无需密钥)',
|
|
463
486
|
ttsRate: '默认语速(如 +0%、+15%、-10%)',
|
|
464
487
|
ttsPitch: '默认音调(如 +0Hz、+2Hz、-1Hz)',
|
|
465
|
-
asrEngine: '语音转文字引擎预设(groq / openai / custom)',
|
|
466
|
-
asrBaseUrl: 'ASR 接口基地址(custom 必填,需含 /v1;groq / openai 可留空)',
|
|
467
|
-
asrApiKey: 'ASR API 密钥(脱敏保存;留空时读取环境变量 DSH_VOICE_ASR_KEY
|
|
488
|
+
asrEngine: '语音转文字引擎预设(groq / openai / siliconflow / custom)',
|
|
489
|
+
asrBaseUrl: 'ASR 接口基地址(custom 必填,需含 /v1;groq / openai / siliconflow 可留空)',
|
|
490
|
+
asrApiKey: 'ASR API 密钥(脱敏保存;留空时读取环境变量 DSH_VOICE_ASR_KEY,本机服务可留空)',
|
|
468
491
|
asrModel: 'ASR 模型名称(可选;留空按引擎预设自动选择)',
|
|
469
|
-
proxyUrl: '
|
|
492
|
+
proxyUrl: '网络代理地址(可选,支持 http(s):// 或 socks5(h)://,如 http://127.0.0.1:7890)',
|
|
470
493
|
timeoutMs: '请求超时毫秒数(5000–600000,默认 60000)',
|
|
471
494
|
overwrite: '允许覆盖同名音频或转写文本(默认关闭,自动追加序号)',
|
|
495
|
+
customVoices: '自定义音色与零样本克隆别名列表(可为常用音色、人设或参考音频命名,保存后立即生效)',
|
|
472
496
|
},
|
|
473
497
|
en: {
|
|
474
|
-
$description: 'Keyless Edge TTS read-aloud and
|
|
475
|
-
|
|
498
|
+
$description: 'Keyless Edge / OpenAI-compatible TTS read-aloud and ASR transcription settings (live on save)',
|
|
499
|
+
ttsEngine: 'Text-to-speech engine preset (edge / openai / siliconflow / custom)',
|
|
500
|
+
ttsBaseUrl: 'TTS API base URL (required when ttsEngine is custom, e.g. https://api.example.com/v1; unused for edge)',
|
|
501
|
+
ttsApiKey: 'TTS API key (masked; falls back to env var DSH_VOICE_TTS_KEY; not needed for edge or loopback URLs)',
|
|
502
|
+
ttsModel: 'TTS model name (optional; defaults to gpt-4o-mini-tts on openai or FunAudioLLM/CosyVoice2-0.5B on siliconflow)',
|
|
503
|
+
ttsVoice: 'Default TTS voice ID (e.g. zh-CN-XiaoxiaoNeural, en-US-AriaNeural; no API key required for edge)',
|
|
476
504
|
ttsRate: 'Default speech rate (e.g. +0%, +15%, -10%)',
|
|
477
505
|
ttsPitch: 'Default pitch offset (e.g. +0Hz, +2Hz, -1Hz)',
|
|
478
|
-
asrEngine: 'Speech-to-text engine preset (groq / openai / custom)',
|
|
506
|
+
asrEngine: 'Speech-to-text engine preset (groq / openai / siliconflow / custom)',
|
|
479
507
|
asrBaseUrl: 'ASR API base URL (required when asrEngine is custom, e.g. https://api.example.com/v1)',
|
|
480
|
-
asrApiKey: 'ASR API key (masked; falls back to env var DSH_VOICE_ASR_KEY;
|
|
481
|
-
asrModel: 'ASR model name (optional; defaults to whisper-large-v3-turbo on groq
|
|
482
|
-
proxyUrl: 'Optional
|
|
508
|
+
asrApiKey: 'ASR API key (masked; falls back to env var DSH_VOICE_ASR_KEY; optional for loopback URLs)',
|
|
509
|
+
asrModel: 'ASR model name (optional; defaults to whisper-large-v3-turbo on groq, whisper-1 on openai, or FunAudioLLM/SenseVoiceSmall on siliconflow)',
|
|
510
|
+
proxyUrl: 'Optional proxy URL (supports http(s):// or socks5(h)://, e.g. http://127.0.0.1:7890)',
|
|
483
511
|
timeoutMs: 'Per-request timeout in ms (5000–600000, default 60000)',
|
|
484
512
|
overwrite: 'Overwrite existing output files instead of auto-numbering (default off)',
|
|
513
|
+
customVoices: 'Custom voice and zero-shot cloning aliases (name reusable voices, personas, or reference audio clips; live on save)',
|
|
485
514
|
},
|
|
486
515
|
});
|
|
487
516
|
/** 活引用视图:同时兼容 DSH >=0.1.7 的 { get() } 活引用与旧宿主/测试传入的普通对象。 */
|
package/lib/index.d.ts
CHANGED
|
@@ -31,8 +31,10 @@ export declare function apply(ctx: VoicePluginContext, config?: VoiceConfig | nu
|
|
|
31
31
|
export * from './config.js';
|
|
32
32
|
export { liveConfig, createVolatile, HostValidationError, type HostSchemaNode, type HostSchemaMeta, type VolatileRef } from './host-config.js';
|
|
33
33
|
export * from './edge-tts.js';
|
|
34
|
+
export * from './openai-tts.js';
|
|
34
35
|
export * from './paths.js';
|
|
35
36
|
export * from './proxy-fetch.js';
|
|
36
37
|
export * from './stt.js';
|
|
37
38
|
export * from './tools.js';
|
|
39
|
+
export * from './tts-text.js';
|
|
38
40
|
export * from './voices.js';
|
package/lib/index.js
CHANGED
|
@@ -56,8 +56,10 @@ export function apply(ctx, config) {
|
|
|
56
56
|
export * from './config.js';
|
|
57
57
|
export { liveConfig, createVolatile, HostValidationError } from './host-config.js';
|
|
58
58
|
export * from './edge-tts.js';
|
|
59
|
+
export * from './openai-tts.js';
|
|
59
60
|
export * from './paths.js';
|
|
60
61
|
export * from './proxy-fetch.js';
|
|
61
62
|
export * from './stt.js';
|
|
62
63
|
export * from './tools.js';
|
|
64
|
+
export * from './tts-text.js';
|
|
63
65
|
export * from './voices.js';
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OpenAI 兼容 TTS 客户端:支持 OpenAI / SiliconFlow / 本地 Kokoro 等 /audio/speech 端点。
|
|
3
|
+
*
|
|
4
|
+
* @module dsh-voice/openai-tts
|
|
5
|
+
*/
|
|
6
|
+
/** 零样本克隆参考音频样本。 */
|
|
7
|
+
export interface ReferenceVoiceSample {
|
|
8
|
+
audioDataUri: string;
|
|
9
|
+
text?: string;
|
|
10
|
+
}
|
|
11
|
+
/** OpenAI 兼容 TTS 合成选项。 */
|
|
12
|
+
export interface OpenAiTtsOptions {
|
|
13
|
+
text: string;
|
|
14
|
+
voice: string;
|
|
15
|
+
model: string;
|
|
16
|
+
speed?: number;
|
|
17
|
+
instructions?: string;
|
|
18
|
+
references?: ReferenceVoiceSample[];
|
|
19
|
+
signal?: AbortSignal;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* 将 edge-tts 百分比语速(如 `+20%`、`-15%`、`+0%`)换算为 OpenAI speed 倍率(钳制在 `[0.25, 4.0]`)。
|
|
23
|
+
*/
|
|
24
|
+
export declare function rateToSpeed(rate: string): number;
|
|
25
|
+
/**
|
|
26
|
+
* 调用 OpenAI 兼容 `/audio/speech` 接口合成 MP3 音频。
|
|
27
|
+
* 本地回环地址(localhost / 127.0.0.1 / ::1)允许空密钥直连。
|
|
28
|
+
*/
|
|
29
|
+
export declare function synthesizeOpenAiSpeech(baseUrl: string, apiKey: string, options: OpenAiTtsOptions, fetchImpl?: typeof fetch, timeoutMs?: number, signal?: AbortSignal): Promise<Buffer>;
|