mirror of
https://github.com/eliasstepanik/strudel-docker.git
synced 2026-01-26 21:18:49 +00:00
adding linear and exponential envelope for fm synthesis
This commit is contained in:
parent
c87c3c6672
commit
c3cd5d08c9
@ -39,6 +39,19 @@ 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);
|
||||||
|
|||||||
@ -1,8 +1,22 @@
|
|||||||
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 } from './helpers.mjs';
|
import { getOscillator, gainNode, getEnvelope, getExpEnvelope } from './helpers.mjs';
|
||||||
|
|
||||||
|
const fm = (osc, harmonicityRatio, modulationIndex, wave) => {
|
||||||
|
/*
|
||||||
|
* This function helps to setup a modulator for FM Synthesis
|
||||||
|
* @returns a modulator
|
||||||
|
*/
|
||||||
|
const carrfreq = osc.frequency.value;
|
||||||
|
const modfreq = carrfreq * harmonicityRatio;
|
||||||
|
const modgain = modfreq * modulationIndex;
|
||||||
|
return mod(modfreq, modgain, wave);
|
||||||
|
};
|
||||||
|
|
||||||
const mod = (freq, range = 1, type) => {
|
const mod = (freq, range = 1, type) => {
|
||||||
|
/*
|
||||||
|
* This function creates an oscillator
|
||||||
|
*/
|
||||||
const ctx = getAudioContext();
|
const ctx = getAudioContext();
|
||||||
const osc = ctx.createOscillator();
|
const osc = ctx.createOscillator();
|
||||||
osc.type = type;
|
osc.type = type;
|
||||||
@ -13,12 +27,6 @@ const mod = (freq, range = 1, type) => {
|
|||||||
return { node: g, stop: (t) => osc.stop(t) };
|
return { node: g, stop: (t) => osc.stop(t) };
|
||||||
};
|
};
|
||||||
|
|
||||||
const fm = (osc, harmonicityRatio, modulationIndex, wave) => {
|
|
||||||
const carrfreq = osc.frequency.value;
|
|
||||||
const modfreq = carrfreq * harmonicityRatio;
|
|
||||||
const modgain = modfreq * modulationIndex;
|
|
||||||
return mod(modfreq, modgain, wave);
|
|
||||||
};
|
|
||||||
|
|
||||||
export function registerSynthSounds() {
|
export function registerSynthSounds() {
|
||||||
['sine', 'square', 'triangle', 'sawtooth'].forEach((wave) => {
|
['sine', 'square', 'triangle', 'sawtooth'].forEach((wave) => {
|
||||||
@ -33,6 +41,12 @@ export function registerSynthSounds() {
|
|||||||
release = 0.01,
|
release = 0.01,
|
||||||
fmh: fmHarmonicity = 1,
|
fmh: fmHarmonicity = 1,
|
||||||
fmi: fmModulationIndex,
|
fmi: fmModulationIndex,
|
||||||
|
fmenv: fmEnvelopeType = 'linear',
|
||||||
|
fmattack: fmAttack = 0.001,
|
||||||
|
fmdecay: fmDecay = 0.2,
|
||||||
|
fmsustain: fmSustain = 0.001,
|
||||||
|
fmrelease: fmRelease = 0.1,
|
||||||
|
fmvelocity: fmVelocity = 1,
|
||||||
fmwave: fmWaveform = 'sine'
|
fmwave: fmWaveform = 'sine'
|
||||||
} = value;
|
} = value;
|
||||||
let { n, note, freq } = value;
|
let { n, note, freq } = value;
|
||||||
@ -49,14 +63,17 @@ export function registerSynthSounds() {
|
|||||||
// make oscillator
|
// make oscillator
|
||||||
const { node: o, stop } = getOscillator({ t, s: wave, freq });
|
const { node: o, stop } = getOscillator({ t, s: wave, freq });
|
||||||
|
|
||||||
let stopFm;
|
let stopFm, fmEnvelope;
|
||||||
if (fmModulationIndex) {
|
if (fmModulationIndex) {
|
||||||
const { node: modulator, stop } = fm(
|
const { node: modulator, stop } = fm( o, fmHarmonicity, fmModulationIndex, fmWaveform);
|
||||||
o, fmHarmonicity,
|
fmEnvelope = getEnvelope(fmAttack, fmDecay, fmSustain, fmRelease, fmVelocity, t);
|
||||||
fmModulationIndex,
|
if (fmEnvelopeType === "exp") {
|
||||||
fmWaveform
|
fmEnvelope = getExpEnvelope(fmAttack, fmDecay, fmVelocity, t);
|
||||||
);
|
fmEnvelope.node.maxValue = fmModulationIndex * 2;
|
||||||
modulator.connect(o.frequency);
|
fmEnvelope.node.minValue = 0.00001;
|
||||||
|
}
|
||||||
|
modulator.connect(fmEnvelope.node);
|
||||||
|
fmEnvelope.node.connect(o.frequency);
|
||||||
stopFm = stop;
|
stopFm = stop;
|
||||||
}
|
}
|
||||||
const g = gainNode(0.3);
|
const g = gainNode(0.3);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user