Tutorial
Let's make some music
Here’s a workable bass from last chapter.
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, 1, 1)
sig >> lowpass(cutoff, 0.5)
}
synth(80, 10, 0.8)
It’s tedious to keep pressing play when we want to hear a sound, so let’s make a loop.
Patterns in swync are lists. The following would be a valid pattern.
[80, 80, 80, 80]
To use this to generate a loop, we need to use play. At it’s most basic we only need to pass play a pattern and function that generates a signal.
play([80, 80, 80, 80], synth)
Ok, that was meh. The releases are fixed times and the decay is one second long, so the audio simply builds up and we don’t hear individual notes.
To change this, we need to change the synth and use one of the builtin variables that a function has access to: dur This function allows an instrument to know the duration of its call in context of the pattern.
Here I reduce the release and make the env duration shorter than its full value to hear the defined individual notes.
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)
Ok, we have a loop that will play endlessly or until I hit stop ⌘ . or Ctrl ..