2020-08-22 Experimenting with Sonic Pi

OK! Real talk about generating music. Here’s some Sonic Pi code:

steps = (range -2, 2, inclusive: true)
steps = steps.push(false)

define :keys do |n|
  last = rand_i 4
  i = 0
  result = []
  result[i] = last
  while i < n
    i += 1
    step = steps.choose
    if step then
      last = last + step
      result[i] = last
    else
      result[i] = false
    end
  end
  print result
  return result
end

define :melody do |key|
  n = (scale key, :minor_pentatonic, num_octaves: 2)
  k = keys 8
  return k.map{ |i|
    if i == nil then :r
    else n[i]
    end
  }.ring
end

live_loop :piano do
  use_synth :piano
  m = melody(:c4).ring
  print m
  32.times do
    play m.tick, amp: 0.8, hard: 0.4
    sleep 0.25
  end
end

tablas = [:tabla_tas1, :tabla_tas3,
          :tabla_tas2, :tabla_tas3,
          false, false, false,
          false, false, false]

define :tabla_pattern do
  return 8.times.map{ tablas.choose }.ring
end

live_loop :tabla do
  p = tabla_pattern
  64.times do
    what = tabla_pattern.tick
    if what then
      play sample what, amp: 0.7
    end
    sleep 0.125
  end
end

Here’s how the melody is generated: a “melody” is eight keys, starting at a random one. Every subsequent key is generated from the previous one by adding a random step. The steps are -2, -1, 0, +1, +2, or a pause (“false”). These keys are used on a minor pentatonic scale over two octaves. In the example I’m using C4.

The live loop then plays 32 of these keys, so the “melody” of eight keys generated above is repeated four times before a new melody is generated.

The tablas are generated based on a “pattern” of eight beats. Each beat is picked from the tablas array, containing four samples and six pauses (“false”).

The live loop then plays 64 of these beats, twice as fast as the melody above (but remember, only 4/10 beats have a tabla sample).

Running from a slow enemy

Not bad! I’m going to try and work on the “endless soundtrack generator”. Something to remove the tablas and changes the piano to flutes for peaceful music, slows it down, adds a bunch of stuff to remind me of birds or whatever. We’ll see.

​#Music ​#Sonic Pi

Comments

(Please contact me if you want to remove your comment.)

I’ve had a lot of fun playing with FoxDot in the past and have toyed with the idea of a live performance. Cool stuff!

– main 2020-08-23 07:24 UTC

---

I did not know about FoxDot. Thanks! I listened to and watched that movie they have on heir front page and it seemed very different (“o-[-]-” and the like) but also very cool.

FoxDot

– Alex Schroeder 2020-08-23 11:49 UTC

---

If you like Sonic Pi and find FoxDot neat, you might really enjoy TidalCycles, my favorite of the bunch by far. I find its pattern creation both more intuitive and more powerful.

– elpherOpFHLR 2020-08-23 15:47 UTC

---

How does one choose between the various options? I’d choose Sonic Pi because Ruby looks a lot like Perl and I like the color scheme of the application. It was super easy to setup. I mean, I love Emacs and all that, but Sonic Pi didn’t even require me to set up an editor. It just worked. The tutorial is great. The PDF is great. How did you get started using TidalCycles? I remember looking at the website not too long ago and having no idea where to start. Did you just start watching the “Learning TidalCycles” course?

– Alex 2020-08-23 18:38 UTC