Revert "envelopes on fmsynth"

This reverts commit 8fc7688585.
This commit is contained in:
Raphael Forment 2023-08-29 16:31:47 +02:00
parent 8fc7688585
commit c87c3c6672
2 changed files with 7 additions and 58 deletions

View file

@ -39,19 +39,6 @@ export const getEnvelope = (attack, decay, sustain, release, velocity, begin) =>
}; };
}; };
export const getExpEnvelope = (attack, decay, velocity, begin) => {
const gainNode = getAudioContext().createGain();
gainNode.gain.setValueAtTime(0.0001, begin);
gainNode.gain.exponentialRampToValueAtTime(velocity, begin + attack);
gainNode.gain.exponentialRampToValueAtTime(0.0001, begin + attack + decay);
return {
node: gainNode,
stop: (t) => {
gainNode.gain.exponentialRampToValueAtTime(0.0001, t + decay);
},
};
};
export const getADSR = (attack, decay, sustain, release, velocity, begin, end) => { export const getADSR = (attack, decay, sustain, release, velocity, begin, end) => {
const gainNode = getAudioContext().createGain(); const gainNode = getAudioContext().createGain();
gainNode.gain.setValueAtTime(0, begin); gainNode.gain.setValueAtTime(0, begin);

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 { getOscillator, gainNode, getEnvelope, getExpEnvelope } from './helpers.mjs'; import { getOscillator, gainNode, getEnvelope } from './helpers.mjs';
const mod = (freq, range = 1, type) => { const mod = (freq, range = 1, type) => {
const ctx = getAudioContext(); const ctx = getAudioContext();
@ -13,38 +13,10 @@ const mod = (freq, range = 1, type) => {
return { node: g, stop: (t) => osc.stop(t) }; return { node: g, stop: (t) => osc.stop(t) };
}; };
const fm = (t, const fm = (osc, harmonicityRatio, modulationIndex, wave) => {
osc,
harmonicityRatio,
harmonicityAttack,
harmonicityDecay,
modulationIndex,
modulationAttack,
modulationDecay,
wave, ) => {
const carrfreq = osc.frequency.value; const carrfreq = osc.frequency.value;
let modfreq; const modfreq = carrfreq * harmonicityRatio;
let modgain; const modgain = modfreq * modulationIndex;
// Setting up envelopes if needed
if ((harmonicityAttack !== null) || (harmonicityDecay !== null)) {
let hattack = harmonicityAttack || 0.01;
let hdecay = harmonicityDecay || 0.5;
const harmonicEnv = getExpEnvelope(hattack, hdecay, 1, t);
modfreq = carrfreq * (harmonicityRatio * harmonicEnv);
} else {
modfreq = carrfreq * harmonicityRatio;
}
if ((modulationAttack !== null) || (modulationDecay !== null)) {
let mattack = modulationAttack || 0.01;
let mdecay = modulationDecay || 0.5;
const modulationEnv = getExpEnvelope(mattack, mdecay, 1, t);
modgain = modfreq * (modulationIndex * modulationEnv);
} else {
modgain = modfreq * modulationIndex;
}
return mod(modfreq, modgain, wave); return mod(modfreq, modgain, wave);
}; };
@ -60,12 +32,8 @@ export function registerSynthSounds() {
sustain = 0.6, sustain = 0.6,
release = 0.01, release = 0.01,
fmh: fmHarmonicity = 1, fmh: fmHarmonicity = 1,
fmhattack: fmHarmonicityAttack = null,
fmhdecay: fmHarmonicityDecay = null,
fmi: fmModulationIndex, fmi: fmModulationIndex,
fmiattack: fmModulationAttack = null, fmwave: fmWaveform = 'sine'
fmidecay: fmModulationDecay = null,
fmwave: fmWaveform = 'sine',
} = value; } = value;
let { n, note, freq } = value; let { n, note, freq } = value;
// with synths, n and note are the same thing // with synths, n and note are the same thing
@ -84,15 +52,9 @@ export function registerSynthSounds() {
let stopFm; let stopFm;
if (fmModulationIndex) { if (fmModulationIndex) {
const { node: modulator, stop } = fm( const { node: modulator, stop } = fm(
t, o, fmHarmonicity,
o,
fmHarmonicity,
fmHarmonicityAttack,
fmHarmonicityDecay,
fmModulationIndex, fmModulationIndex,
fmModulationAttack, fmWaveform
fmModulationDecay,
fmWaveform,
); );
modulator.connect(o.frequency); modulator.connect(o.frequency);
stopFm = stop; stopFm = stop;