Section 7.1
Introduction to the Challenge and String Datatype
Learning Goals
Students will represent a value with a variable (AAP-1.A)
Students will determine the value of a variable as a result of an assignment (AAP-1.B)
Students will represent a list or string using a variable (AAP-1.C)
Students will describe the function of strings
Students will be able to identify the string data type in code
Students will apply strings to the makeBeat() function in Earsketch
AAP-1.A.1: A variable is an abstraction inside a program that can hold a value. Each variable has associated data storage that represents one value at a time, but that value can be a list or other collection that in turn contains multiple values.
AAP-1.A.2: Using meaningful variable names helps with the readability of program code and understanding of what values are represented by the variables.
AAP-1.A.3: Some programming languages provide types to represent data, which are referenced using variables. These types include numbers, Booleans, lists, and strings.
AAP-1.A.4: Some values are better suited to representation using one type of datum rather than another.
AAP-1.B.1: The assignment operator allows a program to change the value represented by a variable.
AAP-1.B.2: The exam reference sheet provides the “←” operator to use for assignment. For example, a ← expression evaluates expression and then assigns the result to the variable a.
AAP-1.B.3: The value stored in a variable will be the most recent value assigned. For example:
a ← 1
b ← a
a ← 2
display(b)
still displays 1.
AAP-1.C.4: A string is an ordered sequence of characters .
DAT-1.A.1: Data values can be stored in variables, lists of items, or standalone constants and can be passed as input to (or output from) procedures.
Objectives and Description
First, the teacher will introduce students to the challenge for this unit by playing the following video and having a discussion about alternative approaches to making music.
The objective of this lesson is to introduce students to the idea of using strings through the use of makeBeat() in Earsketch. By breaking down how makeBeat() interprets strings, students will gain understanding of how the string data type is structured – a sequence of characters being represented as a single data type. Additionally, students will form simple rhythms in Earsketch as a first step in giving them more powerful tools that increase the control over the songs they create in Earsketch.
Activities
Activity 7.1.0 (40 minutes)
Introduce students to variables, data types, and the EarSketch API.
On Slides 4 and 5 have a discussion about the value of a variable is the result of an assignment. Students should be able to explain how this works.
On Slides 5 and 6, using the EarSketch code, demonstrate how data values can be stored In variables, lists of items, or standalone constants and can be passed as input to (or output from) procedures.
Remind students that the AP Exam will use pseudocode and will use an arrow as the operator to assign variables. Take a moment to have students review the AP CSP Exam Reference Sheet. Review the first example at the top of page 3.
On Slide 9, students will create a script using three variables.
Activity 7.1.1 (10 minutes)
Play the following video: Music Sequencer Table
Facilitate a classroom discussion about alternative approaches to making music
How does the table work?
What similarities are there between the table and how music is traditionally played/written?
music can be seen as arrangements of sequences of notes over time
Talk about EarSketch and the DAW
the EarSketch arranges sounds to be played at specific times based on the code we write
Activity 7.1.2 (55 minutes)
Use the provided String Data Type slides and Ableton Step Sequencer website with the described explanation below. The example EarSketch code in the slides is provided as well.
Introduce the step sequencer and break down the grid [Slide 5]
Each square represents 1/16th note. In total, these 16 beats make up 1 measure, which is then looped
Each row corresponds to a particular part of the virtual drum kit. When a square is highlighted, the part of the drum kit for that row will play accordingly.
Have students create their own 1 measure drum beat. Encourage them to make it as simple or complex as they like. (~5 mins)
Walk through the process of creating strings based on these drum parts. [Slide 6]
Pick a row in the sequencer, such as the closed hi-hat, to focus on.
The strings created for use in makeBeat() will consist of 16 characters in sequence, either 0, - or +, surrounded by quotation marks.
Each string should be assigned to its own variable. Use descriptive/memorable names for variables.
Blank squares will correspond to a - and highlighted squares will correspond to a 0 .
For example, the sequence at the bottom of this section for closed hi-hat will result in the string “0-0-0-0-0-0-0-0-”
Use makeBeat() to play the drum beat [Slide 7]
makeBeat() takes 4 inputs: sound, track number, start measure, string
Each string needs its own makeBeat()
To play the closed hi-hat string we created on track 1 starting at measure 1, the code would look something like this
makeBeat(hihat, 1, 1, hihatBeat)
“hihat” is a variable that we assigned a hihat sound to
“hihatBeat” is the variable that we assigned the beat string to
Students should now create strings for their 3- or 4-part drum beats that they made using the sequencer then use makeBeat() to play them [Slide 9]
Remind students that each string should be assigned to its own variable with a descriptive name.
Resources
7.1.0: Introduce variables, data types, and the EarSketch API (Student and Teacher Resource)
7.1.0: AP CSP Exam and Reference Sheet (Student and Teacher Resource)
7.1.2: String Data Type Slide Deck (Student and Teacher Resource)
7.1.2: Ableton Step Sequencer (Student and Teacher Resource)
7.1.2: Example EarSketch code (Student and Teacher Resource)