Tutorial
How to write different notes different ways
Revisiting our synth from last time we have this pretty straight forward pattern:
fn synth(freq, partials=2, fundVol=1, cutoff=80) {
let sig = for i in 1..=partials { saw(freq * i) * fundVol/i } *
env(0.01, 0.1, 0.8, 0.1, dur*0.8)
sig >> lowpass(cutoff, 0.5)
}
play([80, 80, 80, 80], synth)
We are free to keep writing frequencies if we choose. Let’s make it more musical
play([73.42, 87.31, 82.41, 58.27], synth)
You can write out the hertz for d2, f2 and e2 and b flat 1. Signal generators take hertz as an input parameter by default.
Or you can use midi values. For those we need to add a conversion in our synth
fn synth(n, partials=2, fundVol=1, cutoff=80) {
let sig = for i in 1..=partials { saw(n.m2h * i) * fundVol/i } *
env(0.01, 0.1, 0.8, 0.1, dur*0.8)
sig >> lowpass(cutoff, 0.5)
}
play([38, 41, 40, 34], synth)
For convention and clarity’s sake I change freq to n for note.
And since we are passing a midi value in we use n.m2h.
This function converts the midi value to hertz.
Once we have that conversion in our synth, we don’t need to use numbers any more, we can write:
play([d2, f2, e2, bf1], synth)
Note that f stands for flat and s stands for sharp.
I don’t even need to repeat the octave numbers until I need to change octaves.
play([d2, f, e2, bf1], synth)
E is a special case, because it clashes with the rhythm e introduced later.
You can use octave numbers from 0 to 9 representing all of the standard midi pitches except for the bottom -1 octave.
Check out this reference