Talks

How music works, using Ruby

That strange phenomenon where air molecules bounce against each other in a way that somehow comforts you, makes you cry, or makes you dance all night: music. Since the advent of recorded audio, a musician doesn't even need to be present anymore for this to happen (which makes putting "I will always love you" on repeat a little less awkward). Musicians and sound engineers have found many ways of creating music, and making it sound good. Some of their methods have become industry staples used on every recording released today. Let's look at what they do and reproduce some of their methods in Ruby!

RubyConf 2022

00:00:00.000 ready for takeoff
00:00:16.940 well welcome to rubyconf
00:00:19.740 I come really happy to be here
00:00:22.560 nice to be in person I so today I'm
00:00:26.460 going to talk about music a specifically
00:00:28.920 recorded music and like what kind of
00:00:30.900 tricks producers and musicians used to
00:00:33.600 make the sound good and I'm going to
00:00:35.640 explain that with with a bunch of Ruby
00:00:37.920 codes
00:00:39.719 um
00:00:41.180 so what I like to do is is use
00:00:44.160 programming to understand the world so
00:00:45.780 the world is a very complicated place
00:00:47.340 and we are programmers and we have this
00:00:49.920 amazing Tool uh to actually comprehend
00:00:52.140 things because if you write code you
00:00:55.440 actually have to know uh you actually
00:00:57.480 have to understand it or otherwise you
00:00:59.160 can't write the code so it forces you to
00:01:00.840 like really learn it and that's what I
00:01:04.019 did with music
00:01:05.519 uh the music stuff I'm going to show to
00:01:07.619 you today
00:01:08.580 so what I'd like to do is is this
00:01:10.680 process so just don't understand it like
00:01:13.380 try to write some codes and fill for for
00:01:15.780 a while and then at some point
00:01:18.439 you sort of kind of start getting it and
00:01:21.600 you end up with some simple hopefully
00:01:23.159 simple code I'm going to show the
00:01:25.320 outcome of this to you all today
00:01:28.439 so we're going to get started with like
00:01:30.720 a very brief history of the technology
00:01:32.400 behind music that kind of like was
00:01:34.439 developed in the last six years and that
00:01:36.180 will bring us to digital audio and
00:01:38.520 that's where we can start using Ruby to
00:01:40.320 uh to actually see what's going on
00:01:43.140 so there are a bunch of of code examples
00:01:45.720 uh that go along with the stock you can
00:01:48.299 find those here I will also post the
00:01:50.399 slides after the talk but if you want to
00:01:52.979 follow it now you can quickly go to this
00:01:55.200 year wrong
00:01:57.320 so what is music uh actually it's it's
00:02:01.700 described by a couple of things sort of
00:02:04.740 uh uh uh so audio like what you're
00:02:08.700 hearing right now is a waveform uh
00:02:10.979 that's sort of like like moving through
00:02:12.959 the air so there's like a whole bunch of
00:02:14.459 air molecules uh bouncing into each
00:02:17.040 other and our ears perceive those and
00:02:19.680 kind of make a make a model out of that
00:02:22.860 so away from is like the easiest way to
00:02:25.980 visualize it is is like this like it's
00:02:29.220 basically uh the same as a as a wave in
00:02:32.520 a in a pool of water uh except with with
00:02:35.459 air molecules instead of water molecules
00:02:39.060 so this uh these waves kind of like
00:02:41.819 travel through the air from a Sound
00:02:43.379 Source to your air so in this case
00:02:44.879 there's a there's a big speaker here A
00:02:46.739 bunch of speakers and they they create
00:02:48.959 these waves and then there are a bunch
00:02:52.200 of like little hairs in your ear called
00:02:54.120 follicles that actually that vibrate uh
00:02:57.060 as these uh these vibrations come in and
00:03:00.120 they generate the signal that's then
00:03:02.160 processed in your brain and that creates
00:03:04.620 uh this sort of Illusion of speech or or
00:03:08.220 or what we call music so there's nothing
00:03:10.620 like really inherently musical about
00:03:12.900 things
00:03:14.760 five
00:03:15.840 I prefer preferred brand
00:03:18.599 okay that's also audio
00:03:22.680 uh yeah so your brain uh your brain uh
00:03:27.239 generates like it has these models that
00:03:29.819 kind of like make sense of these
00:03:31.500 incoming vibrations and like the the
00:03:33.959 neuroscientists don't really truly
00:03:35.879 understand how this works yet so there's
00:03:37.560 some kind of magic going on that that
00:03:39.840 kind of turns these these very simple
00:03:41.700 patterns into into something we we call
00:03:44.340 music
00:03:45.420 uh
00:03:46.799 so how does the brain uh what I would
00:03:49.260 like the basic uh uh patterns that the
00:03:51.900 brain uses to map this so the first
00:03:54.180 thing is pitch
00:03:55.500 so pitches is uh is the is the amount of
00:03:58.680 oscillations uh per second or like any
00:04:01.080 given time span uh and like and like if
00:04:04.440 if if we go lower you end up with like a
00:04:06.959 basis instrument like a bass guitar and
00:04:09.480 higher up as like a violin and a more
00:04:11.760 oscillations per minute per second you
00:04:14.040 have the higher we perceive it to be so
00:04:16.739 like a basic pitch sounds like this
00:04:20.820 foreign
00:04:26.360 we're all familiar with
00:04:32.580 so this is the exact same shape of a
00:04:35.639 waveform only like it happens like less
00:04:37.860 or more times per second
00:04:41.520 so the next thing uh is stambra so
00:04:44.759 Dumber is all the little variations
00:04:47.100 within those within the page that uh
00:04:50.160 that gonna create like a fiber on the
00:04:52.199 sound so that would sound something like
00:04:53.639 this
00:04:55.320 oh excuse me
00:05:01.160 so this is uh like a little thing from
00:05:04.259 the Beatles they did in the 60s so
00:05:06.960 there's a single pitch here but there's
00:05:08.940 just a whole lot of uh detail to the
00:05:12.000 sound because it's generated by a lot of
00:05:14.280 different different instruments that all
00:05:15.840 afternoon
00:05:18.840 and finally we have Tempo so Tempo is is
00:05:22.440 created by silence really so this uh if
00:05:25.139 you uh introduce some silence in a in a
00:05:29.639 fixed way in between uh uh actual
00:05:33.060 waveforms then you will get something
00:05:34.740 that sounds like a rhythm
00:05:37.199 so drama Loop like this it's like an
00:05:40.139 example of tempo
00:05:42.240 and really it's about the silence you
00:05:44.220 know the the silence in between uh
00:05:46.740 creates this illusion while it's not
00:05:48.300 really an illusion but it creates this
00:05:49.979 perception of of something having a
00:05:51.780 rhythm so if you combine those uh things
00:05:54.180 you end up like with like a little song
00:05:56.520 So if you just get the skill and we get
00:05:59.340 some tempo
00:06:01.820 and we get a little base
00:06:06.740 yeah it's almost nothing but it's
00:06:09.240 already like sort of like a song
00:06:14.580 so back in the day uh music was always
00:06:16.919 live so there was no recorded music
00:06:19.320 and then sort of like technology
00:06:21.479 happened as we're well aware like they
00:06:24.180 changed the world in a number of
00:06:26.160 different ways
00:06:27.840 and this led to recorded music and we're
00:06:30.120 just going to quickly go over uh like
00:06:32.639 this these sort of like these steps and
00:06:34.259 technology that led led us to digital
00:06:36.660 audio today
00:06:39.120 so it all started with this guy uh
00:06:41.880 Thomas Edison so he came up with um like
00:06:45.180 this device
00:06:46.500 so this is like a little role and it if
00:06:50.759 you shout really hard into that hole it
00:06:52.800 will vibrate like a little needle a
00:06:54.720 little bit and that will etch uh etch a
00:06:57.479 pattern into into this wax cylinder in
00:06:59.940 the middle and uh once you play that
00:07:02.520 back you have like a very rudimentary uh
00:07:05.699 sound device so that sounds a little
00:07:07.440 like this
00:07:12.240 so this this wasn't a huge hit yet and
00:07:15.120 Thomas Edison had like had made one
00:07:17.280 really big mistake which led to this not
00:07:19.620 being adopted he liked it really like
00:07:21.780 the patent stuff so he had a patent on
00:07:24.300 the cylinder and therefore nobody could
00:07:26.340 could expand on the technology or like
00:07:29.039 use it for themselves so this ended up
00:07:31.560 completely failing and this other guy
00:07:33.720 called Berliner came up with the idea of
00:07:36.900 just using a cylinder so maybe if Edison
00:07:40.139 had not patented this uh uh sorry you
00:07:43.680 use a disc so maybe if Edison had not
00:07:45.599 patented the cylinder we would have been
00:07:48.479 walking around with like this little
00:07:49.680 cylinder for like 50 years instead of
00:07:51.539 disks and so it goes to show like
00:07:54.419 patterns are dangerous if you want your
00:07:56.160 technology to be adopted
00:07:58.400 so the
00:08:00.300 uh
00:08:01.680 his discs look a little bit better
00:08:06.599 but it was still quite quite uh quite
00:08:08.699 bad
00:08:09.539 and then we got like this Magnetic Tape
00:08:11.580 technology and that sounded pretty good
00:08:18.660 some people say like the technology
00:08:20.879 never got better after this this was
00:08:22.800 sort of like the best thing and it only
00:08:24.660 got worse maybe they're right
00:08:27.360 so these tape decks uh really made like
00:08:30.419 a lot of like stuff possible in the
00:08:33.300 studio and especially after the second
00:08:35.880 world war happened like we got these
00:08:37.680 cubes and we got like multi-channel
00:08:40.560 recording stuff so uh and they're really
00:08:43.560 like created an industry of uh where
00:08:46.620 recorded music actually became more
00:08:48.180 important than live music so it became a
00:08:51.240 product and people made made these
00:08:53.160 records and like mixed them on these uh
00:08:55.800 these desks like this you know we got
00:08:57.779 transistors
00:08:58.980 and that brings us to the 80s because in
00:09:01.019 the 80s uh like a whole Music World
00:09:03.300 digital so up until then uh everything
00:09:06.660 was still really about a Groove uh in a
00:09:09.540 disc for example and it would actually
00:09:11.760 be fully analog but now we got the CD
00:09:15.360 and
00:09:16.920 uh we got computers so now like these um
00:09:20.700 big studios of the past or kind of like
00:09:23.720 uh uh we got software that that could
00:09:26.940 just do the same thing and nowadays you
00:09:28.980 can you can get your little laptop and
00:09:31.019 basically use all the tools that used to
00:09:33.839 be available in the in the past in these
00:09:35.940 big studios
00:09:37.500 so what we're going to do now is kind of
00:09:40.320 dive into what digital audio actually is
00:09:42.480 and we're going to replicate a few of
00:09:44.040 these tools that you're seeing on the
00:09:45.240 screen here now we're using Ruby with
00:09:47.519 the goal of like better understanding
00:09:48.899 what they do
00:09:51.540 so digital audio it's
00:09:55.320 a bunch of samples so if you have a
00:09:57.720 waveform
00:09:59.220 like this one for example like on every
00:10:01.440 corner of in this diagram sample is
00:10:04.680 being made so it's basically measuring
00:10:06.480 like how high the intensity of the
00:10:09.600 signal is at that point
00:10:11.640 and if you use all those samples to you
00:10:14.519 can build build that wave back up and
00:10:16.980 and play it and uh you'll hear the
00:10:19.440 basically the same thing as as a thing
00:10:21.240 that you sampled if you do it often
00:10:22.560 enough the human hearing is not uh
00:10:24.660 cannot perceive enough detail to
00:10:26.220 actually know that it's been sampled
00:10:27.660 it's it's indistinguishable
00:10:31.380 so digital audio is a whole bunch of
00:10:33.420 numbers in a row uh in the sequence uh
00:10:36.899 deck form those waveforms and allow us
00:10:39.240 to to play them back
00:10:41.100 so we've got a bunch of tools and uh
00:10:42.959 away some Ruby of doing something with
00:10:45.000 audio so I picked for this presentation
00:10:47.579 this gem so this is the wave file gem
00:10:50.120 that can write and read waffles which
00:10:54.180 which are basically just sequences of
00:10:56.220 these samples
00:10:58.320 so we've got a little code here which is
00:11:01.380 not super important but this allows us
00:11:03.660 to read back away file and just put it
00:11:05.459 all in a very big array so we have like
00:11:07.500 one uh sequence of numbers to work with
00:11:10.200 which kind of makes other stuff simpler
00:11:12.839 and then you end up with this
00:11:15.000 so it's just a whole lot of numbers uh
00:11:17.880 so they're both positive and negative so
00:11:20.399 they so this waveform kind of oscillates
00:11:22.620 around the zero so so parts of it are uh
00:11:25.920 on the negative side of things and parts
00:11:28.019 of it are on the positive side of things
00:11:30.720 so this is kind of hard to do something
00:11:33.720 with for a human because it's just like
00:11:35.339 random numbers and we can't really make
00:11:37.440 sense of that so we're going to use
00:11:39.360 another gem
00:11:40.500 gently BNG to uh to like make some
00:11:43.980 visualization so far we're actually
00:11:45.480 doing
00:11:47.160 so if we uh loop through these samples
00:11:49.980 and we create an image and we just draw
00:11:53.279 a dots on that image we end up with with
00:11:56.519 a visualization that looks a little like
00:11:58.620 this
00:11:59.700 so this is some real world audio which
00:12:02.399 looks pretty random so like all these
00:12:04.140 dots are just Just One Look one of those
00:12:06.660 samples uh from one one of those
00:12:08.760 waveforms and if you if you do this
00:12:12.060 visualization on a mathematically
00:12:14.339 generated wave then you actually end up
00:12:16.500 with a very uh uh with a very uh
00:12:20.160 readable thing
00:12:23.700 um so this is only like maybe like 10
00:12:25.800 milliseconds of audio that we're looking
00:12:27.300 at here at the moment so what we do next
00:12:31.079 is we're going to group those uh those
00:12:33.779 samples into into buckets so we can like
00:12:36.060 look at like some longer time frames and
00:12:38.220 like still see what's going on
00:12:40.320 and that code looks like this like if
00:12:42.600 you if you this is all available on
00:12:44.399 GitHub so if you want to get into that
00:12:45.959 uh definitely check it out
00:12:48.300 so we end up with a visualization that
00:12:50.279 looks like this so if you've ever work
00:12:52.139 with any digital audio software uh this
00:12:55.260 looks probably looks familiar so you'll
00:12:57.180 see like the intensity level of the
00:12:59.339 Waves basically right here
00:13:02.459 so now we can look at some stuff uh and
00:13:05.040 and next up we can actually start
00:13:07.019 modifying the audio as well using Ruby
00:13:10.380 so the first thing we're going to do is
00:13:12.540 amplify the audio so we're going to make
00:13:14.279 it louder
00:13:16.200 that used to be done with with tubes
00:13:18.420 with resistor amps and we're going to do
00:13:21.060 with Ruby
00:13:22.920 so
00:13:24.120 uh we got a we got a piece of audio here
00:13:26.760 it sounds like this
00:13:33.060 that's already pretty loud so I'm going
00:13:34.680 to turn down the volume a little bit
00:13:39.660 and we want to amplify it so it's a
00:13:42.000 little bit louder so you can see that
00:13:43.500 it's louder here because the Peaks
00:13:44.880 actually go up and down like further
00:13:47.160 than in the first sample
00:13:49.200 and the way we're going we we're going
00:13:51.120 to do that is simply by uh multiplying
00:13:54.600 uh every sample with some ratio so we
00:13:57.480 got we got samples that are like say
00:13:59.880 five uh have a value five thousand and
00:14:02.220 if we multiply it apply them by 2.0 uh
00:14:05.399 everything is going to be like twice as
00:14:06.899 loud and that sounds like this
00:14:12.720 and I think that's sort of the the theme
00:14:15.480 of a common part in this presentation
00:14:16.980 that actually a lot of this audio stuff
00:14:18.779 is quite quite relatively simple math
00:14:21.660 we're doing nothing fancy here and uh
00:14:24.000 and and it actually does what we want
00:14:28.200 um what what could also happen is that
00:14:30.899 like we that we amplify something by too
00:14:33.180 much so if you uh that's called clipping
00:14:37.079 so if we go outside of the range of of
00:14:39.959 of of of what our uh what the floating
00:14:43.260 Point memory we're using here can take
00:14:44.959 uh you end up cutting up the the uh the
00:14:49.019 the highest part of the of the waveform
00:14:51.060 and you end up with like something that
00:14:53.100 that audio Engineers hate I'm sure the
00:14:55.079 guys in the back know about this as well
00:14:57.000 and you get clipping and which sounds
00:14:59.279 like this
00:15:04.620 and uh this this is a form of distortion
00:15:07.440 which also became like a part of of
00:15:09.779 music because like if you think about
00:15:11.519 like a guitar uh uh like heavy metal
00:15:14.160 guitar sound for example that's
00:15:15.720 basically uh like a pleasant way of
00:15:18.180 clipping clipping uh sounds
00:15:23.459 and in this casuals you'll see here that
00:15:26.760 the peaks of this uh of this waveform
00:15:29.459 kind of go over the the limit of what we
00:15:32.160 can take and at least leads to this
00:15:34.380 Distortion effects
00:15:37.199 so the next thing uh we're going to
00:15:39.360 discuss is mixing so mixing used to be
00:15:42.180 done while it's still done on on these
00:15:44.339 desks like this they're kind of like
00:15:45.839 merge electronic electron occurrence
00:15:49.019 and if you want to mix stuff you take
00:15:52.199 multiple uh multiple pieces of audio and
00:15:55.260 you're gonna you merge them together and
00:15:57.120 you end up with a more complicated
00:15:58.380 waveform
00:16:00.360 and in this case we're going to take
00:16:03.839 those two samples of those two pieces of
00:16:06.660 audio at the top and we're going to
00:16:08.160 merge them into the more complicated one
00:16:09.959 at the bottom
00:16:11.220 the way we do that is by summing them up
00:16:14.040 so we're reading like these free wave
00:16:16.560 files so they all have the same length
00:16:20.220 and if we do protocol and and simply sum
00:16:23.880 up the numbers uh uh from each one of
00:16:27.360 them and create a new array output array
00:16:29.760 of them with with like where every
00:16:31.740 sample is summed up on the same position
00:16:34.079 you end up with this waveform so this is
00:16:37.740 like a really like a ruby mixed
00:16:40.579 wave
00:16:42.360 foreign
00:16:46.800 so again what you see here is that if
00:16:49.019 you do the simple stuff uh correctly
00:16:52.440 then it actually just works which kind
00:16:54.420 of surprised me as I made this
00:16:55.740 presentation because like in theory I
00:16:57.420 know that it's supposed to work like
00:16:58.740 that but if you actually just do the
00:17:00.180 math then you get the same result
00:17:03.240 so next up is compression so compression
00:17:06.299 is a trick audio Engineers use to make
00:17:09.179 stuff sound louder basically so you get
00:17:12.059 rid of the Peaks so you can so you can
00:17:14.339 make the the whole thing louder so if
00:17:16.260 you listen to stuff on the radio like
00:17:17.880 any anything you listen to will be
00:17:20.100 compressed
00:17:21.480 uh that used to be done by uh by devices
00:17:24.780 like like this one in the past
00:17:27.000 we can do with Ruby as well so the basic
00:17:30.240 principle is that if we have a waveform
00:17:32.340 like this
00:17:33.480 we're going to choose a threshold for it
00:17:35.880 so we're going to pick a live a point
00:17:38.299 where we want uh where we think that the
00:17:41.280 Peaks that go beyond that point are too
00:17:43.260 loud
00:17:44.460 and then we're going to like
00:17:46.679 make those make those Peaks less loud by
00:17:50.220 just bringing down the volume only on
00:17:51.840 those Peaks
00:17:53.340 and next up we can apply uh we can apply
00:17:57.799 what's called makeup game so because we
00:18:01.440 lower the Peaks we are now able to just
00:18:03.960 bump up the volume on all the uh on all
00:18:06.780 the uh on all the samples without
00:18:09.179 actually hitting this clipping limit uh
00:18:11.460 that we saw earlier
00:18:13.500 and devil uh change uh
00:18:16.919 uh double change the waveform so let's
00:18:19.980 we're starting with like these these two
00:18:21.660 symbol hits
00:18:22.880 uh I'll I'll let you hear them
00:18:27.120 foreign
00:18:30.240 yeah so we've got those two simple hits
00:18:32.100 and what we're going to do is is reduce
00:18:34.559 the Peaks on them so again We're looping
00:18:36.539 through all the samples and we're
00:18:37.799 looking for all the samples that are
00:18:39.480 above the threshold and then we're
00:18:41.400 making them less loud using uh using a
00:18:43.799 ratio
00:18:45.660 and if they're not above the threshold
00:18:47.400 they're just going to keep them as is
00:18:49.679 and then in the second pass we're going
00:18:52.380 to apply makeup game which is exactly
00:18:55.080 the same thing as the amplification
00:18:56.520 thing we used earlier it's uh we just
00:19:00.480 got to make every sample louder now
00:19:03.240 and if you combine those two steps you
00:19:05.580 end up with uh like two two method
00:19:09.000 feeding feeding each other output into
00:19:11.220 each other
00:19:12.780 and we go from like this waveform
00:19:18.600 to this this shape so if you look at
00:19:21.480 them
00:19:23.160 if you look at uh at the difference
00:19:24.840 you'll see that that uh it's way less
00:19:28.380 peaky so like the the sort of like the
00:19:31.200 the samples in the beginning uh they're
00:19:34.260 from the Peaks are kind of like toned
00:19:35.880 down and the softer part is actually
00:19:37.679 louder and that sounds like this
00:19:41.520 foreign
00:19:49.260 so this is probably the world's worst
00:19:51.240 compressor but it is a real compressor
00:19:53.820 and if you start to notice this you'll
00:19:56.700 hear this like everywhere because like
00:19:58.380 any anywhere especially in techno music
00:20:00.840 there's usually a lot of heavy
00:20:02.220 compression going on and you can kind of
00:20:03.840 like detect it are doing it by just
00:20:06.960 listening to sort of like how how things
00:20:08.700 Fade Out like if if a simple Fades out
00:20:11.039 naturally it will it will end quite
00:20:13.559 quickly and it will be a lot softer uh
00:20:15.660 quite fast and if it's compressed then
00:20:18.000 the sort of like tail end of the
00:20:19.559 waveform it's just going to be much
00:20:21.059 longer
00:20:23.100 yeah and that's that's all there is to
00:20:24.900 to compression
00:20:26.940 and uh we're going to finish up with uh
00:20:30.360 a bunch of ways of making sounds
00:20:33.000 so
00:20:34.200 uh
00:20:35.760 a lot of music nowadays uses
00:20:37.799 synthesizers
00:20:39.299 so this is a mooc synthesizer which is a
00:20:42.059 which is really a beast so there's a lot
00:20:43.799 of analog components in here and this
00:20:46.080 thing can do a bunch of different things
00:20:47.700 we're going to cover a couple of them so
00:20:50.100 the first thing we're going to cover is
00:20:51.720 making noise
00:20:53.460 so if we
00:20:55.440 want to make noise and uh in Ruby it's
00:20:59.520 really not that hard because we only
00:21:01.200 have to use a random function to just
00:21:04.200 generate a whole bunch of random samples
00:21:06.539 so uh we're just going to do this a
00:21:08.880 bunch of times and just like get a
00:21:11.160 number in this range and plot it and add
00:21:13.919 it to the array and if you visualize
00:21:15.600 that it looks like this
00:21:17.780 so noise is basically Randomness uh and
00:21:22.200 we can get pretty close with Ruby as
00:21:24.000 close as you can on a computer so this
00:21:26.940 is some real Ruby generator noise
00:21:33.980 you might wonder like why do we need
00:21:36.360 noise because it's noise and nobody
00:21:39.720 cares but noise actually used a lot in
00:21:42.179 music production because if you use
00:21:45.000 silence to kind of like introduce a
00:21:46.740 Tempo you end up with something like
00:21:48.240 this
00:21:55.799 so this these noise samples if you cut
00:21:57.780 them off and like and put silence in the
00:21:59.820 middle you end up with something that
00:22:01.740 looks a lot like sounds a lot like like
00:22:03.960 a snare drum so this this sound is being
00:22:06.299 used in a for percussive sounds in a lot
00:22:09.960 of electronic music
00:22:12.419 uh next up is is the square wave
00:22:15.720 so Square wave is is one of the simplest
00:22:18.539 ways of making a pitch so Square wave
00:22:21.120 sounds like this
00:22:23.820 it's very ugly but if you do some
00:22:26.340 processing it might get better
00:22:29.039 so to do a square wave we're going to
00:22:30.960 create an oscillator in in Ruby so an
00:22:34.260 oscillator is something that that can
00:22:36.299 generate uh these like rows of samples
00:22:40.400 to get a certain uh get a certain sound
00:22:43.440 so we're going to make one using the
00:22:45.140 innumerable
00:22:46.940 module so if you include innumerable and
00:22:50.100 you implement each like this you can
00:22:52.799 just use this in in any uh in other
00:22:57.120 other renewable compatible uh
00:23:00.120 things from Ruby and uh and easily
00:23:02.760 generates from a race
00:23:05.039 so if you look at the implementation of
00:23:07.440 the next sample method here
00:23:09.620 this sample this generates new samples
00:23:13.500 so so anytime you call it for increment
00:23:15.600 increment into position by one
00:23:17.820 and if the position is is in a certain
00:23:22.200 State we're going to like generate a
00:23:24.000 high positive number if it's not if it's
00:23:25.980 in the next state that we're going to
00:23:27.299 generate a high negative number so that
00:23:30.960 so that way we get like an alternated
00:23:35.700 um uh sound in our sample
00:23:40.080 so we're going to call you this
00:23:41.820 oscillator like this so we're creating a
00:23:43.919 new one uh we're creating an output
00:23:46.020 array and we're just going to call the
00:23:47.580 uh this oscillator a bunch of times and
00:23:49.679 add it to add it to the output
00:23:52.020 and then you end up with a
00:23:54.720 uh with an image that looks like this so
00:23:57.780 you'll see that that this is this the
00:23:59.580 reason it's called the square wave is
00:24:00.840 that it like if you would draw a
00:24:02.340 vertical lines in this you would end up
00:24:04.200 with a whole bunch of squares
00:24:07.260 uh so this is one of the LA core
00:24:09.419 components of electronic music even
00:24:11.520 though it sounds horrible on its own and
00:24:13.860 the next one that's used a lot is is the
00:24:16.740 sine wave
00:24:18.120 and the sine wave looks like this
00:24:21.480 you can draw it using a circle as this
00:24:24.059 uh this image demonstrates
00:24:27.360 so this is like a fundamental building
00:24:29.220 block of of a lot of sounds
00:24:32.520 actually all sounds
00:24:35.700 and the way we generate a sine wave is
00:24:37.860 we we use use the math dot scene
00:24:41.580 function in Ruby and we can use spy here
00:24:44.220 to plot a position in this circle based
00:24:48.780 on the position we have and we can can
00:24:51.240 generate use that to
00:24:53.299 generate samples again
00:24:59.100 and then we end up with a uh with a
00:25:01.380 visualization like this and it sounds
00:25:03.000 like this
00:25:08.520 so this is a basic building block block
00:25:10.740 for uh for basically our music and we
00:25:15.240 can also create a chord with these
00:25:16.799 oscillators so if we take three
00:25:18.419 oscillators
00:25:21.360 um and we uh pick different frequencies
00:25:23.940 for all of them
00:25:25.380 and we combine those by doing the mixing
00:25:28.020 trick we did earlier uh and we just sum
00:25:31.140 up those numbers you end up with a uh
00:25:34.799 something that sounds like this
00:25:40.400 and this uh this way you can uh you can
00:25:44.940 generate any combination of of nodes
00:25:47.400 which which forms phone chords uh that
00:25:50.640 would be used in music as well
00:25:52.679 and if you're interested in this it will
00:25:54.840 be it's it's cool to look at the Fourier
00:25:56.520 transform so what we were doing here is
00:25:58.500 we were using multiple formulas to
00:26:01.260 generate sine waves and combine those
00:26:03.020 into uh into a more complex waveform but
00:26:06.900 using the Fourier transform you could
00:26:09.179 like go back and kind of like deduce
00:26:12.000 with sine waves generated this more
00:26:14.400 complex waveform and if you're doing
00:26:16.140 anything with like pattern recognition
00:26:17.700 in in graphs or in Matrix data this is
00:26:21.179 like a really cool thing to to dive into
00:26:25.440 and finally we can also generate some
00:26:27.779 that some tambra using your computer so
00:26:30.779 if we take these oscillators again and
00:26:33.539 we take a sine wave and we take a square
00:26:35.640 wave and we mix those two together
00:26:38.520 you end up with like this weird looking
00:26:40.500 uh wave shape which sounds like this
00:26:45.539 so we're uh this is probably also like
00:26:48.059 the ugliest number I've ever heard
00:26:51.539 but it demonstrates that that we can uh
00:26:54.240 that in the end it all comes down to
00:26:56.400 these very simple building blocks
00:26:58.320 and that brings me to the end of my
00:27:01.260 presentation but
00:27:03.840 um I I I can see that you guys are kind
00:27:06.840 of skeptical that this is already
00:27:08.159 possible so so I decided to make a cover
00:27:10.980 of like my favorite song ever using only
00:27:13.799 the samples that I generated today
00:27:16.080 so
00:27:17.159 here it is
00:27:25.220 thank you
00:27:33.960 foreign