@audio/resample-sinc 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 (3) hide show
  1. package/README.md +5 -0
  2. package/package.json +31 -0
  3. package/sinc.js +31 -0
package/README.md ADDED
@@ -0,0 +1,5 @@
1
+ # @audio/resample-sinc
2
+
3
+ > Windowed-sinc resampling — high quality, anti-aliased (SoX rate / libsamplerate class)
4
+
5
+ Planned — not implemented yet. See the umbrella README for status and sources.
package/package.json ADDED
@@ -0,0 +1,31 @@
1
+ {
2
+ "name": "@audio/resample-sinc",
3
+ "version": "1.0.0",
4
+ "description": "Windowed-sinc (Lanczos, 32-tap) resampling with anti-alias kernel widening on downsample",
5
+ "type": "module",
6
+ "sideEffects": false,
7
+ "main": "sinc.js",
8
+ "exports": {
9
+ ".": "./sinc.js",
10
+ "./package.json": "./package.json"
11
+ },
12
+ "files": [
13
+ "sinc.js"
14
+ ],
15
+ "keywords": [
16
+ "audio",
17
+ "dsp",
18
+ "resample",
19
+ "sinc",
20
+ "lanczos",
21
+ "sample-rate"
22
+ ],
23
+ "license": "MIT",
24
+ "author": "Dmitry Iv. <dfcreative@gmail.com>",
25
+ "engines": {
26
+ "node": ">=18"
27
+ },
28
+ "publishConfig": {
29
+ "access": "public"
30
+ }
31
+ }
package/sinc.js ADDED
@@ -0,0 +1,31 @@
1
+ // Windowed-sinc (Lanczos, 32-tap) resampling with kernel widening on downsample —
2
+ // aliases are suppressed by the widened kernel, no separate lowpass needed.
3
+ // Ported from the audio-core interpolator (same math, standalone API).
4
+
5
+ const HALF = 16
6
+ const sinc = x => x === 0 ? 1 : Math.sin(Math.PI * x) / (Math.PI * x)
7
+
8
+ export default function resample (data, { from, to } = {}) {
9
+ if (!(from > 0) || !(to > 0)) throw new RangeError('resample: from/to must be positive sample rates')
10
+ if (from === to) return Float32Array.from(data)
11
+
12
+ let rate = from / to // input samples per output sample
13
+ let n = Math.round(data.length * to / from)
14
+ let out = new Float32Array(n)
15
+ let scale = rate > 1 ? 1 / rate : 1 // widen kernel when downsampling (anti-alias)
16
+
17
+ for (let i = 0; i < n; i++) {
18
+ let pos = i * rate
19
+ let base = Math.floor(pos), frac = pos - base
20
+ let sum = 0, w = 0
21
+ for (let t = 1 - HALF; t <= HALF; t++) {
22
+ let idx = base + t
23
+ if (idx < 0 || idx >= data.length) continue
24
+ let x = (t - frac) * scale
25
+ let k = sinc(x) * sinc(x / HALF)
26
+ sum += data[idx] * k; w += k
27
+ }
28
+ out[i] = w !== 0 ? sum / w : 0
29
+ }
30
+ return out
31
+ }