Repository Public
1// ORIX Sonata2// File: Examples/lang_recursion.sonata3// Description: Safe recursion: a function divides a duration in halves and thirds until a minimum length, producing a self-similar rhythm; the depth is bounded by construction, far below the 64-call limit.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7//8// category: Language9// title: Recursion with a Floor10// teaches: recursive functions returning lists, durations as values, a termination condition, the call-depth limit11//12// expect: notes 6813// expect: tracks 214// expect: duration 18.2609s15// expect: deterministic16// Splits `d` into smaller values: halves at even depths, thirds at odd ones, down to `floor_value`.17fn split(d: Duration, depth: Number, floor_value: Duration) -> List<Duration> {18 if d <= floor_value or depth >= 6 {19 return [d]20 }21 if depth % 2 == 0 {22 // A long part and a short part that is split again.23 return [d / 2] + split(d / 2, depth + 1, floor_value)24 }25 return split(d / 3, depth + 1, floor_value) + [d / 3, d / 3]26}2728composition "Recursion with a Floor" {29 tempo 92bpm30 key A minor3132 let notes = scale_run(A3, minor, 2)3334 instrument marimba: Marimba {35 gain -5dB36 let i = 037 repeat 6 {38 // Each bar is one whole note split recursively; the pitch walks up the scale.39 for d in split(note_value(1/1), 0, note_value(1/32)) {40 play notes[i % len(notes)] duration d velocity if d >= note_value(1/8) then 0.8 else 0.541 i = i + 242 }43 }44 play notes[0] duration 1/145 }4647 instrument bass: DoubleBass {48 gain -6dB49 articulation pizzicato50 repeat 6 {51 // The same function, one level only: a half and two quarter... thirds.52 for d in split(note_value(1/1), 4, note_value(1/4)) {53 play A1 duration d54 }55 }56 play A1 duration 1/157 }58}59