@audio/window 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.
- package/README.md +5 -0
- package/package.json +34 -0
- package/window.js +42 -0
package/README.md
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@audio/window",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"sideEffects": false,
|
|
6
|
+
"description": "Audio-facing window kit — typed fills (periodic/symmetric), apply, COLA check over window-function",
|
|
7
|
+
"main": "window.js",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": "./window.js",
|
|
10
|
+
"./package.json": "./package.json"
|
|
11
|
+
},
|
|
12
|
+
"files": [
|
|
13
|
+
"window.js"
|
|
14
|
+
],
|
|
15
|
+
"dependencies": {
|
|
16
|
+
"window-function": "^3.0.1"
|
|
17
|
+
},
|
|
18
|
+
"keywords": [
|
|
19
|
+
"audio",
|
|
20
|
+
"dsp",
|
|
21
|
+
"window",
|
|
22
|
+
"hann",
|
|
23
|
+
"cola",
|
|
24
|
+
"stft"
|
|
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/window.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
// Audio-facing window kit over window-function (scijs, canonical per-sample math):
|
|
2
|
+
// typed-array fill with symmetric/periodic variants, in-place apply, and COLA
|
|
3
|
+
// (constant-overlap-add) verification for STFT hop choices.
|
|
4
|
+
|
|
5
|
+
import * as wf from 'window-function'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Fill a Float64Array with the named window.
|
|
9
|
+
* @param {string} name — any window-function export (hann, hamming, blackman, …)
|
|
10
|
+
* @param {number} N — length
|
|
11
|
+
* @param {object} opts — { periodic=true } — periodic (DFT-even, for STFT) vs symmetric (filter design)
|
|
12
|
+
*/
|
|
13
|
+
export default function window (name, N, { periodic = true } = {}) {
|
|
14
|
+
let fn = wf[name]
|
|
15
|
+
if (typeof fn !== 'function') throw new RangeError(`window: unknown window "${name}"`)
|
|
16
|
+
let out = new Float64Array(N)
|
|
17
|
+
let M = periodic ? N + 1 : N
|
|
18
|
+
for (let i = 0; i < N; i++) out[i] = fn(i, M)
|
|
19
|
+
return out
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** Multiply data by win in place. */
|
|
23
|
+
export function apply (data, win) {
|
|
24
|
+
for (let i = 0; i < data.length; i++) data[i] *= win[i % win.length]
|
|
25
|
+
return data
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* COLA check: does Σ win(n − m·hop) (or win² with {squared}) stay constant?
|
|
30
|
+
* @returns {{ ok, ripple, mean }} — ripple = (max−min)/mean over the steady region
|
|
31
|
+
*/
|
|
32
|
+
export function cola (win, hop, { squared = false, tolerance = 1e-6 } = {}) {
|
|
33
|
+
let N = win.length
|
|
34
|
+
let sum = new Float64Array(hop)
|
|
35
|
+
for (let i = 0; i < hop; i++)
|
|
36
|
+
for (let j = i; j < N; j += hop) sum[i] += squared ? win[j] * win[j] : win[j]
|
|
37
|
+
let min = Math.min(...sum), max = Math.max(...sum)
|
|
38
|
+
let mean = sum.reduce((a, x) => a + x, 0) / hop
|
|
39
|
+
return { ok: mean > 0 && (max - min) / mean < tolerance, ripple: mean > 0 ? (max - min) / mean : Infinity, mean }
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export * from 'window-function'
|