Skip to content

Commit 0c124bc

Browse files
committed
basic fm
1 parent 01cccc6 commit 0c124bc

File tree

2 files changed

+63
-2
lines changed

2 files changed

+63
-2
lines changed

packages/core/controls.mjs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,32 @@ const generic_params = [
109109
*/
110110
['attack', 'att'],
111111

112+
/**
113+
* Sets the Frequency Modulation Harmonicity Ratio.
114+
* Controls the timbre of the sound.
115+
* Whole numbers and simple ratios sound more natural,
116+
* while decimal numbers and complex ratios sound metallic.
117+
*
118+
* @name fmh
119+
* @param {number | Pattern} harmonicity
120+
* @example
121+
* note("c e g b").fm(4).fmh("<1 2 1.5 1.61>")
122+
*
123+
*/
124+
[['fmh', 'fmi'], 'fmh'],
125+
/**
126+
* Sets the Frequency Modulation of the synth.
127+
* Controls the modulation index, which defines the brightness of the sound.
128+
*
129+
* @name fm
130+
* @param {number | Pattern} brightness modulation index
131+
* @synonyms fmi
132+
* @example
133+
* note("c e g b").fm("<0 1 2 8 32>")
134+
*
135+
*/
136+
[['fmi', 'fmh'], 'fm'],
137+
112138
/**
113139
* Select the sound bank to use. To be used together with `s`. The bank name (+ "_") will be prepended to the value of `s`.
114140
*

packages/superdough/synth.mjs

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,39 @@ import { midiToFreq, noteToMidi } from './util.mjs';
22
import { registerSound } from './superdough.mjs';
33
import { getOscillator, gainNode, getEnvelope } from './helpers.mjs';
44

5+
const mod = (freq, range = 1, type = 'sine') => {
6+
const ctx = getAudioContext();
7+
const osc = ctx.createOscillator();
8+
osc.type = type;
9+
osc.frequency.value = freq;
10+
osc.start();
11+
const g = new GainNode(ctx, { gain: range });
12+
osc.connect(g); // -range, range
13+
return { node: g, stop: (t) => osc.stop(t) };
14+
};
15+
16+
const fm = (osc, harmonicityRatio, modulationIndex, wave = 'sine') => {
17+
const carrfreq = osc.frequency.value;
18+
const modfreq = carrfreq * harmonicityRatio;
19+
const modgain = modfreq * modulationIndex;
20+
const { node: modulator, stop } = mod(modfreq, modgain, wave);
21+
return { node: modulator, stop };
22+
};
23+
524
export function registerSynthSounds() {
625
['sine', 'square', 'triangle', 'sawtooth'].forEach((wave) => {
726
registerSound(
827
wave,
928
(t, value, onended) => {
1029
// destructure adsr here, because the default should be different for synths and samples
11-
const { attack = 0.001, decay = 0.05, sustain = 0.6, release = 0.01 } = value;
30+
const {
31+
attack = 0.001,
32+
decay = 0.05,
33+
sustain = 0.6,
34+
release = 0.01,
35+
fmh: fmHarmonicity = 1,
36+
fmi: fmModulationIndex,
37+
} = value;
1238
let { n, note, freq } = value;
1339
// with synths, n and note are the same thing
1440
n = note || n || 36;
@@ -22,6 +48,13 @@ export function registerSynthSounds() {
2248
// maybe pull out the above frequency resolution?? (there is also getFrequency but it has no default)
2349
// make oscillator
2450
const { node: o, stop } = getOscillator({ t, s: wave, freq });
51+
52+
let stopFm;
53+
if (fmModulationIndex) {
54+
const { node: modulator, stop } = fm(o, fmHarmonicity, fmModulationIndex);
55+
modulator.connect(o.frequency);
56+
stopFm = stop;
57+
}
2558
const g = gainNode(0.3);
2659
// envelope
2760
const { node: envelope, stop: releaseEnvelope } = getEnvelope(attack, decay, sustain, release, 1, t);
@@ -34,7 +67,9 @@ export function registerSynthSounds() {
3467
node: o.connect(g).connect(envelope),
3568
stop: (releaseTime) => {
3669
releaseEnvelope(releaseTime);
37-
stop(releaseTime + release);
70+
let end = releaseTime + release;
71+
stop(end);
72+
stopFm?.(end);
3873
},
3974
};
4075
},

0 commit comments

Comments
 (0)