SPB Git

Repository Public

st_collatz.sonata 1.9 KB 65 lines

1// ORIX Sonata2// File: Examples/st_collatz.sonata3// Description: The 3n + 1 problem as melody: the Collatz sequences of 27, 97 and 871 are played until they reach 1, the size of each number mapped logarithmically to pitch, so the famous climbs and collapses are audible.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7//8// category: Studies9// title: Collatz Paths10// teaches: iteration with a bound, logarithmic mapping with ln(), building a list in a function, quota-safe loops11//12// expect: notes 26413// expect: tracks 314// expect: duration 14.9062s15// expect: deterministic16// The sequence from `n` down to 1, cut at `limit` steps so it always terminates.17fn collatz(n: Number, limit: Number) -> List<Number> {18    let seq = [n]19    let x = n20    repeat limit {21        if x != 1 {22            x = if x % 2 == 0 then x / 2 else 3 * x + 123            seq = seq + [x]24        }25    }26    return seq27}2829pattern path(start: Number, low: Pitch, step: Duration, limit: Number, spread: Number = 4) {30    for x in collatz(start, limit) {31        // `spread` semitones per doubling: the pitch is the logarithm of the number.32        play low + 1st * round(spread * ln(x) / ln(2)) duration step velocity if x % 2 == 0 then 0.5 else 0.833    }34}3536composition "Collatz Paths" {37    tempo 160bpm3839    instrument harp: Harp {40        gain -5dB41        pan -0.242        // 27 takes 111 steps and climbs to 9232.43        path(27, C2, 1/16, 111)44        play C2 duration 1/245        effects {46            reverb hall(mix: 0.2)47        }48    }4950    instrument vibes: Vibraphone {51        gain -8dB52        pan 0.353        rest 2bars54        // 97 takes 118 steps.55        path(97, C2, 1/16, 118)56        play C3 duration 1/257    }5859    instrument bass: FingerBass {60        gain -6dB61        // 871 takes 178 steps: here only every step as a slow bass, cut at 30.62        path(871, C0, 1/4, 30, spread: 3)63    }64}65
Files main
Menu

Theme