dsh-voice 0.6.0 → 0.7.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/lib/config.js CHANGED
@@ -1,12 +1,34 @@
1
1
  /**
2
- * dsh-voice 配置解析:TTS 默认音色与语速、ASR 引擎与密钥、代理。
2
+ * dsh-voice 配置解析:TTS 引擎与默认音色语速、ASR 引擎与密钥、代理。
3
3
  *
4
4
  * @module dsh-voice/config
5
5
  */
6
- const ENGINE_PRESETS = {
6
+ export const ASR_PRESETS = {
7
7
  groq: { baseUrl: 'https://api.groq.com/openai/v1', model: 'whisper-large-v3-turbo' },
8
8
  openai: { baseUrl: 'https://api.openai.com/v1', model: 'whisper-1' },
9
+ siliconflow: { baseUrl: 'https://api.siliconflow.cn/v1', model: 'FunAudioLLM/SenseVoiceSmall' },
9
10
  };
11
+ export const TTS_PRESETS = {
12
+ openai: { baseUrl: 'https://api.openai.com/v1', model: 'gpt-4o-mini-tts' },
13
+ siliconflow: { baseUrl: 'https://api.siliconflow.cn/v1', model: 'FunAudioLLM/CosyVoice2-0.5B' },
14
+ };
15
+ /** 校验 SSML prosody 参数(语速 / 音调),如 +0%、-15%、+2Hz、-1st。 */
16
+ export function isValidProsody(value) {
17
+ return /^[+-]?\d+(\.\d+)?(%|Hz|st)$/.test(value);
18
+ }
19
+ /** 判断 baseUrl 是否指向本机回环地址(用于允许本地无密钥服务)。 */
20
+ export function isLoopbackBaseUrl(url) {
21
+ const trimmed = url.trim();
22
+ if (trimmed === '')
23
+ return false;
24
+ try {
25
+ const hostname = new URL(trimmed).hostname.toLowerCase();
26
+ return hostname === 'localhost' || hostname === '127.0.0.1' || hostname === '::1' || hostname === '[::1]';
27
+ }
28
+ catch {
29
+ return false;
30
+ }
31
+ }
10
32
  /** ASR 密钥:配置优先,其次环境变量 DSH_VOICE_ASR_KEY。 */
11
33
  export function resolveAsrApiKey(config, env = process.env) {
12
34
  const cfg = config ?? {};
@@ -14,6 +36,13 @@ export function resolveAsrApiKey(config, env = process.env) {
14
36
  return cfg.asrApiKey.trim();
15
37
  return env.DSH_VOICE_ASR_KEY?.trim() ?? '';
16
38
  }
39
+ /** TTS 密钥:配置优先,其次环境变量 DSH_VOICE_TTS_KEY。 */
40
+ export function resolveTtsApiKey(config, env = process.env) {
41
+ const cfg = config ?? {};
42
+ if (typeof cfg.ttsApiKey === 'string' && cfg.ttsApiKey.trim() !== '')
43
+ return cfg.ttsApiKey.trim();
44
+ return env.DSH_VOICE_TTS_KEY?.trim() ?? '';
45
+ }
17
46
  /** 默认告警出口:非法字段逐项回退时点名,避免一个字段拖垮整份配置。 */
18
47
  function defaultWarn(message) {
19
48
  console.warn('[dsh-voice] ' + message);
@@ -24,17 +53,32 @@ function defaultWarn(message) {
24
53
  */
25
54
  export function resolveConfig(config, onWarning = defaultWarn) {
26
55
  const cfg = config ?? {};
56
+ const ttsEngine = cfg.ttsEngine === 'openai' || cfg.ttsEngine === 'siliconflow' || cfg.ttsEngine === 'custom'
57
+ ? cfg.ttsEngine
58
+ : 'edge';
59
+ const ttsPreset = ttsEngine === 'openai' || ttsEngine === 'siliconflow' ? TTS_PRESETS[ttsEngine] : undefined;
60
+ const ttsBaseUrl = (typeof cfg.ttsBaseUrl === 'string' && cfg.ttsBaseUrl.trim() !== '' ? cfg.ttsBaseUrl.trim() : ttsPreset?.baseUrl ?? '').replace(/\/$/, '');
61
+ const ttsModel = typeof cfg.ttsModel === 'string' && cfg.ttsModel.trim() !== '' ? cfg.ttsModel.trim() : ttsPreset?.model ?? '';
62
+ const ttsApiKey = resolveTtsApiKey(cfg);
27
63
  const ttsVoice = typeof cfg.ttsVoice === 'string' && cfg.ttsVoice.trim() !== '' ? cfg.ttsVoice.trim() : 'zh-CN-XiaoxiaoNeural';
28
64
  const ttsRate = typeof cfg.ttsRate === 'string' && cfg.ttsRate.trim() !== '' ? cfg.ttsRate.trim() : '+0%';
65
+ if (typeof cfg.ttsRate === 'string' && cfg.ttsRate.trim() !== '' && !isValidProsody(ttsRate)) {
66
+ onWarning('ttsRate 配置不合法(格式如 +0%、+15%、-10%),当前值:' + ttsRate);
67
+ }
29
68
  const ttsPitch = typeof cfg.ttsPitch === 'string' && cfg.ttsPitch.trim() !== '' ? cfg.ttsPitch.trim() : '+0Hz';
30
- const asrEngine = cfg.asrEngine === 'openai' ? 'openai' : cfg.asrEngine === 'custom' ? 'custom' : 'groq';
31
- const preset = asrEngine === 'custom' ? undefined : ENGINE_PRESETS[asrEngine];
32
- const asrBaseUrl = (typeof cfg.asrBaseUrl === 'string' && cfg.asrBaseUrl.trim() !== '' ? cfg.asrBaseUrl.trim() : preset?.baseUrl ?? '').replace(/\/$/, '');
33
- const asrModel = typeof cfg.asrModel === 'string' && cfg.asrModel.trim() !== '' ? cfg.asrModel.trim() : preset?.model ?? '';
69
+ if (typeof cfg.ttsPitch === 'string' && cfg.ttsPitch.trim() !== '' && !isValidProsody(ttsPitch)) {
70
+ onWarning('ttsPitch 配置不合法(格式如 +0Hz、+2Hz、-1st),当前值:' + ttsPitch);
71
+ }
72
+ const asrEngine = cfg.asrEngine === 'openai' || cfg.asrEngine === 'siliconflow' || cfg.asrEngine === 'custom'
73
+ ? cfg.asrEngine
74
+ : 'groq';
75
+ const asrPreset = asrEngine === 'custom' ? undefined : ASR_PRESETS[asrEngine];
76
+ const asrBaseUrl = (typeof cfg.asrBaseUrl === 'string' && cfg.asrBaseUrl.trim() !== '' ? cfg.asrBaseUrl.trim() : asrPreset?.baseUrl ?? '').replace(/\/$/, '');
77
+ const asrModel = typeof cfg.asrModel === 'string' && cfg.asrModel.trim() !== '' ? cfg.asrModel.trim() : asrPreset?.model ?? '';
34
78
  const asrApiKey = resolveAsrApiKey(cfg);
35
79
  let proxyUrl = typeof cfg.proxyUrl === 'string' && cfg.proxyUrl.trim() !== '' ? cfg.proxyUrl.trim() : '';
36
- if (proxyUrl !== '' && !/^https?:\/\//i.test(proxyUrl)) {
37
- onWarning('proxyUrl 配置不合法(必须以 http(s):// 开头,例如 http://127.0.0.1:7890),已忽略该项,其余配置继续生效:' + proxyUrl);
80
+ if (proxyUrl !== '' && !/^(https?|socks5h?):\/\//i.test(proxyUrl)) {
81
+ onWarning('proxyUrl 配置不合法(必须以 http(s):// 或 socks5(h):// 开头,例如 http://127.0.0.1:7890 或 socks5://127.0.0.1:7890),已忽略该项,其余配置继续生效:' + proxyUrl);
38
82
  proxyUrl = '';
39
83
  }
40
84
  let timeoutMs = 60000;
@@ -47,5 +91,20 @@ export function resolveConfig(config, onWarning = defaultWarn) {
47
91
  }
48
92
  }
49
93
  const overwrite = cfg.overwrite === true;
50
- return { ttsVoice, ttsRate, ttsPitch, asrEngine, asrBaseUrl, asrApiKey, asrModel, proxyUrl, timeoutMs, overwrite };
94
+ return {
95
+ ttsEngine,
96
+ ttsBaseUrl,
97
+ ttsApiKey,
98
+ ttsModel,
99
+ ttsVoice,
100
+ ttsRate,
101
+ ttsPitch,
102
+ asrEngine,
103
+ asrBaseUrl,
104
+ asrApiKey,
105
+ asrModel,
106
+ proxyUrl,
107
+ timeoutMs,
108
+ overwrite,
109
+ };
51
110
  }
package/lib/edge-tts.d.ts CHANGED
@@ -1,3 +1,7 @@
1
+ /** 重置时钟偏差缓存(测试专用)。 */
2
+ export declare function resetClockSkewForTest(): void;
3
+ /** 读取当前时钟偏差(秒)。 */
4
+ export declare function getClockSkewSeconds(): number;
1
5
  /** 合成选项。 */
2
6
  export interface TtsOptions {
3
7
  text: string;
@@ -5,6 +9,13 @@ export interface TtsOptions {
5
9
  rate: string;
6
10
  pitch: string;
7
11
  }
12
+ /** 字幕时间轴条目(毫秒级)。 */
13
+ export interface SubtitleCue {
14
+ index: number;
15
+ startMs: number;
16
+ endMs: number;
17
+ text: string;
18
+ }
8
19
  /** 可注入依赖(测试用假实现)。 */
9
20
  export interface TtsDeps {
10
21
  webSocketFactory?: WebSocketFactory;
@@ -13,12 +24,19 @@ export interface TtsDeps {
13
24
  /** 取消信号(可选);也可通过 synthesizeSpeech 第 4 个参数传入。 */
14
25
  signal?: AbortSignal;
15
26
  }
27
+ /** unexpected-response 事件中的 HTTP 响应最小面。 */
28
+ export interface UnexpectedResponseInfo {
29
+ statusCode?: number;
30
+ headers?: Record<string, string | string[] | undefined>;
31
+ resume?: () => void;
32
+ }
16
33
  /** 最小的 WebSocket 面。 */
17
34
  export interface WebSocketLike {
18
35
  addEventListener(type: 'message', listener: (event: {
19
36
  data: unknown;
20
37
  }) => void): void;
21
38
  addEventListener(type: 'open' | 'close' | 'error', listener: (event?: unknown) => void): void;
39
+ on?(event: 'unexpected-response', listener: (req: unknown, res: UnexpectedResponseInfo) => void): void;
22
40
  send(data: string): void;
23
41
  close(): void;
24
42
  }
@@ -31,6 +49,17 @@ export type WebSocketFactory = (url: string, options: {
31
49
  export declare function generateSecMsGec(nowSeconds?: number): string;
32
50
  /** 生成 SSML。 */
33
51
  export declare function buildSsml(options: TtsOptions): string;
52
+ /**
53
+ * 合成语音并可选返回句/词级边界字幕时间轴。
54
+ * 收到 HTTP 403 且响应带有效 Date 头时自动校准时钟偏差并单次重试。
55
+ * @throws 文本为空/超长 / 连接失败 / 超时 / 无音频数据时抛中文错误。
56
+ */
57
+ export declare function synthesizeSpeechWithMetadata(options: TtsOptions & {
58
+ boundary?: 'sentence' | 'word' | false;
59
+ }, deps?: TtsDeps, timeoutMs?: number, signal?: AbortSignal): Promise<{
60
+ audio: Buffer;
61
+ cues: SubtitleCue[];
62
+ }>;
34
63
  /**
35
64
  * 合成语音,返回 MP3 字节。
36
65
  * @throws 文本为空/超长 / 连接失败 / 超时 / 无音频数据时抛中文错误。
package/lib/edge-tts.js CHANGED
@@ -6,9 +6,9 @@
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
12
  const TRUSTED_CLIENT_TOKEN = '6A5AA1D4EAFF4E9FB37E23D68491D6F4';
13
13
  const WS_BASE = 'wss://speech.platform.bing.com/consumer/speech/synthesize/readaloud/edge/v1';
14
14
  const OUTPUT_FORMAT = 'audio-24khz-48kbitrate-mono-mp3';
@@ -23,14 +23,30 @@ const WSS_HEADERS = {
23
23
  'Sec-WebSocket-Version': '13',
24
24
  '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
25
  };
26
+ /** 403 响应校准出的服务端时钟偏差(秒):serverSeconds - Date.now() / 1000。 */
27
+ let clockSkewSeconds = 0;
28
+ /** 重置时钟偏差缓存(测试专用)。 */
29
+ export function resetClockSkewForTest() {
30
+ clockSkewSeconds = 0;
31
+ }
32
+ /** 读取当前时钟偏差(秒)。 */
33
+ export function getClockSkewSeconds() {
34
+ return clockSkewSeconds;
35
+ }
36
+ /** 内部哨兵错误:收到 403 且已根据服务端 Date 头校准时钟后触发单次重试。 */
37
+ class ClockSkewRetrySignal extends Error {
38
+ constructor() {
39
+ super('clock-skew-retry');
40
+ }
41
+ }
26
42
  /** 直连工厂(ws 库,Node 全版本可用)。 */
27
43
  function directWebSocket(url, options) {
28
44
  return new WebSocket(url, { headers: options.headers });
29
45
  }
30
- /** 代理工厂:经 HTTP CONNECT 隧道走本地代理(大陆直连会被重置时使用)。 */
46
+ /** 代理工厂:复用 proxy-fetch 缓存的 HTTP(S) / SOCKS5 Agent。 */
31
47
  function proxyWebSocket(proxyUrl) {
32
- const agent = new HttpsProxyAgent(proxyUrl);
33
- return (url, options) => new WebSocket(url, { headers: options.headers, agent });
48
+ const agent = getProxyWsAgent(proxyUrl);
49
+ return (url, options) => new WebSocket(url, { headers: options.headers, agent: agent });
34
50
  }
35
51
  /**
36
52
  * 本地生成 Sec-MS-GEC v2 令牌(对齐 edge-tts 7.3.1)。
@@ -42,14 +58,37 @@ export function generateSecMsGec(nowSeconds = Date.now() / 1000) {
42
58
  const raw = String(windowsTicks) + TRUSTED_CLIENT_TOKEN;
43
59
  return edgeTokenV2(raw);
44
60
  }
45
- /** XML 转义。 */
61
+ /** 去除 XML 1.0 非法控制字符(保留 \t \n \r)。 */
62
+ function stripIllegalXmlChars(text) {
63
+ return text.replace(/[\x00-\x08\x0B\x0C\x0E-\x1F]/g, '');
64
+ }
65
+ /** XML 文本转义。 */
46
66
  function escapeXml(text) {
47
- return text.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
67
+ return stripIllegalXmlChars(text)
68
+ .replace(/&/g, '&amp;')
69
+ .replace(/</g, '&lt;')
70
+ .replace(/>/g, '&gt;');
71
+ }
72
+ /** XML 属性转义(同时转义单双引号)。 */
73
+ function escapeXmlAttr(value) {
74
+ return escapeXml(value)
75
+ .replace(/'/g, '&apos;')
76
+ .replace(/"/g, '&quot;');
48
77
  }
49
78
  /** 生成 SSML。 */
50
79
  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='" + lang + "'><voice name='" + options.voice + "'><prosody pitch='" + options.pitch + "' rate='" + options.rate + "' volume='+0%'>" + escapeXml(options.text) + '</prosody></voice></speak>';
80
+ const lang = /^[a-z]{2,3}(?:-[A-Za-z]{2,4})?(?:-[A-Z]{2})?/.exec(options.voice)?.[0] ?? 'zh-CN';
81
+ return "<speak version='1.0' xmlns='http://www.w3.org/2001/10/synthesis' xml:lang='"
82
+ + escapeXmlAttr(lang)
83
+ + "'><voice name='"
84
+ + escapeXmlAttr(options.voice)
85
+ + "'><prosody pitch='"
86
+ + escapeXmlAttr(options.pitch)
87
+ + "' rate='"
88
+ + escapeXmlAttr(options.rate)
89
+ + "' volume='+0%'>"
90
+ + escapeXml(options.text)
91
+ + '</prosody></voice></speak>';
53
92
  }
54
93
  /** 生成带时间戳与路径的协议消息头。 */
55
94
  function protocolHeader(path, extra) {
@@ -59,26 +98,43 @@ function protocolHeader(path, extra) {
59
98
  lines.push('Path:' + path);
60
99
  return lines.join('\r\n') + '\r\n\r\n';
61
100
  }
62
- /**
63
- * 合成语音,返回 MP3 字节。
64
- * @throws 文本为空/超长 / 连接失败 / 超时 / 无音频数据时抛中文错误。
65
- */
66
- export async function synthesizeSpeech(options, deps = {}, timeoutMs = 30000, signal) {
67
- const abortSignal = signal ?? deps.signal;
68
- if (abortSignal?.aborted === true)
69
- throw abortSignal.reason;
70
- if (options.text.trim() === '')
71
- throw new Error('要合成的文本为空。');
72
- if (options.text.length > 5000)
73
- throw new Error('文本过长(超过 5000 字符),请分段合成。');
74
- const factory = deps.webSocketFactory ?? (deps.proxyUrl !== undefined && deps.proxyUrl !== '' ? proxyWebSocket(deps.proxyUrl) : directWebSocket);
75
- const token = generateSecMsGec(deps.nowSeconds?.() ?? Date.now() / 1000);
101
+ /** 从 metadata JSON 文本帧中提取句/词边界字幕。 */
102
+ function parseMetadataCues(data, cues) {
103
+ const sepIndex = data.indexOf('\r\n\r\n');
104
+ if (sepIndex === -1)
105
+ return;
106
+ const body = data.slice(sepIndex + 4).trim();
107
+ if (body === '')
108
+ return;
109
+ let parsed;
110
+ try {
111
+ parsed = JSON.parse(body);
112
+ }
113
+ catch {
114
+ return;
115
+ }
116
+ for (const item of parsed.Metadata ?? []) {
117
+ if (item.Type !== 'SentenceBoundary' && item.Type !== 'WordBoundary')
118
+ continue;
119
+ const offset = Number(item.Data?.Offset ?? 0);
120
+ const duration = Number(item.Data?.Duration ?? 0);
121
+ const text = item.Data?.text?.Text ?? '';
122
+ if (!Number.isFinite(offset) || !Number.isFinite(duration) || text === '')
123
+ continue;
124
+ const startMs = Math.round(offset / 10000);
125
+ const endMs = Math.round((offset + duration) / 10000);
126
+ cues.push({ index: cues.length + 1, startMs, endMs, text });
127
+ }
128
+ }
129
+ async function runSynthesisAttempt(options, factory, nowSeconds, attemptTimeoutMs, totalTimeoutMs, abortSignal, canRetryOn403) {
130
+ const token = generateSecMsGec(nowSeconds);
76
131
  // 对齐 edge-tts 当前协议:Sec-MS-GEC 走 URL 查询参数,MUID 走 header
77
132
  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
133
  const socket = factory(url, {
79
134
  headers: { ...WSS_HEADERS, Cookie: 'muid=' + randomBytes(16).toString('hex').toUpperCase() + ';' },
80
135
  });
81
136
  const chunks = [];
137
+ const cues = [];
82
138
  let done = false;
83
139
  await new Promise((resolvePromise, reject) => {
84
140
  const canListen = typeof abortSignal?.addEventListener === 'function';
@@ -119,24 +175,63 @@ export async function synthesizeSpeech(options, deps = {}, timeoutMs = 30000, si
119
175
  catch { /* 忽略 */ }
120
176
  };
121
177
  timer = setTimeout(() => {
122
- fail(new Error('语音合成超时(' + timeoutMs + ' 毫秒无完整音频),请重试或检查网络。'));
178
+ fail(new Error('语音合成超时(' + totalTimeoutMs + ' 毫秒无完整音频),请重试或检查网络。'));
123
179
  try {
124
180
  socket.close();
125
181
  }
126
182
  catch { /* 忽略 */ }
127
- }, timeoutMs);
183
+ }, attemptTimeoutMs);
128
184
  if (canListen)
129
185
  abortSignal.addEventListener('abort', onAbort, { once: true });
130
186
  if (abortSignal?.aborted === true) {
131
187
  onAbort();
132
188
  return;
133
189
  }
190
+ if (typeof socket.on === 'function') {
191
+ socket.on('unexpected-response', (_req, res) => {
192
+ const status = res?.statusCode;
193
+ if (status === 403 && canRetryOn403) {
194
+ const rawDate = res.headers?.date ?? res.headers?.Date;
195
+ const dateStr = Array.isArray(rawDate) ? rawDate[0] : rawDate;
196
+ const serverSeconds = typeof dateStr === 'string' ? Date.parse(dateStr) / 1000 : Number.NaN;
197
+ if (Number.isFinite(serverSeconds)) {
198
+ clockSkewSeconds = serverSeconds - Date.now() / 1000;
199
+ try {
200
+ res.resume?.();
201
+ }
202
+ catch { /* 忽略 */ }
203
+ fail(new ClockSkewRetrySignal());
204
+ try {
205
+ socket.close();
206
+ }
207
+ catch { /* 忽略 */ }
208
+ return;
209
+ }
210
+ }
211
+ try {
212
+ res?.resume?.();
213
+ }
214
+ catch { /* 忽略 */ }
215
+ fail(new Error(status === 403
216
+ ? 'edge-tts 服务端返回 HTTP 403(请求被拒绝或本机时钟偏差过大)。'
217
+ : 'edge-tts 握手失败(HTTP ' + String(status ?? 'unknown') + ')。'));
218
+ try {
219
+ socket.close();
220
+ }
221
+ catch { /* 忽略 */ }
222
+ });
223
+ }
134
224
  socket.addEventListener('open', () => {
225
+ const sentenceBoundaryEnabled = options.boundary === 'sentence' ? 'true' : 'false';
226
+ const wordBoundaryEnabled = options.boundary === 'word' ? 'true' : 'false';
135
227
  const config = protocolHeader('speech.config', { 'Content-Type': 'application/json; charset=utf-8' })
136
228
  + JSON.stringify({
137
229
  context: {
138
230
  synthesis: {
139
- audio: { metadataoptions: { sentenceBoundaryEnabled: 'false', wordBoundaryEnabled: 'false' }, outputFormat: OUTPUT_FORMAT },
231
+ audio: {
232
+ metadataoptions: { sentenceBoundaryEnabled, wordBoundaryEnabled },
233
+ outputFormat: OUTPUT_FORMAT,
234
+ },
140
235
  },
141
236
  },
142
237
  });
@@ -152,6 +247,10 @@ export async function synthesizeSpeech(options, deps = {}, timeoutMs = 30000, si
152
247
  socket.addEventListener('message', (event) => {
153
248
  const data = event.data;
154
249
  if (typeof data === 'string') {
250
+ if (data.includes('Path:audio.metadata')) {
251
+ parseMetadataCues(data, cues);
252
+ return;
253
+ }
155
254
  if (data.includes('Path:turn.end')) {
156
255
  if (chunks.length === 0) {
157
256
  fail(new Error('合成结束但没有收到音频数据(服务端可能拒绝了请求)。'));
@@ -163,32 +262,68 @@ export async function synthesizeSpeech(options, deps = {}, timeoutMs = 30000, si
163
262
  }
164
263
  return;
165
264
  }
166
- // 二进制帧:2 字节大端头长 + 头文本 + 音频数据
265
+ // 二进制帧:2 字节大端头长 + 头文本 + 音频数据;只收 Path:audio 帧避免非音频头污染 MP3 流
167
266
  const buffer = Buffer.isBuffer(data) ? data : Buffer.from(data);
168
267
  if (buffer.length > 2) {
169
268
  const headerLength = buffer.readUInt16BE(0);
170
269
  if (buffer.length > 2 + headerLength) {
171
- chunks.push(buffer.subarray(2 + headerLength));
270
+ const header = buffer.subarray(2, 2 + headerLength).toString('utf8');
271
+ if (/(?:^|\r?\n)Path:audio(?:\r?\n|$)/.test(header)) {
272
+ chunks.push(buffer.subarray(2 + headerLength));
273
+ }
172
274
  }
173
275
  }
174
276
  });
175
277
  socket.addEventListener('error', () => {
176
- fail(new Error('edge-tts WebSocket 连接失败。若网络需要特殊代理(梯子),请在 cordis.patch.yml 配置 proxyUrl 后重启。'));
278
+ fail(new Error('edge-tts WebSocket 连接失败。若网络需要特殊代理(梯子),请在 DSH「设置 → 插件 → 语音」配置 proxyUrl(保存即生效)。'));
177
279
  });
178
280
  socket.addEventListener('close', () => {
179
281
  if (!done) {
180
- if (chunks.length > 0) {
181
- done = true;
182
- finish();
183
- }
184
- else {
185
- fail(new Error('edge-tts 连接在收到音频前关闭。'));
186
- }
282
+ fail(new Error('edge-tts 连接在收到完整音频前关闭(未收到 turn.end)。'));
187
283
  }
188
284
  });
189
285
  });
190
286
  const audio = Buffer.concat(chunks);
191
287
  if (audio.length === 0)
192
288
  throw new Error('合成结果为空。');
193
- return audio;
289
+ return { audio, cues };
290
+ }
291
+ /**
292
+ * 合成语音并可选返回句/词级边界字幕时间轴。
293
+ * 收到 HTTP 403 且响应带有效 Date 头时自动校准时钟偏差并单次重试。
294
+ * @throws 文本为空/超长 / 连接失败 / 超时 / 无音频数据时抛中文错误。
295
+ */
296
+ export async function synthesizeSpeechWithMetadata(options, deps = {}, timeoutMs = 30000, signal) {
297
+ const abortSignal = signal ?? deps.signal;
298
+ if (abortSignal?.aborted === true)
299
+ throw abortSignal.reason;
300
+ if (options.text.trim() === '')
301
+ throw new Error('要合成的文本为空。');
302
+ if (options.text.length > 5000)
303
+ throw new Error('文本过长(超过 5000 字符),请分段合成。');
304
+ const factory = deps.webSocketFactory ?? (deps.proxyUrl !== undefined && deps.proxyUrl !== '' ? proxyWebSocket(deps.proxyUrl) : directWebSocket);
305
+ const startedAt = Date.now();
306
+ const now = () => (deps.nowSeconds ? deps.nowSeconds() : Date.now() / 1000 + clockSkewSeconds);
307
+ try {
308
+ return await runSynthesisAttempt(options, factory, now(), timeoutMs, timeoutMs, abortSignal, true);
309
+ }
310
+ catch (error) {
311
+ if (!(error instanceof ClockSkewRetrySignal))
312
+ throw error;
313
+ if (abortSignal?.aborted === true)
314
+ throw abortSignal.reason;
315
+ const remainingMs = timeoutMs - (Date.now() - startedAt);
316
+ if (remainingMs <= 0) {
317
+ throw new Error('语音合成超时(' + timeoutMs + ' 毫秒无完整音频),请重试或检查网络。');
318
+ }
319
+ return await runSynthesisAttempt(options, factory, now(), remainingMs, timeoutMs, abortSignal, false);
320
+ }
321
+ }
322
+ /**
323
+ * 合成语音,返回 MP3 字节。
324
+ * @throws 文本为空/超长 / 连接失败 / 超时 / 无音频数据时抛中文错误。
325
+ */
326
+ export async function synthesizeSpeech(options, deps = {}, timeoutMs = 30000, signal) {
327
+ const result = await synthesizeSpeechWithMetadata(options, deps, timeoutMs, signal);
328
+ return result.audio;
194
329
  }
@@ -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() } 活引用与旧宿主/测试传入的普通对象。 */
@@ -433,53 +433,69 @@ 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,无需 API 密钥即可合成 MP3)'),
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('HTTP(S) 代理地址(可选,如 http://127.0.0.1:7890)'),
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('同名输出文件是否直接覆盖(默认关闭,自动加序号防误盖)'),
459
467
  }).i18n({
460
468
  zh: {
461
- $description: '零密钥 Edge 语音合成(TTS)与兼容语音转文字(ASR)设置(保存立即生效)',
462
- ttsVoice: '默认朗读音色(如 zh-CN-XiaoxiaoNeural、en-US-AriaNeural;文字转语音无需密钥)',
469
+ $description: '零密钥 Edge / OpenAI 兼容语音合成(TTS)与语音转文字(ASR)设置(保存立即生效)',
470
+ ttsEngine: '语音合成引擎预设(edge / openai / siliconflow / custom)',
471
+ ttsBaseUrl: 'TTS 接口基地址(custom 必填,需含 /v1;openai / siliconflow 可留空,edge 不需要)',
472
+ ttsApiKey: 'TTS API 密钥(脱敏保存;留空时读取环境变量 DSH_VOICE_TTS_KEY,edge 与本机服务免填)',
473
+ ttsModel: 'TTS 模型名称(可选;留空按引擎预设自动选择)',
474
+ ttsVoice: '默认朗读音色(如 zh-CN-XiaoxiaoNeural、en-US-AriaNeural;edge 模式无需密钥)',
463
475
  ttsRate: '默认语速(如 +0%、+15%、-10%)',
464
476
  ttsPitch: '默认音调(如 +0Hz、+2Hz、-1Hz)',
465
- asrEngine: '语音转文字引擎预设(groq / openai / custom)',
466
- asrBaseUrl: 'ASR 接口基地址(custom 必填,需含 /v1;groq / openai 可留空)',
467
- asrApiKey: 'ASR API 密钥(脱敏保存;留空时读取环境变量 DSH_VOICE_ASR_KEY)',
477
+ asrEngine: '语音转文字引擎预设(groq / openai / siliconflow / custom)',
478
+ asrBaseUrl: 'ASR 接口基地址(custom 必填,需含 /v1;groq / openai / siliconflow 可留空)',
479
+ asrApiKey: 'ASR API 密钥(脱敏保存;留空时读取环境变量 DSH_VOICE_ASR_KEY,本机服务可留空)',
468
480
  asrModel: 'ASR 模型名称(可选;留空按引擎预设自动选择)',
469
- proxyUrl: '网络代理地址(可选,如 http://127.0.0.1:7890)',
481
+ proxyUrl: '网络代理地址(可选,支持 http(s):// 或 socks5(h)://,如 http://127.0.0.1:7890)',
470
482
  timeoutMs: '请求超时毫秒数(5000–600000,默认 60000)',
471
483
  overwrite: '允许覆盖同名音频或转写文本(默认关闭,自动追加序号)',
472
484
  },
473
485
  en: {
474
- $description: 'Keyless Edge TTS read-aloud and OpenAI/Groq-compatible ASR transcription settings (live on save)',
475
- ttsVoice: 'Default TTS voice ID (e.g. zh-CN-XiaoxiaoNeural, en-US-AriaNeural; no API key required)',
486
+ $description: 'Keyless Edge / OpenAI-compatible TTS read-aloud and ASR transcription settings (live on save)',
487
+ ttsEngine: 'Text-to-speech engine preset (edge / openai / siliconflow / custom)',
488
+ ttsBaseUrl: 'TTS API base URL (required when ttsEngine is custom, e.g. https://api.example.com/v1; unused for edge)',
489
+ ttsApiKey: 'TTS API key (masked; falls back to env var DSH_VOICE_TTS_KEY; not needed for edge or loopback URLs)',
490
+ ttsModel: 'TTS model name (optional; defaults to gpt-4o-mini-tts on openai or FunAudioLLM/CosyVoice2-0.5B on siliconflow)',
491
+ ttsVoice: 'Default TTS voice ID (e.g. zh-CN-XiaoxiaoNeural, en-US-AriaNeural; no API key required for edge)',
476
492
  ttsRate: 'Default speech rate (e.g. +0%, +15%, -10%)',
477
493
  ttsPitch: 'Default pitch offset (e.g. +0Hz, +2Hz, -1Hz)',
478
- asrEngine: 'Speech-to-text engine preset (groq / openai / custom)',
494
+ asrEngine: 'Speech-to-text engine preset (groq / openai / siliconflow / custom)',
479
495
  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; only needed for transcription)',
481
- asrModel: 'ASR model name (optional; defaults to whisper-large-v3-turbo on groq or whisper-1 on openai)',
482
- proxyUrl: 'Optional HTTP(S) proxy URL (e.g. http://127.0.0.1:7890)',
496
+ asrApiKey: 'ASR API key (masked; falls back to env var DSH_VOICE_ASR_KEY; optional for loopback URLs)',
497
+ asrModel: 'ASR model name (optional; defaults to whisper-large-v3-turbo on groq, whisper-1 on openai, or FunAudioLLM/SenseVoiceSmall on siliconflow)',
498
+ proxyUrl: 'Optional proxy URL (supports http(s):// or socks5(h)://, e.g. http://127.0.0.1:7890)',
483
499
  timeoutMs: 'Per-request timeout in ms (5000–600000, default 60000)',
484
500
  overwrite: 'Overwrite existing output files instead of auto-numbering (default off)',
485
501
  },
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,22 @@
1
+ /**
2
+ * OpenAI 兼容 TTS 客户端:支持 OpenAI / SiliconFlow / 本地 Kokoro 等 /audio/speech 端点。
3
+ *
4
+ * @module dsh-voice/openai-tts
5
+ */
6
+ /** OpenAI 兼容 TTS 合成选项。 */
7
+ export interface OpenAiTtsOptions {
8
+ text: string;
9
+ voice: string;
10
+ model: string;
11
+ speed?: number;
12
+ signal?: AbortSignal;
13
+ }
14
+ /**
15
+ * 将 edge-tts 百分比语速(如 `+20%`、`-15%`、`+0%`)换算为 OpenAI speed 倍率(钳制在 `[0.25, 4.0]`)。
16
+ */
17
+ export declare function rateToSpeed(rate: string): number;
18
+ /**
19
+ * 调用 OpenAI 兼容 `/audio/speech` 接口合成 MP3 音频。
20
+ * 本地回环地址(localhost / 127.0.0.1 / ::1)允许空密钥直连。
21
+ */
22
+ export declare function synthesizeOpenAiSpeech(baseUrl: string, apiKey: string, options: OpenAiTtsOptions, fetchImpl?: typeof fetch, timeoutMs?: number, signal?: AbortSignal): Promise<Buffer>;