Lab: Music in Scamper

Assigned
Friday, 22 September 2023
Summary
In this laboratory, you will explore the scamper music library and practice your list transformation skills.

Introduction: The Scamper Music Library

So far, we’ve looked at a variety of primitive types—integral and floating-point numbers, characters and strings, and booleans. How can we combine these to form more interesting types? This will be the subject of the remainder of the course, but you have seen at least one example of such an aggregate datatype before: images. For example, consider a rectangle:

(import image)
(rectangle 100 100 "solid" "aqua")

We can think of a rectangle as a datatype made up of four primitive types: two numbers and two strings.

For the remainder of this mini-project, we’ll preview another datatype that will become a major focus of this course: musical compositions. The music library provides a number of functions for creating and manipulating musical compositions. In this part, we’ll introduce the basic functionality of the library and build a simple song. In the final part of the mini-project, you’ll build some functions that you can use to make the song more interesting.

(By the way, don’t worry if you don’t know anything about music. We’ll explain everything along the way!)

The music Library

The most important function that the music library provides is the note function which creates a composition consisting of one note.

(import music)
(note 60 qn)

The note function takes two arguments:

  • A number corresponding to the MIDI note value of the note to be played. MIDI, short for Musical Instrument Digital Interface, is a standard for allowing digital instruments to interface with computers. The value 60 here corresponds to middle C on the keyboard.

  • A duration value that will be the duration of the note to be played. We express durations in terms of ratios of notes. qn is a variable of type duration? that is a quarter note, i.e., the ratio \(\frac{1}{4}\). (A quarter of what? We’ll discuss that in a future mini-project.)

Taken together, (note 60 qn) is a musical composition consisting of a single note that is a middle C played for the length of a quarter note. You can try out the note in the output window above!

With images, shapes like rectangle and circle can be combined to form larger images with functions like above, beside, and overlay. With compositions, we have two options for creating smaller compositions from larger ones:

  • We can play compositions sequentially, i.e., one after the other, with the seq function.
  • We can play compositions in parallel, with the par function.

For example, a B♭ major chord consists of three notes: B♭, D, and F. These correspond to MIDI notes 58, 62, and 65, respectively. With this information, we can play these three notes in sequence or parallel, with seq and par, respectively:

(import music)
(seq (note 58 qn)
     (note 62 qn)
     (note 65 qn))

(par (note 58 qn)
     (note 62 qn)
     (note 65 qn))

Surprisingly, there’s not much left to the music library! With just three functions—note, par, and seq—we can write and explore music in our Scamper programs!

The Lab: MIDI, Scales, and Chords

Download the code from

and follow the instructions.

When you are done, make sure to save the file as text and then upload the completed lab to Gradescope.

Acknowledgements

The music intro for this lab is adapted from a mini-project from fall 2022.