 So welcome everyone to the session closure through the lens of music by Dave Yardwood, so thank you Dave We are so glad that you were able to join us over to you Dave Hello, everyone Good evening. It's actually morning here. It's about 9 o'clock in the morning. I'm 30 in the morning here in Durham, North Carolina on the east coast of the US and I've just had my breakfast and I'm ready to go so So I'm going to talk about the closure standard library And how we can understand What the functions in the library are doing by visualizing them through music But before we get into that a little bit about me I'm a software engineer at Keville But before that my educational background is in music composition and in the mid-2000s I Picked up a strange hobby, which is text-based music composition And I did not invent this concept There are there are actually a lot of languages out there for audio programming or music composition programming But at the time I couldn't find anything that really got at my goals Which were to generate or to create classical music By writing code and To have the full power of Western standard musical notation at my disposal So I ended up creating my own language called Alda And that's been my passion project for the last ten years So a little bit more about Alda As I've said Alda is a text-based music composition programming language The idea is text in music out and I've designed the syntax to be as easy as possible for people with little to no programming experience to learn Partially because at the time I myself had very little programming experience So the the language is a markup. It's what you see is what you get So this is very easy for new people to pick up even if they don't know about Kind of higher-order concepts like functions or classes or anything like that So Especially if you have a little bit of a music background learning this is very similar to it should map very cleanly to your understanding of concepts of music like notes and chords But as programmers we have additional superpowers and Markup languages are very easy to generate From from programming languages like a more touring complete programming language So I've been having a lot of fun exploring that So in this talk, I'll be showing you examples of ways that I've created music by generating This markup language from a programming language in this case closure So the library that I've created for this is called Alda CLJ and Under the hood what this is doing is it's putting together a string of valid Alda markup code Like we saw in the previous slide But now instead of working at the markup level, we're working a level higher where we're generating the markup So now this is great because we can create our own abstractions on top of the base Ideas like music like like notes and chords So we're going to see some examples of that I also want to talk about a couple of big ideas while I'm given this demonstration One of them is an idea that as functional programmers. I think we all understand inherently But you may not have heard it expressed in these terms So I'm going to borrow some terms from Eric Normand who is a well-known figure in the closure community And he just recently wrote this excellent book called Grocking Simplicity and in this book he talks about Ways that functional programmers approach writing new code and maintaining existing code By applying what he calls functional thinking So this is a lot like functional programming, but it's kind of coming from more of an angle of Practical skills that you would use in the workforce And less of an academic angle So even I think even if you have a lot of background in functional programming This is a great book to read because it might put to words some of the ideas that you've had in the back of your head for a long time So in his book Eric talks about one of the first things he talks about is this trichotomy of data calculations and actions and He describes this as skill number one that functional programmers must have in order to do what they need to do So functional programmer is always good at distinguishing between data calculations and actions Every definition in your code will either be data or a calculation or an action So data is just static data. It has no behavior It must be interpreted by some calculation in order for it to have any kind of meaning But it's really just data for example in JavaScript the data Examples of data would be a JSON object or an array And then functions every function is either a calculation or an action And a calculation is what we think of as a pure function. So this is a function that has no side effects It's not printing something. It's not making an HTTP request. It's not launching any missiles It is really just taking data as input and producing data as output and that is all that it's doing an important property of calculations is that You can call them any number of times with the same input and receive the same output So a random number generating function would not be a calculation That would be an action because it matters when you call it. You'll get a different result each time Another important property is that the sequencing of the calculations doesn't matter It doesn't matter what order you call them in because every time you call them you always get the same output from the same input By contrast an action is typically very It matters a lot when you call it and what order you call different actions in because they all tend to rely on global state and You know, let's for a metaphorical example here. Let's let's say we're writing some functions that represent making a peanut butter and jelly sandwich so You might have some functions that represent actions like slicing the bread and spreading the peanut butter But if you're not careful to call the actions in the correct order, you'll end up with a mess so Kind of the lesson to be learned here is that as functional programmers It's really best if we try to stick with data and calculations as much as possible But of course actions are inevitable. We always need to do some kind of action We need to make a database query or put an item in a queue You know actions are important as well, but we can help keep our code manageable by Deferring taking the action until we absolutely need to So why am I mentioning this? Well, I've been finding myself even before I read this book I think I was following this pattern as I've been writing code in Alda or Alda CLJ To create programmatic music. It's always a similar pattern to this where we start with some kind of data An example of some data that I've actually used in a musical composition is Weather forecast data that I pulled from the nether national weather service API So it might be a list of high and low temperatures. It's just data and then The calculation is some kind of algorithm that I come up with that Produces some kind of data that represents music in what I think is an interesting way Now this is just a calculation. We're not we're not actually playing the music data. There's no side effects here We're just taking this input data and producing an output data, which is it represents music We'll see some examples of this in later in the talk And then the action is just taking that data and performing it The other big idea that I want to talk about here is the power of visualization This is a screenshot of this great tutorial for the racket programming language Which I encourage you all to check out This is great because they're using a graphical environment called dr. Rackett it's a sort of like an IDE for racket and The way that they're this is a tutorial to the racket language And so the way that they're presenting the concepts is By using this library of graphics so that you can actually see You know a representation of what the what each function is doing So for example here, we can see that that series is a function that takes You know some kind of shape producing function and it produces three shapes of three different sizes Now we could also understand that by reading through the code and and trying to understand what hcapend is and what all this is doing But at a glance our brains are very good at understanding What this is and we're very good at abstracting especially in a visual way like this so the experiment here in this in this demo is To visualize things not visually but with with audio and with music so I'm going to jump into my editor here and Hopefully the audio is is going to come through here if it isn't please interrupt me But so we're going to use the Alda CLJ library here and I have both an Alda repel and a closure repel running So I can connect the closure repel to the Alda repel and Start a new score and Here we have just the markup syntax that I talked about at the very beginning of the talk So we can play this we can just hand it a string of Alda code and it will just hand that over to Alda So this is you this is useful, but as programmers we really want to work with data and calculations so Alda CLJ gives you this library of Functions that represent musical events and when you evaluate them, this is just a calculation Like we take a string and we end up with a record that represents An event that says to play play the following notes on a piano So this is just a closure map with a indication that this is supposed to be a piano and then we can see that these note Calls evaluate to records as well So these are closure maps that described a note So it has a pitch how high or low the note is and a duration how long the note is to be played And if we play this it produces the same result And it even returns the string of Alda code that it generated But I'm aware that not all of you are musicians So this I'm aware that some of this might confuse you with we're talking about half notes and eighth notes and dotted quarter notes and sharps and flats so actually to facilitate the programmatic Music that we're about to create. We're not going to work with Western music notation instead. We're going to work with just numbers So here's another example of something you can do with Alda CLJ Instead of specifying that a note is a C sharp or an E flat or something like that You can give it a MIDI note number. So the general MIDI spec has a range a very wide range of notes from zero through 128 and we can just give it a number and so what we hear here is The each note will be one semitone higher than the previous And we can also express durations not in musical terms like half notes But in milliseconds So here we have 100 milliseconds 200 milliseconds and we'll hear the that the duration of each note is double of That that was played before it. So this is all set up to kind of show What we can do with programmatic composition And what we just played we heard the notes we saw the notes Explicitly written out here. We have a note. That's 40 the 40 the note 42 For 100 milliseconds and the note 43 at 200 milliseconds But we have a clear pattern here We have a range of notes from 42 through 47 and we have a pattern where we start with 100 and then we double it every time So in closure we can use the iterate function to to multiply a Number by 2 and and keep producing that You know keep multiplying by 2 to get the next item in the sequence So that's an infinite sequence A lazy sequence and we can make it finite by just using by using the take function to just to just take six of those those numbers So we can see this result here So I've annotated this as one source of data and another source another data Source is the range of numbers from 42 through 48 and then we have another calculation here Which is that we're mapping this this this function So this is this is the calculation. We're taking our numbers for example the number 42 and the number 100 and The calculation is that we return a note that has that MIDI note and that number of milliseconds So we can kind of see this if we want to without playing it We can just do this calculation and and see the result So we have a list of six notes six actually six records that represent notes And they have the the note numbers and the durations that we've just talked about Then if we play it we can hear that this is the same as the example above All right, so now we've kind of established the basics of what I'm showing here So we can start to explore some of the functions in the standard library. Here's an easy one you know, there's map so map takes a function and List of values and it produces another list of values That is the result of applying the function to each item in the original list So we can use this to define a helper function called chromatic scale So what this does is it returns a list of notes Where they're just incrementing by one every time So if you were to play this on a piano, you would play every single key on the piano one at a time going up So we can hear what this sounds like on a harp So now we can play around with this a little bit more and bring in some more functions This is just a helper function that I've defined to grab the note number given one of these note records so this is the note record and We can use this function to pull out just the note number part of it So this is useful because we can use this in a predicate with filter So as I'm sure you're all aware filter is a function that takes a function which is a predicate and a list of values and it will return a list of values that match the predicate So we're going to keep only the notes in this chromatic scale That are even numbered So instead of every note in the chromatic scale will have just every other note And we can see if we count these in our original chromatic scale We had 78 notes and now we have half of that 39 notes So here again is so that we can hear the difference. Here's what we just played the full chromatic scale And then here is only the even numbered notes Now if you've watched TV or movies, I'm sure you've heard this sound before This is what we call a whole tone scale where you're moving up by two semi tones every time And this has a very it has kind of a dreamlike quality to it Which is why they often use this in like dream sequences and film and TV So just to play around with this a little bit more This this expression is equivalent to the previous one We've only changed the predicate function and now instead of saying if the note is even we're saying if the note number Modulo two equals zero in other words does the note evenly divide by two? It sounds the same But now we can adjust the number and hear what other intervals sound like So we can hear what it sounds like if you space apart the notes by three instead of two This is what we musicians call a diminished Quality and if we change it to four, this is an augmented quality So it's interesting to hear how these all have different kind of emotional characteristics. Let's hear a few more. Here's five and seven And then here's 12 which is octaves Let's learn about some more functions These are a couple of my favorite ones. Here's Rand Enth. It takes a list of things And it returns randomly selects one of the things in the list So if we call this a few times we can see that we get four and five and one five So it's randomly choosing each time And repeatedly is a great function that takes a number of times and a function And it calls that function that number of times So we can and we collect the results into a sequence or a list So we can collect three random numbers from a list using repeatedly and Rand Enth So let's Let's grab three random numbers from this chromatic range. That's the chromatic range on the harp So we'll get numbers like 33 82 73 So this is a I think this is an interesting way to come up with musical ideas I've done this myself in the past by Generating random numbers and just seeing how it sounds and then finding things that sound good And then I'll keep those numbers and I'll write out a more deliberate composition Using those notes Here's another great function. It's cycle. So this takes A list of things And it produces an infinite lazy sequence Where you repeat those items over and over again So for example, if you cycle one two three you get the infinite list one two three one two three one two three, etc So then we can take that infinite lazy sequence And make it finite by taking just the first 48 items The first 48 note numbers y48 well, it's it's a multiple of four four works well in music Um, I think it's all might also be a multiple of three, which may have been why I chose that because there are three notes Um, so this will sound even rhythmically And then we just take those numbers and map this function that turns each number into a note That one sounds pretty good. So the cool thing is if I evaluate this again All the clj will just depend the new notes onto the end and we'll hear them as soon as it's done playing what it's playing I just really enjoy this. I feel like I could do this all day And just listen for interesting musical ideas So this is great, right? We're generating music by using functional programming techniques Here's another example another function from the closure standard library. This is interpose And this one you can see pretty easily what it does by just if I just evaluate this Um, so we you know if we use interpose x with this list of letters, we get a x b x c x So it's inserting the item in between every item in the list So we can also easily hear what this does by interposing a high note between a bunch of low notes All right, here's another one. Um, and don't look too closely at this code It might not be the most optimized code for generating prime factors But I just wanted to play around with a prime factor function So if we if we map this prime factor function Over the range of numbers from zero through 10 We we will get the prime factors of every number so Zero and one don't have factors prime factors not sure how mathematically correct that is but that's what I came up with Um, and then prime numbers the only factor is the number itself. So two three five seven Those are prime numbers And then the other ones like for example eight is two times two times two So this is just to show that map cat. There's a function called map cat in the closure standard library And it's just like map Except uh, you use it when you have a function like this that produces a list of lists Or it produces a list so that when you map it over a list you get a list of lists Uh, but you might not want a list of lists. You might want a single, uh list So you can concatenate them all together by using map cat instead of map So this is this is also useful for the kind of exploration that we're doing here because we can use map cat to For each number in the list. So here's our data part. It's just the range of numbers between 40 and 60 We can map a function over this list that returns a list of notes for each of those numbers And then we'll we'll concatenate them together because we use map cat So this algorithm is a little bit trickier to explain it's a little bit more complex Um, but what we're doing is for each number in this list We are generating, uh One or more notes And the more notes there are the the faster they'll be so that we can all fit them into the same 500 millisecond span of time So 500 milliseconds for each of those numbers Uh to show the prime factors of each number And we're also, um adding each prime factor to the note itself So that might be confusing to hear verbally But we can um for one thing we can look at this Just look at the data that that this generates And um just for example the first four notes will represent the prime factors for the number 40 And so then those are two and two and two and five So we're adding each of those to the number 40 Because that's the number that we're getting the prime factors of And the duration of the notes will be 500 divided by four the number of notes And then the next number is 41, which is a prime number So that will take up the entire 500 milliseconds because that's the only factor And the note number is 41 plus 41 So there's a pattern here that will allow us to easily pick out the prime numbers because they tend to have high note numbers So, um What I'd like to demonstrate here is that as you listen to this Try to listen for the prime numbers. There'll be they'll they will be the notes that are higher in pitch Did you hear the prime numbers? So I think this is great because even if we you know if you were to look at this list as data You could find all the prime numbers by looking through and scanning for numbers that look higher than the rest But your ear is very quick at picking up the high notes the high frequencies as compared to the low frequencies All right now. I want to give a shout out to aditia. He uh, he asked me to think about generating music from fizz buzz And I got really inspired by that. So I came up with this So, um, we're going to represent fizz buzz just as a quick introduction to fizz buzz this is a elementary programming problem often used to test your skill and programming In any language And the idea is if the number is divisible by three, it's fizz. So three is fizz If it's divisible by five, it's buzz If it's divisible by neither you just get the number itself So one and two are not divisible by three or five and of course when you get to 15 That's divisible by both three and five. So you get fizz buzz So we can actually hear the fizz buzz cycle Uh here. So what I've done is I've defined some functions here without looking too deeply at the code Um from a high level what this is doing is it's taking Uh the divisor so three or five for fizz or buzz And a note number And it's producing a sequence where every n notes. So maybe every three notes we play a note So we'll the oboe will represent the fizz And the buzz will be the clarinet. So every five notes will play this other note And I've also added some percussion to make this So we can hear more of the kind of the rhythm of fizz buzz I'm going to comment these out for now. So we can hear just the pattern So what this is this is periodic notes inverse Now this is like given these factors Play a note whenever it's not divisible by any of those numbers. So this represents like the The the numbers that are neither divisible by three or five So if we hear this we can hear A hi-hat playing the notes that are not divisible by the numbers that are not divisible by three or five And the oboe is the fizz and the clarinet is the buzz Now the interesting the interesting thing about this is that this is actually it kind of has a natural rhythm to it And that's because it's cyclical Right mathematically Once you get to 15 and you go to 16 you've you've actually started over at one essentially with as far as Whether the notes are divisible by three or five And so we actually get this repeating pattern and we can hear this better. We can hear how it repeats if I bring in These other voices in the in the drums. So we have a kick drum and a triangle So these will just kind of show you What the what the pulse is so that you can better hear the rhythms of What the other instruments are doing. So there you go fizz buzz as music Now the last thing I want to show you before I go is an experiment that I did recently Which is applying similar concepts Um I wanted to take words from the dictionary or just any word any arbitrary word that you provide and And interpret it as music And so I was grabbing random words from this dictionary file on my on my hard drive For some reason a lot of these words have apostrophe s on the end I don't quite understand why there's so many of those but I filtered those out just to keep things a little bit more Saying more interesting maybe Um, so, uh You know kind of glossing over what all these functions are doing exactly I came up with um a way of interpreting Oh a string as a list of note numbers So we can see this if I map this function over the word hello We get 76 79 72 72 69 So these are all numbers that represent each character in the string so We can take you know the string functional conf for example And hear what that sounds like if we interpret those letters as notes And the space by the way represents a pause So you'll hear that pause in between And we're going to play the functional conf on a random instrument. You know, that's not a very good one You can't really hear the pitch And I'm going to stop that Let's try that again. This is like live coding in action Oh boy Hold on Sorry I'm going to restart this Okay, uh, let's grab another random instrument that hopefully has pitched this time I think that's kind of a nice rhythm, don't you? So let's uh, let's try this again But this time the words are going to be randomly chosen from that dictionary file that I showed earlier So for example, we might have uh, uh, prin, principe Presumes plaque. Oh, that has a bunch of p words Or we could get rover exterior's biggles I guess we have some proper names in there as well Um, and so each each of these words Is going to be played by a different instrument all at the same time And why this is interesting is that you can hear Uh, kind of the the rhythmic way that these words play off of each other The words are left Transmissible and moisturized The longer words kind of linger at the end because they it takes longer for them to finish All right, and uh, let's actually let's look let's listen to another one. I think we have time This is kind of a crazier one This is we have despoiled daggers and axis And lastly, uh, this is a an absurd poem by e ecomings. Um called me up at does Here this is the poem me up at does out of the floor quietly stare a poisoned mouse Still who alive is asking what have I done that you wouldn't have And so let's hear, uh, how this poem can be interpreted by this algorithm. All right, that's all I've got Thank you very much for listening to my talk And enjoy the rest of the conference Thank you, Dave. Uh, such an amazing and interesting session today. Uh, we have some time so if anybody has any questions, uh, Dave is available for the next Um, seven to eight minutes to answer There are some amazing comments on the chat as well, uh, Dave for For the session so oh, yeah, I'm looking at those now People are actually saying like, uh, I think nadej is saying once more once more Isn't for an encore Yes, absolutely Am I taking dj requests? If yes aditya wants to hear appease demo gods. Yeah, okay. I could do that Let me share my screen again All right, so we'll change this to appease Oh, actually we could do three different words. We could do, um appease Demo gods Oh, I like that one Any other questions? The whole poem up thread. Oh, right, right. Okay Yeah, let's do it This is going to be very interesting because I don't know how well my algorithm handle its, uh, characters like the lambda character there But let's give it a try It'll either want it'll either work or it won't so let's see what happens Let's maybe define this as a var appease demo gods So I think this will be very long. So I might need to cut it short, but we'll just hear what it sounds like It's pretty mysterious In the interest of time, I think I'll cut that off, but yeah It's always interesting to hear what the different, um the like how uh What kind of musical ideas emerge from listening to just random, uh, you know programmatic Output of uh transforming a string into, uh, you know music All right, so rakesh asks are you using a lib to generate the midi instructions or have you written the midi bindings yourself? Ah, yes, so the uh, the midi is actually happening in a background process uh called alda player And it's um these are spawned by alda. It's kind of all under the hood Um, but the alda player process is a jvm process Uh, I actually wrote it in kotlin and Uh, it's using the the java the java actually has a very good midi library built into it There's the java x dot sound midi package And so i'm using a midi synthesizer and midi uh sequencer So, uh, thank you dav and dav do you also want to share uh where people can reach out to you? That would be very helpful for you audience I'm glad you mentioned that I actually had another slide that I totally forgot to show at the end Uh, so yeah, let's chat. Um, uh, I actually uh There's there's a slack group for alda Which is um, not very active at the moment. I'd love to see more activity there. So if anybody is interested in alda Um, please feel free to join for free and you can share your scores that you come up with Uh, share your music with with the community Um, you can you can at mention me there for if you have any questions about alda or anything Um, you can also feel free to reach out to me personally email and twitter are both good ways to do that Wonderful. So thank you so much. Dave. It was I think wonderful Start for the weekend and great, uh presentation. Thank you