mirror of
https://github.com/eliasstepanik/strudel-docker.git
synced 2026-01-21 02:28:34 +00:00
basic fm
This commit is contained in:
parent
01cccc6462
commit
0c124bcca3
@ -109,6 +109,32 @@ const generic_params = [
|
|||||||
*/
|
*/
|
||||||
['attack', 'att'],
|
['attack', 'att'],
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the Frequency Modulation Harmonicity Ratio.
|
||||||
|
* Controls the timbre of the sound.
|
||||||
|
* Whole numbers and simple ratios sound more natural,
|
||||||
|
* while decimal numbers and complex ratios sound metallic.
|
||||||
|
*
|
||||||
|
* @name fmh
|
||||||
|
* @param {number | Pattern} harmonicity
|
||||||
|
* @example
|
||||||
|
* note("c e g b").fm(4).fmh("<1 2 1.5 1.61>")
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
[['fmh', 'fmi'], 'fmh'],
|
||||||
|
/**
|
||||||
|
* Sets the Frequency Modulation of the synth.
|
||||||
|
* Controls the modulation index, which defines the brightness of the sound.
|
||||||
|
*
|
||||||
|
* @name fm
|
||||||
|
* @param {number | Pattern} brightness modulation index
|
||||||
|
* @synonyms fmi
|
||||||
|
* @example
|
||||||
|
* note("c e g b").fm("<0 1 2 8 32>")
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
[['fmi', 'fmh'], 'fm'],
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Select the sound bank to use. To be used together with `s`. The bank name (+ "_") will be prepended to the value of `s`.
|
* Select the sound bank to use. To be used together with `s`. The bank name (+ "_") will be prepended to the value of `s`.
|
||||||
*
|
*
|
||||||
|
|||||||
@ -2,13 +2,39 @@ import { midiToFreq, noteToMidi } from './util.mjs';
|
|||||||
import { registerSound } from './superdough.mjs';
|
import { registerSound } from './superdough.mjs';
|
||||||
import { getOscillator, gainNode, getEnvelope } from './helpers.mjs';
|
import { getOscillator, gainNode, getEnvelope } from './helpers.mjs';
|
||||||
|
|
||||||
|
const mod = (freq, range = 1, type = 'sine') => {
|
||||||
|
const ctx = getAudioContext();
|
||||||
|
const osc = ctx.createOscillator();
|
||||||
|
osc.type = type;
|
||||||
|
osc.frequency.value = freq;
|
||||||
|
osc.start();
|
||||||
|
const g = new GainNode(ctx, { gain: range });
|
||||||
|
osc.connect(g); // -range, range
|
||||||
|
return { node: g, stop: (t) => osc.stop(t) };
|
||||||
|
};
|
||||||
|
|
||||||
|
const fm = (osc, harmonicityRatio, modulationIndex, wave = 'sine') => {
|
||||||
|
const carrfreq = osc.frequency.value;
|
||||||
|
const modfreq = carrfreq * harmonicityRatio;
|
||||||
|
const modgain = modfreq * modulationIndex;
|
||||||
|
const { node: modulator, stop } = mod(modfreq, modgain, wave);
|
||||||
|
return { node: modulator, stop };
|
||||||
|
};
|
||||||
|
|
||||||
export function registerSynthSounds() {
|
export function registerSynthSounds() {
|
||||||
['sine', 'square', 'triangle', 'sawtooth'].forEach((wave) => {
|
['sine', 'square', 'triangle', 'sawtooth'].forEach((wave) => {
|
||||||
registerSound(
|
registerSound(
|
||||||
wave,
|
wave,
|
||||||
(t, value, onended) => {
|
(t, value, onended) => {
|
||||||
// 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 = 0.001, decay = 0.05, sustain = 0.6, release = 0.01 } = value;
|
const {
|
||||||
|
attack = 0.001,
|
||||||
|
decay = 0.05,
|
||||||
|
sustain = 0.6,
|
||||||
|
release = 0.01,
|
||||||
|
fmh: fmHarmonicity = 1,
|
||||||
|
fmi: fmModulationIndex,
|
||||||
|
} = 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
|
||||||
n = note || n || 36;
|
n = note || n || 36;
|
||||||
@ -22,6 +48,13 @@ export function registerSynthSounds() {
|
|||||||
// maybe pull out the above frequency resolution?? (there is also getFrequency but it has no default)
|
// maybe pull out the above frequency resolution?? (there is also getFrequency but it has no default)
|
||||||
// make oscillator
|
// make oscillator
|
||||||
const { node: o, stop } = getOscillator({ t, s: wave, freq });
|
const { node: o, stop } = getOscillator({ t, s: wave, freq });
|
||||||
|
|
||||||
|
let stopFm;
|
||||||
|
if (fmModulationIndex) {
|
||||||
|
const { node: modulator, stop } = fm(o, fmHarmonicity, fmModulationIndex);
|
||||||
|
modulator.connect(o.frequency);
|
||||||
|
stopFm = stop;
|
||||||
|
}
|
||||||
const g = gainNode(0.3);
|
const g = gainNode(0.3);
|
||||||
// envelope
|
// envelope
|
||||||
const { node: envelope, stop: releaseEnvelope } = getEnvelope(attack, decay, sustain, release, 1, t);
|
const { node: envelope, stop: releaseEnvelope } = getEnvelope(attack, decay, sustain, release, 1, t);
|
||||||
@ -34,7 +67,9 @@ export function registerSynthSounds() {
|
|||||||
node: o.connect(g).connect(envelope),
|
node: o.connect(g).connect(envelope),
|
||||||
stop: (releaseTime) => {
|
stop: (releaseTime) => {
|
||||||
releaseEnvelope(releaseTime);
|
releaseEnvelope(releaseTime);
|
||||||
stop(releaseTime + release);
|
let end = releaseTime + release;
|
||||||
|
stop(end);
|
||||||
|
stopFm?.(end);
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user