 I'm Ben Eggit and the very first thing I have to say is thank you. I truly think all of you have supported me, my co-workers, colleagues, Ruby friends, previous students, local community. Thank you Mike for selecting this talk. I'm actually really excited to give it so at least one person in the room is. I'm especially thankful to my wife Lily who came to the front. Thank you. You've been an awesome support to me. I know it's not always easy to be the spouse of a programmer. So a little bit about me. I was born and raised here in Salt Lake City. I've moved around a little bit but ultimately found my way back. This is my family, my wife Lily. She's from Peru. She's one of the most social and awesome people I've ever met. My daughter Kenna, she's halfway from Peru and she's the cutest girl I've ever met. She also likes eating rice and tofu and sushi but don't tell her mom that. So my hobbies like skiing, longboarding, guitar, music obviously and traveling. I've lived in a bunch of different places. Currently work in access development. Fantastic employer if you're interested in what we do please talk to me after. And I'm the organizer for the Drug Downtown Ruby User Group Meetup. Thank you, thank you. For those of you who aren't aware we have weekly hack nights Wednesday just at the coffee shop like two blocks up and monthly meetups just at a local company half a block away from here. So check it out if you're not downtown there's five other chapters right close here. Check it out at UtahRuby.org we have an awesome community so thank you. Okay so do I have any musicians in the house? All right I can't actually see half of you so yeah. Do you know any music theory? Okay so when I was in high school I took a course called AP Music Theory. I've been playing guitar since I was 11 and played in bands and done concerts since middle school and I thought this would be kind of a good way to add some depth to my music knowledge. The course was taught by the band teacher you know started out quite interesting. We learned like relationship between notes, scales, chords, learned circle of fists things like that. We're instructed to compose music by writing sheet music from scratch and we're graded on our ability to play the sheet music on a piano in front of the class. I'd never played piano so that was kind of interesting but the class continued to shift this direction writing melodies with accompanying chords in various styles like classical jazz blues etc etc. The AP test came and it was divided into three parts to test for theoretical knowledge then our ability to like hand write sheet music and a vocal test to see if we could sing along with the theory and I failed. I failed hard. I got a two on the test for those who don't know 3 is passing 5 is kind of the maximum that gets you more college credit or whatever. I actually aced the theoretical part but I'd never claimed to be a vocalist nor a sheet music expert like I said I've never played piano before that. So it was pretty bummed because I thought I was a decent musician you know for a 17-8 year old whatever I was at the time or at least a guitar player and I was abruptly corrected. You know maybe I was dumb maybe or maybe it just was taught in a way that I couldn't really understand. So the point of this talk is I think there's some problems with learning music theory. As a programmer I like to use this one first. Documentation is bad like what is this this beautiful song I'm sure maybe if I spent half a day I could key it out on a piano and it escalates. I think someone's just trolling on the right here actually. It may as well be written like this and for most of us it's more like this. All right so I exaggerate a little bit but enough said. The hippie eye is worse. This is a circle of fists part of my music theory with test was to drop from scratch. This is actually fundamental music. Once I understood it it was actually very simple. Nobody here has ever said that about their code before. Once I understood it it was actually very simple. This is actually a practical implementation of working with a sample of the API and without some kind of cheat sheet it gets pretty rough and even to understand this cheat sheet you better have a pretty deep knowledge just to get what's going on there. So documentation doesn't foster learning. In fact it feels a bit like this. If this were my first introduction to programming I probably would have fallen into a very different career. Though I must say like now this stuff's actually starting to fascinate me. A little bit of a tangent I was looking for some Ruby projects that could run assembly and or write assembly. I stumbled upon this little gem. So I found out like at least one other person here shared this interest. Just check out these features right. Did I love this comment from Ryan? Thank you Seattle RB. Okay so this quote is always wrong true to me. In fact Ryan Davis I just want to say thank you. In my mind you actually truly exemplify this principle here. You're one of those giants in my book so thank you. Okay are you sure you don't know music theory? Okay what is music really? So in this talk I'm going to show you how to start with nothing but a Ruby interpreter generate sine waves notes scales modes chords arpeggios and songs in a way that'll be understandable for those who've never touched an instrument before and hopefully it'll offer kind of an interesting perspective for those who are already well versed in music theory. Now just a warning I don't claim to be an expert remember I failed the AP test. Okay so please heckle me in person not on the interwebs. The remainder of my talk will bounce back and forth between slides music and code. Okay excuse me let's talk about notes. I'd first like to start off by talking about 440 Hertz who here knows what this is and what it has to do with music. Cool okay so 440 Hertz represents kind of 440 cycles per second it is an ISO standard ISO 16 1975 and it looks like this this is an A note okay so let me show you what this sounds like require live coding is my favorite thing it never goes wrong I actually love it but it always goes wrong so I'll show you what this code's doing in just a minute that's an eighth no okay so let's build an api uh this is my daughter uh she loves api as much as I do she just doesn't know yet um content from this talks available on github and upon speaker deck if you want to follow along or play with it after and I just tweeted it right before I started talking as well so in order to understand music we must really understand sound waves like physically as you probably know audio is this vibration typically we're talking about vibrations in the air approximately 20 Hertz to 20 000 Hertz that's kind of our human audible range right it really means the air is moving back and forth like anywhere between 20 and 20 000 times a second okay the lower that frequency the lower the pitch the higher the pitch all right so if you measure that vibration convert it into electrical signal um say using a microphone or a line in or something like that you'll get a electrical signal with the voltage varying in some waveform is the sound all right so we're going to do this like pure tone hypothetical of pure music um this computer generated music um but basically we're creating sine waves that fluctuate between one bolt and negative one bolt okay if you hooked up a oscilloscope volt meter something like that you'd you'd see that on audio and we come to see this in stuff in waves like we see over here um so when we hook our voltmeter to computer and it strikes the computer to read it like 40 if we want to do a cd like 44 100 times per second like our sample rate um we could add another one to do like two channel stereo or something like that that's kind of how music's written at the sound engineering level so let's build an out this talks a bit of music a bit of math and a bit of ruby um so i'm not going to really focus too heavily on the math it'll definitely be up here for those to see but i will go over the basics here so audio is just sine waves sine waves uh this actually the hardest part of the talk if you don't get it straight away it's only exceedance in front of it for a few hours like i did so um but on our examples we're going to be writing a wave file in mono format pcm mono format so let's find a couple terms here frequencies the number of cycles per second um sample rate is the number of frames per second what is a frame in this context will be like i said we're going to be writing wave format so the frame is basically a 16 bit floating bit number between negative one and one and what we need to do to make music is build to build a note is build up an array of different samples for a one second long note at this sample rate so uh we're also just going to find a duration which is how long we want the note to be so to build a one second long note we need to build one second times the sample rate of frames in our case that'll be 22,050 frames next we need to calculate the cycles per frame it's really just a fraction of a cycle each frame and once again once a cycle this is that full sine wave okay um so our fraction of a cycle per frame equals our frequency divided by the sample rate now we can calculate all the samples that make up our note sine wave with these few terms and and math parts so here's what the code might look like okay um i've i've kind of object oriented it to make a little bit easier to build a dsl around or whatever but it very much did not start this way it's just started out as scripting and hash scripting mostly um but anyway the idea is uh we define a frequency we define a duration then here's our three math calculations right in the middle and with that we use our favorite ruby module enumerable to build all these samples so we just map them all together um if you can see down here there's this is the math involved here we're basically just taking the sample um and running sine on a phase and this phase is going to be augmented each time we loop through by the sine wave cycle which is two pi radians as we see here okay so here's a quick demo of what that looks like okay so here's that same bit of code and here's the samples we're generating all right so there's 22,050 of these as you can see they go up and down from one to negative one and back to zero and when we graph it out it looks something like this so there's you know one cycle right there since we're doing an a note at 440 hertz 40 440 hertz there'll be 444 of those 440 of those and that's kind of what it looks like because we zoom out a little bit more so we just generated all of that okay all right um so let's talk about writing a file um we create the wave file with ruby standard library it's actually really not that hard but that's not the folks at this top um at one point i'd written a version of it that was pretty simple then i stumbled around this wave file gem and it's uh still a native ruby implementation it doesn't wrap any c libraries or anything it's actually super basic the whole thing's less than a thousand line of codes including lots of comments and so it's not that difficult to understand but it does support a few other formats like if we want to jump from mono to stereo or or different versions there so we define an output class looks something like this i'm going to cruise through this part um but basically the idea is we set up some some parameters for writing that wave file and then i've created a little play method here and i'm just going to include this module in different parts of my code so once we've done that okay then we can play cool so we're actually just writing that to a wave file um if you take a look it's just storing that it's like note.wave and playing that i'm just using af play which is a built-in apple playing thing um okay so so we learned yesterday from both paul and michael the power programming really lies in our ability to abstract okay i really like this quote abstraction lets us think differently um so the first abstraction i want to make is octaves what's an octave it's just double the frequency so the next day would be 880 hertz or the previous would be 220 hertz all right i've got a lot of slides that look like this and so if they succeed clap hard if they fail clap harder okay because this the this is the fun part of live coding uh or live code running more so here all right so basically the idea is um if we wanted to do an octave we would just double the frequency here all right so this brings us to a little bit more of an application we can extract this out into an octave class um i've added a couple extra features in here just like the ability to set the direction and the ability to do multiple octaves or set an amount of octaves as you build it but basically that is we're just uh taking that starting note looping through and doubling the octave if the direction is ascending or dividing the octave the frequency in in half if we need to go descending just mapping that all together into samples like we did in the notes okay so you know with that we can do something like this and if you see here those are the notes that got mapped out and we can play that same thing so the subtraction right all right let's move on to our next subtraction so tones and semitones semitones are the smallest interval in western music it's one fret on the guitar it's the adjacent note on a piano a tone is two semitones so uh tone is two semitone is one okay um there's 12 semitones in an octave so the mass side of this is basically to move up 12 semitones each step along the way is basically r to the power of 12 equals 2 so the 12th root of 2 i didn't know really how to notate that very well so i just typed it up uh 12th root of 2 equals something like 1.059 okay um so let's play all the notes um we can do something like this okay so if we set something up like the 12th root of 2 equals 2 to the okay we get that fraction there so we can do something like we'll create a note here i'll set it at 220 hertz no thank you and i'll cut my duration in half just to play it out quicker okay so we have this note here so i'm gonna just do the same thing and then at this point loop through it again and say we'll take that note's frequency and times it by our 12th root of 2 and we'll play the note and i forgot to say playing come on buddy we're walking up the scale like a chromatic scale right so let's play in every note from there thank you thank you for that excuse me that brings us to scales um the simplest scale we have is a chromatic scale most common scales have seven notes scales really just kind of all the notes played in order like uh this chromatic scale is all those notes probably in order like we just heard it doesn't actually sound too musical yet but it does sound like a bunch of sounds so let's talk about major scale this is the most common do re mi fa so la ti do basically takes seven specific notes in a pattern it's kind of happy um we have a minor scale that runs in this tone sema tone tone sema tone tone pattern there it's kind of sad i'm gonna kind of through this part a little bit just because i want to get to the fun stuff um who here understands modes okay it's quite a few less hands went down for my music theory guys who can name all the modes okay so modes this difficult concept but it's really easy to understand in ruby there's seven modes all together seven modes because there's seven notes on the scale simply put modes are just rotations of the major scale so take a look at this here's all the modes listed out do you see any patterns these interval sequences can just be rotated through we have this nice rotate method that lets us move arrays around so we can abstract out i just built a little modes module that we'll just kind of take this first pattern here and rotate it through all this stuff at the bottom is just alias methods for me to call it in a more music centered dsl way okay so just a quick demo on that one um there we go okay so that shows kind of our first mode you can see our second mode where you can pull these same things up using like major minor or any of the mode names okay all right so that brings us to scales um back to scales scales are basically just iterations of a mode from a starting point on a code so we use that same twelfth of a root of two the way that we've defined we look up a mode just passing in a scale types so here i've just made a little dsl where you can pass in the mode you want to do it'll just send that method over to our our modes there and we can do something like this deal dot new major okay it maps out all those things we can just plattie them we can play them all right so we got a major scale go we can do the same thing with different types of scales okay cool so next thing is thirds thirds are intervals on a scale they can be major or minor majors choose three semi tones minors four basically to do this we just need to map out a scale and choose a note you basically have two moves it'll either be major or minor depending on that pattern sequence so t plus t equals four t plus s equals three a full tone two full tones or a tone in a semi tone played sequentially this called an arpeggio um it's kind of what it looks like i'm going to choose through it though for sake of time um so if we had a scale here and we could go ahead and say here's a third and pass in that scale and play that so that's a third okay kind of interesting right uh so what are thirds all about really um ah too many windows okay um well third to the building blocks of a chord single notes are easy but to make a chord we need to melt it all down and clamp the values so they fit more in a wave format uh the code looks something like this basically we do the same thing we take our third then we go ahead and flatten all the values we group them all together add them at the different break points and we're going to set a ceiling on them though so that the most it could ever be is one in the case that the values equal more than one okay um so let me show you a chord here here's a chord and here's what it sounds like okay so that's actually a little bit too perfect like the character of music actually comes from instruments not having this perfectly round side wave but we're using computers and computers are pretty good at being perfect okay um so we'll get to that a little later um all right I just have two more talking points here and then I'm going to show a demo um so from this we can make chord scales you can make different chords from a scale they're automatically be minor or major based on what it what will fit into the scale pattern uh or this mode that we're using so each mode has a chord that you can make from throwing it up moving it up in thirds I just made this shoot sorry guys I failed all right um so that's what I was talking through there um here's what our scale steps look like uh that's just a little module that I'm going to include to augment that scale class but basically we're just creating a new scale and setting a jump that just builds a scale off of those next parts okay oh that one got fun okay uh this slide got messed up but the last piece is kind of these chord progressions um where if we have these different chord scales that we can play in order so a common progression is like this one five six four progression um it's basically just chords played in a specific quarter and uh let me give you just a quick demo here okay so I have a scale here we have chord okay so with that little thing I could do like let's play the fifth or the sixth and back to four so that's like this chord progression okay so if that sounds familiar it's because it is every pop song in the world it's ever made any monies written in that chord progression so quick recipe to making money um this could go on for days and days and days and days and days okay so my final abstraction i'm gonna show you some songs uh so here's a quick example whoops uh just writing some arpeggios um like i mentioned arpeggios are just the notes played in order harmonize it with a chord progression on top the extraction is like taking this kodi dsl and making it something we can actually program in um let me show you the code behind that song about six lines that's pretty cool we just define a frequency set up a scale fill up some arpeggios and pull them through okay um let me show you one other song here next song i'm going to show you is a simple song taking a melody once again it's pretty simple we're just defining a melody and running through that same stuff we've all just talked about all right um so we could take that and write more and abstract and do more if anyone recognizes this fan this song we're instantly friends friends okay and the last one i'm gonna show you it's very last thing um is kind of a more complex song and since this is all ruby we can do some pretty cool things with it so this song this song is stochastic it writes itself a little bit differently every time and we can make it write itself even more differently it's really easy what we love about this is we can do things like this like um hey let's rotate this melody like three spots over and let's shuffle this melody so it plays a little different every time and this song is truly writing itself i propose it'll be the new mountain list ruby comp theme song so much you can do with this um now i'm gonna admit this is not the best music library in the world it was never intended to be it's a creative code experiment right it's like this hobby thing that i'm super interested in um it's designed to teach you or rubius some music theory you guys probably have a better understanding after that than i did after this year long ap course and many years to come okay so that's it the code's not perfect there's lots of parts in there i'd see that i'd want to refactor but it's usable and it's flexible the dsl is not complete it's not stable it doesn't need to be it's not production ready whatever it is it's not tested it's not enterprise it's not cloud it's not a sass it doesn't need to be any of those themes okay it's not work so i really want to say plus one plus one love love whatever emoticons i can verbalize to james buck creative code is simply that it's experimentation and it's fun so i push this code up to github use it play with it change it there's no pride of ownership because i i don't think it's perfect still finding better ways to associate these abstractions um but it's fun to me and i hope you guys think about your own creative coding projects