@audio/denoise-core 0.1.3 → 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 (4) hide show
  1. package/index.js +0 -1
  2. package/package.json +2 -4
  3. package/stft.js +4 -1
  4. package/ar.js +0 -131
package/index.js CHANGED
@@ -1,4 +1,3 @@
1
1
  export * from './stft.js'
2
2
  export * from './util.js'
3
- export * from './ar.js'
4
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.3",
4
- "description": "Internal glue for @audio/denoise-* atoms — STFT (batch/stream/analyse), AR modeling, quality metrics, dB/linear + stream-writer utils. VAD → @audio/vad, noise estimation → @audio/noise-estimate.",
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,14 +10,12 @@
10
10
  "./package.json": "./package.json",
11
11
  "./stft": "./stft.js",
12
12
  "./util": "./util.js",
13
- "./ar": "./ar.js",
14
13
  "./quality": "./quality.js"
15
14
  },
16
15
  "files": [
17
16
  "index.js",
18
17
  "stft.js",
19
18
  "util.js",
20
- "ar.js",
21
19
  "quality.js"
22
20
  ],
23
21
  "dependencies": {
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
- }