@audio/spectral-contrast 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/contrast.js +22 -0
  2. package/package.json +29 -0
package/contrast.js ADDED
@@ -0,0 +1,22 @@
1
+ // Spectral contrast — per-octave-band difference between peak and valley energy
2
+ // percentiles of the magnitude spectrum (Jiang 2002; librosa/essentia parity).
3
+
4
+ /**
5
+ * @param {Float32Array} mag — magnitude spectrum
6
+ * @param {object} opts — { fs=44100, n=2·(mag.length−1), fmin=200, bands=6, quantile=0.2 }
7
+ * @returns {Float32Array} contrast in dB per band
8
+ */
9
+ export default function contrast (mag, { fs = 44100, n = 2 * (mag.length - 1), fmin = 200, bands = 6, quantile = 0.2 } = {}) {
10
+ let out = new Float32Array(bands)
11
+ for (let b = 0; b < bands; b++) {
12
+ let lo = fmin * 2 ** b, hi = fmin * 2 ** (b + 1)
13
+ let k0 = Math.max(1, Math.round(lo * n / fs)), k1 = Math.min(mag.length - 1, Math.round(hi * n / fs))
14
+ if (k1 <= k0 + 2) break
15
+ let seg = Array.from(mag.subarray(k0, k1 + 1)).sort((a, b) => a - b)
16
+ let q = Math.max(1, Math.round(seg.length * quantile))
17
+ let valley = 0, peak = 0
18
+ for (let i = 0; i < q; i++) { valley += seg[i]; peak += seg[seg.length - 1 - i] }
19
+ out[b] = 20 * Math.log10((peak + 1e-12) / (valley + 1e-12))
20
+ }
21
+ return out
22
+ }
package/package.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "name": "@audio/spectral-contrast",
3
+ "version": "1.0.0",
4
+ "description": "Spectral contrast — per-octave peak/valley difference (Jiang 2002, librosa parity)",
5
+ "type": "module",
6
+ "sideEffects": false,
7
+ "main": "contrast.js",
8
+ "exports": {
9
+ ".": "./contrast.js",
10
+ "./package.json": "./package.json"
11
+ },
12
+ "files": [
13
+ "contrast.js"
14
+ ],
15
+ "keywords": [
16
+ "audio",
17
+ "dsp",
18
+ "spectral",
19
+ "contrast"
20
+ ],
21
+ "license": "MIT",
22
+ "author": "Dmitry Iv. <dfcreative@gmail.com>",
23
+ "engines": {
24
+ "node": ">=18"
25
+ },
26
+ "publishConfig": {
27
+ "access": "public"
28
+ }
29
+ }