@desert-ant-labs/clear 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/LICENSE.md +136 -0
- package/README.md +143 -0
- package/THIRD_PARTY_NOTICES.md +56 -0
- package/dist/cache-node.d.ts +5 -0
- package/dist/cache-node.js +32 -0
- package/dist/cache-web.d.ts +3 -0
- package/dist/cache-web.js +24 -0
- package/dist/decode-web.d.ts +2 -0
- package/dist/decode-web.js +49 -0
- package/dist/erb.d.ts +9 -0
- package/dist/erb.js +40 -0
- package/dist/features.d.ts +15 -0
- package/dist/features.js +59 -0
- package/dist/fft.d.ts +19 -0
- package/dist/fft.js +145 -0
- package/dist/hub.d.ts +34 -0
- package/dist/hub.js +74 -0
- package/dist/index.browser.d.ts +31 -0
- package/dist/index.browser.js +71 -0
- package/dist/index.node.d.ts +24 -0
- package/dist/index.node.js +44 -0
- package/dist/model.d.ts +50 -0
- package/dist/model.js +194 -0
- package/dist/ort.d.ts +34 -0
- package/dist/ort.js +38 -0
- package/dist/r128.d.ts +31 -0
- package/dist/r128.js +140 -0
- package/dist/stft.d.ts +26 -0
- package/dist/stft.js +105 -0
- package/dist/wav.d.ts +17 -0
- package/dist/wav.js +126 -0
- package/package.json +95 -0
package/dist/ort.d.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/** A tensor — only the fields clear reads/writes. */
|
|
2
|
+
export interface OrtTensor {
|
|
3
|
+
data: Float32Array;
|
|
4
|
+
dispose?(): void;
|
|
5
|
+
}
|
|
6
|
+
export interface OrtTensorConstructor {
|
|
7
|
+
new (type: "float32", data: Float32Array, dims: number[]): OrtTensor;
|
|
8
|
+
}
|
|
9
|
+
export interface OrtSession {
|
|
10
|
+
run(feeds: Record<string, OrtTensor>): Promise<Record<string, OrtTensor>>;
|
|
11
|
+
release?(): Promise<void>;
|
|
12
|
+
}
|
|
13
|
+
export interface OrtSessionFactory {
|
|
14
|
+
create(model: Uint8Array | ArrayBuffer | string, options?: unknown): Promise<OrtSession>;
|
|
15
|
+
}
|
|
16
|
+
/** The subset of an ONNX Runtime module that clear depends on. */
|
|
17
|
+
export interface Ort {
|
|
18
|
+
Tensor: OrtTensorConstructor;
|
|
19
|
+
InferenceSession: OrtSessionFactory;
|
|
20
|
+
env?: Record<string, unknown>;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Inject a pre-loaded ONNX Runtime module. Call before {@link load} to avoid
|
|
24
|
+
* the dynamic import (useful in bundled apps or non-standard runtimes).
|
|
25
|
+
*/
|
|
26
|
+
export declare function setOrt(module: Ort): void;
|
|
27
|
+
/** Returns the injected runtime, if any. */
|
|
28
|
+
export declare function getInjectedOrt(): Ort | null;
|
|
29
|
+
/**
|
|
30
|
+
* Resolve the ONNX Runtime: the injected module if set, else a lazy dynamic
|
|
31
|
+
* import of `specifier`. The specifier is passed as a variable so bundlers and
|
|
32
|
+
* `tsc` don't try to resolve the (optional) peer dependency at build time.
|
|
33
|
+
*/
|
|
34
|
+
export declare function resolveOrt(specifier: string): Promise<Ort>;
|
package/dist/ort.js
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Minimal structural typing for the ONNX Runtime surface clear uses, plus
|
|
3
|
+
// injection. The actual runtime (`onnxruntime-web` in the browser,
|
|
4
|
+
// `onnxruntime-node` on the server) is a peer dependency, loaded lazily so
|
|
5
|
+
// the package itself stays small and bundler-agnostic.
|
|
6
|
+
let injected = null;
|
|
7
|
+
/**
|
|
8
|
+
* Inject a pre-loaded ONNX Runtime module. Call before {@link load} to avoid
|
|
9
|
+
* the dynamic import (useful in bundled apps or non-standard runtimes).
|
|
10
|
+
*/
|
|
11
|
+
export function setOrt(module) {
|
|
12
|
+
injected = module;
|
|
13
|
+
}
|
|
14
|
+
/** Returns the injected runtime, if any. */
|
|
15
|
+
export function getInjectedOrt() {
|
|
16
|
+
return injected;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Resolve the ONNX Runtime: the injected module if set, else a lazy dynamic
|
|
20
|
+
* import of `specifier`. The specifier is passed as a variable so bundlers and
|
|
21
|
+
* `tsc` don't try to resolve the (optional) peer dependency at build time.
|
|
22
|
+
*/
|
|
23
|
+
export async function resolveOrt(specifier) {
|
|
24
|
+
if (injected)
|
|
25
|
+
return injected;
|
|
26
|
+
let mod;
|
|
27
|
+
try {
|
|
28
|
+
mod = (await import(/* @vite-ignore */ specifier));
|
|
29
|
+
}
|
|
30
|
+
catch (err) {
|
|
31
|
+
throw new Error(`clear: could not load "${specifier}". Install it as a peer dependency ` +
|
|
32
|
+
`(\`npm install ${specifier}\`) or inject a runtime with \`setOrt()\`. ` +
|
|
33
|
+
`Original error: ${err?.message ?? err}`);
|
|
34
|
+
}
|
|
35
|
+
const resolved = (mod.default ?? mod);
|
|
36
|
+
injected = resolved;
|
|
37
|
+
return resolved;
|
|
38
|
+
}
|
package/dist/r128.d.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/** BS.1770-gated integrated LUFS for a single (mono) buffer. `-Infinity` for silence. */
|
|
2
|
+
export declare function measureLUFS(audio: Float32Array, sampleRate: number): number;
|
|
3
|
+
/**
|
|
4
|
+
* Look-ahead true-peak limiter (5 ms look-ahead, 50 ms release).
|
|
5
|
+
* @param ceilDBTP ceiling in dBFS (negative — e.g. -1.5)
|
|
6
|
+
*/
|
|
7
|
+
export declare function applyLimiter(audio: Float32Array, sampleRate: number, ceilDBTP: number): Float32Array;
|
|
8
|
+
/** Model-output attenuation in dB — feeds the mastering gain calc. */
|
|
9
|
+
export declare const MODEL_ATTENUATION_DB = 0.7;
|
|
10
|
+
export type MasteringPreset = "applePodcasts" | "spotify" | "youtube" | "broadcast";
|
|
11
|
+
export interface MasteringConfig {
|
|
12
|
+
/** Target integrated loudness in LUFS. */
|
|
13
|
+
integratedLUFS: number;
|
|
14
|
+
/** True-peak ceiling in dBTP (negative). */
|
|
15
|
+
truePeakDBTP: number;
|
|
16
|
+
/** Maximum loudness gain applied while reaching the target, in dB. */
|
|
17
|
+
maxLoudnessGainDB: number;
|
|
18
|
+
}
|
|
19
|
+
/** A loudness target: a named preset, an explicit config, or `bypass`/`null` for raw output. */
|
|
20
|
+
export type MasteringSpec = MasteringPreset | "bypass" | "off" | null | false | undefined | ({
|
|
21
|
+
integratedLUFS: number;
|
|
22
|
+
} & Partial<MasteringConfig>);
|
|
23
|
+
export type ResolvedMastering = ({
|
|
24
|
+
enabled: true;
|
|
25
|
+
} & MasteringConfig) | {
|
|
26
|
+
enabled: false;
|
|
27
|
+
};
|
|
28
|
+
/** Loudness presets matching `Clear.LoudnessPreset` in the Swift package. */
|
|
29
|
+
export declare const MASTERING_PRESETS: Record<MasteringPreset, MasteringConfig>;
|
|
30
|
+
/** Resolve a mastering spec (preset name, config object, or `bypass`/`null`) to a normalized shape. */
|
|
31
|
+
export declare function resolveMastering(spec: MasteringSpec): ResolvedMastering;
|
package/dist/r128.js
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
//
|
|
2
|
+
// EBU R128 / ITU-R BS.1770-4 integrated LUFS + look-ahead true-peak
|
|
3
|
+
// limiter. Non-streaming (buffers in RAM); K-weighting coefficients are
|
|
4
|
+
// the BS.1770-4 reference filters at 48 kHz.
|
|
5
|
+
const KWEIGHT_PRE_B = [1.53512485958697, -2.69169618940638, 1.19839281085285];
|
|
6
|
+
const KWEIGHT_PRE_A = [1.0, -1.69065929318241, 0.73248077421585];
|
|
7
|
+
const KWEIGHT_RLB_B = [1.0, -2.0, 1.0];
|
|
8
|
+
const KWEIGHT_RLB_A = [1.0, -1.99004745483398, 0.99007225036621];
|
|
9
|
+
/** Direct-form-I biquad. Returns a new Float32Array. */
|
|
10
|
+
function biquad(x, b, a) {
|
|
11
|
+
const a0 = a[0];
|
|
12
|
+
const b0 = b[0] / a0;
|
|
13
|
+
const b1 = b[1] / a0;
|
|
14
|
+
const b2 = b[2] / a0;
|
|
15
|
+
const a1 = a[1] / a0;
|
|
16
|
+
const a2 = a[2] / a0;
|
|
17
|
+
const n = x.length;
|
|
18
|
+
const y = new Float32Array(n);
|
|
19
|
+
let x1 = 0;
|
|
20
|
+
let x2 = 0;
|
|
21
|
+
let y1 = 0;
|
|
22
|
+
let y2 = 0;
|
|
23
|
+
for (let i = 0; i < n; i++) {
|
|
24
|
+
const xi = x[i];
|
|
25
|
+
const yi = b0 * xi + b1 * x1 + b2 * x2 - a1 * y1 - a2 * y2;
|
|
26
|
+
y[i] = yi;
|
|
27
|
+
x2 = x1;
|
|
28
|
+
x1 = xi;
|
|
29
|
+
y2 = y1;
|
|
30
|
+
y1 = yi;
|
|
31
|
+
}
|
|
32
|
+
return y;
|
|
33
|
+
}
|
|
34
|
+
/** BS.1770-gated integrated LUFS for a single (mono) buffer. `-Infinity` for silence. */
|
|
35
|
+
export function measureLUFS(audio, sampleRate) {
|
|
36
|
+
const blockLen = Math.round(0.4 * sampleRate);
|
|
37
|
+
const blockHop = Math.round(0.1 * sampleRate);
|
|
38
|
+
if (audio.length < blockLen)
|
|
39
|
+
return -Infinity;
|
|
40
|
+
const pre = biquad(audio, KWEIGHT_PRE_B, KWEIGHT_PRE_A);
|
|
41
|
+
const kw = biquad(pre, KWEIGHT_RLB_B, KWEIGHT_RLB_A);
|
|
42
|
+
const blocks = [];
|
|
43
|
+
for (let i = 0; i + blockLen <= kw.length; i += blockHop) {
|
|
44
|
+
let sq = 0;
|
|
45
|
+
for (let j = 0; j < blockLen; j++) {
|
|
46
|
+
const v = kw[i + j];
|
|
47
|
+
sq += v * v;
|
|
48
|
+
}
|
|
49
|
+
const ms = sq / blockLen;
|
|
50
|
+
blocks.push(ms > 1e-12 ? -0.691 + 10 * Math.log10(ms) : -Infinity);
|
|
51
|
+
}
|
|
52
|
+
return gatedIntegrated(blocks);
|
|
53
|
+
}
|
|
54
|
+
function gatedIntegrated(blocks) {
|
|
55
|
+
const absGated = blocks.filter((b) => b > -70.0);
|
|
56
|
+
if (!absGated.length)
|
|
57
|
+
return -Infinity;
|
|
58
|
+
let sumE = 0;
|
|
59
|
+
for (const b of absGated)
|
|
60
|
+
sumE += Math.pow(10, b / 10);
|
|
61
|
+
const meanE = sumE / absGated.length;
|
|
62
|
+
const relThresh = 10 * Math.log10(meanE) - 10;
|
|
63
|
+
const relGated = absGated.filter((b) => b > relThresh);
|
|
64
|
+
if (!relGated.length)
|
|
65
|
+
return -Infinity;
|
|
66
|
+
let sumE2 = 0;
|
|
67
|
+
for (const b of relGated)
|
|
68
|
+
sumE2 += Math.pow(10, b / 10);
|
|
69
|
+
return 10 * Math.log10(sumE2 / relGated.length);
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Look-ahead true-peak limiter (5 ms look-ahead, 50 ms release).
|
|
73
|
+
* @param ceilDBTP ceiling in dBFS (negative — e.g. -1.5)
|
|
74
|
+
*/
|
|
75
|
+
export function applyLimiter(audio, sampleRate, ceilDBTP) {
|
|
76
|
+
const n = audio.length;
|
|
77
|
+
if (n === 0)
|
|
78
|
+
return audio;
|
|
79
|
+
const ceil = Math.pow(10, ceilDBTP / 20);
|
|
80
|
+
const lookahead = Math.round(0.005 * sampleRate);
|
|
81
|
+
const releaseCoef = Math.exp(-1 / (sampleRate * 0.05));
|
|
82
|
+
const out = new Float32Array(n);
|
|
83
|
+
const ax = new Float32Array(n);
|
|
84
|
+
for (let i = 0; i < n; i++)
|
|
85
|
+
ax[i] = audio[i] >= 0 ? audio[i] : -audio[i];
|
|
86
|
+
const dq = new Int32Array(n + 1);
|
|
87
|
+
let head = 0;
|
|
88
|
+
let tail = 0;
|
|
89
|
+
const seedN = Math.min(lookahead, n);
|
|
90
|
+
for (let j = 0; j < seedN; j++) {
|
|
91
|
+
const vj = ax[j];
|
|
92
|
+
while (tail > head && ax[dq[tail - 1]] <= vj)
|
|
93
|
+
tail--;
|
|
94
|
+
dq[tail++] = j;
|
|
95
|
+
}
|
|
96
|
+
let env = 1.0;
|
|
97
|
+
for (let i = 0; i < n; i++) {
|
|
98
|
+
const end = i + lookahead;
|
|
99
|
+
if (end < n) {
|
|
100
|
+
const ve = ax[end];
|
|
101
|
+
while (tail > head && ax[dq[tail - 1]] <= ve)
|
|
102
|
+
tail--;
|
|
103
|
+
dq[tail++] = end;
|
|
104
|
+
}
|
|
105
|
+
while (head < tail && dq[head] < i)
|
|
106
|
+
head++;
|
|
107
|
+
const maxAhead = head < tail ? ax[dq[head]] : 0;
|
|
108
|
+
if (maxAhead > ceil) {
|
|
109
|
+
const required = ceil / maxAhead;
|
|
110
|
+
if (required < env)
|
|
111
|
+
env = required;
|
|
112
|
+
}
|
|
113
|
+
out[i] = audio[i] * env;
|
|
114
|
+
env = 1.0 - (1.0 - env) * releaseCoef;
|
|
115
|
+
}
|
|
116
|
+
return out;
|
|
117
|
+
}
|
|
118
|
+
/** Model-output attenuation in dB — feeds the mastering gain calc. */
|
|
119
|
+
export const MODEL_ATTENUATION_DB = 0.7;
|
|
120
|
+
/** Loudness presets matching `Clear.LoudnessPreset` in the Swift package. */
|
|
121
|
+
export const MASTERING_PRESETS = {
|
|
122
|
+
applePodcasts: { integratedLUFS: -19.0, truePeakDBTP: -1.5, maxLoudnessGainDB: 9.0 },
|
|
123
|
+
spotify: { integratedLUFS: -14.0, truePeakDBTP: -1.5, maxLoudnessGainDB: 9.0 },
|
|
124
|
+
youtube: { integratedLUFS: -14.0, truePeakDBTP: -1.5, maxLoudnessGainDB: 9.0 },
|
|
125
|
+
broadcast: { integratedLUFS: -23.0, truePeakDBTP: -1.5, maxLoudnessGainDB: 9.0 },
|
|
126
|
+
};
|
|
127
|
+
/** Resolve a mastering spec (preset name, config object, or `bypass`/`null`) to a normalized shape. */
|
|
128
|
+
export function resolveMastering(spec) {
|
|
129
|
+
if (spec === null || spec === undefined || spec === false || spec === "bypass" || spec === "off") {
|
|
130
|
+
return { enabled: false };
|
|
131
|
+
}
|
|
132
|
+
if (typeof spec === "string") {
|
|
133
|
+
const p = MASTERING_PRESETS[spec];
|
|
134
|
+
if (!p)
|
|
135
|
+
throw new Error(`unknown mastering preset: ${spec}`);
|
|
136
|
+
return { ...p, enabled: true };
|
|
137
|
+
}
|
|
138
|
+
const p = { ...MASTERING_PRESETS.applePodcasts, ...spec };
|
|
139
|
+
return { ...p, enabled: true };
|
|
140
|
+
}
|
package/dist/stft.d.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export declare const FFT_SIZE = 960;
|
|
2
|
+
export declare const HOP_SIZE = 480;
|
|
3
|
+
export declare const N_FREQ: number;
|
|
4
|
+
export interface STFTResult {
|
|
5
|
+
/** [nFrames, nFreq] row-major real part. */
|
|
6
|
+
real: Float32Array;
|
|
7
|
+
/** [nFrames, nFreq] row-major imaginary part. */
|
|
8
|
+
imag: Float32Array;
|
|
9
|
+
nFrames: number;
|
|
10
|
+
}
|
|
11
|
+
export declare class STFT {
|
|
12
|
+
readonly fftSize: number;
|
|
13
|
+
readonly hopSize: number;
|
|
14
|
+
readonly nFreq: number;
|
|
15
|
+
private readonly window;
|
|
16
|
+
private readonly wnorm;
|
|
17
|
+
private readonly fft;
|
|
18
|
+
private readonly _rIn;
|
|
19
|
+
private readonly _iIn;
|
|
20
|
+
private readonly _rOut;
|
|
21
|
+
private readonly _iOut;
|
|
22
|
+
constructor(fftSize?: number, hopSize?: number);
|
|
23
|
+
forward(audio: Float32Array): STFTResult;
|
|
24
|
+
/** Inverse STFT. `real`/`imag` are [nFrames, nFreq]; prePad is trimmed off the front. */
|
|
25
|
+
inverse(real: Float32Array, imag: Float32Array, nFrames: number): Float32Array;
|
|
26
|
+
}
|
package/dist/stft.js
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
//
|
|
2
|
+
// STFT / ISTFT: Vorbis window, n_fft=960, hop=480, forward gain
|
|
3
|
+
// wnorm = 2·hop/n_fft² = 1/960. Pre-pads (n_fft − hop) zeros; trims them
|
|
4
|
+
// off the inverse output. Unnormalized DFT both directions — wnorm plus
|
|
5
|
+
// the squared-Vorbis COLA closes the round-trip at unity.
|
|
6
|
+
import { BluesteinFFT } from "./fft.js";
|
|
7
|
+
export const FFT_SIZE = 960;
|
|
8
|
+
export const HOP_SIZE = 480;
|
|
9
|
+
export const N_FREQ = FFT_SIZE / 2 + 1;
|
|
10
|
+
function vorbisWindow(N) {
|
|
11
|
+
const w = new Float32Array(N);
|
|
12
|
+
const halfN = N / 2;
|
|
13
|
+
for (let n = 0; n < N; n++) {
|
|
14
|
+
const s = Math.sin((0.5 * Math.PI * (n + 0.5)) / halfN);
|
|
15
|
+
w[n] = Math.sin(0.5 * Math.PI * s * s);
|
|
16
|
+
}
|
|
17
|
+
return w;
|
|
18
|
+
}
|
|
19
|
+
export class STFT {
|
|
20
|
+
fftSize;
|
|
21
|
+
hopSize;
|
|
22
|
+
nFreq;
|
|
23
|
+
window;
|
|
24
|
+
wnorm;
|
|
25
|
+
fft;
|
|
26
|
+
_rIn;
|
|
27
|
+
_iIn;
|
|
28
|
+
_rOut;
|
|
29
|
+
_iOut;
|
|
30
|
+
constructor(fftSize = FFT_SIZE, hopSize = HOP_SIZE) {
|
|
31
|
+
this.fftSize = fftSize;
|
|
32
|
+
this.hopSize = hopSize;
|
|
33
|
+
this.nFreq = fftSize / 2 + 1;
|
|
34
|
+
this.window = vorbisWindow(fftSize);
|
|
35
|
+
this.wnorm = (2 * hopSize) / (fftSize * fftSize);
|
|
36
|
+
this.fft = new BluesteinFFT(fftSize);
|
|
37
|
+
this._rIn = new Float64Array(fftSize);
|
|
38
|
+
this._iIn = new Float64Array(fftSize);
|
|
39
|
+
this._rOut = new Float64Array(fftSize);
|
|
40
|
+
this._iOut = new Float64Array(fftSize);
|
|
41
|
+
}
|
|
42
|
+
forward(audio) {
|
|
43
|
+
const { fftSize, hopSize, nFreq, window, wnorm, fft } = this;
|
|
44
|
+
const prePad = fftSize - hopSize;
|
|
45
|
+
const paddedLen = prePad + audio.length;
|
|
46
|
+
if (paddedLen < fftSize) {
|
|
47
|
+
return { real: new Float32Array(0), imag: new Float32Array(0), nFrames: 0 };
|
|
48
|
+
}
|
|
49
|
+
const padded = new Float32Array(paddedLen);
|
|
50
|
+
padded.set(audio, prePad);
|
|
51
|
+
const nFrames = (((paddedLen - fftSize) / hopSize) | 0) + 1;
|
|
52
|
+
const real = new Float32Array(nFrames * nFreq);
|
|
53
|
+
const imag = new Float32Array(nFrames * nFreq);
|
|
54
|
+
const rIn = this._rIn;
|
|
55
|
+
const iIn = this._iIn;
|
|
56
|
+
const rOut = this._rOut;
|
|
57
|
+
const iOut = this._iOut;
|
|
58
|
+
for (let t = 0; t < nFrames; t++) {
|
|
59
|
+
const off = t * hopSize;
|
|
60
|
+
for (let i = 0; i < fftSize; i++) {
|
|
61
|
+
rIn[i] = padded[off + i] * window[i];
|
|
62
|
+
iIn[i] = 0;
|
|
63
|
+
}
|
|
64
|
+
fft.transform(rIn, iIn, rOut, iOut, false);
|
|
65
|
+
const base = t * nFreq;
|
|
66
|
+
for (let k = 0; k < nFreq; k++) {
|
|
67
|
+
real[base + k] = rOut[k] * wnorm;
|
|
68
|
+
imag[base + k] = iOut[k] * wnorm;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return { real, imag, nFrames };
|
|
72
|
+
}
|
|
73
|
+
/** Inverse STFT. `real`/`imag` are [nFrames, nFreq]; prePad is trimmed off the front. */
|
|
74
|
+
inverse(real, imag, nFrames) {
|
|
75
|
+
const { fftSize, hopSize, nFreq, window, fft } = this;
|
|
76
|
+
const prePad = fftSize - hopSize;
|
|
77
|
+
const rawLen = (nFrames - 1) * hopSize + fftSize;
|
|
78
|
+
const out = new Float32Array(rawLen);
|
|
79
|
+
const rIn = this._rIn;
|
|
80
|
+
const iIn = this._iIn;
|
|
81
|
+
const rOut = this._rOut;
|
|
82
|
+
const iOut = this._iOut;
|
|
83
|
+
const mirrorLen = fftSize - nFreq;
|
|
84
|
+
for (let t = 0; t < nFrames; t++) {
|
|
85
|
+
const base = t * nFreq;
|
|
86
|
+
for (let k = 0; k < nFreq; k++) {
|
|
87
|
+
rIn[k] = real[base + k];
|
|
88
|
+
iIn[k] = imag[base + k];
|
|
89
|
+
}
|
|
90
|
+
for (let m = 0; m < mirrorLen; m++) {
|
|
91
|
+
const srcK = mirrorLen - m;
|
|
92
|
+
rIn[nFreq + m] = real[base + srcK];
|
|
93
|
+
iIn[nFreq + m] = -imag[base + srcK];
|
|
94
|
+
}
|
|
95
|
+
fft.transform(rIn, iIn, rOut, iOut, true);
|
|
96
|
+
const off = t * hopSize;
|
|
97
|
+
for (let i = 0; i < fftSize; i++) {
|
|
98
|
+
out[off + i] += rOut[i] * window[i];
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
if (rawLen > prePad)
|
|
102
|
+
return out.subarray(prePad, rawLen);
|
|
103
|
+
return out;
|
|
104
|
+
}
|
|
105
|
+
}
|
package/dist/wav.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Encode mono PCM (`[-1, 1]`) to 16-bit WAV bytes.
|
|
3
|
+
*
|
|
4
|
+
* In the browser, wrap for download: `new Blob([encodeWav(pcm, 48000)], { type: "audio/wav" })`.
|
|
5
|
+
*/
|
|
6
|
+
export declare function encodeWav(samples: Float32Array, sampleRate: number): Uint8Array;
|
|
7
|
+
export interface DecodedWav {
|
|
8
|
+
/** Mono PCM in `[-1, 1]`. */
|
|
9
|
+
samples: Float32Array;
|
|
10
|
+
sampleRate: number;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Decode a PCM/float WAV (the Node input path — no Web Audio). Supports
|
|
14
|
+
* 16/24/32-bit integer and 32-bit float, any channel count (downmixed to mono).
|
|
15
|
+
* For compressed formats (mp3, m4a, …) decode upstream and pass a Float32Array.
|
|
16
|
+
*/
|
|
17
|
+
export declare function decodeWav(input: ArrayBuffer | ArrayBufferView): DecodedWav;
|
package/dist/wav.js
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Minimal mono WAV codec. `encodeWav` writes 16-bit PCM bytes (wrap in a
|
|
3
|
+
// Blob for browser download); `decodeWav` reads PCM int16/24/32 and
|
|
4
|
+
// float32 mono/multi-channel WAVs (downmixing to mono) for the Node path.
|
|
5
|
+
/**
|
|
6
|
+
* Encode mono PCM (`[-1, 1]`) to 16-bit WAV bytes.
|
|
7
|
+
*
|
|
8
|
+
* In the browser, wrap for download: `new Blob([encodeWav(pcm, 48000)], { type: "audio/wav" })`.
|
|
9
|
+
*/
|
|
10
|
+
export function encodeWav(samples, sampleRate) {
|
|
11
|
+
const numSamples = samples.length;
|
|
12
|
+
const byteRate = sampleRate * 2;
|
|
13
|
+
const dataSize = numSamples * 2;
|
|
14
|
+
const buffer = new ArrayBuffer(44 + dataSize);
|
|
15
|
+
const view = new DataView(buffer);
|
|
16
|
+
writeString(view, 0, "RIFF");
|
|
17
|
+
view.setUint32(4, 36 + dataSize, true);
|
|
18
|
+
writeString(view, 8, "WAVE");
|
|
19
|
+
writeString(view, 12, "fmt ");
|
|
20
|
+
view.setUint32(16, 16, true);
|
|
21
|
+
view.setUint16(20, 1, true); // PCM
|
|
22
|
+
view.setUint16(22, 1, true); // channels
|
|
23
|
+
view.setUint32(24, sampleRate, true);
|
|
24
|
+
view.setUint32(28, byteRate, true);
|
|
25
|
+
view.setUint16(32, 2, true); // block align
|
|
26
|
+
view.setUint16(34, 16, true); // bits per sample
|
|
27
|
+
writeString(view, 36, "data");
|
|
28
|
+
view.setUint32(40, dataSize, true);
|
|
29
|
+
let off = 44;
|
|
30
|
+
for (let i = 0; i < numSamples; i++) {
|
|
31
|
+
const v = Math.max(-1, Math.min(1, samples[i]));
|
|
32
|
+
view.setInt16(off, v < 0 ? v * 0x8000 : v * 0x7fff, true);
|
|
33
|
+
off += 2;
|
|
34
|
+
}
|
|
35
|
+
return new Uint8Array(buffer);
|
|
36
|
+
}
|
|
37
|
+
function writeString(view, offset, str) {
|
|
38
|
+
for (let i = 0; i < str.length; i++)
|
|
39
|
+
view.setUint8(offset + i, str.charCodeAt(i));
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Decode a PCM/float WAV (the Node input path — no Web Audio). Supports
|
|
43
|
+
* 16/24/32-bit integer and 32-bit float, any channel count (downmixed to mono).
|
|
44
|
+
* For compressed formats (mp3, m4a, …) decode upstream and pass a Float32Array.
|
|
45
|
+
*/
|
|
46
|
+
export function decodeWav(input) {
|
|
47
|
+
const bytes = input instanceof ArrayBuffer
|
|
48
|
+
? new Uint8Array(input)
|
|
49
|
+
: new Uint8Array(input.buffer, input.byteOffset, input.byteLength);
|
|
50
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
51
|
+
if (str(view, 0, 4) !== "RIFF" || str(view, 8, 4) !== "WAVE") {
|
|
52
|
+
throw new Error("clear: not a RIFF/WAVE file");
|
|
53
|
+
}
|
|
54
|
+
let fmtFound = false;
|
|
55
|
+
let audioFormat = 1;
|
|
56
|
+
let channels = 1;
|
|
57
|
+
let sampleRate = 48_000;
|
|
58
|
+
let bitsPerSample = 16;
|
|
59
|
+
let dataOffset = -1;
|
|
60
|
+
let dataSize = 0;
|
|
61
|
+
let pos = 12;
|
|
62
|
+
while (pos + 8 <= view.byteLength) {
|
|
63
|
+
const id = str(view, pos, 4);
|
|
64
|
+
const size = view.getUint32(pos + 4, true);
|
|
65
|
+
const body = pos + 8;
|
|
66
|
+
if (id === "fmt ") {
|
|
67
|
+
audioFormat = view.getUint16(body, true);
|
|
68
|
+
channels = view.getUint16(body + 2, true);
|
|
69
|
+
sampleRate = view.getUint32(body + 4, true);
|
|
70
|
+
bitsPerSample = view.getUint16(body + 14, true);
|
|
71
|
+
fmtFound = true;
|
|
72
|
+
}
|
|
73
|
+
else if (id === "data") {
|
|
74
|
+
dataOffset = body;
|
|
75
|
+
dataSize = Math.min(size, view.byteLength - body);
|
|
76
|
+
}
|
|
77
|
+
pos = body + size + (size & 1); // chunks are word-aligned
|
|
78
|
+
}
|
|
79
|
+
if (!fmtFound || dataOffset < 0)
|
|
80
|
+
throw new Error("clear: WAV missing fmt/data chunk");
|
|
81
|
+
const isFloat = audioFormat === 3;
|
|
82
|
+
const bytesPerSample = bitsPerSample >> 3;
|
|
83
|
+
const frameSize = bytesPerSample * channels;
|
|
84
|
+
const numFrames = Math.floor(dataSize / frameSize);
|
|
85
|
+
const mono = new Float32Array(numFrames);
|
|
86
|
+
const invCh = 1 / channels;
|
|
87
|
+
for (let i = 0; i < numFrames; i++) {
|
|
88
|
+
let acc = 0;
|
|
89
|
+
const frame = dataOffset + i * frameSize;
|
|
90
|
+
for (let ch = 0; ch < channels; ch++) {
|
|
91
|
+
acc += readSample(view, frame + ch * bytesPerSample, bitsPerSample, isFloat);
|
|
92
|
+
}
|
|
93
|
+
mono[i] = acc * invCh;
|
|
94
|
+
}
|
|
95
|
+
return { samples: mono, sampleRate };
|
|
96
|
+
}
|
|
97
|
+
function readSample(view, off, bits, isFloat) {
|
|
98
|
+
if (isFloat) {
|
|
99
|
+
return bits === 64 ? view.getFloat64(off, true) : view.getFloat32(off, true);
|
|
100
|
+
}
|
|
101
|
+
switch (bits) {
|
|
102
|
+
case 8:
|
|
103
|
+
return (view.getUint8(off) - 128) / 128;
|
|
104
|
+
case 16:
|
|
105
|
+
return view.getInt16(off, true) / 0x8000;
|
|
106
|
+
case 24: {
|
|
107
|
+
const b0 = view.getUint8(off);
|
|
108
|
+
const b1 = view.getUint8(off + 1);
|
|
109
|
+
const b2 = view.getUint8(off + 2);
|
|
110
|
+
let v = b0 | (b1 << 8) | (b2 << 16);
|
|
111
|
+
if (v & 0x800000)
|
|
112
|
+
v |= ~0xffffff; // sign-extend
|
|
113
|
+
return v / 0x800000;
|
|
114
|
+
}
|
|
115
|
+
case 32:
|
|
116
|
+
return view.getInt32(off, true) / 0x80000000;
|
|
117
|
+
default:
|
|
118
|
+
throw new Error(`clear: unsupported WAV bit depth ${bits}`);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
function str(view, off, len) {
|
|
122
|
+
let s = "";
|
|
123
|
+
for (let i = 0; i < len; i++)
|
|
124
|
+
s += String.fromCharCode(view.getUint8(off + i));
|
|
125
|
+
return s;
|
|
126
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@desert-ant-labs/clear",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "On-device speech enhancement (denoise + dereverb + podcast mastering) for Node and the browser.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.node.js",
|
|
7
|
+
"types": "./dist/index.node.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"browser": {
|
|
11
|
+
"types": "./dist/index.browser.d.ts",
|
|
12
|
+
"default": "./dist/index.browser.js"
|
|
13
|
+
},
|
|
14
|
+
"worker": {
|
|
15
|
+
"types": "./dist/index.browser.d.ts",
|
|
16
|
+
"default": "./dist/index.browser.js"
|
|
17
|
+
},
|
|
18
|
+
"workerd": {
|
|
19
|
+
"types": "./dist/index.browser.d.ts",
|
|
20
|
+
"default": "./dist/index.browser.js"
|
|
21
|
+
},
|
|
22
|
+
"edge-light": {
|
|
23
|
+
"types": "./dist/index.browser.d.ts",
|
|
24
|
+
"default": "./dist/index.browser.js"
|
|
25
|
+
},
|
|
26
|
+
"node": {
|
|
27
|
+
"types": "./dist/index.node.d.ts",
|
|
28
|
+
"default": "./dist/index.node.js"
|
|
29
|
+
},
|
|
30
|
+
"default": {
|
|
31
|
+
"types": "./dist/index.node.d.ts",
|
|
32
|
+
"default": "./dist/index.node.js"
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
"./core": {
|
|
36
|
+
"types": "./dist/model.d.ts",
|
|
37
|
+
"default": "./dist/model.js"
|
|
38
|
+
},
|
|
39
|
+
"./package.json": "./package.json"
|
|
40
|
+
},
|
|
41
|
+
"files": [
|
|
42
|
+
"dist",
|
|
43
|
+
"THIRD_PARTY_NOTICES.md"
|
|
44
|
+
],
|
|
45
|
+
"scripts": {
|
|
46
|
+
"build": "tsc",
|
|
47
|
+
"test": "node --import tsx --test test/*.test.ts",
|
|
48
|
+
"lint:publint": "publint --strict",
|
|
49
|
+
"lint:attw": "attw --pack . --profile esm-only",
|
|
50
|
+
"check:pkg": "npm run build && npm run lint:publint && npm run lint:attw",
|
|
51
|
+
"prepare": "npm run build"
|
|
52
|
+
},
|
|
53
|
+
"keywords": [
|
|
54
|
+
"speech-enhancement",
|
|
55
|
+
"denoise",
|
|
56
|
+
"dereverb",
|
|
57
|
+
"audio",
|
|
58
|
+
"podcast",
|
|
59
|
+
"loudness",
|
|
60
|
+
"on-device",
|
|
61
|
+
"onnx"
|
|
62
|
+
],
|
|
63
|
+
"license": "SEE LICENSE IN LICENSE.md",
|
|
64
|
+
"repository": {
|
|
65
|
+
"type": "git",
|
|
66
|
+
"url": "git+https://github.com/Desert-Ant-Labs/clear-js.git"
|
|
67
|
+
},
|
|
68
|
+
"homepage": "https://github.com/Desert-Ant-Labs/clear-js",
|
|
69
|
+
"engines": {
|
|
70
|
+
"node": ">=18"
|
|
71
|
+
},
|
|
72
|
+
"sideEffects": false,
|
|
73
|
+
"publishConfig": {
|
|
74
|
+
"access": "public"
|
|
75
|
+
},
|
|
76
|
+
"peerDependencies": {
|
|
77
|
+
"onnxruntime-node": ">=1.20",
|
|
78
|
+
"onnxruntime-web": ">=1.20"
|
|
79
|
+
},
|
|
80
|
+
"peerDependenciesMeta": {
|
|
81
|
+
"onnxruntime-node": {
|
|
82
|
+
"optional": true
|
|
83
|
+
},
|
|
84
|
+
"onnxruntime-web": {
|
|
85
|
+
"optional": true
|
|
86
|
+
}
|
|
87
|
+
},
|
|
88
|
+
"devDependencies": {
|
|
89
|
+
"@arethetypeswrong/cli": "^0.18",
|
|
90
|
+
"@types/node": "^22",
|
|
91
|
+
"publint": "^0.3",
|
|
92
|
+
"tsx": "^4",
|
|
93
|
+
"typescript": "~5.6.3"
|
|
94
|
+
}
|
|
95
|
+
}
|