Repository Public
1<!--2ORIX Sonata3File: Documentation/manual/tutorial.md4Author: Simon-Pierre Boucher5Contact: contact@spboucher.ai6-->7# Tutorial89Nine steps, from one note to an exported piece. Each step is a complete program: use **Run**10to compile it here, or **Open** to get it in a window of its own and play it.1112## 1. A first note1314```sonata15composition "Step 1" {16 instrument piano: GrandPiano {17 play D4 duration 1/218 }19}20```2122One composition, one track, one note: the D above middle C, for a half note. Press Play23(⌘↩). The waveform under the editor is the sound you just heard.2425Change `D4` to `D3` and listen again; then `duration 1/2` to `duration 2bars`.2627## 2. A phrase and chords2829```sonata30composition "Step 2" {31 tempo 92bpm32 key D minor33 instrument piano: GrandPiano {34 chord [D3, A3, F4] duration 1/235 play A4 duration 1/436 play G4 duration 1/437 chord [Bb2, F3, D4] duration 1/238 play F4 duration 1/439 play E4 duration 1/440 chord [A2, E3, C#4] duration 1/141 }42}43```4445Statements follow one another in time. A list in brackets is a chord. `tempo` and `key`46describe the whole piece.4748Instead of spelling every chord you can build them: replace the first chord line by49`chord chord(D3, minor) duration 1/2`.5051## 3. Several instruments5253```sonata54composition "Step 3" {55 tempo 92bpm56 key D minor5758 let harmony = [chord(D3, minor), chord(Bb2, major), chord(F3, major), chord(A2, major)]5960 instrument piano: GrandPiano {61 gain -4dB62 for c in harmony {63 arpeggio(c + (c + 12st), step: 1/8)64 rest 1/465 }66 }6768 instrument strings: ConcertStrings {69 gain -10dB70 articulation legato71 for c in harmony {72 chord c + 12st duration 1bar73 }74 }7576 instrument bass: DoubleBass {77 gain -6dB78 for c in harmony {79 play c[0] - 12st duration 1bar80 }81 }82}83```8485Three tracks play together. The harmony is written once, as a list of chords, and each86instrument reads it in its own way: the piano as arpeggios, the strings as held chords, the87bass as roots. Change one chord in `harmony` and all three follow.8889Open the **Studio** (⌘3) to see the three tracks and their notes, and the **Mixer** to90balance them while the piece plays.9192## 4. Rhythm9394```sonata95composition "Step 4" {96 tempo 104bpm97 swing 60%9899 instrument kick: Kick {100 repeat 4 {101 rhythm(C2, euclid(3, 8), step: 1/8)102 }103 }104105 instrument hat: ClosedHat {106 gain -12dB107 repeat 4 {108 for i in 0..8 {109 play F#2 duration 1/8 velocity if i % 2 == 0 then 0.7 else 0.4110 }111 }112 }113114 instrument bass: FingerBass {115 gain -4dB116 repeat 2 {117 play D2 duration 3/8118 play D2 duration 1/8 staccato119 play F2 duration 1/4120 play A2 duration 1/4121 play G2 duration 3/8122 play G2 duration 1/8 staccato123 tuplet 3 in 2 {124 play A2 duration 1/8125 play C3 duration 1/8126 play A2 duration 1/8127 }128 play G2 duration 1/4129 }130 }131}132```133134`repeat` and `for` build patterns, `euclid` spreads three hits over eight steps, `swing`135delays the off-beats and `tuplet` writes a triplet. See [Rhythm](rhythm.md).136137## 5. A synthesizer of your own138139```sonata140instrument Glow {141 model patch142 graph {143 node osc = saw(freq: note.freq) + pulse(freq: note.freq * 0.5, width: 0.3) * 0.5144 node env = adsr(attack: 10ms, decay: 400ms, sustain: 0.5, release: 600ms)145 node tone = lowpass(in: osc, cutoff: 500Hz + env * 3000 * note.velocity, q: 2)146 out tone * env * 0.4147 }148}149150composition "Step 5" {151 tempo 96bpm152 instrument lead: Glow {153 for n in [D4, F4, A4, C5, A4, F4] {154 play n duration 1/8 velocity 0.5 + (n - D4) / 24st155 }156 play D4 duration 1/2157 effects {158 delay(time: note_value(3/16), feedback: 0.35, mix: 0.25)159 }160 }161}162```163164`model patch` lets you draw the signal path of one voice: two oscillators, an envelope, a165filter that the envelope and the velocity open. Louder notes are brighter because166`note.velocity` multiplies the filter sweep. Change the `cutoff` expression and listen.167168## 6. A physical instrument169170```sonata171instrument Koto {172 model plucked_string173 preset harp174 damping 0.03175 brightness 0.85176 excitation pluck(position: 0.08)177}178179composition "Step 6" {180 tempo 80bpm181 instrument koto: Koto {182 for n in [D4, Eb4, G4, A4, Bb4, D5] {183 play n duration 1/8184 }185 play A4 duration 1/4 bend { 0ms: 0st, 150ms: 0st, 400ms: 1st }186 play D4 duration 1/2187 effects {188 reverb room(mix: 0.2)189 }190 }191}192```193194This instrument is a model of a plucked string. Plucking close to the end195(`position: 0.08`) gives a thin, bright tone; move it to `0.5` and the tone becomes round and196hollow, because plucking a string in its middle removes the even harmonics. The `bend`197imitates pressing the string behind the bridge.198199## 7. Movement200201```sonata202composition "Step 7" {203 tempo 110bpm204 key E minor205206 instrument pad: SynthPad {207 gain -8dB208 automate gain {209 0bars: -36dB210 2bars: -8dB exp211 }212 automate instrument.filter.cutoff {213 0bars: 250Hz214 4bars: 6000Hz exp215 }216 automate pan {217 0bars: -0.6218 4bars: 0.6219 }220 chord chord(E3, min9) duration 2bars221 chord chord(C3, maj7) duration 2bars222 effects {223 reverb hall(mix: 0.25)224 }225 }226}227```228229Three curves: the level fades in, the filter opens and the sound crosses from left to right.230In the Studio the curves are drawn as dashed lines over the track.231232## 8. Music from data233234```sonata235// Daily rainfall in millimetres over three weeks (an illustrative series).236const rain = [0, 0, 2, 11, 24, 8, 0, 0, 0, 5, 17, 31, 12, 3, 0, 0, 1, 9, 22, 6, 0]237238composition "Step 8" {239 tempo 150bpm240 let most = max(rain)241 let notes = scale_run(A2, pentatonic_minor, 3)242243 instrument drops: Kalimba {244 for mm in rain {245 if mm == 0 {246 rest 1/8247 } else {248 let x = mm / most249 play notes[round(x * (len(notes) - 1))] duration 1/8 velocity lerp(0.4, 1, x)250 }251 }252 effects {253 delay(time: note_value(1/8), feedback: 0.3, mix: 0.2)254 }255 }256}257```258259Dry days are silent; the more rain, the higher and louder the note. Mapping data to sound is260three decisions: what is silence, what is pitch, what is loudness.261262## 9. Look at the sound, then export it263264```sonata265composition "Step 9" {266 instrument tone: FluteOrgan {267 gain -12dB268 play A4 duration 2bars269 }270 master {271 peak_limit off272 }273}274```275276A single held A. Play it and open **WaveLab** (⌘4): the spectrum of the master shows the277partials of the tone: the lowest at 440 Hz, the frequency of A4, then 1 320 Hz and 2 200 Hz278(this organ stop sounds the odd harmonics). Change the note to `A3` and the whole pattern279moves down an octave, to 220, 660 and 1 100 Hz. How to read the spectrum, choose the window and the280size of the analysis is explained in [WaveLab](wavelab.md).281282Then export: **Composition › Render to Audio File…** (⌘E), or283284```text285sonata render step9.sonata --output step9.wav286```287288The command prints what was written: duration, peak, true peak and loudness. That is the289whole path — text, sound, picture, file.290291## Where to go from here292293* The pieces in `Examples/` are complete compositions in many styles; open one from the294 **Library** space and change it.295* [Instruments](instruments.md), [DSP and effects](effects.md) and [Mixing](mixing.md) go296 deeper into sound.297* [Algorithmic composition](algorithmic-composition.md) is about letting the program compose.298