@audio/saturate-core 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/core.js +31 -0
  2. package/package.json +32 -0
package/core.js ADDED
@@ -0,0 +1,31 @@
1
+ // Shared saturation machinery — oversampled nonlinearity application.
2
+ // Upsample (windowed-sinc) → per-sample transfer → decimate (windowed-sinc, anti-aliased),
3
+ // so waveshaping harmonics above Nyquist don't fold back as aliases.
4
+
5
+ import resample from '@audio/resample-sinc'
6
+
7
+ /**
8
+ * Apply a memoryless transfer function with oversampling, in place.
9
+ * @param {Float32Array} data
10
+ * @param {(x:number)=>number} fn — transfer curve
11
+ * @param {object} opts — { fs=44100, oversample=4 (1 disables), mix=1 }
12
+ */
13
+ export function shape (data, fn, { fs = 44100, oversample = 4, mix = 1 } = {}) {
14
+ if (oversample > 1) {
15
+ let up = resample(data, { from: fs, to: fs * oversample })
16
+ for (let i = 0; i < up.length; i++) up[i] = fn(up[i])
17
+ let down = resample(up, { from: fs * oversample, to: fs })
18
+ let n = Math.min(data.length, down.length)
19
+ for (let i = 0; i < n; i++) data[i] = data[i] * (1 - mix) + down[i] * mix
20
+ } else {
21
+ for (let i = 0; i < data.length; i++) data[i] = data[i] * (1 - mix) + fn(data[i]) * mix
22
+ }
23
+ return data
24
+ }
25
+
26
+ /** one-pole lowpass in place (post-shaping tone rounding) */
27
+ export function onepole (data, coef) {
28
+ let s = 0
29
+ for (let i = 0; i < data.length; i++) s = data[i] = data[i] * (1 - coef) + s * coef
30
+ return data
31
+ }
package/package.json ADDED
@@ -0,0 +1,32 @@
1
+ {
2
+ "name": "@audio/saturate-core",
3
+ "version": "1.0.0",
4
+ "description": "Shared saturation machinery \u2014 sinc-oversampled transfer application (no aliasing waveshapers)",
5
+ "type": "module",
6
+ "sideEffects": false,
7
+ "main": "core.js",
8
+ "exports": {
9
+ ".": "./core.js",
10
+ "./package.json": "./package.json"
11
+ },
12
+ "files": [
13
+ "core.js"
14
+ ],
15
+ "dependencies": {
16
+ "@audio/resample-sinc": "^1.0.0"
17
+ },
18
+ "keywords": [
19
+ "audio",
20
+ "dsp",
21
+ "saturation",
22
+ "core"
23
+ ],
24
+ "license": "MIT",
25
+ "author": "Dmitry Iv. <dfcreative@gmail.com>",
26
+ "engines": {
27
+ "node": ">=18"
28
+ },
29
+ "publishConfig": {
30
+ "access": "public"
31
+ }
32
+ }