@try-works/tool-mode-runtime-onnx 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.
@@ -0,0 +1,487 @@
1
+ /**
2
+ * The DeepFilterNet3 front end, chunk layout and mastering the clear graph depends on
3
+ * (processor `clear/stft-v1`).
4
+ *
5
+ * THE GRAPH TAKES FEATURES, NOT A WAVEFORM: inputs `spec` (interleaved complex STFT),
6
+ * `feat_erb` and `feat_spec` (DFN3 features computed here), output `spec_enhanced`. So
7
+ * every constant below is part of the model contract, and a plausible-looking substitute
8
+ * (1024-point FFT, Hann window, log-mel filterbank) is silent garbage rather than an error.
9
+ *
10
+ * PRIMARY SOURCE for every value, desert-ant-core commit
11
+ * baf4e6e3b4655c8e54aa8d5a25215b5f35f899a7:
12
+ * - Sources/Clear/DSP.swift L31-46 (geometry, normAlpha, ERB widths), L271-302 (Vorbis
13
+ * window, wnorm, prepad), L343-476 (framing, mirror, overlap-add, prepad drop);
14
+ * - Sources/Clear/Features.swift L20-125 (ERB dB, running normalization, unit-norm);
15
+ * - Sources/Clear/Enhancer.swift L55-73, L185-259 (200-frame chunks, interleaved layout,
16
+ * the two-frame feature lookahead);
17
+ * - Sources/Clear/Clear.swift L428-434, L478-494 (48 kHz, mono downmix, mastering);
18
+ * - Sources/AudioDSP/Loudness.swift L24-27, L90-244, L264-285 (K-weighting, blocks,
19
+ * gating, gain cap);
20
+ * - Sources/AudioDSP/Limiter.swift L28-30, L169-198 (look-ahead and release).
21
+ *
22
+ * The module is PURE: no I/O, no engine. `ClearStft` holds scratch buffers and is used
23
+ * sequentially, like the reference.
24
+ *
25
+ * @module clear-dsp
26
+ */
27
+ import { Dft960 } from "./fft.js";
28
+ /** The model's sample rate, in and out. */
29
+ export const CLEAR_SAMPLE_RATE = 48_000;
30
+ /** STFT size (libDF / DFN3). */
31
+ export const CLEAR_FFT_SIZE = 960;
32
+ /** STFT hop; half the window, so consecutive frames overlap by 50%. */
33
+ export const CLEAR_HOP_SIZE = 480;
34
+ /** STFT bins (fftSize/2 + 1). */
35
+ export const CLEAR_N_FREQ = 481;
36
+ /** ERB bands for feat_erb. */
37
+ export const CLEAR_N_ERB = 32;
38
+ /** Complex bins kept for feat_spec. */
39
+ export const CLEAR_N_DF = 96;
40
+ /** Feature lookahead: feat_erb/feat_spec frame t come from analysis frame t+2. */
41
+ export const CLEAR_LOOKAHEAD = 2;
42
+ /** Frames per model window. */
43
+ export const CLEAR_CHUNK_FRAMES = 200;
44
+ /** The running-normalization coefficient: round(exp(-hop/sr/tau), 3) with tau = 1 s. */
45
+ export const CLEAR_NORM_ALPHA = 0.99;
46
+ /**
47
+ * ERB band widths, fixed at training time: thirteen 2-bin bands, then 5, 5, 7, 7, 8, 10,
48
+ * 12, 13, 15, 18, 20, 24, 28, 31, 37, 42, 50, 56, 67. Their sum is exactly nFreq.
49
+ */
50
+ export const CLEAR_ERB_WIDTHS = [
51
+ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
52
+ 5, 5, 7, 7, 8, 10, 12, 13, 15, 18, 20,
53
+ 24, 28, 31, 37, 42, 50, 56, 67,
54
+ ];
55
+ /** The SDK's default integrated-loudness target (Apple Podcasts). */
56
+ export const CLEAR_TARGET_LUFS = -19;
57
+ /** Cap on upward loudness gain, so a quiet input does not lift the noise floor. */
58
+ export const CLEAR_MAX_GAIN_DB = 9;
59
+ /** True-peak ceiling for the look-ahead limiter. */
60
+ export const CLEAR_PEAK_CEILING_DBTP = -1.5;
61
+ /** WAV header size for 16-bit PCM. */
62
+ export const CLEAR_WAV_HEADER_BYTES = 44;
63
+ /** JSON envelope allowance reserved beside audio_base64 in the result budget. */
64
+ export const CLEAR_OUTPUT_ENVELOPE_BYTES = 1024;
65
+ /** The Vorbis window: sin(pi/2 * sin^2(pi/2 * (n+0.5)/halfN)), halfN = 480. */
66
+ const VORBIS_WINDOW = (() => {
67
+ const window = new Float32Array(CLEAR_FFT_SIZE);
68
+ const halfN = CLEAR_FFT_SIZE / 2;
69
+ for (let n = 0; n < CLEAR_FFT_SIZE; n += 1) {
70
+ const s = Math.sin((0.5 * Math.PI * (n + 0.5)) / halfN);
71
+ window[n] = Math.sin(0.5 * Math.PI * s * s);
72
+ }
73
+ return window;
74
+ })();
75
+ /**
76
+ * The analysis/synthesis window.
77
+ *
78
+ * @returns a copy, so a caller cannot change the transform for everyone
79
+ */
80
+ export function vorbisWindow() {
81
+ return Float32Array.from(VORBIS_WINDOW);
82
+ }
83
+ /**
84
+ * The clear STFT: Vorbis window, n_fft 960, hop 480, 480-sample analysis prepad, forward
85
+ * gain 1/960, unscaled inverse DFT and windowed COLA overlap-add.
86
+ *
87
+ * Holds one DFT and scratch buffers; use it sequentially.
88
+ */
89
+ export class ClearStft {
90
+ /** The transform size. */
91
+ fftSize = CLEAR_FFT_SIZE;
92
+ /** The hop size. */
93
+ hopSize = CLEAR_HOP_SIZE;
94
+ /** The number of bins kept. */
95
+ nFreq = CLEAR_N_FREQ;
96
+ dft = new Dft960();
97
+ /** wnorm = 2*hop/fftSize^2 = 1/960: compensates the unscaled inverse DFT. */
98
+ wnorm = (2 * CLEAR_HOP_SIZE) / (CLEAR_FFT_SIZE * CLEAR_FFT_SIZE);
99
+ frame = new Float32Array(CLEAR_FFT_SIZE);
100
+ fullReal = new Float32Array(CLEAR_FFT_SIZE);
101
+ fullImag = new Float32Array(CLEAR_FFT_SIZE);
102
+ timeReal = new Float32Array(CLEAR_FFT_SIZE);
103
+ timeImag = new Float32Array(CLEAR_FFT_SIZE);
104
+ /**
105
+ * Analyse a mono 48 kHz waveform.
106
+ *
107
+ * @param audio - the samples
108
+ * @returns the frame-major spectrum
109
+ */
110
+ forward(audio) {
111
+ const prePad = this.fftSize - this.hopSize;
112
+ // The reference pads the tail so the last frames are complete: see ClearEnhancer
113
+ // prepareInput, Sources/Clear/Enhancer.swift L390-403.
114
+ const windowed = Math.max(this.fftSize, Math.ceil(audio.length / this.hopSize) * this.hopSize + this.fftSize);
115
+ const paddedLength = prePad + Math.max(windowed, audio.length);
116
+ const padded = new Float32Array(paddedLength);
117
+ for (let i = 0; i < audio.length; i += 1) {
118
+ const value = audio[i] ?? 0;
119
+ padded[prePad + i] = Number.isFinite(value) ? value : 0;
120
+ }
121
+ const nFrames = Math.floor((paddedLength - this.fftSize) / this.hopSize) + 1;
122
+ const real = new Float32Array(nFrames * this.nFreq);
123
+ const imag = new Float32Array(nFrames * this.nFreq);
124
+ for (let t = 0; t < nFrames; t += 1) {
125
+ const offset = t * this.hopSize;
126
+ for (let n = 0; n < this.fftSize; n += 1) {
127
+ this.frame[n] = (padded[offset + n] ?? 0) * (VORBIS_WINDOW[n] ?? 0);
128
+ }
129
+ this.dft.forward(this.frame, 0, this.fullReal, this.fullImag, 0);
130
+ const base = t * this.nFreq;
131
+ for (let k = 0; k < this.nFreq; k += 1) {
132
+ real[base + k] = (this.fullReal[k] ?? 0) * this.wnorm;
133
+ imag[base + k] = (this.fullImag[k] ?? 0) * this.wnorm;
134
+ }
135
+ }
136
+ return { real, imag, nFrames };
137
+ }
138
+ /**
139
+ * Synthesize a waveform from a spectrum.
140
+ *
141
+ * @param real - frame-major real parts
142
+ * @param imag - frame-major imaginary parts
143
+ * @param nFrames - how many frames to synthesize
144
+ * @returns the waveform with the analysis prepad dropped (at least the input length)
145
+ */
146
+ inverse(real, imag, nFrames) {
147
+ const prePad = this.fftSize - this.hopSize;
148
+ const rawLength = (nFrames - 1) * this.hopSize + this.fftSize;
149
+ const out = new Float32Array(rawLength);
150
+ for (let t = 0; t < nFrames; t += 1) {
151
+ const base = t * this.nFreq;
152
+ for (let k = 0; k < this.nFreq; k += 1) {
153
+ this.fullReal[k] = real[base + k] ?? 0;
154
+ this.fullImag[k] = imag[base + k] ?? 0;
155
+ }
156
+ // Hermitian mirror: X[960-k] = conj(X[k]) for k in 1..479. Bin 480 (Nyquist) and bin
157
+ // 0 are self-conjugate and already in place.
158
+ for (let k = 1; k < this.nFreq - 1; k += 1) {
159
+ this.fullReal[this.fftSize - k] = real[base + k] ?? 0;
160
+ this.fullImag[this.fftSize - k] = -(imag[base + k] ?? 0);
161
+ }
162
+ this.dft.inverseUnscaled(this.fullReal, this.fullImag, 0, this.timeReal, this.timeImag, 0);
163
+ const offset = t * this.hopSize;
164
+ for (let n = 0; n < this.fftSize; n += 1) {
165
+ out[offset + n] = (out[offset + n] ?? 0) + (this.timeReal[n] ?? 0) * (VORBIS_WINDOW[n] ?? 0);
166
+ }
167
+ }
168
+ return out.subarray(prePad);
169
+ }
170
+ }
171
+ /**
172
+ * Compute feat_erb and feat_spec exactly as the reference does.
173
+ *
174
+ * @param real - frame-major real spectrum
175
+ * @param imag - frame-major imaginary spectrum
176
+ * @param nFrames - the number of frames
177
+ * @returns the two feature tensors
178
+ */
179
+ export function clearFeatures(real, imag, nFrames) {
180
+ const alpha = CLEAR_NORM_ALPHA;
181
+ const oneMinus = 1 - alpha;
182
+ const erbState = new Float64Array(CLEAR_N_ERB);
183
+ const erbStep = (-90 - -60) / (CLEAR_N_ERB - 1);
184
+ for (let band = 0; band < CLEAR_N_ERB; band += 1)
185
+ erbState[band] = -60 + band * erbStep;
186
+ const featErb = new Float32Array(nFrames * CLEAR_N_ERB);
187
+ for (let t = 0; t < nFrames; t += 1) {
188
+ const rowIn = t * CLEAR_N_FREQ;
189
+ const rowOut = t * CLEAR_N_ERB;
190
+ let offset = 0;
191
+ for (let band = 0; band < CLEAR_N_ERB; band += 1) {
192
+ const width = CLEAR_ERB_WIDTHS[band] ?? 1;
193
+ let power = 0;
194
+ for (let k = 0; k < width; k += 1) {
195
+ const at = rowIn + offset + k;
196
+ const r = real[at] ?? 0;
197
+ const m = imag[at] ?? 0;
198
+ power += r * r + m * m;
199
+ }
200
+ offset += width;
201
+ const db = 10 * Math.log10(power / width + 1e-10);
202
+ const state = db * oneMinus + (erbState[band] ?? 0) * alpha;
203
+ erbState[band] = state;
204
+ featErb[rowOut + band] = (db - state) / 40;
205
+ }
206
+ }
207
+ const running = new Float64Array(CLEAR_N_DF);
208
+ const runStep = (0.0001 - 0.001) / (CLEAR_N_DF - 1);
209
+ for (let f = 0; f < CLEAR_N_DF; f += 1)
210
+ running[f] = 0.001 + f * runStep;
211
+ const featSpecReal = new Float32Array(nFrames * CLEAR_N_DF);
212
+ const featSpecImag = new Float32Array(nFrames * CLEAR_N_DF);
213
+ for (let t = 0; t < nFrames; t += 1) {
214
+ const rowIn = t * CLEAR_N_FREQ;
215
+ const rowOut = t * CLEAR_N_DF;
216
+ for (let f = 0; f < CLEAR_N_DF; f += 1) {
217
+ const r = real[rowIn + f] ?? 0;
218
+ const m = imag[rowIn + f] ?? 0;
219
+ const magnitude = Math.hypot(r, m);
220
+ const next = magnitude * oneMinus + (running[f] ?? 0) * alpha;
221
+ running[f] = next;
222
+ const inverse = 1 / Math.sqrt(next);
223
+ featSpecReal[rowOut + f] = r * inverse;
224
+ featSpecImag[rowOut + f] = m * inverse;
225
+ }
226
+ }
227
+ return { featErb, featSpecReal, featSpecImag };
228
+ }
229
+ /**
230
+ * Pack one 200-frame model window.
231
+ *
232
+ * The spectrum is read at the window's own frames; the FEATURES are read two frames ahead
233
+ * and zero-filled past the tail (Sources/Clear/Enhancer.swift L198-237).
234
+ *
235
+ * @param request - the whole-signal arrays and where the window starts
236
+ * @returns the three input tensors
237
+ */
238
+ export function clearChunkInputs(request) {
239
+ const chunkFrames = request.chunkFrames ?? CLEAR_CHUNK_FRAMES;
240
+ const lookahead = request.lookahead ?? CLEAR_LOOKAHEAD;
241
+ const { nFrames, start } = request;
242
+ const spec = new Float32Array(chunkFrames * CLEAR_N_FREQ * 2);
243
+ const featErb = new Float32Array(chunkFrames * CLEAR_N_ERB);
244
+ const featSpec = new Float32Array(chunkFrames * CLEAR_N_DF * 2);
245
+ const featureStart = Math.max(0, -start - lookahead);
246
+ const featureEnd = Math.min(chunkFrames, nFrames - start - lookahead);
247
+ for (let t = featureStart; t < featureEnd; t += 1) {
248
+ const source = start + t + lookahead;
249
+ for (let f = 0; f < CLEAR_N_ERB; f += 1) {
250
+ featErb[t * CLEAR_N_ERB + f] = request.featErb[source * CLEAR_N_ERB + f] ?? 0;
251
+ }
252
+ for (let f = 0; f < CLEAR_N_DF; f += 1) {
253
+ const at = (t * CLEAR_N_DF + f) * 2;
254
+ const sourceAt = source * CLEAR_N_DF + f;
255
+ featSpec[at] = request.featSpecReal[sourceAt] ?? 0;
256
+ featSpec[at + 1] = request.featSpecImag[sourceAt] ?? 0;
257
+ }
258
+ }
259
+ const spectrumEnd = Math.min(chunkFrames, nFrames - start);
260
+ for (let t = 0; t < spectrumEnd; t += 1) {
261
+ const source = start + t;
262
+ for (let k = 0; k < CLEAR_N_FREQ; k += 1) {
263
+ const at = (t * CLEAR_N_FREQ + k) * 2;
264
+ const sourceAt = source * CLEAR_N_FREQ + k;
265
+ spec[at] = request.specReal[sourceAt] ?? 0;
266
+ spec[at + 1] = request.specImag[sourceAt] ?? 0;
267
+ }
268
+ }
269
+ return { spec, featErb, featSpec, chunkFrames, validFrames: Math.max(0, spectrumEnd) };
270
+ }
271
+ /**
272
+ * Copy a chunk's enhanced spectrum into the whole-signal planes.
273
+ *
274
+ * @param request - the output tensor, the window start and the destination planes
275
+ */
276
+ export function scatterEnhanced(request) {
277
+ const { enhanced, start, validFrames } = request;
278
+ for (let t = 0; t < validFrames; t += 1) {
279
+ const destination = start + t;
280
+ for (let k = 0; k < CLEAR_N_FREQ; k += 1) {
281
+ const at = (t * CLEAR_N_FREQ + k) * 2;
282
+ const target = destination * CLEAR_N_FREQ + k;
283
+ request.outReal[target] = enhanced[at] ?? 0;
284
+ request.outImag[target] = enhanced[at + 1] ?? 0;
285
+ }
286
+ }
287
+ }
288
+ /** BS.1770-4 K-weighting stage 1 (high shelf) at 48 kHz. */
289
+ const K_WEIGHT_STAGE1_B = [1.53512485958697, -2.69169618940638, 1.19839281085285];
290
+ const K_WEIGHT_STAGE1_A = [-1.69065929318241, 0.73248077421585];
291
+ /** BS.1770-4 K-weighting stage 2 (high pass) at 48 kHz. */
292
+ const K_WEIGHT_STAGE2_B = [1.0, -2.0, 1.0];
293
+ const K_WEIGHT_STAGE2_A = [-1.99004745483398, 0.99007225036621];
294
+ /** One direct-form-I biquad over a Float32 signal, in place. */
295
+ function biquadInPlace(samples, b, a) {
296
+ const b0 = b[0] ?? 0;
297
+ const b1 = b[1] ?? 0;
298
+ const b2 = b[2] ?? 0;
299
+ const a0 = a[0] ?? 0;
300
+ const a1 = a[1] ?? 0;
301
+ let x1 = 0;
302
+ let x2 = 0;
303
+ let y1 = 0;
304
+ let y2 = 0;
305
+ for (let i = 0; i < samples.length; i += 1) {
306
+ const xn = samples[i] ?? 0;
307
+ const yn = b0 * xn + b1 * x1 + b2 * x2 - a0 * y1 - a1 * y2;
308
+ x2 = x1;
309
+ x1 = xn;
310
+ y2 = y1;
311
+ y1 = yn;
312
+ samples[i] = yn;
313
+ }
314
+ }
315
+ /**
316
+ * ITU-R BS.1770-4 integrated loudness of mono samples at 48 kHz.
317
+ *
318
+ * 400 ms blocks at a 100 ms step; gated at -70 LUFS, then at (gated mean - 10 LU).
319
+ *
320
+ * @param samples - the signal
321
+ * @param sampleRate - must be 48 kHz, the rate the K-weighting coefficients were derived for
322
+ * @returns LUFS, or null for silence or audio shorter than one block
323
+ */
324
+ export function integratedLufs(samples, sampleRate = CLEAR_SAMPLE_RATE) {
325
+ if (sampleRate !== CLEAR_SAMPLE_RATE || samples.length === 0)
326
+ return null;
327
+ const weighted = Float32Array.from(samples);
328
+ biquadInPlace(weighted, K_WEIGHT_STAGE1_B, K_WEIGHT_STAGE1_A);
329
+ biquadInPlace(weighted, K_WEIGHT_STAGE2_B, K_WEIGHT_STAGE2_A);
330
+ const block = Math.round(0.4 * sampleRate);
331
+ const step = Math.round(0.1 * sampleRate);
332
+ if (weighted.length < block)
333
+ return null;
334
+ const blockLoud = [];
335
+ const meanSquares = [];
336
+ for (let start = 0; start + block <= weighted.length; start += step) {
337
+ let sum = 0;
338
+ for (let i = start; i < start + block; i += 1) {
339
+ const value = weighted[i] ?? 0;
340
+ sum += value * value;
341
+ }
342
+ const meanSquare = sum / block;
343
+ meanSquares.push(meanSquare);
344
+ blockLoud.push(-0.691 + 10 * Math.log10(meanSquare + 1e-12));
345
+ }
346
+ const gated = (threshold) => {
347
+ let accumulator = 0;
348
+ let count = 0;
349
+ for (let i = 0; i < blockLoud.length; i += 1) {
350
+ if ((blockLoud[i] ?? -Infinity) > threshold) {
351
+ accumulator += meanSquares[i] ?? 0;
352
+ count += 1;
353
+ }
354
+ }
355
+ return count > 0 ? -0.691 + 10 * Math.log10(accumulator / count + 1e-12) : null;
356
+ };
357
+ const firstPass = gated(-70);
358
+ if (firstPass === null)
359
+ return null;
360
+ return gated(firstPass - 10) ?? firstPass;
361
+ }
362
+ /**
363
+ * Normalize loudness in place: measure, apply min(maxGain, target - measured), then limit.
364
+ *
365
+ * @param samples - the signal, at 48 kHz
366
+ * @param options - target, gain cap and peak ceiling
367
+ * @returns the measured input LUFS, or null when the signal was left untouched (silence)
368
+ */
369
+ export function normalizeLoudnessInPlace(samples, options = {}) {
370
+ const target = options.targetLufs ?? CLEAR_TARGET_LUFS;
371
+ const maxGain = options.maxGainDb ?? CLEAR_MAX_GAIN_DB;
372
+ const ceiling = options.peakCeilingDbtp ?? CLEAR_PEAK_CEILING_DBTP;
373
+ const measured = integratedLufs(samples);
374
+ if (measured === null)
375
+ return null;
376
+ // Only upward gain is capped: attenuating a loud input is always safe.
377
+ const gainDb = Math.min(maxGain, target - measured);
378
+ const gain = Math.pow(10, gainDb / 20);
379
+ for (let i = 0; i < samples.length; i += 1)
380
+ samples[i] = (samples[i] ?? 0) * gain;
381
+ limitInPlace(samples, ceiling, CLEAR_SAMPLE_RATE);
382
+ return measured;
383
+ }
384
+ /**
385
+ * A look-ahead peak limiter, in place.
386
+ *
387
+ * Instantaneous attack: the envelope is already down when a peak arrives, because the
388
+ * window maximum looks 5 ms ahead. Exponential release. This is the reference's single-pass
389
+ * form of Limiter.Streaming.process + flush (same envelope over the whole signal).
390
+ *
391
+ * @param samples - the signal
392
+ * @param ceilingDbtp - the sample-peak ceiling
393
+ * @param sampleRate - the sample rate the window and release are derived for
394
+ */
395
+ export function limitInPlace(samples, ceilingDbtp, sampleRate) {
396
+ const n = samples.length;
397
+ if (n === 0)
398
+ return;
399
+ const ceiling = Math.pow(10, ceilingDbtp / 20);
400
+ const lookahead = Math.max(0, Math.round(0.005 * sampleRate));
401
+ const release = Math.exp(-1 / (sampleRate * 0.05));
402
+ const magnitude = new Float32Array(n);
403
+ for (let i = 0; i < n; i += 1)
404
+ magnitude[i] = Math.abs(samples[i] ?? 0);
405
+ // Monotonic-decreasing deque of candidate peak indices; head advances, tail pops.
406
+ const deque = [];
407
+ let head = 0;
408
+ const push = (index) => {
409
+ const value = magnitude[index] ?? 0;
410
+ while (deque.length > head && (magnitude[deque[deque.length - 1] ?? 0] ?? 0) <= value)
411
+ deque.pop();
412
+ deque.push(index);
413
+ };
414
+ for (let j = 0; j < Math.min(lookahead, n); j += 1)
415
+ push(j);
416
+ let envelope = 1;
417
+ for (let i = 0; i < n; i += 1) {
418
+ const ahead = i + lookahead;
419
+ if (ahead < n)
420
+ push(ahead);
421
+ while (deque.length > head && (deque[head] ?? 0) < i)
422
+ head += 1;
423
+ const upcoming = deque.length > head ? magnitude[deque[head] ?? 0] ?? 0 : 0;
424
+ if (upcoming > ceiling) {
425
+ const required = ceiling / upcoming;
426
+ if (required < envelope)
427
+ envelope = required;
428
+ }
429
+ samples[i] = (samples[i] ?? 0) * envelope;
430
+ envelope = 1 - (1 - envelope) * release;
431
+ }
432
+ }
433
+ /**
434
+ * Encode mono float samples as a 16-bit PCM RIFF/WAVE document.
435
+ *
436
+ * @param samples - the samples
437
+ * @param sampleRate - the sample rate to declare
438
+ * @returns the WAV bytes
439
+ */
440
+ export function encodePcm16Wav(samples, sampleRate) {
441
+ const dataBytes = samples.length * 2;
442
+ const bytes = new Uint8Array(CLEAR_WAV_HEADER_BYTES + dataBytes);
443
+ const view = new DataView(bytes.buffer);
444
+ const writeAscii = (offset, text) => {
445
+ for (let i = 0; i < text.length; i += 1)
446
+ bytes[offset + i] = text.charCodeAt(i);
447
+ };
448
+ writeAscii(0, 'RIFF');
449
+ view.setUint32(4, 36 + dataBytes, true);
450
+ writeAscii(8, 'WAVE');
451
+ writeAscii(12, 'fmt ');
452
+ view.setUint32(16, 16, true);
453
+ view.setUint16(20, 1, true);
454
+ view.setUint16(22, 1, true);
455
+ view.setUint32(24, sampleRate, true);
456
+ view.setUint32(28, sampleRate * 2, true);
457
+ view.setUint16(32, 2, true);
458
+ view.setUint16(34, 16, true);
459
+ writeAscii(36, 'data');
460
+ view.setUint32(40, dataBytes, true);
461
+ for (let i = 0; i < samples.length; i += 1) {
462
+ const clamped = Math.max(-1, Math.min(1, samples[i] ?? 0));
463
+ view.setInt16(CLEAR_WAV_HEADER_BYTES + i * 2, Math.round(clamped * 32_767), true);
464
+ }
465
+ return bytes;
466
+ }
467
+ /**
468
+ * The most 48 kHz mono PCM16 samples whose base64 WAV fits the host's result budget.
469
+ *
470
+ * base64 is 4/3 of the raw WAV, which is 44 header bytes plus 2 bytes per sample. The
471
+ * 1024-byte envelope allowance is for the rest of the JSON result.
472
+ *
473
+ * @param maxResultBytes - context.limits.maxResultBytes
474
+ * @returns the sample cap, or 0 when even a header cannot fit
475
+ */
476
+ export function maxSamplesForResultBudget(maxResultBytes) {
477
+ if (!Number.isFinite(maxResultBytes))
478
+ return 0;
479
+ const usable = maxResultBytes - CLEAR_OUTPUT_ENVELOPE_BYTES;
480
+ if (usable <= CLEAR_WAV_HEADER_BYTES)
481
+ return 0;
482
+ const wavBytes = Math.floor((usable * 3) / 4);
483
+ if (wavBytes <= CLEAR_WAV_HEADER_BYTES)
484
+ return 0;
485
+ return Math.floor((wavBytes - CLEAR_WAV_HEADER_BYTES) / 2);
486
+ }
487
+ //# sourceMappingURL=clear-dsp.js.map
@@ -0,0 +1,30 @@
1
+ /**
2
+ * Processor `clear/stft-v1`: DeepFilterNet3 speech enhancement (Phase 3, after uhm).
3
+ *
4
+ * The graph takes HOST-COMPUTED features (`spec`, `feat_erb`, `feat_spec`) and returns
5
+ * `spec_enhanced`; the DSP that computes and inverts them is `clear-dsp.ts`, pinned to the
6
+ * primary source. This module is the pipeline: window the spectrum, run the model per
7
+ * 200-frame chunk, overlap-add, master, encode a WAV under the host's result budget.
8
+ *
9
+ * OUTPUT IS BYTES, NOT A PATH. InvocationContextV1 has no writable path (resolvePath
10
+ * grants reads), so the enhanced audio travels back as base64 WAV and the caller writes it.
11
+ *
12
+ * @module clear-processor
13
+ */
14
+ import type { ProcessorDefinition } from './processor.ts';
15
+ /** The recipe processor id this module implements. */
16
+ export declare const CLEAR_STFT_PROCESSOR = "clear/stft-v1";
17
+ /** The graph binding the provider proves at load time. */
18
+ export interface ClearBinding {
19
+ readonly spec: string;
20
+ readonly featErb: string;
21
+ readonly featSpec: string;
22
+ readonly output: string;
23
+ }
24
+ /**
25
+ * The clear processor definition.
26
+ *
27
+ * @returns the definition the provider factory dispatches to
28
+ */
29
+ export declare function clearProcessor(): ProcessorDefinition<void, ClearBinding>;
30
+ //# sourceMappingURL=clear-processor.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"clear-processor.d.ts","sourceRoot":"","sources":["../src/clear-processor.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAmBH,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,gBAAgB,CAAA;AAEzD,sDAAsD;AACtD,eAAO,MAAM,oBAAoB,kBAAkB,CAAA;AAUnD,0DAA0D;AAC1D,MAAM,WAAW,YAAY;IAC3B,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;IACrB,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAA;IACxB,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAA;IACzB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAA;CACxB;AAED;;;;GAIG;AACH,wBAAgB,cAAc,IAAI,mBAAmB,CAAC,IAAI,EAAE,YAAY,CAAC,CAgKxE"}