Repository Public
1// ORIX Sonata2// File: Examples/lang_const_tables.sonata3// Description: A whole arrangement driven by tables: chords, a bass rhythm and a strumming mask are constants, and the tracks are short loops that read them, so changing the song means editing data, not code.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7//8// category: Language9// title: A Song as Data10// teaches: const tables, nested lists, indexing, loops that read data, separating data from playing11//12// expect: notes 31413// expect: tracks 314// expect: duration 20.7692s15// expect: deterministic16// Root, quality and length in bars of every chord of the form.17const progression = [[D3, "major", 1], [B2, "minor", 1], [G2, "major", 1], [A2, "dom7", 1], [D3, "major", 1], [G2, "major", 1], [A2, "sus4", 1], [D3, "major", 1]]1819// Sixteenth-note masks for one bar: 1 = down-stroke, 2 = up-stroke (softer), 0 = silence.20const strum_mask = [1, 0, 2, 0, 1, 0, 2, 2, 0, 2, 1, 0, 2, 0, 1, 2]21const bass_mask = [1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 2, 0]2223// A row mixes a pitch, a name and a number, so the parameter carries no type annotation.24fn chord_of(row) -> Chord {25 return chord(row[0], row[1])26}2728composition "A Song as Data" {29 tempo 104bpm30 key D major3132 instrument guitar: SteelGuitar {33 gain -6dB34 pan -0.235 for row in progression {36 repeat row[2] {37 for s in strum_mask {38 if s == 0 {39 rest 1/1640 } else {41 chord chord_of(row) + 12st duration 1/16 velocity if s == 1 then 0.75 else 0.4542 }43 }44 }45 }46 chord chord_of(progression[0]) + 12st duration 1/147 }4849 instrument bass: FingerBass {50 gain -5dB51 for row in progression {52 repeat row[2] {53 for s in bass_mask {54 if s == 0 {55 rest 1/1656 } else {57 // 2 is the fifth of the chord, an octave down.58 play if s == 1 then row[0] - 12st else row[0] - 5st duration 1/16 tenuto59 }60 }61 }62 }63 play progression[0][0] - 12st duration 1/164 }6566 instrument pad: SynthStrings {67 gain -14dB68 pan 0.269 for row in progression {70 chord chord_of(row) + 12st duration 1bar * row[2] velocity 0.571 }72 chord chord_of(progression[0]) + 12st duration 1/1 velocity 0.573 }74}75