@audio/synth-voice 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/package.json +33 -0
  2. package/voice.js +21 -0
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "@audio/synth-voice",
3
+ "version": "1.0.0",
4
+ "description": "Synth voice — osc × ADSR through envelope-scaled lowpass (Tone.js Synth class)",
5
+ "type": "module",
6
+ "sideEffects": false,
7
+ "main": "voice.js",
8
+ "exports": {
9
+ ".": "./voice.js",
10
+ "./package.json": "./package.json"
11
+ },
12
+ "files": [
13
+ "voice.js"
14
+ ],
15
+ "dependencies": {
16
+ "@audio/synth-osc": "^1.0.0",
17
+ "@audio/synth-envelope": "^1.0.0"
18
+ },
19
+ "keywords": [
20
+ "audio",
21
+ "dsp",
22
+ "synth",
23
+ "voice"
24
+ ],
25
+ "license": "MIT",
26
+ "author": "Dmitry Iv. <dfcreative@gmail.com>",
27
+ "engines": {
28
+ "node": ">=18"
29
+ },
30
+ "publishConfig": {
31
+ "access": "public"
32
+ }
33
+ }
package/voice.js ADDED
@@ -0,0 +1,21 @@
1
+ // Synth voice — oscillator × ADSR envelope through a one-pole lowpass with envelope-scaled
2
+ // cutoff (Tone.js Synth/MonoSynth class), composed from @audio/synth-osc + synth-envelope.
3
+
4
+ import osc from '@audio/synth-osc'
5
+ import adsr from '@audio/synth-envelope'
6
+
7
+ export default function voice (freq, { fs = 44100, type = 'sawtooth', duration = 0.6,
8
+ attack = 0.01, decay = 0.15, sustain = 0.6, release = 0.25,
9
+ cutoff = 3000, envAmount = 0.6, amp = 0.7 } = {}) {
10
+ let env = adsr({ attack, decay, sustain, release, duration, fs })
11
+ let src = osc(freq, { duration: env.length / fs, fs, type, amp: 1 })
12
+ let out = new Float32Array(env.length)
13
+ let lp = 0
14
+ for (let i = 0; i < env.length; i++) {
15
+ let fc = cutoff * (1 - envAmount + envAmount * env[i])
16
+ let a = Math.exp(-2 * Math.PI * fc / fs)
17
+ lp = src[i] * env[i] * (1 - a) + lp * a
18
+ out[i] = amp * lp
19
+ }
20
+ return out
21
+ }