dsh-voice 0.9.0 → 0.10.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/web.js CHANGED
@@ -1,19 +1,29 @@
1
1
  import { EventEmitter } from 'node:events';
2
+ import { lstatSync, readdirSync, readFileSync, realpathSync, statSync } from 'node:fs';
3
+ import { basename, dirname, extname, isAbsolute, join, relative, resolve, sep } from 'node:path';
2
4
  import { Readable } from 'node:stream';
3
5
  import { isLoopbackBaseUrl, isValidProsody, resolveCustomVoice, } from './config.js';
4
- import { fetchEdgeVoices, synthesizeSpeech } from './edge-tts.js';
6
+ import { fetchEdgeVoices, synthesizeSpeechWithMetadata, } from './edge-tts.js';
5
7
  import { rateToSpeed, synthesizeOpenAiSpeech } from './openai-tts.js';
8
+ import { assertAudioFile, ensureWorkspaceDirectory, resolveOutputPath, writeWorkspaceFile, } from './paths.js';
6
9
  import { createProxyFetch } from './proxy-fetch.js';
7
10
  import { hasFfmpeg, transcribe } from './stt.js';
8
11
  import { DEFAULT_PREVIEW_TEXT } from './tools.js';
9
- import { applyPronunciationsPlain, inspectMp3Buffer } from './tts-text.js';
12
+ import { applyPronunciationsPlain, concatMp3Buffers, createSilenceMp3, formatSubtitles, inspectMp3Buffer, mixAudioWithBgm, normalizeAudioLoudness, offsetCues, parseSubtitleFile, } from './tts-text.js';
10
13
  import { filterVoices, isValidVoiceId, resolvePersona, VOICE_PERSONAS, VOICES } from './voices.js';
11
14
  export const VOICE_STUDIO_SETTINGS_ROUTE = '/_dsh/dsh-voice/studio';
12
15
  export const VOICE_STUDIO_API_ROUTE = '/api/dsh-voice/studio';
13
16
  const AUTHENTICATED_CARRIER = Symbol('DSH authenticated Voice Studio connection');
14
17
  const MAX_AUDIO_BYTES = 10 * 1024 * 1024;
18
+ const MAX_MEDIA_BYTES = 15 * 1024 * 1024;
15
19
  const MAX_BODY = 16 * 1024 * 1024;
16
20
  const MAX_PREVIEW_CHARS = 300;
21
+ const MAX_DIALOGUE_SEGMENTS = 20;
22
+ const MAX_DIALOGUE_CHARS = 6000;
23
+ const MAX_LIBRARY_ITEMS = 40;
24
+ const ALLOWED_CLONE_EXTS = new Set(['.wav', '.mp3', '.webm', '.ogg', '.m4a']);
25
+ const LIBRARY_AUDIO_EXTS = new Set(['.mp3', '.wav', '.m4a', '.ogg', '.webm', '.flac']);
26
+ const LIBRARY_SUBDIRS = ['', 'voice_outputs', 'voice_previews', 'voice_clones'];
17
27
  const loopback = (host) => ['127.0.0.1', 'localhost', '::1', '[::1]', '::ffff:127.0.0.1'].includes(host.toLowerCase());
18
28
  const record = (value) => value && typeof value === 'object' && !Array.isArray(value) ? value : {};
19
29
  const text = (args, key) => typeof args[key] === 'string' ? args[key].trim() : '';
@@ -26,6 +36,63 @@ export class VoiceStudioError extends Error {
26
36
  this.code = code;
27
37
  }
28
38
  }
39
+ function resolveWorkspaceRoot(cwd) {
40
+ const absolute = resolve(cwd);
41
+ try {
42
+ return realpathSync(absolute);
43
+ }
44
+ catch {
45
+ return absolute;
46
+ }
47
+ }
48
+ function isInsideRoot(root, candidate) {
49
+ const rel = relative(root, candidate);
50
+ return rel === '' || (!isAbsolute(rel) && rel !== '..' && !rel.startsWith('..' + sep));
51
+ }
52
+ function toWorkspaceRelativePath(filePath, cwd) {
53
+ const root = resolveWorkspaceRoot(cwd);
54
+ let realTarget = resolve(root, filePath);
55
+ try {
56
+ realTarget = realpathSync(realTarget);
57
+ }
58
+ catch {
59
+ // 保留基于 root 解析的目标路径
60
+ }
61
+ return relative(root, realTarget).replace(/\\/g, '/');
62
+ }
63
+ function audioMimeFromExt(ext) {
64
+ switch (ext.toLowerCase()) {
65
+ case '.mp3':
66
+ return 'audio/mpeg';
67
+ case '.wav':
68
+ return 'audio/wav';
69
+ case '.m4a':
70
+ return 'audio/mp4';
71
+ case '.ogg':
72
+ return 'audio/ogg';
73
+ case '.webm':
74
+ return 'audio/webm';
75
+ case '.flac':
76
+ return 'audio/flac';
77
+ default:
78
+ return 'application/octet-stream';
79
+ }
80
+ }
81
+ function decodeAudioBase64(raw, label = '录音音频') {
82
+ const rawB64 = typeof raw === 'string' ? raw.trim() : '';
83
+ const cleanedB64 = rawB64.replace(/^data:[^;]+;base64,/i, '');
84
+ if (!cleanedB64) {
85
+ throw new VoiceStudioError(`请提供${label}数据(audioBase64)。`, 400);
86
+ }
87
+ const audioBuf = Buffer.from(cleanedB64, 'base64');
88
+ if (audioBuf.length === 0) {
89
+ throw new VoiceStudioError(`${label}数据为空或格式无效。`, 400);
90
+ }
91
+ if (audioBuf.length > MAX_AUDIO_BYTES) {
92
+ throw new VoiceStudioError(`${label}超过 10 MiB 上限。`, 413, 'bodyLarge');
93
+ }
94
+ return audioBuf;
95
+ }
29
96
  function requestGuard(req) {
30
97
  const host = req.headers.host;
31
98
  if (!host || !loopback(req.socket.remoteAddress ?? '')) {
@@ -87,7 +154,7 @@ function carrierGuard(request) {
87
154
  }
88
155
  }
89
156
  function readBody(req, signal) {
90
- return new Promise((resolve, reject) => {
157
+ return new Promise((resolvePromise, reject) => {
91
158
  let length = 0;
92
159
  const chunks = [];
93
160
  const cleanup = () => {
@@ -119,7 +186,7 @@ function readBody(req, signal) {
119
186
  const value = JSON.parse(Buffer.concat(chunks).toString('utf8'));
120
187
  if (!value || typeof value !== 'object' || Array.isArray(value))
121
188
  throw new Error();
122
- resolve(value);
189
+ resolvePromise(value);
123
190
  }
124
191
  catch {
125
192
  reject(new VoiceStudioError('请求内容不是有效的 JSON。'));
@@ -283,7 +350,173 @@ export function createVoiceStudioBackend(readCfg, options = {}) {
283
350
  }
284
351
  return cachedFetchImpl;
285
352
  };
353
+ const getWorkspaceDir = () => {
354
+ const raw = typeof options.workspaceDir === 'function' ? options.workspaceDir() : options.workspaceDir;
355
+ return typeof raw === 'string' && raw.trim() !== '' ? raw.trim() : process.cwd();
356
+ };
286
357
  const writable = () => options.writable !== undefined ? options.writable() !== false : typeof options.updateSettings === 'function';
358
+ const resolveUseEdge = (cfg) => options.tts !== undefined || options.ttsWithMeta !== undefined
359
+ ? true
360
+ : options.openaiTts !== undefined && cfg.ttsEngine !== 'edge'
361
+ ? false
362
+ : cfg.ttsEngine === 'edge';
363
+ const resolveStudioVoice = (cfg, useEdge, cwd, input, refAudioCache) => {
364
+ const rawVoice = input.voice;
365
+ const rawPersona = input.persona;
366
+ let customAlias;
367
+ let voiceConsumedAsAlias = false;
368
+ let personaConsumedAsAlias = false;
369
+ if (rawPersona !== undefined) {
370
+ customAlias = resolveCustomVoice(cfg.customVoices, rawPersona);
371
+ if (customAlias !== undefined)
372
+ personaConsumedAsAlias = true;
373
+ }
374
+ if (customAlias === undefined && rawVoice !== undefined) {
375
+ customAlias = resolveCustomVoice(cfg.customVoices, rawVoice);
376
+ if (customAlias !== undefined)
377
+ voiceConsumedAsAlias = true;
378
+ }
379
+ if (customAlias === undefined &&
380
+ rawVoice === undefined &&
381
+ rawPersona === undefined &&
382
+ input.speakerAlias !== undefined) {
383
+ customAlias = resolveCustomVoice(cfg.customVoices, input.speakerAlias);
384
+ }
385
+ let defaultAlias;
386
+ if (rawVoice === undefined && rawPersona === undefined && customAlias === undefined) {
387
+ defaultAlias = resolveCustomVoice(cfg.customVoices, cfg.ttsVoice);
388
+ }
389
+ const activeAlias = customAlias ?? defaultAlias;
390
+ let voiceConsumedAsPersona = false;
391
+ let personaSpec = !personaConsumedAsAlias && rawPersona !== undefined ? resolvePersona(rawPersona) : undefined;
392
+ if (personaSpec === undefined && activeAlias?.persona !== undefined) {
393
+ personaSpec = resolvePersona(activeAlias.persona);
394
+ }
395
+ if (personaSpec === undefined && !voiceConsumedAsAlias && rawVoice !== undefined && !isValidVoiceId(rawVoice)) {
396
+ personaSpec = resolvePersona(rawVoice);
397
+ if (personaSpec !== undefined)
398
+ voiceConsumedAsPersona = true;
399
+ }
400
+ let defaultPersona;
401
+ if (rawVoice === undefined &&
402
+ rawPersona === undefined &&
403
+ activeAlias === undefined &&
404
+ !isValidVoiceId(cfg.ttsVoice)) {
405
+ defaultPersona = resolvePersona(cfg.ttsVoice);
406
+ }
407
+ const activePersona = personaSpec ?? defaultPersona;
408
+ let references;
409
+ if (!useEdge && activeAlias?.referenceAudio) {
410
+ const refPath = assertAudioFile(activeAlias.referenceAudio, cwd);
411
+ let buf = refAudioCache?.get(refPath);
412
+ if (buf === undefined) {
413
+ const size = statSync(refPath).size;
414
+ if (size <= 0 || size > MAX_AUDIO_BYTES) {
415
+ throw new VoiceStudioError('参考音频大小异常(必须为 1B 至 10MiB):' + activeAlias.referenceAudio, 400);
416
+ }
417
+ buf = readFileSync(refPath);
418
+ refAudioCache?.set(refPath, buf);
419
+ }
420
+ references = [
421
+ {
422
+ audioDataUri: 'data:' + audioMimeFromExt(extname(refPath)) + ';base64,' + buf.toString('base64'),
423
+ rawAudio: buf,
424
+ filename: basename(refPath),
425
+ ...(activeAlias.referenceText ? { text: activeAlias.referenceText } : {}),
426
+ },
427
+ ];
428
+ }
429
+ const explicitRawVoice = !voiceConsumedAsAlias && !voiceConsumedAsPersona ? rawVoice : undefined;
430
+ const resolvedVoice = !useEdge && references !== undefined && explicitRawVoice === undefined
431
+ ? (activeAlias?.voice ?? '')
432
+ : (explicitRawVoice ?? activeAlias?.voice ?? activePersona?.voice ?? cfg.ttsVoice);
433
+ const displayVoice = resolvedVoice || activeAlias?.name || cfg.ttsVoice;
434
+ const resolvedRate = input.rate ?? activeAlias?.rate ?? activePersona?.rate ?? cfg.ttsRate;
435
+ const resolvedPitch = input.pitch ?? activeAlias?.pitch ?? activePersona?.pitch ?? cfg.ttsPitch;
436
+ const resolvedInstructions = input.instructions ?? activeAlias?.instructions;
437
+ const defaultPause = activePersona?.pauseMs ?? 300;
438
+ const rawPause = input.pauseMs !== undefined && Number.isFinite(input.pauseMs) ? input.pauseMs : defaultPause;
439
+ const resolvedPauseMs = Math.min(5000, Math.max(0, Math.round(rawPause)));
440
+ if (!isValidProsody(resolvedRate)) {
441
+ throw new VoiceStudioError('语速格式不合法(如 +0%、+15%、-10%)。', 400);
442
+ }
443
+ if (!isValidProsody(resolvedPitch)) {
444
+ throw new VoiceStudioError('音调格式不合法(如 +0Hz、+2Hz、-1st)。', 400);
445
+ }
446
+ return {
447
+ voice: resolvedVoice,
448
+ displayVoice,
449
+ rate: resolvedRate,
450
+ pitch: resolvedPitch,
451
+ pauseMs: resolvedPauseMs,
452
+ ...(resolvedInstructions ? { instructions: resolvedInstructions } : {}),
453
+ ...(references ? { references } : {}),
454
+ };
455
+ };
456
+ const synthesizeSegmentWithCues = async (cfg, useEdge, segText, spec, pronunciations, signal) => {
457
+ if (useEdge) {
458
+ if (!isValidVoiceId(spec.voice)) {
459
+ throw new VoiceStudioError('音色 ID 不合法:' + spec.voice, 400);
460
+ }
461
+ const pronOpt = pronunciations.length > 0 ? { pronunciations } : {};
462
+ if (options.ttsWithMeta !== undefined || options.tts === undefined) {
463
+ const withMeta = options.ttsWithMeta ?? synthesizeSpeechWithMetadata;
464
+ const res = await withMeta({
465
+ text: segText,
466
+ voice: spec.voice,
467
+ rate: spec.rate,
468
+ pitch: spec.pitch,
469
+ boundary: 'sentence',
470
+ ...pronOpt,
471
+ }, { proxyUrl: cfg.proxyUrl }, cfg.timeoutMs, signal);
472
+ const info = inspectMp3Buffer(res.audio);
473
+ const cues = res.cues.length > 0
474
+ ? res.cues
475
+ : [{ index: 1, startMs: 0, endMs: Math.max(1, info.durationMs), text: segText }];
476
+ return { audio: res.audio, info, cues };
477
+ }
478
+ const audio = await options.tts({
479
+ text: segText,
480
+ voice: spec.voice,
481
+ rate: spec.rate,
482
+ pitch: spec.pitch,
483
+ ...pronOpt,
484
+ }, { proxyUrl: cfg.proxyUrl }, cfg.timeoutMs, signal);
485
+ const info = inspectMp3Buffer(audio);
486
+ return {
487
+ audio,
488
+ info,
489
+ cues: [{ index: 1, startMs: 0, endMs: Math.max(1, info.durationMs), text: segText }],
490
+ };
491
+ }
492
+ if (cfg.ttsBaseUrl === '') {
493
+ throw new VoiceStudioError('未配置 TTS 接口地址(ttsBaseUrl)。', 400, 'config');
494
+ }
495
+ if (cfg.ttsApiKey === '' && !isLoopbackBaseUrl(cfg.ttsBaseUrl)) {
496
+ throw new VoiceStudioError('未配置 TTS 密钥(ttsApiKey)。', 400, 'config');
497
+ }
498
+ const hasRefs = Array.isArray(spec.references) && spec.references.length > 0;
499
+ if (spec.voice.trim() === '' && !hasRefs) {
500
+ throw new VoiceStudioError('音色 ID 不能为空。', 400);
501
+ }
502
+ const spokenText = pronunciations.length > 0 ? applyPronunciationsPlain(segText, pronunciations) : segText;
503
+ const openaiFn = options.openaiTts ?? synthesizeOpenAiSpeech;
504
+ const audio = await openaiFn(cfg.ttsBaseUrl, cfg.ttsApiKey, {
505
+ text: spokenText,
506
+ voice: spec.voice,
507
+ model: cfg.ttsModel !== '' ? cfg.ttsModel : 'gpt-4o-mini-tts',
508
+ speed: rateToSpeed(spec.rate),
509
+ ...(spec.instructions ? { instructions: spec.instructions } : {}),
510
+ ...(hasRefs ? { references: spec.references } : {}),
511
+ signal,
512
+ }, getProxyFetch(cfg.proxyUrl) ?? globalThis.fetch, cfg.timeoutMs, signal);
513
+ const info = inspectMp3Buffer(audio);
514
+ return {
515
+ audio,
516
+ info,
517
+ cues: [{ index: 1, startMs: 0, endMs: Math.max(1, info.durationMs), text: segText }],
518
+ };
519
+ };
287
520
  const snapshot = () => {
288
521
  const cfg = readCfg();
289
522
  const pronunciations = readPronunciations(cfg);
@@ -359,98 +592,17 @@ export function createVoiceStudioBackend(readCfg, options = {}) {
359
592
  if (previewText.length > MAX_PREVIEW_CHARS) {
360
593
  throw new VoiceStudioError(`试听文本不能超过 ${MAX_PREVIEW_CHARS} 个字符。`, 400, 'textTooLong');
361
594
  }
362
- const rawVoice = text(args, 'voice') || undefined;
363
- const rawPersona = text(args, 'persona') || undefined;
364
- const rawRate = text(args, 'rate') || undefined;
365
- const rawPitch = text(args, 'pitch') || undefined;
366
- const rawInstructions = text(args, 'instructions') || undefined;
367
- let customAlias;
368
- let voiceConsumedAsAlias = false;
369
- let personaConsumedAsAlias = false;
370
- if (rawPersona !== undefined) {
371
- customAlias = resolveCustomVoice(cfg.customVoices, rawPersona);
372
- if (customAlias !== undefined)
373
- personaConsumedAsAlias = true;
374
- }
375
- if (customAlias === undefined && rawVoice !== undefined) {
376
- customAlias = resolveCustomVoice(cfg.customVoices, rawVoice);
377
- if (customAlias !== undefined)
378
- voiceConsumedAsAlias = true;
379
- }
380
- let defaultAlias;
381
- if (rawVoice === undefined && rawPersona === undefined) {
382
- defaultAlias = resolveCustomVoice(cfg.customVoices, cfg.ttsVoice);
383
- }
384
- const activeAlias = customAlias ?? defaultAlias;
385
- let voiceConsumedAsPersona = false;
386
- let personaSpec = !personaConsumedAsAlias && rawPersona !== undefined ? resolvePersona(rawPersona) : undefined;
387
- if (personaSpec === undefined && activeAlias?.persona !== undefined) {
388
- personaSpec = resolvePersona(activeAlias.persona);
389
- }
390
- if (personaSpec === undefined && !voiceConsumedAsAlias && rawVoice !== undefined && !isValidVoiceId(rawVoice)) {
391
- personaSpec = resolvePersona(rawVoice);
392
- if (personaSpec !== undefined)
393
- voiceConsumedAsPersona = true;
394
- }
395
- let defaultPersona;
396
- if (rawVoice === undefined && rawPersona === undefined && activeAlias === undefined && !isValidVoiceId(cfg.ttsVoice)) {
397
- defaultPersona = resolvePersona(cfg.ttsVoice);
398
- }
399
- const activePersona = personaSpec ?? defaultPersona;
400
- const explicitRawVoice = !voiceConsumedAsAlias && !voiceConsumedAsPersona ? rawVoice : undefined;
401
- const resolvedVoice = explicitRawVoice ?? activeAlias?.voice ?? activePersona?.voice ?? cfg.ttsVoice;
402
- const resolvedRate = rawRate ?? activeAlias?.rate ?? activePersona?.rate ?? cfg.ttsRate;
403
- const resolvedPitch = rawPitch ?? activeAlias?.pitch ?? activePersona?.pitch ?? cfg.ttsPitch;
404
- const resolvedInstructions = rawInstructions ?? activeAlias?.instructions;
405
- if (!isValidProsody(resolvedRate)) {
406
- throw new VoiceStudioError('语速格式不合法(如 +0%、+15%、-10%)。', 400);
407
- }
408
- if (!isValidProsody(resolvedPitch)) {
409
- throw new VoiceStudioError('音调格式不合法(如 +0Hz、+2Hz、-1st)。', 400);
410
- }
411
- const useEdge = options.tts !== undefined
412
- ? true
413
- : options.openaiTts !== undefined && cfg.ttsEngine !== 'edge'
414
- ? false
415
- : cfg.ttsEngine === 'edge';
595
+ const cwd = getWorkspaceDir();
596
+ const useEdge = resolveUseEdge(cfg);
597
+ const spec = resolveStudioVoice(cfg, useEdge, cwd, {
598
+ voice: text(args, 'voice') || undefined,
599
+ persona: text(args, 'persona') || undefined,
600
+ rate: text(args, 'rate') || undefined,
601
+ pitch: text(args, 'pitch') || undefined,
602
+ instructions: text(args, 'instructions') || undefined,
603
+ });
416
604
  const pronunciations = readPronunciations(cfg);
417
- let audio;
418
- if (useEdge) {
419
- if (!isValidVoiceId(resolvedVoice)) {
420
- throw new VoiceStudioError('音色 ID 不合法:' + resolvedVoice, 400);
421
- }
422
- const ttsFn = options.tts ?? synthesizeSpeech;
423
- const ttsOpts = {
424
- text: previewText,
425
- voice: resolvedVoice,
426
- rate: resolvedRate,
427
- pitch: resolvedPitch,
428
- ...(pronunciations.length > 0 ? { pronunciations } : {}),
429
- };
430
- audio = await ttsFn(ttsOpts, { proxyUrl: cfg.proxyUrl }, cfg.timeoutMs, signal);
431
- }
432
- else {
433
- if (cfg.ttsBaseUrl === '') {
434
- throw new VoiceStudioError('未配置 TTS 接口地址(ttsBaseUrl)。', 400, 'config');
435
- }
436
- if (cfg.ttsApiKey === '' && !isLoopbackBaseUrl(cfg.ttsBaseUrl)) {
437
- throw new VoiceStudioError('未配置 TTS 密钥(ttsApiKey)。', 400, 'config');
438
- }
439
- if (resolvedVoice.trim() === '') {
440
- throw new VoiceStudioError('音色 ID 不能为空。', 400);
441
- }
442
- const spokenText = pronunciations.length > 0 ? applyPronunciationsPlain(previewText, pronunciations) : previewText;
443
- const openaiFn = options.openaiTts ?? synthesizeOpenAiSpeech;
444
- audio = await openaiFn(cfg.ttsBaseUrl, cfg.ttsApiKey, {
445
- text: spokenText,
446
- voice: resolvedVoice,
447
- model: cfg.ttsModel !== '' ? cfg.ttsModel : 'gpt-4o-mini-tts',
448
- speed: rateToSpeed(resolvedRate),
449
- ...(resolvedInstructions ? { instructions: resolvedInstructions } : {}),
450
- signal,
451
- }, getProxyFetch(cfg.proxyUrl) ?? globalThis.fetch, cfg.timeoutMs, signal);
452
- }
453
- const info = inspectMp3Buffer(audio);
605
+ const { audio, info, cues } = await synthesizeSegmentWithCues(cfg, useEdge, previewText, spec, pronunciations, signal);
454
606
  return {
455
607
  preview: {
456
608
  audioBase64: audio.toString('base64'),
@@ -458,23 +610,13 @@ export function createVoiceStudioBackend(readCfg, options = {}) {
458
610
  bytes: audio.length,
459
611
  durationMs: info.durationMs,
460
612
  durationSec: Math.round(info.durationMs / 100) / 10,
461
- voice: resolvedVoice,
613
+ voice: spec.displayVoice,
614
+ cues,
462
615
  },
463
616
  };
464
617
  }
465
618
  if (action === 'transcribeMic') {
466
- const rawB64 = typeof args.audioBase64 === 'string' ? args.audioBase64.trim() : '';
467
- const cleanedB64 = rawB64.replace(/^data:[^;]+;base64,/i, '');
468
- if (!cleanedB64) {
469
- throw new VoiceStudioError('请提供录音音频数据(audioBase64)。', 400);
470
- }
471
- const audioBuf = Buffer.from(cleanedB64, 'base64');
472
- if (audioBuf.length === 0) {
473
- throw new VoiceStudioError('录音音频数据为空或格式无效。', 400);
474
- }
475
- if (audioBuf.length > MAX_AUDIO_BYTES) {
476
- throw new VoiceStudioError('录音音频超过 10 MiB 上限。', 413, 'bodyLarge');
477
- }
619
+ const audioBuf = decodeAudioBase64(args.audioBase64, '录音音频');
478
620
  const rawFilename = text(args, 'filename');
479
621
  const filename = (rawFilename || 'mic-recording.webm').replace(/[/\\]/g, '_');
480
622
  const language = text(args, 'language') || undefined;
@@ -497,6 +639,364 @@ export function createVoiceStudioBackend(readCfg, options = {}) {
497
639
  },
498
640
  };
499
641
  }
642
+ if (action === 'saveCloneVoice') {
643
+ const aliasName = text(args, 'aliasName');
644
+ if (!aliasName) {
645
+ throw new VoiceStudioError('克隆角色名称(aliasName)不能为空。', 400);
646
+ }
647
+ if (aliasName.length > 64) {
648
+ throw new VoiceStudioError('克隆角色名称不能超过 64 个字符。', 400);
649
+ }
650
+ if (/[/\\:\0]/.test(aliasName) || aliasName === '.' || aliasName === '..' || aliasName.includes('..')) {
651
+ throw new VoiceStudioError('克隆角色名称包含非法路径字符。', 400, 'pathTraversal');
652
+ }
653
+ const rawFilename = text(args, 'filename');
654
+ if (rawFilename && (/[/\\:\0]/.test(rawFilename) || rawFilename.includes('..'))) {
655
+ throw new VoiceStudioError('录音文件名包含非法路径字符。', 400, 'pathTraversal');
656
+ }
657
+ const shouldSaveAlias = args.saveAlias !== false;
658
+ if (shouldSaveAlias && (!writable() || typeof options.updateSettings !== 'function')) {
659
+ throw new VoiceStudioError('当前配置是只读的,无法保存克隆角色。', 403, 'readonly');
660
+ }
661
+ const audioBuf = decodeAudioBase64(args.audioBase64, '克隆样本音频');
662
+ const cwd = getWorkspaceDir();
663
+ const cloneDir = ensureWorkspaceDirectory('voice_clones', cwd);
664
+ const rawExt = rawFilename ? extname(rawFilename).toLowerCase() : '';
665
+ const safeExt = ALLOWED_CLONE_EXTS.has(rawExt) ? rawExt : '.webm';
666
+ const safeSlug = aliasName
667
+ .normalize('NFKC')
668
+ .replace(/[^\p{L}\p{N}_-]+/gu, '-')
669
+ .replace(/-+/g, '-')
670
+ .replace(/^-|-$/g, '')
671
+ .toLowerCase()
672
+ .slice(0, 40) || 'voice';
673
+ const baseName = `clone-${safeSlug}${safeExt}`;
674
+ const targetPath = resolveOutputPath(join(cloneDir, baseName), '', cfg.overwrite, cwd);
675
+ const writtenPath = writeWorkspaceFile(targetPath, audioBuf, cwd, undefined, cfg.overwrite);
676
+ const relAudioPath = toWorkspaceRelativePath(writtenPath, cwd);
677
+ let refText = text(args, 'referenceText');
678
+ let transcribeWarning = '';
679
+ if (refText === '' && args.autoTranscribe !== false) {
680
+ const asrReady = options.stt !== undefined ||
681
+ (cfg.asrBaseUrl !== '' &&
682
+ (cfg.asrApiKey !== '' || (cfg.asrEngine === 'custom' && isLoopbackBaseUrl(cfg.asrBaseUrl))));
683
+ if (asrReady) {
684
+ try {
685
+ const sttFn = options.stt ?? transcribe;
686
+ const language = text(args, 'language') || undefined;
687
+ const sttRes = await sttFn(cfg.asrBaseUrl, cfg.asrApiKey, {
688
+ audio: audioBuf,
689
+ filename: baseName,
690
+ model: cfg.asrModel !== '' ? cfg.asrModel : 'whisper-1',
691
+ ...(language ? { language } : {}),
692
+ allowEmptyApiKey: cfg.asrEngine === 'custom' && isLoopbackBaseUrl(cfg.asrBaseUrl),
693
+ signal,
694
+ }, getProxyFetch(cfg.proxyUrl) ?? globalThis.fetch, cfg.timeoutMs, signal);
695
+ signal.throwIfAborted();
696
+ if (typeof sttRes?.text === 'string' && sttRes.text.trim() !== '') {
697
+ refText = sttRes.text.trim();
698
+ }
699
+ }
700
+ catch (error) {
701
+ signal.throwIfAborted();
702
+ transcribeWarning = error instanceof Error ? error.message : String(error);
703
+ }
704
+ }
705
+ }
706
+ if (shouldSaveAlias) {
707
+ const voiceOpt = text(args, 'voice');
708
+ const personaOpt = text(args, 'persona');
709
+ const entry = {
710
+ name: aliasName,
711
+ referenceAudio: relAudioPath,
712
+ ...(refText ? { referenceText: refText } : {}),
713
+ ...(voiceOpt ? { voice: voiceOpt } : {}),
714
+ ...(personaOpt ? { persona: personaOpt } : {}),
715
+ };
716
+ const existing = Array.isArray(cfg.customVoices) ? cfg.customVoices : [];
717
+ const lower = aliasName.toLowerCase();
718
+ const existingIdx = existing.findIndex((item) => item.name.trim().toLowerCase() === lower);
719
+ const next = existingIdx >= 0
720
+ ? existing.map((item, idx) => (idx === existingIdx ? entry : item))
721
+ : [...existing, entry];
722
+ await options.updateSettings({ customVoices: next });
723
+ }
724
+ return {
725
+ clone: {
726
+ name: aliasName,
727
+ referenceAudio: relAudioPath,
728
+ referenceText: refText,
729
+ bytes: audioBuf.length,
730
+ savedAlias: shouldSaveAlias,
731
+ ...(transcribeWarning ? { transcribeWarning } : {}),
732
+ },
733
+ };
734
+ }
735
+ if (action === 'synthesizeDialogue') {
736
+ if (!Array.isArray(args.segments) || args.segments.length === 0) {
737
+ throw new VoiceStudioError('请提供至少 1 段对话台词(segments)。', 400);
738
+ }
739
+ if (args.segments.length > MAX_DIALOGUE_SEGMENTS) {
740
+ throw new VoiceStudioError(`单次对话合成最多支持 ${MAX_DIALOGUE_SEGMENTS} 段台词。`, 400);
741
+ }
742
+ const rawOutputName = text(args, 'outputName');
743
+ if (rawOutputName && (/[/\\:\0]/.test(rawOutputName) || rawOutputName.includes('..'))) {
744
+ throw new VoiceStudioError('输出文件名包含非法路径字符。', 400, 'pathTraversal');
745
+ }
746
+ const cwd = getWorkspaceDir();
747
+ const bgmFile = text(args, 'bgmFile');
748
+ let bgmPath;
749
+ if (bgmFile !== '') {
750
+ bgmPath = assertAudioFile(bgmFile, cwd);
751
+ if (!hasFfmpeg()) {
752
+ throw new VoiceStudioError('使用背景音乐混音需要本机安装 ffmpeg。', 400, 'ffmpegRequired');
753
+ }
754
+ }
755
+ const parsedSegments = [];
756
+ let totalChars = 0;
757
+ for (let i = 0; i < args.segments.length; i++) {
758
+ const seg = record(args.segments[i]);
759
+ const segText = text(seg, 'text');
760
+ if (!segText) {
761
+ throw new VoiceStudioError(`segments[${i}].text 不能为空。`, 400);
762
+ }
763
+ totalChars += segText.length;
764
+ const rawPause = seg.pauseMs;
765
+ const pauseMs = typeof rawPause === 'number' && Number.isFinite(rawPause)
766
+ ? Math.min(5000, Math.max(0, Math.round(rawPause)))
767
+ : undefined;
768
+ parsedSegments.push({
769
+ speaker: text(seg, 'speaker') || undefined,
770
+ voice: text(seg, 'voice') || undefined,
771
+ persona: text(seg, 'persona') || undefined,
772
+ text: segText,
773
+ rate: text(seg, 'rate') || undefined,
774
+ pitch: text(seg, 'pitch') || undefined,
775
+ pauseMs,
776
+ instructions: text(seg, 'instructions') || undefined,
777
+ });
778
+ }
779
+ if (totalChars > MAX_DIALOGUE_CHARS) {
780
+ throw new VoiceStudioError(`对话总字数不能超过 ${MAX_DIALOGUE_CHARS} 个字符。`, 400, 'textTooLong');
781
+ }
782
+ const useEdge = resolveUseEdge(cfg);
783
+ const pronunciations = readPronunciations(cfg);
784
+ const refAudioCache = new Map();
785
+ const buffers = [];
786
+ const allCues = [];
787
+ let offsetMs = 0;
788
+ for (let i = 0; i < parsedSegments.length; i++) {
789
+ signal.throwIfAborted();
790
+ const seg = parsedSegments[i];
791
+ const spec = resolveStudioVoice(cfg, useEdge, cwd, {
792
+ voice: seg.voice,
793
+ persona: seg.persona,
794
+ speakerAlias: seg.speaker,
795
+ rate: seg.rate,
796
+ pitch: seg.pitch,
797
+ pauseMs: seg.pauseMs,
798
+ instructions: seg.instructions,
799
+ }, refAudioCache);
800
+ const { audio: segAudio, info: segInfo, cues: segCues } = await synthesizeSegmentWithCues(cfg, useEdge, seg.text, spec, pronunciations, signal);
801
+ signal.throwIfAborted();
802
+ const prefixedCues = seg.speaker
803
+ ? segCues.map((cue) => ({ ...cue, text: `[${seg.speaker}] ${cue.text}` }))
804
+ : segCues;
805
+ allCues.push(...offsetCues(prefixedCues, offsetMs, allCues.length + 1));
806
+ buffers.push(segAudio);
807
+ offsetMs += segInfo.durationMs;
808
+ if (i < parsedSegments.length - 1 && spec.pauseMs > 0) {
809
+ const silenceBuf = createSilenceMp3(spec.pauseMs, segInfo.sampleRate || 24000);
810
+ if (silenceBuf.length > 0) {
811
+ buffers.push(silenceBuf);
812
+ offsetMs += inspectMp3Buffer(silenceBuf).durationMs;
813
+ }
814
+ }
815
+ }
816
+ let finalAudio = concatMp3Buffers(buffers);
817
+ if (bgmPath !== undefined) {
818
+ const rawVol = typeof args.bgmVolume === 'number' ? args.bgmVolume : Number(args.bgmVolume);
819
+ const bgmVolume = Number.isFinite(rawVol) ? Math.min(1.0, Math.max(0.02, rawVol)) : 0.18;
820
+ finalAudio = await mixAudioWithBgm(finalAudio, {
821
+ bgmPath,
822
+ bgmVolume,
823
+ ducking: true,
824
+ normalizeLoudness: args.normalizeLoudness === true,
825
+ }, signal);
826
+ }
827
+ else if (args.normalizeLoudness === true && hasFfmpeg()) {
828
+ finalAudio = await normalizeAudioLoudness(finalAudio, signal);
829
+ }
830
+ let savedMp3 = '';
831
+ let savedSubtitle = '';
832
+ if (args.saveToWorkspace === true) {
833
+ const outDir = ensureWorkspaceDirectory('voice_outputs', cwd);
834
+ const rawStem = rawOutputName ? rawOutputName.replace(/\.(mp3|srt|vtt)$/i, '').trim() : '';
835
+ const safeStem = (rawStem || `dialogue-${Date.now()}`)
836
+ .normalize('NFKC')
837
+ .replace(/[^\p{L}\p{N}._-]+/gu, '-')
838
+ .replace(/-+/g, '-')
839
+ .replace(/^[.-]+|[.-]+$/g, '')
840
+ .slice(0, 64) || `dialogue-${Date.now()}`;
841
+ const mp3Target = resolveOutputPath(join(outDir, `${safeStem}.mp3`), '', cfg.overwrite, cwd);
842
+ const writtenMp3 = writeWorkspaceFile(mp3Target, finalAudio, cwd, undefined, cfg.overwrite);
843
+ const srtCandidate = join(dirname(writtenMp3), basename(writtenMp3, extname(writtenMp3)) + '.srt');
844
+ const srtTarget = resolveOutputPath(srtCandidate, '', cfg.overwrite, cwd);
845
+ const writtenSrt = writeWorkspaceFile(srtTarget, formatSubtitles(allCues, 'srt'), cwd, undefined, cfg.overwrite);
846
+ savedMp3 = toWorkspaceRelativePath(writtenMp3, cwd);
847
+ savedSubtitle = toWorkspaceRelativePath(writtenSrt, cwd);
848
+ }
849
+ const info = inspectMp3Buffer(finalAudio);
850
+ return {
851
+ synthesis: {
852
+ audioBase64: finalAudio.toString('base64'),
853
+ mime: 'audio/mpeg',
854
+ bytes: finalAudio.length,
855
+ durationMs: info.durationMs,
856
+ durationSec: Math.round(info.durationMs / 100) / 10,
857
+ cues: allCues,
858
+ ...(savedMp3 ? { savedMp3, savedSubtitle } : {}),
859
+ },
860
+ };
861
+ }
862
+ if (action === 'workspaceLibrary') {
863
+ const cwd = getWorkspaceDir();
864
+ const root = resolveWorkspaceRoot(cwd);
865
+ const items = [];
866
+ for (const relDir of LIBRARY_SUBDIRS) {
867
+ const dirPath = relDir === '' ? root : join(root, relDir);
868
+ let realDir;
869
+ try {
870
+ const dirStat = lstatSync(dirPath);
871
+ if (dirStat.isSymbolicLink() || !dirStat.isDirectory())
872
+ continue;
873
+ realDir = realpathSync(dirPath);
874
+ if (!isInsideRoot(root, realDir))
875
+ continue;
876
+ }
877
+ catch {
878
+ continue;
879
+ }
880
+ let entries;
881
+ try {
882
+ entries = readdirSync(realDir, { withFileTypes: true });
883
+ }
884
+ catch {
885
+ continue;
886
+ }
887
+ for (const entry of entries) {
888
+ if (entry.isSymbolicLink() || !entry.isFile())
889
+ continue;
890
+ const ext = extname(entry.name).toLowerCase();
891
+ if (!LIBRARY_AUDIO_EXTS.has(ext))
892
+ continue;
893
+ const candidateAudio = join(realDir, entry.name);
894
+ let verifiedAudio;
895
+ let st;
896
+ try {
897
+ verifiedAudio = assertAudioFile(candidateAudio, cwd);
898
+ st = statSync(verifiedAudio);
899
+ }
900
+ catch {
901
+ continue;
902
+ }
903
+ if (!st.isFile() || st.size <= 0)
904
+ continue;
905
+ const relAudio = toWorkspaceRelativePath(verifiedAudio, cwd);
906
+ const stem = basename(entry.name, extname(entry.name));
907
+ let subtitlePath;
908
+ for (const subExt of ['.srt', '.vtt']) {
909
+ const subCandidate = join(realDir, stem + subExt);
910
+ try {
911
+ const subStat = lstatSync(subCandidate);
912
+ if (subStat.isSymbolicLink() || !subStat.isFile())
913
+ continue;
914
+ const realSub = realpathSync(subCandidate);
915
+ if (!isInsideRoot(root, realSub))
916
+ continue;
917
+ subtitlePath = relative(root, realSub).replace(/\\/g, '/');
918
+ break;
919
+ }
920
+ catch {
921
+ // 无同名陪伴字幕
922
+ }
923
+ }
924
+ items.push({
925
+ path: relAudio,
926
+ name: entry.name,
927
+ bytes: st.size,
928
+ mtimeMs: Math.round(st.mtimeMs),
929
+ ...(subtitlePath ? { subtitlePath } : {}),
930
+ });
931
+ }
932
+ }
933
+ items.sort((a, b) => b.mtimeMs - a.mtimeMs || a.path.localeCompare(b.path));
934
+ return { items: items.slice(0, MAX_LIBRARY_ITEMS) };
935
+ }
936
+ if (action === 'loadWorkspaceMedia') {
937
+ const rawPath = text(args, 'path');
938
+ if (!rawPath) {
939
+ throw new VoiceStudioError('请提供要加载的音频文件路径(path)。', 400);
940
+ }
941
+ const cwd = getWorkspaceDir();
942
+ const root = resolveWorkspaceRoot(cwd);
943
+ const audioPath = assertAudioFile(rawPath, cwd);
944
+ const ext = extname(audioPath).toLowerCase();
945
+ if (!LIBRARY_AUDIO_EXTS.has(ext)) {
946
+ throw new VoiceStudioError('仅支持加载工作区内的音频文件(.mp3/.wav/.m4a/.ogg/.webm/.flac)。', 400);
947
+ }
948
+ const st = statSync(audioPath);
949
+ if (st.size <= 0) {
950
+ throw new VoiceStudioError('音频文件为空。', 400);
951
+ }
952
+ if (st.size > MAX_MEDIA_BYTES) {
953
+ throw new VoiceStudioError('工作区音频文件超过 15 MiB 在线预览上限。', 413, 'bodyLarge');
954
+ }
955
+ const buf = readFileSync(audioPath);
956
+ if (buf.length > MAX_MEDIA_BYTES) {
957
+ throw new VoiceStudioError('工作区音频文件超过 15 MiB 在线预览上限。', 413, 'bodyLarge');
958
+ }
959
+ let cues = [];
960
+ const audioDir = dirname(audioPath);
961
+ const stem = basename(audioPath, extname(audioPath));
962
+ for (const subExt of ['.srt', '.vtt']) {
963
+ const subCandidate = join(audioDir, stem + subExt);
964
+ try {
965
+ const subStat = lstatSync(subCandidate);
966
+ if (subStat.isSymbolicLink() || !subStat.isFile() || subStat.size > 2 * 1024 * 1024)
967
+ continue;
968
+ const realSub = realpathSync(subCandidate);
969
+ if (!isInsideRoot(root, realSub))
970
+ continue;
971
+ cues = parseSubtitleFile(readFileSync(realSub, 'utf8'));
972
+ break;
973
+ }
974
+ catch {
975
+ // 忽略不存在或不可读的字幕文件
976
+ }
977
+ }
978
+ let durationMs = 0;
979
+ if (ext === '.mp3') {
980
+ durationMs = inspectMp3Buffer(buf).durationMs;
981
+ }
982
+ else if (cues.length > 0) {
983
+ durationMs = cues[cues.length - 1].endMs;
984
+ }
985
+ const durationSec = Math.round(durationMs / 100) / 10;
986
+ const relPath = toWorkspaceRelativePath(audioPath, cwd);
987
+ return {
988
+ media: {
989
+ path: relPath,
990
+ name: basename(audioPath),
991
+ mime: audioMimeFromExt(ext),
992
+ audioBase64: buf.toString('base64'),
993
+ bytes: buf.length,
994
+ durationMs,
995
+ durationSec,
996
+ cues,
997
+ },
998
+ };
999
+ }
500
1000
  if (action === 'updateConfig') {
501
1001
  if (!writable() || typeof options.updateSettings !== 'function') {
502
1002
  throw new VoiceStudioError('当前配置是只读的,无法保存设置。', 403, 'readonly');
@@ -616,11 +1116,13 @@ export function createVoiceStudioBackend(readCfg, options = {}) {
616
1116
  }
617
1117
  export function installVoiceStudioWeb(ctx, readCfg, deps = {}) {
618
1118
  let updateSettings = deps.updateSettings;
1119
+ let isHostWritable;
619
1120
  const backend = createVoiceStudioBackend(readCfg, {
620
1121
  ...deps,
621
- writable: deps.writable ?? (() => typeof updateSettings === 'function'),
1122
+ writable: deps.writable ??
1123
+ (() => typeof updateSettings === 'function' && (isHostWritable ? isHostWritable() : true)),
622
1124
  updateSettings: (patch) => {
623
- if (typeof updateSettings !== 'function') {
1125
+ if (typeof updateSettings !== 'function' || (isHostWritable && !isHostWritable())) {
624
1126
  return Promise.reject(new VoiceStudioError('当前配置是只读的,无法保存设置。', 403, 'readonly'));
625
1127
  }
626
1128
  return updateSettings(patch);
@@ -631,9 +1133,21 @@ export function installVoiceStudioWeb(ctx, readCfg, deps = {}) {
631
1133
  if (!deps.updateSettings) {
632
1134
  try {
633
1135
  ctx.inject(['settings'], (host) => {
634
- const pluginId = ctx.fiber?.entry?.options?.id || 'dsh-voice';
1136
+ const rawId = ctx.fiber?.entry?.options?.id;
1137
+ const pluginId = typeof rawId === 'string' && rawId.trim() !== '' ? rawId.trim() : 'voice';
635
1138
  if (typeof host?.settings?.update === 'function') {
636
- updateSettings = (patch) => host.settings.update(pluginId, patch);
1139
+ const setFn = (patch) => host.settings.update(pluginId, patch);
1140
+ const checkWritable = () => host.settings?.writable !== false;
1141
+ updateSettings = setFn;
1142
+ isHostWritable = checkWritable;
1143
+ if (typeof host.effect === 'function') {
1144
+ host.effect(() => () => {
1145
+ if (updateSettings === setFn)
1146
+ updateSettings = undefined;
1147
+ if (isHostWritable === checkWritable)
1148
+ isHostWritable = undefined;
1149
+ }, 'dsh-voice: settings binding');
1150
+ }
637
1151
  }
638
1152
  });
639
1153
  }