@audio/denoise-core 0.1.0 → 0.1.2
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/package.json +4 -3
- package/stft.js +1 -0
- package/util.js +5 -3
- package/vad.js +3 -91
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.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).",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
7
7
|
"main": "index.js",
|
|
@@ -25,7 +25,8 @@
|
|
|
25
25
|
"quality.js"
|
|
26
26
|
],
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"fourier-transform": "^2.0.0"
|
|
28
|
+
"fourier-transform": "^2.0.0",
|
|
29
|
+
"@audio/vad": "^1.0.0"
|
|
29
30
|
},
|
|
30
31
|
"keywords": [
|
|
31
32
|
"audio",
|
package/stft.js
CHANGED
package/util.js
CHANGED
|
@@ -120,7 +120,7 @@ export function makeStreamBufs(N, nf = 0) {
|
|
|
120
120
|
N, nf,
|
|
121
121
|
ib: new Float32Array(N * 4), il: 0,
|
|
122
122
|
ob: new Float32Array(N * 8), nb: new Float32Array(N * 8),
|
|
123
|
-
pos: 0, oread: 0
|
|
123
|
+
pos: 0, oread: 0, hi: 0
|
|
124
124
|
}
|
|
125
125
|
}
|
|
126
126
|
|
|
@@ -156,9 +156,11 @@ export function take(st, upTo) {
|
|
|
156
156
|
}
|
|
157
157
|
st.oread += len
|
|
158
158
|
if (st.oread > st.N * 8) {
|
|
159
|
+
// shift left; zero only past the high-water mark — frames extend N−hop beyond
|
|
160
|
+
// pos, so zeroing from pos would erase the last frame's partial overlap-add tail
|
|
159
161
|
st.ob.copyWithin(0, st.oread); st.nb.copyWithin(0, st.oread)
|
|
160
|
-
st.pos -= st.oread; st.oread = 0
|
|
161
|
-
st.ob.fill(0, st.
|
|
162
|
+
st.pos -= st.oread; st.hi = Math.max(0, st.hi - st.oread); st.oread = 0
|
|
163
|
+
st.ob.fill(0, st.hi); st.nb.fill(0, st.hi)
|
|
162
164
|
}
|
|
163
165
|
return out
|
|
164
166
|
}
|
package/vad.js
CHANGED
|
@@ -1,91 +1,3 @@
|
|
|
1
|
-
//
|
|
2
|
-
//
|
|
3
|
-
|
|
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
|
-
}
|
|
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'
|