@audio/denoise-core 0.1.2 → 0.1.4

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.
Files changed (6) hide show
  1. package/index.js +0 -3
  2. package/package.json +4 -13
  3. package/stft.js +4 -1
  4. package/ar.js +0 -131
  5. package/noise.js +0 -119
  6. package/vad.js +0 -3
package/index.js CHANGED
@@ -1,6 +1,3 @@
1
1
  export * from './stft.js'
2
2
  export * from './util.js'
3
- export * from './noise.js'
4
- export * from './vad.js'
5
- export * from './ar.js'
6
3
  export * from './quality.js'
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@audio/denoise-core",
3
- "version": "0.1.2",
4
- "description": "Internal glue for @audio/denoise-* atoms — STFT (batch/stream/analyse), noise estimation (min-stats, IMCRA), AR modeling, quality metrics. VAD lives in @audio/vad (re-exported here).",
3
+ "version": "0.1.4",
4
+ "description": "Internal glue for @audio/denoise-* atoms — STFT (batch/stream/analyse), quality metrics, dB/linear + stream-writer utils. VAD @audio/vad, noise estimation → @audio/noise-estimate, LPC/AR → @audio/lpc.",
5
5
  "type": "module",
6
6
  "sideEffects": false,
7
7
  "main": "index.js",
@@ -10,31 +10,22 @@
10
10
  "./package.json": "./package.json",
11
11
  "./stft": "./stft.js",
12
12
  "./util": "./util.js",
13
- "./noise": "./noise.js",
14
- "./vad": "./vad.js",
15
- "./ar": "./ar.js",
16
13
  "./quality": "./quality.js"
17
14
  },
18
15
  "files": [
19
16
  "index.js",
20
17
  "stft.js",
21
18
  "util.js",
22
- "noise.js",
23
- "vad.js",
24
- "ar.js",
25
19
  "quality.js"
26
20
  ],
27
21
  "dependencies": {
28
- "fourier-transform": "^2.0.0",
29
- "@audio/vad": "^1.0.0"
22
+ "fourier-transform": "^2.0.0"
30
23
  },
31
24
  "keywords": [
32
25
  "audio",
33
26
  "dsp",
34
27
  "denoise",
35
- "stft",
36
- "vad",
37
- "noise-estimation"
28
+ "stft"
38
29
  ],
39
30
  "license": "MIT",
40
31
  "author": "Dmitry Iv. <dfcreative@gmail.com>",
package/stft.js CHANGED
@@ -55,7 +55,10 @@ export function stftBatch(data, process, opts) {
55
55
  let sc = scratch(N, half)
56
56
  let pos = 0
57
57
 
58
- while (pos + N <= outLen + hop) {
58
+ // Start a frame at every hop up to the last sample so the tail keeps the
59
+ // steady-state overlap count — bounding by `pos + N <= outLen` would stop
60
+ // N−hop samples early (tail under-normalized) and drop inputs shorter than N.
61
+ while (pos < outLen) {
59
62
  let sf = frame(data, pos, win, half, process, state, ctx, sc)
60
63
  for (let i = 0; i < N && pos + i < outLen; i++) {
61
64
  out[pos + i] += sf[i] * win[i]
package/ar.js DELETED
@@ -1,131 +0,0 @@
1
- // Auto-regressive modelling for click/click-burst interpolation and de-clip extrapolation.
2
- // - Levinson-Durbin recursion → AR(p) coefficients from autocorrelation
3
- // - LS interpolation of missing samples from neighbouring AR predictions
4
- // - Forward AR extrapolation (used by de-clip)
5
- //
6
- // Reference: Godsill & Rayner (1998), "Digital Audio Restoration", §5.
7
-
8
- // Biased autocorrelation R[0..p]. Bias is preferable for short windows (Toeplitz PSD).
9
- export function autocorr(x, p) {
10
- let n = x.length, R = new Float64Array(p + 1)
11
- for (let k = 0; k <= p; k++) {
12
- let s = 0
13
- for (let i = 0; i + k < n; i++) s += x[i] * x[i + k]
14
- R[k] = s
15
- }
16
- return R
17
- }
18
-
19
- // Levinson-Durbin: solve Toeplitz Yule-Walker for AR(p) coefficients.
20
- // Returns { a: Float64Array(p+1), e: residual variance }. a[0] = 1 by convention.
21
- export function levinson(R, p) {
22
- let a = new Float64Array(p + 1)
23
- let prev = new Float64Array(p + 1)
24
- a[0] = 1
25
- let e = R[0]
26
- if (e <= 0) return { a, e: 0 }
27
-
28
- for (let i = 1; i <= p; i++) {
29
- let k = -R[i]
30
- for (let j = 1; j < i; j++) k -= a[j] * R[i - j]
31
- k /= e
32
- for (let j = 0; j <= i; j++) prev[j] = a[j]
33
- a[i] = k
34
- for (let j = 1; j < i; j++) a[j] = prev[j] + k * prev[i - j]
35
- e *= 1 - k * k
36
- if (e <= 0) { e = 0; break }
37
- }
38
- return { a, e }
39
- }
40
-
41
- // Convenience: AR fit on a window.
42
- export function arFit(x, p) {
43
- return levinson(autocorr(x, p), p)
44
- }
45
-
46
- // Predict next sample via AR(p): x̂[n] = -∑ a[k]·x[n-k], k=1..p.
47
- export function arPredict(a, hist) {
48
- let p = a.length - 1, s = 0
49
- for (let k = 1; k <= p; k++) s -= a[k] * hist[hist.length - k]
50
- return s
51
- }
52
-
53
- // Forward AR extrapolation by m samples beyond context tail.
54
- // Used for de-clip: fit AR on un-clipped neighbourhood, project into clipped region.
55
- export function arExtrapolate(context, a, m) {
56
- let p = a.length - 1
57
- let buf = new Float64Array(p + m)
58
- for (let i = 0; i < p; i++) buf[i] = context[context.length - p + i]
59
- for (let i = 0; i < m; i++) {
60
- let s = 0
61
- for (let k = 1; k <= p; k++) s -= a[k] * buf[p + i - k]
62
- buf[p + i] = s
63
- }
64
- return buf.subarray(p)
65
- }
66
-
67
- // Least-squares interpolation of indices `gap` (sorted ints) inside x using AR(p).
68
- // Solves Bᵀ B · u = -Bᵀ A · k, where u = unknowns, k = knowns,
69
- // (B,A) split of the AR convolution matrix on (gap, ¬gap).
70
- //
71
- // Direct sparse Gauss-Seidel is enough for clusters up to ~50 samples; very small
72
- // gaps (≤8) reduce to a few iterations and are dominated by the AR fit cost itself.
73
- export function arInterpolate(x, gap, a) {
74
- let p = a.length - 1
75
- let n = x.length, m = gap.length
76
- if (m === 0) return x
77
-
78
- let inGap = new Uint8Array(n)
79
- for (let i = 0; i < m; i++) inGap[gap[i]] = 1
80
-
81
- // M = sum over t of (sum_{k:t-k∈gap} a[k] * a[k - (t - gap_j)]) — assemble m×m system implicitly.
82
- // For practicality, use Jacobi iteration: x_g = -∑_{j≠g} M[g,j]/M[g,g] · x_j + b/M[g,g]
83
- // with M[g,g] = ∑_k a[k]² for k where g+k≤n+p
84
- // and forcing term computed from neighbours.
85
- let mdiag = new Float64Array(m)
86
- for (let i = 0; i < m; i++) {
87
- let g = gap[i], s = 0
88
- for (let k = 0; k <= p; k++) {
89
- let t = g + k
90
- if (t >= 0 && t < n + p) s += a[k] * a[k]
91
- }
92
- mdiag[i] = s || 1
93
- }
94
-
95
- // initial: linear interpolation between gap boundaries
96
- for (let i = 0; i < m; i++) {
97
- let g = gap[i]
98
- let lo = g, hi = g
99
- while (lo > 0 && inGap[lo - 1]) lo--
100
- while (hi < n - 1 && inGap[hi + 1]) hi++
101
- let xLo = lo > 0 ? x[lo - 1] : 0
102
- let xHi = hi < n - 1 ? x[hi + 1] : 0
103
- x[g] = xLo + (xHi - xLo) * (g - lo + 1) / (hi - lo + 2)
104
- }
105
-
106
- // 30 Gauss-Seidel sweeps converge well past audible accuracy for short gaps
107
- let iter = 30
108
- for (let it = 0; it < iter; it++) {
109
- for (let i = 0; i < m; i++) {
110
- let g = gap[i]
111
- // residual r[t] = ∑_k a[k] x[t-k] for t = g..g+p; gradient wrt x[g] is
112
- // ∑_k a[k] r[g+k]; setting it to zero gives the update.
113
- let num = 0
114
- for (let k = 0; k <= p; k++) {
115
- let t = g + k
116
- if (t < 0 || t >= n + p) continue
117
- let rt = 0
118
- for (let j = 0; j <= p; j++) {
119
- let idx = t - j
120
- if (idx < 0 || idx >= n) continue
121
- rt += a[j] * x[idx]
122
- }
123
- // remove self-contribution so we can solve for x[g]
124
- rt -= a[k] * x[g]
125
- num -= a[k] * rt
126
- }
127
- x[g] = num / mdiag[i]
128
- }
129
- }
130
- return x
131
- }
package/noise.js DELETED
@@ -1,119 +0,0 @@
1
- // Noise PSD estimators feeding the statistical denoisers (Wiener, OM-LSA, MMSE).
2
- // - profile: average |X|² over a user-chosen quiet segment (manual baseline)
3
- // - minimumStatistics: Martin (2001) — track minima of smoothed |X|² in sliding window
4
- // - imcra: Cohen (2003) — Improved MCRA, two-iteration smoothing + speech-presence-driven
5
- //
6
- // All estimators are stateful: pass the same params object across frames in stream mode.
7
-
8
- import { stftAnalyse } from './stft.js'
9
-
10
- // One-shot batch profile from a quiet segment of `data`. Returns Float64Array(N/2+1).
11
- export function noiseProfile(data, opts = {}) {
12
- let N = opts.frameSize || 2048
13
- let hop = opts.hopSize || (N >> 2)
14
- let half = N >> 1
15
- let from = Math.max(0, opts.from ?? 0)
16
- let to = Math.min(data.length, opts.to ?? Math.min(data.length, from + N * 8))
17
- let seg = data.subarray(from, to)
18
- let psd = new Float64Array(half + 1)
19
- let count = 0
20
- stftAnalyse(seg, mag => {
21
- for (let k = 0; k <= half; k++) psd[k] += mag[k] * mag[k]
22
- count++
23
- }, { frameSize: N, hopSize: hop })
24
- let scale = count ? 1 / count : 0
25
- for (let k = 0; k <= half; k++) psd[k] *= scale
26
- return psd
27
- }
28
-
29
- // Minimum Statistics (Martin 2001) — frame-by-frame online updater.
30
- // Keeps a rolling D-frame minimum of smoothed PSD per bin; multiplies by a bias
31
- // compensation factor so the minimum tracks E{|N|²} rather than the lower-tail.
32
- //
33
- // Usage:
34
- // let est = minStats(half, { D: 96 })
35
- // stftAnalyse(data, m => est.update(m))
36
- // let psd = est.psd // current noise PSD
37
- //
38
- // D ≈ 1.5 s of frames at hop = N/4 ≈ 96 frames @ 44.1k / N=2048.
39
- export function minStats(half, opts = {}) {
40
- let D = opts.D || 96
41
- let alpha = opts.alpha ?? 0.7 // smoothing on PSD
42
- let bias = opts.bias ?? 1.5 // empirical comp from Martin §VII
43
- let smoothed = new Float64Array(half + 1)
44
- let psd = new Float64Array(half + 1)
45
- let buf = [] // ring of recent smoothed frames
46
-
47
- return {
48
- psd,
49
- update(mag) {
50
- let p = new Float64Array(half + 1)
51
- for (let k = 0; k <= half; k++) {
52
- let pk = mag[k] * mag[k]
53
- smoothed[k] = alpha * smoothed[k] + (1 - alpha) * pk
54
- p[k] = smoothed[k]
55
- }
56
- buf.push(p)
57
- if (buf.length > D) buf.shift()
58
- for (let k = 0; k <= half; k++) {
59
- let mn = Infinity
60
- for (let i = 0; i < buf.length; i++) if (buf[i][k] < mn) mn = buf[i][k]
61
- psd[k] = mn * bias
62
- }
63
- }
64
- }
65
- }
66
-
67
- // IMCRA — Improved Minima Controlled Recursive Averaging (Cohen 2003).
68
- // Drives noise PSD via speech-presence probability (SPP) so it stops updating
69
- // during voiced regions. SPP itself is supplied by the caller (see vad.js spp())
70
- // or computed internally from the smoothed-to-min PSD ratio.
71
- export function imcra(half, opts = {}) {
72
- let alpha = opts.alpha ?? 0.92
73
- let alphaD = opts.alphaD ?? 0.85
74
- let beta = opts.beta ?? 1.47
75
- let psd = new Float64Array(half + 1)
76
- let psdInit = false
77
- let smoothed = new Float64Array(half + 1)
78
- let mins = new Float64Array(half + 1)
79
- let prevMins = new Float64Array(half + 1)
80
- let frameCount = 0
81
- let resetEvery = opts.resetEvery || 80 // ≈ 0.9 s @ 44.1k, hop=512
82
-
83
- return {
84
- psd,
85
- update(mag, sppOverride) {
86
- let init = !psdInit
87
- for (let k = 0; k <= half; k++) {
88
- let pk = mag[k] * mag[k]
89
- smoothed[k] = init ? pk : alpha * smoothed[k] + (1 - alpha) * pk
90
- if (init || smoothed[k] < mins[k]) mins[k] = smoothed[k]
91
- if (!init && smoothed[k] < prevMins[k]) prevMins[k] = smoothed[k]
92
- }
93
- frameCount++
94
- if (frameCount >= resetEvery) {
95
- for (let k = 0; k <= half; k++) {
96
- let mn = Math.min(mins[k], prevMins[k])
97
- prevMins[k] = mins[k]
98
- mins[k] = smoothed[k]
99
- // bias-compensated minimum tracker
100
- if (init) psd[k] = mn * beta
101
- }
102
- frameCount = 0
103
- psdInit = true
104
- }
105
-
106
- if (!psdInit) return
107
-
108
- for (let k = 0; k <= half; k++) {
109
- let mn = Math.min(mins[k], prevMins[k]) * beta
110
- // SPP from local SNR vs minimum: high if smoothed >> minimum.
111
- let snr = smoothed[k] / Math.max(mn, 1e-30)
112
- let p = sppOverride !== undefined ? sppOverride : 1 / (1 + Math.exp(-3 * (snr - 5)))
113
- let alphaTilde = alphaD + (1 - alphaD) * p
114
- let pk = mag[k] * mag[k]
115
- psd[k] = alphaTilde * psd[k] + (1 - alphaTilde) * pk
116
- }
117
- }
118
- }
119
- }
package/vad.js DELETED
@@ -1,3 +0,0 @@
1
- // vad/spp/ddSnr were promoted to the standalone @audio/vad atom.
2
- // Re-exported here so restoration code (and the @audio/denoise umbrella) keep working.
3
- export * from '@audio/vad'