Intro to Python Programming

Introduction to Computer Programming (Python)

Hello, and welcome to the course page for the Introduction to Computer Programming course, focusing on Python. This course will be using the EarSketch software to teach Python using music composition and remixing. If you have no prior experience in programming or music, don’t worry. Both will be helpful, but are not necessary. I will be updating this site with content and resources for each day’s lesson, so check back regularly to review what we did each day, or look ahead for the next day.

 

If you have any questions, please feel free to email me at michael@ilabliberia.org.

 

Thank you,

Michael Madaio

 

 

Useful Links:

 

 

Day 10 (6/25/14)

 

This is our last day to work! Remember that Friday is our Course Showcase, where you will be playing your songs for an audience. You should still arrive before 4:00, and we will award you the certificates of completion for the course at that time. Then, if you have friends or family coming, tell them to get here before 5:00, and we will have the showcase from 5:00-6:30.

 

 

Today, all you have to do is finish your Final Song, and upload it to the Social Media site (so you can share it with friends). You can also download the .WAV file to listen to on your own, if you’d like.

 

In order to determine if you receive a Certificate of Participation or a Certificate of Completion (or Outstanding Achievement), we will be grading your Final Song, to see if it met the criteria below, as well as grading a short test that we will be administering at the end of class today.

 

Here are the requirements again:

 

  • Must be at least 32 measures, and if it is more than 32, it must be in multiples of 4. (i.e.: 32, 36, 40, 44, 48, etc)
  • Must use at least 10 different variables (at least 6 melodic and 4 percussive. Please ask if you aren’t sure)
  • Must use the fitMedia and makeBeat functions to place those variables in your song.
  • Must use at least 5 different “for loops” with at least 1 using 3 parameters for the range() function, to “step” upwards.
  • Must use at least 3 different if/else conditionals within those “for loops” to place tracks.
  • Must use at least 1 modulo in those if/else conditionals to place a track on every other (even or odd) measure.
  • For extra credit, may use one setEffect() function.
At 4:45 today, we will stop working and post our songs online, then at 5:00 we will take our final test for the course. (We’re ending at 5:30 today, since I’m giving a Guest Lecture at 6:00 on “Coding as a Creative Practice” – I’ll be talking about EarSketch, so you might be interested in staying to check it out!)

 

 

Before I give you the link to the test, please take this post-course survey on your experience in the class. The link to the test is at the end of the survey. Thanks!

 

 

 

Day 9 (6/23/14)

Welcome to our last week of the course! Remember, Friday night from 5:00-6:30 will be our showcase for this course and the Digital Video Productions course. Tell your family and friends to come!

 

We will begin today by playing the songs we created on Friday, and giving each other feedback on each other’s code. Useful feedback is:

  • Constructive (not destructive)
  • Specific (not general)
  • Useful
  • Ask questions about choices they made
Then, we will begin our 3rd and Final Song. This will be the one we will play on Friday.
As our final song, it will be more complex, and hopefully, will start to become interesting to listen to. At this point, we are not learning any more new concepts, only learning how to better use the ones we have. If you want (and I hope you do), please continue to work through the EarSketch curriculum on the website after this course is over. There are a lot more advanced programming concepts explained in detail on there, all using EarSketch to teach.

For your Final Project, you will be making a piece of music that incorporates all of the skills so far.

 

It must have:

 

  • Must be at least 32 measures, and if it is more than 32, it must be in multiples of 4. (i.e.: 32, 36, 40, 44, 48, etc)
  • Must use at least 10 different variables (at least 6 melodic and 4 percussive. Please ask if you aren’t sure)
  • Must use the fitMedia and makeBeat functions to place those variables in your song.
  • Must use at least 5 different “for loops” with at least 1 using 3 parameters for the range() function, to “step” upwards.
  • Must use at least 3 different if/else conditionals within those “for loops” to place tracks.
  • Must use at least 1 modulo in those if/else conditionals to place a track on every other (even or odd) measure.
  • For extra credit, may use one setEffect() function.
________________________________
Optional:
If you are interested in pushing your skills further, see below on how to define, or create your own functions.
This is a good way to make full-length songs, using a traditional pop music structure.
Each of these will be one “phrase” of music, and will be either 4, or 8 measures long. Typically, you see music structured as ABAB C ABAB C, or some variation of that. (Most popular music uses the ABAB C BB form, as seen below.
You might also call them Verse1, Verse2, Chorus, with an Intro, and Outro, and maybe a Bridge in the middle.
Intro
Verse1
Chorus
Verse2
Chorus
Bridge
Chorus
Chorus
Outro
To make constructing these easier, we’re going to define our own functions (rather than using only the built-in EarSketch ones). We will be using the command def to do this.
Let’s look at the following example:
2 Music with A and B sections
3 '''
4 from earsketch import *
5
6 # initialize workstation
7 init()
8 setTempo(120)
9
10 # Create an A section between measures 1 and 5
11 fitMedia(Y01_GUITAR_1, 115# leadguitar
12 fitMedia(Y01_DRUMS_1, 215# drums
13 # B section between measures 5 and 7
14  fitMedia(Y01_OPEN_HI_HATS_1, 157# drums
15 fitMedia(Y01_WAH_GUITAR_3, 257# secondguitar
16 fitMedia(Y01_CRASH_1, 356# cymbal crash
17
18 # Then back to section A at measure 7
19 fitMedia(Y01_GUITAR_1, 1711# guitar
20 fitMedia(Y01_DRUMS_1, 2711# drums
BUT, an easier way to do this, (especially for longer songs, with more repeated sections of code), is to define a function that simply creates the A section each time you call it.

 

 

See below:

leadGuitar = Y01_GUITAR_1

secondGuitar = Y01_WAH_GUITAR_3

drums = Y01_DRUMS_1

cymbalCrash = Y01_CRASH_1

 

 

# A section
11 def sectionA(leadGuitar, drums, startMeasure, endMeasure):
12     # create an A section
13     fitMedia(leadGuitar, 1, startMeasure, endMeasure) # lead
14     fitMedia(drums, 2, startMeasure, endMeasure) # drums
15    
16 # B section
17 def sectionB(secondGuitar, drums, cymbalCrash, startMeasure, endMeasure):
18     fitMedia(drums, 1, startMeasure, endMeasure)
26     fitMedia(secondGuitar, 2, startMeasure, endMeasure)
27     fitMedia(cymbalCrash, 3, startMeasure, startMeasure+1)
28
29 # set up an ABA musical form through function calls
30 sectionA(leadGuitar, drums, 15)
31 sectionB(secondGuitar, drums, cymbalCrash, 57)
32 sectionA(leadGuitar, drums, 711)

 

Try calling sectionA and sectionB 4 more times!

 

 

 

 

 

 

Day 8 (6/20/14)

 

Today we will be working on and finishing our 2nd song, the ones we started on Wednesday. For this, we will mainly be using for loops and if/else conditionals. 

 

Review:

If you have not yet, already, choose 2 different versions of the same melodic instrument sample (drums and percussive sounds are not melodic. Keep these to things like guitars, bass guitars, pianos, synths, strings, organs, horns, brass, etc). Also, be sure that these are multiple measure long looped samples, from the Young Guru or Richard Devine folders not the single beat samples from the MakeBeat Folder.

 

Once you have those 2 different versions of the same instrument, assigned to variables (perhaps guitar1 and guitar2, or piano and newPiano, or something), then create a for loop for a 32 measure long piece (with range(1,33)). Be sure to include the colon “:” after the range parameters.

 

Then, indented (with the Tab button, not spacebar), inside the for loop, create an if/else conditional to check to see if it is the beginning or end of the song. IF it is the beginning (measures 1-17), then fitMedia your first sample into the song. ELSE, fitMedia the 2nd sample into it.

 

for measure in range(1,33):

[indent here] if measure <17:

[indent 1 more Tab over here] fitMedia(guitar, 1, measure, measure+1)

[indent a single tab, to align with the IF] else:

[indent over 2 tabs, to align with the fitMedia above] fitMedia(guitar2, 1, measure, measure+1)

 

 

 

Now, change some of the parameters of the range, or the conditions, or add multiple instruments in either the IF condition or the ELSE condition, with more fitMedia function calls. Also, try adding multiple for loops, with their own if/else conditionals.

 

Try having multiple conditions! (“if measure < 9 or >= 25:”)

 

New Material:

 

What if we wanted to have pianoplay on every odd measure and pianoplay on every even measure? How would we do that?

 

We would use something called a modulo, or “modular division” sign, which finds the remainder of 2 values divided by each other. The symbol for this is %. So, whereas 8 / 4 = 2, if you took 8 % 4, it would = 0, since there is no remainder. 8 % 3 = 2, since the remainder of 8 / 3 is 2.

 

This is a useful way to check to see if a value is even or odd. What would the modulo be for all even numbers? What about all odd numbers?

 

 

If you said modulo = = 0 for all even and modulo != 0 for all odd numbers, you’re correct!

 

Therefore, let’s create a for loop, from 1 to 9, and write an if/else statement inside of it, checking to see if the modulo is 0, at which point, we might want to place one of the samples, perhaps piano1, and if modulo != (not equal) 0,  (or we could just write the else condition), then we would place the second sample, piano2. 

 

for measure in range(1,9):

[indent 1] if measure % 2 == 0:

[indent 2] fitMedia(piano1, 1, measure, measure+1)

[indent 1] else:

[indent 2] fitMedia(piano2, 1, measure, measure+1)

 

 

Now try your own! Use different ranges, add different combinations.

 

You have the rest of class to work on Song 2. It should follow these criteria:

 

  • At least 32 measures (if it’s longer, have it be in multiples of 4 or 8)
  • Use at least 4 for loops
  • Use at least 4 if/else conditions
  • Use at least 1 modulo
  • Use at least 1 effect.
  • You do not need to use makeBeat for this song.

 

 

Day 7 (6/18/14)

 

Today we will add some variety to the songs you’ve created so far!

 

We will be using what is called conditional logic, or, if statements, to control whether or not something will happen. At its most basic level, these statements check to see if a condition is met or not. If it is, the program will do something, if it is not, you can tell it to do something else. See below:

 

checkValue = 1    # This sets a variable that we will check later.

 

if checkValue > 0

fitMedia(HIPHOP_SYNTH_LEAD_001, 1, 1, 5)

 

 

However, this is redundant, since you have declared your own checkValue.

 

What if we used this in conjunction with the for loops?

 

for measure in range(1,33):
    if measure < 17 :
            fitMedia(EIGHT_BIT_ATARI_SYNTH_003, 1, measure, measure+1)
               else:
                       fitMedia(EIGHT_BIT_ATARI_SYNTH_005, 1, measure, measure+1)

 

 

 

 

Day 6 (6/16/14)

 

Halfway through your journey with Python music! Let’s take some time to review what we know so far, then we’ll push onwards.

 

If you haven’t yet, finish your first actual piece of music – see my explanation above.

 

Check yourself:

 

  • Did you use fitMedia() to place melodic instrument samples such as guitar, bassGuitar, synth, piano, strings, horns, etc?
  • Did you use makeBeat() to create a beat out of strings and the instrument samples in the makeBeat folder?
  • Did you use 2 for loops to iterate across multiple makeBeat() (OR fitMedia!) functions in a given range()?
  • -Did one of them use 2 parameters – such as range(1,9), and one use 3 parameters – such as range(1,33,4)?
  • Did you use 2 effects to change the sound of some of your tracks?
  • Was the whole thing 32 measures long?

 

If not…. make it so!

 

If the answer to all of the above is YES, then try the following:

 

Add some variety to the loops you’ve created. If your fitMedia() functions look like the following, then find some DIFFERENT samples, using the same instruments to insert in different sections:

 

fitMedia(guitar, 1, 1, 33) #Playing the same guitar sample for all 32 measures
fitMedia(piano, 2, 1, 33) #Same piano sample for 32 measures
fitMedia(bassGuitar, 3, 1, 33) #Same bassGuitar
fitMedia(strings, 4, 1, 33) #Same strings
TRY THIS:

 

fitMedia(beginningGuitar, 1, 1, 17)
fitMedia(endGuitar, 1, 17, 33) # Plays 2 different guitar samples (be sure to assign them to different CONSTANTS, for the beginning and end of your song

 

Now, try the same thing with the other instruments, or try different combinations of measures (not just 1-17 and 17-33, but maybe 1-9, 9-17, 17-25, 25-33, etc).

 

If you did that, now try having different Beats in different sections of your song, rather than the same beat looping for all 32 measures. Create different strings for the 2 versions of each samples’ beat, then call them in a different for loop’s range.

 

For example: (and BE SURE to indent all of your functions within the loop, even though mine are not appearing that way here on the site)

 

kickBeat1 = “0—0—0–0–0-”
kickBeat2 = “0—–0-0—–0-”
snareBeat1 = “—-0——-0—”
snareBeat2 = “0–0–0—0-0—”
cymBeat = “0000000000000000” # I’m keeping the same cymBeat the whole time

 

for measure in range(1,17): # This will play for the first half of the song
makeBeat(kick, 5, measure, kickBeat1) # I’m putting this on track 5 because I’m assuming you have fitMedia() instruments in the first 4 tracks, right?
makeBeat(snare, 6, measure, snareBeat1)
makeBeat(cym, 7, measure, cymBeat)

 

for measure in range(17, 33): # This will play for the second half of the song
makeBeat(kick, 5, measure, kickBeat2)
makeBeat(snare, 6, measure, snareBeat2)
makeBeat(cym, 7, measure, cymBeat)

 

 

 

Day 5 (6/13/14):

Hello! Today we will create our first “real” song. Open up EarSketch. We will begin by reviewing the use of “for” loops to iterate across multiple makeBeat functions. Note that you can use this for ANY function, including fitMedia. If you haven’t already, play with using the 3rd parameter for the range() function to “step” upwards by that value each time.

Remember, if you only use 2 parameters, then the default step upwards is 1. range(1,9) steps upwards by 1 – 1,2,3,4…etc. Whereas, range(0,10,3) gives you 0,3,6,9.

 

Now, let’s try to use the setEffect() function!

 

As you can see in the API and in the page on “Every Effect Explained”, each Effect takes 4 main parameters:

 

setEffect(trackNumber, EFFECT_NAME, EFFECT_PARAMETER, and effectValue)

 

For instance, the Volume Effect would look like this:

 

setEffect(1, VOLUME, GAIN, 12.0)

 

Some main effects you might want to use are:

 

Volume, Delay, Distortion, Filter, Pan, Pitchshift, Tremolo. Take a look at the “Every Effect Explained” link above to see more about what each of those does, what they sound like, and what their effect parameters are!

 

We will spend the majority of class today writing our first song.

 

  • Use fitMedia() to insert AT LEAST 4 melodic instruments (instruments with a melody that extends for multiple measures – i.e.: not samples from the makeBeat folder).
  • Create a beat to use at various points in your song. (hint: it doesn’t need to be the same beat for all 32 measures)
  • Use at least 2 for loop to loop your makeBeat() functions – 1 with 2 parameters for the range(), and 1 with 3 parameters for the range() function.
  • Use at least 2 effects (perhaps Volume and Pan, or Delay)
  • Make it at least 32 measures long.

 

 

Day 4 (6/11/14)

Today, we will review the fitMedia() and makeBeat() functions, and review how to use strings to create beats. As always, go to earsketch.gatech.edu/earsketch2 to begin.

 

Create a short beat using 3 variables assigned to music samples from the MAKEBEAT folder on the left side of the screen. Be sure to create 3 different beatStrings for those different instruments. Then, call the makeBeat function once for each of those beats to place them on different tracks, but all starting in measure 1.

Remember the parameters that makeBeat takes:

makeBeat(fileName, trackNumber, measureStartNumber, beatString)

 

Example:

 

from earsketch import *

init()

setTempo(120)

 

kick = OS_KICK01
cym = OS_CLOSEDHAT01
clap = OS_CLAP01

 

kickBeat = “0—0—0—0-0-”    # Remember that the beatString should be 16 characters, a mixture of 0’s and -‘s for now.

cymBeat = “0-000-000-000-00”

clapBeat = “—-0——-0—”

 

makeBeat(kick, 1, 1, kickBeat)

makeBeat(cym, 2, 1, cymBeat)

makeBeat(clap, 3, 1, clapBeat)

 

finish()

 

 

 

If you want to extend that for more than one measure, then simply copy the makeBeat() functions and paste them again, changing the 3rd parameter, which is the measure number, to 2 (or 3, or 4, or whatever measure you want it to play during).

 

#Measure 1

makeBeat(kick, 1, 1, kickBeat)

makeBeat(cym, 2, 1, cymBeat)

makeBeat(clap, 3, 1, clapBeat)

 

#Measure 2

makeBeat(kick, 1, 2, kickBeat)

makeBeat(cym, 2, 2, cymBeat)

makeBeat(clap, 3, 2, clapBeat)

 

#Measure 3

makeBeat(kick, 1, 3, kickBeat)

makeBeat(cym, 2, 3, cymBeat)

makeBeat(clap, 3, 3, clapBeat)

 

#Measure 4

 

makeBeat(kick, 1, 4, kickBeat)

makeBeat(cym, 2, 4, cymBeat)

makeBeat(clap, 3, 4, clapBeat)

 

 

 

As you can see, that will quickly get tedious, and as a programmer, we are always looking for ways to automate processes that can be thought of differently, as a loop. Up until now, our programs have run sequentially, or, the program has executed from the first line, straight down to the last line, without any iteration or looping. Now, we will use what is called a for loop to make those makeBeat functions more efficient.

 

“for” is a built in command into Python, as well as in nearly every dynamic programming language. In addition, we will use the built in range() function that is built in to Python, as well. Range() takes either 1, 2, or 3 parameters. If it takes 2 parameters, then it is an inclusive start and exclusive end – range(1,9) iterates from 1 through 8, but does not include 9. If it takes 1 parameter – range(9), it iterates from 0 to 9, including 0. If it takes 3 parameters, it iterates from the first to the 2nd parameter, increasing by the value of the 3rd parameter each time – range(1,10,4) would include 1, 5, and 9. For more information, see this page from the EarSketch website.

 

Generically:

 

for variable in range(start,stop):

do(this)

 

 

Specifically:

 

for number in range(1,9):

print(number)

 

The print function would print out the variable number, which starts by being assigned to the 1st number in the range(1, 9), and then increases until it prints the number 8, then it stops and continues on to the next line in your code.

 

For EarSketch, we will start using for loops to make our makeBeat functions more efficient.

 

After you assign your samples to variables and your beatStrings to other variables, delete all of your makeBeat() functions and type the following code.

 

for measure in range(1,9):

makeBeat(kick, 1, measure, kickBeat)

 

 

Notice that the variables for the fileName, the track number, and the variable for the beatString are still the same as  they were above. We have just added a variable (measure) in place of the 3rd parameter, which used to be a static numerical value. Now it changes every time the code loops through the range, creating the beat on measure 1, 2, 3, 4, 5, 6, 7, and 8.

 

Try changing the end value of the range to see how it affects your song! Or, if you’re feeling adventurous, try adding the 3rd parameter to “step” upwards through the range in larger increments than 1 (which is the default).

 

If you’re feeling doubly adventurous, try adding more makeBeat functions “inside” the for loop (which just means that they are underneath and indented like you saw above, which tells the program that as it iterates through the for loop, it will execute that function each time.

 

For instance:

 

for measure in range(1,9):

makeBeat(kick, 1, measure, kickBeat)

makeBeat(cym, 2, measure, kickBeat)

makeBeat(clap, 3, measure, kickBeat)

 

 

If you’re feeling TRIPLY adventurous, then take a look at the EarSketch website’s explanation of “fills” and experiment!

 

 

 

 

Day 3 (6/9/14)

Hopefully everyone was able to listen to some music this weekend and think through how it might look in terms of the code we learned on Friday. As a reminder, if you haven’t yet taken the survey about how you use iLab, please do so. This will help us immensely.

 

Today, we will review the basic fitMedia() function that we learned on Friday, and how we assign variables to our musical samples and use those variables in the parameters of the fitMedia() function to place our samples in our tracks. Open your script from Friday, or, if you weren’t here, review the material listed above. For the first 15 minutes, I want you to write a piece of music using at LEAST 4 different instruments (a drum track, a guitar and/or bass, a synth or piano, and one other), and have this song last for 32 measures. Try to have each instrument play in multiples of 4 measures (so, if it starts on measure 1, have it last until measure 5, 9, 13, 17, 21, 25, 29, or 33). We will then share them with each other.

 

Today’s concept is called Beats, and it is how you can create your own rhythms for various instruments, though we will use this mainly for drum tracks. Instead of simply inserting the drum sample in its 4 measure entirety, you will be able to choose what rhythm your sample plays. Remember that each measure is sub-divided into 4 beats. In music, we call those quarter notes, because they take up 1/4 of the measure. Well, each of those beats can be further divided into what we call 8th notes (there are 8 of them in a measure, 2 per beat), and 16th notes (16 of them in a measure, 4 in a beat). See here for a more in-depth explanation.

 

For our code, we will represent those rhythms with what we call a string, or a series of characters contained within quotation marks, that the program doesn’t read or interpret as code. Remember that this is one of the main types of values that a variable could be assigned to.

food = “Kidney beans and rice”

presidentOfLiberia = “Ellen Johnson Sirleaf”

mySchool = “iLab Liberia”

 

In this case, we will be writing the counts that our beat will play during, and which counts it will not play for. This will be written in 16th notes (16 per measure, 4 per beat), and we will use the characters “0”, “-“, and “+” to refer to times when the beat plays, does not play, or extends (if the sample lasts for more than 1 beat).

beat = “0-00-00-0+++0+0+”

 

Using strings, it’s possible to make complex beats that would be very tedious to generate using functions like fitMedia() or by hand in Reaper’s graphical interface.

In order to generate this beat in Reaper from the string, we use the function makeBeat(), which takes a sound file, a track number, a measure number, and a string pattern as its parameters. Let’s replace our drums on track 3 with our own custom beat. Replace the entire music section beginning at line 18 with the following code:

18 #music section
19 synth1 = TECHNO_SYNTHPLUCK_001
20 synth2 = TECHNO_SYNTHPLUCK_002
21 introBeat = TECHNO_LOOP_PART_002
22 drums = TECHNO_LOOP_PART_006
23
24 #beats
25 introBeatString1 = "0-------0-------"
26 makeBeat(introBeat, 34, introBeatString1)
27 makeBeat(introBeat, 35, introBeatString1)
28 makeBeat(introBeat, 36, introBeatString1)
29 makeBeat(introBeat, 37, introBeatString1)
30 makeBeat(introBeat, 38, introBeatString1)
31
32 #melody
33 fitMedia(synth1, 112)
34 fitMedia(synth2, 223)
35 fitMedia(synth1, 134)
36 fitMedia(synth2, 245)
37 fitMedia(synth1, 156)
38 fitMedia(synth2, 267)
39 fitMedia(synth1, 178)
40 fitMedia(synth2, 289)
41
42 #drums
43 fitMedia(drums, 459)
44

Line 21 creates an introBeat variable for the beat sound file. Line 25 creates a introBeatString1 variable to hold the beat string. Lines 26-30 call the EarSketch makeBeat() function to add a beat to track 3 (measures 4-8). Because we have five makeBeat() calls, the beat on track 3 will repeat five times. (Soon we will learn an easier, more compact way of doing this.)

 

 

Now, you try!

 

 

 

 

Day 2 (6/6/14)

 

Welcome back to the second day of Python Programming with EarSketch!  Before we start for today, please participate in taking a brief survey for research that I am conducting on the iLab. Click here to begin. If you get to class early, you may begin working through it.

 

We will start by reviewing some of the concepts from Wednesday’s class, then we will start to write our first program! Go to theEarSketch code editor, and log in. Begin to click through some of the sound samples on the left side of the page, and preview which ones you might want to start using for your first project.

 

In your code editor, add your name and the name of your song as a comment, by using the pound sign (#) in front of that line. That will tell the computer to not read that code, but we humans can read it. Then, for organization, add a comment in the main body of the code between the setTempo() function and the finish() function. This will be where we will assign our instrument variables to the sound sample CONSTANTS, and use our fitMedia() function to place them on various tracks in our piece of music.

 

 

# Song Name
# Author

 

from earsketch import *

 

init()
setTempo(120)

 

# Music

 

finish()

 

 

 

Now, in the next line under the # Music comment, we will create our first variable. Type in the name of the first instrument you will use. This will be where we will declare our variable, and assign a value to it.

 

What you type does not matter, as long as it makes sense to you, and it is one single word (or two words in camelCase, like funkyDrums, or rockGuitar). If you want more tips on Python variable naming conventions, check out this w3resource page. (w3 is a GREAT tool for Python programmers, and it has resources for many many other languages as well. I would recommend bookmarking their main page.)

 

Then, type an = sign after the variable name, and put the name of the CONSTANT (sound sample), that you want that variable to “refer to.”

 

Like so:

 

# First Song
# Michael Madaio
from earsketch import *

 

init()
setTempo(120)

 

# Music

drums = Y04_DRUMS_SAMPLE_1

 

finish()

 

 

In programming, variables are assigned values, and those values can be numeric valuesstrings of letters, or other variables.  However, the reason why programs are as flexible and adaptable as they are is that the values that one variable stores can change over time. That is to say, later in the code, that same variable can be used to refer to a different value. This is a tricky concept, and not one that we will use at the moment, but it will be useful to keep in the back of your mind for later.

 

Ok! Now that we’ve declared our variable, we have the “ingredients” that we’ll be using for this code. Next, we need to actually do something with it, and that’s where functions come into the picture.

 

Functions, much like variables, can be defined, or created by users to do basically anything that you want the computer to do. However, for most software systems, they come with a built-in, predefined set of functions, or their API (Application programming interface). This is the reference, or resource documentation that the programmers who created that software give to all of the programmers who use it. For instance, Facebook’s API lets programmers “embed” Facebook functions and interfaces into other programs.

For EarSketch, the main built-in function we will be using is called the “fitMedia()” function.

 

This is the explanation in EarSketch’s API for fitMedia():

 

fitMedia(fileName, trackNumber, startLocation, endLocation)

Adds an audio file to a specified track number.

This function inserts the audio file as many times as possible between the
specified measure number.
If the file is too long to fit in the range it truncates the sound file.

Parameters:
fileName (String)     – Audio file to insert on track, typically this is a constant variable from the Audio Loop Browser
trackNumber (Integer) – Track number to insert media file onto
startLocation (Float) – Location for where the initial soundfile will be placed
endLocation (Float)   – Exclusive location for the end of the soundfile(s) (e.g. File will not occur after this point)

Example:
# The following function inserts audio file on track two, measures 1 to 9 (e.g. File not included on measure 9).
fitMedia(HIPHOP_FUNKBEAT_001, 2, 1, 9)

 

Now, you try!

 

 

# First Song
# Michael Madaio
from earsketch import *

 

init()
setTempo(120)

 

# Music

drums = Y04_DRUMS_SAMPLE_1

fitMedia(drums, 1, 1, 9)

 

finish()

 

 

How do you think you would add a piano to this?

 

By the time you leave today, you will have written your first program, and we will share our songs with each other!

 

 

 

 

Day 1 (6/4/14):

Today, we will discuss what Python is and why we are learning it, then we will discuss EarSketch, and why it makes sense to use music as a way to understand code.

 

First, navigate to the EarSketch website. There is a lot of information on here, so feel free to read the explanations on the website to understand more about what it is and how it works. Basically, we will be writing code that uses musical samples to create songs. If you like, click on the Social Media tab at the top of the EarSketch website, then click on Projects to listen to songs written with Python by other EarSketch students around the world, and look at the code that created those songs.

 

To begin, click on the Download tab at the top, and find the link to EarSketch Online. You can also use this link.

 

 

A program, or script, is a set of instructions given to a computer using words, symbols, and numbers that both humans and computers can understand. Typically, you will write your code in an IDE (integrated development environment), which can then “compile” the code, or, translate it into the 1’s and 0’s that cause the computer to do things.

 

All programs consist of statements, which are instructions that act on objects. These objects can be as simple as a number, a string of letters (words or sentences), or even other instructions, called functions. Think of a recipe: there are ingredients (the objects), and the actions you take on those ingredients, such as chop, mix, boil, etc, which are the functions.

 

 

All music is based on a set of patterns, of repeated sounds that change slightly to create different feelings or effects. In programming, something that changes is called a variable. Something that never changes is called a constant.
Constants are used for elements of your program that remain the same throughout the entire usage of your code. In a typical Python program, constants might be used to represent employee names, or any other value that would not likely change throughout the life of the program. For our purposes, the musical samples that we will be using to create our songs will be our constants. These come from a database that we have created, using sound files from professional music producers that we work with, such as Young Guru(the producer for Jay-Z), and a dub step producer named Richard Devine. In Python programming, constants are sometimes called “immutables,” and are always CAPITALIZED.

e.g.: HIPHOP_FUNKBASS_001

 

Variables are elements of the code that change depending on information you declare, or that the program returns to the variable. In a typical Python program, variables might be used to represent employee salaries, or interest rates, or any other value that might change throughout the life of the program. These are always lower-case, or, if you have two words, they are joined together, with the first letter of the following words capitalized (this is also called camel case, like a camel’s humps). e.g.: camelCase

 

For EarSketch, your variables will be used to represent the CONSTANTS that are the musical samples. For instance, when you want to use a particular sound file for one of your instruments, you will declare that variable before you use it.

e.g.: bass = HIPHOP_FUNKBASS_001

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *