From 9663c2ec858cc37ede5211d59297b8b849761d84 Mon Sep 17 00:00:00 2001 From: Jade Rowland Date: Fri, 15 Dec 2023 12:16:28 -0700 Subject: [PATCH] fileter envelopes --- packages/superdough/helpers.mjs | 19 +++++++---------- packages/superdough/sampler.mjs | 4 ++-- packages/superdough/superdough.mjs | 33 ++++++++++++++++++------------ packages/superdough/synth.mjs | 14 +++++-------- 4 files changed, 34 insertions(+), 36 deletions(-) diff --git a/packages/superdough/helpers.mjs b/packages/superdough/helpers.mjs index 91bc17bc..94bdc11f 100644 --- a/packages/superdough/helpers.mjs +++ b/packages/superdough/helpers.mjs @@ -88,21 +88,16 @@ export function getCompressor(ac, threshold, ratio, knee, attack, release) { }; return new DynamicsCompressorNode(ac, options); } - -const adsrmin = 0.001; -export const getADSRDefaults = ( - a, - d, - s, - r, - def = { attack: adsrmin, decay: adsrmin, sustain: 1, release: adsrmin }, -) => { +const envmin = 0.001; +export const getADSRValues = (params, defaultValues = [envmin, envmin, 1, envmin]) => { + const [a, d, s, r] = params; + const [defA, defD, defS, defR] = defaultValues; console.log(a, d, s, r); if (a == null && d == null && s == null && r == null) { - return def; + return defaultValues; } - const sustain = s ?? ((a != null && d == null) || (a == null && d == null)) ? def.sustain : adsrmin; - return { attack: a ?? adsrmin, decay: d ?? adsrmin, sustain, release: r ?? adsrmin }; + const sustain = s != null ? s : (a != null && d == null) || (a == null && d == null) ? defS : envmin; + return [a ?? envmin, d ?? envmin, sustain, r ?? envmin]; }; export function createFilter( diff --git a/packages/superdough/sampler.mjs b/packages/superdough/sampler.mjs index e4ead52e..fb1dfda9 100644 --- a/packages/superdough/sampler.mjs +++ b/packages/superdough/sampler.mjs @@ -1,6 +1,6 @@ import { noteToMidi, valueToMidi, nanFallback } from './util.mjs'; import { getAudioContext, registerSound } from './index.mjs'; -import { getADSRDefaults, getEnvelope } from './helpers.mjs'; +import { getADSRValues, getEnvelope } from './helpers.mjs'; import { logger } from './logger.mjs'; const bufferCache = {}; // string: Promise @@ -252,7 +252,7 @@ export async function onTriggerSample(t, value, onended, bank, resolveUrl) { const ac = getAudioContext(); // destructure adsr here, because the default should be different for synths and samples - const { attack, decay, sustain, release } = getADSRDefaults(value.attack, value.decay, value.sustain, value.release); + const [attack, decay, sustain, release] = getADSRValues([value.attack, value.decay, value.sustain, value.release]); //const soundfont = getSoundfontKey(s); const time = t + nudge; diff --git a/packages/superdough/superdough.mjs b/packages/superdough/superdough.mjs index 3be97615..036e63ba 100644 --- a/packages/superdough/superdough.mjs +++ b/packages/superdough/superdough.mjs @@ -9,7 +9,7 @@ import './reverb.mjs'; import './vowel.mjs'; import { clamp, nanFallback } from './util.mjs'; import workletsUrl from './worklets.mjs?url'; -import { createFilter, gainNode, getCompressor } from './helpers.mjs'; +import { createFilter, gainNode, getADSRValues, getCompressor } from './helpers.mjs'; import { map } from 'nanostores'; import { logger } from './logger.mjs'; import { loadBuffer } from './sampler.mjs'; @@ -269,26 +269,16 @@ export const superdough = async (value, deadline, hapDuration) => { // low pass cutoff, lpenv, - lpattack = 0.01, - lpdecay = 0.01, - lpsustain = 1, - lprelease = 0.01, resonance = 1, // high pass hpenv, hcutoff, - hpattack = 0.01, - hpdecay = 0.01, - hpsustain = 1, - hprelease = 0.01, + hresonance = 1, // band pass bpenv, bandf, - bpattack = 0.01, - bpdecay = 0.01, - bpsustain = 1, - bprelease = 0.01, + bandq = 1, channels = [1, 2], //phaser @@ -322,6 +312,7 @@ export const superdough = async (value, deadline, hapDuration) => { compressorAttack, compressorRelease, } = value; + gain = nanFallback(gain, 1); //music programs/audio gear usually increments inputs/outputs from 1, so imitate that behavior @@ -366,7 +357,15 @@ export const superdough = async (value, deadline, hapDuration) => { // gain stage chain.push(gainNode(gain)); + const filterEnvDefaults = [0.01, 0.01, 1, 0.01]; + if (cutoff !== undefined) { + const [lpattack, lpdecay, lpsustain, lprelease] = getADSRValues( + [value.lpattack, value.lpdecay, value.lpsustain, value.lprelease], + filterEnvDefaults, + ); + console.log(lpattack, 'atta'); + let lp = () => createFilter( ac, @@ -389,6 +388,10 @@ export const superdough = async (value, deadline, hapDuration) => { } if (hcutoff !== undefined) { + const [hpattack, hpdecay, hpsustain, hprelease] = getADSRValues( + [value.hpattack, value.hpdecay, value.hpsustain, value.hprelease], + filterEnvDefaults, + ); let hp = () => createFilter( ac, @@ -411,6 +414,10 @@ export const superdough = async (value, deadline, hapDuration) => { } if (bandf !== undefined) { + const [bpattack, bpdecay, bpsustain, bprelease] = getADSRValues( + [value.bpattack, value.bpdecay, value.bpsustain, value.bprelease], + filterEnvDefaults, + ); let bp = () => createFilter( ac, diff --git a/packages/superdough/synth.mjs b/packages/superdough/synth.mjs index a715947e..5977e9ac 100644 --- a/packages/superdough/synth.mjs +++ b/packages/superdough/synth.mjs @@ -1,6 +1,6 @@ import { midiToFreq, noteToMidi } from './util.mjs'; import { registerSound, getAudioContext } from './superdough.mjs'; -import { gainNode, getADSRDefaults, getEnvelope, getExpEnvelope } from './helpers.mjs'; +import { gainNode, getADSRValues, getEnvelope, getExpEnvelope } from './helpers.mjs'; import { getNoiseMix, getNoiseOscillator } from './noise.mjs'; const mod = (freq, range = 1, type = 'sine') => { @@ -29,15 +29,11 @@ export function registerSynthSounds() { registerSound( s, (t, value, onended) => { - // destructure adsr here, because the default should be different for synths and samples - const { attack, decay, sustain, release } = getADSRDefaults( - value.attack, - value.decay, - value.sustain, - value.release, - { attack: 0.001, decay: 0.05, sustain: 0.6, release: 0.01 }, + const defaultADSRValues = [0.001, 0.05, 0.6, 0.01]; + const [attack, decay, sustain, release] = getADSRValues( + [value.attack, value.decay, value.sustain, value.release], + defaultADSRValues, ); - console.log({ attack, decay, sustain, release }); let sound; if (waveforms.includes(s)) {