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/tools.js
CHANGED
|
@@ -3,13 +3,16 @@
|
|
|
3
3
|
*
|
|
4
4
|
* @module dsh-voice/tools
|
|
5
5
|
*/
|
|
6
|
-
import { readFileSync, statSync } from 'node:fs';
|
|
7
|
-
import { basename, join } from 'node:path';
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
6
|
+
import { openAsBlob, readFileSync, statSync } from 'node:fs';
|
|
7
|
+
import { basename, extname, join } from 'node:path';
|
|
8
|
+
import { isLoopbackBaseUrl, isValidProsody, resolveCustomVoice } from './config.js';
|
|
9
|
+
import { fetchEdgeVoices, synthesizeSpeech, synthesizeSpeechWithMetadata } from './edge-tts.js';
|
|
10
|
+
import { rateToSpeed, synthesizeOpenAiSpeech } from './openai-tts.js';
|
|
11
|
+
import { assertAudioFile, assertWorkspaceFile, ensureWorkspaceDirectory, resolveOutputPath, writeWorkspaceFile } from './paths.js';
|
|
10
12
|
import { createProxyFetch } from './proxy-fetch.js';
|
|
11
|
-
import { assertAudioSize, transcribe } from './stt.js';
|
|
12
|
-
import {
|
|
13
|
+
import { assertAudioSize, formatSttSubtitles, hasFfmpeg, prepareAudioForAsr, transcribe } from './stt.js';
|
|
14
|
+
import { cleanMarkdownForSpeech, concatMp3Buffers, createSilenceMp3, estimateMp3DurationMs, formatSubtitles, normalizeAudioLoudness, offsetCues, renderPreviewHtml, splitTextIntoChunks, } from './tts-text.js';
|
|
15
|
+
import { filterVoices, isValidVoiceId, resolvePersona, VOICE_PERSONAS, VOICES } from './voices.js';
|
|
13
16
|
function compileParameters(spec) {
|
|
14
17
|
const properties = {};
|
|
15
18
|
const required = [];
|
|
@@ -25,6 +28,16 @@ function compileParameters(spec) {
|
|
|
25
28
|
node.enum = prop.enum;
|
|
26
29
|
if (prop?.items !== undefined)
|
|
27
30
|
node.items = prop.items;
|
|
31
|
+
if (prop?.properties !== undefined)
|
|
32
|
+
node.properties = prop.properties;
|
|
33
|
+
if (Array.isArray(prop?.required))
|
|
34
|
+
node.required = prop.required;
|
|
35
|
+
if (prop?.default !== undefined)
|
|
36
|
+
node.default = prop.default;
|
|
37
|
+
if (typeof prop?.minimum === 'number')
|
|
38
|
+
node.minimum = prop.minimum;
|
|
39
|
+
if (typeof prop?.maximum === 'number')
|
|
40
|
+
node.maximum = prop.maximum;
|
|
28
41
|
properties[key] = node;
|
|
29
42
|
}
|
|
30
43
|
return { type: 'object', properties, ...(required.length > 0 ? { required } : {}) };
|
|
@@ -87,6 +100,23 @@ function sessionCwd(exec) {
|
|
|
87
100
|
const cwd = exec?.agent?.session?.header?.cwd;
|
|
88
101
|
return typeof cwd === 'string' && cwd.trim() !== '' ? cwd : process.cwd();
|
|
89
102
|
}
|
|
103
|
+
function mimeFromPath(filePath) {
|
|
104
|
+
const ext = extname(filePath).toLowerCase();
|
|
105
|
+
const table = {
|
|
106
|
+
'.mp3': 'audio/mpeg',
|
|
107
|
+
'.wav': 'audio/wav',
|
|
108
|
+
'.m4a': 'audio/mp4',
|
|
109
|
+
'.mp4': 'audio/mp4',
|
|
110
|
+
'.ogg': 'audio/ogg',
|
|
111
|
+
'.opus': 'audio/ogg',
|
|
112
|
+
'.flac': 'audio/flac',
|
|
113
|
+
'.webm': 'audio/webm',
|
|
114
|
+
};
|
|
115
|
+
return table[ext] ?? 'application/octet-stream';
|
|
116
|
+
}
|
|
117
|
+
function subtitleFormatOf(filePath) {
|
|
118
|
+
return extname(filePath).toLowerCase() === '.vtt' ? 'vtt' : 'srt';
|
|
119
|
+
}
|
|
90
120
|
const voiceItemSchema = {
|
|
91
121
|
type: 'object',
|
|
92
122
|
properties: { id: { type: 'string' }, gender: { type: 'string' }, locale: { type: 'string' } },
|
|
@@ -94,7 +124,14 @@ const voiceItemSchema = {
|
|
|
94
124
|
};
|
|
95
125
|
const listSchema = {
|
|
96
126
|
type: 'object',
|
|
97
|
-
properties: {
|
|
127
|
+
properties: {
|
|
128
|
+
count: { type: 'integer' },
|
|
129
|
+
voices: { type: 'array', items: voiceItemSchema },
|
|
130
|
+
source: { type: 'string' },
|
|
131
|
+
personas: { type: 'array', items: { type: 'object', additionalProperties: true } },
|
|
132
|
+
customVoices: { type: 'array', items: { type: 'object', additionalProperties: true } },
|
|
133
|
+
fallbackReason: { type: 'string' },
|
|
134
|
+
},
|
|
98
135
|
additionalProperties: true,
|
|
99
136
|
};
|
|
100
137
|
const ttsSchema = {
|
|
@@ -106,6 +143,8 @@ const ttsSchema = {
|
|
|
106
143
|
rate: { type: 'string' },
|
|
107
144
|
pitch: { type: 'string' },
|
|
108
145
|
textLength: { type: 'integer' },
|
|
146
|
+
chunks: { type: 'integer' },
|
|
147
|
+
subtitleFile: { type: 'string' },
|
|
109
148
|
},
|
|
110
149
|
additionalProperties: true,
|
|
111
150
|
};
|
|
@@ -117,6 +156,7 @@ const sttSchema = {
|
|
|
117
156
|
language: { type: 'string' },
|
|
118
157
|
audio: { type: 'string' },
|
|
119
158
|
transcriptFile: { type: 'string' },
|
|
159
|
+
subtitleFile: { type: 'string' },
|
|
120
160
|
},
|
|
121
161
|
additionalProperties: true,
|
|
122
162
|
};
|
|
@@ -127,21 +167,33 @@ const previewSchema = {
|
|
|
127
167
|
samples: { type: 'array', items: { type: 'object', additionalProperties: true } },
|
|
128
168
|
failed: { type: 'array', items: { type: 'object', additionalProperties: true } },
|
|
129
169
|
text: { type: 'string' },
|
|
170
|
+
indexHtml: { type: 'string' },
|
|
130
171
|
},
|
|
131
172
|
additionalProperties: true,
|
|
132
173
|
};
|
|
133
174
|
/** 自定义端点没配 asrModel 时的兜底模型名。 */
|
|
134
175
|
const FALLBACK_ASR_MODEL = 'whisper-1';
|
|
176
|
+
/** 单次 TTS 最大字符上限(长文自动分段合成)。 */
|
|
177
|
+
const MAX_TTS_CHARS = 100000;
|
|
178
|
+
/** 多角色对话/播客分段数上限。 */
|
|
179
|
+
const MAX_TTS_SEGMENTS = 50;
|
|
180
|
+
/** OpenAI 兼容 TTS 默认模型。 */
|
|
181
|
+
const FALLBACK_TTS_MODEL = 'gpt-4o-mini-tts';
|
|
182
|
+
/** ASR 直传上限(25MB)。 */
|
|
183
|
+
const MAX_ASR_BYTES = 25 * 1024 * 1024;
|
|
184
|
+
/** 零样本克隆参考音频上限(10MB)。 */
|
|
185
|
+
const MAX_REFERENCE_AUDIO_BYTES = 10 * 1024 * 1024;
|
|
135
186
|
/** 默认试听文本(中英混合,便于感知发音差异)。 */
|
|
136
187
|
export const DEFAULT_PREVIEW_TEXT = '你好,这是音色试听。Hello, this is a voice preview.';
|
|
137
188
|
/** voice_preview 同时合成的音色数上限:有界并发,避免 8 个音色串行把最坏等待拖成 8 倍超时。 */
|
|
138
189
|
const PREVIEW_CONCURRENCY = 3;
|
|
190
|
+
/** voice_tts 分段合成的并发上限。 */
|
|
191
|
+
const TTS_CHUNK_CONCURRENCY = 2;
|
|
139
192
|
/**
|
|
140
193
|
* 构建五个语音工具定义(支持静态配置对象或活引用配置 getter)。
|
|
141
194
|
*/
|
|
142
195
|
export function buildVoiceTools(config, deps = {}) {
|
|
143
196
|
const getCfg = typeof config === 'function' ? config : () => config;
|
|
144
|
-
const initialCfg = getCfg();
|
|
145
197
|
let cachedProxyUrl = '';
|
|
146
198
|
let cachedFetchImpl;
|
|
147
199
|
const getProxyFetch = (proxyUrl) => {
|
|
@@ -153,43 +205,319 @@ export function buildVoiceTools(config, deps = {}) {
|
|
|
153
205
|
}
|
|
154
206
|
return cachedFetchImpl;
|
|
155
207
|
};
|
|
208
|
+
const assertProsody = (rate, pitch) => {
|
|
209
|
+
if (!isValidProsody(rate))
|
|
210
|
+
throw new Error('语速 rate 不合法:' + rate + '。合法格式如 +10%、-2Hz、+1st。');
|
|
211
|
+
if (!isValidProsody(pitch))
|
|
212
|
+
throw new Error('音调 pitch 不合法:' + pitch + '。合法格式如 +10%、-2Hz、+1st。');
|
|
213
|
+
};
|
|
214
|
+
const assertTtsEngineReady = (cfg, useEdge) => {
|
|
215
|
+
if (useEdge)
|
|
216
|
+
return;
|
|
217
|
+
if (cfg.ttsBaseUrl === '') {
|
|
218
|
+
throw new Error('未配置 TTS 接口地址:ttsEngine=' + cfg.ttsEngine + ' 时必须在 DSH「设置 → 插件 → 语音」配置 ttsBaseUrl(保存即生效)。');
|
|
219
|
+
}
|
|
220
|
+
if (cfg.ttsApiKey === '' && !isLoopbackBaseUrl(cfg.ttsBaseUrl)) {
|
|
221
|
+
throw new Error('未配置 TTS 密钥:请在 DSH「设置 → 插件 → 语音」配置 ttsApiKey,或设置环境变量 DSH_VOICE_TTS_KEY。');
|
|
222
|
+
}
|
|
223
|
+
};
|
|
224
|
+
const assertTtsVoice = (voice, useEdge, hasReferences = false) => {
|
|
225
|
+
if (useEdge) {
|
|
226
|
+
if (!isValidVoiceId(voice)) {
|
|
227
|
+
throw new Error('音色 id 不合法:' + voice + '。请用 voice_list 查看常用音色,或在 DSH「设置 → 插件 → 语音」修改 ttsVoice(合法格式如 zh-CN-XiaoxiaoNeural)。');
|
|
228
|
+
}
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
if (voice.trim() === '' && !hasReferences)
|
|
232
|
+
throw new Error('音色 id 不合法:不能为空。');
|
|
233
|
+
};
|
|
234
|
+
const resolveUseEdge = (cfg, hasReferenceAudio) => {
|
|
235
|
+
if (deps.tts !== undefined || deps.ttsWithMeta !== undefined)
|
|
236
|
+
return true;
|
|
237
|
+
if (deps.openaiTts !== undefined && (cfg.ttsEngine !== 'edge' || hasReferenceAudio))
|
|
238
|
+
return false;
|
|
239
|
+
return cfg.ttsEngine === 'edge';
|
|
240
|
+
};
|
|
241
|
+
const resolveEffectiveVoiceSpec = (cfg, cwd, input) => {
|
|
242
|
+
let customAlias;
|
|
243
|
+
let voiceConsumedAsAlias = false;
|
|
244
|
+
let personaConsumedAsAlias = false;
|
|
245
|
+
if (input.persona !== undefined) {
|
|
246
|
+
customAlias = resolveCustomVoice(cfg.customVoices, input.persona);
|
|
247
|
+
if (customAlias !== undefined)
|
|
248
|
+
personaConsumedAsAlias = true;
|
|
249
|
+
}
|
|
250
|
+
if (customAlias === undefined && input.voice !== undefined) {
|
|
251
|
+
customAlias = resolveCustomVoice(cfg.customVoices, input.voice);
|
|
252
|
+
if (customAlias !== undefined)
|
|
253
|
+
voiceConsumedAsAlias = true;
|
|
254
|
+
}
|
|
255
|
+
let defaultAlias;
|
|
256
|
+
if (input.voice === undefined && input.persona === undefined) {
|
|
257
|
+
defaultAlias = resolveCustomVoice(cfg.customVoices, cfg.ttsVoice);
|
|
258
|
+
}
|
|
259
|
+
const activeAlias = customAlias ?? defaultAlias;
|
|
260
|
+
let voiceConsumedAsPersona = false;
|
|
261
|
+
let personaSpec = !personaConsumedAsAlias && input.persona !== undefined
|
|
262
|
+
? resolvePersona(input.persona)
|
|
263
|
+
: undefined;
|
|
264
|
+
if (personaSpec === undefined && activeAlias?.persona !== undefined) {
|
|
265
|
+
personaSpec = resolvePersona(activeAlias.persona);
|
|
266
|
+
}
|
|
267
|
+
if (personaSpec === undefined && !voiceConsumedAsAlias && input.voice !== undefined && !isValidVoiceId(input.voice)) {
|
|
268
|
+
personaSpec = resolvePersona(input.voice);
|
|
269
|
+
if (personaSpec !== undefined)
|
|
270
|
+
voiceConsumedAsPersona = true;
|
|
271
|
+
}
|
|
272
|
+
let defaultPersona;
|
|
273
|
+
if (input.voice === undefined && input.persona === undefined && activeAlias === undefined && !isValidVoiceId(cfg.ttsVoice)) {
|
|
274
|
+
defaultPersona = resolvePersona(cfg.ttsVoice);
|
|
275
|
+
}
|
|
276
|
+
const activePersona = personaSpec ?? defaultPersona;
|
|
277
|
+
const explicitRawVoice = !voiceConsumedAsAlias && !voiceConsumedAsPersona ? input.voice : undefined;
|
|
278
|
+
const effectiveInstructions = input.instructions ?? activeAlias?.instructions;
|
|
279
|
+
const effectiveReferenceAudio = input.referenceAudio ?? activeAlias?.referenceAudio;
|
|
280
|
+
const effectiveReferenceText = input.referenceText ?? activeAlias?.referenceText;
|
|
281
|
+
let references;
|
|
282
|
+
const hasReferenceAudio = effectiveReferenceAudio !== undefined && effectiveReferenceAudio.trim() !== '';
|
|
283
|
+
if (hasReferenceAudio) {
|
|
284
|
+
const refPath = assertAudioFile(effectiveReferenceAudio, cwd);
|
|
285
|
+
const size = statSync(refPath).size;
|
|
286
|
+
if (size <= 0) {
|
|
287
|
+
throw new Error('参考音频文件为空,无法用于音色克隆:' + effectiveReferenceAudio);
|
|
288
|
+
}
|
|
289
|
+
if (size > MAX_REFERENCE_AUDIO_BYTES) {
|
|
290
|
+
throw new Error('参考音频超过 10MB 上限(当前 ' + size + ' 字节),请裁剪为 5–10 秒人声片段:' + effectiveReferenceAudio);
|
|
291
|
+
}
|
|
292
|
+
const buf = readFileSync(refPath);
|
|
293
|
+
if (buf.length === 0 || buf.length > MAX_REFERENCE_AUDIO_BYTES) {
|
|
294
|
+
throw new Error('参考音频大小异常(必须为 1 字节至 10MB):' + effectiveReferenceAudio);
|
|
295
|
+
}
|
|
296
|
+
if (resolveUseEdge(cfg, true)) {
|
|
297
|
+
throw new Error('零样本参考音频克隆(referenceAudio)需要兼容克隆的 TTS 引擎:请在 DSH「设置 → 插件 → 语音」将 ttsEngine 切换为 siliconflow(CosyVoice2-0.5B)或 custom(如本地 GPT-SoVITS / IndexTTS)。');
|
|
298
|
+
}
|
|
299
|
+
references = [{
|
|
300
|
+
audioDataUri: 'data:' + mimeFromPath(refPath) + ';base64,' + buf.toString('base64'),
|
|
301
|
+
...(effectiveReferenceText !== undefined && effectiveReferenceText.trim() !== '' ? { text: effectiveReferenceText.trim() } : {}),
|
|
302
|
+
}];
|
|
303
|
+
}
|
|
304
|
+
const effectiveVoice = references !== undefined && explicitRawVoice === undefined
|
|
305
|
+
? (activeAlias?.voice ?? '')
|
|
306
|
+
: (explicitRawVoice ?? activeAlias?.voice ?? activePersona?.voice ?? cfg.ttsVoice);
|
|
307
|
+
const effectiveRate = input.rate ?? activeAlias?.rate ?? activePersona?.rate ?? cfg.ttsRate;
|
|
308
|
+
const effectivePitch = input.pitch ?? activeAlias?.pitch ?? activePersona?.pitch ?? cfg.ttsPitch;
|
|
309
|
+
const effectivePauseMs = input.pauseMs ?? activePersona?.pauseMs ?? 300;
|
|
310
|
+
return {
|
|
311
|
+
voice: effectiveVoice,
|
|
312
|
+
rate: effectiveRate,
|
|
313
|
+
pitch: effectivePitch,
|
|
314
|
+
pauseMs: effectivePauseMs,
|
|
315
|
+
...(effectiveInstructions !== undefined ? { instructions: effectiveInstructions } : {}),
|
|
316
|
+
...(references !== undefined ? { references } : {}),
|
|
317
|
+
hasReferenceAudio,
|
|
318
|
+
};
|
|
319
|
+
};
|
|
320
|
+
const maybeNormalizeAudio = async (shouldNormalize, audio, signal) => {
|
|
321
|
+
if (!shouldNormalize)
|
|
322
|
+
return audio;
|
|
323
|
+
if (deps.normalizeAudio === undefined && !hasFfmpeg()) {
|
|
324
|
+
throw new Error('开启 normalizeLoudness 需要本机安装 ffmpeg(或使用 dsh-ffmpeg)。');
|
|
325
|
+
}
|
|
326
|
+
const normalized = await raceAbort((deps.normalizeAudio ?? normalizeAudioLoudness)(audio, signal), signal);
|
|
327
|
+
throwIfAborted(signal);
|
|
328
|
+
return normalized;
|
|
329
|
+
};
|
|
330
|
+
const synthesizeChunk = async (cfg, useEdge, chunkText, voice, rate, pitch, needSubtitles, signal, instructions, references) => {
|
|
331
|
+
if (useEdge) {
|
|
332
|
+
if (!needSubtitles) {
|
|
333
|
+
const ttsFn = deps.tts ?? (deps.ttsWithMeta !== undefined
|
|
334
|
+
? async (opts, ttsDeps, timeoutMs, abortSig) => (await deps.ttsWithMeta(opts, ttsDeps, timeoutMs, abortSig)).audio
|
|
335
|
+
: synthesizeSpeech);
|
|
336
|
+
const audio = await raceAbort(ttsFn({ text: chunkText, voice, rate, pitch }, { proxyUrl: cfg.proxyUrl }, cfg.timeoutMs, signal), signal);
|
|
337
|
+
return { audio, cues: [] };
|
|
338
|
+
}
|
|
339
|
+
if (deps.ttsWithMeta !== undefined || deps.tts === undefined) {
|
|
340
|
+
const withMetaFn = deps.ttsWithMeta ?? synthesizeSpeechWithMetadata;
|
|
341
|
+
const res = await raceAbort(withMetaFn({ text: chunkText, voice, rate, pitch, boundary: 'sentence' }, { proxyUrl: cfg.proxyUrl }, cfg.timeoutMs, signal), signal);
|
|
342
|
+
const cues = res.cues.length > 0
|
|
343
|
+
? res.cues
|
|
344
|
+
: [{ index: 1, startMs: 0, endMs: Math.max(1, estimateMp3DurationMs(res.audio)), text: chunkText }];
|
|
345
|
+
return { audio: res.audio, cues };
|
|
346
|
+
}
|
|
347
|
+
const audio = await raceAbort(deps.tts({ text: chunkText, voice, rate, pitch }, { proxyUrl: cfg.proxyUrl }, cfg.timeoutMs, signal), signal);
|
|
348
|
+
return {
|
|
349
|
+
audio,
|
|
350
|
+
cues: [{ index: 1, startMs: 0, endMs: Math.max(1, estimateMp3DurationMs(audio)), text: chunkText }],
|
|
351
|
+
};
|
|
352
|
+
}
|
|
353
|
+
const openaiFn = deps.openaiTts ?? synthesizeOpenAiSpeech;
|
|
354
|
+
const audio = await raceAbort(openaiFn(cfg.ttsBaseUrl, cfg.ttsApiKey, {
|
|
355
|
+
text: chunkText,
|
|
356
|
+
voice,
|
|
357
|
+
model: cfg.ttsModel !== '' ? cfg.ttsModel : FALLBACK_TTS_MODEL,
|
|
358
|
+
speed: rateToSpeed(rate),
|
|
359
|
+
...(instructions !== undefined ? { instructions } : {}),
|
|
360
|
+
...(references !== undefined && references.length > 0 ? { references } : {}),
|
|
361
|
+
signal,
|
|
362
|
+
}, getProxyFetch(cfg.proxyUrl) ?? globalThis.fetch, cfg.timeoutMs, signal), signal);
|
|
363
|
+
return {
|
|
364
|
+
audio,
|
|
365
|
+
cues: needSubtitles
|
|
366
|
+
? [{ index: 1, startMs: 0, endMs: Math.max(1, estimateMp3DurationMs(audio)), text: chunkText }]
|
|
367
|
+
: [],
|
|
368
|
+
};
|
|
369
|
+
};
|
|
156
370
|
const voiceList = {
|
|
157
371
|
name: 'voice_list',
|
|
158
372
|
isConcurrencySafe: () => true,
|
|
159
|
-
description: '
|
|
160
|
-
parameters: compileParameters({
|
|
373
|
+
description: '列出语音合成音色与角色人设配方:默认返回内置 45+ 精选音色(涵盖童声、少年、成熟/纪录片、老年、多语种与方言),设 online=true 可实时拉取微软 Edge 全量 300+ 音色目录(失败自动回退)。同时返回内置角色人设配方(personas)与自定义音色别名(customVoices),支持按 locale/gender/tag 筛选。',
|
|
374
|
+
parameters: compileParameters({
|
|
375
|
+
locale: { type: 'string', description: '按语言/地区或名称关键字筛选(可选,如 zh-CN、en-US、粤语)。' },
|
|
376
|
+
gender: { type: 'string', enum: ['Female', 'Male'], description: '按性别筛选(可选:Female 或 Male)。' },
|
|
377
|
+
tag: { type: 'string', description: '按场景标签筛选(可选,如 podcast、multilingual、news、dialect、child、teen、mature、elder、anime、cartoon)。' },
|
|
378
|
+
online: { type: 'boolean', description: '是否联网拉取微软 Edge 全量 300+ 实时音色目录(可选,默认 false 使用内置精选目录;联网失败自动回退内置目录)。' },
|
|
379
|
+
includePersonas: { type: 'boolean', description: '是否同时返回内置角色人设配方(personas)与自定义音色别名(默认 true)。' },
|
|
380
|
+
}),
|
|
161
381
|
output: {
|
|
162
382
|
schema: listSchema,
|
|
163
383
|
render: (_args, value) => {
|
|
164
384
|
const rec = asRecord(value);
|
|
165
385
|
const voices = Array.isArray(rec.voices) ? rec.voices : [];
|
|
166
|
-
const
|
|
386
|
+
const source = typeof rec.source === 'string' ? rec.source : 'builtin';
|
|
387
|
+
const fallbackReason = typeof rec.fallbackReason === 'string' ? rec.fallbackReason : '';
|
|
388
|
+
const header = source === 'edge-online'
|
|
389
|
+
? '共 ' + voices.length + ' 个在线 Edge 音色:'
|
|
390
|
+
: source === 'builtin-fallback'
|
|
391
|
+
? '在线目录拉取失败(' + fallbackReason + '),已回退内置目录,共 ' + voices.length + ' 个常用音色:'
|
|
392
|
+
: '共 ' + voices.length + ' 个常用音色:';
|
|
393
|
+
const lines = [header];
|
|
167
394
|
for (const voice of voices) {
|
|
168
395
|
const v = asRecord(voice);
|
|
169
|
-
|
|
396
|
+
const tags = Array.isArray(v.tags) && v.tags.length > 0 ? ' [' + v.tags.join(', ') + ']' : '';
|
|
397
|
+
lines.push('- ' + v.id + '(' + v.locale + ')' + tags);
|
|
398
|
+
}
|
|
399
|
+
const personas = Array.isArray(rec.personas) ? rec.personas : [];
|
|
400
|
+
if (personas.length > 0) {
|
|
401
|
+
const summary = personas.map((p) => {
|
|
402
|
+
const item = asRecord(p);
|
|
403
|
+
return String(item.id ?? '') + '(' + String(item.label ?? '') + ')';
|
|
404
|
+
}).join('、');
|
|
405
|
+
lines.push('', '可用人设配方(persona):' + summary);
|
|
406
|
+
}
|
|
407
|
+
const customVoices = Array.isArray(rec.customVoices) ? rec.customVoices : [];
|
|
408
|
+
if (customVoices.length > 0) {
|
|
409
|
+
const summary = customVoices.map((c) => {
|
|
410
|
+
const item = asRecord(c);
|
|
411
|
+
const extras = [
|
|
412
|
+
typeof item.persona === 'string' && item.persona !== '' ? 'persona=' + item.persona : '',
|
|
413
|
+
typeof item.voice === 'string' && item.voice !== '' ? 'voice=' + item.voice : '',
|
|
414
|
+
item.hasReferenceAudio === true ? '含参考音频' : '',
|
|
415
|
+
].filter(Boolean).join(', ');
|
|
416
|
+
return String(item.name ?? '') + (extras !== '' ? '(' + extras + ')' : '');
|
|
417
|
+
}).join('、');
|
|
418
|
+
lines.push('自定义音色别名(customVoices):' + summary);
|
|
170
419
|
}
|
|
171
420
|
return [{ type: 'text', text: lines.join('\n') }];
|
|
172
421
|
},
|
|
173
422
|
},
|
|
174
|
-
async execute() {
|
|
175
|
-
|
|
423
|
+
async execute(rawArgs, exec) {
|
|
424
|
+
const cfg = getCfg();
|
|
425
|
+
const signal = execSignal(exec);
|
|
426
|
+
throwIfAborted(signal);
|
|
427
|
+
const args = asRecord(rawArgs);
|
|
428
|
+
const locale = optionalString(args, 'locale');
|
|
429
|
+
const gender = optionalString(args, 'gender');
|
|
430
|
+
const tag = optionalString(args, 'tag');
|
|
431
|
+
const hasFilter = locale !== undefined || gender !== undefined || tag !== undefined;
|
|
432
|
+
let sourceVoices = VOICES;
|
|
433
|
+
let source = 'builtin';
|
|
434
|
+
let fallbackReason = '';
|
|
435
|
+
if (args.online === true) {
|
|
436
|
+
try {
|
|
437
|
+
const fetched = await raceAbort((deps.fetchVoices ?? fetchEdgeVoices)({ fetchImpl: getProxyFetch(cfg.proxyUrl) ?? globalThis.fetch, signal }, cfg.timeoutMs), signal);
|
|
438
|
+
throwIfAborted(signal);
|
|
439
|
+
if (Array.isArray(fetched) && fetched.length > 0) {
|
|
440
|
+
sourceVoices = fetched;
|
|
441
|
+
source = 'edge-online';
|
|
442
|
+
}
|
|
443
|
+
else {
|
|
444
|
+
source = 'builtin-fallback';
|
|
445
|
+
fallbackReason = '在线音色目录返回为空';
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
catch (error) {
|
|
449
|
+
throwIfAborted(signal);
|
|
450
|
+
source = 'builtin-fallback';
|
|
451
|
+
fallbackReason = error instanceof Error ? error.message : String(error);
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
const voices = hasFilter ? filterVoices({ locale, gender, tag }, sourceVoices) : sourceVoices;
|
|
455
|
+
const personas = args.includePersonas === false ? [] : VOICE_PERSONAS;
|
|
456
|
+
const customVoices = cfg.customVoices.map((c) => ({
|
|
457
|
+
name: c.name,
|
|
458
|
+
voice: c.voice ?? '',
|
|
459
|
+
persona: c.persona ?? '',
|
|
460
|
+
hasReferenceAudio: Boolean(c.referenceAudio),
|
|
461
|
+
}));
|
|
462
|
+
return {
|
|
463
|
+
count: voices.length,
|
|
464
|
+
voices,
|
|
465
|
+
source,
|
|
466
|
+
personas,
|
|
467
|
+
customVoices,
|
|
468
|
+
...(fallbackReason !== '' ? { fallbackReason } : {}),
|
|
469
|
+
};
|
|
176
470
|
},
|
|
177
471
|
};
|
|
178
472
|
const voiceTts = {
|
|
179
473
|
name: 'voice_tts',
|
|
180
|
-
|
|
474
|
+
isConcurrencySafe: () => !getCfg().overwrite,
|
|
475
|
+
description: '文字转语音:默认调用 edge-tts(微软 Edge 朗读服务,免费)合成 MP3,也可在设置中切换 OpenAI/SiliconFlow/自定义 TTS 引擎。支持内置角色人设 persona、自然语言情绪指令 instructions、5–10 秒参考音频零样本克隆 referenceAudio、多角色播客 segments、读取工作区 inputFile(自动清理 Markdown)、长文自动分段(≤100000 字符)、导出 .srt/.vtt 字幕与 -16 LUFS 响度均衡 normalizeLoudness;输出默认 voice_output.mp3。',
|
|
181
476
|
parameters: compileParameters({
|
|
182
|
-
text: { type: 'string',
|
|
183
|
-
|
|
477
|
+
text: { type: 'string', description: '要合成的文本(若提供 inputFile 或 segments 则可选;支持长文本自动分段,≤100000 字符)。' },
|
|
478
|
+
inputFile: { type: 'string', description: '工作区内文稿文件路径(可选,如 article.md;指定时直接从文件读取)。' },
|
|
479
|
+
cleanMarkdown: { type: 'boolean', description: '是否清理 Markdown 语法以便朗读(可选;inputFile 为 .md/.markdown 时默认 true)。' },
|
|
480
|
+
segments: {
|
|
481
|
+
type: 'array',
|
|
482
|
+
description: '多角色对话/播客分段(可选,最多 50 段):每段可独立指定 voice/persona/text/rate/pitch/pauseMs/instructions/referenceAudio/referenceText。',
|
|
483
|
+
items: {
|
|
484
|
+
type: 'object',
|
|
485
|
+
properties: {
|
|
486
|
+
voice: { type: 'string' },
|
|
487
|
+
persona: { type: 'string' },
|
|
488
|
+
text: { type: 'string' },
|
|
489
|
+
rate: { type: 'string' },
|
|
490
|
+
pitch: { type: 'string' },
|
|
491
|
+
pauseMs: { type: 'number' },
|
|
492
|
+
instructions: { type: 'string' },
|
|
493
|
+
referenceAudio: { type: 'string' },
|
|
494
|
+
referenceText: { type: 'string' },
|
|
495
|
+
},
|
|
496
|
+
required: ['text'],
|
|
497
|
+
},
|
|
498
|
+
},
|
|
499
|
+
voice: { type: 'string', description: '音色 id 或自定义音色别名(可选,默认配置的 ttsVoice)。' },
|
|
500
|
+
persona: { type: 'string', description: '内置角色人设 ID/标签(如 late-night-radio、anime-boy)或自定义音色别名(可选)。' },
|
|
184
501
|
rate: { type: 'string', description: '语速,如 +20% 或 -10%(可选)。' },
|
|
185
502
|
pitch: { type: 'string', description: '音调,如 +2Hz 或 -1Hz(可选)。' },
|
|
503
|
+
instructions: { type: 'string', description: '自然语言情绪/语气指令(可选,OpenAI gpt-4o-mini-tts 与 SiliconFlow CosyVoice2 可用,如“用开心轻快的语气说”,支持 [laughter] / [breath])。' },
|
|
504
|
+
referenceAudio: { type: 'string', description: '零样本克隆参考音频路径(可选,会话工作区内 5–10 秒干净人声,≤10MB;需 siliconflow 或 custom 引擎)。' },
|
|
505
|
+
referenceText: { type: 'string', description: '参考音频对应的原始台词文本(可选,零样本克隆推荐填写)。' },
|
|
506
|
+
normalizeLoudness: { type: 'boolean', default: false, description: '是否在合成后通过 ffmpeg 执行 -16 LUFS 播客响度均衡(可选,默认 false)。' },
|
|
186
507
|
output: { type: 'string', description: '输出 MP3 路径(可选,相对路径按会话工作目录解析)。' },
|
|
508
|
+
subtitleOutput: { type: 'string', description: '导出同步字幕文件路径(可选,.srt 或 .vtt,相对路径按会话工作目录解析)。' },
|
|
187
509
|
}),
|
|
188
510
|
output: {
|
|
189
511
|
schema: ttsSchema,
|
|
190
512
|
render: (_args, value) => {
|
|
191
513
|
const rec = asRecord(value);
|
|
192
|
-
|
|
514
|
+
const chunksInfo = typeof rec.chunks === 'number' && rec.chunks > 1 ? ',共 ' + rec.chunks + ' 段' : '';
|
|
515
|
+
const voiceLabel = typeof rec.voice === 'string' && rec.voice !== '' ? rec.voice : '参考音频克隆';
|
|
516
|
+
const lines = ['语音合成完成:' + rec.output + '(' + rec.bytes + ' 字节,音色 ' + voiceLabel + chunksInfo + ')'];
|
|
517
|
+
const subtitleFile = typeof rec.subtitleFile === 'string' ? rec.subtitleFile : '';
|
|
518
|
+
if (subtitleFile !== '')
|
|
519
|
+
lines.push('字幕文件已写入:' + subtitleFile);
|
|
520
|
+
return [{ type: 'text', text: lines.join('\n') }];
|
|
193
521
|
},
|
|
194
522
|
},
|
|
195
523
|
async execute(rawArgs, exec) {
|
|
@@ -197,30 +525,249 @@ export function buildVoiceTools(config, deps = {}) {
|
|
|
197
525
|
const signal = execSignal(exec);
|
|
198
526
|
throwIfAborted(signal);
|
|
199
527
|
const args = asRecord(rawArgs);
|
|
200
|
-
const
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
const
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
const
|
|
207
|
-
const
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
528
|
+
const cwd = sessionCwd(exec);
|
|
529
|
+
const topVoice = optionalString(args, 'voice');
|
|
530
|
+
const topPersona = optionalString(args, 'persona');
|
|
531
|
+
const topRate = optionalString(args, 'rate');
|
|
532
|
+
const topPitch = optionalString(args, 'pitch');
|
|
533
|
+
const topInstructions = optionalString(args, 'instructions');
|
|
534
|
+
const topReferenceAudio = optionalString(args, 'referenceAudio');
|
|
535
|
+
const topReferenceText = optionalString(args, 'referenceText');
|
|
536
|
+
const normalizeLoudness = args.normalizeLoudness === true;
|
|
537
|
+
const requested = optionalString(args, 'output');
|
|
538
|
+
const subtitleOutput = optionalString(args, 'subtitleOutput');
|
|
539
|
+
const needSubtitles = subtitleOutput !== undefined;
|
|
540
|
+
const rawSegments = Array.isArray(args.segments) && args.segments.length > 0 ? args.segments : undefined;
|
|
541
|
+
if (rawSegments !== undefined) {
|
|
542
|
+
if (rawSegments.length > MAX_TTS_SEGMENTS) {
|
|
543
|
+
throw new Error('segments 最多 ' + MAX_TTS_SEGMENTS + ' 段(当前 ' + rawSegments.length + ' 段)。');
|
|
544
|
+
}
|
|
545
|
+
const topSpec = resolveEffectiveVoiceSpec(cfg, cwd, {
|
|
546
|
+
voice: topVoice,
|
|
547
|
+
persona: topPersona,
|
|
548
|
+
rate: topRate,
|
|
549
|
+
pitch: topPitch,
|
|
550
|
+
instructions: topInstructions,
|
|
551
|
+
referenceAudio: topReferenceAudio,
|
|
552
|
+
referenceText: topReferenceText,
|
|
553
|
+
});
|
|
554
|
+
const parsedSegments = [];
|
|
555
|
+
let totalChars = 0;
|
|
556
|
+
for (let i = 0; i < rawSegments.length; i++) {
|
|
557
|
+
const seg = asRecord(rawSegments[i]);
|
|
558
|
+
const rawSegText = optionalString(seg, 'text');
|
|
559
|
+
if (rawSegText === undefined)
|
|
560
|
+
throw new Error('第 ' + (i + 1) + ' 段 text 不能为空。');
|
|
561
|
+
const segText = args.cleanMarkdown === true ? cleanMarkdownForSpeech(rawSegText).trim() : rawSegText;
|
|
562
|
+
if (segText === '')
|
|
563
|
+
throw new Error('第 ' + (i + 1) + ' 段清理 Markdown 后文本为空。');
|
|
564
|
+
totalChars += segText.length;
|
|
565
|
+
const segVoice = optionalString(seg, 'voice');
|
|
566
|
+
const segPersona = optionalString(seg, 'persona');
|
|
567
|
+
const segRate = optionalString(seg, 'rate');
|
|
568
|
+
const segPitch = optionalString(seg, 'pitch');
|
|
569
|
+
const segPauseMs = typeof seg.pauseMs === 'number' && Number.isFinite(seg.pauseMs) && seg.pauseMs >= 0
|
|
570
|
+
? Math.min(10000, Math.round(seg.pauseMs))
|
|
571
|
+
: undefined;
|
|
572
|
+
const segInstructions = optionalString(seg, 'instructions');
|
|
573
|
+
const segReferenceAudio = optionalString(seg, 'referenceAudio');
|
|
574
|
+
const segReferenceText = optionalString(seg, 'referenceText');
|
|
575
|
+
let segSpec;
|
|
576
|
+
if (segVoice !== undefined || segPersona !== undefined) {
|
|
577
|
+
segSpec = resolveEffectiveVoiceSpec(cfg, cwd, {
|
|
578
|
+
voice: segVoice,
|
|
579
|
+
persona: segPersona,
|
|
580
|
+
rate: segRate ?? topRate,
|
|
581
|
+
pitch: segPitch ?? topPitch,
|
|
582
|
+
pauseMs: segPauseMs,
|
|
583
|
+
instructions: segInstructions ?? topInstructions,
|
|
584
|
+
referenceAudio: segReferenceAudio ?? topReferenceAudio,
|
|
585
|
+
referenceText: segReferenceText ?? topReferenceText,
|
|
586
|
+
});
|
|
587
|
+
}
|
|
588
|
+
else if (segReferenceAudio !== undefined) {
|
|
589
|
+
segSpec = resolveEffectiveVoiceSpec(cfg, cwd, {
|
|
590
|
+
voice: topVoice,
|
|
591
|
+
persona: topPersona,
|
|
592
|
+
rate: segRate ?? topRate,
|
|
593
|
+
pitch: segPitch ?? topPitch,
|
|
594
|
+
pauseMs: segPauseMs,
|
|
595
|
+
instructions: segInstructions ?? topInstructions,
|
|
596
|
+
referenceAudio: segReferenceAudio,
|
|
597
|
+
referenceText: segReferenceText ?? topReferenceText,
|
|
598
|
+
});
|
|
599
|
+
}
|
|
600
|
+
else {
|
|
601
|
+
const reusedRefs = topSpec.references !== undefined && segReferenceText !== undefined
|
|
602
|
+
? topSpec.references.map((r) => ({ audioDataUri: r.audioDataUri, text: segReferenceText }))
|
|
603
|
+
: topSpec.references;
|
|
604
|
+
segSpec = {
|
|
605
|
+
voice: topSpec.voice,
|
|
606
|
+
rate: segRate ?? topSpec.rate,
|
|
607
|
+
pitch: segPitch ?? topSpec.pitch,
|
|
608
|
+
pauseMs: segPauseMs ?? topSpec.pauseMs,
|
|
609
|
+
...(segInstructions !== undefined || topSpec.instructions !== undefined
|
|
610
|
+
? { instructions: segInstructions ?? topSpec.instructions }
|
|
611
|
+
: {}),
|
|
612
|
+
...(reusedRefs !== undefined ? { references: reusedRefs } : {}),
|
|
613
|
+
hasReferenceAudio: topSpec.hasReferenceAudio,
|
|
614
|
+
};
|
|
615
|
+
}
|
|
616
|
+
const segUseEdge = resolveUseEdge(cfg, segSpec.hasReferenceAudio);
|
|
617
|
+
assertTtsEngineReady(cfg, segUseEdge);
|
|
618
|
+
assertTtsVoice(segSpec.voice, segUseEdge, segSpec.references !== undefined);
|
|
619
|
+
assertProsody(segSpec.rate, segSpec.pitch);
|
|
620
|
+
const chunks = segText.length > 4000 ? splitTextIntoChunks(segText, 2800) : [segText];
|
|
621
|
+
parsedSegments.push({
|
|
622
|
+
chunks,
|
|
623
|
+
voice: segSpec.voice,
|
|
624
|
+
rate: segSpec.rate,
|
|
625
|
+
pitch: segSpec.pitch,
|
|
626
|
+
pauseMs: segSpec.pauseMs,
|
|
627
|
+
useEdge: segUseEdge,
|
|
628
|
+
instructions: segSpec.instructions,
|
|
629
|
+
references: segSpec.references,
|
|
630
|
+
});
|
|
631
|
+
}
|
|
632
|
+
if (totalChars > MAX_TTS_CHARS) {
|
|
633
|
+
throw new Error('segments 总文本超过 ' + MAX_TTS_CHARS + ' 字符上限(当前 ' + totalChars + ' 字符)。');
|
|
634
|
+
}
|
|
635
|
+
resolveOutputPath(requested, 'voice_output.mp3', cfg.overwrite, cwd);
|
|
636
|
+
if (subtitleOutput !== undefined)
|
|
637
|
+
resolveOutputPath(subtitleOutput, '', cfg.overwrite, cwd);
|
|
638
|
+
const tasks = [];
|
|
639
|
+
for (let s = 0; s < parsedSegments.length; s++) {
|
|
640
|
+
const seg = parsedSegments[s];
|
|
641
|
+
for (const chunkText of seg.chunks) {
|
|
642
|
+
tasks.push({
|
|
643
|
+
segIndex: s,
|
|
644
|
+
text: chunkText,
|
|
645
|
+
voice: seg.voice,
|
|
646
|
+
rate: seg.rate,
|
|
647
|
+
pitch: seg.pitch,
|
|
648
|
+
useEdge: seg.useEdge,
|
|
649
|
+
instructions: seg.instructions,
|
|
650
|
+
references: seg.references,
|
|
651
|
+
});
|
|
652
|
+
}
|
|
653
|
+
}
|
|
654
|
+
const chunkResults = await mapLimit(tasks, TTS_CHUNK_CONCURRENCY, signal, (task) => synthesizeChunk(cfg, task.useEdge, task.text, task.voice, task.rate, task.pitch, needSubtitles, signal, task.instructions, task.references));
|
|
655
|
+
throwIfAborted(signal);
|
|
656
|
+
const buffers = [];
|
|
657
|
+
const allCues = [];
|
|
658
|
+
let offsetMs = 0;
|
|
659
|
+
let cueIndex = 1;
|
|
660
|
+
let resultIdx = 0;
|
|
661
|
+
for (let s = 0; s < parsedSegments.length; s++) {
|
|
662
|
+
const seg = parsedSegments[s];
|
|
663
|
+
for (let c = 0; c < seg.chunks.length; c++) {
|
|
664
|
+
const res = chunkResults[resultIdx++];
|
|
665
|
+
buffers.push(res.audio);
|
|
666
|
+
const chunkDurationMs = estimateMp3DurationMs(res.audio);
|
|
667
|
+
if (needSubtitles) {
|
|
668
|
+
const shifted = offsetCues(res.cues, offsetMs, cueIndex);
|
|
669
|
+
allCues.push(...shifted);
|
|
670
|
+
cueIndex += shifted.length;
|
|
671
|
+
const cueSpan = shifted.length > 0 ? Math.max(0, shifted[shifted.length - 1].endMs - offsetMs) : 0;
|
|
672
|
+
offsetMs += Math.max(chunkDurationMs, cueSpan);
|
|
673
|
+
}
|
|
674
|
+
}
|
|
675
|
+
if (s < parsedSegments.length - 1 && seg.pauseMs > 0) {
|
|
676
|
+
const silence = createSilenceMp3(seg.pauseMs);
|
|
677
|
+
if (silence.length > 0) {
|
|
678
|
+
buffers.push(silence);
|
|
679
|
+
offsetMs += estimateMp3DurationMs(silence);
|
|
680
|
+
}
|
|
681
|
+
}
|
|
682
|
+
}
|
|
683
|
+
let audio = buffers.length === 1 ? buffers[0] : concatMp3Buffers(buffers);
|
|
684
|
+
audio = await maybeNormalizeAudio(normalizeLoudness, audio, signal);
|
|
685
|
+
const output = writeWorkspaceFile(resolveOutputPath(requested, 'voice_output.mp3', cfg.overwrite, cwd), audio, cwd, undefined, cfg.overwrite);
|
|
686
|
+
let subtitleFile = '';
|
|
687
|
+
if (subtitleOutput !== undefined) {
|
|
688
|
+
const formatted = formatSubtitles(allCues, subtitleFormatOf(subtitleOutput));
|
|
689
|
+
subtitleFile = writeWorkspaceFile(resolveOutputPath(subtitleOutput, '', cfg.overwrite, cwd), formatted, cwd, undefined, cfg.overwrite);
|
|
690
|
+
}
|
|
691
|
+
return { output, bytes: audio.length, voice: topSpec.voice, rate: topSpec.rate, pitch: topSpec.pitch, textLength: totalChars, chunks: tasks.length, subtitleFile };
|
|
692
|
+
}
|
|
693
|
+
const inputFile = optionalString(args, 'inputFile');
|
|
694
|
+
let finalText;
|
|
695
|
+
if (inputFile !== undefined) {
|
|
696
|
+
const resolvedInput = assertWorkspaceFile(inputFile, cwd, '文稿文件');
|
|
697
|
+
const rawText = readFileSync(resolvedInput, 'utf8');
|
|
698
|
+
const shouldClean = typeof args.cleanMarkdown === 'boolean'
|
|
699
|
+
? args.cleanMarkdown
|
|
700
|
+
: /\.(md|markdown)$/i.test(resolvedInput);
|
|
701
|
+
finalText = (shouldClean ? cleanMarkdownForSpeech(rawText) : rawText).trim();
|
|
702
|
+
if (finalText === '') {
|
|
703
|
+
throw new Error(shouldClean ? '文稿文件在清理 Markdown 后内容为空。' : '文稿文件内容为空。');
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
else {
|
|
707
|
+
const rawText = requiredString(args, 'text', '要合成的文本');
|
|
708
|
+
finalText = args.cleanMarkdown === true ? cleanMarkdownForSpeech(rawText).trim() : rawText;
|
|
709
|
+
if (finalText === '')
|
|
710
|
+
throw new Error('要合成的文本在清理 Markdown 后为空。');
|
|
211
711
|
}
|
|
212
|
-
|
|
712
|
+
if (finalText.length > MAX_TTS_CHARS) {
|
|
713
|
+
throw new Error('文本超过 ' + MAX_TTS_CHARS + ' 字符上限(当前 ' + finalText.length + ' 字符),请拆分处理。');
|
|
714
|
+
}
|
|
715
|
+
const spec = resolveEffectiveVoiceSpec(cfg, cwd, {
|
|
716
|
+
voice: topVoice,
|
|
717
|
+
persona: topPersona,
|
|
718
|
+
rate: topRate,
|
|
719
|
+
pitch: topPitch,
|
|
720
|
+
instructions: topInstructions,
|
|
721
|
+
referenceAudio: topReferenceAudio,
|
|
722
|
+
referenceText: topReferenceText,
|
|
723
|
+
});
|
|
724
|
+
const useEdge = resolveUseEdge(cfg, spec.hasReferenceAudio);
|
|
725
|
+
assertTtsEngineReady(cfg, useEdge);
|
|
726
|
+
assertTtsVoice(spec.voice, useEdge, spec.references !== undefined);
|
|
727
|
+
assertProsody(spec.rate, spec.pitch);
|
|
213
728
|
resolveOutputPath(requested, 'voice_output.mp3', cfg.overwrite, cwd);
|
|
214
|
-
|
|
729
|
+
if (subtitleOutput !== undefined)
|
|
730
|
+
resolveOutputPath(subtitleOutput, '', cfg.overwrite, cwd);
|
|
731
|
+
let audio;
|
|
732
|
+
let chunkCount = 1;
|
|
733
|
+
let subtitleFile = '';
|
|
734
|
+
if (finalText.length <= 5000 && !needSubtitles && useEdge) {
|
|
735
|
+
const res = await synthesizeChunk(cfg, true, finalText, spec.voice, spec.rate, spec.pitch, false, signal, spec.instructions, spec.references);
|
|
736
|
+
audio = res.audio;
|
|
737
|
+
}
|
|
738
|
+
else {
|
|
739
|
+
const chunks = splitTextIntoChunks(finalText, 2800);
|
|
740
|
+
chunkCount = chunks.length;
|
|
741
|
+
const results = await mapLimit(chunks, TTS_CHUNK_CONCURRENCY, signal, (chunk) => synthesizeChunk(cfg, useEdge, chunk, spec.voice, spec.rate, spec.pitch, needSubtitles, signal, spec.instructions, spec.references));
|
|
742
|
+
throwIfAborted(signal);
|
|
743
|
+
audio = results.length === 1 ? results[0].audio : concatMp3Buffers(results.map((r) => r.audio));
|
|
744
|
+
if (subtitleOutput !== undefined) {
|
|
745
|
+
const allCues = [];
|
|
746
|
+
let offsetMs = 0;
|
|
747
|
+
let cueIndex = 1;
|
|
748
|
+
for (const res of results) {
|
|
749
|
+
const shifted = offsetCues(res.cues, offsetMs, cueIndex);
|
|
750
|
+
allCues.push(...shifted);
|
|
751
|
+
cueIndex += shifted.length;
|
|
752
|
+
const chunkDurationMs = estimateMp3DurationMs(res.audio);
|
|
753
|
+
const cueSpan = shifted.length > 0 ? Math.max(0, shifted[shifted.length - 1].endMs - offsetMs) : 0;
|
|
754
|
+
offsetMs += Math.max(chunkDurationMs, cueSpan);
|
|
755
|
+
}
|
|
756
|
+
const formatted = formatSubtitles(allCues, subtitleFormatOf(subtitleOutput));
|
|
757
|
+
subtitleFile = writeWorkspaceFile(resolveOutputPath(subtitleOutput, '', cfg.overwrite, cwd), formatted, cwd, undefined, cfg.overwrite);
|
|
758
|
+
}
|
|
759
|
+
}
|
|
215
760
|
throwIfAborted(signal);
|
|
761
|
+
audio = await maybeNormalizeAudio(normalizeLoudness, audio, signal);
|
|
216
762
|
const output = writeWorkspaceFile(resolveOutputPath(requested, 'voice_output.mp3', cfg.overwrite, cwd), audio, cwd, undefined, cfg.overwrite);
|
|
217
|
-
return { output, bytes: audio.length, voice, rate, pitch, textLength:
|
|
763
|
+
return { output, bytes: audio.length, voice: spec.voice, rate: spec.rate, pitch: spec.pitch, textLength: finalText.length, chunks: chunkCount, subtitleFile };
|
|
218
764
|
},
|
|
219
|
-
timeoutMs
|
|
765
|
+
get timeoutMs() { return getCfg().timeoutMs + 10000; },
|
|
220
766
|
};
|
|
221
767
|
const voiceStt = {
|
|
222
768
|
name: 'voice_stt',
|
|
223
|
-
|
|
769
|
+
isConcurrencySafe: () => !getCfg().overwrite,
|
|
770
|
+
description: '语音转文字:调用 OpenAI 兼容 ASR 接口(默认 Groq whisper-large-v3-turbo;可切 openai/siliconflow/custom)。audio 为音频文件路径(mp3/wav/m4a/ogg/flac,≤25MB;超过 25MB 可设 autoCompress=true 用 ffmpeg 自动压缩分段);language/prompt 可选;output 可写 .txt,subtitleOutput 可写 .srt/.vtt 字幕。',
|
|
224
771
|
parameters: compileParameters({
|
|
225
772
|
audio: { type: 'string', required: true, description: '音频文件路径(必填)。' },
|
|
226
773
|
engine: { type: 'string', enum: ['groq', 'openai', 'custom'], description: '引擎(可选):只接受与配置 asrEngine 相同的值,用于显式确认;接口地址与密钥始终取配置。' },
|
|
@@ -228,6 +775,9 @@ export function buildVoiceTools(config, deps = {}) {
|
|
|
228
775
|
language: { type: 'string', description: '语言提示,如 zh(可选)。' },
|
|
229
776
|
prompt: { type: 'string', description: '提示词(专有名词/术语纠偏,可选)。' },
|
|
230
777
|
output: { type: 'string', description: '把转写文本写成 .txt 的路径(可选,相对路径按会话工作目录解析)。' },
|
|
778
|
+
subtitleOutput: { type: 'string', description: '导出字幕路径(可选,.srt 或 .vtt,相对路径按会话工作目录解析;指定时自动开启时间戳)。' },
|
|
779
|
+
timestamps: { type: 'boolean', description: '是否请求分段时间戳(可选,默认 false)。' },
|
|
780
|
+
autoCompress: { type: 'boolean', description: '超过 25MB 时是否自动调用本地 ffmpeg 压缩与分段转写(可选,默认 false)。' },
|
|
231
781
|
}),
|
|
232
782
|
output: {
|
|
233
783
|
schema: sttSchema,
|
|
@@ -238,6 +788,9 @@ export function buildVoiceTools(config, deps = {}) {
|
|
|
238
788
|
const saved = typeof rec.transcriptFile === 'string' ? rec.transcriptFile : '';
|
|
239
789
|
if (saved !== '')
|
|
240
790
|
lines.push('', '转写文本已写入:' + saved);
|
|
791
|
+
const subSaved = typeof rec.subtitleFile === 'string' ? rec.subtitleFile : '';
|
|
792
|
+
if (subSaved !== '')
|
|
793
|
+
lines.push('字幕文件已写入:' + subSaved);
|
|
241
794
|
return [{ type: 'text', text: lines.join('\n') }];
|
|
242
795
|
},
|
|
243
796
|
},
|
|
@@ -249,49 +802,154 @@ export function buildVoiceTools(config, deps = {}) {
|
|
|
249
802
|
const cwd = sessionCwd(exec);
|
|
250
803
|
const audioPath = assertAudioFile(requiredString(args, 'audio', '音频文件'), cwd);
|
|
251
804
|
const requestedEngine = optionalString(args, 'engine');
|
|
252
|
-
if (requestedEngine !== undefined && !['groq', 'openai', 'custom'].includes(requestedEngine)) {
|
|
805
|
+
if (requestedEngine !== undefined && !['groq', 'openai', 'siliconflow', 'custom'].includes(requestedEngine)) {
|
|
253
806
|
throw new Error('engine 必须是 groq / openai / custom 之一(当前:' + requestedEngine + ')。');
|
|
254
807
|
}
|
|
255
808
|
if (requestedEngine !== undefined && requestedEngine !== cfg.asrEngine) {
|
|
256
809
|
throw new Error('voice_stt 不支持用 engine 参数临时换引擎(配置 asrEngine=' + cfg.asrEngine + ',本次传入 ' + requestedEngine + ')。接口地址与密钥是一组配置,混着用会把密钥发到非预期的服务;如需切换请在 DSH「设置 → 插件 → 语音」修改 asrEngine / asrBaseUrl(保存即生效)。');
|
|
257
810
|
}
|
|
258
|
-
// 接口地址一律取配置:openai/groq 预设已由 resolveConfig 回填,自定义端点原样使用。
|
|
811
|
+
// 接口地址一律取配置:openai/groq/siliconflow 预设已由 resolveConfig 回填,自定义端点原样使用。
|
|
259
812
|
const baseUrl = cfg.asrBaseUrl;
|
|
260
813
|
if (baseUrl === '')
|
|
261
814
|
throw new Error('未配置 ASR 接口地址:asrEngine=custom 时必须在 DSH「设置 → 插件 → 语音」或配置里提供 asrBaseUrl(保存即生效)。');
|
|
262
|
-
|
|
263
|
-
const
|
|
264
|
-
|
|
265
|
-
|
|
815
|
+
const output = optionalString(args, 'output');
|
|
816
|
+
const subtitleOutput = optionalString(args, 'subtitleOutput');
|
|
817
|
+
if (output !== undefined)
|
|
818
|
+
resolveOutputPath(output, '', cfg.overwrite, cwd);
|
|
819
|
+
if (subtitleOutput !== undefined)
|
|
820
|
+
resolveOutputPath(subtitleOutput, '', cfg.overwrite, cwd);
|
|
266
821
|
const model = optionalString(args, 'model') ?? (cfg.asrModel !== '' ? cfg.asrModel : FALLBACK_ASR_MODEL);
|
|
267
|
-
const
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
822
|
+
const language = optionalString(args, 'language');
|
|
823
|
+
const prompt = optionalString(args, 'prompt');
|
|
824
|
+
const needTimestamps = args.timestamps === true || subtitleOutput !== undefined;
|
|
825
|
+
const allowEmptyApiKey = cfg.asrEngine === 'custom' && isLoopbackBaseUrl(baseUrl);
|
|
826
|
+
const fetchImpl = getProxyFetch(cfg.proxyUrl) ?? globalThis.fetch;
|
|
827
|
+
const sttFn = deps.stt ?? transcribe;
|
|
828
|
+
const fileSize = statSync(audioPath).size;
|
|
829
|
+
let text = '';
|
|
830
|
+
let detectedLanguage = '';
|
|
831
|
+
let duration;
|
|
832
|
+
let segments;
|
|
833
|
+
if (fileSize > MAX_ASR_BYTES) {
|
|
834
|
+
if (args.autoCompress !== true)
|
|
835
|
+
assertAudioSize(fileSize);
|
|
836
|
+
if (deps.prepareAudio === undefined && !hasFfmpeg()) {
|
|
837
|
+
throw new Error('音频超过 25MB,且未检测到 ffmpeg;请先安装 ffmpeg(或使用 dsh-ffmpeg 压缩),再启用 autoCompress=true。');
|
|
838
|
+
}
|
|
839
|
+
const prep = await raceAbort((deps.prepareAudio ?? prepareAudioForAsr)(audioPath, signal), signal);
|
|
840
|
+
try {
|
|
841
|
+
const chunkResults = await mapLimit(prep.chunks, 2, signal, async (chunk) => {
|
|
842
|
+
const res = await raceAbort(sttFn(baseUrl, cfg.asrApiKey, {
|
|
843
|
+
audio: chunk.audio,
|
|
844
|
+
filename: chunk.filename,
|
|
845
|
+
model,
|
|
846
|
+
language,
|
|
847
|
+
prompt,
|
|
848
|
+
timestamps: needTimestamps,
|
|
849
|
+
allowEmptyApiKey,
|
|
850
|
+
}, fetchImpl, cfg.timeoutMs, signal), signal);
|
|
851
|
+
return { chunk, res };
|
|
852
|
+
});
|
|
853
|
+
throwIfAborted(signal);
|
|
854
|
+
text = chunkResults.map((item) => item.res.text.trim()).filter(Boolean).join('\n');
|
|
855
|
+
detectedLanguage = chunkResults.find((item) => typeof item.res.language === 'string' && item.res.language !== '')?.res.language ?? '';
|
|
856
|
+
let totalDuration = 0;
|
|
857
|
+
let hasDuration = false;
|
|
858
|
+
const mergedSegments = [];
|
|
859
|
+
let segId = 1;
|
|
860
|
+
for (const { chunk, res } of chunkResults) {
|
|
861
|
+
if (typeof res.duration === 'number' && Number.isFinite(res.duration)) {
|
|
862
|
+
totalDuration = Math.max(totalDuration, chunk.offsetSeconds + res.duration);
|
|
863
|
+
hasDuration = true;
|
|
864
|
+
}
|
|
865
|
+
if (Array.isArray(res.segments) && res.segments.length > 0) {
|
|
866
|
+
for (const seg of res.segments) {
|
|
867
|
+
mergedSegments.push({
|
|
868
|
+
id: segId++,
|
|
869
|
+
start: seg.start + chunk.offsetSeconds,
|
|
870
|
+
end: seg.end + chunk.offsetSeconds,
|
|
871
|
+
text: seg.text,
|
|
872
|
+
});
|
|
873
|
+
}
|
|
874
|
+
}
|
|
875
|
+
}
|
|
876
|
+
if (hasDuration)
|
|
877
|
+
duration = totalDuration;
|
|
878
|
+
if (mergedSegments.length > 0)
|
|
879
|
+
segments = mergedSegments;
|
|
880
|
+
}
|
|
881
|
+
finally {
|
|
882
|
+
prep.cleanup();
|
|
883
|
+
}
|
|
884
|
+
}
|
|
885
|
+
else {
|
|
886
|
+
let audioInput;
|
|
887
|
+
if (deps.stt !== undefined) {
|
|
888
|
+
// 保持单元测试兼容性:注入假 stt 时传 Buffer(部分测试会对 captured.audio 做扩展运算符迭代)。
|
|
889
|
+
const buf = readFileSync(audioPath);
|
|
890
|
+
assertAudioSize(buf.length);
|
|
891
|
+
audioInput = buf;
|
|
892
|
+
}
|
|
893
|
+
else {
|
|
894
|
+
let blob = await openAsBlob(audioPath, { type: mimeFromPath(audioPath) });
|
|
895
|
+
// 伪文件(如 procfs)size 报 0 字节,回退到 readFileSync 再判一次。
|
|
896
|
+
if (blob.size === 0)
|
|
897
|
+
blob = readFileSync(audioPath);
|
|
898
|
+
assertAudioSize(Buffer.isBuffer(blob) ? blob.length : blob.size);
|
|
899
|
+
audioInput = blob;
|
|
900
|
+
}
|
|
901
|
+
const res = await raceAbort(sttFn(baseUrl, cfg.asrApiKey, {
|
|
902
|
+
audio: audioInput,
|
|
903
|
+
filename: basename(audioPath),
|
|
904
|
+
model,
|
|
905
|
+
language,
|
|
906
|
+
prompt,
|
|
907
|
+
timestamps: needTimestamps,
|
|
908
|
+
allowEmptyApiKey,
|
|
909
|
+
}, fetchImpl, cfg.timeoutMs, signal), signal);
|
|
910
|
+
throwIfAborted(signal);
|
|
911
|
+
text = res.text;
|
|
912
|
+
detectedLanguage = res.language ?? '';
|
|
913
|
+
duration = res.duration;
|
|
914
|
+
segments = res.segments;
|
|
915
|
+
}
|
|
275
916
|
let transcriptFile = '';
|
|
276
|
-
const output = optionalString(args, 'output');
|
|
277
917
|
if (output !== undefined) {
|
|
278
918
|
const target = resolveOutputPath(output, '', cfg.overwrite, cwd);
|
|
279
919
|
// 转写文本可能含隐私内容,只用 0600 写入。
|
|
280
920
|
transcriptFile = writeWorkspaceFile(target, text, cwd, 0o600, cfg.overwrite);
|
|
281
921
|
}
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
922
|
+
let subtitleFile = '';
|
|
923
|
+
if (subtitleOutput !== undefined) {
|
|
924
|
+
const cueSegments = segments !== undefined && segments.length > 0
|
|
925
|
+
? segments
|
|
926
|
+
: [{ id: 1, start: 0, end: duration !== undefined && duration > 0 ? duration : 5, text }];
|
|
927
|
+
const subText = formatSttSubtitles(cueSegments, subtitleFormatOf(subtitleOutput));
|
|
928
|
+
subtitleFile = writeWorkspaceFile(resolveOutputPath(subtitleOutput, '', cfg.overwrite, cwd), subText, cwd, 0o600, cfg.overwrite);
|
|
929
|
+
}
|
|
930
|
+
// The output schema declares string fields — never return null, or dsh rejects the whole tool result.
|
|
931
|
+
return {
|
|
932
|
+
text,
|
|
933
|
+
model,
|
|
934
|
+
language: language ?? detectedLanguage,
|
|
935
|
+
audio: audioPath,
|
|
936
|
+
transcriptFile,
|
|
937
|
+
subtitleFile,
|
|
938
|
+
...(duration !== undefined ? { duration } : {}),
|
|
939
|
+
...(segments !== undefined ? { segments } : {}),
|
|
940
|
+
};
|
|
285
941
|
},
|
|
286
|
-
timeoutMs
|
|
942
|
+
get timeoutMs() { return getCfg().timeoutMs + 10000; },
|
|
287
943
|
};
|
|
288
944
|
const voicePreview = {
|
|
289
945
|
name: 'voice_preview',
|
|
290
|
-
|
|
946
|
+
isConcurrencySafe: () => !getCfg().overwrite,
|
|
947
|
+
description: '音色试听:用一段固定试听文本批量生成短样例 MP3,并默认生成可直接在浏览器试听对比的 index.html。voices 支持音色 id、内置角色人设(persona)或自定义音色别名(customVoices)数组(可选,缺省 voice_list 前 4 个,最多 8 个);text 可自定义(≤200 字符);样例文件写入输出目录(默认会话工作目录 voice_previews)。单个音色失败不阻断其他。',
|
|
291
948
|
parameters: compileParameters({
|
|
292
|
-
voices: { type: 'array', items: { type: 'string' }, description: '音色 id
|
|
949
|
+
voices: { type: 'array', items: { type: 'string' }, description: '音色 id、内置人设 ID/标签(如 late-night-radio、anime-boy)或自定义音色别名数组(可选,缺省 voice_list 前 4 个,最多 8 个)。' },
|
|
293
950
|
text: { type: 'string', description: '试听文本(可选,默认中英混合试听句,≤200 字符)。' },
|
|
294
951
|
outputDir: { type: 'string', description: '输出目录(可选,默认会话工作目录下 voice_previews)。' },
|
|
952
|
+
htmlIndex: { type: 'boolean', default: true, description: '是否在输出目录生成可试听的 index.html 试听页(默认 true)。' },
|
|
295
953
|
}),
|
|
296
954
|
output: {
|
|
297
955
|
schema: previewSchema,
|
|
@@ -308,6 +966,9 @@ export function buildVoiceTools(config, deps = {}) {
|
|
|
308
966
|
const f = asRecord(item);
|
|
309
967
|
lines.push('- ' + f.voice + ' 失败:' + String(f.error ?? ''));
|
|
310
968
|
}
|
|
969
|
+
if (typeof rec.indexHtml === 'string' && rec.indexHtml !== '') {
|
|
970
|
+
lines.push('', '试听对比页:' + rec.indexHtml);
|
|
971
|
+
}
|
|
311
972
|
return [{ type: 'text', text: lines.join('\n') }];
|
|
312
973
|
},
|
|
313
974
|
},
|
|
@@ -325,25 +986,33 @@ export function buildVoiceTools(config, deps = {}) {
|
|
|
325
986
|
const text = optionalString(args, 'text') ?? DEFAULT_PREVIEW_TEXT;
|
|
326
987
|
if (text.length > 200)
|
|
327
988
|
throw new Error('试听文本请控制在 200 字以内(试听要短平快)。');
|
|
989
|
+
assertTtsEngineReady(cfg, resolveUseEdge(cfg, false));
|
|
328
990
|
const cwd = sessionCwd(exec);
|
|
329
991
|
const outDir = ensureWorkspaceDirectory(optionalString(args, 'outputDir') ?? 'voice_previews', cwd);
|
|
330
|
-
const outcomes = await mapLimit(targets, PREVIEW_CONCURRENCY, signal, async (
|
|
992
|
+
const outcomes = await mapLimit(targets, PREVIEW_CONCURRENCY, signal, async (target) => {
|
|
331
993
|
throwIfAborted(signal);
|
|
332
|
-
if (!isValidVoiceId(voice)) {
|
|
333
|
-
return { voice, error: '音色 id 不合法(应为 zh-CN-XXXNeural 形式的 edge 音色)' };
|
|
334
|
-
}
|
|
335
994
|
try {
|
|
336
|
-
const
|
|
995
|
+
const spec = resolveEffectiveVoiceSpec(cfg, cwd, { voice: target });
|
|
996
|
+
const targetUseEdge = resolveUseEdge(cfg, spec.hasReferenceAudio);
|
|
997
|
+
assertTtsEngineReady(cfg, targetUseEdge);
|
|
998
|
+
if (targetUseEdge && !isValidVoiceId(spec.voice)) {
|
|
999
|
+
return { voice: target, error: '音色 id 不合法(应为 zh-CN-XXXNeural 形式的 edge 音色)' };
|
|
1000
|
+
}
|
|
1001
|
+
if (!targetUseEdge && spec.voice.trim() === '' && (spec.references === undefined || spec.references.length === 0)) {
|
|
1002
|
+
return { voice: target, error: '音色 id 不合法:不能为空' };
|
|
1003
|
+
}
|
|
1004
|
+
assertProsody(spec.rate, spec.pitch);
|
|
1005
|
+
const { audio } = await synthesizeChunk(cfg, targetUseEdge, text, spec.voice, spec.rate, spec.pitch, false, signal, spec.instructions, spec.references);
|
|
337
1006
|
throwIfAborted(signal);
|
|
338
|
-
const name = 'voice-preview-' +
|
|
1007
|
+
const name = 'voice-preview-' + target.replace(/[^a-zA-Z0-9-\u4e00-\u9fa5]/g, '_') + '.mp3';
|
|
339
1008
|
// 复用 resolveOutputPath:overwrite=false 时改名加序号,不再直接覆盖同名样例。
|
|
340
1009
|
const file = resolveOutputPath(join(outDir, name), '', cfg.overwrite, cwd);
|
|
341
1010
|
const output = writeWorkspaceFile(file, audio, cwd, undefined, cfg.overwrite);
|
|
342
|
-
return { voice, output, bytes: audio.length };
|
|
1011
|
+
return { voice: target, output, bytes: audio.length };
|
|
343
1012
|
}
|
|
344
1013
|
catch (error) {
|
|
345
1014
|
throwIfAborted(signal);
|
|
346
|
-
return { voice, error: error instanceof Error ? error.message : String(error) };
|
|
1015
|
+
return { voice: target, error: error instanceof Error ? error.message : String(error) };
|
|
347
1016
|
}
|
|
348
1017
|
});
|
|
349
1018
|
const samples = [];
|
|
@@ -354,15 +1023,60 @@ export function buildVoiceTools(config, deps = {}) {
|
|
|
354
1023
|
else
|
|
355
1024
|
samples.push({ voice: outcome.voice, output: outcome.output, bytes: outcome.bytes });
|
|
356
1025
|
}
|
|
357
|
-
|
|
1026
|
+
let indexHtml = '';
|
|
1027
|
+
if (samples.length > 0 && args.htmlIndex !== false) {
|
|
1028
|
+
const voiceMap = new Map(VOICES.map((v) => [v.id, v]));
|
|
1029
|
+
const htmlSamples = samples.map((s) => {
|
|
1030
|
+
const id = String(s.voice);
|
|
1031
|
+
const meta = voiceMap.get(id);
|
|
1032
|
+
if (meta !== undefined) {
|
|
1033
|
+
return {
|
|
1034
|
+
voice: id,
|
|
1035
|
+
fileName: basename(String(s.output)),
|
|
1036
|
+
locale: meta.locale,
|
|
1037
|
+
gender: meta.gender,
|
|
1038
|
+
};
|
|
1039
|
+
}
|
|
1040
|
+
const persona = resolvePersona(id);
|
|
1041
|
+
if (persona !== undefined) {
|
|
1042
|
+
return {
|
|
1043
|
+
voice: id,
|
|
1044
|
+
fileName: basename(String(s.output)),
|
|
1045
|
+
locale: persona.label + '(' + persona.voice + ')',
|
|
1046
|
+
};
|
|
1047
|
+
}
|
|
1048
|
+
const alias = resolveCustomVoice(cfg.customVoices, id);
|
|
1049
|
+
if (alias !== undefined) {
|
|
1050
|
+
return {
|
|
1051
|
+
voice: id,
|
|
1052
|
+
fileName: basename(String(s.output)),
|
|
1053
|
+
locale: '自定义音色' + (alias.voice ? '(' + alias.voice + ')' : ''),
|
|
1054
|
+
};
|
|
1055
|
+
}
|
|
1056
|
+
return {
|
|
1057
|
+
voice: id,
|
|
1058
|
+
fileName: basename(String(s.output)),
|
|
1059
|
+
};
|
|
1060
|
+
});
|
|
1061
|
+
const html = renderPreviewHtml(htmlSamples, text);
|
|
1062
|
+
try {
|
|
1063
|
+
indexHtml = writeWorkspaceFile(resolveOutputPath(join(outDir, 'index.html'), '', true, cwd), html, cwd, undefined, true);
|
|
1064
|
+
}
|
|
1065
|
+
catch {
|
|
1066
|
+
// 即使 index.html 遇到符号链接/硬链接保护,也不阻断试听样例生成本身。
|
|
1067
|
+
}
|
|
1068
|
+
}
|
|
1069
|
+
return { count: samples.length, samples, failed, text, ...(indexHtml !== '' ? { indexHtml } : {}) };
|
|
358
1070
|
},
|
|
359
|
-
timeoutMs
|
|
1071
|
+
get timeoutMs() { return getCfg().timeoutMs * 3 + 15000; },
|
|
360
1072
|
};
|
|
361
1073
|
const voiceHealth = {
|
|
362
1074
|
name: 'voice_health',
|
|
363
1075
|
isConcurrencySafe: () => true,
|
|
364
|
-
description: 'dsh-voice 自检:检查 TTS
|
|
365
|
-
parameters: compileParameters({
|
|
1076
|
+
description: 'dsh-voice 自检:检查 TTS 音色/引擎/语速音调、代理配置与 ASR 引擎/接口地址/密钥就绪状态(不发起网络请求)。支持按 scope(all/tts/stt)分别检查。',
|
|
1077
|
+
parameters: compileParameters({
|
|
1078
|
+
scope: { type: 'string', enum: ['all', 'tts', 'stt'], description: '检查范围(可选:all 全部 / tts 仅语音合成 / stt 仅语音转写,默认 all)。' },
|
|
1079
|
+
}),
|
|
366
1080
|
output: {
|
|
367
1081
|
schema: { type: 'object', additionalProperties: true },
|
|
368
1082
|
render: (_args, value) => {
|
|
@@ -383,12 +1097,23 @@ export function buildVoiceTools(config, deps = {}) {
|
|
|
383
1097
|
return [{ type: 'text', text: lines.join('\n') }];
|
|
384
1098
|
},
|
|
385
1099
|
},
|
|
386
|
-
async execute() {
|
|
1100
|
+
async execute(rawArgs) {
|
|
387
1101
|
const cfg = getCfg();
|
|
1102
|
+
const args = asRecord(rawArgs);
|
|
1103
|
+
const scopeArg = optionalString(args, 'scope');
|
|
1104
|
+
if (scopeArg !== undefined && !['all', 'tts', 'stt'].includes(scopeArg)) {
|
|
1105
|
+
throw new Error('scope 必须是 all / tts / stt 之一(当前:' + scopeArg + ')。');
|
|
1106
|
+
}
|
|
1107
|
+
const scope = scopeArg ?? 'all';
|
|
388
1108
|
const checks = [];
|
|
389
|
-
const
|
|
390
|
-
|
|
391
|
-
|
|
1109
|
+
const ttsHints = [];
|
|
1110
|
+
const sttHints = [];
|
|
1111
|
+
// 1. TTS 音色(保持 checks[0] 以兼容既有测试对 checks[0].detail 的断言)
|
|
1112
|
+
const matchedAlias = resolveCustomVoice(cfg.customVoices, cfg.ttsVoice);
|
|
1113
|
+
const matchedPersona = resolvePersona(cfg.ttsVoice);
|
|
1114
|
+
const voiceOk = cfg.ttsEngine === 'edge'
|
|
1115
|
+
? (isValidVoiceId(cfg.ttsVoice) || matchedAlias !== undefined || matchedPersona !== undefined)
|
|
1116
|
+
: cfg.ttsVoice.trim() !== '';
|
|
392
1117
|
checks.push({
|
|
393
1118
|
name: 'TTS 音色',
|
|
394
1119
|
ok: voiceOk,
|
|
@@ -396,34 +1121,83 @@ export function buildVoiceTools(config, deps = {}) {
|
|
|
396
1121
|
...(voiceOk ? {} : { hint: '运行 voice_list 挑一个清单内音色,在 DSH「设置 → 插件 → 语音」修改 ttsVoice(保存即生效);合法 id 形如 zh-CN-XiaoxiaoNeural。' }),
|
|
397
1122
|
});
|
|
398
1123
|
if (!voiceOk) {
|
|
399
|
-
|
|
400
|
-
|
|
1124
|
+
ttsHints.push('TTS 音色不合法:先用 voice_list 选音色,再在 DSH「设置 → 插件 → 语音」修改 ttsVoice(保存即生效);在此之前 voice_tts 调用会直接报错。');
|
|
1125
|
+
}
|
|
1126
|
+
// 2. TTS 引擎
|
|
1127
|
+
let ttsEngineOk = true;
|
|
1128
|
+
let ttsEngineDetail = 'edge(免费零配置)';
|
|
1129
|
+
let ttsEngineHint;
|
|
1130
|
+
if (cfg.ttsEngine !== 'edge') {
|
|
1131
|
+
const ttsBaseOk = cfg.ttsBaseUrl !== '';
|
|
1132
|
+
const ttsKeyOk = cfg.ttsApiKey !== '' || (ttsBaseOk && isLoopbackBaseUrl(cfg.ttsBaseUrl));
|
|
1133
|
+
ttsEngineOk = ttsBaseOk && ttsKeyOk;
|
|
1134
|
+
if (!ttsBaseOk) {
|
|
1135
|
+
ttsEngineDetail = '未配置:ttsEngine=' + cfg.ttsEngine + ' 时必须提供 ttsBaseUrl';
|
|
1136
|
+
ttsEngineHint = '在 DSH「设置 → 插件 → 语音」补上 ttsBaseUrl(含 /v1 的 OpenAI 兼容地址),或改回 edge。';
|
|
1137
|
+
ttsHints.push('TTS 接口地址缺失:当前 ttsEngine=' + cfg.ttsEngine + ',需配置 ttsBaseUrl 或切回免费 edge 引擎。');
|
|
1138
|
+
}
|
|
1139
|
+
else if (!ttsKeyOk) {
|
|
1140
|
+
ttsEngineDetail = '未配置 TTS 密钥:需要 DSH_VOICE_TTS_KEY 或配置 ttsApiKey';
|
|
1141
|
+
ttsEngineHint = '在 DSH「设置 → 插件 → 语音」填写 ttsApiKey,或设置环境变量 DSH_VOICE_TTS_KEY(或切回免费 edge 引擎)。';
|
|
1142
|
+
ttsHints.push('缺 TTS 密钥:当前 ttsEngine=' + cfg.ttsEngine + ' 需要 ttsApiKey / DSH_VOICE_TTS_KEY,或改回零密钥的 edge。');
|
|
1143
|
+
}
|
|
1144
|
+
else {
|
|
1145
|
+
ttsEngineDetail = cfg.ttsEngine + '(' + cfg.ttsBaseUrl + ')';
|
|
1146
|
+
}
|
|
1147
|
+
}
|
|
1148
|
+
checks.push({
|
|
1149
|
+
name: 'TTS 引擎',
|
|
1150
|
+
ok: ttsEngineOk,
|
|
1151
|
+
detail: ttsEngineDetail,
|
|
1152
|
+
...(ttsEngineHint !== undefined ? { hint: ttsEngineHint } : {}),
|
|
1153
|
+
});
|
|
1154
|
+
// 3. TTS 语速与音调
|
|
1155
|
+
const prosodyOk = isValidProsody(cfg.ttsRate) && isValidProsody(cfg.ttsPitch);
|
|
1156
|
+
checks.push({
|
|
1157
|
+
name: 'TTS 语速与音调',
|
|
1158
|
+
ok: prosodyOk,
|
|
1159
|
+
detail: prosodyOk ? 'rate=' + cfg.ttsRate + ', pitch=' + cfg.ttsPitch : '不合法:rate=' + cfg.ttsRate + ', pitch=' + cfg.ttsPitch,
|
|
1160
|
+
...(prosodyOk ? {} : { hint: '语速/音调必须符合 +10%、-20%、+2Hz、-1st 格式,请在 DSH「设置 → 插件 → 语音」修正 ttsRate / ttsPitch。' }),
|
|
1161
|
+
});
|
|
1162
|
+
if (!prosodyOk) {
|
|
1163
|
+
ttsHints.push('TTS 语速或音调格式不合法:请在设置中修正 ttsRate / ttsPitch(如 +0% / +0Hz)。');
|
|
401
1164
|
}
|
|
1165
|
+
// 4. 特殊代理
|
|
402
1166
|
checks.push({ name: '特殊代理', ok: true, detail: cfg.proxyUrl !== '' ? '已配置 ' + cfg.proxyUrl : '未配置' });
|
|
1167
|
+
// 5. ASR 引擎
|
|
403
1168
|
checks.push({ name: 'ASR 引擎', ok: true, detail: cfg.asrEngine });
|
|
1169
|
+
// 6. ASR 接口地址
|
|
404
1170
|
const baseUrlOk = cfg.asrBaseUrl !== '';
|
|
405
1171
|
checks.push({
|
|
406
1172
|
name: 'ASR 接口地址',
|
|
407
1173
|
ok: baseUrlOk,
|
|
408
1174
|
detail: baseUrlOk ? cfg.asrBaseUrl : '未配置:asrEngine=custom 时必须提供 asrBaseUrl。',
|
|
409
|
-
...(baseUrlOk ? {} : { hint: '在 DSH「设置 → 插件 → 语音」补上 asrBaseUrl(含 /v1 的 OpenAI 兼容地址,保存即生效),或改回 groq / openai 预设。' }),
|
|
1175
|
+
...(baseUrlOk ? {} : { hint: '在 DSH「设置 → 插件 → 语音」补上 asrBaseUrl(含 /v1 的 OpenAI 兼容地址,保存即生效),或改回 groq / openai / siliconflow 预设。' }),
|
|
410
1176
|
});
|
|
411
1177
|
if (!baseUrlOk) {
|
|
412
|
-
|
|
413
|
-
hints.push('ASR 接口地址缺失:只有 voice_stt 受影响,voice_tts / voice_list / voice_preview 仍可零配置正常使用。');
|
|
1178
|
+
sttHints.push('ASR 接口地址缺失:只有 voice_stt 受影响,voice_tts / voice_list / voice_preview 仍可零配置正常使用。');
|
|
414
1179
|
}
|
|
415
|
-
|
|
1180
|
+
// 7. ASR 密钥(cfg.asrApiKey 已通过 resolveAsrApiKey 完成 trim;custom 本地回环端点免密钥)
|
|
1181
|
+
const isLocalLoopbackAsr = cfg.asrEngine === 'custom' && cfg.asrBaseUrl !== '' && isLoopbackBaseUrl(cfg.asrBaseUrl);
|
|
1182
|
+
const hasKey = cfg.asrApiKey !== '' || isLocalLoopbackAsr;
|
|
416
1183
|
checks.push({
|
|
417
1184
|
name: 'ASR 密钥',
|
|
418
1185
|
ok: hasKey,
|
|
419
|
-
detail:
|
|
1186
|
+
detail: cfg.asrApiKey !== ''
|
|
1187
|
+
? '已配置'
|
|
1188
|
+
: isLocalLoopbackAsr
|
|
1189
|
+
? '本地回环端点免密钥(' + cfg.asrBaseUrl + ')'
|
|
1190
|
+
: '未配置:voice_stt 需要 DSH_VOICE_ASR_KEY 环境变量或在「设置 → 插件 → 语音」配置 asrApiKey',
|
|
420
1191
|
...(hasKey ? {} : { hint: '在 DSH「设置 → 插件 → 语音」填写 asrApiKey(自动脱敏、保存即生效),或设置环境变量 DSH_VOICE_ASR_KEY;密钥缺失只影响 voice_stt。' }),
|
|
421
1192
|
});
|
|
422
1193
|
if (!hasKey) {
|
|
423
|
-
|
|
424
|
-
hints.push('缺 ASR 密钥:仅 voice_stt 录音转写需要密钥;文字转语音(voice_tts / voice_list / voice_preview)无需任何密钥,现在即可直接使用。');
|
|
1194
|
+
sttHints.push('缺 ASR 密钥:仅 voice_stt 录音转写需要密钥;文字转语音(voice_tts / voice_list / voice_preview)无需任何密钥,现在即可直接使用。');
|
|
425
1195
|
}
|
|
426
|
-
|
|
1196
|
+
const ttsReady = voiceOk && ttsEngineOk && prosodyOk;
|
|
1197
|
+
const sttReady = baseUrlOk && hasKey;
|
|
1198
|
+
const ok = scope === 'tts' ? ttsReady : scope === 'stt' ? sttReady : (ttsReady && sttReady);
|
|
1199
|
+
const hints = scope === 'tts' ? ttsHints : scope === 'stt' ? sttHints : [...ttsHints, ...sttHints];
|
|
1200
|
+
return { ok, ttsReady, sttReady, plugin: 'dsh-voice', checks, ...(hints.length > 0 ? { hints } : {}) };
|
|
427
1201
|
},
|
|
428
1202
|
timeoutMs: 5000,
|
|
429
1203
|
};
|