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/lib/stt.js CHANGED
@@ -68,7 +68,7 @@ export async function transcribe(baseUrl, apiKey, options, fetchImpl = globalThi
68
68
  form.append('language', options.language);
69
69
  if (options.prompt !== undefined && options.prompt !== '')
70
70
  form.append('prompt', options.prompt);
71
- if (options.timestamps === true)
71
+ if (options.timestamps === true || options.diarize === true)
72
72
  form.append('response_format', 'verbose_json');
73
73
  const headers = apiKey !== '' ? { authorization: 'Bearer ' + apiKey } : {};
74
74
  const timeoutSignal = AbortSignal.timeout(timeoutMs);
@@ -134,19 +134,47 @@ export async function transcribe(baseUrl, apiKey, options, fetchImpl = globalThi
134
134
  if (Array.isArray(rec.segments)) {
135
135
  segments = rec.segments
136
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
- }));
137
+ .map((item, idx) => {
138
+ let speaker;
139
+ if (typeof item.speaker === 'string' && item.speaker.trim() !== '') {
140
+ speaker = item.speaker.trim();
141
+ }
142
+ else if (typeof item.speaker === 'number' && Number.isFinite(item.speaker)) {
143
+ speaker = 'Speaker ' + String(item.speaker);
144
+ }
145
+ return {
146
+ id: typeof item.id === 'number' && Number.isFinite(item.id) ? item.id : idx,
147
+ start: typeof item.start === 'number' && Number.isFinite(item.start) ? item.start : 0,
148
+ end: typeof item.end === 'number' && Number.isFinite(item.end) ? item.end : 0,
149
+ text: typeof item.text === 'string' ? item.text.trim() : '',
150
+ ...(speaker !== undefined ? { speaker } : {}),
151
+ };
152
+ });
153
+ if (options.diarize === true && segments.length > 0 && segments.every((seg) => seg.speaker === undefined)) {
154
+ let speakerTurn = 0;
155
+ segments[0].speaker = '说话人 A';
156
+ for (let i = 1; i < segments.length; i++) {
157
+ if (segments[i].start - segments[i - 1].end >= 1.2) {
158
+ speakerTurn = 1 - speakerTurn;
159
+ }
160
+ segments[i].speaker = speakerTurn === 0 ? '说话人 A' : '说话人 B';
161
+ }
162
+ }
143
163
  }
164
+ const speakerSet = new Set();
165
+ for (const seg of segments ?? []) {
166
+ if (typeof seg.speaker === 'string' && seg.speaker.trim() !== '') {
167
+ speakerSet.add(seg.speaker.trim());
168
+ }
169
+ }
170
+ const speakers = speakerSet.size > 0 ? Array.from(speakerSet) : undefined;
144
171
  return {
145
172
  text,
146
173
  model: options.model,
147
174
  ...(language !== undefined ? { language } : {}),
148
175
  ...(duration !== undefined ? { duration } : {}),
149
176
  ...(segments !== undefined ? { segments } : {}),
177
+ ...(speakers !== undefined ? { speakers } : {}),
150
178
  };
151
179
  }
152
180
  function formatSubtitleTimestamp(seconds, decimalSep) {
@@ -160,6 +188,11 @@ function formatSubtitleTimestamp(seconds, decimalSep) {
160
188
  const hour = Math.floor(totalMin / 60);
161
189
  return `${String(hour).padStart(2, '0')}:${String(min).padStart(2, '0')}:${String(sec).padStart(2, '0')}${decimalSep}${String(ms).padStart(3, '0')}`;
162
190
  }
191
+ function formatSegmentText(seg) {
192
+ const clean = seg.text.trim();
193
+ const speaker = typeof seg.speaker === 'string' ? seg.speaker.trim() : '';
194
+ return speaker !== '' ? `[${speaker}] ${clean}` : clean;
195
+ }
163
196
  /** 将 ASR 返回的 segments 格式化为 SRT 或 WebVTT 字幕文本。 */
164
197
  export function formatSttSubtitles(segments, format) {
165
198
  const valid = segments.filter((seg) => seg.text.trim() !== '');
@@ -167,16 +200,51 @@ export function formatSttSubtitles(segments, format) {
167
200
  if (valid.length === 0)
168
201
  return 'WEBVTT\n';
169
202
  const body = valid
170
- .map((seg) => `${formatSubtitleTimestamp(seg.start, '.')} --> ${formatSubtitleTimestamp(seg.end, '.')}\n${seg.text.trim()}`)
203
+ .map((seg) => `${formatSubtitleTimestamp(seg.start, '.')} --> ${formatSubtitleTimestamp(seg.end, '.')}\n${formatSegmentText(seg)}`)
171
204
  .join('\n\n');
172
205
  return `WEBVTT\n\n${body}\n`;
173
206
  }
174
207
  if (valid.length === 0)
175
208
  return '';
176
209
  return (valid
177
- .map((seg, idx) => `${idx + 1}\n${formatSubtitleTimestamp(seg.start, ',')} --> ${formatSubtitleTimestamp(seg.end, ',')}\n${seg.text.trim()}`)
210
+ .map((seg, idx) => `${idx + 1}\n${formatSubtitleTimestamp(seg.start, ',')} --> ${formatSubtitleTimestamp(seg.end, ',')}\n${formatSegmentText(seg)}`)
178
211
  .join('\n\n') + '\n');
179
212
  }
213
+ function formatTranscriptTimestamp(seconds) {
214
+ const safe = Number.isFinite(seconds) ? Math.max(0, Math.floor(seconds)) : 0;
215
+ const hours = Math.floor(safe / 3600);
216
+ const minutes = Math.floor((safe % 3600) / 60);
217
+ const secs = safe % 60;
218
+ if (hours > 0) {
219
+ return `${String(hours).padStart(2, '0')}:${String(minutes).padStart(2, '0')}:${String(secs).padStart(2, '0')}`;
220
+ }
221
+ return `${String(minutes).padStart(2, '0')}:${String(secs).padStart(2, '0')}`;
222
+ }
223
+ /** 将带说话人标签的分段合并为按发言人分段的可读逐字稿(如 `[说话人 A 00:01] ...`)。 */
224
+ export function formatDiarizedTranscript(segments) {
225
+ const valid = segments.filter((seg) => seg.text.trim() !== '');
226
+ if (valid.length === 0)
227
+ return '';
228
+ const groups = [];
229
+ for (const seg of valid) {
230
+ const speaker = typeof seg.speaker === 'string' && seg.speaker.trim() !== ''
231
+ ? seg.speaker.trim()
232
+ : '说话人';
233
+ const part = seg.text.trim();
234
+ const last = groups[groups.length - 1];
235
+ if (last && last.speaker === speaker) {
236
+ const needsSpace = !/[\u4e00-\u9fff\u3000-\u303f\uff00-\uffef]$/.test(last.text)
237
+ || !/^[\u4e00-\u9fff]/.test(part);
238
+ last.text += (needsSpace ? ' ' : '') + part;
239
+ }
240
+ else {
241
+ groups.push({ speaker, start: seg.start, text: part });
242
+ }
243
+ }
244
+ return groups
245
+ .map((group) => `[${group.speaker} ${formatTranscriptTimestamp(group.start)}] ${group.text}`)
246
+ .join('\n\n');
247
+ }
180
248
  /** 检测环境 PATH 中是否可用 ffmpeg。 */
181
249
  export function hasFfmpeg(env = process.env) {
182
250
  try {
@@ -258,6 +326,86 @@ export async function prepareAudioForAsr(audioPath, signal) {
258
326
  throw error;
259
327
  }
260
328
  }
329
+ async function probeAudioChannels(audioPath, signal) {
330
+ throwIfAborted(signal);
331
+ const viaFfprobe = await new Promise((resolve) => {
332
+ execFile('ffprobe', ['-v', 'error', '-select_streams', 'a:0', '-show_entries', 'stream=channels', '-of', 'csv=p=0', audioPath], { signal, maxBuffer: 1024 * 1024 }, (error, stdout) => {
333
+ if (error) {
334
+ resolve(null);
335
+ return;
336
+ }
337
+ const parsed = Number.parseInt(String(stdout).trim(), 10);
338
+ resolve(Number.isFinite(parsed) && parsed > 0 ? parsed : null);
339
+ });
340
+ });
341
+ throwIfAborted(signal);
342
+ if (viaFfprobe !== null)
343
+ return viaFfprobe;
344
+ return await new Promise((resolve) => {
345
+ execFile('ffmpeg', ['-i', audioPath], { signal, maxBuffer: 2 * 1024 * 1024 }, (_error, _stdout, stderr) => {
346
+ const text = typeof stderr === 'string' ? stderr : '';
347
+ const audioLine = text.split(/\r?\n/).find((line) => /\bAudio:\b/.test(line)) ?? '';
348
+ if (/\b(?:stereo|quad|5\.1|7\.1)\b/i.test(audioLine) || /\b2 channels\b/i.test(audioLine)) {
349
+ resolve(2);
350
+ return;
351
+ }
352
+ const chMatch = /\b(\d+)\s+channels?\b/i.exec(audioLine);
353
+ if (chMatch) {
354
+ resolve(Number.parseInt(chMatch[1], 10) || 1);
355
+ return;
356
+ }
357
+ resolve(1);
358
+ });
359
+ });
360
+ }
361
+ /**
362
+ * 探测音频是否包含 >= 2 声道;若为立体声/多声道,拆分为左右声道(16kHz 32kbps 单声道 MP3)用于精准分轨说话人分离。
363
+ * 若非立体声或 ffmpeg 不可用/失败,返回 null。
364
+ */
365
+ export async function splitStereoChannelsForDiarization(audioPath, signal) {
366
+ throwIfAborted(signal);
367
+ let channels = 1;
368
+ try {
369
+ channels = await probeAudioChannels(audioPath, signal);
370
+ }
371
+ catch {
372
+ throwIfAborted(signal);
373
+ return null;
374
+ }
375
+ throwIfAborted(signal);
376
+ if (channels < 2)
377
+ return null;
378
+ const tempDir = mkdtempSync(join(tmpdir(), 'dsh-voice-stereo-'));
379
+ const cleanup = () => {
380
+ try {
381
+ rmSync(tempDir, { recursive: true, force: true });
382
+ }
383
+ catch {
384
+ // 尽力清理临时目录
385
+ }
386
+ };
387
+ try {
388
+ const leftPath = join(tempDir, 'channel_left.mp3');
389
+ const rightPath = join(tempDir, 'channel_right.mp3');
390
+ await runFfmpeg(['-y', '-i', audioPath, '-vn', '-af', 'pan=mono|c0=c0', '-ar', '16000', '-ac', '1', '-b:a', '32k', leftPath], signal);
391
+ await runFfmpeg(['-y', '-i', audioPath, '-vn', '-af', 'pan=mono|c0=c1', '-ar', '16000', '-ac', '1', '-b:a', '32k', rightPath], signal);
392
+ throwIfAborted(signal);
393
+ const leftBlob = await openAsBlob(leftPath, { type: 'audio/mpeg' });
394
+ const rightBlob = await openAsBlob(rightPath, { type: 'audio/mpeg' });
395
+ return {
396
+ channels: [
397
+ { audio: leftBlob, filename: 'channel_left.mp3', speaker: '说话人 A' },
398
+ { audio: rightBlob, filename: 'channel_right.mp3', speaker: '说话人 B' },
399
+ ],
400
+ cleanup,
401
+ };
402
+ }
403
+ catch {
404
+ cleanup();
405
+ throwIfAborted(signal);
406
+ return null;
407
+ }
408
+ }
261
409
  /** 常见音频扩展名 → MIME。 */
262
410
  function mimeOf(filename) {
263
411
  const ext = extname(filename).toLowerCase();
package/lib/tools.d.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  import { type ResolvedVoiceConfig } from './config.js';
2
2
  import { fetchEdgeVoices, synthesizeSpeech, synthesizeSpeechWithMetadata } from './edge-tts.js';
3
3
  import { synthesizeOpenAiSpeech } from './openai-tts.js';
4
- import { prepareAudioForAsr, transcribe } from './stt.js';
5
- import { normalizeAudioLoudness } from './tts-text.js';
4
+ import { prepareAudioForAsr, splitStereoChannelsForDiarization, transcribe } from './stt.js';
5
+ import { mixAudioWithBgm, normalizeAudioLoudness } from './tts-text.js';
6
6
  /** 模型可见的内容块。 */
7
7
  export interface ContentBlock {
8
8
  type: 'text';
@@ -36,6 +36,8 @@ export interface VoiceToolDeps {
36
36
  prepareAudio?: typeof prepareAudioForAsr;
37
37
  fetchVoices?: typeof fetchEdgeVoices;
38
38
  normalizeAudio?: typeof normalizeAudioLoudness;
39
+ mixBgm?: typeof mixAudioWithBgm;
40
+ splitStereo?: typeof splitStereoChannelsForDiarization;
39
41
  }
40
42
  /**
41
43
  * 构建五个语音工具定义(支持静态配置对象或活引用配置 getter)。