# Fibonacci Sequences

**URL:** <https://in-thread.sonic-pi.net/t/fibonacci-sequences/8369>\
**Category:** Creations & Ideas\
**Created:** [November 15, 2023, 8:08pm UTC](https://in-thread.sonic-pi.net/t/fibonacci-sequences/8369 "2023-11-15T20:08:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Davids-Music-Lab](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/davids-music-lab/32/512_2.png) [@Davids-Music-Lab](https://in-thread.sonic-pi.net/u/Davids-Music-Lab)\
**Post date:** [November 15, 2023, 8:08pm UTC](https://in-thread.sonic-pi.net/t/fibonacci-sequences/8369/1 "2023-11-15T20:08:28Z")

</div>

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

```ruby
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:

[![](https://img.youtube.com/vi/xj3OxBxSPY4/maxresdefault.jpg "Fibonacci Experiment 2 - Now in a Function") ](https://www.youtube.com/watch?v=xj3OxBxSPY4)

---

<div class="post-metadata">

**Author:** ![Relaxnow](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/relaxnow/32/2172_2.png) [@Relaxnow](https://in-thread.sonic-pi.net/u/Relaxnow)\
**Post date:** [November 15, 2023, 10:45pm UTC](https://in-thread.sonic-pi.net/t/fibonacci-sequences/8369/2 "2023-11-15T22:45:21Z")

</div>

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

```ruby
# 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

```

---

<div class="post-metadata">

**Author:** ![jonny](https://avatars.discourse-cdn.com/v4/letter/j/47e85d/32.png) [@jonny](https://in-thread.sonic-pi.net/u/jonny)\
**Post date:** [November 17, 2023, 6:30pm UTC](https://in-thread.sonic-pi.net/t/fibonacci-sequences/8369/3 "2023-11-17T18:30:31Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![Relaxnow](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/relaxnow/32/2172_2.png) [@Relaxnow](https://in-thread.sonic-pi.net/u/Relaxnow)\
**Post date:** [November 17, 2023, 11:41pm UTC](https://in-thread.sonic-pi.net/t/fibonacci-sequences/8369/4 "2023-11-17T23:41:48Z")

</div>

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.
