Simple Song using Prime Factorization

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


define :factorize do |n|
  while n >= p^2
    if n%p == 0
      puts p
      play scale(:c3, :major_pentatonic, num_octaves: 4)[p], release: 3
      n = n/p
  if n != 1
    puts n
    play scale(:c3, :major_pentatonic, num_octaves: 4)[n], release: 3
  if f == 1
    sample :perc_bell, rate: 0.5, cutoff: 90, amp: 0.5
  if f >= 4
    sample :drum_bass_soft, amp: 0.5
  sleep 1

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
  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

  factorize beats
  beats = beats+1

I love it :slight_smile:


@aoimatsu I love it! :grinning:

I’ve been doing some similar experiments with algorithms lately, was curious to see if it could be possible to create a “music piece” that also allows you to hear what the algorithm is doing under the hood…

I started with bubble sort but definitely, your idea of exploring prime factorization is more interesting :slight_smile:

Maybe this kind of approach has some educational value for people new to computer science?

Here is my code for bubble sort Sonic Pi version :joy:
Feel free to play around with it if you’re inspired, I’m having fun playing around with your code :upside_down_face:

# Bubble Sort by Earth To Abigail
unsorted_arr = [81, 79, 69, 59, 55, 71, 83, 52, 64, 74, 76, 62, 57, 67, 86, 88]

use_bpm 90

def sorted arr
  4.times do
    in_thread do
      arr.each { |n|
        play n, release: 0.1
        sleep 0.25
    in_thread do # Keeps track of the One
      sample :bd_tek
      sleep 16
    # Gives a nice and steady rhythm that marks we have successfully sorted the list
    sample :loop_breakbeat, beat_stretch: 4, amp: 2
    sleep 4

def bubble_sort array
  arr = array.dup # Modify a copy of the array in case we want to loop the function calls
  swapped = false
  r = arr.length - 2
  num_iters = 0
  arr.each { |n| play n; sleep 0.25 } # Initial array
  while true do
      swaps = 0
      num_iters += 1
      in_thread do
        use_synth :dsaw # Gives a base frequency (take lowest value of array)
        play 52, amp: 0.5, attack: 2, sustain: 6, decay: 2, release: 4, cutoff: 60
        sample :bd_tek # Tracking when we are entering the loop
      in_thread do # Gives a sense of how many iterations we've done so far
        num_iters.times do |i|
          sample :drum_cymbal_closed, amp: 1.0 + (i.to_f / 2.0), rate: 2
          sleep (2.0 / num_iters).round(2)
      for i in 0..r
        play arr[i], release: 0.1 # play the current value
        sleep 0.25
        if arr[i] > arr[i+1]
          arr[i], arr[i+1] = arr[i+1], arr[i]
          swapped = true if !swapped
          sample :elec_blip2, amp: 1.5 # Tracking when a swap occurs
          sleep 0.25
          play arr[i] # Which value was compared to current value
          sleep 0.25
          swaps += 1
      swapped ? swapped = false : break
    sorted arr # Plays the sorted array 4 times with different sound settings
  with_fx :reverb, room: 1 do
    live_loop :sort do
      bubble_sort unsorted_arr

In case you’re curious… I also made a live coding video where I mix different bubble sort loops and take the concept a bit further on a musical level:

Keep it up with Sonic Pi! Excited to see what you will come up with next :wink:


@EarthToAbigail I’m glad you like it! I really like your piece as well! The overall sound and structure is really well developed! (And your code is much neater :sweat_smile:)
I was approaching my piece on primes more with pure mathematics in mind, but I think your idea with the bubble sort digs better into the power of Sonic Pi to represent concepts in computer science directly though code. You could even do some interesting stuff with stochastic algorithms and aleatoric music if you were interested in randomness… Definitely has some potential for education at many levels.
Thanks for posting your code. It’s helpful to me as a beginner in this language, and it will be fun to mess around with! Your live coding video was super cool too! :smiley:

Thank you! I’m really happy you like it and see some educational potential in this :slight_smile:

I’m planning to explore different algorithms in the coming weeks and definitely, I love to work with randomness! I’m not familiar with what stochastic algorithms are but you spiked my curiosity, will do some research (I’m mostly self-taught…) :slight_smile:

Keep us updated if you make more experiments like this one!

1 Like

Great stuff, brilliant idea.

1 Like