Make Music with Code using EarSketch

This post for the Naas-Sallins CoderDojo is about making music by writing code using Earsketch. There are lots of fantastic instructions on the website including a section for teachers. I strongly suggest that you have a look at the documentation but sometimes it’s fun just to jump straight in and see what we can make.

Earsketch Online supports Chrome, Firefox and Safari so using one of those browsers lets get started by clicking here.

The Earsketch window has 4 main areas and looks like this.

EarSketch

They are

  1. Sound selection: Here you can sample the different sounds available for making music. Expand each list and click on a play icon to hear what it sounds like.
  2. Message Console: Error messages and other information is displayed here.
  3. Sound Window: A graphical representation of your music is shown in this area.
  4. Code Window: This is where you type your python (or javascript) code.

So lets jump in and make some Noise!

Lets start with an intro, in the code window after setTempo(120) type

MyIntro =

then go to the sound selection area and expand by clicking RICHARDDEVINE – HOUSE_120_BPM – HOUSEFXWHOOSH click on the play icon to hear the sound and click on the clipboard icon to copy this sounds name into your code

EarSketch1

It is a good idea to define each sound type that you intend to use at the top of your code so that if you change your mind about it you only have to change your code in one place.

Now that we have defined our intro lets figure out how to play it.

We can do this by using the fitMedia command. This takes the form

fitMedia(sound, track, start playing, stop playing) The start and stop time are defined in measures. A measure of music is a block of time defined by a specified number of beats. In the EarSketch environment, a measure is always defined by four beats.

In our case we want or code to be


from earsketch import * 

init()
setTempo(120) 

MyIntro = HOUSE_SFX_WHOOSH_001

fitMedia(MyIntro, 0, 1, 3)

finish()

To see if this works click on the run button and if everything goes ok click on the play button in the sound window. Any errors will be shown in the console.

EarSketch2

Now we can build up a piece of music using the same steps as above. Lets define our main beat as MyBeat = HOUSE_MAIN_BEAT_008 so our code is now


from earsketch import *

init()
setTempo(120)

MyIntro = HOUSE_SFX_WHOOSH_001
MyBeat = HOUSE_MAIN_BEAT_008

fitMedia(MyIntro, 0, 1, 3)
fitMedia(MyBeat, 0, 3, 25)

finish()

When we run this code we can see that the sound window expands to show our longer tune because our beat starts at 3 when the intro ends, and finishes on measure 25. If we want more then one sound to be played at a time we need to use a different ‘track’. For example if we add some bass and set it to use track 1 rather than 0 with our fit media command


from earsketch import *

init()
setTempo(120)

MyIntro = HOUSE_SFX_WHOOSH_001
MyBeat = HOUSE_MAIN_BEAT_008
MyBass = HOUSE_DEEP_BASS_001

fitMedia(MyIntro, 0, 1, 3)
fitMedia(MyBeat, 0, 3, 25)
fitMedia(MyBass, 1, 2.5, 25)

finish()

We can see that when we press run we get a second track shown in the sound window

EarSketch3

Next we are going to add a lead, but we are going to use two different sounds. It’s fun messing around to see what fits together but in this case I have changed the code to define MyLead_1 & MyLead_2. You can see from our fitMedia commands that I switch to MyLead_2 for 10 measures and switch back at measure 17 and we run this on track 2.


from earsketch import * 

init()
setTempo(120) 

MyIntro = HOUSE_SFX_WHOOSH_001
MyBeat = HOUSE_MAIN_BEAT_008
MyBass = HOUSE_DEEP_BASS_001
MyLead_1 = HOUSE_DEEP_ARPLEAD_001
MyLead_2 = HOUSE_DEEP_CRYSTALCHORD_001

fitMedia(MyIntro, 0, 1, 3)
fitMedia(MyBeat, 0, 3, 25)
fitMedia(MyBass, 1, 2.5, 25)
fitMedia(MyLead_1, 2, 3, 10)
fitMedia(MyLead_2, 2, 10, 17)
fitMedia(MyLead_1, 2, 17, 25)

finish()

Next I want add a custom user sound. You can record your own voice, or like me you can use a text 2 voice service. The audio needs to be in wav format so you may need to convert it using an online media converter or a windows program like this. You will need to be logged in to Earsketch to upload your file, when you are click on upload new sounds at the top of the sound selection window and enter the details into the dialog box. In this case I decided to use my coderdojo sound with the makeBeat command. There are plenty of details in the documents but to be honest I copied an example and then messed around with it. I use a for loop that goes from 11 to 16 in steps of 2 to play this sound at those points. The beat = “0++00-00-0+++0+0++++++” string controls how the sound is played.

The final code is as below


from earsketch import *

init()
setTempo(120)
MyIntro = HOUSE_SFX_WHOOSH_001
MyBeat = HOUSE_MAIN_BEAT_008
MyBass = HOUSE_DEEP_BASS_001
MyLead_1 = HOUSE_DEEP_ARPLEAD_001
MyLead_2 = HOUSE_DEEP_CRYSTALCHORD_001

MySound1 = DANGERMSE_CODER_DOJO_01
beat = "0++00-00-0+++0+0++++++"

# using iteration instead of repeating similar lines of code
for measure in range(11, 16, 2):
    makeBeat(MySound1, 3, measure, beat)

fitMedia(MyIntro, 0, 1, 3)
fitMedia(MyBeat, 0, 3, 25)
fitMedia(MyBass, 1, 2.5, 25)
fitMedia(MyLead_1, 2, 3, 10)
fitMedia(MyLead_2, 2, 10, 17)
fitMedia(MyLead_1, 2, 17, 25)

finish()

You can download an mp3 of your tune by selecting to do so from the options button above the code window.

The Tune for above is here

 

Finally I would again suggest that you should have a look at the Earsketch Website which really has great quality instructions.

 

Advertisements