DEV Community

Cover image for Today I learned: how to generate music with Sonic Pi!
Fen Slattery
Fen Slattery

Posted on

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!

Top comments (16)

Collapse
 
ben profile image
Ben Halpern

Sonic Pi is one of the coolest things. I wish I had any musical talent. 😳

Collapse
 
andrerpena profile image
André Pena

Oh my God you hooked me into sonic-pi!

Collapse
 
goku132 profile image
Michael

Sonic Pi made it so much more easier for music programming. Languages like Csound and supercollider are really good. But have a big learning curve. Sonic Pi is a very fun, great to help people understand programming basics as well.

Collapse
 
itsmeseb profile image
sebkolind

That is so cool!! Gj :)

Collapse
 
nguyenquangtin profile image
Tony Tin Nguyen

It's awesome. Thanks Fen for introduce to us.

Collapse
 
daveyman123 profile image
David Graff

I made this site so people can collaboratively code sonic-pi online makemusiconline.net:8080

Collapse
 
socratesdz profile image
Sócrates Díaz

This is really cool! Thanks for sharing! :D

Collapse
 
gauravchaddha1996 profile image
Gaurav Chaddha

This is so cool. Thanks for writing about this.

Collapse
 
hutepa profile image
Mahmoud Abdelsalam

oh my!! I love this.

Collapse
 
guneyozsan profile image
Guney Ozsan

I can't miss experimenting with this. Thanks for pointing out!

Some comments may only be visible to logged-in visitors. Sign in to view all comments.