@audio/sinusoidal-residual 1.0.0

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 +34 -0
  2. package/residual.js +67 -0
package/package.json ADDED
@@ -0,0 +1,34 @@
1
+ {
2
+ "name": "@audio/sinusoidal-residual",
3
+ "version": "1.0.0",
4
+ "description": "Residual noise layer — spectral subtraction of tracked partials with input phase (Serra SMS)",
5
+ "type": "module",
6
+ "sideEffects": false,
7
+ "main": "residual.js",
8
+ "exports": {
9
+ ".": "./residual.js",
10
+ "./package.json": "./package.json"
11
+ },
12
+ "files": [
13
+ "residual.js"
14
+ ],
15
+ "dependencies": {
16
+ "fourier-transform": "^2.2.0",
17
+ "@audio/sinusoidal-track": "^1.0.0"
18
+ },
19
+ "keywords": [
20
+ "audio",
21
+ "dsp",
22
+ "sinusoidal",
23
+ "sms",
24
+ "residual"
25
+ ],
26
+ "license": "MIT",
27
+ "author": "Dmitry Iv. <dfcreative@gmail.com>",
28
+ "engines": {
29
+ "node": ">=18"
30
+ },
31
+ "publishConfig": {
32
+ "access": "public"
33
+ }
34
+ }
package/residual.js ADDED
@@ -0,0 +1,67 @@
1
+ // Residual — input minus additive resynthesis of its tracked partials: the noise layer
2
+ // of the sinusoids+noise model (Serra-Smith SMS). Phase-blind subtraction is imperfect
3
+ // per partial, so the sinusoidal layer is estimated and the residual computed spectrally:
4
+ // per frame, subtract the tracked partials' magnitude from the input magnitude (Wiener-style
5
+ // soft mask), then resynthesize with the input phase — robust without original-phase tracking.
6
+
7
+ import { fft, ifft } from 'fourier-transform'
8
+ import track from '@audio/sinusoidal-track'
9
+
10
+ /**
11
+ * @param {Float32Array} data — mono PCM
12
+ * @param {object} opts — track options; also { model } to reuse an existing analysis
13
+ * @returns {Float32Array} residual (same length)
14
+ */
15
+ export default function residual (data, opts = {}) {
16
+ let model = opts.model || track(data, opts)
17
+ let { frames, hop, frameSize, fs } = model
18
+ let half = frameSize / 2
19
+
20
+ // per-frame set of partial (freq, amp)
21
+ let perFrame = Array.from({ length: frames }, () => [])
22
+ for (let p of model.partials) {
23
+ for (let j = 0; j < p.freqs.length; j++) {
24
+ let t = p.start + j
25
+ if (t < frames) perFrame[t].push([p.freqs[j], p.amps[j]])
26
+ }
27
+ }
28
+
29
+ let win = new Float64Array(frameSize)
30
+ let winSum = 0
31
+ for (let i = 0; i < frameSize; i++) { win[i] = 0.5 - 0.5 * Math.cos(2 * Math.PI * i / frameSize); winSum += win[i] }
32
+ let buf = new Float64Array(frameSize)
33
+ let out = new Float64Array(data.length)
34
+ let norm = new Float64Array(data.length)
35
+
36
+ for (let t = 0; t < frames; t++) {
37
+ for (let i = 0; i < frameSize; i++) buf[i] = (data[t * hop + i] || 0) * win[i]
38
+ let fr = fft(buf)
39
+ let re = Float64Array.from(fr[0]), im = Float64Array.from(fr[1])
40
+ // sinusoidal magnitude estimate per bin (hann mainlobe ≈ 2 bins wide)
41
+ for (let [f, a] of perFrame[t]) {
42
+ let kc = f * frameSize / fs
43
+ let k0 = Math.max(0, Math.floor(kc - 3)), k1 = Math.min(half, Math.ceil(kc + 3))
44
+ let peakMag = a * winSum / 2
45
+ for (let k = k0; k <= k1; k++) {
46
+ let d = Math.abs(k - kc)
47
+ let lobe = d < 1 ? 1 : d < 2 ? 0.35 : d < 3 ? 0.08 : 0.02
48
+ let m = Math.hypot(re[k], im[k])
49
+ let sub = Math.min(m, peakMag * lobe)
50
+ if (m > 1e-12) {
51
+ let g = (m - sub) / m
52
+ re[k] *= g; im[k] *= g
53
+ }
54
+ }
55
+ }
56
+ let y = ifft(re, im)
57
+ for (let i = 0; i < frameSize; i++) {
58
+ let j = t * hop + i
59
+ if (j >= data.length) break
60
+ out[j] += y[i] * win[i]
61
+ norm[j] += win[i] * win[i]
62
+ }
63
+ }
64
+ let res = new Float32Array(data.length)
65
+ for (let i = 0; i < data.length; i++) res[i] = norm[i] > 1e-9 ? out[i] / norm[i] : data[i]
66
+ return res
67
+ }