Hello! I just started learning Sonic Pi this week and I was curious about its potential to represent prime factorizations, since they could be thought of in cycles of prime lengths over the number line. I made this simple program to map each number’s prime factors to notes on the pentatonic scale. A bell rings when a number is prime and drum beats if a number has four or more factors. The :amb_drone
samples are random. I tried to spruce it up so it would sound nice, but I’m not too savvy with the range of sounds the program has built in yet…
# Primes by aoimatsu
use_bpm 240
use_random_seed Time.now.to_i
define :factorize do |n|
p=2
f=0
while n >= p^2
if n%p == 0
f=f+1
puts p
play scale(:c3, :major_pentatonic, num_octaves: 4)[p], release: 3
n = n/p
else
p=p+1
end
end
if n != 1
f=f+1
puts n
play scale(:c3, :major_pentatonic, num_octaves: 4)[n], release: 3
end
if f == 1
sample :perc_bell, rate: 0.5, cutoff: 90, amp: 0.5
end
if f >= 4
sample :drum_bass_soft, amp: 0.5
end
sleep 1
end
sample :perc_bell, rate: 0.5, cutoff: 90
sleep 30
sample :drum_bass_soft, amp: 0.5
sleep 2
beats = 2
loop do
puts beats
if (beats%16)==0
sample :ambi_drone
end
if ((dice 50) == 24) && (beats >= 16)
in_thread do
2.times do
sample :ambi_drone, rate: 2, pan: rrand(-0.5,0.5)
sleep (sample_duration :ambi_drone)/2
end
end
end
factorize beats
beats = beats+1
end