@hyperwindmill/caretaker-cli 0.30.0 → 0.32.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/dist/cli/web/mcp_bridge.d.ts +1 -0
- package/dist/cli/web/mcp_bridge.d.ts.map +1 -1
- package/dist/cli/web/mcp_bridge.js +11 -2
- package/dist/cli/web/scheduler/email_triage.d.ts +63 -0
- package/dist/cli/web/scheduler/email_triage.d.ts.map +1 -0
- package/dist/cli/web/scheduler/email_triage.js +247 -0
- package/dist/cli/web/scheduler/email_triage_agent.d.ts +16 -0
- package/dist/cli/web/scheduler/email_triage_agent.d.ts.map +1 -0
- package/dist/cli/web/scheduler/email_triage_agent.js +93 -0
- package/dist/cli/web/scheduler.d.ts.map +1 -1
- package/dist/cli/web/scheduler.js +2 -0
- package/dist/cli/web/voice_backend.d.ts +4 -0
- package/dist/cli/web/voice_backend.d.ts.map +1 -1
- package/dist/cli/web/voice_backend.js +14 -3
- package/dist/cli/web/voice_builtin.d.ts.map +1 -1
- package/dist/cli/web/voice_builtin.js +32 -13
- package/dist/cli/web/voice_proxy.d.ts +3 -0
- package/dist/cli/web/voice_proxy.d.ts.map +1 -1
- package/dist/cli/web/voice_proxy.js +28 -3
- package/dist/harness/tools/builtin/email_tools.d.ts +1 -0
- package/dist/harness/tools/builtin/email_tools.d.ts.map +1 -1
- package/dist/harness/tools/builtin/email_tools.js +41 -1
- package/dist/harness/tools/builtin/index.d.ts +2 -2
- package/dist/harness/tools/builtin/index.d.ts.map +1 -1
- package/dist/harness/tools/builtin/index.js +3 -2
- package/dist/harness/tools/builtin/set_workspace.d.ts.map +1 -1
- package/dist/harness/tools/builtin/set_workspace.js +26 -4
- package/dist/lib/email.d.ts +20 -0
- package/dist/lib/email.d.ts.map +1 -1
- package/dist/lib/email.js +106 -33
- package/dist/session/store.d.ts +2 -2
- package/dist/session/store.d.ts.map +1 -1
- package/dist/session/store.js +5 -2
- package/dist/session/types.d.ts +2 -1
- package/dist/session/types.d.ts.map +1 -1
- package/dist/store/db.d.ts.map +1 -1
- package/dist/store/db.js +3 -1
- package/dist/voice/pocket/install.d.ts +16 -2
- package/dist/voice/pocket/install.d.ts.map +1 -1
- package/dist/voice/pocket/install.js +59 -5
- package/dist/voice/pronunciation.d.ts.map +1 -1
- package/dist/voice/pronunciation.js +3 -0
- package/dist/voice/whisper/catalog.d.ts +15 -0
- package/dist/voice/whisper/catalog.d.ts.map +1 -0
- package/dist/voice/whisper/catalog.js +53 -0
- package/dist/voice/whisper/decoder.d.ts +3 -0
- package/dist/voice/whisper/decoder.d.ts.map +1 -0
- package/dist/voice/whisper/decoder.js +48 -0
- package/dist/voice/whisper/engine.d.ts +8 -0
- package/dist/voice/whisper/engine.d.ts.map +1 -0
- package/dist/voice/whisper/engine.js +77 -0
- package/dist/voice/whisper/features.d.ts +10 -0
- package/dist/voice/whisper/features.d.ts.map +1 -0
- package/dist/voice/whisper/features.js +149 -0
- package/dist/voice/whisper/inference.d.ts +17 -0
- package/dist/voice/whisper/inference.d.ts.map +1 -0
- package/dist/voice/whisper/inference.js +257 -0
- package/dist/voice/whisper/wav.d.ts +5 -0
- package/dist/voice/whisper/wav.d.ts.map +1 -0
- package/dist/voice/whisper/wav.js +48 -0
- package/dist/webview/standalone.js +1114 -689
- package/dist/webview/standalone.js.map +4 -4
- package/package.json +3 -3
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { PronunciationEntry } from 'caretaker-types';
|
|
2
2
|
import { ORT_TARBALLS, POCKET_LANGUAGES, type PocketLanguage } from './catalog.js';
|
|
3
|
+
import { WHISPER_MODELS, type WhisperSize } from '../whisper/catalog.js';
|
|
3
4
|
export type BuiltinVoiceStatus = {
|
|
4
5
|
runtime: {
|
|
5
6
|
installed: boolean;
|
|
@@ -13,11 +14,16 @@ export type BuiltinVoiceStatus = {
|
|
|
13
14
|
installed: boolean;
|
|
14
15
|
bytes: number;
|
|
15
16
|
}>;
|
|
17
|
+
stt: Array<{
|
|
18
|
+
id: WhisperSize;
|
|
19
|
+
installed: boolean;
|
|
20
|
+
bytes: number;
|
|
21
|
+
}>;
|
|
16
22
|
voices: string[];
|
|
17
23
|
defaults: Record<string, PronunciationEntry[]>;
|
|
18
24
|
};
|
|
19
25
|
export type BuiltinInstallProgress = {
|
|
20
|
-
step: 'runtime' | 'bundle';
|
|
26
|
+
step: 'runtime' | 'bundle' | 'model';
|
|
21
27
|
file: string;
|
|
22
28
|
received: number;
|
|
23
29
|
total: number;
|
|
@@ -34,6 +40,8 @@ type Catalog = {
|
|
|
34
40
|
ortVersion: string;
|
|
35
41
|
revision: string;
|
|
36
42
|
baseUrl: string;
|
|
43
|
+
whisper: typeof WHISPER_MODELS;
|
|
44
|
+
whisperBaseUrl: string;
|
|
37
45
|
};
|
|
38
46
|
export declare function __setCatalogForTest(c: Partial<Catalog>): void;
|
|
39
47
|
export declare function __setFetch(f: typeof fetch): void;
|
|
@@ -42,13 +50,19 @@ export declare function runtimeDir(): string;
|
|
|
42
50
|
/** Absolute path of onnxruntime-node's entry file, for `createRequire(...)`. */
|
|
43
51
|
export declare function runtimeEntry(): string;
|
|
44
52
|
export declare function bundleDir(lang: PocketLanguage): string;
|
|
53
|
+
/** `<dataDir>/voice/whisper/<size>/<revision>`; the repo's `onnx/` subdirectory is kept. */
|
|
54
|
+
export declare function whisperDir(size: WhisperSize): string;
|
|
55
|
+
export declare function installBuiltinStt(size: WhisperSize): AsyncGenerator<BuiltinInstallProgress>;
|
|
45
56
|
export declare function installBuiltinVoice(lang: PocketLanguage): AsyncGenerator<BuiltinInstallProgress>;
|
|
46
|
-
/** Removes the language's bundle(s); removes the runtime too once
|
|
57
|
+
/** Removes the language's bundle(s); removes the runtime too once nothing else needs it,
|
|
47
58
|
* unless `keepRuntime` (the native binding is loaded in this process: Windows refuses
|
|
48
59
|
* to delete it, and a half-deleted runtime would break reinstalling). */
|
|
49
60
|
export declare function deleteBuiltinVoice(lang: PocketLanguage, opts?: {
|
|
50
61
|
keepRuntime?: boolean;
|
|
51
62
|
}): Promise<void>;
|
|
63
|
+
export declare function deleteBuiltinStt(size: WhisperSize, opts?: {
|
|
64
|
+
keepRuntime?: boolean;
|
|
65
|
+
}): Promise<void>;
|
|
52
66
|
export declare function builtinVoiceStatus(): Promise<BuiltinVoiceStatus>;
|
|
53
67
|
export {};
|
|
54
68
|
//# sourceMappingURL=install.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"install.d.ts","sourceRoot":"","sources":["../../../src/voice/pocket/install.ts"],"names":[],"mappings":"AAeA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AAG1D,OAAO,EACL,YAAY,EAGZ,gBAAgB,EAGhB,KAAK,cAAc,EACpB,MAAM,cAAc,CAAC;
|
|
1
|
+
{"version":3,"file":"install.d.ts","sourceRoot":"","sources":["../../../src/voice/pocket/install.ts"],"names":[],"mappings":"AAeA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AAG1D,OAAO,EACL,YAAY,EAGZ,gBAAgB,EAGhB,KAAK,cAAc,EACpB,MAAM,cAAc,CAAC;AACtB,OAAO,EAAoB,cAAc,EAAE,KAAK,WAAW,EAAE,MAAM,uBAAuB,CAAC;AAG3F,MAAM,MAAM,kBAAkB,GAAG;IAC/B,OAAO,EAAE;QAAE,SAAS,EAAE,OAAO,CAAC;QAAC,OAAO,EAAE,MAAM,CAAC;QAAC,SAAS,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC;IACrE,SAAS,EAAE,KAAK,CAAC;QAAE,EAAE,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAC;QAAC,WAAW,EAAE,MAAM,CAAC;QAAC,SAAS,EAAE,OAAO,CAAC;QAAC,KAAK,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC;IACvG,GAAG,EAAE,KAAK,CAAC;QAAE,EAAE,EAAE,WAAW,CAAC;QAAC,SAAS,EAAE,OAAO,CAAC;QAAC,KAAK,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC;IACnE,MAAM,EAAE,MAAM,EAAE,CAAC;IACjB,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,kBAAkB,EAAE,CAAC,CAAC;CAChD,CAAC;AACF,MAAM,MAAM,sBAAsB,GAC9B;IAAE,IAAI,EAAE,SAAS,GAAG,QAAQ,GAAG,OAAO,CAAC;IAAC,IAAI,EAAE,MAAM,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,GACvF;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,kBAAkB,CAAA;CAAE,GAC5C;IAAE,IAAI,EAAE,OAAO,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,CAAC;AAEvC,KAAK,OAAO,GAAG;IACb,SAAS,EAAE,OAAO,gBAAgB,CAAC;IACnC,QAAQ,EAAE,OAAO,YAAY,CAAC;IAC9B,UAAU,EAAE,MAAM,CAAC;IACnB,QAAQ,EAAE,MAAM,CAAC;IACjB,OAAO,EAAE,MAAM,CAAC;IAChB,OAAO,EAAE,OAAO,cAAc,CAAC;IAC/B,cAAc,EAAE,MAAM,CAAC;CACxB,CAAC;AAWF,wBAAgB,mBAAmB,CAAC,CAAC,EAAE,OAAO,CAAC,OAAO,CAAC,GAAG,IAAI,CAE7D;AAED,wBAAgB,UAAU,CAAC,CAAC,EAAE,OAAO,KAAK,GAAG,IAAI,CAEhD;AACD,wBAAgB,YAAY,IAAI,IAAI,CAGnC;AAGD,wBAAgB,UAAU,IAAI,MAAM,CAEnC;AACD,gFAAgF;AAChF,wBAAgB,YAAY,IAAI,MAAM,CAErC;AACD,wBAAgB,SAAS,CAAC,IAAI,EAAE,cAAc,GAAG,MAAM,CAEtD;AACD,4FAA4F;AAC5F,wBAAgB,UAAU,CAAC,IAAI,EAAE,WAAW,GAAG,MAAM,CAEpD;AAoND,wBAAuB,iBAAiB,CAAC,IAAI,EAAE,WAAW,GAAG,cAAc,CAAC,sBAAsB,CAAC,CASlG;AAED,wBAAuB,mBAAmB,CAAC,IAAI,EAAE,cAAc,GAAG,cAAc,CAAC,sBAAsB,CAAC,CASvG;AAcD;;0EAE0E;AAC1E,wBAAsB,kBAAkB,CAAC,IAAI,EAAE,cAAc,EAAE,IAAI,GAAE;IAAE,WAAW,CAAC,EAAE,OAAO,CAAA;CAAO,GAAG,OAAO,CAAC,IAAI,CAAC,CAGlH;AAED,wBAAsB,gBAAgB,CAAC,IAAI,EAAE,WAAW,EAAE,IAAI,GAAE;IAAE,WAAW,CAAC,EAAE,OAAO,CAAA;CAAO,GAAG,OAAO,CAAC,IAAI,CAAC,CAG7G;AAED,wBAAsB,kBAAkB,IAAI,OAAO,CAAC,kBAAkB,CAAC,CAwBtE"}
|
|
@@ -15,12 +15,15 @@ import { createGunzip } from 'node:zlib';
|
|
|
15
15
|
import { dataDir } from '../../store/json.js';
|
|
16
16
|
import { DEFAULT_PRONUNCIATION } from '../pronunciation.js';
|
|
17
17
|
import { ORT_TARBALLS, ORT_VERSION, POCKET_BASE_URL, POCKET_LANGUAGES, POCKET_REVISION, POCKET_VOICES, } from './catalog.js';
|
|
18
|
+
import { WHISPER_BASE_URL, WHISPER_MODELS } from '../whisper/catalog.js';
|
|
18
19
|
const REAL_CATALOG = {
|
|
19
20
|
languages: POCKET_LANGUAGES,
|
|
20
21
|
tarballs: ORT_TARBALLS,
|
|
21
22
|
ortVersion: ORT_VERSION,
|
|
22
23
|
revision: POCKET_REVISION,
|
|
23
24
|
baseUrl: POCKET_BASE_URL,
|
|
25
|
+
whisper: WHISPER_MODELS,
|
|
26
|
+
whisperBaseUrl: WHISPER_BASE_URL,
|
|
24
27
|
};
|
|
25
28
|
let cat = REAL_CATALOG;
|
|
26
29
|
export function __setCatalogForTest(c) {
|
|
@@ -45,6 +48,10 @@ export function runtimeEntry() {
|
|
|
45
48
|
export function bundleDir(lang) {
|
|
46
49
|
return join(dataDir(), 'voice', 'pocket', cat.languages[lang].dir, cat.revision);
|
|
47
50
|
}
|
|
51
|
+
/** `<dataDir>/voice/whisper/<size>/<revision>`; the repo's `onnx/` subdirectory is kept. */
|
|
52
|
+
export function whisperDir(size) {
|
|
53
|
+
return join(dataDir(), 'voice', 'whisper', size, cat.whisper[size].revision);
|
|
54
|
+
}
|
|
48
55
|
const tmpName = (p) => `${p}.tmp-${process.pid}`;
|
|
49
56
|
/** Streams `url` to `dest`, verifying the digest. `expected` is a sha256 hex string or an
|
|
50
57
|
* npm `sha512-<base64>` integrity string. Removes `dest` and throws on any failure. */
|
|
@@ -232,6 +239,39 @@ async function* installBundle(lang) {
|
|
|
232
239
|
throw e;
|
|
233
240
|
}
|
|
234
241
|
}
|
|
242
|
+
async function* installModel(size) {
|
|
243
|
+
const dir = whisperDir(size);
|
|
244
|
+
if (existsSync(dir))
|
|
245
|
+
return;
|
|
246
|
+
const spec = cat.whisper[size];
|
|
247
|
+
const tmp = tmpName(dir);
|
|
248
|
+
await rm(tmp, { recursive: true, force: true });
|
|
249
|
+
try {
|
|
250
|
+
for (const f of spec.files) {
|
|
251
|
+
const url = `${cat.whisperBaseUrl}/${spec.repo}/resolve/${spec.revision}/${f.name}`;
|
|
252
|
+
for await (const g of download(url, join(tmp, f.name), f.sha256, f.name, f.bytes)) {
|
|
253
|
+
yield { step: 'model', file: f.name, ...g };
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
await rename(tmp, dir);
|
|
257
|
+
}
|
|
258
|
+
catch (e) {
|
|
259
|
+
await rm(tmp, { recursive: true, force: true });
|
|
260
|
+
throw e;
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
export async function* installBuiltinStt(size) {
|
|
264
|
+
try {
|
|
265
|
+
if (!cat.whisper[size])
|
|
266
|
+
throw new Error(`Unknown model size: ${size}`);
|
|
267
|
+
yield* installRuntime();
|
|
268
|
+
yield* installModel(size);
|
|
269
|
+
yield { step: 'done', status: await builtinVoiceStatus() };
|
|
270
|
+
}
|
|
271
|
+
catch (e) {
|
|
272
|
+
yield { step: 'error', message: e instanceof Error ? e.message : String(e) };
|
|
273
|
+
}
|
|
274
|
+
}
|
|
235
275
|
export async function* installBuiltinVoice(lang) {
|
|
236
276
|
try {
|
|
237
277
|
if (!cat.languages[lang])
|
|
@@ -244,16 +284,25 @@ export async function* installBuiltinVoice(lang) {
|
|
|
244
284
|
yield { step: 'error', message: e instanceof Error ? e.message : String(e) };
|
|
245
285
|
}
|
|
246
286
|
}
|
|
247
|
-
|
|
248
|
-
|
|
287
|
+
/** The runtime is shared: it stays while any Pocket bundle or Whisper model remains. */
|
|
288
|
+
async function anyModelInstalled() {
|
|
289
|
+
return (Object.keys(cat.languages).some((l) => existsSync(bundleDir(l))) ||
|
|
290
|
+
Object.keys(cat.whisper).some((z) => existsSync(whisperDir(z))));
|
|
291
|
+
}
|
|
292
|
+
async function dropRuntimeIfUnused(keepRuntime) {
|
|
293
|
+
if (!keepRuntime && !(await anyModelInstalled()))
|
|
294
|
+
await rm(runtimeRoot(), { recursive: true, force: true });
|
|
249
295
|
}
|
|
250
|
-
/** Removes the language's bundle(s); removes the runtime too once
|
|
296
|
+
/** Removes the language's bundle(s); removes the runtime too once nothing else needs it,
|
|
251
297
|
* unless `keepRuntime` (the native binding is loaded in this process: Windows refuses
|
|
252
298
|
* to delete it, and a half-deleted runtime would break reinstalling). */
|
|
253
299
|
export async function deleteBuiltinVoice(lang, opts = {}) {
|
|
254
300
|
await rm(dirname(bundleDir(lang)), { recursive: true, force: true });
|
|
255
|
-
|
|
256
|
-
|
|
301
|
+
await dropRuntimeIfUnused(opts.keepRuntime);
|
|
302
|
+
}
|
|
303
|
+
export async function deleteBuiltinStt(size, opts = {}) {
|
|
304
|
+
await rm(dirname(whisperDir(size)), { recursive: true, force: true });
|
|
305
|
+
await dropRuntimeIfUnused(opts.keepRuntime);
|
|
257
306
|
}
|
|
258
307
|
export async function builtinVoiceStatus() {
|
|
259
308
|
const defaults = {};
|
|
@@ -269,6 +318,11 @@ export async function builtinVoiceStatus() {
|
|
|
269
318
|
installed: existsSync(bundleDir(id)),
|
|
270
319
|
bytes: s.files.reduce((n, f) => n + f.bytes, 0),
|
|
271
320
|
})),
|
|
321
|
+
stt: Object.keys(cat.whisper).map((id) => ({
|
|
322
|
+
id,
|
|
323
|
+
installed: existsSync(whisperDir(id)),
|
|
324
|
+
bytes: cat.whisper[id].files.reduce((n, f) => n + f.bytes, 0),
|
|
325
|
+
})),
|
|
272
326
|
voices: [...POCKET_VOICES],
|
|
273
327
|
defaults,
|
|
274
328
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pronunciation.d.ts","sourceRoot":"","sources":["../../src/voice/pronunciation.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAGvE;;4BAE4B;AAC5B,eAAO,MAAM,qBAAqB,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,SAAS,kBAAkB,EAAE,CAAC,
|
|
1
|
+
{"version":3,"file":"pronunciation.d.ts","sourceRoot":"","sources":["../../src/voice/pronunciation.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAGvE;;4BAE4B;AAC5B,eAAO,MAAM,qBAAqB,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,SAAS,kBAAkB,EAAE,CAAC,CAezF,CAAC;AAEF;4EAC4E;AAC5E,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,WAAW,GAAG,MAAM,GAAG,IAAI,CAMnE;AAED;6CAC6C;AAC7C,wBAAgB,aAAa,CAAC,KAAK,EAAE,WAAW,EAAE,IAAI,EAAE,MAAM,GAAG,SAAS,kBAAkB,EAAE,CAE7F;AAID;;0CAE0C;AAC1C,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,SAAS,kBAAkB,EAAE,GAAG,MAAM,CAa/F"}
|
|
@@ -13,6 +13,9 @@ export const DEFAULT_PRONUNCIATION = {
|
|
|
13
13
|
{ term: 'build', say: 'bìld' },
|
|
14
14
|
{ term: 'review', say: 'riviù' },
|
|
15
15
|
{ term: 'stage', say: 'stèij' },
|
|
16
|
+
{ term: 'file', say: 'fàil' },
|
|
17
|
+
{ term: 'ok', say: 'okèi' },
|
|
18
|
+
{ term: 'buffer', say: 'bàffer' },
|
|
16
19
|
],
|
|
17
20
|
};
|
|
18
21
|
/** ISO code of the language being synthesized, or null when the server cannot
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export type WhisperSize = 'tiny' | 'base' | 'small';
|
|
2
|
+
export declare const WHISPER_BASE_URL = "https://huggingface.co";
|
|
3
|
+
export type WhisperFile = {
|
|
4
|
+
name: string;
|
|
5
|
+
sha256: string;
|
|
6
|
+
bytes: number;
|
|
7
|
+
};
|
|
8
|
+
export declare const WHISPER_MODELS: Readonly<Record<WhisperSize, {
|
|
9
|
+
repo: string;
|
|
10
|
+
revision: string;
|
|
11
|
+
files: readonly WhisperFile[];
|
|
12
|
+
}>>;
|
|
13
|
+
export declare const DEFAULT_WHISPER_SIZE: WhisperSize;
|
|
14
|
+
export declare const WHISPER_SAMPLE_RATE = 16000;
|
|
15
|
+
//# sourceMappingURL=catalog.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"catalog.d.ts","sourceRoot":"","sources":["../../../src/voice/whisper/catalog.ts"],"names":[],"mappings":"AAQA,MAAM,MAAM,WAAW,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC;AAGpD,eAAO,MAAM,gBAAgB,2BAA2B,CAAC;AAEzD,MAAM,MAAM,WAAW,GAAG;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,CAAC;AAE1E,eAAO,MAAM,cAAc,EAAE,QAAQ,CACnC,MAAM,CAAC,WAAW,EAAE;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,SAAS,WAAW,EAAE,CAAA;CAAE,CAAC,CAyCvF,CAAC;AAEF,eAAO,MAAM,oBAAoB,EAAE,WAAoB,CAAC;AAGxD,eAAO,MAAM,mBAAmB,QAAQ,CAAC"}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
// Pinned catalog for the built-in Whisper transcription engine: ONNX exports on Hugging Face.
|
|
2
|
+
//
|
|
3
|
+
// Weights: OpenAI Whisper, MIT licence. ONNX export: onnx-community (transformers.js
|
|
4
|
+
// layout). Each size is pinned to one commit, with sha256 and size per file; the two
|
|
5
|
+
// graphs are the quantized encoder and the q4 merged decoder (the int8 decoder was
|
|
6
|
+
// measured 2.5x slower per token on a CPU without VNNI). `name` keeps the repo path;
|
|
7
|
+
// the installer decides how to lay the files out on disk.
|
|
8
|
+
// Files live at <base>/onnx-community/whisper-<size>/resolve/<revision>/<name>.
|
|
9
|
+
export const WHISPER_BASE_URL = 'https://huggingface.co';
|
|
10
|
+
export const WHISPER_MODELS = {
|
|
11
|
+
tiny: {
|
|
12
|
+
repo: 'onnx-community/whisper-tiny',
|
|
13
|
+
revision: 'ff4177021cc41f7db950912b73ea4fdf7d01d8e7',
|
|
14
|
+
files: [
|
|
15
|
+
{ name: 'onnx/encoder_model_quantized.onnx', sha256: '2af4a414ca47aa30f61246017e5fe82b0a8d229281d1255ba666a2a7f6b84d19', bytes: 10124990 },
|
|
16
|
+
{ name: 'onnx/decoder_model_merged_q4.onnx', sha256: 'a7573efde84f7d018d59e50e7f860c03e26623d68e23eb6037b76135367e72eb', bytes: 86713702 },
|
|
17
|
+
{ name: 'vocab.json', sha256: '50d6a919f0a0601d56a04eb583c780d18553aa388254ba3158eb6a00f13e2c1a', bytes: 1036584 },
|
|
18
|
+
{ name: 'added_tokens.json', sha256: '9715fd2243b6f06a5858b5e32950d2853f73dd5bc201aafcf76f5082a2d8acd1', bytes: 34604 },
|
|
19
|
+
{ name: 'generation_config.json', sha256: 'f5c67e5a4f7102f8cb4d058bc95da276bbc19eeec997267c3bb0f25ef68facd1', bytes: 3772 },
|
|
20
|
+
{ name: 'preprocessor_config.json', sha256: 'a6a76d28c93edb273669eb9e0b0636a2bddbb1272c3261e47b7ca6dfdbac1b8d', bytes: 339 },
|
|
21
|
+
{ name: 'config.json', sha256: '46aeea0a406afbeb563fc8e59ca10609203df4299af6a83f73752fef369efd2d', bytes: 2243 },
|
|
22
|
+
],
|
|
23
|
+
},
|
|
24
|
+
base: {
|
|
25
|
+
repo: 'onnx-community/whisper-base',
|
|
26
|
+
revision: '27a07b4b52b3a4c5886134bb4320e93e1a4bbc7c',
|
|
27
|
+
files: [
|
|
28
|
+
{ name: 'onnx/encoder_model_quantized.onnx', sha256: '5862993336bf33acd23736071aae2b32261d3b1b2f37780194460d4ef974dd46', bytes: 23201314 },
|
|
29
|
+
{ name: 'onnx/decoder_model_merged_q4.onnx', sha256: '09f83b71ceedc97dab1d90b914715dc532a646a147abda11d83c64867b7c319c', bytes: 123602419 },
|
|
30
|
+
{ name: 'vocab.json', sha256: '50d6a919f0a0601d56a04eb583c780d18553aa388254ba3158eb6a00f13e2c1a', bytes: 1036584 },
|
|
31
|
+
{ name: 'added_tokens.json', sha256: '9715fd2243b6f06a5858b5e32950d2853f73dd5bc201aafcf76f5082a2d8acd1', bytes: 34604 },
|
|
32
|
+
{ name: 'generation_config.json', sha256: '61070cf8de25b1e9256e8e102ded49d8d24a8369ed36ef84fdf21549e68125a0', bytes: 3832 },
|
|
33
|
+
{ name: 'preprocessor_config.json', sha256: 'a6a76d28c93edb273669eb9e0b0636a2bddbb1272c3261e47b7ca6dfdbac1b8d', bytes: 339 },
|
|
34
|
+
{ name: 'config.json', sha256: 'f4d0608f7d918166da7edb3e188de5ef1bfe70d9802e785d271fd88111e9cf4b', bytes: 2243 },
|
|
35
|
+
],
|
|
36
|
+
},
|
|
37
|
+
small: {
|
|
38
|
+
repo: 'onnx-community/whisper-small',
|
|
39
|
+
revision: '36050c46d777d46dc4b5f43f6d90574fc38f8732',
|
|
40
|
+
files: [
|
|
41
|
+
{ name: 'onnx/encoder_model_quantized.onnx', sha256: 'a43a83f3c5361cd591cfa7c36f14b43cf7cb22f47a415cc14a8d557be800fa92', bytes: 92326160 },
|
|
42
|
+
{ name: 'onnx/decoder_model_merged_q4.onnx', sha256: '795c61b344576719c2b249cc9911c78b4cc552c8c6a5407be21a3380a0791b13', bytes: 233149327 },
|
|
43
|
+
{ name: 'vocab.json', sha256: '50d6a919f0a0601d56a04eb583c780d18553aa388254ba3158eb6a00f13e2c1a', bytes: 1036584 },
|
|
44
|
+
{ name: 'added_tokens.json', sha256: '9715fd2243b6f06a5858b5e32950d2853f73dd5bc201aafcf76f5082a2d8acd1', bytes: 34604 },
|
|
45
|
+
{ name: 'generation_config.json', sha256: 'f538b28220c6a6d6f1af1458d4141cacb4ef4963df3de98a19490440c412ddf0', bytes: 3893 },
|
|
46
|
+
{ name: 'preprocessor_config.json', sha256: 'a6a76d28c93edb273669eb9e0b0636a2bddbb1272c3261e47b7ca6dfdbac1b8d', bytes: 339 },
|
|
47
|
+
{ name: 'config.json', sha256: '457854d452f17661e197d74aee12b8e74fb75ba30ebfaa7426d0d61ea1e08a18', bytes: 2227 },
|
|
48
|
+
],
|
|
49
|
+
},
|
|
50
|
+
};
|
|
51
|
+
export const DEFAULT_WHISPER_SIZE = 'base';
|
|
52
|
+
// Whisper's feature extractor and encoder are fixed at 16 kHz mono.
|
|
53
|
+
export const WHISPER_SAMPLE_RATE = 16000;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"decoder.d.ts","sourceRoot":"","sources":["../../../src/voice/whisper/decoder.ts"],"names":[],"mappings":"AAuBA,wBAAgB,UAAU,CACxB,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,EAC7B,OAAO,GAAE,QAAQ,CAAC,MAAM,CAAM,GAC7B,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAKrB;AAID,wBAAgB,UAAU,CAAC,GAAG,EAAE,MAAM,EAAE,EAAE,KAAK,EAAE,WAAW,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,MAAM,CAWpF"}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
// Whisper's tokenizer is GPT-2's byte-level BPE: every vocabulary entry is a string of
|
|
2
|
+
// stand-in characters, one per byte. Decoding only needs the reverse map; encoding is
|
|
3
|
+
// never needed (the prompt is special tokens only).
|
|
4
|
+
// GPT-2 bytes_to_unicode, reversed: stand-in character -> byte.
|
|
5
|
+
const BYTE_OF = (() => {
|
|
6
|
+
const bytes = [];
|
|
7
|
+
for (let b = 33; b <= 126; b++)
|
|
8
|
+
bytes.push(b);
|
|
9
|
+
for (let b = 161; b <= 172; b++)
|
|
10
|
+
bytes.push(b);
|
|
11
|
+
for (let b = 174; b <= 255; b++)
|
|
12
|
+
bytes.push(b);
|
|
13
|
+
const chars = bytes.slice();
|
|
14
|
+
let n = 0;
|
|
15
|
+
for (let b = 0; b < 256; b++) {
|
|
16
|
+
if (!bytes.includes(b)) {
|
|
17
|
+
bytes.push(b);
|
|
18
|
+
chars.push(256 + n++);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
return new Map(bytes.map((b, i) => [String.fromCharCode(chars[i]), b]));
|
|
22
|
+
})();
|
|
23
|
+
// vocab.json (token -> id) to id -> token, leaving out the special tokens: they carry no
|
|
24
|
+
// text, so decoding skips them.
|
|
25
|
+
export function parseVocab(vocab, special = []) {
|
|
26
|
+
const skip = new Set(special);
|
|
27
|
+
const out = new Map();
|
|
28
|
+
for (const [token, id] of Object.entries(vocab))
|
|
29
|
+
if (!skip.has(id))
|
|
30
|
+
out.set(id, token);
|
|
31
|
+
return out;
|
|
32
|
+
}
|
|
33
|
+
// Ids missing from the vocabulary (special tokens) are skipped. A multi-byte character
|
|
34
|
+
// split across tokens is reassembled because all bytes are joined before UTF-8 decoding.
|
|
35
|
+
export function byteDecode(ids, vocab) {
|
|
36
|
+
const bytes = [];
|
|
37
|
+
for (const id of ids) {
|
|
38
|
+
const token = vocab.get(id);
|
|
39
|
+
if (token === undefined)
|
|
40
|
+
continue;
|
|
41
|
+
for (const ch of token) {
|
|
42
|
+
const b = BYTE_OF.get(ch);
|
|
43
|
+
if (b !== undefined)
|
|
44
|
+
bytes.push(b);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return Buffer.from(bytes).toString('utf8');
|
|
48
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { BuiltinTranscriber } from '../../cli/web/voice_proxy.js';
|
|
2
|
+
import { type WhisperSize } from './catalog.js';
|
|
3
|
+
import { type WhisperModel } from './inference.js';
|
|
4
|
+
export type WhisperLoader = (size: WhisperSize) => Promise<WhisperModel>;
|
|
5
|
+
export declare function setWhisperLoaderForTest(l: WhisperLoader | null): void;
|
|
6
|
+
export declare function releaseWhisperModels(): Promise<void>;
|
|
7
|
+
export declare const transcribeBuiltin: BuiltinTranscriber;
|
|
8
|
+
//# sourceMappingURL=engine.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"engine.d.ts","sourceRoot":"","sources":["../../../src/voice/whisper/engine.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,8BAA8B,CAAC;AAGvE,OAAO,EAA6D,KAAK,WAAW,EAAE,MAAM,cAAc,CAAC;AAC3G,OAAO,EAAoC,KAAK,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAErF,MAAM,MAAM,aAAa,GAAG,CAAC,IAAI,EAAE,WAAW,KAAK,OAAO,CAAC,YAAY,CAAC,CAAC;AASzE,wBAAgB,uBAAuB,CAAC,CAAC,EAAE,aAAa,GAAG,IAAI,GAAG,IAAI,CAErE;AAqBD,wBAAsB,oBAAoB,IAAI,OAAO,CAAC,IAAI,CAAC,CAI1D;AAOD,eAAO,MAAM,iBAAiB,EAAE,kBA6B/B,CAAC"}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
// The built-in transcription engine: one warm Whisper model at a time, calls
|
|
2
|
+
// serialized, audio over 30 s split into windows and joined. Shares the ONNX runtime
|
|
3
|
+
// the built-in voice downloads (and loads) — see voice/pocket/engine.ts.
|
|
4
|
+
import { existsSync } from 'node:fs';
|
|
5
|
+
import os from 'node:os';
|
|
6
|
+
import { loadOnnxRuntime } from '../pocket/engine.js';
|
|
7
|
+
import { runtimeEntry, whisperDir } from '../pocket/install.js';
|
|
8
|
+
import { DEFAULT_WHISPER_SIZE, WHISPER_MODELS, WHISPER_SAMPLE_RATE } from './catalog.js';
|
|
9
|
+
import { createWhisperModel, splitWindows } from './inference.js';
|
|
10
|
+
// Physical cores: the encoder scales with threads (6 beat 4 on a 6-core Zen 1),
|
|
11
|
+
// the decoder does not care.
|
|
12
|
+
const threads = () => Math.max(1, Math.floor(os.availableParallelism() / 2));
|
|
13
|
+
const defaultLoader = async (size) => createWhisperModel(loadOnnxRuntime(), whisperDir(size), { threads: threads() });
|
|
14
|
+
let loader = defaultLoader;
|
|
15
|
+
export function setWhisperLoaderForTest(l) {
|
|
16
|
+
loader = l ?? defaultLoader;
|
|
17
|
+
}
|
|
18
|
+
let slot = null;
|
|
19
|
+
function closeSlot(s) {
|
|
20
|
+
return s.tail
|
|
21
|
+
.then(() => s.model)
|
|
22
|
+
.then((m) => m.close())
|
|
23
|
+
.catch(() => { });
|
|
24
|
+
}
|
|
25
|
+
function slotFor(size) {
|
|
26
|
+
if (slot?.size === size)
|
|
27
|
+
return slot;
|
|
28
|
+
if (slot)
|
|
29
|
+
void closeSlot(slot);
|
|
30
|
+
const s = { size, model: loader(size), tail: Promise.resolve() };
|
|
31
|
+
s.model.catch(() => slot === s && (slot = null));
|
|
32
|
+
slot = s;
|
|
33
|
+
return s;
|
|
34
|
+
}
|
|
35
|
+
export async function releaseWhisperModels() {
|
|
36
|
+
const s = slot;
|
|
37
|
+
slot = null;
|
|
38
|
+
if (s)
|
|
39
|
+
await closeSlot(s);
|
|
40
|
+
}
|
|
41
|
+
const text = (status, body) => new Response(body, { status });
|
|
42
|
+
/** Primary subtag of the configured language, or null to let Whisper detect it. */
|
|
43
|
+
const languageOf = (voice) => voice.lang?.split(/[-_]/)[0]?.trim().toLowerCase() || null;
|
|
44
|
+
export const transcribeBuiltin = async (voice, audio, signal) => {
|
|
45
|
+
const size = voice.sttBuiltinModel ?? DEFAULT_WHISPER_SIZE;
|
|
46
|
+
if (!Object.hasOwn(WHISPER_MODELS, size))
|
|
47
|
+
return text(400, `Unknown built-in transcription model: ${size}.`);
|
|
48
|
+
if (!existsSync(runtimeEntry()))
|
|
49
|
+
return text(409, 'The built-in voice runtime is not installed. Install a transcription model in Settings → Voice.');
|
|
50
|
+
if (!existsSync(whisperDir(size)))
|
|
51
|
+
return text(409, `The ${size} transcription model is not installed. Install it in Settings → Voice.`);
|
|
52
|
+
// Take a turn on the model: one transcription at a time, in arrival order.
|
|
53
|
+
const s = slotFor(size);
|
|
54
|
+
let release;
|
|
55
|
+
const prev = s.tail;
|
|
56
|
+
s.tail = new Promise((r) => (release = r));
|
|
57
|
+
await prev;
|
|
58
|
+
try {
|
|
59
|
+
const model = await s.model;
|
|
60
|
+
const lang = languageOf(voice);
|
|
61
|
+
const parts = [];
|
|
62
|
+
for (const window of splitWindows(audio, WHISPER_SAMPLE_RATE)) {
|
|
63
|
+
if (signal.aborted)
|
|
64
|
+
return new Response(null, { status: 204 });
|
|
65
|
+
const t = (await model.transcribe(window, lang, signal)).trim();
|
|
66
|
+
if (t)
|
|
67
|
+
parts.push(t);
|
|
68
|
+
}
|
|
69
|
+
return Response.json({ text: parts.join(' ') });
|
|
70
|
+
}
|
|
71
|
+
catch (e) {
|
|
72
|
+
return text(500, e instanceof Error ? e.message : String(e));
|
|
73
|
+
}
|
|
74
|
+
finally {
|
|
75
|
+
release();
|
|
76
|
+
}
|
|
77
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export declare const SAMPLE_RATE = 16000;
|
|
2
|
+
export declare const N_FFT = 400;
|
|
3
|
+
export declare const HOP_LENGTH = 160;
|
|
4
|
+
export declare const N_MELS = 80;
|
|
5
|
+
export declare const N_SAMPLES: number;
|
|
6
|
+
export declare const N_FRAMES: number;
|
|
7
|
+
export declare function makeFFT(n: number): (inR: Float64Array, inI: Float64Array, outR: Float64Array, outI: Float64Array) => void;
|
|
8
|
+
export declare function melFilters(nMels?: number, nFft?: number, sampleRate?: number): Float64Array;
|
|
9
|
+
export declare function logMel(samples: Float32Array): Float32Array;
|
|
10
|
+
//# sourceMappingURL=features.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"features.d.ts","sourceRoot":"","sources":["../../../src/voice/whisper/features.ts"],"names":[],"mappings":"AAMA,eAAO,MAAM,WAAW,QAAQ,CAAC;AACjC,eAAO,MAAM,KAAK,MAAM,CAAC;AACzB,eAAO,MAAM,UAAU,MAAM,CAAC;AAC9B,eAAO,MAAM,MAAM,KAAK,CAAC;AACzB,eAAO,MAAM,SAAS,QAAmB,CAAC;AAC1C,eAAO,MAAM,QAAQ,QAAyB,CAAC;AAO/C,wBAAgB,OAAO,CACrB,CAAC,EAAE,MAAM,GACR,CAAC,GAAG,EAAE,YAAY,EAAE,GAAG,EAAE,YAAY,EAAE,IAAI,EAAE,YAAY,EAAE,IAAI,EAAE,YAAY,KAAK,IAAI,CA+DxF;AASD,wBAAgB,UAAU,CAAC,KAAK,SAAS,EAAE,IAAI,SAAQ,EAAE,UAAU,SAAc,GAAG,YAAY,CAkB/F;AAiBD,wBAAgB,MAAM,CAAC,OAAO,EAAE,YAAY,GAAG,YAAY,CAyC1D"}
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
// Whisper log-mel features with the semantics of HF WhisperFeatureExtractor (measured to
|
|
2
|
+
// match faster-whisper's extractor to 3.8e-6): 16 kHz audio padded to 30 s, STFT with
|
|
3
|
+
// n_fft 400 / hop 160 / periodic Hann / reflect padding, power spectrum, 80 Slaney mel
|
|
4
|
+
// bins (the ONNX export ships no filterbank, so it is computed here), log10, clamp to
|
|
5
|
+
// max - 8, (x + 4) / 4.
|
|
6
|
+
export const SAMPLE_RATE = 16000;
|
|
7
|
+
export const N_FFT = 400;
|
|
8
|
+
export const HOP_LENGTH = 160;
|
|
9
|
+
export const N_MELS = 80;
|
|
10
|
+
export const N_SAMPLES = 30 * SAMPLE_RATE; // the encoder's fixed 30 s window
|
|
11
|
+
export const N_FRAMES = N_SAMPLES / HOP_LENGTH;
|
|
12
|
+
const N_BINS = N_FFT / 2 + 1;
|
|
13
|
+
const LOG_FLOOR = -10; // log10(1e-10), the value of an all-zero frame
|
|
14
|
+
// Mixed-radix FFT for any length with small factors (400 = 4 * 4 * 5 * 5). Writes the
|
|
15
|
+
// complex spectrum of (inR, inI) into (outR, outI).
|
|
16
|
+
export function makeFFT(n) {
|
|
17
|
+
const factors = [];
|
|
18
|
+
let m = n;
|
|
19
|
+
for (const p of [4, 2, 3, 5, 7]) {
|
|
20
|
+
while (m % p === 0) {
|
|
21
|
+
factors.push(p);
|
|
22
|
+
m /= p;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
if (m !== 1)
|
|
26
|
+
factors.push(m);
|
|
27
|
+
const cos = new Float64Array(n);
|
|
28
|
+
const sin = new Float64Array(n);
|
|
29
|
+
for (let i = 0; i < n; i++) {
|
|
30
|
+
cos[i] = Math.cos((-2 * Math.PI * i) / n);
|
|
31
|
+
sin[i] = Math.sin((-2 * Math.PI * i) / n);
|
|
32
|
+
}
|
|
33
|
+
const maxP = Math.max(1, ...factors);
|
|
34
|
+
const tr = new Float64Array(maxP);
|
|
35
|
+
const ti = new Float64Array(maxP);
|
|
36
|
+
const rec = (inR, inI, inOff, stride, len, oR, oI, oOff, fi) => {
|
|
37
|
+
if (len === 1) {
|
|
38
|
+
oR[oOff] = inR[inOff];
|
|
39
|
+
oI[oOff] = inI[inOff];
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
const p = factors[fi];
|
|
43
|
+
const sub = len / p;
|
|
44
|
+
const tw = n / len;
|
|
45
|
+
for (let q = 0; q < p; q++)
|
|
46
|
+
rec(inR, inI, inOff + q * stride, stride * p, sub, oR, oI, oOff + q * sub, fi + 1);
|
|
47
|
+
for (let k = 0; k < sub; k++) {
|
|
48
|
+
for (let q = 0; q < p; q++) {
|
|
49
|
+
tr[q] = oR[oOff + q * sub + k];
|
|
50
|
+
ti[q] = oI[oOff + q * sub + k];
|
|
51
|
+
}
|
|
52
|
+
for (let s = 0; s < p; s++) {
|
|
53
|
+
const kk = k + s * sub;
|
|
54
|
+
let ar = 0;
|
|
55
|
+
let ai = 0;
|
|
56
|
+
for (let q = 0; q < p; q++) {
|
|
57
|
+
const idx = ((q * kk) % len) * tw;
|
|
58
|
+
const c = cos[idx];
|
|
59
|
+
const d = sin[idx];
|
|
60
|
+
ar += tr[q] * c - ti[q] * d;
|
|
61
|
+
ai += tr[q] * d + ti[q] * c;
|
|
62
|
+
}
|
|
63
|
+
oR[oOff + kk] = ar;
|
|
64
|
+
oI[oOff + kk] = ai;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
return (inR, inI, outR, outI) => rec(inR, inI, 0, 1, n, outR, outI, 0, 0);
|
|
69
|
+
}
|
|
70
|
+
// Slaney mel scale and area-normalized triangles (librosa.filters.mel(htk=False,
|
|
71
|
+
// norm='slaney'), as HF WhisperFeatureExtractor). Row-major [nMels][nFft / 2 + 1].
|
|
72
|
+
const hzToMel = (f) => f < 1000 ? (3 * f) / 200 : 15 + (Math.log(f / 1000) * 27) / Math.log(6.4);
|
|
73
|
+
const melToHz = (m) => m < 15 ? (200 * m) / 3 : 1000 * Math.exp((Math.log(6.4) / 27) * (m - 15));
|
|
74
|
+
export function melFilters(nMels = N_MELS, nFft = N_FFT, sampleRate = SAMPLE_RATE) {
|
|
75
|
+
const nBins = nFft / 2 + 1;
|
|
76
|
+
const mMin = hzToMel(0);
|
|
77
|
+
const mMax = hzToMel(sampleRate / 2);
|
|
78
|
+
const pts = Array.from({ length: nMels + 2 }, (_, i) => melToHz(mMin + ((mMax - mMin) * i) / (nMels + 1)));
|
|
79
|
+
const fb = new Float64Array(nMels * nBins);
|
|
80
|
+
for (let m = 0; m < nMels; m++) {
|
|
81
|
+
const enorm = 2 / (pts[m + 2] - pts[m]);
|
|
82
|
+
for (let k = 0; k < nBins; k++) {
|
|
83
|
+
const f = (k * sampleRate) / nFft;
|
|
84
|
+
const down = (f - pts[m]) / (pts[m + 1] - pts[m]);
|
|
85
|
+
const up = (pts[m + 2] - f) / (pts[m + 2] - pts[m + 1]);
|
|
86
|
+
fb[m * nBins + k] = Math.max(0, Math.min(down, up)) * enorm;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
return fb;
|
|
90
|
+
}
|
|
91
|
+
let tables = null;
|
|
92
|
+
function getTables() {
|
|
93
|
+
tables ??= {
|
|
94
|
+
fft: makeFFT(N_FFT),
|
|
95
|
+
fb: melFilters(),
|
|
96
|
+
hann: Float64Array.from({ length: N_FFT }, (_, i) => 0.5 - 0.5 * Math.cos((2 * Math.PI * i) / N_FFT)),
|
|
97
|
+
};
|
|
98
|
+
return tables;
|
|
99
|
+
}
|
|
100
|
+
// [N_MELS * N_FRAMES], row-major by mel bin. Input is 16 kHz mono; anything past 30 s is
|
|
101
|
+
// ignored (callers split longer audio first).
|
|
102
|
+
export function logMel(samples) {
|
|
103
|
+
const { fft, fb, hann } = getTables();
|
|
104
|
+
const len = Math.min(samples.length, N_SAMPLES);
|
|
105
|
+
const x = new Float64Array(N_SAMPLES);
|
|
106
|
+
x.set(samples.subarray(0, len));
|
|
107
|
+
const pad = N_FFT / 2;
|
|
108
|
+
const mel = new Float32Array(N_MELS * N_FRAMES).fill(LOG_FLOOR);
|
|
109
|
+
const fr = new Float64Array(N_FFT);
|
|
110
|
+
const fi = new Float64Array(N_FFT);
|
|
111
|
+
const oR = new Float64Array(N_FFT);
|
|
112
|
+
const oI = new Float64Array(N_FFT);
|
|
113
|
+
const power = new Float64Array(N_BINS);
|
|
114
|
+
let max = -Infinity;
|
|
115
|
+
for (let t = 0; t < N_FRAMES; t++) {
|
|
116
|
+
const start = t * HOP_LENGTH - pad;
|
|
117
|
+
if (start >= len) {
|
|
118
|
+
// The frame lies entirely in the zero padding.
|
|
119
|
+
max = Math.max(max, LOG_FLOOR);
|
|
120
|
+
continue;
|
|
121
|
+
}
|
|
122
|
+
for (let i = 0; i < N_FFT; i++) {
|
|
123
|
+
let idx = start + i;
|
|
124
|
+
if (idx < 0)
|
|
125
|
+
idx = -idx;
|
|
126
|
+
else if (idx >= N_SAMPLES)
|
|
127
|
+
idx = 2 * (N_SAMPLES - 1) - idx; // reflect padding
|
|
128
|
+
fr[i] = x[idx] * hann[i];
|
|
129
|
+
fi[i] = 0;
|
|
130
|
+
}
|
|
131
|
+
fft(fr, fi, oR, oI);
|
|
132
|
+
for (let k = 0; k < N_BINS; k++)
|
|
133
|
+
power[k] = oR[k] * oR[k] + oI[k] * oI[k];
|
|
134
|
+
for (let m = 0; m < N_MELS; m++) {
|
|
135
|
+
let s = 0;
|
|
136
|
+
const row = m * N_BINS;
|
|
137
|
+
for (let k = 0; k < N_BINS; k++)
|
|
138
|
+
s += fb[row + k] * power[k];
|
|
139
|
+
const v = Math.log10(Math.max(s, 1e-10));
|
|
140
|
+
mel[m * N_FRAMES + t] = v;
|
|
141
|
+
if (v > max)
|
|
142
|
+
max = v;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
const floor = max - 8;
|
|
146
|
+
for (let i = 0; i < mel.length; i++)
|
|
147
|
+
mel[i] = (Math.max(mel[i], floor) + 4) / 4;
|
|
148
|
+
return mel;
|
|
149
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { OrtModule } from '../pocket/inference.js';
|
|
2
|
+
export interface WhisperModel {
|
|
3
|
+
transcribe(audio: Float32Array, lang: string | null, signal: AbortSignal): Promise<string>;
|
|
4
|
+
close(): Promise<void>;
|
|
5
|
+
}
|
|
6
|
+
export declare const WHISPER_GRAPHS: {
|
|
7
|
+
readonly encoder: "encoder_model_quantized.onnx";
|
|
8
|
+
readonly decoder: "decoder_model_merged_q4.onnx";
|
|
9
|
+
};
|
|
10
|
+
export declare const WHISPER_FILES: readonly ["config.json", "generation_config.json", "added_tokens.json", "vocab.json"];
|
|
11
|
+
export declare const MAX_NEW_TOKENS = 224;
|
|
12
|
+
export declare function modelFile(dir: string, name: string): string;
|
|
13
|
+
export declare function createWhisperModel(ort: OrtModule, dir: string, opts: {
|
|
14
|
+
threads: number;
|
|
15
|
+
}): Promise<WhisperModel>;
|
|
16
|
+
export declare function splitWindows(audio: Float32Array, rate?: number): Float32Array[];
|
|
17
|
+
//# sourceMappingURL=inference.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"inference.d.ts","sourceRoot":"","sources":["../../../src/voice/whisper/inference.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,SAAS,EAAyB,MAAM,wBAAwB,CAAC;AAI/E,MAAM,WAAW,YAAY;IAG3B,UAAU,CAAC,KAAK,EAAE,YAAY,EAAE,IAAI,EAAE,MAAM,GAAG,IAAI,EAAE,MAAM,EAAE,WAAW,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;IAC3F,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CACxB;AAID,eAAO,MAAM,cAAc;;;CAGjB,CAAC;AACX,eAAO,MAAM,aAAa,uFAKhB,CAAC;AAGX,eAAO,MAAM,cAAc,MAAM,CAAC;AAWlC,wBAAgB,SAAS,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,CAG3D;AAkBD,wBAAsB,kBAAkB,CACtC,GAAG,EAAE,SAAS,EACd,GAAG,EAAE,MAAM,EACX,IAAI,EAAE;IAAE,OAAO,EAAE,MAAM,CAAA;CAAE,GACxB,OAAO,CAAC,YAAY,CAAC,CA6BvB;AAuKD,wBAAgB,YAAY,CAAC,KAAK,EAAE,YAAY,EAAE,IAAI,GAAE,MAAoB,GAAG,YAAY,EAAE,CA2B5F"}
|