dsh-voice 0.6.0 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +14 -0
- package/README.en.md +22 -15
- package/README.md +22 -15
- package/docs/USAGE.en.md +46 -37
- package/docs/USAGE.md +46 -37
- package/lib/config.d.ts +29 -3
- package/lib/config.js +68 -9
- package/lib/edge-tts.d.ts +29 -0
- package/lib/edge-tts.js +172 -37
- package/lib/host-config.d.ts +1 -1
- package/lib/host-config.js +36 -20
- package/lib/index.d.ts +2 -0
- package/lib/index.js +2 -0
- package/lib/openai-tts.d.ts +22 -0
- package/lib/openai-tts.js +129 -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 +6 -2
- package/lib/tools.js +541 -71
- package/lib/tts-text.d.ts +48 -0
- package/lib/tts-text.js +308 -0
- package/lib/update-client.js +145 -53
- package/lib/voices.d.ts +9 -2
- package/lib/voices.js +53 -25
- package/locale/en.json +1 -1
- package/locale/zh.json +1 -1
- package/package.json +5 -4
- package/skills/voice-workflow/SKILL.md +78 -0
package/lib/stt.js
CHANGED
|
@@ -1,14 +1,21 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* ASR 客户端:OpenAI 兼容 /audio/transcriptions multipart
|
|
2
|
+
* ASR 客户端:OpenAI 兼容 /audio/transcriptions multipart 上传、字幕导出与超限音频自动压缩分段。
|
|
3
3
|
*
|
|
4
4
|
* @module dsh-voice/stt
|
|
5
5
|
*/
|
|
6
|
-
import {
|
|
6
|
+
import { execFile, spawnSync } from 'node:child_process';
|
|
7
|
+
import { mkdtempSync, openAsBlob, readdirSync, rmSync, statSync } from 'node:fs';
|
|
8
|
+
import { tmpdir } from 'node:os';
|
|
9
|
+
import { extname, join } from 'node:path';
|
|
10
|
+
import { isLoopbackBaseUrl } from './config.js';
|
|
7
11
|
const MAX_AUDIO_BYTES = 25 * 1024 * 1024;
|
|
12
|
+
const COMPRESSED_CHUNK_LIMIT_BYTES = 24 * 1024 * 1024;
|
|
13
|
+
const SEGMENT_DURATION_SECONDS = 1200;
|
|
8
14
|
/** 字节数超过 25MB 时抛错:调用方可在读文件前先按 statSync 大小快速拒绝,避免白读整文件。 */
|
|
9
15
|
export function assertAudioSize(bytes) {
|
|
10
|
-
if (bytes > MAX_AUDIO_BYTES)
|
|
11
|
-
throw new Error('音频超过 25MB 上限(ASR
|
|
16
|
+
if (bytes > MAX_AUDIO_BYTES) {
|
|
17
|
+
throw new Error('音频超过 25MB 上限(ASR 接口限制)。可传入 autoCompress=true 自动压缩分段,或先用 dsh-ffmpeg / ffmpeg 压缩。');
|
|
18
|
+
}
|
|
12
19
|
}
|
|
13
20
|
/** 已取消则抛出取消原因;作为 await 前后的统一取消检查。 */
|
|
14
21
|
function throwIfAborted(signal) {
|
|
@@ -28,28 +35,42 @@ function raceAbort(value, signal) {
|
|
|
28
35
|
promise.then((settled) => { signal.removeEventListener('abort', onAbort); resolvePromise(settled); }, (error) => { signal.removeEventListener('abort', onAbort); rejectPromise(error); });
|
|
29
36
|
});
|
|
30
37
|
}
|
|
38
|
+
/**
|
|
39
|
+
* 调用 OpenAI 兼容 ASR 接口转写音频。
|
|
40
|
+
* @throws 缺密钥 / 文件过大 / HTTP 错误 / 无文本时抛中文错误。
|
|
41
|
+
*/
|
|
31
42
|
export async function transcribe(baseUrl, apiKey, options, fetchImpl = globalThis.fetch, timeoutMs = 120000, signal) {
|
|
32
43
|
const abortSignal = signal ?? options.signal;
|
|
33
44
|
throwIfAborted(abortSignal);
|
|
34
|
-
|
|
45
|
+
const allowEmptyKey = options.allowEmptyApiKey === true || isLoopbackBaseUrl(baseUrl);
|
|
46
|
+
if (apiKey === '' && !allowEmptyKey) {
|
|
35
47
|
throw new Error('未配置 ASR 密钥:可在 DSH「设置 → 插件 → 语音(dsh-voice)」直接填写 asrApiKey(保存即生效,无需重启),或设置环境变量 DSH_VOICE_ASR_KEY,或在 cordis.patch.yml 配置 asrApiKey。(注:文字转语音无需密钥即可直接使用。)');
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
48
|
+
}
|
|
49
|
+
if (baseUrl === '') {
|
|
50
|
+
throw new Error('未配置 ASR 接口地址:asrEngine=custom 时必须在 DSH「设置 → 插件 → 语音」或 cordis.patch.yml 提供 asrBaseUrl(或切回 groq / openai / siliconflow 预设)。');
|
|
51
|
+
}
|
|
52
|
+
const byteLength = Buffer.isBuffer(options.audio) ? options.audio.length : options.audio.size;
|
|
53
|
+
if (byteLength === 0)
|
|
39
54
|
throw new Error('音频文件为空。');
|
|
40
|
-
assertAudioSize(
|
|
55
|
+
assertAudioSize(byteLength);
|
|
41
56
|
// 代理 fetch(undici)只认它自己那份 FormData:喂 Node 全局 FormData 会被当成普通
|
|
42
57
|
// 对象序列化成 "[object FormData]"(content-type: text/plain),所以优先用调用方 fetch
|
|
43
|
-
//
|
|
58
|
+
// 附带的构造器。传入 Blob 时直接零拷贝追加。
|
|
44
59
|
const FormDataImpl = fetchImpl.FormData ?? FormData;
|
|
45
60
|
const form = new FormDataImpl();
|
|
46
61
|
const mime = mimeOf(options.filename);
|
|
47
|
-
|
|
62
|
+
const filePart = Buffer.isBuffer(options.audio)
|
|
63
|
+
? new Blob([options.audio], { type: mime })
|
|
64
|
+
: options.audio;
|
|
65
|
+
form.append('file', filePart, options.filename);
|
|
48
66
|
form.append('model', options.model);
|
|
49
67
|
if (options.language !== undefined && options.language !== '')
|
|
50
68
|
form.append('language', options.language);
|
|
51
69
|
if (options.prompt !== undefined && options.prompt !== '')
|
|
52
70
|
form.append('prompt', options.prompt);
|
|
71
|
+
if (options.timestamps === true)
|
|
72
|
+
form.append('response_format', 'verbose_json');
|
|
73
|
+
const headers = apiKey !== '' ? { authorization: 'Bearer ' + apiKey } : {};
|
|
53
74
|
const timeoutSignal = AbortSignal.timeout(timeoutMs);
|
|
54
75
|
const requestSignal = abortSignal === undefined ? timeoutSignal : AbortSignal.any([timeoutSignal, abortSignal]);
|
|
55
76
|
let response;
|
|
@@ -58,7 +79,7 @@ export async function transcribe(baseUrl, apiKey, options, fetchImpl = globalThi
|
|
|
58
79
|
method: 'POST',
|
|
59
80
|
// 音频只交给配置的端点;307/308 也不能把 multipart 转发到另一站。
|
|
60
81
|
redirect: 'manual',
|
|
61
|
-
headers
|
|
82
|
+
headers,
|
|
62
83
|
body: form,
|
|
63
84
|
signal: requestSignal,
|
|
64
85
|
}), requestSignal);
|
|
@@ -103,10 +124,139 @@ export async function transcribe(baseUrl, apiKey, options, fetchImpl = globalThi
|
|
|
103
124
|
if (typeof json !== 'object' || json === null || typeof json.text !== 'string') {
|
|
104
125
|
throw new Error('ASR 响应缺少文本字段,请检查接口是否兼容 /audio/transcriptions。');
|
|
105
126
|
}
|
|
106
|
-
const
|
|
127
|
+
const rec = json;
|
|
128
|
+
const text = rec.text;
|
|
107
129
|
if (text.trim() === '')
|
|
108
130
|
throw new Error('未识别到语音,请检查音频是否包含清晰的人声。');
|
|
109
|
-
|
|
131
|
+
const language = typeof rec.language === 'string' && rec.language !== '' ? rec.language : undefined;
|
|
132
|
+
const duration = typeof rec.duration === 'number' && Number.isFinite(rec.duration) ? rec.duration : undefined;
|
|
133
|
+
let segments;
|
|
134
|
+
if (Array.isArray(rec.segments)) {
|
|
135
|
+
segments = rec.segments
|
|
136
|
+
.filter((item) => typeof item === 'object' && item !== null)
|
|
137
|
+
.map((item, idx) => ({
|
|
138
|
+
id: typeof item.id === 'number' && Number.isFinite(item.id) ? item.id : idx,
|
|
139
|
+
start: typeof item.start === 'number' && Number.isFinite(item.start) ? item.start : 0,
|
|
140
|
+
end: typeof item.end === 'number' && Number.isFinite(item.end) ? item.end : 0,
|
|
141
|
+
text: typeof item.text === 'string' ? item.text.trim() : '',
|
|
142
|
+
}));
|
|
143
|
+
}
|
|
144
|
+
return {
|
|
145
|
+
text,
|
|
146
|
+
model: options.model,
|
|
147
|
+
...(language !== undefined ? { language } : {}),
|
|
148
|
+
...(duration !== undefined ? { duration } : {}),
|
|
149
|
+
...(segments !== undefined ? { segments } : {}),
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
function formatSubtitleTimestamp(seconds, decimalSep) {
|
|
153
|
+
const safeSeconds = Number.isFinite(seconds) ? Math.max(0, seconds) : 0;
|
|
154
|
+
const totalMs = Math.round(safeSeconds * 1000);
|
|
155
|
+
const ms = totalMs % 1000;
|
|
156
|
+
const totalSec = Math.floor(totalMs / 1000);
|
|
157
|
+
const sec = totalSec % 60;
|
|
158
|
+
const totalMin = Math.floor(totalSec / 60);
|
|
159
|
+
const min = totalMin % 60;
|
|
160
|
+
const hour = Math.floor(totalMin / 60);
|
|
161
|
+
return `${String(hour).padStart(2, '0')}:${String(min).padStart(2, '0')}:${String(sec).padStart(2, '0')}${decimalSep}${String(ms).padStart(3, '0')}`;
|
|
162
|
+
}
|
|
163
|
+
/** 将 ASR 返回的 segments 格式化为 SRT 或 WebVTT 字幕文本。 */
|
|
164
|
+
export function formatSttSubtitles(segments, format) {
|
|
165
|
+
const valid = segments.filter((seg) => seg.text.trim() !== '');
|
|
166
|
+
if (format === 'vtt') {
|
|
167
|
+
if (valid.length === 0)
|
|
168
|
+
return 'WEBVTT\n';
|
|
169
|
+
const body = valid
|
|
170
|
+
.map((seg) => `${formatSubtitleTimestamp(seg.start, '.')} --> ${formatSubtitleTimestamp(seg.end, '.')}\n${seg.text.trim()}`)
|
|
171
|
+
.join('\n\n');
|
|
172
|
+
return `WEBVTT\n\n${body}\n`;
|
|
173
|
+
}
|
|
174
|
+
if (valid.length === 0)
|
|
175
|
+
return '';
|
|
176
|
+
return (valid
|
|
177
|
+
.map((seg, idx) => `${idx + 1}\n${formatSubtitleTimestamp(seg.start, ',')} --> ${formatSubtitleTimestamp(seg.end, ',')}\n${seg.text.trim()}`)
|
|
178
|
+
.join('\n\n') + '\n');
|
|
179
|
+
}
|
|
180
|
+
/** 检测环境 PATH 中是否可用 ffmpeg。 */
|
|
181
|
+
export function hasFfmpeg(env = process.env) {
|
|
182
|
+
try {
|
|
183
|
+
return spawnSync('ffmpeg', ['-version'], { env, stdio: 'ignore', timeout: 3000 }).status === 0;
|
|
184
|
+
}
|
|
185
|
+
catch {
|
|
186
|
+
return false;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
function runFfmpeg(args, signal) {
|
|
190
|
+
throwIfAborted(signal);
|
|
191
|
+
return new Promise((resolve, reject) => {
|
|
192
|
+
execFile('ffmpeg', args, { signal, maxBuffer: 4 * 1024 * 1024 }, (error, _stdout, stderr) => {
|
|
193
|
+
if (signal?.aborted) {
|
|
194
|
+
reject(signal.reason);
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
if (error) {
|
|
198
|
+
const detail = typeof stderr === 'string' && stderr.trim() !== ''
|
|
199
|
+
? stderr.trim().slice(-300)
|
|
200
|
+
: error.message;
|
|
201
|
+
reject(new Error('ffmpeg 音频压缩/分段失败:' + detail));
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
resolve();
|
|
205
|
+
});
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* 使用 ffmpeg 将大于 25MB 的音频转码为 16kHz 单声道 32kbps MP3;
|
|
210
|
+
* 若转码后仍超过 24MB,则按每段 20 分钟(1200 秒)无损切分。
|
|
211
|
+
*/
|
|
212
|
+
export async function prepareAudioForAsr(audioPath, signal) {
|
|
213
|
+
throwIfAborted(signal);
|
|
214
|
+
const tempDir = mkdtempSync(join(tmpdir(), 'dsh-voice-asr-'));
|
|
215
|
+
const cleanup = () => {
|
|
216
|
+
try {
|
|
217
|
+
rmSync(tempDir, { recursive: true, force: true });
|
|
218
|
+
}
|
|
219
|
+
catch {
|
|
220
|
+
// 尽力清理临时目录
|
|
221
|
+
}
|
|
222
|
+
};
|
|
223
|
+
try {
|
|
224
|
+
const compressedPath = join(tempDir, 'compressed.mp3');
|
|
225
|
+
await runFfmpeg(['-y', '-i', audioPath, '-vn', '-ac', '1', '-ar', '16000', '-b:a', '32k', compressedPath], signal);
|
|
226
|
+
throwIfAborted(signal);
|
|
227
|
+
const compressedSize = statSync(compressedPath).size;
|
|
228
|
+
if (compressedSize <= COMPRESSED_CHUNK_LIMIT_BYTES) {
|
|
229
|
+
const blob = await openAsBlob(compressedPath, { type: 'audio/mpeg' });
|
|
230
|
+
return {
|
|
231
|
+
chunks: [{ audio: blob, filename: 'compressed.mp3', offsetSeconds: 0 }],
|
|
232
|
+
cleanup,
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
const segmentPattern = join(tempDir, 'part_%03d.mp3');
|
|
236
|
+
await runFfmpeg(['-y', '-i', compressedPath, '-f', 'segment', '-segment_time', String(SEGMENT_DURATION_SECONDS), '-c', 'copy', segmentPattern], signal);
|
|
237
|
+
throwIfAborted(signal);
|
|
238
|
+
const partNames = readdirSync(tempDir)
|
|
239
|
+
.filter((name) => /^part_\d+\.mp3$/.test(name))
|
|
240
|
+
.sort();
|
|
241
|
+
if (partNames.length === 0) {
|
|
242
|
+
throw new Error('ffmpeg 分段后未生成任何音频切片。');
|
|
243
|
+
}
|
|
244
|
+
const chunks = [];
|
|
245
|
+
for (let idx = 0; idx < partNames.length; idx++) {
|
|
246
|
+
const name = partNames[idx];
|
|
247
|
+
const blob = await openAsBlob(join(tempDir, name), { type: 'audio/mpeg' });
|
|
248
|
+
chunks.push({
|
|
249
|
+
audio: blob,
|
|
250
|
+
filename: name,
|
|
251
|
+
offsetSeconds: idx * SEGMENT_DURATION_SECONDS,
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
return { chunks, cleanup };
|
|
255
|
+
}
|
|
256
|
+
catch (error) {
|
|
257
|
+
cleanup();
|
|
258
|
+
throw error;
|
|
259
|
+
}
|
|
110
260
|
}
|
|
111
261
|
/** 常见音频扩展名 → MIME。 */
|
|
112
262
|
function mimeOf(filename) {
|
package/lib/tools.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { type ResolvedVoiceConfig } from './config.js';
|
|
2
|
-
import { synthesizeSpeech } from './edge-tts.js';
|
|
3
|
-
import {
|
|
2
|
+
import { synthesizeSpeech, synthesizeSpeechWithMetadata } from './edge-tts.js';
|
|
3
|
+
import { synthesizeOpenAiSpeech } from './openai-tts.js';
|
|
4
|
+
import { prepareAudioForAsr, transcribe } from './stt.js';
|
|
4
5
|
/** 模型可见的内容块。 */
|
|
5
6
|
export interface ContentBlock {
|
|
6
7
|
type: 'text';
|
|
@@ -28,7 +29,10 @@ export declare const DEFAULT_PREVIEW_TEXT = "\u4F60\u597D\uFF0C\u8FD9\u662F\u97F
|
|
|
28
29
|
/** 可注入依赖(测试用假实现)。 */
|
|
29
30
|
export interface VoiceToolDeps {
|
|
30
31
|
tts?: typeof synthesizeSpeech;
|
|
32
|
+
ttsWithMeta?: typeof synthesizeSpeechWithMetadata;
|
|
33
|
+
openaiTts?: typeof synthesizeOpenAiSpeech;
|
|
31
34
|
stt?: typeof transcribe;
|
|
35
|
+
prepareAudio?: typeof prepareAudioForAsr;
|
|
32
36
|
}
|
|
33
37
|
/**
|
|
34
38
|
* 构建五个语音工具定义(支持静态配置对象或活引用配置 getter)。
|