Ruby
Learn to Make Music. With Ruby.
Summarized using AI

Learn to Make Music. With Ruby.

by Darin Wilson

In this engaging talk by Darin Wilson at RubyConf 2015, attendees are introduced to the world of music-making using Ruby, specifically through a platform called Sonic Pi. Sonic Pi is a free and open-source application that enables users to create music by coding, making it accessible to both seasoned programmers and novices alike.

Key Points:

  • Introduction to Sonic Pi: Sonic Pi was developed by Sam Aaron, who aimed to provide a tool for creativity and self-expression through coding. Designed to work on multiple platforms, including Raspberry Pi, Sonic Pi is user-friendly, catering especially to beginners.
  • Elements of Music: Wilson emphasizes breaking down music into four fundamental elements: melody, harmony, rhythm, and timbre. Understanding these components allows one to approach music composition more manageably.
  • Creating Melodies: The talk includes practical demonstrations of how to create melodies using the play command in Sonic Pi, teaching attendees how to specify notes and the importance of using the sleep command to create pauses and melodic structure.
  • Exploring Scales: Participants learn to work with scales, which help in creating coherent music. Sonic Pi simplifies this process, allowing users to explore different scales for more emotionally resonant compositions.
  • Incorporating Harmony: Wilson discusses how to use chords and arpeggios, enhancing melodies by layering notes and creating a richer musical texture.
  • Understanding Rhythm: The discussion includes creating drum patterns and using sampling to add depth to compositions. Wilson highlights that Sonic Pi's looping capabilities simplify the process of rhythm-making.
  • Experimentation with Sound: Sonic Pi promotes experimentation, enabling real-time adjustments and live loops. Users can manipulate various synthesizers and effects to create unique soundscapes.
  • Community Engagement: Wilson encourages attendees to engage with the Sonic Pi community for support and to share their experiences. He offers insights into potential questions participants might have regarding the use of Sonic Pi.

Conclusions and Takeaways:

  • Sonic Pi stands out as a tool that democratizes music creation, allowing anyone, regardless of their musical background, to explore and create music through coding.
  • The session concluded with a call to action, urging participants to download Sonic Pi, explore its tutorials, and experiment with music-making. Wilson emphasizes the immense potential for creativity and learning through this platform.

This talk not only demystifies music composition for programmers but also illustrates the joy and accessibility of creating music through code.

00:00:15.180 Okay, good morning! Thank you all for coming. We know you have a choice in conference talks, and we appreciate you flying Sonic Pi Airlines. Today, I'd like to talk about making music with Ruby, specifically using a software system called Sonic Pi.
00:00:27.130 Sonic Pi was created by a programmer named Sam Aaron, who is based in the UK. One of his primary motivations for developing Sonic Pi was to provide people with a creative experience using code. That may sound funny because many people think of code as just a tool for creating serious business applications, government databases, or sharing pictures of their food on the Internet. While these uses are important, Sam believed that code could be much more than that—it could be a vehicle for self-expression and creativity. He wanted everyone to have the chance to experience that freedom, especially those who had never coded before.
00:00:46.000 To make Sonic Pi as accessible as possible, it is completely free and open-sourced. It runs on Mac, Windows, and Linux. Sonic Pi was specifically designed to operate on the Raspberry Pi computer, which we saw a little bit of in the last talk if you were here. This computer is about the size of a credit card and costs around $35. You simply plug in a USB keyboard and mouse, connect it to a monitor or TV, and you are ready to go.
00:01:02.000 Sonic Pi is written in Ruby, and the language it uses for creating music is both elegant and approachable. After seeing Betsy's talk yesterday, I'm not even sure it's a DSL; for the most part, it's straight-up Ruby with a really nice API. That makes it easy for just about anyone to get started with it. In fact, Sam collaborated with a school teacher in the UK to develop a curriculum that brings Sonic Pi into primary schools, teaching ten-year-olds the basics of coding.
00:02:20.649 The real miracle of Sonic Pi, for me, is that it is simple enough for ten-year-old kids to play around with and create interesting sounds, yet sophisticated enough that Sam and others have taken it to live performances in nightclubs, coding while people dance. Imagine that for a moment!
00:02:36.630 Here's an excerpt from one of Sam's performances. If the gods of wireless are smiling on us at this moment... as you can see, there’s a camera on Sam in the upper right-hand corner, and he usually has the code projected on the screen behind him so the audience can see what’s going on. It gives you a taste of the experience.
00:03:02.459 By the way, that's all running on the Raspberry Pi. You don't necessarily need a super powerful computer to use Sonic Pi! This system is remarkable not only for getting people started with coding, but also for introducing them to making music. As a musician for most of my life, I earned my living as one for a few years until I transitioned into programming. My poor parents are still getting over that! I often meet people who say that they would love to learn how to make music, but they hesitate because it seems too daunting or too expensive, or they think it would take too long to create something interesting. Sonic Pi provides a really nice friendly on-ramp to this.
00:03:43.420 So, when I meet people like this, I can now say, 'Try out Sonic Pi.' In this talk, I'm going to provide you with a quick music lesson using Sonic Pi and hopefully convince you to give it a try yourself. Now, we’ve seen the slide a lot, and I’m sorry to say there's going to be a little bit of that in the talk. There will be moments where things seem clear, and then suddenly it’s going to look like I take some weird logical leap. I appreciate your patience with that. If you don’t grasp every single aspect, don’t worry about it. I obviously can’t turn you all into musical masters in the next thirty minutes or whatever remains, but I hope to give you a few things to start with and a little nudge across the starting line.
00:04:34.300 Let's talk about music. Music can be a little daunting if you've never worked with it before; it seems vast, rich, and very abstract. So how do you even get started? How do you approach it? For us programmers, we learn that if you have a big, complex, seemingly insurmountable problem, the way to tackle it is to break it down into smaller, more manageable problems. The good news is that you can do that with music too.
00:04:59.380 One way to think of music is that it consists of four different elements: melody, harmony, rhythm, and timbre. You probably have at least an instinctive sense of melody, harmony, and rhythm. Timbre may not be as familiar, but it refers to the sound quality of the instruments producing the music. Think about the difference between a flute, a guitar, and a kazoo; the differences between these instruments are what timbre is all about. I'll go through each of these elements and show you how they manifest in Sonic Pi, hopefully giving you enough information to explore further.
00:06:16.810 Let's switch over to Sonic Pi. When you first install and run Sonic Pi, this is what you see. You download it as a binary and you are ready to go; no need to worry about installing Ruby or getting your bundler file just right. There’s no need to install gems, and you don’t have to fuss over whether your version of Mac has issues this week. This main window is where you enter the text, then you click the Run button to start the music. The Stop button stops it, and that’s really all you need to know to get started. For beginners, it provides a very nice experience.
00:06:57.160 Now let’s talk about melody. What is a melody? A melody is simply a sequence of single notes played in succession. It’s usually the most forward-facing part of a piece of music. If you’re driving down the road, listening to your iPod, and singing along to your favorite song, chances are you're singing the melody. So how do we play a melody in Sonic Pi? To play a single note, we use the play command. That immediately makes me happy!
00:07:22.040 What do we pass to the play command? We specify the note that we want to play, which can be done simply with a number. Higher numbers represent higher pitches, while lower numbers represent lower pitches. Each increase of one in integer corresponds to walking up a piano by one note or moving up a fret on a guitar. Interestingly, Sonic Pi allows you to work with notes that aren’t on the piano as well, which opens up new possibilities.
00:07:52.660 If you have some musical background and know the names of notes, you can work with those too. For some, that might be easier—it's just another way of representing the same thing. You can also set parameters for the play command, like release to sustain the note longer or modify the attack to make it swell before reaching full volume. Let's keep it simple at first and work with single notes to create a melody. However, we want to ensure that we add pauses between the notes so it sounds melodic instead of random. In Sonic Pi, we do that using the sleep command, which defines how long to wait between notes.
00:09:04.020 Now, Sonic Pi’s timing system is a bit more complex. The sleep value in Sonic Pi represents beats rather than seconds. So if the system is playing at 60 BPM (beats per minute), that matters when deciding the space between notes. If we want to speed things up, we can adjust our sleep intervals. With the two commands we've just learned, you can express any piece of music that can be notated. You can build your melodies using appropriate numbers and sequencing.
00:10:17.360 But how do you know what numbers to use? For beginners, it can be a challenge. However, Sonic Pi can help by introducing some randomness into the notes. By shaping the parameters of that randomness, you can experiment with the sounds until you find something pleasing. Let's create a loop so we can generate several notes simultaneously, and try to make it sound nice. We can use a random function to choose a note within a specified range. This is an approach that allows for capturing some musical value without too much complexity.
00:10:46.600 Here’s the fun part: scales. As we play with notes, remember that music often works with a subset of available notes. This is known as a scale. A scale helps create coherence in music. They're like color palettes in design; you choose a limited number of notes to create consistent sound quality. Sonic Pi simplifies scale creation with a specific function that returns a list of notes. You can try playing with a major scale, minor scale, and other types of scales to notice their emotional quality.
00:11:44.720 To generate melodies from these scales, we can use the scale function, pick a root note, and then let Sonic Pi help you explore the different scales. By doing so, you can create melodies based on different emotional impressions they impart. It may take some time, but through experimentation and play, you’ll figure out what resonates with you. Once you understand scales, you will be able to create interesting composition patterns.
00:12:45.400 Now that we have a grasp on melody and harmony, let’s explore how to combine them. Harmony refers to when two or more notes sound together. A chord is when you have multiple notes sounding in unison. Sonic Pi has a specific function for creating chords, similar to how we create scales. Harmony provides a broader texture to music and enhances your melodies if applied correctly.
00:13:57.360 To combine chords and melodies, you can articulate the notes of the chord one at a time, creating what is known as an arpeggio—a common practice in music. It's an easy way to implement harmonies and reinforce melodies seamlessly. You can experiment with this by taking your created chords and repeating their notes into a melody. Let’s try it out and see how those two elements work harmoniously.
00:15:00.930 Now, let’s shift gears and talk about rhythm. Rhythm deals with the duration of notes. The sleep commands we’ve used thus far have introduced timing, but when creating music, we can also create specific drum beats. Up until now, we've primarily worked with synthesized sounds. However, we can also explore sampling, which involves using predefined audio recordings. This plays a significant role in genres like hip-hop. Let’s look at how we can incorporate sampled sounds into our music-making experience.
00:16:08.960 When working with samples, a simple snare drum sound can provide a realistic backing. Sampling allows you to manipulate playback rates. For instance, playing a sample at double speed significantly alters its pitch. So lets try laying a simple 4 on the floor bass drum pattern. We might consider including a snare every other beat for a classic driving rhythm. Using a 'tick' utility may help synchronize rhythms effectively.
00:17:42.790 Creating rhythms can become intricate. To facilitate this, Sonic Pi offers looping samples among the built-in options. Looping samples can effectively simplify rhythm creation since they allow you to repeat pre-built musical elements. Incorporating these samples allows for more complexity without the need for generating all musical components from scratch. You can combine elements, find coherent patterns, and develop interesting rhythmic structures within your Sonic Pi setups.
00:18:25.990 Now let’s get back to blending melody, harmony, and rhythm together. We can attempt to layer these three elements using Sonic Pi's multiple thread capabilities. With careful coordination, we can achieve synchrony between the musical fragments we’re creating. Sonic Pi is programmed to handle these interactions beautifully through a system of threading that allows everything to run simultaneously without any hiccups.
00:19:40.680 As we integrate all elements of music, we can also examine timbre. Timbre relates to the quality of sound produced by different instruments. Sonic Pi provides an array of synthesizers from which you can select and customize the sound that you wish to generate. By utilizing different synthesizers, you can explore an extensive range of textures and colors within your music. You can also play with effects like reverb and echo, which add layers of depth to the sounds created.
00:21:29.310 The beauty of using Sonic Pi lies in its ability to facilitate experimentation effortlessly. It encourages you to make adjustments in real-time, using live loops. Every iteration and change can be immediately apprehended, allowing for fluid creativity. This aspect enhances the overall performance experience because you can evolve the music dynamically without interruption, minimizing the tension associated with traditional performance settings.
00:22:54.240 Now, to summarize, I encourage you to download and install Sonic Pi, which is completely free. Take advantage of the excellent help system and the detailed tutorial that accompanies it. Consider exploring the documentation and examples available, which range in difficulty from simple to complex levels. Finally, key to diving into music-making is just experimenting. Keep it easily accessible on your computer and, during any idle time, feel free to explore sounds and create.
00:24:04.880 If you have questions while learning, don’t hesitate to engage with the community. I’m on Twitter as @DarinWilson, and I’d love to hear about your experiences with Sonic Pi. There’s a wealth of support material that can enhance your learning journey as you build music with code. If you get stuck or need a pair of eyes, just reach out. I really believe in this system, and I encourage you all to give it a shot and surprise yourself with what you can create!
00:25:58.300 I'll now take a few questions, if there are any. One question I often receive is whether you can create your own synthesizers within Sonic Pi. While you could hack the source code and submit a pull request, it is built on a synth engine called SuperCollider, which has its documentation. As a beginner, this may be complex, but with effort, it’s achievable!
00:27:05.420 You might ask whether there's a system to scan sheet music and convert it. In theory, certain technologies can turn sheet music into MIDI, which Sonic Pi does not currently support natively. It is a theoretical possibility rather than a practical function available right now.
00:27:18.260 You can use samples from your local disk in Sonic Pi, which allows for more diverse sounds beyond the built-in ones. However, remember that using local samples makes sharing your work less portable, since that would require sending the files along with your Sonic Pi project. Utilizing the local sounds can, in fact, enrich your soundscapes, especially when blending them strategically in your compositions...
00:28:11.590 ...I do see a time when more integrations with VSTs and other synths might be developed, but that’s a distance away. Right now, the focus remains on building your music through the existing samples and built-in synths. While exporting MIDI is unavailable, there is indeed a record function in Sonic Pi, allowing you to capture your performances as WAV files. You can later convert these to MP3 formats.
00:28:43.510 Regarding sequencing capabilities, Sonic Pi does not feature any built-in sequencing functions. However, through the Ruby coding functionality, you can define your own sequences and functions to create your musical patterns. That way, you can implement an array of structures, such as loops, that allow for controlled playback of different music sections.
00:29:51.720 As time goes on, I hope that this tool will continue to expand in capabilities, inviting more users to embrace the code-as-music development. There’s an immense potential for learning and growth with Sonic Pi. Thank you all for your attentive engagement today; I hope you leave ready to explore the world of music-making with code! I will be available after this talk for further questions you might have. Thank you!
Explore all talks recorded at RubyConf 2015
+75