Talks

Lightning Talk: Making music with Ruby and Sonic Pi

Lightning Talk: Making music with Ruby and Sonic Pi

by Julian Cheal

In this lightning talk at Friendly.rb 2023, Julian Cheal introduces the audience to making music using Ruby and Sonic Pi, a platform designed for coding music. The session highlights several key points:

  • Introduction to Sonic Pi: Sonic Pi was developed by Sam Aaron to encourage children to learn programming through music creation. Initially designed for the Raspberry Pi, it now runs on various platforms and allows users to write music using Ruby.
  • Live Coding in Sonic Pi: Julian demonstrates how to use Sonic Pi's text editor to write Ruby code, showcasing how to create music through simple commands. He presents a live coding example by playing scales and modifying variables to create a more robust sound.
  • Integration with Visuals and Hardware: Julian emphasizes the importance of engaging visuals while live coding and mentions the app Hydra as a tool for visual enhancement. Furthermore, Sonic Pi's MIDI implementation allows for interaction with physical hardware, broadening its capabilities.
  • Personal Experience and Demos: While sharing his experience, Julian references his attempts to compose Romanian music and illustrates live music coding with Ableton Live, demonstrating how Sonic Pi can connect with synthesizers and drum machines.
  • Community and Collaboration: The talk also reflects on community aspects, sharing personal anecdotes like his interaction with Sam Aaron and the developments in the Sonic Pi community.

Overall, Julian Cheal's talk showcases the innovative blend of programming and music through Sonic Pi, inspiring attendees to explore creative coding beyond traditional applications. He concludes with a live demonstration of music production, emphasizing both the fun and educational aspects of coding music.

The key takeaways from this session revolve around the unique possibilities Sonic Pi offers, bridging programming knowledge with the art of music.

00:00:05.640 For those of you who don't know me, my name is Julian. I'm a British Ruby developer, and as you can see, I have a penchant for tweed. I never changed from the skeuomorphic design, so my website has buttons that you can click. Speaking of social media, if you're into trendy viral tweets, you can check out my SoundCloud at cl.com/julan. Some of my tracks are private, like one called 'Sup Yaro'. It's a good way to test if the audio is working.
00:00:28.000 So, this is a collection of samples that someone sent me, and I turned them into a song. I don’t know if he’s here, but he might appreciate it. There he is! Thank you. I'm a Ruby developer, and I try to make music. I’ve attempted to produce some Romanian music—it’s not very good. I'm just using some Romanian samples trying to make something cool, but it's not great, so please don’t listen to it. Oh, what's this? A friendly theme song? You may have heard of the friendly podcast; there may be a theme song coming for that soon. Anyway, this is my music, and you should check it out, but I'm not here to talk about myself, even though it's one of my favorite topics. I'm here to talk about Sonic Pi.
00:01:16.359 Sonic Pi was created by a chap named Sam Aaron. It was originally developed with the purpose of getting kids into programming. It's called 'Pi' because it used to run on the Raspberry Pi—pretty much it still does, but you can run it anywhere. With Sonic Pi, you create music and write it using Ruby, which is really cool. Unfortunately, Sonic Pi is written in some language called ‘Elixir’ or something—not as cool as Ruby, but while using the application, you write everything in Ruby. Let's take a look at Sonic Pi.
00:02:53.960 You can see that you write Ruby code, and it has a little text editor. You just run the code, and it does stuff. You can live code and do all sorts of cool things. Here, we named a block 'AR 2'. We're using the 'DW' function; a 'saw wave' is a type of wave, and I forget what the 'D' stands for—perhaps 'D-tuned' or 'duper cool'. Inside Sonic Pi, it uses an app called SuperCollider, which is really powerful software for synthesis. If you think like Erlang or Lisp-like stuff, SuperCollider is even crazier, but it’s nice to have this Domain-Specific Language (DSL).
00:03:55.760 In this code, we’re going to play the E2 scale in a minor pentatonic with three octaves. Every time it runs, it will randomly choose another note to play. You can also change some variables; let’s make the amplitude smaller so we can see the entire thing.
00:05:03.080 If we run this, we should get music... or not? Let's check the output. Sound? Let's try turning it off and on again. Oh, computers! Now you can see that it's playing the scale. Give me a musical note between A and G. Let's say G. If I type that in and rerun it, we can go up an octave. There we go! We’ve created music! We are music maestros now.
00:05:40.720 You can do really cool and simple things like this. Sam Aaron has a band called Metx, and if you go to his YouTube channel, he does hour-long live coding streams, which is fantastic. You can use a method called 'sleep' in Sonic Pi; it's really powerful because you can program events to occur after certain periods. This allows you to make evolving musical tracks. As I adjust the background, you can see I have an app called Hydra here. I'm still learning it, but it's essentially a JavaScript UI tool that you can connect to Sonic Pi. The idea is to make it look more exciting than just someone typing code for a whole hour.
00:07:03.360 The incorporation of visuals makes the experience more engaging than watching someone write code. There’s so much you can do with Sonic Pi, but I won't fit everything in. You can control the beat or switch notes, which is really cool. One of the exciting features of Sonic Pi is its ability to interact with physical hardware using a protocol called MIDI (Musical Instrument Digital Interface). Sonic Pi didn’t originally support MIDI; I wrote a version before Sam did, which was hacky using DRb and other things.
00:07:51.160 Sam took notice of that and said it was cool but decided to implement it in Elixir for better performance. If I had all my hardware here, I could control them easily, but since I couldn’t pack everything, I’m using Ableton Live instead. I have multiple synthesizers and drum machines here, and I’m going to connect Sonic Pi to Ableton and live code some music. Rather than the way Sam works, where he writes code live, I’ll be uncommenting and commenting on the code, like most of us do when developing.
00:09:19.200 Let's see what happens as I adjust this. Alright, let's get going, shall we?
00:10:58.040 Here we go! [Music plays] Yeah, something like that! Thank you very much.