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!

Latest comments (16)

Collapse
 
garrett profile image
Garrett / G66

This is awesome! The track reminds me a lot of the song that Aphex Twin remixed for Nine Inch Nails - Further Down The Spiral

Collapse
 
isalevine profile image
Isa Levine

omg this is amazing! i was JUST wondering if i knew enough ruby to help me make music :)

Collapse
 
merongivian profile image
Jose AΓ±asco

Hey, i made this dsl/language inspired in sonic pi and tidal cycles: negasonic.org. It works on the browser, so you don't have to install supercollider or anything to start using it

Collapse
 
daveyman123 profile image
David Graff

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

Collapse
 
guneyozsan profile image
Guney Ozsan

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

Collapse
 
hutepa profile image
Mahmoud Abdelsalam

oh my!! I love this.

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
Sebastian L. K. SΓΈrensen

That is so cool!! Gj :)

Collapse
 
nguyenquangtin profile image
Tony Tin Nguyen

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

Collapse
 
socratesdz profile image
SΓ³crates DΓ­az

This is really cool! Thanks for sharing! :D

Collapse
 
andrerpena profile image
AndrΓ© Pena

Oh my God you hooked me into sonic-pi!

Collapse
 
andrerpena profile image
AndrΓ© Pena

The Sound Cloud link is broken: soundcloud.com/wombatgal/sonic-pi-...

Collapse
 
sublimemarch profile image
Fen Slattery

Hm, could you take a look again? It's working just fine for other folks.

soundcloud.com/wombatgal/sonic-pi-...

Collapse
 
gauravchaddha1996 profile image
Gaurav Chaddha

This is so cool. Thanks for writing about this.

Collapse
 
ben profile image
Ben Halpern

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

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