fileter envelopes

This commit is contained in:
Jade Rowland 2023-12-15 12:16:28 -07:00
parent 7da7554493
commit 9663c2ec85
4 changed files with 34 additions and 36 deletions

View file

@ -88,21 +88,16 @@ export function getCompressor(ac, threshold, ratio, knee, attack, release) {
}; };
return new DynamicsCompressorNode(ac, options); return new DynamicsCompressorNode(ac, options);
} }
const envmin = 0.001;
const adsrmin = 0.001; export const getADSRValues = (params, defaultValues = [envmin, envmin, 1, envmin]) => {
export const getADSRDefaults = ( const [a, d, s, r] = params;
a, const [defA, defD, defS, defR] = defaultValues;
d,
s,
r,
def = { attack: adsrmin, decay: adsrmin, sustain: 1, release: adsrmin },
) => {
console.log(a, d, s, r); console.log(a, d, s, r);
if (a == null && d == null && s == null && r == null) { 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; const sustain = s != null ? s : (a != null && d == null) || (a == null && d == null) ? defS : envmin;
return { attack: a ?? adsrmin, decay: d ?? adsrmin, sustain, release: r ?? adsrmin }; return [a ?? envmin, d ?? envmin, sustain, r ?? envmin];
}; };
export function createFilter( export function createFilter(

View file

@ -1,6 +1,6 @@
import { noteToMidi, valueToMidi, nanFallback } from './util.mjs'; import { noteToMidi, valueToMidi, nanFallback } from './util.mjs';
import { getAudioContext, registerSound } from './index.mjs'; import { getAudioContext, registerSound } from './index.mjs';
import { getADSRDefaults, getEnvelope } from './helpers.mjs'; import { getADSRValues, getEnvelope } from './helpers.mjs';
import { logger } from './logger.mjs'; import { logger } from './logger.mjs';
const bufferCache = {}; // string: Promise<ArrayBuffer> const bufferCache = {}; // string: Promise<ArrayBuffer>
@ -252,7 +252,7 @@ export async function onTriggerSample(t, value, onended, bank, resolveUrl) {
const ac = getAudioContext(); const ac = getAudioContext();
// destructure adsr here, because the default should be different for synths and samples // 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 soundfont = getSoundfontKey(s);
const time = t + nudge; const time = t + nudge;

View file

@ -9,7 +9,7 @@ import './reverb.mjs';
import './vowel.mjs'; import './vowel.mjs';
import { clamp, nanFallback } from './util.mjs'; import { clamp, nanFallback } from './util.mjs';
import workletsUrl from './worklets.mjs?url'; 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 { map } from 'nanostores';
import { logger } from './logger.mjs'; import { logger } from './logger.mjs';
import { loadBuffer } from './sampler.mjs'; import { loadBuffer } from './sampler.mjs';
@ -269,26 +269,16 @@ export const superdough = async (value, deadline, hapDuration) => {
// low pass // low pass
cutoff, cutoff,
lpenv, lpenv,
lpattack = 0.01,
lpdecay = 0.01,
lpsustain = 1,
lprelease = 0.01,
resonance = 1, resonance = 1,
// high pass // high pass
hpenv, hpenv,
hcutoff, hcutoff,
hpattack = 0.01,
hpdecay = 0.01,
hpsustain = 1,
hprelease = 0.01,
hresonance = 1, hresonance = 1,
// band pass // band pass
bpenv, bpenv,
bandf, bandf,
bpattack = 0.01,
bpdecay = 0.01,
bpsustain = 1,
bprelease = 0.01,
bandq = 1, bandq = 1,
channels = [1, 2], channels = [1, 2],
//phaser //phaser
@ -322,6 +312,7 @@ export const superdough = async (value, deadline, hapDuration) => {
compressorAttack, compressorAttack,
compressorRelease, compressorRelease,
} = value; } = value;
gain = nanFallback(gain, 1); gain = nanFallback(gain, 1);
//music programs/audio gear usually increments inputs/outputs from 1, so imitate that behavior //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 // gain stage
chain.push(gainNode(gain)); chain.push(gainNode(gain));
const filterEnvDefaults = [0.01, 0.01, 1, 0.01];
if (cutoff !== undefined) { if (cutoff !== undefined) {
const [lpattack, lpdecay, lpsustain, lprelease] = getADSRValues(
[value.lpattack, value.lpdecay, value.lpsustain, value.lprelease],
filterEnvDefaults,
);
console.log(lpattack, 'atta');
let lp = () => let lp = () =>
createFilter( createFilter(
ac, ac,
@ -389,6 +388,10 @@ export const superdough = async (value, deadline, hapDuration) => {
} }
if (hcutoff !== undefined) { if (hcutoff !== undefined) {
const [hpattack, hpdecay, hpsustain, hprelease] = getADSRValues(
[value.hpattack, value.hpdecay, value.hpsustain, value.hprelease],
filterEnvDefaults,
);
let hp = () => let hp = () =>
createFilter( createFilter(
ac, ac,
@ -411,6 +414,10 @@ export const superdough = async (value, deadline, hapDuration) => {
} }
if (bandf !== undefined) { if (bandf !== undefined) {
const [bpattack, bpdecay, bpsustain, bprelease] = getADSRValues(
[value.bpattack, value.bpdecay, value.bpsustain, value.bprelease],
filterEnvDefaults,
);
let bp = () => let bp = () =>
createFilter( createFilter(
ac, ac,

View file

@ -1,6 +1,6 @@
import { midiToFreq, noteToMidi } from './util.mjs'; import { midiToFreq, noteToMidi } from './util.mjs';
import { registerSound, getAudioContext } from './superdough.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'; import { getNoiseMix, getNoiseOscillator } from './noise.mjs';
const mod = (freq, range = 1, type = 'sine') => { const mod = (freq, range = 1, type = 'sine') => {
@ -29,15 +29,11 @@ export function registerSynthSounds() {
registerSound( registerSound(
s, s,
(t, value, onended) => { (t, value, onended) => {
// destructure adsr here, because the default should be different for synths and samples const defaultADSRValues = [0.001, 0.05, 0.6, 0.01];
const { attack, decay, sustain, release } = getADSRDefaults( const [attack, decay, sustain, release] = getADSRValues(
value.attack, [value.attack, value.decay, value.sustain, value.release],
value.decay, defaultADSRValues,
value.sustain,
value.release,
{ attack: 0.001, decay: 0.05, sustain: 0.6, release: 0.01 },
); );
console.log({ attack, decay, sustain, release });
let sound; let sound;
if (waveforms.includes(s)) { if (waveforms.includes(s)) {