Fibonacci Sequences

I saw a video recently showing a fibonacci sequence mapped to musical scales.
Here is my version made in Sonic Pi:

define :fibonacci do |s0,s1,m|
  #s0,s1 are the seed
  #m is the modulo
  sequence = [s0,s1]
  f = true  #Set to false when the looping point is detected
  i = 0  #Index
  while f do
    n = (sequence[i]+sequence[i+1])%m
    sequence += [n]
    f = false if (sequence[i+1]+n)%m == sequence[0] && (n + sequence[0])%m == sequence[1]
    i += 1
  end
  return sequence
end

And in action:

3 Likes

Very nice. Thanks for sharing the code. I played around a bit a came up with this.

# 231115 2347 fibonacci music with beep
# Saved 231115 2347
# https://in-thread.sonic-pi.net/t/fibonacci-sequences/8369/2

define :fibonacci do |s0,s1,m|
  #s0,s1 are the seed
  #m is the modulo
  sequence = [s0,s1]
  f = true  #Set to false when the looping point is detected
  i = 0  #Index
  while f do
    n = (sequence[i]+sequence[i+1])%m
    sequence += [n]
    f = false if (sequence[i+1]+n)%m == sequence[0] && (n + sequence[0])%m == sequence[1]
    i += 1
  end
  return sequence
end

with_fx :ping_pong do
  #with_fx :reverb, room: 0.75 do
  
  notes = fibonacci 1,1,10
  
  notes.length.times do
    tick
    puts notes.look
    
    use_synth_defaults  release: [0.1,0.2,0.3].choose
    
    use_synth :beep
    play (scale :d4, :minor)[notes.look], pan: rdist([0,0.2,0.4,0.6,0.8,1].choose)
    
    / bass /
    play (scale :d2, :minor)[notes.look], pan: rdist([0,0.2,0.4,0.6,0.8,1].shuffle.choose), amp: knit(0,8, 1,4).look
    
    #sleep 0.125*[1,2,3,5].choose
    sleep 0.125*notes.look
  end
  #end
end
1 Like

Those comments at the top of your script @Relaxnow … Are they manually typed, or is there a way to get them inserted?

Just curious as seems like a good idea, and semi-common

Hopefully not too stupid a question!

I type manually to keep track of the code.
Filename is “231115 2347 fibonacci music with beep”
“231115 2347” to get is sorted nicely in the the windows explorer

I usually work on the code in Sonic Pi not saving as a “.rb” file, but when I do that, I write the “# Saved 231115 2347” just to keep track of that.

I usually have multiple buffers with different projects going at the same time, so it can be hard to keep track without notes, so notes helps me with that.