use closures to simplify api

This commit is contained in:
Felix Roos 2022-06-18 00:56:58 +02:00
parent faa4019b27
commit 44574af3eb

View File

@ -19,16 +19,16 @@ export const getAudioContext = () => {
return audioContext; return audioContext;
}; };
const getFilter = (ac, type, frequency, Q) => { const getFilter = (type, frequency, Q) => {
const filter = ac.createBiquadFilter(); const filter = getAudioContext().createBiquadFilter();
filter.type = type; filter.type = type;
filter.frequency.value = frequency; filter.frequency.value = frequency;
filter.Q.value = Q; filter.Q.value = Q;
return filter; return filter;
}; };
const getADSR = (ac, attack, decay, sustain, release, velocity, begin, end) => { const getADSR = (attack, decay, sustain, release, velocity, begin, end) => {
const gainNode = ac.createGain(); const gainNode = getAudioContext().createGain();
gainNode.gain.setValueAtTime(0, begin); gainNode.gain.setValueAtTime(0, begin);
gainNode.gain.linearRampToValueAtTime(velocity, begin + attack); // attack gainNode.gain.linearRampToValueAtTime(velocity, begin + attack); // attack
gainNode.gain.linearRampToValueAtTime(sustain * velocity, begin + attack + decay); // sustain start gainNode.gain.linearRampToValueAtTime(sustain * velocity, begin + attack + decay); // sustain start
@ -82,12 +82,12 @@ Pattern.prototype.out = function () {
o.stop(t + hap.duration + release); o.stop(t + hap.duration + release);
chain.push(o); chain.push(o);
// envelope // envelope
const adsr = getADSR(ac, attack, decay, sustain, release, 1, t, t + hap.duration); const adsr = getADSR(attack, decay, sustain, release, 1, t, t + hap.duration);
chain.push(adsr); chain.push(adsr);
// filters // filters
cutoff !== undefined && chain.push(getFilter(ac, 'lowpass', cutoff, resonance)); cutoff !== undefined && chain.push(getFilter('lowpass', cutoff, resonance));
hcutoff !== undefined && chain.push(getFilter(ac, 'highpass', hcutoff, hresonance)); hcutoff !== undefined && chain.push(getFilter('highpass', hcutoff, hresonance));
bandf !== undefined && chain.push(getFilter(ac, 'bandpass', bandf, bandq)); bandf !== undefined && chain.push(getFilter('bandpass', bandf, bandq));
// TODO vowel // TODO vowel
// TODO delay / delaytime / delayfeedback // TODO delay / delaytime / delayfeedback
// panning // panning