Today I learned: how to generate music with Sonic Pi!

sublimemarch

Fen Slattery

Posted on October 3, 2018

Today I learned: how to generate music with Sonic Pi!

Sonic Pi is a live-coding music program that uses Ruby to generate synth music. Ever since I learned about Algorave earlier this year, I've been really interested in trying out generating music with code, and Sonic Pi seemed like a great place to start.

Sonic Pi is free and open-source, and available for Windows, macOS, and Raspberry Pi. It has a fantastic built-in tutorial that had me writing music right away, and I was really pleased with the quality of the tutorial. I already know Ruby and some intermediate music theory, but it doesn't assume any prior programming or music knowledge.

In about two hours or so, I completed the tutorial and whipped together some rad synth sounds to have on in the background while I code. Check it out on Soundcloud, the code is below!

live_loop :background do
  sample :loop_garzul
  use_synth :prophet
  play :c1, release: 4, cutoff: rrand(70, 130)
  sleep 4
  play :c2, release: 4, cutoff: rrand(70, 130)
end

live_loop :middle do
  sync :background
  use_random_seed 4923
  use_synth :tb303
  notes = (scale :c2, :minor_pentatonic, num_octaves: 1)
  sleep 4
  8.times do
    play notes.choose, release: 1, cutoff: rrand(30, 50), amp: 0.3
    sleep 0.5
  end

  sleep 4

  8.times do
    play notes.choose, release: 1, cutoff: rrand(30, 50), amp: 0.3
    sleep 0.5
  end
end

There's a pretty active Sonic Pi Twitter account that shares recent creations (some of which can fit in a single tweet!) as well as a forum which I haven't explored much. Have you used Sonic Pi or something else to generate music? Let me know in the comments!

💖 💪 🙅 🚩
sublimemarch
Fen Slattery

Posted on October 3, 2018

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related