@try-works/tool-mode-runtime-litert 0.0.0-stage → 0.1.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/lib/ear-frontend.d.ts +113 -0
- package/lib/ear-frontend.d.ts.map +1 -0
- package/lib/ear-frontend.js +258 -0
- package/lib/ear.d.ts +51 -0
- package/lib/ear.d.ts.map +1 -0
- package/lib/ear.js +296 -0
- package/lib/emo-tokenizer.d.ts +87 -0
- package/lib/emo-tokenizer.d.ts.map +1 -0
- package/lib/emo-tokenizer.js +365 -0
- package/lib/emo.d.ts +53 -0
- package/lib/emo.d.ts.map +1 -0
- package/lib/emo.js +218 -0
- package/lib/errors.d.ts +14 -0
- package/lib/errors.d.ts.map +1 -0
- package/lib/errors.js +29 -0
- package/lib/index.d.ts +28 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +34 -0
- package/lib/model.d.ts +103 -0
- package/lib/model.d.ts.map +1 -0
- package/lib/model.js +112 -0
- package/lib/moderator.d.ts +59 -0
- package/lib/moderator.d.ts.map +1 -0
- package/lib/moderator.js +189 -0
- package/lib/pillow.d.ts +75 -0
- package/lib/pillow.d.ts.map +1 -0
- package/lib/pillow.js +187 -0
- package/lib/png.d.ts +27 -0
- package/lib/png.d.ts.map +1 -0
- package/lib/png.js +176 -0
- package/lib/processor.d.ts +43 -0
- package/lib/processor.d.ts.map +1 -0
- package/lib/processor.js +48 -0
- package/lib/provider-factory.d.ts +44 -0
- package/lib/provider-factory.d.ts.map +1 -0
- package/lib/provider-factory.js +219 -0
- package/lib/runtime.d.ts +41 -0
- package/lib/runtime.d.ts.map +1 -0
- package/lib/runtime.js +66 -0
- package/lib/shapes.d.ts +28 -0
- package/lib/shapes.d.ts.map +1 -0
- package/lib/shapes.js +187 -0
- package/lib/shim.d.ts +45 -0
- package/lib/shim.d.ts.map +1 -0
- package/lib/shim.js +215 -0
- package/lib/wav.d.ts +39 -0
- package/lib/wav.d.ts.map +1 -0
- package/lib/wav.js +134 -0
- package/package.json +47 -4
- package/README.md +0 -3
package/lib/shim.js
ADDED
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The socket-free LiteRT.js boot shim.
|
|
3
|
+
*
|
|
4
|
+
* WHY THIS EXISTS. `@litertjs/core` loads its Emscripten build by injecting a <script> into a
|
|
5
|
+
* browser document and fetching the Wasm binary from a URL. In node it finds neither, so its
|
|
6
|
+
* loader fails unless a document, a `__dirname`, and a URL scheme that resolves to a local file
|
|
7
|
+
* are supplied. The proven spike
|
|
8
|
+
* (D:/DEV/dsh-model-as-tool/spike/runtime-probe/litert/litert-file-shim.mjs, 2026-11-14)
|
|
9
|
+
* showed that installing those globals plus a `fetch` interceptor for `litert://wasm/` is
|
|
10
|
+
* sufficient — no HTTP server and no copied Wasm.
|
|
11
|
+
*
|
|
12
|
+
* WHAT IT DOES: installs `self/document/module/exports/require/__dirname/__filename` and a
|
|
13
|
+
* `fetch` interceptor that maps `litert://wasm/<file>` to `path.join(wasmDir, <file>)`
|
|
14
|
+
* (application/wasm for .wasm, text/javascript for .js). It never opens a socket and never
|
|
15
|
+
* copies the Wasm files.
|
|
16
|
+
*
|
|
17
|
+
* WHAT IT DOES NOT DO: it does not load or compile a model (see model.ts), and it does not
|
|
18
|
+
* restore itself automatically — keep the handle and call `dispose()` only when the process is
|
|
19
|
+
* done with LiteRT, because the Wasm module is process-global.
|
|
20
|
+
*/
|
|
21
|
+
import { readFile } from 'node:fs/promises';
|
|
22
|
+
import { createRequire } from 'node:module';
|
|
23
|
+
import path from 'node:path';
|
|
24
|
+
import vm from 'node:vm';
|
|
25
|
+
import { Schema } from 'effect';
|
|
26
|
+
import { RuntimeUnavailable, describeCause } from "./errors.js";
|
|
27
|
+
/** The only URL scheme this shim intercepts. */
|
|
28
|
+
export const LITERT_WASM_PREFIX = 'litert://wasm/';
|
|
29
|
+
const SHIM_GLOBALS = [
|
|
30
|
+
'self',
|
|
31
|
+
'location',
|
|
32
|
+
'module',
|
|
33
|
+
'exports',
|
|
34
|
+
'require',
|
|
35
|
+
'__dirname',
|
|
36
|
+
'__filename',
|
|
37
|
+
'document',
|
|
38
|
+
];
|
|
39
|
+
// SAFETY: these optional globals exist only in a browser or after this shim installs them;
|
|
40
|
+
// node's lib types do not declare them, and no other code reads them through this alias.
|
|
41
|
+
const globalScope = globalThis;
|
|
42
|
+
let installed;
|
|
43
|
+
const CONTENT_TYPES = new Map([
|
|
44
|
+
['.wasm', 'application/wasm'],
|
|
45
|
+
['.js', 'text/javascript'],
|
|
46
|
+
]);
|
|
47
|
+
const createScriptElement = (tagName) => {
|
|
48
|
+
const listeners = {};
|
|
49
|
+
let source = '';
|
|
50
|
+
return {
|
|
51
|
+
tagName,
|
|
52
|
+
crossOrigin: 'anonymous',
|
|
53
|
+
get src() {
|
|
54
|
+
return source;
|
|
55
|
+
},
|
|
56
|
+
set src(value) {
|
|
57
|
+
source = value;
|
|
58
|
+
},
|
|
59
|
+
listeners,
|
|
60
|
+
addEventListener: (event, listener) => {
|
|
61
|
+
listeners[event] = listener;
|
|
62
|
+
},
|
|
63
|
+
};
|
|
64
|
+
};
|
|
65
|
+
const ScriptExports = Schema.Struct({
|
|
66
|
+
ModuleFactory: Schema.optional(Schema.Unknown),
|
|
67
|
+
default: Schema.optional(Schema.Unknown),
|
|
68
|
+
});
|
|
69
|
+
/**
|
|
70
|
+
* The minimal document the Emscripten loader expects: `createElement('script')`, an element
|
|
71
|
+
* whose `src` plus `load` listener resolves to evaluating the fetched script, and a body whose
|
|
72
|
+
* `appendChild` starts that load. The evaluated script's `module.exports` (or `default`)
|
|
73
|
+
* becomes `self.ModuleFactory`, as the loader expects.
|
|
74
|
+
*/
|
|
75
|
+
const createDocumentStub = () => {
|
|
76
|
+
const evaluateScript = async (element) => {
|
|
77
|
+
try {
|
|
78
|
+
const response = await globalThis.fetch(element.src);
|
|
79
|
+
if (!response.ok)
|
|
80
|
+
throw new Error(`script fetch failed with HTTP ${response.status}`);
|
|
81
|
+
const code = await response.text();
|
|
82
|
+
const moduleGlobal = { exports: {} };
|
|
83
|
+
globalScope.module = moduleGlobal;
|
|
84
|
+
globalScope.exports = moduleGlobal.exports;
|
|
85
|
+
vm.runInThisContext(code, { filename: element.src });
|
|
86
|
+
const exportsValue = moduleGlobal.exports;
|
|
87
|
+
const parsed = Schema.decodeUnknownOption(ScriptExports)(exportsValue);
|
|
88
|
+
globalScope.ModuleFactory =
|
|
89
|
+
parsed._tag === 'Some' ? (parsed.value.ModuleFactory ?? parsed.value.default ?? exportsValue) : exportsValue;
|
|
90
|
+
element.listeners['load']?.();
|
|
91
|
+
}
|
|
92
|
+
catch (error) {
|
|
93
|
+
element.listeners['error']?.(error instanceof Error ? error : new Error(describeCause(error)));
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
return {
|
|
97
|
+
currentScript: null,
|
|
98
|
+
createElement: (tagName) => createScriptElement(tagName),
|
|
99
|
+
body: {
|
|
100
|
+
appendChild: (element) => {
|
|
101
|
+
void evaluateScript(element);
|
|
102
|
+
},
|
|
103
|
+
},
|
|
104
|
+
head: { appendChild: () => { } },
|
|
105
|
+
};
|
|
106
|
+
};
|
|
107
|
+
const readUrl = (input) => {
|
|
108
|
+
if (Schema.is(Schema.String)(input))
|
|
109
|
+
return input;
|
|
110
|
+
return input instanceof URL ? input.href : input.url;
|
|
111
|
+
};
|
|
112
|
+
/**
|
|
113
|
+
* Intercepts `litert://wasm/<file>` and serves the bytes from `wasmDir`. Every other URL is
|
|
114
|
+
* forwarded to the real fetch, unchanged.
|
|
115
|
+
*/
|
|
116
|
+
const interceptLitertFetch = (wasmDir, realFetch) => {
|
|
117
|
+
const intercepted = async (input, init) => {
|
|
118
|
+
let url;
|
|
119
|
+
try {
|
|
120
|
+
url = readUrl(input);
|
|
121
|
+
}
|
|
122
|
+
catch {
|
|
123
|
+
return new Response(null, { status: 400 });
|
|
124
|
+
}
|
|
125
|
+
if (!url.startsWith(LITERT_WASM_PREFIX))
|
|
126
|
+
return realFetch(input, init);
|
|
127
|
+
let file;
|
|
128
|
+
try {
|
|
129
|
+
const relative = decodeURIComponent(new URL(url).pathname.replace(/^\/+/, ''));
|
|
130
|
+
file = path.resolve(wasmDir, relative);
|
|
131
|
+
}
|
|
132
|
+
catch {
|
|
133
|
+
return new Response(null, { status: 404 });
|
|
134
|
+
}
|
|
135
|
+
// A request may not escape the wasm directory.
|
|
136
|
+
if (file !== wasmDir && !file.startsWith(wasmDir + path.sep)) {
|
|
137
|
+
return new Response(null, { status: 404 });
|
|
138
|
+
}
|
|
139
|
+
try {
|
|
140
|
+
const bytes = await readFile(file);
|
|
141
|
+
const contentType = CONTENT_TYPES.get(path.extname(file).toLowerCase()) ?? 'application/octet-stream';
|
|
142
|
+
return new Response(new Uint8Array(bytes), {
|
|
143
|
+
status: 200,
|
|
144
|
+
headers: { 'content-type': contentType },
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
catch {
|
|
148
|
+
return new Response(null, { status: 404 });
|
|
149
|
+
}
|
|
150
|
+
};
|
|
151
|
+
return intercepted;
|
|
152
|
+
};
|
|
153
|
+
/**
|
|
154
|
+
* Installs the globals and fetch interceptor LiteRT.js needs in node.
|
|
155
|
+
*
|
|
156
|
+
* Idempotent for one wasm directory; refuses a second directory in the same process with
|
|
157
|
+
* `RuntimeUnavailable` because the Wasm module is process-global. Call `dispose()` to restore
|
|
158
|
+
* the globals and fetch.
|
|
159
|
+
*/
|
|
160
|
+
export const installLitertFetchShim = (options) => {
|
|
161
|
+
const wasmDir = path.resolve(options.wasmDir);
|
|
162
|
+
if (installed !== undefined) {
|
|
163
|
+
if (installed.wasmDir === wasmDir)
|
|
164
|
+
return installed.shim;
|
|
165
|
+
throw new RuntimeUnavailable({
|
|
166
|
+
provider: 'litert',
|
|
167
|
+
reason: `the LiteRT fetch shim is already installed for ${installed.wasmDir}; it cannot target ${wasmDir} in the same process`,
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
const realFetch = globalThis.fetch;
|
|
171
|
+
const previous = new Map();
|
|
172
|
+
for (const key of SHIM_GLOBALS) {
|
|
173
|
+
previous.set(key, { present: key in globalThis, value: globalScope[key] });
|
|
174
|
+
}
|
|
175
|
+
globalScope.self = globalThis;
|
|
176
|
+
globalScope.location = new URL(LITERT_WASM_PREFIX);
|
|
177
|
+
globalScope.module = { exports: {} };
|
|
178
|
+
globalScope.exports = globalScope.module.exports;
|
|
179
|
+
globalScope.require = createRequire(import.meta.url);
|
|
180
|
+
globalScope.__dirname = wasmDir;
|
|
181
|
+
globalScope.__filename = path.join(wasmDir, 'loader.mjs');
|
|
182
|
+
globalScope.document = createDocumentStub();
|
|
183
|
+
globalThis.fetch = interceptLitertFetch(wasmDir, realFetch);
|
|
184
|
+
let disposed = false;
|
|
185
|
+
const shim = {
|
|
186
|
+
wasmDir,
|
|
187
|
+
dispose: () => {
|
|
188
|
+
if (disposed)
|
|
189
|
+
return;
|
|
190
|
+
disposed = true;
|
|
191
|
+
globalThis.fetch = realFetch;
|
|
192
|
+
for (const [key, entry] of previous) {
|
|
193
|
+
if (entry.present)
|
|
194
|
+
Object.assign(globalThis, { [key]: entry.value });
|
|
195
|
+
else
|
|
196
|
+
delete globalScope[key];
|
|
197
|
+
}
|
|
198
|
+
installed = undefined;
|
|
199
|
+
},
|
|
200
|
+
};
|
|
201
|
+
installed = { wasmDir, shim };
|
|
202
|
+
return shim;
|
|
203
|
+
};
|
|
204
|
+
/**
|
|
205
|
+
* The directory of Emscripten files shipped inside the installed `@litertjs/core`.
|
|
206
|
+
*
|
|
207
|
+
* Resolution is relative to this module, so it works from the built `lib/` output in any host
|
|
208
|
+
* that installed the dependency (the package has no `exports` map restricting deep paths).
|
|
209
|
+
*/
|
|
210
|
+
export const resolveLitertWasmDir = () => {
|
|
211
|
+
const require = createRequire(import.meta.url);
|
|
212
|
+
const packageJson = require.resolve('@litertjs/core/package.json');
|
|
213
|
+
return path.join(path.dirname(packageJson), 'wasm');
|
|
214
|
+
};
|
|
215
|
+
//# sourceMappingURL=shim.js.map
|
package/lib/wav.d.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WAV decoding and linear resampling for the ear tool.
|
|
3
|
+
*
|
|
4
|
+
* The same contract as the Phase 3 waveform processor (runtime-onnx/src/wav.ts): PCM 16-bit
|
|
5
|
+
* or IEEE float32, mono or stereo, any declared rate. It is duplicated here deliberately so
|
|
6
|
+
* the LiteRT deployment pulls in no ONNX dependency; the behaviour is identical.
|
|
7
|
+
*
|
|
8
|
+
* Every unsupported document is refused BY NAME rather than guessed at, because a
|
|
9
|
+
* mis-decoded waveform is a wrong language with no other symptom.
|
|
10
|
+
*/
|
|
11
|
+
/** The rate the ear model expects. */
|
|
12
|
+
export declare const EAR_SAMPLE_RATE = 16000;
|
|
13
|
+
/** A decoded WAV: mono interleaved-averaged float samples at the SOURCE rate. */
|
|
14
|
+
export interface WavAudio {
|
|
15
|
+
readonly sampleRate: number;
|
|
16
|
+
readonly channels: number;
|
|
17
|
+
readonly encoding: 'pcm16' | 'float32';
|
|
18
|
+
readonly samples: Float32Array;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Decode a WAV document (RIFF/WAVE) to mono float samples at its declared rate.
|
|
22
|
+
*
|
|
23
|
+
* Accepted: PCM 16-bit and IEEE float32, one or two channels. Everything else refused by name.
|
|
24
|
+
*
|
|
25
|
+
* @param bytes - the file bytes
|
|
26
|
+
* @returns the decoded audio
|
|
27
|
+
* @throws Error naming the unsupported or malformed part
|
|
28
|
+
*/
|
|
29
|
+
export declare function decodeWav(bytes: Uint8Array): WavAudio;
|
|
30
|
+
/**
|
|
31
|
+
* Linearly resample mono samples (Resample.linear, AudioIO/Resample.swift:25-40).
|
|
32
|
+
*
|
|
33
|
+
* @param samples - mono source samples
|
|
34
|
+
* @param fromRate - the source sample rate
|
|
35
|
+
* @param toRate - the target sample rate
|
|
36
|
+
* @returns the resampled samples; a copy when the rates match
|
|
37
|
+
*/
|
|
38
|
+
export declare function resampleLinear(samples: Float32Array, fromRate: number, toRate: number): Float32Array;
|
|
39
|
+
//# sourceMappingURL=wav.d.ts.map
|
package/lib/wav.d.ts.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"wav.d.ts","sourceRoot":"","sources":["../src/wav.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,sCAAsC;AACtC,eAAO,MAAM,eAAe,QAAS,CAAA;AAErC,iFAAiF;AACjF,MAAM,WAAW,QAAQ;IACvB,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAA;IAC3B,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAA;IACzB,QAAQ,CAAC,QAAQ,EAAE,OAAO,GAAG,SAAS,CAAA;IACtC,QAAQ,CAAC,OAAO,EAAE,YAAY,CAAA;CAC/B;AAUD;;;;;;;;GAQG;AACH,wBAAgB,SAAS,CAAC,KAAK,EAAE,UAAU,GAAG,QAAQ,CAmErD;AAED;;;;;;;GAOG;AACH,wBAAgB,cAAc,CAAC,OAAO,EAAE,YAAY,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,YAAY,CAsBpG"}
|
package/lib/wav.js
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WAV decoding and linear resampling for the ear tool.
|
|
3
|
+
*
|
|
4
|
+
* The same contract as the Phase 3 waveform processor (runtime-onnx/src/wav.ts): PCM 16-bit
|
|
5
|
+
* or IEEE float32, mono or stereo, any declared rate. It is duplicated here deliberately so
|
|
6
|
+
* the LiteRT deployment pulls in no ONNX dependency; the behaviour is identical.
|
|
7
|
+
*
|
|
8
|
+
* Every unsupported document is refused BY NAME rather than guessed at, because a
|
|
9
|
+
* mis-decoded waveform is a wrong language with no other symptom.
|
|
10
|
+
*/
|
|
11
|
+
/** The rate the ear model expects. */
|
|
12
|
+
export const EAR_SAMPLE_RATE = 16_000;
|
|
13
|
+
const ascii = (bytes, offset) => String.fromCharCode(bytes[offset] ?? 0, bytes[offset + 1] ?? 0, bytes[offset + 2] ?? 0, bytes[offset + 3] ?? 0);
|
|
14
|
+
/** Fail with a named reason (a never-returning call, so callers can prune branches). */
|
|
15
|
+
function refuse(reason) {
|
|
16
|
+
throw new Error(reason);
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Decode a WAV document (RIFF/WAVE) to mono float samples at its declared rate.
|
|
20
|
+
*
|
|
21
|
+
* Accepted: PCM 16-bit and IEEE float32, one or two channels. Everything else refused by name.
|
|
22
|
+
*
|
|
23
|
+
* @param bytes - the file bytes
|
|
24
|
+
* @returns the decoded audio
|
|
25
|
+
* @throws Error naming the unsupported or malformed part
|
|
26
|
+
*/
|
|
27
|
+
export function decodeWav(bytes) {
|
|
28
|
+
if (bytes.length < 12)
|
|
29
|
+
refuse('the document is truncated: it is shorter than a RIFF header');
|
|
30
|
+
if (ascii(bytes, 0) !== 'RIFF')
|
|
31
|
+
refuse('the document is not a RIFF file: it starts with ' + JSON.stringify(ascii(bytes, 0)));
|
|
32
|
+
if (ascii(bytes, 8) !== 'WAVE')
|
|
33
|
+
refuse('the document is not a WAVE document: its form type is ' + JSON.stringify(ascii(bytes, 8)));
|
|
34
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
35
|
+
let format;
|
|
36
|
+
let data;
|
|
37
|
+
let offset = 12;
|
|
38
|
+
while (offset + 8 <= bytes.length) {
|
|
39
|
+
const id = ascii(bytes, offset);
|
|
40
|
+
const size = view.getUint32(offset + 4, true);
|
|
41
|
+
const body = offset + 8;
|
|
42
|
+
if (body + size > bytes.length)
|
|
43
|
+
refuse('the ' + JSON.stringify(id) + ' chunk is truncated: it declares ' + String(size) + ' bytes');
|
|
44
|
+
if (id === 'fmt ') {
|
|
45
|
+
if (size < 16)
|
|
46
|
+
refuse('the fmt chunk is too short to describe a sample format');
|
|
47
|
+
format = {
|
|
48
|
+
audioFormat: view.getUint16(body, true),
|
|
49
|
+
channels: view.getUint16(body + 2, true),
|
|
50
|
+
sampleRate: view.getUint32(body + 4, true),
|
|
51
|
+
bitsPerSample: view.getUint16(body + 14, true),
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
else if (id === 'data' && data === undefined) {
|
|
55
|
+
data = bytes.subarray(body, body + size);
|
|
56
|
+
}
|
|
57
|
+
offset = body + size + (size % 2);
|
|
58
|
+
}
|
|
59
|
+
if (format === undefined)
|
|
60
|
+
refuse('the document has no fmt chunk, so its samples cannot be interpreted');
|
|
61
|
+
if (data === undefined)
|
|
62
|
+
refuse('the document has no data chunk');
|
|
63
|
+
const encoding = format.audioFormat === 1 ? 'pcm16' : format.audioFormat === 3 ? 'float32' : undefined;
|
|
64
|
+
if (encoding === undefined) {
|
|
65
|
+
refuse('the audio format ' + String(format.audioFormat) + ' is not supported; export PCM 16-bit or IEEE float32');
|
|
66
|
+
}
|
|
67
|
+
const requiredBits = encoding === 'pcm16' ? 16 : 32;
|
|
68
|
+
if (format.bitsPerSample !== requiredBits) {
|
|
69
|
+
refuse(String(format.bitsPerSample) + ' bits per sample is not supported in this encoding; export '
|
|
70
|
+
+ (encoding === 'pcm16' ? 'PCM 16-bit' : 'IEEE float32'));
|
|
71
|
+
}
|
|
72
|
+
if (format.channels !== 1 && format.channels !== 2) {
|
|
73
|
+
refuse(String(format.channels) + ' audio channels is not supported; mono or stereo only');
|
|
74
|
+
}
|
|
75
|
+
if (!Number.isInteger(format.sampleRate) || format.sampleRate <= 0) {
|
|
76
|
+
refuse('the sample rate ' + String(format.sampleRate) + ' is not a positive integer');
|
|
77
|
+
}
|
|
78
|
+
const bytesPerSample = requiredBits / 8;
|
|
79
|
+
const bytesPerFrame = bytesPerSample * format.channels;
|
|
80
|
+
if (data.length % bytesPerFrame !== 0) {
|
|
81
|
+
refuse('the data chunk is ' + String(data.length) + ' bytes, which is not a whole number of ' + String(format.channels) + '-channel frames');
|
|
82
|
+
}
|
|
83
|
+
const frames = data.length / bytesPerFrame;
|
|
84
|
+
const samples = new Float32Array(frames);
|
|
85
|
+
const dataOffset = data.byteOffset;
|
|
86
|
+
for (let frame = 0; frame < frames; frame += 1) {
|
|
87
|
+
let sum = 0;
|
|
88
|
+
for (let channel = 0; channel < format.channels; channel += 1) {
|
|
89
|
+
const at = dataOffset + frame * bytesPerFrame + channel * bytesPerSample;
|
|
90
|
+
const value = encoding === 'pcm16' ? view.getInt16(at, true) / 32_768 : view.getFloat32(at, true);
|
|
91
|
+
if (!Number.isFinite(value))
|
|
92
|
+
refuse('sample ' + String(frame) + ' of channel ' + String(channel) + ' is not finite');
|
|
93
|
+
sum += value;
|
|
94
|
+
}
|
|
95
|
+
samples[frame] = sum / format.channels;
|
|
96
|
+
}
|
|
97
|
+
return { sampleRate: format.sampleRate, channels: format.channels, encoding, samples };
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Linearly resample mono samples (Resample.linear, AudioIO/Resample.swift:25-40).
|
|
101
|
+
*
|
|
102
|
+
* @param samples - mono source samples
|
|
103
|
+
* @param fromRate - the source sample rate
|
|
104
|
+
* @param toRate - the target sample rate
|
|
105
|
+
* @returns the resampled samples; a copy when the rates match
|
|
106
|
+
*/
|
|
107
|
+
export function resampleLinear(samples, fromRate, toRate) {
|
|
108
|
+
if (!Number.isFinite(fromRate) || fromRate <= 0)
|
|
109
|
+
throw new Error('the source sample rate must be a positive number');
|
|
110
|
+
if (!Number.isFinite(toRate) || toRate <= 0)
|
|
111
|
+
throw new Error('the target sample rate must be a positive number');
|
|
112
|
+
if (samples.length <= 1 || fromRate === toRate)
|
|
113
|
+
return Float32Array.from(samples);
|
|
114
|
+
const outputLength = Math.round((samples.length * toRate) / fromRate);
|
|
115
|
+
if (outputLength <= 0)
|
|
116
|
+
return new Float32Array(0);
|
|
117
|
+
const output = new Float32Array(outputLength);
|
|
118
|
+
const step = fromRate / toRate;
|
|
119
|
+
const last = samples.length - 1;
|
|
120
|
+
for (let index = 0; index < outputLength; index += 1) {
|
|
121
|
+
const position = index * step;
|
|
122
|
+
const lower = Math.trunc(position);
|
|
123
|
+
if (lower >= last) {
|
|
124
|
+
output[index] = samples[last] ?? 0;
|
|
125
|
+
continue;
|
|
126
|
+
}
|
|
127
|
+
const fraction = position - lower;
|
|
128
|
+
const start = samples[lower] ?? 0;
|
|
129
|
+
const end = samples[lower + 1] ?? start;
|
|
130
|
+
output[index] = start * (1 - fraction) + end * fraction;
|
|
131
|
+
}
|
|
132
|
+
return output;
|
|
133
|
+
}
|
|
134
|
+
//# sourceMappingURL=wav.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,49 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@try-works/tool-mode-runtime-litert",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Socket-free LiteRT.js runtime for Tool Mode: a fetch-interception shim plus an Effect LitertSession that serializes inference",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"publishConfig": {
|
|
7
|
+
"access": "public",
|
|
8
|
+
"registry": "https://registry.npmjs.org/"
|
|
9
|
+
},
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/try-works/tool-mode.git",
|
|
13
|
+
"directory": "packages/tool-mode-runtime-litert"
|
|
14
|
+
},
|
|
15
|
+
"type": "module",
|
|
16
|
+
"main": "lib/index.js",
|
|
17
|
+
"types": "lib/index.d.ts",
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"types": "./lib/index.d.ts",
|
|
21
|
+
"default": "./lib/index.js"
|
|
22
|
+
},
|
|
23
|
+
"./package.json": "./package.json"
|
|
24
|
+
},
|
|
25
|
+
"files": [
|
|
26
|
+
"lib/**/*.js",
|
|
27
|
+
"lib/**/*.d.ts",
|
|
28
|
+
"lib/**/*.d.ts.map"
|
|
29
|
+
],
|
|
30
|
+
"scripts": {
|
|
31
|
+
"build": "tsc -p tsconfig.build.json",
|
|
32
|
+
"typecheck": "tsc --noEmit -p tsconfig.json",
|
|
33
|
+
"test": "tsx --test tests/*.test.ts"
|
|
34
|
+
},
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"@litertjs/core": "2.5.3",
|
|
37
|
+
"@try-works/tool-mode-extension-sdk": "0.1.0",
|
|
38
|
+
"@try-works/tool-mode-runtime-core": "0.1.0",
|
|
39
|
+
"@try-works/tool-mode-runtime-recipes": "0.1.0"
|
|
40
|
+
},
|
|
41
|
+
"peerDependencies": {
|
|
42
|
+
"effect": "4.1.2"
|
|
43
|
+
},
|
|
44
|
+
"devDependencies": {
|
|
45
|
+
"@types/node": "^24.0.0",
|
|
46
|
+
"tsx": "^4.20.0",
|
|
47
|
+
"typescript": "^5.9.0"
|
|
48
|
+
}
|
|
49
|
+
}
|
package/README.md
DELETED