Repository Public
1// ORIX Sonata2// File: Examples/st_primes.sonata3// Description: The integers from 2 to 97 as a pulse: every number is a soft marimba note, every prime rings a bell whose pitch is the prime folded into two octaves, and the gaps between primes become the rhythm.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7//8// category: Studies9// title: Primes10// teaches: computing in functions, loops with early return, modulo, mapping numbers to pitch11//12// expect: notes 13113// expect: tracks 314// expect: duration 21.7391s15// expect: deterministic16fn is_prime(n: Number) -> Bool {17 if n < 2 {18 return false19 }20 for d in 2..n {21 if d * d > n {22 return true23 }24 if n % d == 0 {25 return false26 }27 }28 return true29}3031composition "Primes" {32 tempo 138bpm33 key D minor3435 let notes = scale_run(D4, pentatonic_minor, 2)3637 instrument count: Marimba {38 gain -10dB39 pan -0.340 for n in 2..98 {41 play D3 duration 1/8 velocity if n % 10 == 0 then 0.7 else 0.342 }43 play D3 duration 1/244 }4546 instrument primes: Bell {47 gain -8dB48 pan 0.349 for n in 2..98 {50 if is_prime(n) {51 play notes[n % len(notes)] duration 1/8 velocity 0.7552 } else {53 rest 1/854 }55 }56 play D5 duration 1/257 effects {58 reverb plate(mix: 0.25)59 }60 }6162 instrument twins: Glockenspiel {63 gain -14dB64 // Twin primes (p and p + 2 both prime) get a second, higher bell.65 for n in 2..98 {66 if is_prime(n) and is_prime(n + 2) {67 play A6 duration 1/868 } else {69 rest 1/870 }71 }72 }73}74