dsh-voice 0.8.1 → 0.9.1
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 +17 -15
- package/README.md +17 -15
- package/docs/USAGE.en.md +39 -16
- package/docs/USAGE.md +41 -18
- package/lib/client.js +5067 -0
- package/lib/config.d.ts +7 -0
- package/lib/config.js +21 -0
- package/lib/edge-tts.d.ts +4 -0
- package/lib/edge-tts.js +38 -1
- package/lib/host-config.js +7 -0
- package/lib/index.d.ts +11 -10
- package/lib/index.js +81 -1
- package/lib/openai-tts.d.ts +19 -6
- package/lib/openai-tts.js +163 -2
- package/lib/stt.d.ts +19 -0
- package/lib/stt.js +157 -9
- package/lib/tools.d.ts +4 -2
- package/lib/tools.js +380 -70
- package/lib/tts-text.d.ts +46 -3
- package/lib/tts-text.js +345 -16
- package/lib/web.d.ts +66 -0
- package/lib/web.js +691 -0
- package/locale/en.json +1 -1
- package/locale/zh.json +1 -1
- package/package.json +10 -5
- package/skills/voice-workflow/SKILL.md +51 -19
- package/lib/update-client.js +0 -745
package/lib/config.d.ts
CHANGED
|
@@ -7,6 +7,11 @@
|
|
|
7
7
|
export type TtsEngine = 'edge' | 'openai' | 'siliconflow' | 'custom';
|
|
8
8
|
/** ASR 引擎类型。 */
|
|
9
9
|
export type AsrEngine = 'groq' | 'openai' | 'siliconflow' | 'custom';
|
|
10
|
+
/** 发音替读规则(字幕保留原词,朗读按 alias 发音)。 */
|
|
11
|
+
export interface PronunciationRule {
|
|
12
|
+
word: string;
|
|
13
|
+
alias: string;
|
|
14
|
+
}
|
|
10
15
|
/** 自定义音色与零样本克隆别名。 */
|
|
11
16
|
export interface CustomVoiceAlias {
|
|
12
17
|
name: string;
|
|
@@ -35,6 +40,7 @@ export interface VoiceConfig {
|
|
|
35
40
|
timeoutMs?: number;
|
|
36
41
|
overwrite?: boolean;
|
|
37
42
|
customVoices?: CustomVoiceAlias[];
|
|
43
|
+
pronunciations?: PronunciationRule[];
|
|
38
44
|
}
|
|
39
45
|
/** 解析后的配置。 */
|
|
40
46
|
export interface ResolvedVoiceConfig {
|
|
@@ -53,6 +59,7 @@ export interface ResolvedVoiceConfig {
|
|
|
53
59
|
timeoutMs: number;
|
|
54
60
|
overwrite: boolean;
|
|
55
61
|
customVoices: CustomVoiceAlias[];
|
|
62
|
+
pronunciations: PronunciationRule[];
|
|
56
63
|
}
|
|
57
64
|
export declare const ASR_PRESETS: Record<'groq' | 'openai' | 'siliconflow', {
|
|
58
65
|
baseUrl: string;
|
package/lib/config.js
CHANGED
|
@@ -108,6 +108,25 @@ function parseCustomVoices(raw, onWarning) {
|
|
|
108
108
|
}
|
|
109
109
|
return result;
|
|
110
110
|
}
|
|
111
|
+
function parsePronunciations(raw, onWarning) {
|
|
112
|
+
if (raw === undefined)
|
|
113
|
+
return [];
|
|
114
|
+
if (!Array.isArray(raw)) {
|
|
115
|
+
onWarning('pronunciations 配置不合法(必须是数组),已忽略该项。');
|
|
116
|
+
return [];
|
|
117
|
+
}
|
|
118
|
+
const result = [];
|
|
119
|
+
for (const item of raw) {
|
|
120
|
+
if (!isPlainObject(item))
|
|
121
|
+
continue;
|
|
122
|
+
const word = trimOptionalString(item.word);
|
|
123
|
+
const alias = trimOptionalString(item.alias);
|
|
124
|
+
if (word === undefined || alias === undefined)
|
|
125
|
+
continue;
|
|
126
|
+
result.push({ word, alias });
|
|
127
|
+
}
|
|
128
|
+
return result;
|
|
129
|
+
}
|
|
111
130
|
/** 按别名(大小写不敏感)查找自定义音色配置。 */
|
|
112
131
|
export function resolveCustomVoice(customVoices, nameOrId) {
|
|
113
132
|
const target = (typeof nameOrId === 'string' ? nameOrId : '').trim().toLowerCase();
|
|
@@ -160,6 +179,7 @@ export function resolveConfig(config, onWarning = defaultWarn) {
|
|
|
160
179
|
}
|
|
161
180
|
const overwrite = cfg.overwrite === true;
|
|
162
181
|
const customVoices = parseCustomVoices(cfg.customVoices, onWarning);
|
|
182
|
+
const pronunciations = parsePronunciations(cfg.pronunciations, onWarning);
|
|
163
183
|
return {
|
|
164
184
|
ttsEngine,
|
|
165
185
|
ttsBaseUrl,
|
|
@@ -176,5 +196,6 @@ export function resolveConfig(config, onWarning = defaultWarn) {
|
|
|
176
196
|
timeoutMs,
|
|
177
197
|
overwrite,
|
|
178
198
|
customVoices,
|
|
199
|
+
pronunciations,
|
|
179
200
|
};
|
|
180
201
|
}
|
package/lib/edge-tts.d.ts
CHANGED
package/lib/edge-tts.js
CHANGED
|
@@ -78,6 +78,43 @@ function escapeXmlAttr(value) {
|
|
|
78
78
|
.replace(/'/g, ''')
|
|
79
79
|
.replace(/"/g, '"');
|
|
80
80
|
}
|
|
81
|
+
function renderSsmlBody(text, pronunciations) {
|
|
82
|
+
const cleanText = stripIllegalXmlChars(text);
|
|
83
|
+
if (!Array.isArray(pronunciations) || pronunciations.length === 0) {
|
|
84
|
+
return escapeXml(cleanText);
|
|
85
|
+
}
|
|
86
|
+
const map = new Map();
|
|
87
|
+
for (const rule of pronunciations) {
|
|
88
|
+
if (!rule || typeof rule.word !== 'string' || typeof rule.alias !== 'string')
|
|
89
|
+
continue;
|
|
90
|
+
const word = stripIllegalXmlChars(rule.word).trim();
|
|
91
|
+
const alias = stripIllegalXmlChars(rule.alias).trim();
|
|
92
|
+
if (word !== '' && alias !== '') {
|
|
93
|
+
map.set(word, alias);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
if (map.size === 0) {
|
|
97
|
+
return escapeXml(cleanText);
|
|
98
|
+
}
|
|
99
|
+
const sortedWords = Array.from(map.keys()).sort((a, b) => b.length - a.length);
|
|
100
|
+
const regex = new RegExp(sortedWords.map((w) => w.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')).join('|'), 'g');
|
|
101
|
+
let out = '';
|
|
102
|
+
let lastIndex = 0;
|
|
103
|
+
let match;
|
|
104
|
+
while ((match = regex.exec(cleanText)) !== null) {
|
|
105
|
+
if (match.index > lastIndex) {
|
|
106
|
+
out += escapeXml(cleanText.slice(lastIndex, match.index));
|
|
107
|
+
}
|
|
108
|
+
const matchedWord = match[0];
|
|
109
|
+
const alias = map.get(matchedWord);
|
|
110
|
+
out += `<sub alias='${escapeXmlAttr(alias)}'>${escapeXml(matchedWord)}</sub>`;
|
|
111
|
+
lastIndex = match.index + matchedWord.length;
|
|
112
|
+
}
|
|
113
|
+
if (lastIndex < cleanText.length) {
|
|
114
|
+
out += escapeXml(cleanText.slice(lastIndex));
|
|
115
|
+
}
|
|
116
|
+
return out;
|
|
117
|
+
}
|
|
81
118
|
/** 生成 SSML。 */
|
|
82
119
|
export function buildSsml(options) {
|
|
83
120
|
const lang = /^[a-z]{2,3}(?:-[A-Za-z]{2,4})?(?:-[A-Z]{2})?/.exec(options.voice)?.[0] ?? 'zh-CN';
|
|
@@ -90,7 +127,7 @@ export function buildSsml(options) {
|
|
|
90
127
|
+ "' rate='"
|
|
91
128
|
+ escapeXmlAttr(options.rate)
|
|
92
129
|
+ "' volume='+0%'>"
|
|
93
|
-
+
|
|
130
|
+
+ renderSsmlBody(options.text, options.pronunciations)
|
|
94
131
|
+ '</prosody></voice></speak>';
|
|
95
132
|
}
|
|
96
133
|
/** 生成带时间戳与路径的协议消息头。 */
|
package/lib/host-config.js
CHANGED
|
@@ -475,6 +475,11 @@ export const Config = z.object({
|
|
|
475
475
|
referenceText: z.string().description('参考音频对应的原始台词文本(可选,CosyVoice2 等克隆模型推荐填写)'),
|
|
476
476
|
})).collapse().volatile()
|
|
477
477
|
.description('自定义音色与零样本克隆别名列表(保存后立即生效)'),
|
|
478
|
+
pronunciations: z.array(z.object({
|
|
479
|
+
word: z.string().required().description('原词或缩写(如 Nginx、SQL、重构)'),
|
|
480
|
+
alias: z.string().required().description('替读发音文本(如 Engine X、S Q L、虫构)'),
|
|
481
|
+
})).collapse().volatile()
|
|
482
|
+
.description('自定义发音词典(专有名词、缩写与多音字替读规则,保存后立即生效)'),
|
|
478
483
|
}).i18n({
|
|
479
484
|
zh: {
|
|
480
485
|
$description: '零密钥 Edge / OpenAI 兼容语音合成(TTS)与语音转文字(ASR)设置(保存立即生效)',
|
|
@@ -493,6 +498,7 @@ export const Config = z.object({
|
|
|
493
498
|
timeoutMs: '请求超时毫秒数(5000–600000,默认 60000)',
|
|
494
499
|
overwrite: '允许覆盖同名音频或转写文本(默认关闭,自动追加序号)',
|
|
495
500
|
customVoices: '自定义音色与零样本克隆别名列表(可为常用音色、人设或参考音频命名,保存后立即生效)',
|
|
501
|
+
pronunciations: '自定义发音词典(专有名词、缩写与多音字替读规则,字幕保留原词,保存后立即生效)',
|
|
496
502
|
},
|
|
497
503
|
en: {
|
|
498
504
|
$description: 'Keyless Edge / OpenAI-compatible TTS read-aloud and ASR transcription settings (live on save)',
|
|
@@ -511,6 +517,7 @@ export const Config = z.object({
|
|
|
511
517
|
timeoutMs: 'Per-request timeout in ms (5000–600000, default 60000)',
|
|
512
518
|
overwrite: 'Overwrite existing output files instead of auto-numbering (default off)',
|
|
513
519
|
customVoices: 'Custom voice and zero-shot cloning aliases (name reusable voices, personas, or reference audio clips; live on save)',
|
|
520
|
+
pronunciations: 'Custom pronunciation lexicon (override spoken aliases for acronyms/terms while keeping subtitles intact; live on save)',
|
|
514
521
|
},
|
|
515
522
|
});
|
|
516
523
|
/** 活引用视图:同时兼容 DSH >=0.1.7 的 { get() } 活引用与旧宿主/测试传入的普通对象。 */
|
package/lib/index.d.ts
CHANGED
|
@@ -1,18 +1,17 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* dsh-voice —— 语音双件套工具插件(node 半身,配置走 cordis.patch.yml)。
|
|
3
|
-
*
|
|
4
|
-
* 插件导出 apply(ctx, config):注册五个面向模型的工具(voice_tts / voice_stt /
|
|
5
|
-
* voice_list / voice_preview / voice_health)。TTS 走 edge-tts 协议(原生 WebSocket,
|
|
6
|
-
* 零 API 成本);STT 走 OpenAI 兼容 ASR 接口(Groq/OpenAI/自定义端点)。
|
|
7
|
-
*
|
|
8
|
-
* @module dsh-voice
|
|
9
|
-
*/
|
|
10
1
|
import { type VoiceConfig } from './config.js';
|
|
11
2
|
import { type VoiceToolDefinition } from './tools.js';
|
|
12
3
|
/** cordis 服务注入:apply 里要用 ctx.tools,必须显式声明。 */
|
|
13
4
|
export declare const Config: import("./host-config.js").HostSchemaNode<VoiceConfig>;
|
|
14
5
|
export declare const name = "voice";
|
|
15
6
|
export declare const inject: string[];
|
|
7
|
+
/** 随包分发的技能目录绝对路径。 */
|
|
8
|
+
export declare function bundledSkillsDir(): string;
|
|
9
|
+
/** 解析 SKILL.md 的 YAML frontmatter(零外部依赖)。 */
|
|
10
|
+
export declare function parseSkillFile(text: string): {
|
|
11
|
+
name: string;
|
|
12
|
+
description: string;
|
|
13
|
+
content: string;
|
|
14
|
+
};
|
|
16
15
|
/** 插件所需的最小 ctx 面。 */
|
|
17
16
|
export interface VoicePluginContext {
|
|
18
17
|
logger?: {
|
|
@@ -21,11 +20,12 @@ export interface VoicePluginContext {
|
|
|
21
20
|
tools: {
|
|
22
21
|
register(definition: VoiceToolDefinition): () => void;
|
|
23
22
|
};
|
|
23
|
+
inject?: Function;
|
|
24
24
|
effect?(effect: () => () => void, label?: string): unknown;
|
|
25
25
|
on?(event: string, listener: () => void): () => void;
|
|
26
26
|
}
|
|
27
27
|
/**
|
|
28
|
-
*
|
|
28
|
+
* 插件入口:解析配置、注册五个语音工具、按需注入运行时工作流技能并挂载 Voice Studio Web 路由。
|
|
29
29
|
*/
|
|
30
30
|
export declare function apply(ctx: VoicePluginContext, config?: VoiceConfig | null): void;
|
|
31
31
|
export * from './config.js';
|
|
@@ -38,3 +38,4 @@ export * from './stt.js';
|
|
|
38
38
|
export * from './tools.js';
|
|
39
39
|
export * from './tts-text.js';
|
|
40
40
|
export * from './voices.js';
|
|
41
|
+
export * from './web.js';
|
package/lib/index.js
CHANGED
|
@@ -8,15 +8,92 @@ import { registerUpdates } from '@stardustlc/dsh-updates';
|
|
|
8
8
|
*
|
|
9
9
|
* @module dsh-voice
|
|
10
10
|
*/
|
|
11
|
+
import { readFileSync } from 'node:fs';
|
|
12
|
+
import { join } from 'node:path';
|
|
13
|
+
import { fileURLToPath } from 'node:url';
|
|
11
14
|
import { resolveConfig } from './config.js';
|
|
12
15
|
import { Config as VoiceConfigSchema, liveConfig } from './host-config.js';
|
|
13
16
|
import { buildVoiceTools } from './tools.js';
|
|
17
|
+
import { installVoiceStudioWeb } from './web.js';
|
|
14
18
|
/** cordis 服务注入:apply 里要用 ctx.tools,必须显式声明。 */
|
|
15
19
|
export const Config = VoiceConfigSchema;
|
|
16
20
|
export const name = 'voice';
|
|
17
21
|
export const inject = ['tools'];
|
|
22
|
+
/** 随包分发的技能目录绝对路径。 */
|
|
23
|
+
export function bundledSkillsDir() {
|
|
24
|
+
return fileURLToPath(new URL('../skills/', import.meta.url));
|
|
25
|
+
}
|
|
26
|
+
function unquoteYamlScalar(value) {
|
|
27
|
+
const trimmed = value.trim();
|
|
28
|
+
if ((trimmed.startsWith('"') && trimmed.endsWith('"')) || (trimmed.startsWith("'") && trimmed.endsWith("'"))) {
|
|
29
|
+
return trimmed.slice(1, -1).replace(/\\"/g, '"').replace(/\\n/g, '\n').trim();
|
|
30
|
+
}
|
|
31
|
+
return trimmed;
|
|
32
|
+
}
|
|
33
|
+
/** 解析 SKILL.md 的 YAML frontmatter(零外部依赖)。 */
|
|
34
|
+
export function parseSkillFile(text) {
|
|
35
|
+
const normalized = text.replace(/^\uFEFF/, '').replace(/\r\n?/g, '\n');
|
|
36
|
+
const match = /^---[ \t]*\n([\s\S]*?)\n---[ \t]*\n?/.exec(normalized);
|
|
37
|
+
if (!match)
|
|
38
|
+
return { name: '', description: '', content: normalized };
|
|
39
|
+
let skillName = '';
|
|
40
|
+
let description = '';
|
|
41
|
+
for (const rawLine of match[1].split('\n')) {
|
|
42
|
+
const keyMatch = /^([A-Za-z][A-Za-z0-9_-]*):[ \t]*(.*)$/.exec(rawLine.trim());
|
|
43
|
+
if (!keyMatch)
|
|
44
|
+
continue;
|
|
45
|
+
if (keyMatch[1] === 'name')
|
|
46
|
+
skillName = unquoteYamlScalar(keyMatch[2]);
|
|
47
|
+
else if (keyMatch[1] === 'description')
|
|
48
|
+
description = unquoteYamlScalar(keyMatch[2]);
|
|
49
|
+
}
|
|
50
|
+
return { name: skillName, description, content: normalized.slice(match[0].length).trimStart() };
|
|
51
|
+
}
|
|
52
|
+
function installVoiceWorkflowSkill(ctx, warn, disposers) {
|
|
53
|
+
if (typeof ctx.inject !== 'function')
|
|
54
|
+
return;
|
|
55
|
+
try {
|
|
56
|
+
ctx.inject(['skills'], (skillCtx) => {
|
|
57
|
+
if (typeof skillCtx?.skills?.register !== 'function')
|
|
58
|
+
return;
|
|
59
|
+
try {
|
|
60
|
+
const dir = join(bundledSkillsDir(), 'voice-workflow');
|
|
61
|
+
const parsed = parseSkillFile(readFileSync(join(dir, 'SKILL.md'), 'utf8'));
|
|
62
|
+
if (!parsed.name || !parsed.description || !parsed.content) {
|
|
63
|
+
throw new Error('voice-workflow 的 frontmatter 不完整');
|
|
64
|
+
}
|
|
65
|
+
const off = skillCtx.skills.register({
|
|
66
|
+
name: parsed.name,
|
|
67
|
+
description: parsed.description,
|
|
68
|
+
content: parsed.content,
|
|
69
|
+
resourceBase: { kind: 'directory', path: dir },
|
|
70
|
+
source: 'runtime',
|
|
71
|
+
});
|
|
72
|
+
if (typeof off === 'function') {
|
|
73
|
+
let cleaned = false;
|
|
74
|
+
const cleanup = () => {
|
|
75
|
+
if (cleaned)
|
|
76
|
+
return;
|
|
77
|
+
cleaned = true;
|
|
78
|
+
off();
|
|
79
|
+
};
|
|
80
|
+
disposers.push(cleanup);
|
|
81
|
+
if (typeof skillCtx.effect === 'function') {
|
|
82
|
+
skillCtx.effect(() => cleanup, 'dsh-voice: voice-workflow skill');
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
catch (error) {
|
|
87
|
+
warn('[dsh-voice] 技能加载失败:' + (error instanceof Error ? error.message : String(error)));
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
catch (error) {
|
|
92
|
+
warn('[dsh-voice] 技能注入未启用:' + (error instanceof Error ? error.message : String(error)));
|
|
93
|
+
}
|
|
94
|
+
}
|
|
18
95
|
/**
|
|
19
|
-
*
|
|
96
|
+
* 插件入口:解析配置、注册五个语音工具、按需注入运行时工作流技能并挂载 Voice Studio Web 路由。
|
|
20
97
|
*/
|
|
21
98
|
export function apply(ctx, config) {
|
|
22
99
|
registerUpdates(ctx, { name: "dsh-voice" });
|
|
@@ -42,6 +119,8 @@ export function apply(ctx, config) {
|
|
|
42
119
|
for (const definition of buildVoiceTools(readCfg)) {
|
|
43
120
|
disposers.push(ctx.tools.register(definition));
|
|
44
121
|
}
|
|
122
|
+
installVoiceWorkflowSkill(ctx, warn, disposers);
|
|
123
|
+
installVoiceStudioWeb(ctx, readCfg);
|
|
45
124
|
const teardown = () => {
|
|
46
125
|
for (const dispose of disposers)
|
|
47
126
|
dispose();
|
|
@@ -63,3 +142,4 @@ export * from './stt.js';
|
|
|
63
142
|
export * from './tools.js';
|
|
64
143
|
export * from './tts-text.js';
|
|
65
144
|
export * from './voices.js';
|
|
145
|
+
export * from './web.js';
|
package/lib/openai-tts.d.ts
CHANGED
|
@@ -1,13 +1,26 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* OpenAI 兼容 TTS 客户端:支持 OpenAI / SiliconFlow / 本地 Kokoro 等 /audio/speech 端点。
|
|
3
|
-
*
|
|
4
|
-
* @module dsh-voice/openai-tts
|
|
5
|
-
*/
|
|
6
1
|
/** 零样本克隆参考音频样本。 */
|
|
7
2
|
export interface ReferenceVoiceSample {
|
|
8
3
|
audioDataUri: string;
|
|
9
4
|
text?: string;
|
|
5
|
+
rawAudio?: Buffer;
|
|
6
|
+
filename?: string;
|
|
10
7
|
}
|
|
8
|
+
/** 传给 TTS / 上传接口的 fetch:支持像代理那样附带自己的 FormData 构造器。 */
|
|
9
|
+
export type OpenAiTtsFetch = typeof fetch & {
|
|
10
|
+
FormData?: typeof FormData;
|
|
11
|
+
};
|
|
12
|
+
/** 重置 SiliconFlow 参考音频 URI 内存缓存(测试专用)。 */
|
|
13
|
+
export declare function resetUploadedVoiceCacheForTest(): void;
|
|
14
|
+
/**
|
|
15
|
+
* 上传参考音频到 SiliconFlow `/uploads/audio/voice` 并返回可复用的 `speech:...` URI;
|
|
16
|
+
* 相同 `baseUrl + model + text + audio SHA-256` 命中内存缓存直接返回,避免分段重复上传。
|
|
17
|
+
*/
|
|
18
|
+
export declare function uploadSiliconFlowVoice(baseUrl: string, apiKey: string, sample: {
|
|
19
|
+
audio: Buffer;
|
|
20
|
+
filename: string;
|
|
21
|
+
model: string;
|
|
22
|
+
text: string;
|
|
23
|
+
}, fetchImpl?: OpenAiTtsFetch, timeoutMs?: number, signal?: AbortSignal): Promise<string>;
|
|
11
24
|
/** OpenAI 兼容 TTS 合成选项。 */
|
|
12
25
|
export interface OpenAiTtsOptions {
|
|
13
26
|
text: string;
|
|
@@ -26,4 +39,4 @@ export declare function rateToSpeed(rate: string): number;
|
|
|
26
39
|
* 调用 OpenAI 兼容 `/audio/speech` 接口合成 MP3 音频。
|
|
27
40
|
* 本地回环地址(localhost / 127.0.0.1 / ::1)允许空密钥直连。
|
|
28
41
|
*/
|
|
29
|
-
export declare function synthesizeOpenAiSpeech(baseUrl: string, apiKey: string, options: OpenAiTtsOptions, fetchImpl?:
|
|
42
|
+
export declare function synthesizeOpenAiSpeech(baseUrl: string, apiKey: string, options: OpenAiTtsOptions, fetchImpl?: OpenAiTtsFetch, timeoutMs?: number, signal?: AbortSignal): Promise<Buffer>;
|
package/lib/openai-tts.js
CHANGED
|
@@ -3,6 +3,138 @@
|
|
|
3
3
|
*
|
|
4
4
|
* @module dsh-voice/openai-tts
|
|
5
5
|
*/
|
|
6
|
+
import { createHash } from 'node:crypto';
|
|
7
|
+
import { extname } from 'node:path';
|
|
8
|
+
const uploadedVoiceUriCache = new Map();
|
|
9
|
+
const inFlightVoiceUploads = new Map();
|
|
10
|
+
/** 重置 SiliconFlow 参考音频 URI 内存缓存(测试专用)。 */
|
|
11
|
+
export function resetUploadedVoiceCacheForTest() {
|
|
12
|
+
uploadedVoiceUriCache.clear();
|
|
13
|
+
inFlightVoiceUploads.clear();
|
|
14
|
+
}
|
|
15
|
+
function mimeOfAudio(filename) {
|
|
16
|
+
const ext = extname(filename).toLowerCase();
|
|
17
|
+
const table = {
|
|
18
|
+
'.mp3': 'audio/mpeg',
|
|
19
|
+
'.wav': 'audio/wav',
|
|
20
|
+
'.m4a': 'audio/mp4',
|
|
21
|
+
'.mp4': 'audio/mp4',
|
|
22
|
+
'.ogg': 'audio/ogg',
|
|
23
|
+
'.opus': 'audio/ogg',
|
|
24
|
+
'.flac': 'audio/flac',
|
|
25
|
+
'.webm': 'audio/webm',
|
|
26
|
+
};
|
|
27
|
+
return table[ext] ?? 'application/octet-stream';
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* 上传参考音频到 SiliconFlow `/uploads/audio/voice` 并返回可复用的 `speech:...` URI;
|
|
31
|
+
* 相同 `baseUrl + model + text + audio SHA-256` 命中内存缓存直接返回,避免分段重复上传。
|
|
32
|
+
*/
|
|
33
|
+
export async function uploadSiliconFlowVoice(baseUrl, apiKey, sample, fetchImpl = globalThis.fetch, timeoutMs = 30000, signal) {
|
|
34
|
+
throwIfAborted(signal);
|
|
35
|
+
const trimmedBaseUrl = baseUrl.trim().replace(/\/+$/, '');
|
|
36
|
+
if (trimmedBaseUrl === '') {
|
|
37
|
+
throw new Error('未配置 TTS 接口地址,无法上传参考音频。');
|
|
38
|
+
}
|
|
39
|
+
const key = apiKey.trim();
|
|
40
|
+
if (key === '' && !isLoopbackUrl(trimmedBaseUrl)) {
|
|
41
|
+
throw new Error('未配置 TTS 密钥,无法上传参考音频。');
|
|
42
|
+
}
|
|
43
|
+
if (!Buffer.isBuffer(sample.audio) || sample.audio.length === 0) {
|
|
44
|
+
throw new Error('参考音频为空,无法上传。');
|
|
45
|
+
}
|
|
46
|
+
if (typeof sample.text !== 'string' || sample.text.trim() === '') {
|
|
47
|
+
throw new Error('参考音频文本为空,无法上传 SiliconFlow 音色。');
|
|
48
|
+
}
|
|
49
|
+
const audioHash = createHash('sha256').update(sample.audio).digest('hex');
|
|
50
|
+
const cacheKey = createHash('sha256')
|
|
51
|
+
.update(trimmedBaseUrl + '|' + sample.model + '|' + sample.text + '|' + audioHash)
|
|
52
|
+
.digest('hex');
|
|
53
|
+
const cached = uploadedVoiceUriCache.get(cacheKey);
|
|
54
|
+
if (cached !== undefined) {
|
|
55
|
+
return cached;
|
|
56
|
+
}
|
|
57
|
+
const existing = inFlightVoiceUploads.get(cacheKey);
|
|
58
|
+
if (existing !== undefined) {
|
|
59
|
+
return await raceAbort(existing, signal);
|
|
60
|
+
}
|
|
61
|
+
const uploadPromise = (async () => {
|
|
62
|
+
const FormDataImpl = fetchImpl.FormData ?? FormData;
|
|
63
|
+
const form = new FormDataImpl();
|
|
64
|
+
const filename = sample.filename.trim() !== '' ? sample.filename.trim() : 'reference.mp3';
|
|
65
|
+
const mime = mimeOfAudio(filename);
|
|
66
|
+
form.append('model', sample.model);
|
|
67
|
+
form.append('customName', 'dsh_' + cacheKey.slice(0, 10));
|
|
68
|
+
form.append('text', sample.text);
|
|
69
|
+
form.append('file', new Blob([sample.audio], { type: mime }), filename);
|
|
70
|
+
const headers = key !== '' ? { authorization: 'Bearer ' + key } : {};
|
|
71
|
+
const timeoutSignal = AbortSignal.timeout(timeoutMs);
|
|
72
|
+
const requestSignal = signal === undefined ? timeoutSignal : AbortSignal.any([timeoutSignal, signal]);
|
|
73
|
+
let response;
|
|
74
|
+
try {
|
|
75
|
+
response = await raceAbort(fetchImpl(trimmedBaseUrl + '/uploads/audio/voice', {
|
|
76
|
+
method: 'POST',
|
|
77
|
+
redirect: 'manual',
|
|
78
|
+
headers,
|
|
79
|
+
body: form,
|
|
80
|
+
signal: requestSignal,
|
|
81
|
+
}), requestSignal);
|
|
82
|
+
}
|
|
83
|
+
catch (error) {
|
|
84
|
+
throwIfAborted(signal);
|
|
85
|
+
if (timeoutSignal.aborted)
|
|
86
|
+
throw new Error('SiliconFlow 参考音频上传超时。');
|
|
87
|
+
throw new Error('SiliconFlow 参考音频上传失败:' + (error instanceof Error ? error.message : String(error)));
|
|
88
|
+
}
|
|
89
|
+
if (response.status >= 300 && response.status < 400) {
|
|
90
|
+
try {
|
|
91
|
+
await raceAbort(response.body?.cancel(), requestSignal);
|
|
92
|
+
}
|
|
93
|
+
catch { /* 忽略 */ }
|
|
94
|
+
throwIfAborted(signal);
|
|
95
|
+
throw new Error('SiliconFlow 音色上传返回重定向(HTTP ' + response.status + '),已阻止跳转。');
|
|
96
|
+
}
|
|
97
|
+
if (!response.ok) {
|
|
98
|
+
let body = '';
|
|
99
|
+
try {
|
|
100
|
+
body = (await raceAbort(response.text(), requestSignal)).slice(0, 300);
|
|
101
|
+
}
|
|
102
|
+
catch (error) {
|
|
103
|
+
throwIfAborted(signal);
|
|
104
|
+
if (timeoutSignal.aborted)
|
|
105
|
+
throw new Error('SiliconFlow 音色上传响应读取超时。');
|
|
106
|
+
throw error;
|
|
107
|
+
}
|
|
108
|
+
throw new Error('SiliconFlow 音色上传失败:HTTP ' + response.status + '。' + body);
|
|
109
|
+
}
|
|
110
|
+
let json;
|
|
111
|
+
try {
|
|
112
|
+
json = await raceAbort(response.json(), requestSignal);
|
|
113
|
+
}
|
|
114
|
+
catch (error) {
|
|
115
|
+
throwIfAborted(signal);
|
|
116
|
+
if (timeoutSignal.aborted)
|
|
117
|
+
throw new Error('SiliconFlow 音色上传响应读取超时。');
|
|
118
|
+
throw new Error('SiliconFlow 音色上传响应不是合法 JSON:' + (error instanceof Error ? error.message : String(error)));
|
|
119
|
+
}
|
|
120
|
+
const uri = typeof json === 'object' && json !== null && typeof json.uri === 'string'
|
|
121
|
+
? json.uri.trim()
|
|
122
|
+
: '';
|
|
123
|
+
if (uri === '') {
|
|
124
|
+
throw new Error('SiliconFlow 音色上传响应缺少 uri 字段。');
|
|
125
|
+
}
|
|
126
|
+
uploadedVoiceUriCache.set(cacheKey, uri);
|
|
127
|
+
return uri;
|
|
128
|
+
})();
|
|
129
|
+
uploadPromise.catch(() => { });
|
|
130
|
+
inFlightVoiceUploads.set(cacheKey, uploadPromise);
|
|
131
|
+
try {
|
|
132
|
+
return await raceAbort(uploadPromise, signal);
|
|
133
|
+
}
|
|
134
|
+
finally {
|
|
135
|
+
inFlightVoiceUploads.delete(cacheKey);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
6
138
|
function isLoopbackUrl(rawUrl) {
|
|
7
139
|
try {
|
|
8
140
|
const host = new URL(rawUrl).hostname.toLowerCase();
|
|
@@ -76,14 +208,43 @@ export async function synthesizeOpenAiSpeech(baseUrl, apiKey, options, fetchImpl
|
|
|
76
208
|
if (instr !== '' && isCosyVoice && !options.text.includes('<|endofprompt|>')) {
|
|
77
209
|
input = instr + '<|endofprompt|>' + options.text;
|
|
78
210
|
}
|
|
211
|
+
let effectiveVoice = options.voice;
|
|
212
|
+
let sendInlineReferences = hasReferences;
|
|
213
|
+
if (options.voice === ''
|
|
214
|
+
&& hasReferences
|
|
215
|
+
&& options.references.length === 1
|
|
216
|
+
&& Buffer.isBuffer(options.references[0].rawAudio)
|
|
217
|
+
&& options.references[0].rawAudio.length > 0
|
|
218
|
+
&& typeof options.references[0].text === 'string'
|
|
219
|
+
&& options.references[0].text.trim() !== ''
|
|
220
|
+
&& trimmedBaseUrl.toLowerCase().includes('siliconflow.')
|
|
221
|
+
&& key !== '') {
|
|
222
|
+
const ref = options.references[0];
|
|
223
|
+
try {
|
|
224
|
+
const uri = await uploadSiliconFlowVoice(trimmedBaseUrl, key, {
|
|
225
|
+
audio: ref.rawAudio,
|
|
226
|
+
filename: ref.filename && ref.filename.trim() !== '' ? ref.filename.trim() : 'reference.mp3',
|
|
227
|
+
model: effectiveModel,
|
|
228
|
+
text: ref.text.trim(),
|
|
229
|
+
}, fetchImpl, timeoutMs, abortSignal);
|
|
230
|
+
if (uri.trim() !== '') {
|
|
231
|
+
effectiveVoice = uri.trim();
|
|
232
|
+
sendInlineReferences = false;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
catch {
|
|
236
|
+
throwIfAborted(abortSignal);
|
|
237
|
+
// 若上传端点不可用(如仅 mock /audio/speech 的单测),静默回退为内联 references
|
|
238
|
+
}
|
|
239
|
+
}
|
|
79
240
|
const payload = {
|
|
80
241
|
model: effectiveModel,
|
|
81
242
|
input,
|
|
82
|
-
voice:
|
|
243
|
+
voice: effectiveVoice,
|
|
83
244
|
response_format: 'mp3',
|
|
84
245
|
...(options.speed !== undefined && options.speed !== 1 ? { speed: options.speed } : {}),
|
|
85
246
|
...(instr !== '' && !isCosyVoice ? { instructions: instr } : {}),
|
|
86
|
-
...(
|
|
247
|
+
...(sendInlineReferences
|
|
87
248
|
? {
|
|
88
249
|
references: options.references.map((ref) => ({
|
|
89
250
|
audio: ref.audioDataUri,
|
package/lib/stt.d.ts
CHANGED
|
@@ -4,6 +4,7 @@ export interface SttSegment {
|
|
|
4
4
|
start: number;
|
|
5
5
|
end: number;
|
|
6
6
|
text: string;
|
|
7
|
+
speaker?: string;
|
|
7
8
|
}
|
|
8
9
|
/** 转写选项。 */
|
|
9
10
|
export interface SttOptions {
|
|
@@ -13,6 +14,7 @@ export interface SttOptions {
|
|
|
13
14
|
language?: string;
|
|
14
15
|
prompt?: string;
|
|
15
16
|
timestamps?: boolean;
|
|
17
|
+
diarize?: boolean;
|
|
16
18
|
allowEmptyApiKey?: boolean;
|
|
17
19
|
/** 取消信号(可选);也可通过 transcribe 第 6 个参数传入。 */
|
|
18
20
|
signal?: AbortSignal;
|
|
@@ -24,6 +26,7 @@ export interface SttResult {
|
|
|
24
26
|
language?: string;
|
|
25
27
|
duration?: number;
|
|
26
28
|
segments?: SttSegment[];
|
|
29
|
+
speakers?: string[];
|
|
27
30
|
}
|
|
28
31
|
/** 预处理后的单个音频切片。 */
|
|
29
32
|
export interface PreparedAudioChunk {
|
|
@@ -49,6 +52,8 @@ export type SttFetch = typeof fetch & {
|
|
|
49
52
|
export declare function transcribe(baseUrl: string, apiKey: string, options: SttOptions, fetchImpl?: SttFetch, timeoutMs?: number, signal?: AbortSignal): Promise<SttResult>;
|
|
50
53
|
/** 将 ASR 返回的 segments 格式化为 SRT 或 WebVTT 字幕文本。 */
|
|
51
54
|
export declare function formatSttSubtitles(segments: SttSegment[], format: 'srt' | 'vtt'): string;
|
|
55
|
+
/** 将带说话人标签的分段合并为按发言人分段的可读逐字稿(如 `[说话人 A 00:01] ...`)。 */
|
|
56
|
+
export declare function formatDiarizedTranscript(segments: SttSegment[]): string;
|
|
52
57
|
/** 检测环境 PATH 中是否可用 ffmpeg。 */
|
|
53
58
|
export declare function hasFfmpeg(env?: NodeJS.ProcessEnv): boolean;
|
|
54
59
|
/**
|
|
@@ -56,3 +61,17 @@ export declare function hasFfmpeg(env?: NodeJS.ProcessEnv): boolean;
|
|
|
56
61
|
* 若转码后仍超过 24MB,则按每段 20 分钟(1200 秒)无损切分。
|
|
57
62
|
*/
|
|
58
63
|
export declare function prepareAudioForAsr(audioPath: string, signal?: AbortSignal): Promise<OversizeAudioPreparation>;
|
|
64
|
+
/** 双声道分离结果(含临时目录清理回调)。 */
|
|
65
|
+
export interface StereoSplitResult {
|
|
66
|
+
channels: Array<{
|
|
67
|
+
audio: Blob | Buffer;
|
|
68
|
+
filename: string;
|
|
69
|
+
speaker: string;
|
|
70
|
+
}>;
|
|
71
|
+
cleanup: () => void;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* 探测音频是否包含 >= 2 声道;若为立体声/多声道,拆分为左右声道(16kHz 32kbps 单声道 MP3)用于精准分轨说话人分离。
|
|
75
|
+
* 若非立体声或 ffmpeg 不可用/失败,返回 null。
|
|
76
|
+
*/
|
|
77
|
+
export declare function splitStereoChannelsForDiarization(audioPath: string, signal?: AbortSignal): Promise<StereoSplitResult | null>;
|