The 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.
This week, we will look at another datatype aligned with the audio theme 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.
(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
60here 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.
qnis a variable of typeduration?that is a quarter note, i.e., the ratio \( \frac{1}{4} \). (A quarter of what? We'll discuss that in the lab!)
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
seqfunction. - We can play compositions in parallel, with the
parfunction.
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: Music in Scamper
Download the code from
and follow the instructions.
When you are done, make sure to save the file and then upload the completed lab to Gradescope.