Getting Started with EarSketch
In this chapter you will learn how EarSketch works. You will place sounds into your music and see how to debug your code.
Discover EarSketch
In EarSketch, you will give the computer instructions by writing code. One line of code is one instruction. All the instructions together are called the program (these instructions can also be called an algorithm). Just like following a recipe in a cookbook can lead to cooked meal, executing a program in EarSketch can lead to a song. How does it work? Find out in the video below!
As you become familiar with EarSketch these are the main panels.
-
Content Manager (left): Your scripts and sounds
-
DAW (top-center): The song timeline and "play" button
-
Editor (mid-center): The code editor and "run" button
-
Console (bottom-center): The script output and errors
What is a DAW?
A DAW, or Digital Audio Workstation, is software that produces or edits audio on a computer, be it in a professional studio or in home laptop-based studios.
Some popular DAWs include Ableton Live, FL Studio, Logic Pro, GarageBand, Pro Tools, and Reaper.
EarSketch is DAW that allows you to create music by writing code.
Here is how to make full use of the DAW:
Let’s try running a code example in EarSketch! On the box below, press the blue clipboard icon in the top right corner. This will paste the example code onto a new file in the code editor. No need to understand the code yet, just press the run button and your music will show up in the DAW. You can press the play button to hear it.
# Intro Script: This code adds one sound to the DAW
# Setup Section
from earsketch import *
setTempo(120)
# Music Section
fitMedia(TECHNO_SYNTHPLUCK_001, 1, 1, 9)
// Intro Script: This code adds one sound to the DAW
// Setup Section
setTempo(120);
// Music Section
fitMedia(TECHNO_SYNTHPLUCK_001, 1, 1, 9);
Create your first script!
Let’s see how to create a script from scratch.
-
Create. In the editor tabs, click the white "+" icon.
If this is your first script, click the large blue text "Click here to create a new script!"
Figure 1.2.1: Create a new script, open scripts -
Choose a name and language. Give your new script a name, and choose between Python and JavaScript programming languages.
Figure 1.2.2: The create a new script dialog boxFigure 1.2.2: The create a new script dialog box -
(OPTIONAL) Sign in or create a new account. To save your scripts for future editing and sharing, sign in with an EarSketch account.
Note: Your EarSketch account does not require any personal information or email.
What is a programming language?
Code is written in a programming language. Like a spoken language it has vocabulary and syntax. You need to follow your programming language’s grammar rules as you would with spoken language.
When you press the run button, a compiler translates the Python or JavaScript instructions into machine code which the computer can understand. Then the computer executes the code, which causes your music appear in the DAW.
At the deepest level, computers execute code using binary. This means that all computer code eventually becomes 1
's and 0
's as it is processed by electrical components in the computer.
The fitMedia()
function
Now that you have created your first script, let’s start working on your music!
Watch this video to see how to add an audio clip to your song:
To add a sound to the DAW, we start by typing fitMedia()
and use the following form.
fitMedia(sound, track, start, end)
There are 4 parameters, separated by commas.
-
Sound: the sound constant from the sound browser
-
Track: the track number
-
Start: the starting measure
-
End: the ending measure
Example:
fitMedia(Y18_DRUM_SAMPLES_2, 2, 1, 5)
The statement above will place the sound Y18_DRUM_SAMPLES_2
on track 2
from measures 1
to 5
. A statement tells the computer to carry out an action.
Try adding fitMedia()
to a script.
When you’ve finished adding all your parameters, press run. You will see your sounds visualized in the DAW. Press play to listen to your song.
Note: In JavaScript an optional semicolon ;
can be added to the end of each statement. Use of the semicolon is your personal choice.
The Sound Browser
The sound browser provides a library of over four thousand sounds to use in your music. Contributors include celebrity musicians Alicia Keys, Ciara, Common, Khalid, Pharrell, Richard Devine, and Young Guru.
You can explore the sound browser within the Content Manager (left) by clicking "sounds". Try using different sound constants inside fitMedia()
.
Note: The EarSketch License Agreement covers fair use of stock sounds.
Below is an example script using fitMedia()
. Remember, you can import this script by clicking the blue clipboard icon.
# Using fitMedia(): Adding a sound to the DAW
# Setup
from earsketch import *
setTempo(120)
# Music
fitMedia(Y18_DRUM_SAMPLES_2, 1, 1, 5)
// Using fitMedia(): Adding a sound to the DAW
// Setup
setTempo(120);
// Music
fitMedia(Y18_DRUM_SAMPLES_2, 1, 1, 5);
For an extra challenge, add more fitMedia()
calls to your script like we do below. Notice that we use a different track number for each fitMedia()
call:
# Using fitMedia() 2: Multiple fitMedia() calls on different tracks
# Setup Section
from earsketch import *
setTempo(100)
# Music Section
fitMedia(Y01_DRUMS_1, 1, 1, 9)
fitMedia(Y11_BASS_1, 2, 1, 9)
fitMedia(Y11_GUITAR_1, 3, 1, 9)
// Using fitMedia() 2: Multiple fitMedia() calls on different tracks
// Setup Section
setTempo(100);
// Music Section
fitMedia(Y01_DRUMS_1, 1, 1, 9);
fitMedia(Y11_BASS_1, 2, 1, 9);
fitMedia(Y11_GUITAR_1, 3, 1, 9);
Debug your code
Sometimes programmers make mistakes that cause code to work incorrectly, or not run at all. In programming, coding faults are called errors, or bugs. The process of finding and fixing bugs is called debugging. You can use debugging strategies, using the console.
What are the different types of errors?
-
Syntax errors: Your program does not run because your code breaks the language’s syntax rules (ex: you forgot to close a parenthesis, or you wrote
fitMedia
incorrectly). -
Runtime errors: Your program starts to run but halts due to an error.
-
Logic errors: Your program runs, but it doesn’t do what is expected. You can fix these by looking at the DAW to check if the clips you meant to add were actually added in the right place.
Here are some common errors:
-
Misspelling: Check the spelling when using a function like
fitMedia()
or sound constants. -
Case sensitivity: Most words used in programming are case-sensitive (the computer recognizes the difference between capitalized and uncapitalized letters). Pay attention to lowercase and uppercase letters. For example, write
fitMedia()
and notFitMedia()
orfitmedia()
. The functions in EarSketch use a style called Camel Case: the first word is lower case, and the first letter of subsequent words are capitalized, as inexampleFunctionName()
. -
Parentheses: Forgetting an opening or closing parenthesis where needed will cause a syntax error.
-
Script setup: EarSketch adds some code to a new script automatically, but you might accidentally delete
from earsketch import *
. -
Punctuation: Missing commas or other punctuation errors
-
Misspelling: Check the spelling when using a function like
fitMedia()
or sound constants. -
Case sensitivity: Most words used in programming are case-sensitive (the computer recognizes the difference between capitalized and uncapitalized letters). Pay attention to lowercase and uppercase letters. For example, write
fitMedia()
and notFitMedia()
orfitmedia()
. The functions in EarSketch use a style called Camel Case: the first word is lower case, and the first letter of subsequent words are capitalized, as inexampleFunctionName()
. -
Parentheses: Forgetting an opening or closing parenthesis where needed will cause a syntax error.
-
Punctuation: Missing commas or other punctuation errors
Time to practice! Find the five errors in the following code:
# Finding errors: Five errors below must be fixed
from earsketch import *
setTempo(88
fitMdia(HIPHOP_DUSTYGROOVEPART_001, 1, 1 9)
fitmedia(2, HIPHOP_DUSTYGROOVEPART_003, 1, 9)
// Finding errors: Five errors below must be fixed
setTempo(88;
fitMdia(HIPHOP_DUSTYGROOVEPART_001, 1, 1 9);
fitmedia(2, HIPHOP_DUSTYGROOVEPART_001, 1, 9);
Here is the answer
-
The
setTempo()
function is missing a parentheses -
The first
fitMedia()
is missing ane
-
The first
fitMedia()
is missing a comma between the third and fourth parameters -
The second
fitMedia()
is missing an uppercaseM
-
In the second
fitMedia()
, the order of parameters is not correct: it should be sound clip name then track number
Take a look at Every Error Explained in Detail for a description of different error types and what you can do to prevent them.
Chapter 1 Summary
-
A computer program is a sequence of instructions the computer executes to accomplish a specific task.
-
A script is a nickname for a short program.
-
A DAW, or Digital Audio Workstation, is a type of computer software for recording and editing audio. EarSketch is a DAW that lets you make music with code.
-
To make music, type code into the editor, click "run", and click "play" to listen.
-
Sounds can be found in the Sound Browser. You can add sounds to your code by using the sound constant in a function like
fitMedia()
. -
Programming languages are collections of words and symbols that are understood by the computer.
-
The rules of syntax define how code must be written and how punctuation (
.
,,
) is used. -
Create a new script by clicking the large blue link "Click here to create…" or the "+" icon on the editor tabs.
-
fitMedia()
is a way of adding sounds to the DAW. It has the following four parameters.-
Sound: the sound constant from the sound browser
-
Track: the track number
-
Start: the starting measure
-
End: the ending measure
-
-
Debugging is the process of finding and fixing bugs, or errors, made by the programmer.
-
The console shows information about the state of a program, making it useful for debugging syntax errors.
-
Common programming errors include typos, incorrect cases, missing parentheses, improper script setup, and logic errors.
Questions
Which of the following is NOT a panel in the EarSketch workspace?
-
The effects browser
-
The code editor
-
The DAW
-
The console
How many parameters do you need for your fitMedia()
function?
-
4
-
6
-
2
-
3
One script corresponds to…
-
One EarSketch song
-
One line of code
-
One programming language
-
One programmer
What is a measure?
-
A musical time unit
-
An audio volume unit
-
A line in the DAW
-
A pitch unit
Which of the following is NOT a common type of error found in code?
-
Grammatical Errors
-
Runtime Errors
-
Logic Errors
-
Syntax Errors
Where in the EarSketch workspace can you get information about your bugs?
-
The console
-
The sound browser
-
The script browser
-
The DAW