Nicolas Dermine

Live Code Music

wroc_love.rb 2015

00:00:10.240 And now, please welcome Nico. Thank you.
00:00:19.439 The music you're hearing now, or the noise, depending on your sensibility,
00:00:25.039 is generated entirely by code that you see here. I hope you can see it sort of okay.
00:00:31.760 So you write code in Ruby and you get music out when you execute it.
00:00:37.840 This software is called Sonic Pi. I'll let your ears rest a little bit.
00:00:46.000 There are many things that can go wrong in this talk because you'll have to read,
00:00:51.360 well, if you want to code on the screen and listen to music over the speakers.
00:00:58.879 We'll see how it goes. Today is a special day, and does anybody know what today's date is?
00:01:05.680 Today's date is special: it's Pi Day! Yeah, I didn't know about that,
00:01:11.760 but I read a tweet by Corey Haynes two days ago, and he mentioned today was going to be Pi Day.
00:01:17.520 Is the software called Sonic Pi a coincidence? I don't know. Pi is a number, as you all know, and it has many digits.
00:01:30.799 If you separate the digits, you get today's date. Let's keep this in mind.
00:01:38.000 And we have the time as well, so I don't know who made the schedule, but I'm sure it was a very intelligent person.
00:01:44.479 Subsonic Pi, that's maybe the logo of Sonic Pi. As I said, it's a piece of software that allows you to create music by writing code in Ruby.
00:01:52.159 OK, I'd like to start with some questions too, because I made some assumptions about you and what you know about Sonic Pi.
00:02:09.360 But I'd like to finally see if they were accurate. First of all, please raise your hand if it has ever happened to you that you enjoyed writing code.
00:02:14.640 Ah, not everybody? Really? Okay, sometimes? At least I hope it's often.
00:02:21.280 Now please raise your hand if you sometimes enjoy creating music—perhaps you might play an instrument or sing, maybe in the shower.
00:02:27.680 Okay, less people. Best of some. Now please raise your hand if you've already heard of Sonic Pi before.
00:02:33.519 Ah, very few people, but it's in the program. Did you read the program at all? No? Okay, you're not here for the talks, maybe.
00:02:41.680 So out of the few that raised their hand, did you try Sonic Pi? One person? Two? Wow! And out of those two, did you try it enough to be comfortable with it? Or just a little bit?
00:02:57.760 Just a little bit? Okay, so that fits more or less my assumptions. In this talk, I'd like to briefly tell you the story of how Sonic Pi was created and why it was created.
00:03:12.720 Then I'll try to show you reasons why you might want to use it, even if you didn't raise your hand when I asked if you liked creating music.
00:03:19.320 But you enjoy writing code, so there’s still hope there.
00:03:24.720 Finally, I'd like to show you in the app how to get started—the basics of how to put everything together to create music.
00:03:30.000 Can you hear me okay in the back? Can you understand what I'm saying? Yeah? Okay.
00:03:38.320 Don’t hesitate to stop me if you don’t understand something. Here’s the story. Back in November, only four months ago, I saw a tweet by James.
00:03:48.319 He asked very politely if I could watch a talk, along with every other programmer. It was a talk at the Strange Loop conference last summer.
00:03:58.800 Called 'Our Shared Programming,' it was given by Karen Meyer and Sam Aaron.
00:04:02.960 They discussed various topics, and then they had a demo with live music and robots dancing to the music.
00:04:09.800 Did anybody watch this talk or video? For the others, I encourage you to watch it.
00:04:14.800 I was really moved by the talk at moments. It was a really interesting talk.
00:04:20.000 That’s where I learned about Sam, the creator of Sonic Pi. He lives in the UK.
00:04:27.360 He describes himself as a live coder. When I watched this talk for me, live coding was this thing that you’re not supposed to do at a conference.
00:04:39.040 It’s risky because it’s hard to pull it off. Like Ryan said this morning, many things can go wrong.
00:04:46.000 But Sam has a different perspective. For him, the live coder codes music. He’ll be at a computer, writing some code, tweaking the code.
00:04:59.840 Every now and then, the code is reloaded, and music comes out of the speakers. There’s an audience.
00:05:06.079 The code is projected on screen, often with visual effects in the background. For people like me, it looks pretty cool.
00:05:13.680 If you look at the code, you might notice that it’s not Ruby. At the time, he used a Clojure library called Overtone.
00:05:20.960 He was part of the core team of Overtone. Well, he was part of the core team.
00:05:28.480 One day, a teacher came to Sam and said, 'I really love what you're doing with this live music coding thing. It looks really cool.'
00:05:38.800 He mentioned he had a group of students who were going to learn about programming on the Raspberry Pi.
00:05:45.120 I’m sure everybody knows what a Raspberry Pi is—it’s a small computer.
00:05:52.800 The teacher asked Sam, 'When I see what you're doing, it’s such a cool environment. You seem to have so much fun with that.
00:06:00.640 I wonder if my students could learn programming in such a fun way that they would get excited about learning.
00:06:10.640 They might think programming isn't boring, and they might learn better.'
00:06:16.640 So she asked Sam what he thought about that, and he said he thought it was a really good idea.
00:06:25.080 He said he would see what he could do and he tried to load Overtone on the Raspberry Pi.
00:06:31.280 However, it was pretty slow to boot at the time, and on top of that, he knew that when you're talking to an audience,
00:06:37.040 it’s very hard to give more than a little bite of information at a time, especially to a class of young students.
00:06:44.240 So he thought teaching them programming in Clojure through Emacs would be a little bit difficult.
00:06:51.000 That’s why he had the idea to create something new, and that’s how Sonic Pi was born.
00:06:56.480 He worked for three weeks on the project to get a first version out.
00:07:03.040 The students tried it out—they had fun. It was a very good experience for them.
00:07:09.760 That’s how Sonic Pi was born. Seeing how excited the students were learning with the tool,
00:07:15.680 Sam thought it would be a great tool for teaching programming and music simultaneously.
00:07:21.280 For him, as a live coder, he felt something was missing because he couldn’t use it for his own live performances. It was still too much of a toy.
00:07:30.880 That’s why he wrote a second version, adding many features he needed for his own live coding music performances.
00:07:37.680 From the second version of Sonic Pi, you have capabilities of live coding, and that's what we’ll look at in a moment.
00:07:44.080 That's it for the story. The project is free and open-source; you can find it on GitHub.
00:07:50.560 There’s a version for the Raspberry Pi, but also for Mac, Windows, and Linux.
00:07:56.560 If you want to try it out, the platform shouldn't be a problem. There's a little core team assembling around Sam to help with different platforms and new features.
00:08:03.680 Now I'd like to list four reasons why you might want to use Sonic Pi. They're quite different.
00:08:08.960 One reason is not really a reason, but I've been playing with Sonic Pi live for four months—most days.
00:08:14.720 And it's a ton of fun. Even though I'm not a good live coder and don't know much about electronic music,
00:08:20.720 I find it really enjoyable to play some notes and see how they interact.
00:08:29.440 I enjoy playing a loop, adding effects, and creating random numbers to make things more interesting.
00:08:37.840 I’ll try to show you that afterwards.
00:08:43.120 I have children, and I shared Sonic Pi with my daughters a little bit. It's great seeing their excitement.
00:08:50.560 They grasp concepts like, 'Oh, you could put this in a loop and run it a thousand times!' and they're really excited about it.
00:09:00.160 For us, it’s normal programming, but for them, you see this excitement.
00:09:06.240 That’s where I see many possibilities in this tool. If you enjoy teaching, it's a really fun environment.
00:09:12.720 An obvious reason to use Sonic Pi would be to create music. If you have a game, you might want to create a background music track.
00:09:19.200 If you have a podcast, you could create a jingle or an introduction. You could compose music and record it.
00:09:26.080 You could play live. There are two ways of playing: you can program the whole music and play it in one go,
00:09:34.000 or you can live code, edit it, and have it reload while taking the changes into account.
00:09:40.000 So if you're DJing at a party, you could live code for fun.
00:09:50.640 You could record that and share it, and you could even play with other people using instruments.
00:09:56.640 Another reason to learn with Sonic Pi is that if you don’t know anything about programming, you could learn about programming just by following the tutorial.
00:10:03.280 You could learn about music. If you know programming but not Ruby, you could learn about Ruby because it uses Ruby.
00:10:09.680 You can learn about music synthesizers and how they're made if you want to dig deeper.
00:10:16.160 If you're interested in teaching, you could teach with Sonic Pi as well.
00:10:23.200 Here's a question for you: have you ever been asked by someone if you could teach them programming?
00:10:29.760 Not that many people? Well, maybe you're asleep already.
00:10:34.480 I find it really hard. Like we talked about with Rails Girls, after two days they can build a web app with Rails, and that's impressive.
00:10:41.920 But I find that when you have someone who is new, you want to give them something interesting.
00:10:47.680 If you give them something too hard, they might feel overwhelmed by the complexity.
00:10:52.720 And they might think that programming is not for them.
00:10:57.680 So now, we’ll look at the app and the code. You have one graphical user interface with a single code editor window.
00:11:06.880 The white part is just the code editor, there are buttons to run and stop the execution.
00:11:12.080 There's a help system with a tutorial, code examples, and documentation for synthesizers, effects, samples, and all the functions available in Sonic Pi.
00:11:18.080 The environment is pretty contained. You write code only in Ruby in one place.
00:11:26.080 There's no need for 20 files; it's all in one code editor window.
00:11:34.560 This creates a very tight feedback loop; you write a line of code, run it, and you can hear if it sounds like what you expected or not.
00:11:41.360 So it's a simpler environment—it can be easier and safer to learn programming that way.
00:11:48.400 If you want to teach programming, if you have kids, or if someday you have kids, you might want to sit down with them.
00:11:55.760 You could follow the tutorial or, if you know a little bit about Sonic Pi, teach them yourself.
00:12:02.080 By following the tutorial, you'll be exposed to all the programming concepts that we use as programmers.
00:12:09.920 You’ll learn about functions, variables, parameters, conditionals, iterations,
00:12:14.880 random numbers, and even concurrency and threads, which is very interesting in music to have things playing in parallel.
00:12:20.560 Let’s look at what the code looks like. When Sam started this up, he wanted it to be as simple as possible for beginners.
00:12:27.920 If you want to play a note, you can type 'play 60' and run it.
00:12:32.640 This will play a note (60) as a MIDI note.
00:12:40.640 If you're a musician, that might not resonate, but it's about a note in the middle of the piano—C if you know the ABC notation.
00:12:46.640 Higher integers yield higher notes while lower numbers yield lower notes on the piano.
00:12:51.040 It’s simple to play a note; it might not be the most beautiful sound in the world, but it’s a synthesizer.
00:12:56.560 I’ll show you how to change synthesizers in a moment. Now I'd like to play a little game with you.
00:13:03.840 I don't have a TV, but my in-laws do, and I live in Poland.
00:13:09.680 They often watch a game called 'Jak to Melodia,' which I guess every country has its type of game.
00:13:16.240 Participants must guess the title of a song by hearing a couple of notes.
00:13:24.080 So I’d like to play that with you. It’s very hard because I won’t tell you the artist.
00:13:31.840 In the game, they know which artist plays the song, but I won’t tell you.
00:13:39.000 So it may be too hard to guess, but I’ll play three notes.
00:13:45.200 We learned about the play function, so let’s see what happens.
00:13:51.360 If I play a note like this, coming from a PHP background, I'm assuming, of course,
00:13:57.680 that the second note should play after the first. But no—the calls to play a trigger sound, and if you don’t tell Sonic Pi to sleep,
00:14:04.960 it plays everything simultaneously.
00:14:12.560 This gives you a chord, which is very useful in music. If you want to play a melody,
00:14:18.240 you have to tell Sonic Pi how long to wait between every note.
00:14:25.760 For example, we can set a sleep for one beat.
00:14:31.840 By default, you have 60 beats per minute in Sonic Pi, which you can change.
00:14:37.680 Now, I’d like to ask an American participant to play this song.
00:14:43.280 Thanks, Jim! You’ve already heard it four times, but I’ll play it once more.
00:14:50.480 I didn’t choose an American by chance. Maybe you've already guessed the song; maybe not.
00:14:56.720 It's alright if you don't; it's very hard to guess a song with just three notes.
00:15:02.920 Now, was that close? American Anthem? Yes, the Star-Spangled Banner!
00:15:09.360 It’s normal; it’s really not easy. But when we reflect a bit,
00:15:15.760 we see that we can create chords and melodies with just those two functions.
00:15:21.440 For me, I wouldn’t want to do it that way; it would be cumbersome to calculate the spacing between the notes.
00:15:26.640 But with Sonic Pi, we also have the functions documented in the help system.
00:15:33.440 I won’t mention too many functions today, just five or six, which is enough to get started and have fun.
00:15:39.120 When you try this out, you can see what those other functions do.
00:15:46.040 Sonic Pi provides an API. My point is that you have Ruby available for programming how those notes play.
00:15:53.760 When I learned about that, I was intrigued—it's programming!
00:15:58.960 It’s not that different from using a library, but it's applied to music.
00:16:04.720 It’s easy to share what you find—your code. You can just copy and paste it on GitHub and share it on Twitter.
00:16:12.920 You could make open-source music. How cool is that!
00:16:19.120 To illustrate how to use Ruby, we could write loops to avoid excessive copying and pasting.
00:16:27.440 Here, I’d play the same note three times. Sonic Pi provides a function called 'scale'.
00:16:33.760 You probably know what a scale is; it’s just a set of notes.
00:16:39.440 You can generate various crazy scales from just the fundamental note.
00:16:46.560 You can experiment with the notes and create interesting compositions.
00:16:52.720 Now, back to our game, I have a song in mind, but I'm sorry,
00:16:58.320 only Polish people may know it better—maybe parents will do better.
00:17:03.680 So if there's anyone who wants to join me, go ahead, but if not, I’ll just play it.
00:17:10.240 If anyone knows the song, we’ll see.
00:17:17.760 Good—so you see, you're a good audience. It’s a lullaby involving two small kittens.
00:17:22.160 Notice I said we would play three random notes, but the seed will always be the same.
00:17:28.240 You'll be guaranteed to have the same sequence. If you want to change the sequence, you can set a different seed.
00:17:36.080 We can play this in a loop as well.
00:17:44.880 My children love it when it plays indefinitely!
00:17:51.120 Now I want to introduce live coding; a basic loop is nice but if I update and run it repeatedly,
00:17:58.560 the old code continues executing while I make the new changes.
00:18:04.320 With Sonic Pi, you can use the 'in_thread' function for composing music, especially if you're not live coding.
00:18:11.440 You can place your code in a 'thread', allowing simultaneous loops.
00:18:18.240 Now, we can add extra threads for more complex sound.
00:18:27.320 This is key—if you want to have things in parallel, you'll need to use this 'in_thread' function.
00:18:34.560 You pass it a block, and you’ll have independent functions running alongside.
00:18:43.520 The 'live_loop' function really helps out with seamless coding.
00:18:50.560 If you give it a name, it will allow you to see error messages.
00:18:57.120 Now, when we make a change and reload, the current code continues until the end of that iteration.
00:19:03.680 It starts over with the new code after that.
00:19:09.840 You can tweak things around and play with them endlessly.
00:19:16.560 You’ll be able to have multiple live loops running.
00:19:22.400 Now, let’s not just play a melody; we can explore various additional functions.
00:19:30.000 For example, let’s play a basic note and sleep for a beat or more.
00:19:36.240 If I turn up the volume, tell me if it's too loud.
00:19:44.000 So far, we got lucky—let's adjust the beat.
00:19:50.560 Now we need to see how the sync function operates.
00:19:58.560 The sync function works like event emitters; it allows you to interrupt and react.
00:20:06.560 Now we have a way to ensure that loops remain nicely synchronized.
00:20:12.960 That’s it for now. We’ve seen a few essential functions, enough to have fun.
00:20:21.440 There are many synth features and sound effects that Sonic Pi offers.
00:20:27.040 You could create different types of synthesizers and sounds.
00:20:32.640 If you type Control-I on any function, you'll find doc links for the synthesizer.
00:20:41.280 The help system details the synthesizers and their parameters for adjusting sounds.
00:20:49.360 You can explore the different dimensions to change your music.
00:20:54.640 So using Sonic Pi gives you many ways to create sounds with synthesizers.
00:21:00.960 Another exciting way is to use samples. You can sound samples using the sample function.
00:21:07.760 Sonic Pi comes with public domain samples, and you can add your recordings.
00:21:13.600 By calling the sample function, you can mix and play different audio.
00:21:19.920 Let’s try playing a bass sound from the samples.
00:21:27.120 And there’s a snare sound, let me see if you can hear it.
00:21:32.240 Most of these may play at the same time, though it's not always nice.
00:21:38.240 To add variations, you can use randomness.
00:21:44.440 After playing around with the sounds, you could combine them to create a drum set.
00:21:51.920 We have random elements to keep everything fresh and engaging.
00:21:57.360 Just by changing the speed or manipulating the samples, you create different beats.
00:22:03.040 You can use playback rates to double the frequency, allowing unique sound variations.
00:22:09.760 Again, adjusting the randomness can lead to exciting and unpredictable results.
00:22:16.560 That’s how you can have various dimensions of sound and create unique music.
00:22:23.840 I mentioned earlier a lot of features Sonic Pi has and how it can facilitate your code.
00:22:30.480 We've seen ways to combine the sounds and how to change the output for different effects.
00:22:36.880 I hope you find it a fun project to play with, and I encourage you to explore the tutorials.
00:22:44.560 You could share your findings, any interesting experiments or music you create.
00:22:52.480 I think that’s it. Thank you for your attention.
00:22:59.760 Thank you, Nico!
00:23:02.920 Does anybody have any questions?
00:23:06.112 I was thinking about this while preparing the talk. During my preparations,
00:23:13.112 I would run every morning, gradually increasing my running time—running more each time.
00:23:18.920 I noticed my talk was becoming longer and longer. I was a bit worried when it took an hour and a half.
00:23:26.920 This brings me to testing and the live loop functions.
00:23:34.920 The last part is quick feedback loops, checking if your code works.
00:23:41.920 But it’s about the response of your code through iterations.
00:23:46.920 Just remember, our ears are great—we can quickly tell when something isn’t right.
00:23:54.560 Does that make sense to everyone?
00:24:01.600 I noticed you used the number 60 in the beginning to express the note.
00:24:07.600 What's the reason behind those numbers? Is it MIDI?
00:24:15.600 Yes! It’s a MIDI number, indeed. One last question, anyone?
00:24:22.720 This might be a challenging one. Have you seen Brett Victor's talks?
00:24:28.920 His recent discussions highlight when using different expressions can be better than code.
00:24:37.680 In music, we have tools like Max and Ableton Live where musicians use live coding.
00:24:43.760 I enjoy Sonic Pi for teaching and having fun, but do you think it’s more expressive than those tools?
00:24:50.720 I don't know, as I'm not familiar with those tools. I can’t definitively say.
00:25:00.440 Creating professional music typically requires advanced tools and effects.
00:25:07.440 But Sonic Pi is great for teaching, having fun, and playing live.
00:25:14.720 With that, I don't know their electronic scene, which hinders my input.
00:25:19.000 Alright, thank you very much!