Repository Public
1// ORIX Sonata2// File: Examples/lang_functions_and_lists.sonata3// Description: A tour of list functions: one motif is reversed, rotated, cycled, stretched and filtered by small pure functions, and each result is played so the code and the sound can be compared line by line.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7//8// category: Language9// title: Functions and Lists10// teaches: fn with typed parameters, list built-ins, building lists in loops, default arguments11//12// expect: notes 6613// expect: tracks 114// expect: duration 23.5345s15// expect: deterministic16const motif = [C4, E4, G4, B4, A4, F4]1718// Every other element.19fn alternate(xs: List<Pitch>, start: Number = 0) -> List<Pitch> {20 let acc = []21 for i in 0..len(xs) {22 if i % 2 == start {23 acc = acc + [xs[i]]24 }25 }26 return acc27}2829// The motif mirrored around its first note.30fn mirror(xs: List<Pitch>) -> List<Pitch> {31 let acc = []32 for x in xs {33 acc = acc + [xs[0] - interval(xs[0], x)]34 }35 return acc36}3738// Two lists woven together.39fn weave(xs: List<Pitch>, ys: List<Pitch>) -> List<Pitch> {40 let acc = []41 for i in 0..min(len(xs), len(ys)) {42 acc = acc + [xs[i], ys[i]]43 }44 return acc45}4647pattern say(notes: List<Pitch>) {48 for n in notes {49 play n duration 1/850 }51 rest 1/452}5354composition "Functions and Lists" {55 tempo 116bpm56 key C major5758 instrument vibes: Vibraphone {59 gain -5dB60 say(motif)61 say(reverse(motif))62 say(rotate(motif, 2))63 say(take(motif, 3) + drop(motif, 4))64 say(cycle(take(motif, 2), 6))65 say(alternate(motif))66 say(alternate(motif, start: 1))67 say(mirror(motif))68 say(weave(motif, motif + 12st))69 say(up_down(motif))70 chord [motif.first, motif[2], motif.last] duration 1/171 effects {72 reverb room(mix: 0.2)73 }74 }75}76