@audio/synth-envelope 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/envelope.js +15 -0
  2. package/package.json +29 -0
package/envelope.js ADDED
@@ -0,0 +1,15 @@
1
+ // ADSR envelope generator — returns a gain contour (attack/decay linear, release exp-ish),
2
+ // to multiply onto any source (gain, filter cutoff, amplitude).
3
+
4
+ export default function adsr ({ attack = 0.01, decay = 0.1, sustain = 0.7, release = 0.3, duration = 1, fs = 44100 } = {}) {
5
+ let n = Math.round((duration + release) * fs)
6
+ let out = new Float32Array(n)
7
+ let aN = Math.round(attack * fs), dN = Math.round(decay * fs), susEnd = Math.round(duration * fs)
8
+ for (let i = 0; i < n; i++) {
9
+ if (i < aN) out[i] = i / (aN || 1)
10
+ else if (i < aN + dN) out[i] = 1 - (1 - sustain) * (i - aN) / (dN || 1)
11
+ else if (i < susEnd) out[i] = sustain
12
+ else out[i] = sustain * Math.exp(-3 * (i - susEnd) / (n - susEnd || 1))
13
+ }
14
+ return out
15
+ }
package/package.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "name": "@audio/synth-envelope",
3
+ "version": "1.0.0",
4
+ "description": "ADSR envelope generator — gain contour for amplitude/filter modulation",
5
+ "type": "module",
6
+ "sideEffects": false,
7
+ "main": "envelope.js",
8
+ "exports": {
9
+ ".": "./envelope.js",
10
+ "./package.json": "./package.json"
11
+ },
12
+ "files": [
13
+ "envelope.js"
14
+ ],
15
+ "keywords": [
16
+ "audio",
17
+ "dsp",
18
+ "synth",
19
+ "envelope"
20
+ ],
21
+ "license": "MIT",
22
+ "author": "Dmitry Iv. <dfcreative@gmail.com>",
23
+ "engines": {
24
+ "node": ">=18"
25
+ },
26
+ "publishConfig": {
27
+ "access": "public"
28
+ }
29
+ }