dsh-podwave 0.11.27 → 0.11.29
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 +45 -4
- package/README.md +28 -16
- package/dist/client.js +132 -5
- package/package.json +1 -1
- package/scripts/speaker_server.py +114 -0
- package/scripts/translate_local.py +59 -32
- package/src/asr-api.js +230 -0
- package/src/config.js +49 -5
- package/src/host.js +69 -10
- package/src/library.js +24 -14
- package/src/live-speaker.js +242 -0
- package/src/segment.js +205 -0
- package/src/setup.js +36 -16
- package/src/transcribe.js +517 -35
- package/src/translate.js +135 -37
package/src/host.js
CHANGED
|
@@ -56,6 +56,7 @@ import {
|
|
|
56
56
|
deleteTranscript,
|
|
57
57
|
getTranscriptionState,
|
|
58
58
|
migrateAllTranscripts,
|
|
59
|
+
sweepWorkDirs,
|
|
59
60
|
readTranscript,
|
|
60
61
|
runtimeInfo,
|
|
61
62
|
startTranscription,
|
|
@@ -68,10 +69,12 @@ import {
|
|
|
68
69
|
editTranscriptLine,
|
|
69
70
|
enqueueBatch,
|
|
70
71
|
mergeTranscriptLine,
|
|
72
|
+
resegmentTranscript,
|
|
71
73
|
saveTranscript,
|
|
72
74
|
splitTranscriptLine,
|
|
73
75
|
validateTranscript,
|
|
74
76
|
} from './transcribe.js'
|
|
77
|
+
import { closeLiveSpeakers } from './live-speaker.js'
|
|
75
78
|
import { isLinux, isMac, isWindows } from './platform.js'
|
|
76
79
|
import { listJobs, removeJob, upsertJob, getJob } from './jobs.js'
|
|
77
80
|
import { scan as scanSetup, plan as planSetup, install as installSetup, cancelSetup, setupJob, llamaBinary, verify as verifySetup } from './setup.js'
|
|
@@ -256,7 +259,8 @@ function boot() {
|
|
|
256
259
|
booted = (async () => {
|
|
257
260
|
await initState()
|
|
258
261
|
loadXyz()
|
|
259
|
-
await
|
|
262
|
+
await sweepWorkDirs().catch(() => {})
|
|
263
|
+
await migrateAllTranscripts().catch((error) => console.error('[dsh-podwave] transcript migration failed:', error.message))
|
|
260
264
|
if (getState().folders.length) {
|
|
261
265
|
scanLibrary()
|
|
262
266
|
.then(() => refreshWatchers())
|
|
@@ -490,7 +494,8 @@ async function route(ctx, req, res) {
|
|
|
490
494
|
}
|
|
491
495
|
if (routePath === 'api/setup/plan' && method === 'GET') {
|
|
492
496
|
const source = url.searchParams.get('source') || 'auto'
|
|
493
|
-
|
|
497
|
+
const mode = url.searchParams.get('mode') === 'local' ? 'local' : 'basic'
|
|
498
|
+
return json(res, 200, planSetup({ source, mode }))
|
|
494
499
|
}
|
|
495
500
|
|
|
496
501
|
if (routePath === 'api/setup/verify' && method === 'GET') {
|
|
@@ -509,7 +514,7 @@ async function route(ctx, req, res) {
|
|
|
509
514
|
return json(res, 200, {
|
|
510
515
|
ok: true,
|
|
511
516
|
generatedAt: new Date().toISOString(),
|
|
512
|
-
version: '0.11.
|
|
517
|
+
version: '0.11.29',
|
|
513
518
|
platform,
|
|
514
519
|
engine: recommendEngine(platform),
|
|
515
520
|
paths: {
|
|
@@ -526,7 +531,8 @@ async function route(ctx, req, res) {
|
|
|
526
531
|
if (routePath === 'api/setup/install' && method === 'POST') {
|
|
527
532
|
const body = await readJson(req)
|
|
528
533
|
if (setupJob()?.running) return json(res, 409, { error: '安装已在进行中' })
|
|
529
|
-
|
|
534
|
+
// basic:只装 ffmpeg,配合云端 API 用;local:额外装 python 运行时 + 本地模型。
|
|
535
|
+
installSetup({ source: body?.source || 'auto', mode: body?.mode === 'local' ? 'local' : 'basic' }).catch(() => {})
|
|
530
536
|
return json(res, 200, { started: true })
|
|
531
537
|
}
|
|
532
538
|
if (routePath === 'api/setup/cancel' && method === 'POST') {
|
|
@@ -538,7 +544,7 @@ async function route(ctx, req, res) {
|
|
|
538
544
|
}
|
|
539
545
|
if ((method === 'GET' || method === 'HEAD') && routePath === 'api/health') {
|
|
540
546
|
await mediaReady
|
|
541
|
-
return json(res, 200, { ok: true, app: 'podwave', version: '0.11.
|
|
547
|
+
return json(res, 200, { ok: true, app: 'podwave', version: '0.11.29', mediaBase, transcribe: runtimeInfo(), diarize: diarizeInfo(), translate: publicConfig() })
|
|
542
548
|
}
|
|
543
549
|
if (method === 'GET' && routePath === 'api/state') {
|
|
544
550
|
const state = getState()
|
|
@@ -921,7 +927,7 @@ async function route(ctx, req, res) {
|
|
|
921
927
|
}
|
|
922
928
|
if (!episode) return json(res, 404, { error: '单集不存在' })
|
|
923
929
|
const body = await readJson(req)
|
|
924
|
-
const result = await startTranscription(episode, { force: Boolean(body?.force), resume: Boolean(body?.resume) })
|
|
930
|
+
const result = await startTranscription(episode, { force: Boolean(body?.force), resume: Boolean(body?.resume), realtime: Boolean(body?.realtime) })
|
|
925
931
|
return json(res, 200, result)
|
|
926
932
|
}
|
|
927
933
|
|
|
@@ -959,15 +965,22 @@ async function route(ctx, req, res) {
|
|
|
959
965
|
}
|
|
960
966
|
|
|
961
967
|
if (routePath === 'api/transcribe/settings') {
|
|
962
|
-
if (method === 'GET')
|
|
968
|
+
if (method === 'GET') {
|
|
969
|
+
// 密钥统一放在「DeepSeek API」那张卡片里,这里只回引擎选择;前端看 runtime.api.hasKey 判断填没填。
|
|
970
|
+
const section = getSection('transcribe') || {}
|
|
971
|
+
return json(res, 200, { ...section, runtime: runtimeInfo() })
|
|
972
|
+
}
|
|
963
973
|
if (method === 'PUT') {
|
|
964
974
|
const body = await readJson(req)
|
|
965
975
|
const patch = {}
|
|
976
|
+
if (typeof body?.provider === 'string') patch.provider = body.provider === 'local' ? 'local' : 'api'
|
|
966
977
|
if (Array.isArray(body?.hotwords)) patch.hotwords = body.hotwords.map((word) => String(word || '').trim()).filter(Boolean).slice(0, 500)
|
|
967
978
|
if (Number.isFinite(Number(body?.chunkMinutes))) patch.chunkMinutes = Math.max(1, Math.min(60, Number(body.chunkMinutes)))
|
|
968
979
|
if (typeof body?.diarize === 'boolean') patch.diarize = body.diarize
|
|
980
|
+
if (typeof body?.realtime === 'boolean') patch.realtime = body.realtime
|
|
969
981
|
if (!Object.keys(patch).length) return json(res, 400, { error: '没有可保存的设置' })
|
|
970
|
-
|
|
982
|
+
const saved = updateSection('transcribe', patch)
|
|
983
|
+
return json(res, 200, { ...saved, runtime: runtimeInfo() })
|
|
971
984
|
}
|
|
972
985
|
}
|
|
973
986
|
|
|
@@ -1018,6 +1031,21 @@ async function route(ctx, req, res) {
|
|
|
1018
1031
|
}
|
|
1019
1032
|
}
|
|
1020
1033
|
|
|
1034
|
+
const resegmentMatch = /^api\/episodes\/([^/]+)\/transcript\/resegment$/.exec(routePath)
|
|
1035
|
+
if (resegmentMatch && method === 'POST') {
|
|
1036
|
+
const id = decodeURIComponent(resegmentMatch[1])
|
|
1037
|
+
if (!findEpisode(id) && !isXyzId(id)) return json(res, 404, { error: '单集不存在' })
|
|
1038
|
+
const before = await readTranscript(id)
|
|
1039
|
+
if (!before) return json(res, 404, { error: '还没有转写' })
|
|
1040
|
+
try {
|
|
1041
|
+
pushUndo(id, before)
|
|
1042
|
+
const result = await resegmentTranscript(id)
|
|
1043
|
+
return json(res, 200, result)
|
|
1044
|
+
} catch (error) {
|
|
1045
|
+
return json(res, 400, { error: error.message })
|
|
1046
|
+
}
|
|
1047
|
+
}
|
|
1048
|
+
|
|
1021
1049
|
const undoMatch = /^api\/episodes\/([^/]+)\/transcript\/undo$/.exec(routePath)
|
|
1022
1050
|
if (undoMatch && method === 'POST') {
|
|
1023
1051
|
const id = decodeURIComponent(undoMatch[1])
|
|
@@ -1150,7 +1178,25 @@ async function route(ctx, req, res) {
|
|
|
1150
1178
|
})
|
|
1151
1179
|
}
|
|
1152
1180
|
items.sort((a, b) => (b.completedAt || 0) - (a.completedAt || 0))
|
|
1153
|
-
|
|
1181
|
+
let bytes = 0
|
|
1182
|
+
try {
|
|
1183
|
+
for (const file of files) bytes += (await stat(path.join(TRANSCRIPTS_DIR, file))).size
|
|
1184
|
+
} catch {
|
|
1185
|
+
bytes = bytes || 0
|
|
1186
|
+
}
|
|
1187
|
+
return json(res, 200, { items, dir: TRANSCRIPTS_DIR, bytes })
|
|
1188
|
+
}
|
|
1189
|
+
|
|
1190
|
+
if (method === 'POST' && routePath === 'api/history/open') {
|
|
1191
|
+
try {
|
|
1192
|
+
fs.mkdirSync(TRANSCRIPTS_DIR, { recursive: true })
|
|
1193
|
+
const command = process.platform === 'darwin' ? 'open' : process.platform === 'win32' ? 'explorer' : 'xdg-open'
|
|
1194
|
+
const { spawn } = await import('node:child_process')
|
|
1195
|
+
spawn(command, [TRANSCRIPTS_DIR], { detached: true, stdio: 'ignore' }).unref()
|
|
1196
|
+
} catch {
|
|
1197
|
+
return json(res, 500, { error: '无法打开文件夹' })
|
|
1198
|
+
}
|
|
1199
|
+
return json(res, 200, { ok: true, dir: TRANSCRIPTS_DIR })
|
|
1154
1200
|
}
|
|
1155
1201
|
|
|
1156
1202
|
if (routePath === 'api/ui-settings') {
|
|
@@ -1160,6 +1206,16 @@ async function route(ctx, req, res) {
|
|
|
1160
1206
|
const patch = {}
|
|
1161
1207
|
if (typeof body?.showSidebarPanel === 'boolean') patch.showSidebarPanel = body.showSidebarPanel
|
|
1162
1208
|
if (typeof body?.sidebarCollapsed === 'boolean') patch.sidebarCollapsed = body.sidebarCollapsed
|
|
1209
|
+
if (Number.isFinite(Number(body?.subtitleSize))) patch.subtitleSize = Math.max(12, Math.min(42, Math.round(Number(body.subtitleSize))))
|
|
1210
|
+
if (typeof body?.captionOpen === 'boolean') patch.captionOpen = body.captionOpen
|
|
1211
|
+
// 悬浮字幕的位置:传 null / 空串表示回到默认位置(注意 Number(null) 是 0,不能直接判有限)。
|
|
1212
|
+
const coordinate = (value) => {
|
|
1213
|
+
if (value === null || value === undefined || value === '') return null
|
|
1214
|
+
const number = Number(value)
|
|
1215
|
+
return Number.isFinite(number) ? Math.round(number) : null
|
|
1216
|
+
}
|
|
1217
|
+
if (body && 'captionX' in body) patch.captionX = coordinate(body.captionX)
|
|
1218
|
+
if (body && 'captionY' in body) patch.captionY = coordinate(body.captionY)
|
|
1163
1219
|
if (!Object.keys(patch).length) return json(res, 400, { error: '没有可保存的设置' })
|
|
1164
1220
|
return json(res, 200, saveUiSettings(patch))
|
|
1165
1221
|
}
|
|
@@ -1285,6 +1341,9 @@ export default {
|
|
|
1285
1341
|
}),
|
|
1286
1342
|
'dsh-podwave: /podwave routes',
|
|
1287
1343
|
)
|
|
1288
|
-
ctx.effect(() => () =>
|
|
1344
|
+
ctx.effect(() => () => {
|
|
1345
|
+
stopAll()
|
|
1346
|
+
closeLiveSpeakers()
|
|
1347
|
+
}, 'dsh-podwave: stop transcription workers')
|
|
1289
1348
|
},
|
|
1290
1349
|
}
|
package/src/library.js
CHANGED
|
@@ -42,7 +42,7 @@ function isInside(parent, child) {
|
|
|
42
42
|
return rel !== '' && !rel.startsWith('..') && !path.isAbsolute(rel)
|
|
43
43
|
}
|
|
44
44
|
|
|
45
|
-
async function walk(dir, depth, files) {
|
|
45
|
+
async function walk(dir, depth, files, root, realRoot = root) {
|
|
46
46
|
let entries
|
|
47
47
|
try {
|
|
48
48
|
entries = await fsp.readdir(dir, { withFileTypes: true })
|
|
@@ -53,14 +53,15 @@ async function walk(dir, depth, files) {
|
|
|
53
53
|
if (entry.name.startsWith('.')) continue
|
|
54
54
|
const full = path.join(dir, entry.name)
|
|
55
55
|
if (entry.isDirectory()) {
|
|
56
|
-
if (depth < SCAN_DEPTH) await walk(full, depth + 1, files)
|
|
56
|
+
if (depth < SCAN_DEPTH) await walk(full, depth + 1, files, root, realRoot)
|
|
57
57
|
} else if (entry.isFile() && AUDIO_EXTENSIONS.has(path.extname(entry.name).toLowerCase())) {
|
|
58
|
-
|
|
58
|
+
// 记下这个文件属于哪个「添加进来的根文件夹」,前端才能按目录逐层浏览。
|
|
59
|
+
files.push({ file: full, root, realRoot })
|
|
59
60
|
}
|
|
60
61
|
}
|
|
61
62
|
}
|
|
62
63
|
|
|
63
|
-
async function readEpisode(file) {
|
|
64
|
+
async function readEpisode(file, root, realRoot = root) {
|
|
64
65
|
const stat = await fsp.stat(file)
|
|
65
66
|
const id = episodeId(file)
|
|
66
67
|
const cacheKey = `${stat.mtimeMs}:${stat.size}`
|
|
@@ -107,6 +108,9 @@ async function readEpisode(file) {
|
|
|
107
108
|
duration,
|
|
108
109
|
hasCover,
|
|
109
110
|
folder: path.dirname(file),
|
|
111
|
+
root,
|
|
112
|
+
// 相对根文件夹的路径('' 表示就在根目录里),前端靠它逐层浏览目录树。
|
|
113
|
+
rel: path.relative(realRoot || root || '', path.dirname(file)).split(path.sep).join('/'),
|
|
110
114
|
file,
|
|
111
115
|
}
|
|
112
116
|
metaCache.set(id, { key: cacheKey, data })
|
|
@@ -125,31 +129,37 @@ async function doScan() {
|
|
|
125
129
|
} catch {
|
|
126
130
|
continue
|
|
127
131
|
}
|
|
128
|
-
|
|
132
|
+
let realRoot = folder
|
|
133
|
+
try {
|
|
134
|
+
realRoot = await fsp.realpath(folder)
|
|
135
|
+
} catch {
|
|
136
|
+
realRoot = folder
|
|
137
|
+
}
|
|
138
|
+
await walk(folder, 0, files, folder, realRoot)
|
|
129
139
|
}
|
|
130
140
|
|
|
131
141
|
const unique = []
|
|
132
142
|
const seen = new Set()
|
|
133
|
-
for (const
|
|
134
|
-
let real = file
|
|
143
|
+
for (const entry of files) {
|
|
144
|
+
let real = entry.file
|
|
135
145
|
try {
|
|
136
|
-
real = await fsp.realpath(file)
|
|
146
|
+
real = await fsp.realpath(entry.file)
|
|
137
147
|
} catch {
|
|
138
148
|
/* keep the original path */
|
|
139
149
|
}
|
|
140
150
|
if (seen.has(real)) continue
|
|
141
151
|
seen.add(real)
|
|
142
|
-
unique.push(real)
|
|
152
|
+
unique.push({ file: real, root: entry.root, realRoot: entry.realRoot })
|
|
143
153
|
}
|
|
144
154
|
|
|
145
155
|
const items = []
|
|
146
156
|
let cursor = 0
|
|
147
157
|
async function worker() {
|
|
148
158
|
while (cursor < unique.length) {
|
|
149
|
-
const
|
|
159
|
+
const entry = unique[cursor]
|
|
150
160
|
cursor += 1
|
|
151
161
|
try {
|
|
152
|
-
items.push(await readEpisode(file))
|
|
162
|
+
items.push(await readEpisode(entry.file, entry.root, entry.realRoot))
|
|
153
163
|
} catch {
|
|
154
164
|
/* skip unreadable files */
|
|
155
165
|
}
|
|
@@ -196,10 +206,10 @@ export function getEpisodePaths() {
|
|
|
196
206
|
}
|
|
197
207
|
|
|
198
208
|
export function getEpisodes() {
|
|
199
|
-
return episodes.map(({ file,
|
|
209
|
+
return episodes.map(({ file, ...rest }) => ({
|
|
200
210
|
...rest,
|
|
201
|
-
//
|
|
202
|
-
location: folder.split(path.sep).slice(-2).join('/'),
|
|
211
|
+
// folder/root 是绝对路径,前端靠它们做逐层目录浏览;location 只是给人看的短路径。
|
|
212
|
+
location: (rest.folder || '').split(path.sep).slice(-2).join('/'),
|
|
203
213
|
}))
|
|
204
214
|
}
|
|
205
215
|
|
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
// 实时说话人:常驻声纹服务 + 在线聚类。
|
|
2
|
+
//
|
|
3
|
+
// 整段说话人分离(diarize.js)要等全部音频转完才跑,实时跟播用不了。
|
|
4
|
+
// 这里的做法是「边播边猜」:每行字幕出来就取那一小段音频算一个声纹向量,
|
|
5
|
+
// 和已知说话人的质心比余弦相似度,够像就归入,不像就新建一个。
|
|
6
|
+
// 播完以后 transcribe.js 还会用整段 diarize 再校正一遍。
|
|
7
|
+
//
|
|
8
|
+
// 声纹模型和 diarize 用的是同一份 3D-Speaker embedding 模型(本地模型目录里已有)。
|
|
9
|
+
import fs from 'node:fs'
|
|
10
|
+
import path from 'node:path'
|
|
11
|
+
import { spawn } from 'node:child_process'
|
|
12
|
+
import { fileURLToPath } from 'node:url'
|
|
13
|
+
import { venvPython } from './platform.js'
|
|
14
|
+
import { diarizationPaths } from './diarize.js'
|
|
15
|
+
|
|
16
|
+
const SCRIPT = path.join(path.dirname(fileURLToPath(import.meta.url)), '..', 'scripts', 'speaker_server.py')
|
|
17
|
+
const READY_TIMEOUT_MS = 20000
|
|
18
|
+
const REQUEST_TIMEOUT_MS = 12000
|
|
19
|
+
// 同一个人在相邻片段上的余弦相似度一般 0.75 以上,不同人通常低于 0.5。
|
|
20
|
+
// 取 0.72 宁可多认出一个人,也别把两个人并成一个(后面还有整段校正兜底)。
|
|
21
|
+
const MATCH_THRESHOLD = 0.72
|
|
22
|
+
const MAX_SPEAKERS = 12
|
|
23
|
+
|
|
24
|
+
export function liveSpeakerInfo() {
|
|
25
|
+
const python = venvPython()
|
|
26
|
+
const { embModel } = diarizationPaths()
|
|
27
|
+
return {
|
|
28
|
+
available: fs.existsSync(python) && fs.existsSync(SCRIPT) && fs.existsSync(embModel),
|
|
29
|
+
python,
|
|
30
|
+
script: SCRIPT,
|
|
31
|
+
embModel,
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function normalize(vector) {
|
|
36
|
+
let sum = 0
|
|
37
|
+
for (const value of vector) sum += value * value
|
|
38
|
+
const length = Math.sqrt(sum) || 1
|
|
39
|
+
return vector.map((value) => value / length)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function cosine(left, right) {
|
|
43
|
+
let sum = 0
|
|
44
|
+
for (let index = 0; index < left.length; index += 1) sum += left[index] * right[index]
|
|
45
|
+
return sum
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// 常驻进程:模型加载一次要一两秒,每行都起进程肯定来不及。
|
|
49
|
+
class SpeakerServer {
|
|
50
|
+
constructor(child) {
|
|
51
|
+
this.child = child
|
|
52
|
+
this.pending = new Map()
|
|
53
|
+
this.nextId = 0
|
|
54
|
+
this.buffer = ''
|
|
55
|
+
this.closed = false
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
static async start(info) {
|
|
59
|
+
const child = spawn(info.python, [SCRIPT, '--model', info.embModel], {
|
|
60
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
61
|
+
env: { ...process.env, PYTHONUNBUFFERED: '1' },
|
|
62
|
+
})
|
|
63
|
+
const server = new SpeakerServer(child)
|
|
64
|
+
const ready = new Promise((resolve, reject) => {
|
|
65
|
+
const timer = setTimeout(() => reject(new Error('声纹服务启动超时')), READY_TIMEOUT_MS)
|
|
66
|
+
const onExit = () => { clearTimeout(timer); reject(new Error('声纹服务没能启动')) }
|
|
67
|
+
child.once('exit', onExit)
|
|
68
|
+
server.readyResolve = (value) => { clearTimeout(timer); child.off('exit', onExit); resolve(value) }
|
|
69
|
+
server.readyReject = (error) => { clearTimeout(timer); child.off('exit', onExit); reject(error) }
|
|
70
|
+
})
|
|
71
|
+
server.wire()
|
|
72
|
+
await ready
|
|
73
|
+
return server
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
wire() {
|
|
77
|
+
this.child.stdout.on('data', (chunk) => {
|
|
78
|
+
this.buffer += chunk.toString()
|
|
79
|
+
let index = this.buffer.indexOf('\n')
|
|
80
|
+
while (index >= 0) {
|
|
81
|
+
const line = this.buffer.slice(0, index).trim()
|
|
82
|
+
this.buffer = this.buffer.slice(index + 1)
|
|
83
|
+
if (line) this.handle(line)
|
|
84
|
+
index = this.buffer.indexOf('\n')
|
|
85
|
+
}
|
|
86
|
+
})
|
|
87
|
+
this.child.stderr.on('data', () => { /* 服务端日志忽略,出错会从 stdout 回来 */ })
|
|
88
|
+
this.child.on('exit', () => {
|
|
89
|
+
this.closed = true
|
|
90
|
+
for (const [, entry] of this.pending) entry.reject(new Error('声纹服务已退出'))
|
|
91
|
+
this.pending.clear()
|
|
92
|
+
if (this.readyReject) this.readyReject(new Error('声纹服务已退出'))
|
|
93
|
+
})
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
handle(line) {
|
|
97
|
+
let payload
|
|
98
|
+
try {
|
|
99
|
+
payload = JSON.parse(line)
|
|
100
|
+
} catch {
|
|
101
|
+
return
|
|
102
|
+
}
|
|
103
|
+
if (payload.ready) {
|
|
104
|
+
this.readyResolve?.(true)
|
|
105
|
+
return
|
|
106
|
+
}
|
|
107
|
+
if (payload.error && payload.id === undefined) {
|
|
108
|
+
this.readyReject?.(new Error(payload.error))
|
|
109
|
+
return
|
|
110
|
+
}
|
|
111
|
+
const entry = this.pending.get(payload.id)
|
|
112
|
+
if (!entry) return
|
|
113
|
+
this.pending.delete(payload.id)
|
|
114
|
+
clearTimeout(entry.timer)
|
|
115
|
+
if (payload.error) entry.reject(new Error(payload.error))
|
|
116
|
+
else entry.resolve(payload.embedding || null)
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
request(audio, start, end) {
|
|
120
|
+
if (this.closed) return Promise.reject(new Error('声纹服务已退出'))
|
|
121
|
+
const id = (this.nextId += 1)
|
|
122
|
+
return new Promise((resolve, reject) => {
|
|
123
|
+
const timer = setTimeout(() => {
|
|
124
|
+
this.pending.delete(id)
|
|
125
|
+
reject(new Error('声纹计算超时'))
|
|
126
|
+
}, REQUEST_TIMEOUT_MS)
|
|
127
|
+
this.pending.set(id, { resolve, reject, timer })
|
|
128
|
+
this.child.stdin.write(`${JSON.stringify({ id, audio, start, end })}\n`)
|
|
129
|
+
})
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
close() {
|
|
133
|
+
this.closed = true
|
|
134
|
+
try { this.child.stdin.end() } catch { /* 已经关了 */ }
|
|
135
|
+
try { this.child.kill() } catch { /* 已经退了 */ }
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// 多个转写任务共用一个常驻进程(同一个 profile 只有一个用户)。
|
|
140
|
+
let sharedServer = null
|
|
141
|
+
|
|
142
|
+
async function ensureServer() {
|
|
143
|
+
const info = liveSpeakerInfo()
|
|
144
|
+
if (!info.available) return null
|
|
145
|
+
if (!sharedServer || sharedServer.closed) sharedServer = await SpeakerServer.start(info)
|
|
146
|
+
return sharedServer
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
async function sharedEmbed(audio, start, end) {
|
|
150
|
+
const server = await ensureServer()
|
|
151
|
+
if (!server) return null
|
|
152
|
+
return server.request(audio, start, end)
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
export function closeLiveSpeakers() {
|
|
156
|
+
if (sharedServer) {
|
|
157
|
+
sharedServer.close()
|
|
158
|
+
sharedServer = null
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
export function partForTime(parts, time) {
|
|
163
|
+
for (const part of parts) {
|
|
164
|
+
if (time >= part.start - 0.001 && time < part.end) return part
|
|
165
|
+
}
|
|
166
|
+
return parts[parts.length - 1] || null
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
export class OnlineSpeakers {
|
|
170
|
+
constructor(embed) {
|
|
171
|
+
this.embed = embed
|
|
172
|
+
this.centroids = []
|
|
173
|
+
this.counts = []
|
|
174
|
+
this.cache = new Map()
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
assign(vector) {
|
|
178
|
+
const unit = normalize(vector)
|
|
179
|
+
let best = -1
|
|
180
|
+
let bestScore = -Infinity
|
|
181
|
+
for (let index = 0; index < this.centroids.length; index += 1) {
|
|
182
|
+
const score = cosine(unit, this.centroids[index])
|
|
183
|
+
if (score > bestScore) {
|
|
184
|
+
bestScore = score
|
|
185
|
+
best = index
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
if (best >= 0 && bestScore >= MATCH_THRESHOLD) {
|
|
189
|
+
const count = this.counts[best] + 1
|
|
190
|
+
const merged = this.centroids[best].map((value, index) => (value * this.counts[best] + unit[index]) / count)
|
|
191
|
+
this.centroids[best] = normalize(merged)
|
|
192
|
+
this.counts[best] = count
|
|
193
|
+
return best
|
|
194
|
+
}
|
|
195
|
+
if (this.centroids.length >= MAX_SPEAKERS) return best >= 0 ? best : null
|
|
196
|
+
this.centroids.push(unit)
|
|
197
|
+
this.counts.push(1)
|
|
198
|
+
return this.centroids.length - 1
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
speakers() {
|
|
202
|
+
return this.centroids.map((_, index) => ({ id: index, label: `说话人 ${index + 1}` }))
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// 给还没有 speaker 的行打实时标签。已有的不覆盖(可能来自整段校正)。
|
|
206
|
+
async labelLines(lines, parts) {
|
|
207
|
+
for (const line of lines) {
|
|
208
|
+
if (line.speaker !== null && line.speaker !== undefined) continue
|
|
209
|
+
const key = `${Math.round(line.start * 100)}|${line.text}`
|
|
210
|
+
let index = this.cache.get(key)
|
|
211
|
+
if (index === undefined) {
|
|
212
|
+
const part = partForTime(parts, line.start)
|
|
213
|
+
if (!part) continue
|
|
214
|
+
const start = Math.max(0, line.start - part.start)
|
|
215
|
+
const end = Math.min(part.duration, line.end - part.start)
|
|
216
|
+
if (end - start < 0.4) continue
|
|
217
|
+
let vector = null
|
|
218
|
+
try {
|
|
219
|
+
vector = await this.embed(part.file, start, end)
|
|
220
|
+
} catch {
|
|
221
|
+
vector = null
|
|
222
|
+
}
|
|
223
|
+
if (!vector) continue
|
|
224
|
+
index = this.assign(vector)
|
|
225
|
+
if (index === null) continue
|
|
226
|
+
this.cache.set(key, index)
|
|
227
|
+
}
|
|
228
|
+
line.speaker = index
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
// 拿不到本地声纹服务(没装 python 运行时/模型)就返回 null,实时只做转写和翻译。
|
|
234
|
+
export async function createLiveSpeakers() {
|
|
235
|
+
try {
|
|
236
|
+
const server = await ensureServer()
|
|
237
|
+
if (!server) return null
|
|
238
|
+
return new OnlineSpeakers(sharedEmbed)
|
|
239
|
+
} catch {
|
|
240
|
+
return null
|
|
241
|
+
}
|
|
242
|
+
}
|