@audio/denoise-core 0.1.1 → 0.1.3
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/index.js +0 -2
- package/package.json +3 -9
- package/noise.js +0 -119
- package/vad.js +0 -91
package/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@audio/denoise-core",
|
|
3
|
-
"version": "0.1.
|
|
4
|
-
"description": "
|
|
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.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
7
7
|
"main": "index.js",
|
|
@@ -10,8 +10,6 @@
|
|
|
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
13
|
"./ar": "./ar.js",
|
|
16
14
|
"./quality": "./quality.js"
|
|
17
15
|
},
|
|
@@ -19,8 +17,6 @@
|
|
|
19
17
|
"index.js",
|
|
20
18
|
"stft.js",
|
|
21
19
|
"util.js",
|
|
22
|
-
"noise.js",
|
|
23
|
-
"vad.js",
|
|
24
20
|
"ar.js",
|
|
25
21
|
"quality.js"
|
|
26
22
|
],
|
|
@@ -31,9 +27,7 @@
|
|
|
31
27
|
"audio",
|
|
32
28
|
"dsp",
|
|
33
29
|
"denoise",
|
|
34
|
-
"stft"
|
|
35
|
-
"vad",
|
|
36
|
-
"noise-estimation"
|
|
30
|
+
"stft"
|
|
37
31
|
],
|
|
38
32
|
"license": "MIT",
|
|
39
33
|
"author": "Dmitry Iv. <dfcreative@gmail.com>",
|
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,91 +0,0 @@
|
|
|
1
|
-
// Voice Activity Detection + Speech Presence Probability.
|
|
2
|
-
// - vad: hard 0/1 decision per frame from energy + spectral flatness + ZCR
|
|
3
|
-
// - spp: soft probability per bin from a-priori SNR (drives OM-LSA, IMCRA)
|
|
4
|
-
//
|
|
5
|
-
// Use vad() for gating decisions (debreath, gate); spp() for spectral denoise gain.
|
|
6
|
-
|
|
7
|
-
import { stftAnalyse } from './stft.js'
|
|
8
|
-
import { lin2db } from './util.js'
|
|
9
|
-
|
|
10
|
-
// Returns { active: Uint8Array(frames), times: Float32Array(frames) of frame-start sec }.
|
|
11
|
-
// Active iff energy is N dB above the rolling noise floor AND spectral flatness < flatTh.
|
|
12
|
-
//
|
|
13
|
-
// Floor estimation: minimum of energy over a sliding D-frame window with bias
|
|
14
|
-
// correction — Martin-style minimum tracking. This avoids the "speech eats its own
|
|
15
|
-
// floor" failure mode of plain exponential smoothing.
|
|
16
|
-
//
|
|
17
|
-
// flatTh ≈ 0.4 — speech is tonal (low flatness), noise is flat (high flatness).
|
|
18
|
-
export function vad(data, opts = {}) {
|
|
19
|
-
let N = opts.frameSize || 1024
|
|
20
|
-
let hop = opts.hopSize || (N >> 1)
|
|
21
|
-
let fs = opts.fs || 44100
|
|
22
|
-
let snrTh = opts.snrTh ?? 6 // dB above floor
|
|
23
|
-
let flatTh = opts.flatTh ?? 0.4 // spectral flatness threshold
|
|
24
|
-
let D = opts.window || 64 // frames in min-tracker window
|
|
25
|
-
let bias = opts.bias ?? 5 // dB — added to min to estimate true floor
|
|
26
|
-
|
|
27
|
-
let frames = Math.max(0, Math.floor((data.length - N) / hop) + 1)
|
|
28
|
-
let active = new Uint8Array(frames)
|
|
29
|
-
let times = new Float32Array(frames)
|
|
30
|
-
let energies = new Float64Array(frames)
|
|
31
|
-
let flats = new Float64Array(frames)
|
|
32
|
-
let i = 0
|
|
33
|
-
|
|
34
|
-
stftAnalyse(data, (mag, _phase, pos) => {
|
|
35
|
-
let half = mag.length - 1, e = 0
|
|
36
|
-
let logSum = 0, linSum = 0, nz = 0
|
|
37
|
-
for (let k = 1; k <= half; k++) {
|
|
38
|
-
let p = mag[k] * mag[k]
|
|
39
|
-
e += p
|
|
40
|
-
if (p > 1e-30) { logSum += Math.log(p); nz++ }
|
|
41
|
-
linSum += p
|
|
42
|
-
}
|
|
43
|
-
let geom = nz ? Math.exp(logSum / nz) : 0
|
|
44
|
-
let arith = linSum / Math.max(half, 1)
|
|
45
|
-
flats[i] = arith > 1e-30 ? geom / arith : 1
|
|
46
|
-
energies[i] = lin2db(Math.sqrt(e / N))
|
|
47
|
-
times[i] = pos / fs
|
|
48
|
-
i++
|
|
49
|
-
}, { frameSize: N, hopSize: hop })
|
|
50
|
-
|
|
51
|
-
// Global noise floor = 10th-percentile energy + bias. Robust on signals where
|
|
52
|
-
// any short window may be entirely speech.
|
|
53
|
-
let sorted = Float64Array.from(energies).sort()
|
|
54
|
-
let floor = sorted[Math.floor(sorted.length * 0.1)] + bias
|
|
55
|
-
|
|
56
|
-
for (let j = 0; j < frames; j++) {
|
|
57
|
-
active[j] = (energies[j] - floor > snrTh && flats[j] < flatTh) ? 1 : 0
|
|
58
|
-
}
|
|
59
|
-
return { active, times, hop, frameSize: N }
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
// Per-bin Speech Presence Probability from a-priori SNR ξ:
|
|
63
|
-
// p = ξ / (1 + ξ) (Bayesian formulation under Gaussian model, q-prior = 0.5)
|
|
64
|
-
// Bind to a noise PSD source (e.g. minStats.psd or imcra.psd) and an a-priori SNR estimate.
|
|
65
|
-
export function spp(mag, noisePsd, opts = {}) {
|
|
66
|
-
let xiMin = opts.xiMin ?? 0.0316 // -15 dB floor
|
|
67
|
-
let half = mag.length - 1
|
|
68
|
-
let p = new Float64Array(half + 1)
|
|
69
|
-
for (let k = 0; k <= half; k++) {
|
|
70
|
-
let post = (mag[k] * mag[k]) / Math.max(noisePsd[k], 1e-30) - 1
|
|
71
|
-
let xi = Math.max(xiMin, post)
|
|
72
|
-
p[k] = xi / (1 + xi)
|
|
73
|
-
}
|
|
74
|
-
return p
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
// Decision-Directed a-priori SNR (Ephraim-Malah 1984), recursive smoothing.
|
|
78
|
-
// ξ̂[k] = α · |X̂_prev[k]|² / N̂[k] + (1-α) · max(γ-1, 0)
|
|
79
|
-
// γ = posterior SNR = |Y[k]|² / N̂[k]. Used by Wiener, MMSE, OM-LSA gain rules.
|
|
80
|
-
export function ddSnr(mag, noisePsd, prevGain, prevMag, alpha = 0.98) {
|
|
81
|
-
let half = mag.length - 1
|
|
82
|
-
let xi = new Float64Array(half + 1)
|
|
83
|
-
for (let k = 0; k <= half; k++) {
|
|
84
|
-
let n = Math.max(noisePsd[k], 1e-30)
|
|
85
|
-
let post = (mag[k] * mag[k]) / n
|
|
86
|
-
let g = prevGain[k] * prevMag[k]
|
|
87
|
-
let prev = (g * g) / n
|
|
88
|
-
xi[k] = Math.max(0.0316, alpha * prev + (1 - alpha) * Math.max(post - 1, 0))
|
|
89
|
-
}
|
|
90
|
-
return xi
|
|
91
|
-
}
|