dsh-voice 0.7.0 → 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 +9 -0
- package/README.en.md +18 -14
- package/README.md +18 -14
- package/docs/USAGE.en.md +58 -23
- package/docs/USAGE.md +57 -22
- package/lib/config.d.ts +15 -0
- package/lib/config.js +70 -0
- package/lib/edge-tts.d.ts +15 -0
- package/lib/edge-tts.js +133 -0
- package/lib/host-config.js +13 -0
- package/lib/openai-tts.d.ts +7 -0
- package/lib/openai-tts.js +22 -2
- package/lib/tools.d.ts +4 -1
- package/lib/tools.js +362 -58
- package/lib/tts-text.d.ts +5 -5
- package/lib/tts-text.js +60 -0
- package/lib/update-client.js +1 -1
- package/lib/voices.d.ts +18 -3
- package/lib/voices.js +132 -6
- package/locale/en.json +1 -1
- package/locale/zh.json +1 -1
- package/package.json +1 -1
- package/skills/voice-workflow/SKILL.md +37 -19
package/lib/config.js
CHANGED
|
@@ -47,6 +47,74 @@ export function resolveTtsApiKey(config, env = process.env) {
|
|
|
47
47
|
function defaultWarn(message) {
|
|
48
48
|
console.warn('[dsh-voice] ' + message);
|
|
49
49
|
}
|
|
50
|
+
function isPlainObject(value) {
|
|
51
|
+
if (typeof value !== 'object' || value === null || Array.isArray(value))
|
|
52
|
+
return false;
|
|
53
|
+
const proto = Object.getPrototypeOf(value);
|
|
54
|
+
return proto === Object.prototype || proto === null;
|
|
55
|
+
}
|
|
56
|
+
function trimOptionalString(value) {
|
|
57
|
+
if (typeof value !== 'string')
|
|
58
|
+
return undefined;
|
|
59
|
+
const trimmed = value.trim();
|
|
60
|
+
return trimmed === '' ? undefined : trimmed;
|
|
61
|
+
}
|
|
62
|
+
function parseCustomVoices(raw, onWarning) {
|
|
63
|
+
if (raw === undefined)
|
|
64
|
+
return [];
|
|
65
|
+
if (!Array.isArray(raw)) {
|
|
66
|
+
onWarning('customVoices 配置不合法(必须是数组),已忽略该项。');
|
|
67
|
+
return [];
|
|
68
|
+
}
|
|
69
|
+
const result = [];
|
|
70
|
+
for (let i = 0; i < raw.length; i += 1) {
|
|
71
|
+
const item = raw[i];
|
|
72
|
+
if (!isPlainObject(item))
|
|
73
|
+
continue;
|
|
74
|
+
const name = trimOptionalString(item.name);
|
|
75
|
+
if (name === undefined)
|
|
76
|
+
continue;
|
|
77
|
+
const entry = { name };
|
|
78
|
+
const voice = trimOptionalString(item.voice);
|
|
79
|
+
if (voice !== undefined)
|
|
80
|
+
entry.voice = voice;
|
|
81
|
+
const persona = trimOptionalString(item.persona);
|
|
82
|
+
if (persona !== undefined)
|
|
83
|
+
entry.persona = persona;
|
|
84
|
+
const rate = trimOptionalString(item.rate);
|
|
85
|
+
if (rate !== undefined) {
|
|
86
|
+
if (isValidProsody(rate))
|
|
87
|
+
entry.rate = rate;
|
|
88
|
+
else
|
|
89
|
+
onWarning(`customVoices[${i}].rate 配置不合法(格式如 +0%、+15%、-10%),已忽略该字段,当前值:${rate}`);
|
|
90
|
+
}
|
|
91
|
+
const pitch = trimOptionalString(item.pitch);
|
|
92
|
+
if (pitch !== undefined) {
|
|
93
|
+
if (isValidProsody(pitch))
|
|
94
|
+
entry.pitch = pitch;
|
|
95
|
+
else
|
|
96
|
+
onWarning(`customVoices[${i}].pitch 配置不合法(格式如 +0Hz、+2Hz、-1st),已忽略该字段,当前值:${pitch}`);
|
|
97
|
+
}
|
|
98
|
+
const instructions = trimOptionalString(item.instructions);
|
|
99
|
+
if (instructions !== undefined)
|
|
100
|
+
entry.instructions = instructions;
|
|
101
|
+
const referenceAudio = trimOptionalString(item.referenceAudio);
|
|
102
|
+
if (referenceAudio !== undefined)
|
|
103
|
+
entry.referenceAudio = referenceAudio;
|
|
104
|
+
const referenceText = trimOptionalString(item.referenceText);
|
|
105
|
+
if (referenceText !== undefined)
|
|
106
|
+
entry.referenceText = referenceText;
|
|
107
|
+
result.push(entry);
|
|
108
|
+
}
|
|
109
|
+
return result;
|
|
110
|
+
}
|
|
111
|
+
/** 按别名(大小写不敏感)查找自定义音色配置。 */
|
|
112
|
+
export function resolveCustomVoice(customVoices, nameOrId) {
|
|
113
|
+
const target = (typeof nameOrId === 'string' ? nameOrId : '').trim().toLowerCase();
|
|
114
|
+
if (target === '')
|
|
115
|
+
return undefined;
|
|
116
|
+
return customVoices.find((alias) => alias.name.trim().toLowerCase() === target);
|
|
117
|
+
}
|
|
50
118
|
/**
|
|
51
119
|
* 解析并校验配置:非法字段逐项回退到默认值,其余字段照常生效。
|
|
52
120
|
* @param onWarning 非法字段的告警回调,默认打到 console.warn。
|
|
@@ -91,6 +159,7 @@ export function resolveConfig(config, onWarning = defaultWarn) {
|
|
|
91
159
|
}
|
|
92
160
|
}
|
|
93
161
|
const overwrite = cfg.overwrite === true;
|
|
162
|
+
const customVoices = parseCustomVoices(cfg.customVoices, onWarning);
|
|
94
163
|
return {
|
|
95
164
|
ttsEngine,
|
|
96
165
|
ttsBaseUrl,
|
|
@@ -106,5 +175,6 @@ export function resolveConfig(config, onWarning = defaultWarn) {
|
|
|
106
175
|
proxyUrl,
|
|
107
176
|
timeoutMs,
|
|
108
177
|
overwrite,
|
|
178
|
+
customVoices,
|
|
109
179
|
};
|
|
110
180
|
}
|
package/lib/edge-tts.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { type VoiceInfo } from './voices.js';
|
|
1
2
|
/** 重置时钟偏差缓存(测试专用)。 */
|
|
2
3
|
export declare function resetClockSkewForTest(): void;
|
|
3
4
|
/** 读取当前时钟偏差(秒)。 */
|
|
@@ -65,3 +66,17 @@ export declare function synthesizeSpeechWithMetadata(options: TtsOptions & {
|
|
|
65
66
|
* @throws 文本为空/超长 / 连接失败 / 超时 / 无音频数据时抛中文错误。
|
|
66
67
|
*/
|
|
67
68
|
export declare function synthesizeSpeech(options: TtsOptions, deps?: TtsDeps, timeoutMs?: number, signal?: AbortSignal): Promise<Buffer>;
|
|
69
|
+
/** 在线音色列表拉取的可注入依赖。 */
|
|
70
|
+
export interface FetchEdgeVoicesDeps {
|
|
71
|
+
fetchImpl?: typeof fetch;
|
|
72
|
+
nowSeconds?: () => number;
|
|
73
|
+
signal?: AbortSignal;
|
|
74
|
+
forceRefresh?: boolean;
|
|
75
|
+
}
|
|
76
|
+
/** 重置 Edge 在线音色内存缓存(测试专用)。 */
|
|
77
|
+
export declare function resetEdgeVoicesCacheForTest(): void;
|
|
78
|
+
/**
|
|
79
|
+
* 动态拉取微软 Edge 在线全量(300+)音色目录并缓存 24 小时。
|
|
80
|
+
* @throws 请求失败 / 重定向 / 响应异常 / 超时时抛中文错误。
|
|
81
|
+
*/
|
|
82
|
+
export declare function fetchEdgeVoices(deps?: FetchEdgeVoicesDeps, timeoutMs?: number): Promise<VoiceInfo[]>;
|
package/lib/edge-tts.js
CHANGED
|
@@ -9,8 +9,11 @@ import { randomBytes, randomUUID } from 'node:crypto';
|
|
|
9
9
|
import WebSocket from 'ws';
|
|
10
10
|
import { edgeTokenV2 } from './edge-drm-v2.js';
|
|
11
11
|
import { getProxyWsAgent } from './proxy-fetch.js';
|
|
12
|
+
import { VOICES } from './voices.js';
|
|
12
13
|
const TRUSTED_CLIENT_TOKEN = '6A5AA1D4EAFF4E9FB37E23D68491D6F4';
|
|
13
14
|
const WS_BASE = 'wss://speech.platform.bing.com/consumer/speech/synthesize/readaloud/edge/v1';
|
|
15
|
+
const VOICES_LIST_URL = 'https://speech.platform.bing.com/consumer/speech/synthesize/readaloud/voices/list';
|
|
16
|
+
const EDGE_VOICES_CACHE_TTL_MS = 24 * 60 * 60 * 1000;
|
|
14
17
|
const OUTPUT_FORMAT = 'audio-24khz-48kbitrate-mono-mp3';
|
|
15
18
|
const SEC_MS_GEC_VERSION = '2-143.0.3650.75';
|
|
16
19
|
const WIN_EPOCH_SECONDS = 11644473600;
|
|
@@ -327,3 +330,133 @@ export async function synthesizeSpeech(options, deps = {}, timeoutMs = 30000, si
|
|
|
327
330
|
const result = await synthesizeSpeechWithMetadata(options, deps, timeoutMs, signal);
|
|
328
331
|
return result.audio;
|
|
329
332
|
}
|
|
333
|
+
let cachedEdgeVoices;
|
|
334
|
+
/** 重置 Edge 在线音色内存缓存(测试专用)。 */
|
|
335
|
+
export function resetEdgeVoicesCacheForTest() {
|
|
336
|
+
cachedEdgeVoices = undefined;
|
|
337
|
+
}
|
|
338
|
+
function throwIfAborted(signal) {
|
|
339
|
+
if (signal?.aborted === true)
|
|
340
|
+
throw signal.reason;
|
|
341
|
+
}
|
|
342
|
+
function raceAbortSignal(value, signal) {
|
|
343
|
+
const promise = Promise.resolve(value);
|
|
344
|
+
if (signal === undefined || typeof signal.addEventListener !== 'function')
|
|
345
|
+
return promise;
|
|
346
|
+
if (signal.aborted === true)
|
|
347
|
+
return Promise.reject(signal.reason);
|
|
348
|
+
return new Promise((resolvePromise, rejectPromise) => {
|
|
349
|
+
const onAbort = () => rejectPromise(signal.reason);
|
|
350
|
+
signal.addEventListener('abort', onAbort, { once: true });
|
|
351
|
+
promise.then((settled) => { signal.removeEventListener('abort', onAbort); resolvePromise(settled); }, (error) => { signal.removeEventListener('abort', onAbort); rejectPromise(error); });
|
|
352
|
+
});
|
|
353
|
+
}
|
|
354
|
+
function collectLowercaseTags(builtinTags, voiceTag) {
|
|
355
|
+
const tagSet = new Set();
|
|
356
|
+
const pushAll = (items) => {
|
|
357
|
+
if (!Array.isArray(items))
|
|
358
|
+
return;
|
|
359
|
+
for (const item of items) {
|
|
360
|
+
if (typeof item !== 'string')
|
|
361
|
+
continue;
|
|
362
|
+
const normalized = item.trim().toLowerCase();
|
|
363
|
+
if (normalized !== '')
|
|
364
|
+
tagSet.add(normalized);
|
|
365
|
+
}
|
|
366
|
+
};
|
|
367
|
+
pushAll(builtinTags);
|
|
368
|
+
pushAll(voiceTag?.ContentCategories);
|
|
369
|
+
pushAll(voiceTag?.VoicePersonalities);
|
|
370
|
+
return Array.from(tagSet);
|
|
371
|
+
}
|
|
372
|
+
/**
|
|
373
|
+
* 动态拉取微软 Edge 在线全量(300+)音色目录并缓存 24 小时。
|
|
374
|
+
* @throws 请求失败 / 重定向 / 响应异常 / 超时时抛中文错误。
|
|
375
|
+
*/
|
|
376
|
+
export async function fetchEdgeVoices(deps = {}, timeoutMs = 15000) {
|
|
377
|
+
throwIfAborted(deps.signal);
|
|
378
|
+
if (deps.forceRefresh !== true
|
|
379
|
+
&& cachedEdgeVoices !== undefined
|
|
380
|
+
&& Date.now() - cachedEdgeVoices.fetchedAtMs < EDGE_VOICES_CACHE_TTL_MS) {
|
|
381
|
+
return cachedEdgeVoices.voices;
|
|
382
|
+
}
|
|
383
|
+
const nowSec = deps.nowSeconds ? deps.nowSeconds() : Date.now() / 1000 + clockSkewSeconds;
|
|
384
|
+
const token = generateSecMsGec(nowSec);
|
|
385
|
+
const url = `${VOICES_LIST_URL}?trustedclienttoken=${TRUSTED_CLIENT_TOKEN}&Sec-MS-GEC=${token}&Sec-MS-GEC-Version=${SEC_MS_GEC_VERSION}`;
|
|
386
|
+
const fetchImpl = deps.fetchImpl ?? globalThis.fetch;
|
|
387
|
+
const timeoutSignal = AbortSignal.timeout(timeoutMs);
|
|
388
|
+
const requestSignal = deps.signal === undefined ? timeoutSignal : AbortSignal.any([timeoutSignal, deps.signal]);
|
|
389
|
+
let response;
|
|
390
|
+
try {
|
|
391
|
+
response = await raceAbortSignal(fetchImpl(url, {
|
|
392
|
+
method: 'GET',
|
|
393
|
+
redirect: 'manual',
|
|
394
|
+
headers: {
|
|
395
|
+
'User-Agent': WSS_HEADERS['User-Agent'],
|
|
396
|
+
Accept: 'application/json',
|
|
397
|
+
'Sec-MS-GEC': token,
|
|
398
|
+
'Sec-MS-GEC-Version': SEC_MS_GEC_VERSION,
|
|
399
|
+
},
|
|
400
|
+
signal: requestSignal,
|
|
401
|
+
}), requestSignal);
|
|
402
|
+
}
|
|
403
|
+
catch (error) {
|
|
404
|
+
throwIfAborted(deps.signal);
|
|
405
|
+
if (timeoutSignal.aborted)
|
|
406
|
+
throw new Error('拉取 Edge 在线音色列表超时,请重试或检查网络。');
|
|
407
|
+
throw new Error('拉取 Edge 在线音色列表失败:' + (error instanceof Error ? error.message : String(error)));
|
|
408
|
+
}
|
|
409
|
+
if (response.status >= 300 && response.status < 400) {
|
|
410
|
+
try {
|
|
411
|
+
await raceAbortSignal(response.body?.cancel(), requestSignal);
|
|
412
|
+
}
|
|
413
|
+
catch { /* 忽略 */ }
|
|
414
|
+
throwIfAborted(deps.signal);
|
|
415
|
+
throw new Error('Edge 在线音色列表返回重定向(HTTP ' + response.status + '),已阻止跳转。');
|
|
416
|
+
}
|
|
417
|
+
if (!response.ok) {
|
|
418
|
+
try {
|
|
419
|
+
await raceAbortSignal(response.body?.cancel(), requestSignal);
|
|
420
|
+
}
|
|
421
|
+
catch { /* 忽略 */ }
|
|
422
|
+
throwIfAborted(deps.signal);
|
|
423
|
+
throw new Error('拉取 Edge 在线音色列表失败:HTTP ' + response.status + '。');
|
|
424
|
+
}
|
|
425
|
+
let rawList;
|
|
426
|
+
try {
|
|
427
|
+
rawList = await raceAbortSignal(response.json(), requestSignal);
|
|
428
|
+
}
|
|
429
|
+
catch (error) {
|
|
430
|
+
throwIfAborted(deps.signal);
|
|
431
|
+
if (timeoutSignal.aborted)
|
|
432
|
+
throw new Error('拉取 Edge 在线音色列表超时,请重试或检查网络。');
|
|
433
|
+
throw new Error('Edge 在线音色列表响应不是合法 JSON:' + (error instanceof Error ? error.message : String(error)));
|
|
434
|
+
}
|
|
435
|
+
if (!Array.isArray(rawList) || rawList.length === 0) {
|
|
436
|
+
throw new Error('Edge 在线音色列表为空或格式异常。');
|
|
437
|
+
}
|
|
438
|
+
const builtinById = new Map(VOICES.map((voice) => [voice.id, voice]));
|
|
439
|
+
const mapped = [];
|
|
440
|
+
for (const entry of rawList) {
|
|
441
|
+
if (typeof entry !== 'object' || entry === null)
|
|
442
|
+
continue;
|
|
443
|
+
const item = entry;
|
|
444
|
+
if (typeof item.ShortName !== 'string' || item.ShortName.trim() === '')
|
|
445
|
+
continue;
|
|
446
|
+
const id = item.ShortName.trim();
|
|
447
|
+
const gender = item.Gender === 'Male' ? 'Male' : 'Female';
|
|
448
|
+
const builtin = builtinById.get(id);
|
|
449
|
+
const localeRaw = typeof item.Locale === 'string' && item.Locale.trim() !== ''
|
|
450
|
+
? item.Locale.trim()
|
|
451
|
+
: id.split('-').slice(0, 2).join('-');
|
|
452
|
+
const friendlyRaw = typeof item.FriendlyName === 'string' ? item.FriendlyName.trim() : '';
|
|
453
|
+
const locale = builtin !== undefined ? builtin.locale : `${localeRaw}(${friendlyRaw || id})`;
|
|
454
|
+
const tags = collectLowercaseTags(builtin?.tags, item.VoiceTag);
|
|
455
|
+
mapped.push({ id, gender, locale, tags });
|
|
456
|
+
}
|
|
457
|
+
if (mapped.length === 0) {
|
|
458
|
+
throw new Error('Edge 在线音色列表为空或格式异常。');
|
|
459
|
+
}
|
|
460
|
+
cachedEdgeVoices = { voices: mapped, fetchedAtMs: Date.now() };
|
|
461
|
+
return mapped;
|
|
462
|
+
}
|
package/lib/host-config.js
CHANGED
|
@@ -464,6 +464,17 @@ export const Config = z.object({
|
|
|
464
464
|
.description('单次合成或转写超时毫秒数(5000–600000,默认 60000)'),
|
|
465
465
|
overwrite: z.boolean().volatile()
|
|
466
466
|
.description('同名输出文件是否直接覆盖(默认关闭,自动加序号防误盖)'),
|
|
467
|
+
customVoices: z.array(z.object({
|
|
468
|
+
name: z.string().required().description('自定义音色别名(如 老王、女主持、悬疑旁白)'),
|
|
469
|
+
voice: z.string().description('底层音色 ID 或混合表达式(可选,如 zh-CN-YunzeNeural、FunAudioLLM/CosyVoice2-0.5B:alex、zf_xiaobei*0.6+zf_xiaoni*0.4)'),
|
|
470
|
+
persona: z.string().description('继承的内置人设 ID(可选,如 late-night-radio)'),
|
|
471
|
+
rate: z.string().description('专属语速(可选,如 -8%、+10%)'),
|
|
472
|
+
pitch: z.string().description('专属音调(可选,如 -2Hz、+2Hz)'),
|
|
473
|
+
instructions: z.string().description('情绪/语气指令(可选,用于 gpt-4o-mini-tts 或 CosyVoice2,如“用低沉神秘的语气轻声讲述”)'),
|
|
474
|
+
referenceAudio: z.string().description('零样本克隆参考音频路径(可选,会话工作区内 5–10 秒干净人声 wav/mp3)'),
|
|
475
|
+
referenceText: z.string().description('参考音频对应的原始台词文本(可选,CosyVoice2 等克隆模型推荐填写)'),
|
|
476
|
+
})).collapse().volatile()
|
|
477
|
+
.description('自定义音色与零样本克隆别名列表(保存后立即生效)'),
|
|
467
478
|
}).i18n({
|
|
468
479
|
zh: {
|
|
469
480
|
$description: '零密钥 Edge / OpenAI 兼容语音合成(TTS)与语音转文字(ASR)设置(保存立即生效)',
|
|
@@ -481,6 +492,7 @@ export const Config = z.object({
|
|
|
481
492
|
proxyUrl: '网络代理地址(可选,支持 http(s):// 或 socks5(h)://,如 http://127.0.0.1:7890)',
|
|
482
493
|
timeoutMs: '请求超时毫秒数(5000–600000,默认 60000)',
|
|
483
494
|
overwrite: '允许覆盖同名音频或转写文本(默认关闭,自动追加序号)',
|
|
495
|
+
customVoices: '自定义音色与零样本克隆别名列表(可为常用音色、人设或参考音频命名,保存后立即生效)',
|
|
484
496
|
},
|
|
485
497
|
en: {
|
|
486
498
|
$description: 'Keyless Edge / OpenAI-compatible TTS read-aloud and ASR transcription settings (live on save)',
|
|
@@ -498,6 +510,7 @@ export const Config = z.object({
|
|
|
498
510
|
proxyUrl: 'Optional proxy URL (supports http(s):// or socks5(h)://, e.g. http://127.0.0.1:7890)',
|
|
499
511
|
timeoutMs: 'Per-request timeout in ms (5000–600000, default 60000)',
|
|
500
512
|
overwrite: 'Overwrite existing output files instead of auto-numbering (default off)',
|
|
513
|
+
customVoices: 'Custom voice and zero-shot cloning aliases (name reusable voices, personas, or reference audio clips; live on save)',
|
|
501
514
|
},
|
|
502
515
|
});
|
|
503
516
|
/** 活引用视图:同时兼容 DSH >=0.1.7 的 { get() } 活引用与旧宿主/测试传入的普通对象。 */
|
package/lib/openai-tts.d.ts
CHANGED
|
@@ -3,12 +3,19 @@
|
|
|
3
3
|
*
|
|
4
4
|
* @module dsh-voice/openai-tts
|
|
5
5
|
*/
|
|
6
|
+
/** 零样本克隆参考音频样本。 */
|
|
7
|
+
export interface ReferenceVoiceSample {
|
|
8
|
+
audioDataUri: string;
|
|
9
|
+
text?: string;
|
|
10
|
+
}
|
|
6
11
|
/** OpenAI 兼容 TTS 合成选项。 */
|
|
7
12
|
export interface OpenAiTtsOptions {
|
|
8
13
|
text: string;
|
|
9
14
|
voice: string;
|
|
10
15
|
model: string;
|
|
11
16
|
speed?: number;
|
|
17
|
+
instructions?: string;
|
|
18
|
+
references?: ReferenceVoiceSample[];
|
|
12
19
|
signal?: AbortSignal;
|
|
13
20
|
}
|
|
14
21
|
/**
|
package/lib/openai-tts.js
CHANGED
|
@@ -59,18 +59,38 @@ export async function synthesizeOpenAiSpeech(baseUrl, apiKey, options, fetchImpl
|
|
|
59
59
|
if (key === '' && !isLoopbackUrl(trimmedBaseUrl)) {
|
|
60
60
|
throw new Error('未配置 TTS 密钥:请在 DSH「设置 → 插件 → 语音」填写 ttsApiKey 或设置环境变量 DSH_VOICE_TTS_KEY。');
|
|
61
61
|
}
|
|
62
|
+
const hasReferences = Array.isArray(options.references) && options.references.length > 0;
|
|
63
|
+
if (options.voice.trim() === '' && !hasReferences) {
|
|
64
|
+
throw new Error('未指定音色:请填写 voice 或提供参考音频 references。');
|
|
65
|
+
}
|
|
62
66
|
const headers = {
|
|
63
67
|
'content-type': 'application/json',
|
|
64
68
|
};
|
|
65
69
|
if (key !== '') {
|
|
66
70
|
headers.authorization = 'Bearer ' + key;
|
|
67
71
|
}
|
|
72
|
+
const effectiveModel = options.model || 'gpt-4o-mini-tts';
|
|
73
|
+
const isCosyVoice = effectiveModel.toLowerCase().includes('cosyvoice');
|
|
74
|
+
const instr = options.instructions?.trim() ?? '';
|
|
75
|
+
let input = options.text;
|
|
76
|
+
if (instr !== '' && isCosyVoice && !options.text.includes('<|endofprompt|>')) {
|
|
77
|
+
input = instr + '<|endofprompt|>' + options.text;
|
|
78
|
+
}
|
|
68
79
|
const payload = {
|
|
69
|
-
model:
|
|
70
|
-
input
|
|
80
|
+
model: effectiveModel,
|
|
81
|
+
input,
|
|
71
82
|
voice: options.voice,
|
|
72
83
|
response_format: 'mp3',
|
|
73
84
|
...(options.speed !== undefined && options.speed !== 1 ? { speed: options.speed } : {}),
|
|
85
|
+
...(instr !== '' && !isCosyVoice ? { instructions: instr } : {}),
|
|
86
|
+
...(hasReferences
|
|
87
|
+
? {
|
|
88
|
+
references: options.references.map((ref) => ({
|
|
89
|
+
audio: ref.audioDataUri,
|
|
90
|
+
text: ref.text ?? '',
|
|
91
|
+
})),
|
|
92
|
+
}
|
|
93
|
+
: {}),
|
|
74
94
|
};
|
|
75
95
|
const timeoutSignal = AbortSignal.timeout(timeoutMs);
|
|
76
96
|
const requestSignal = abortSignal === undefined ? timeoutSignal : AbortSignal.any([timeoutSignal, abortSignal]);
|
package/lib/tools.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { type ResolvedVoiceConfig } from './config.js';
|
|
2
|
-
import { synthesizeSpeech, synthesizeSpeechWithMetadata } from './edge-tts.js';
|
|
2
|
+
import { fetchEdgeVoices, synthesizeSpeech, synthesizeSpeechWithMetadata } from './edge-tts.js';
|
|
3
3
|
import { synthesizeOpenAiSpeech } from './openai-tts.js';
|
|
4
4
|
import { prepareAudioForAsr, transcribe } from './stt.js';
|
|
5
|
+
import { normalizeAudioLoudness } from './tts-text.js';
|
|
5
6
|
/** 模型可见的内容块。 */
|
|
6
7
|
export interface ContentBlock {
|
|
7
8
|
type: 'text';
|
|
@@ -33,6 +34,8 @@ export interface VoiceToolDeps {
|
|
|
33
34
|
openaiTts?: typeof synthesizeOpenAiSpeech;
|
|
34
35
|
stt?: typeof transcribe;
|
|
35
36
|
prepareAudio?: typeof prepareAudioForAsr;
|
|
37
|
+
fetchVoices?: typeof fetchEdgeVoices;
|
|
38
|
+
normalizeAudio?: typeof normalizeAudioLoudness;
|
|
36
39
|
}
|
|
37
40
|
/**
|
|
38
41
|
* 构建五个语音工具定义(支持静态配置对象或活引用配置 getter)。
|