Tutorial
Nine steps, from one note to an exported piece. Each step is a complete program: use Run to compile it here, or Open to get it in a window of its own and play it.
1. A first note
composition "Step 1" {
instrument piano: GrandPiano {
play D4 duration 1/2
}
}One composition, one track, one note: the D above middle C, for a half note. Press Play (⌘↩). The waveform under the editor is the sound you just heard.
Change D4 to D3 and listen again; then duration 1/2 to duration 2bars.
2. A phrase and chords
composition "Step 2" {
tempo 92bpm
key D minor
instrument piano: GrandPiano {
chord [D3, A3, F4] duration 1/2
play A4 duration 1/4
play G4 duration 1/4
chord [Bb2, F3, D4] duration 1/2
play F4 duration 1/4
play E4 duration 1/4
chord [A2, E3, C#4] duration 1/1
}
}Statements follow one another in time. A list in brackets is a chord. tempo and key
describe the whole piece.
Instead of spelling every chord you can build them: replace the first chord line by
chord chord(D3, minor) duration 1/2.
3. Several instruments
composition "Step 3" {
tempo 92bpm
key D minor
let harmony = [chord(D3, minor), chord(Bb2, major), chord(F3, major), chord(A2, major)]
instrument piano: GrandPiano {
gain -4dB
for c in harmony {
arpeggio(c + (c + 12st), step: 1/8)
rest 1/4
}
}
instrument strings: ConcertStrings {
gain -10dB
articulation legato
for c in harmony {
chord c + 12st duration 1bar
}
}
instrument bass: DoubleBass {
gain -6dB
for c in harmony {
play c[0] - 12st duration 1bar
}
}
}Three tracks play together. The harmony is written once, as a list of chords, and each
instrument reads it in its own way: the piano as arpeggios, the strings as held chords, the
bass as roots. Change one chord in harmony and all three follow.
Open the Studio (⌘3) to see the three tracks and their notes, and the Mixer to balance them while the piece plays.
4. Rhythm
composition "Step 4" {
tempo 104bpm
swing 60%
instrument kick: Kick {
repeat 4 {
rhythm(C2, euclid(3, 8), step: 1/8)
}
}
instrument hat: ClosedHat {
gain -12dB
repeat 4 {
for i in 0..8 {
play F#2 duration 1/8 velocity if i % 2 == 0 then 0.7 else 0.4
}
}
}
instrument bass: FingerBass {
gain -4dB
repeat 2 {
play D2 duration 3/8
play D2 duration 1/8 staccato
play F2 duration 1/4
play A2 duration 1/4
play G2 duration 3/8
play G2 duration 1/8 staccato
tuplet 3 in 2 {
play A2 duration 1/8
play C3 duration 1/8
play A2 duration 1/8
}
play G2 duration 1/4
}
}
}repeat and for build patterns, euclid spreads three hits over eight steps, swing
delays the off-beats and tuplet writes a triplet. See Rhythm.
5. A synthesizer of your own
instrument Glow {
model patch
graph {
node osc = saw(freq: note.freq) + pulse(freq: note.freq * 0.5, width: 0.3) * 0.5
node env = adsr(attack: 10ms, decay: 400ms, sustain: 0.5, release: 600ms)
node tone = lowpass(in: osc, cutoff: 500Hz + env * 3000 * note.velocity, q: 2)
out tone * env * 0.4
}
}
composition "Step 5" {
tempo 96bpm
instrument lead: Glow {
for n in [D4, F4, A4, C5, A4, F4] {
play n duration 1/8 velocity 0.5 + (n - D4) / 24st
}
play D4 duration 1/2
effects {
delay(time: note_value(3/16), feedback: 0.35, mix: 0.25)
}
}
}model patch lets you draw the signal path of one voice: two oscillators, an envelope, a
filter that the envelope and the velocity open. Louder notes are brighter because
note.velocity multiplies the filter sweep. Change the cutoff expression and listen.
6. A physical instrument
instrument Koto {
model plucked_string
preset harp
damping 0.03
brightness 0.85
excitation pluck(position: 0.08)
}
composition "Step 6" {
tempo 80bpm
instrument koto: Koto {
for n in [D4, Eb4, G4, A4, Bb4, D5] {
play n duration 1/8
}
play A4 duration 1/4 bend { 0ms: 0st, 150ms: 0st, 400ms: 1st }
play D4 duration 1/2
effects {
reverb room(mix: 0.2)
}
}
}This instrument is a model of a plucked string. Plucking close to the end
(position: 0.08) gives a thin, bright tone; move it to 0.5 and the tone becomes round and
hollow, because plucking a string in its middle removes the even harmonics. The bend
imitates pressing the string behind the bridge.
7. Movement
composition "Step 7" {
tempo 110bpm
key E minor
instrument pad: SynthPad {
gain -8dB
automate gain {
0bars: -36dB
2bars: -8dB exp
}
automate instrument.filter.cutoff {
0bars: 250Hz
4bars: 6000Hz exp
}
automate pan {
0bars: -0.6
4bars: 0.6
}
chord chord(E3, min9) duration 2bars
chord chord(C3, maj7) duration 2bars
effects {
reverb hall(mix: 0.25)
}
}
}Three curves: the level fades in, the filter opens and the sound crosses from left to right. In the Studio the curves are drawn as dashed lines over the track.
8. Music from data
// Daily rainfall in millimetres over three weeks (an illustrative series).
const rain = [0, 0, 2, 11, 24, 8, 0, 0, 0, 5, 17, 31, 12, 3, 0, 0, 1, 9, 22, 6, 0]
composition "Step 8" {
tempo 150bpm
let most = max(rain)
let notes = scale_run(A2, pentatonic_minor, 3)
instrument drops: Kalimba {
for mm in rain {
if mm == 0 {
rest 1/8
} else {
let x = mm / most
play notes[round(x * (len(notes) - 1))] duration 1/8 velocity lerp(0.4, 1, x)
}
}
effects {
delay(time: note_value(1/8), feedback: 0.3, mix: 0.2)
}
}
}Dry days are silent; the more rain, the higher and louder the note. Mapping data to sound is three decisions: what is silence, what is pitch, what is loudness.
9. Look at the sound, then export it
composition "Step 9" {
instrument tone: FluteOrgan {
gain -12dB
play A4 duration 2bars
}
master {
peak_limit off
}
}A single held A. Play it and open WaveLab (⌘4): the spectrum of the master shows the
partials of the tone: the lowest at 440 Hz, the frequency of A4, then 1 320 Hz and 2 200 Hz
(this organ stop sounds the odd harmonics). Change the note to A3 and the whole pattern
moves down an octave, to 220, 660 and 1 100 Hz. How to read the spectrum, choose the window and the
size of the analysis is explained in WaveLab.
Then export: Composition › Render to Audio File… (⌘E), or
sonata render step9.sonata --output step9.wavThe command prints what was written: duration, peak, true peak and loudness. That is the whole path — text, sound, picture, file.
Where to go from here
- The pieces in
Examples/are complete compositions in many styles; open one from the Library space and change it. - Instruments, DSP and effects and Mixing go deeper into sound.
- Algorithmic composition is about letting the program compose.