@audio/denoise-core 0.1.1 → 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.
Files changed (2) hide show
  1. package/package.json +4 -3
  2. 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.1",
4
- "description": "Shared restoration primitives — STFT (batch/stream/analyse), noise estimation (min-stats, IMCRA), VAD, AR modeling, quality metrics",
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/vad.js CHANGED
@@ -1,91 +1,3 @@
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
- }
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'