Algorithmic Composition: A Gentle Introduction to Music Composition Using Common LISP and Common Music
Skip other details (including permanent urls, DOI, citation information) :This work is protected by copyright and may be linked to without seeking permission. Permission must be received for subsequent distribution in print or electronically. Please contact : [email protected] for more information.
For more information, read Michigan Publishing's access and usage policy.
Chapter 4: Object Oriented Programming and Common Music
Common LISP has been extended to include the Common LISP Object System (CLOS). CLOS is a set of tools used to facilitate object-oriented programming in Common LISP. Object-oriented programming is a style of programming where problems are solved through the development and relationship among objects.
In Chapter 3, we use the term object to refer to an instance of a data type. The number 4, the symbol f-major and the list '(A B (C D)) are all instances of Common LISP data types. In CLOS, we extend our definition of an object to be an instance of a data type that may have certain attributes. In addition, objects may contain methods that are procedures that describe how the object relates to other objects.
Objects may be grouped into classes . The grouping of objects into classes is dependent on the relationship among the objects.
4.1 Object Hierarchy
Many things we encounter in everyday life are organized hierarchically. Consider traditional musical instruments . We can group musical instruments into three sections: strings , winds , and percussion . The winds section is comprised of woodwinds and brass . The wind instruments are grouped as a family because each member of the family produces sound by blowing air through a tube. Woodwind instruments include the clarinet, flute and oboe. Brass instruments include the trumpet, French horn, and tuba. Wind instruments may be described as a class of musical instruments.
In an object-oriented model, the relationship between objects in the hierarchy is described as either superclass or subclass . A superclass is an object one-level higher in the hierarchy than an object and a subclass is an object one-level below. Notice in Figure 4.1, instruments is a superclass of winds and trumpet , horn , and tuba are subclasses of brass . The connection between a superclass and its subclass is called an inheritance link . The subclass inherits attributes, referred to as slots , from its superclass.
Figure 4.1.1
We can further refine our instruments hierarchy by adding particular objects referred to as instances of an object. For example, if I play tuba in an ensemble, I could refer to my tuba as the object my-tuba . my-tuba is a particular instance of the class tuba . my-tuba has all of the attributes of the class tuba that it inherits from the brass superclass.
The way I perform my-tuba is different from the way someone plays a trumpet . Even though my-tuba and trumpet inherit from brass , there are differences in the method of performance.
4.2 Common Music Object System
Common Music uses an object class called container to store musical data.container is at the top of the class hierarchy. Subclasses of container are thread, merge, and algorithm. Figure 4.2.1 is graphically describes the relationship among container, thread, merge, and algorithm. Containers have slots for start and end time. Without a specified start, a container begins playing immediately or at time 0. Without a specified end or length, a container could theoretically play forever.
Figure 4.2.1
Figure 4.2.2
Another class in Common Music is called element. Element has rest and note as its subclasses. A rest, symbolized by the letter r, increments time but is silent. There are several subclasses of note depending on the output syntax. For example, the midi-note class has slots that correlate to the MIDI 1.0 Specification.
Figure 4.2.3
Slot Name | Relationship to MIDI 1.0 Specification | Default Value |
note | MIDI keynumber in the range 0-127 | None. Slot must be assigned. |
amplitude | MIDI note-on velocity in the range 0-1.0 (MIDI note-on velocity / 127) | Default is .5 or note-on velocity of 64. |
channel | MIDI channel in the range 0-15 | Default is MIDI channel 0. |
duration | The time between a note-on and its note-off | Defaults to same as value assigned to the rhythm slot. |
rhythm | The time between a note-on and a subsequent note-on | None. Slot must be assigned. |
Instances of a container may be positioned in the hierarchy. The highest level of the Common Music object hierarchy is called Top-Level. By positioning instances of containers in the hierarchy, the composer may create musical structure on several levels.
4.3 Stella: Common Music's Command Interpreter
Stella is Common Music's command interpreter. Stella serves as a common interface for all ports of Common Music. When you type a command to Stella, you are issuing a command to Common Music. The relationship between Stella and Common Music is similar to the relationship between the Macintosh OS and the Finder. For example, to communicate with the Macintosh OS, you issue commands (albeit through a graphic interface) to the Finder.
To start Stella from Common Music, type (stella) at the ? prompt . If your port of Common Music has a graphic user interface, from the Common Music menu, select Stella. The prompt changes from the ? of the interpreter to Stella's Top Level prompt:
Stella [Top-Level]:
Our next step is to create an instance of a container. There are two ways to create an instance of a container. The first way is to use the command new <container-class> at the Stella prompt where <container-class> corresponds to the type of container you would like to create.
<cr> means press enter or carriage return.
Common Music creates an instance of a generator and prompts you for its name and position within the container hierarchy. In this case, Common Music assigns a default name of Generator-10 at a position one-level below the Top-Level. You may override the default container name assigned by Common Music by typing your generator name at the "Name for new Generator:" prompt. You may view the container in the hierarchy by using the Common Music command list. The Stella prompt indicates we are at Top-Level looking down one level in the hierarchy to our newly created generator named Generator-10.
Example 4.3.2
Stella [Top-Level]:listFigure 4.3.1 graphically describes the relationship between the Top-Level and Generator-10.
To change our focus object from the Top-Level to Generator-10, use the Common Music command go <container-name> or go <container-number> where <container-name> or <container-number> corresponds to the container that you would like to make the focus object. The focus object is your current position in the hierarchy. Example 4.3.3 references the container by name. Alternatively, go 1 could have been entered. Referencing a container by name is called absolute referencing . Referencing a container by its position in the hierarchy is called relative referencing .
Example 4.3.3
Stella [Top-Level]: go generator-10Focus: Generator-10
Objects: 0
Start: unset
Notice that by going to a specific container in the hierarchy, we are given some information about that container. In this case, Generator-10 is a container of type generator and is of normal status (more on this later). Generator-10 contains 0 objects and its start time has been unset.
With the container as our current focus object, we can create instances of new notes to place inside the container using the Common Music command new midi-note.
Example 4.3.4
Common Music prompts you how many midi-notes you'd like to create. In this case, we choose 1. The default <cr> is used to create as many midi-notes as are required by the algorithm that makes the slot assignments. The slots are assigned in slot-name - value space-delimited pairs. Common Music does not care what order you enter the slot-name and value pairs. If the amplitude, channel, and duration slots are not assigned, they will be assigned a default value as described in Figure 4.2.3. With Generator-10 as our focus object, we can look down the hierarchy and see one midi-note.
Figure 4.3.2
Stella [Generator-10]: listNotice that we see a midi-note with slots assignments for note (or keynumber) that has a value of 60, rhythm that has a value of .5, duration that has a value of .25, amplitude (or velocity) that has a value of .75, and channel that has a value of 0.
You probably would like to listen to your midi-note. First, make sure you are routing MIDI data to your MIDI interface. At the Stella prompt, type the Common Music command open midi. If your port of Common Music has a graphic user interface, you have a menu option to open MIDI.
Example 4.3.4
To listen to the midi-note, use the Common Music command mix.
Common Music assumes you want to mix the container that is the current focus object. Notice that you can set the start time if you want to delay playback by a specified number of seconds.
Return to the Top-Level by issuing the Common Music command go up or alternatively, go Top-Level.
Example 4.3.6
Next, list the containers at Top-Level. Notice the status of Generator-10 has changed from normal to frozen.
Example 4.3.7
Use the Common Music command go followed by the container name to change the focus object back to Generator-10.
Example 4.3.8
Once again, we see that Generator-10 has a frozen status. What does it mean when a generator is frozen? A frozen generator is a generator whose slot values remain fixed until the generator is unfrozen and re-computed. To unfreeze a generator, use the Common Music command unfreeze followed by the object name or its integer identifier. Since we only have one note and we did not describe the slots algorithmically, the frozen state of our generator is of little concern to us.
With Generator-10 as the focus object, create a new thread using the Common Music command new thread.
Example 4.3.9
Use the Common Music command list to see the new thread in the hierarchy.
Example 4.3.10
Create a new midi-note and assign its slots.
Use list to see the contents of the thread.
Example 4.3.13
Figure 4.3.3 graphically depicts the arrangement of objects in our hierarchy.
Now that we know how to add objects to the hierarchy, we should learn how to remove objects from the hierarchy. Stella employs a two-step process to remove objects. First, items are marked for deletion using the Common Music command delete (or del ) followed by the object name or its integer identifier. A deleted item is not played by the mix command. To truly eliminate an object from the hierarchy, the deleted object must be expunged using the Common Music command expunge (or exp ). Like the delete command, expunge must be followed by the object name or its integer identifier.
Example 4.3.14
Stella [Thread-12]: exp 1
4.4 Common Music's Global Variables
Global variables are used to assign values to variables that operate on a systemic level. By convention, global variable names are enclosed in asterisks. For example, the Common Music global variable *standard-tempo* sets a global metronome marking measured in beats per minute (bpm). The default value of *standard-tempo* is 60 bpm. You may query the value of a global variable in the Stella interpreter by preceding the variable name with a comma. The comma tells Stella you are not issuing a Common Music command.
Example 4.4.1
Stella [Top-Level]: ,*standard-tempo*
60.0
You may change the value of a global variable by using the Common LISP macro function SETF. The template for SETF is:(SETF VARIABLE-NAME VALUE)
You may use SETF to alter the value of *standard-tempo*. For example, to double the value of *standard-tempo*, type (SETF *STANDARD-TEMPO* (* 2 *STANDARD-TEMPO*)). Common LISP evaluates the innermost set of parenthesis first and the current value of *standard-tempo* is multiplied by 2. The product is then re-assigned to *standard-tempo*.
Example 4.4.2
Notice that the SETF is enclosed in parentheses. We use parentheses because SETF is a Common LISP macro function instead of a Stella command.
If we use relative rhythms and durations such as eighth note, quarter note, half note, etc., absolute duration is calculated based on the relative duration and the value of *standard-tempo*. For example, given a time signature of 4/4 with a tempo of 60 bpm, the absolute duration of a quarter note is 1/60 of a minute or one second. Figure 4.4.1 shows Common Music's ordinal and symbolic representation of relative note values.
Figure 4.4.1
Note Value | Ordinal Representation | Symbolic Representation |
sixty-fourth | 64th | |
thirty-second | 32nd | |
sixteenth | 16th | s |
eighth | 8th | e |
quarter | 4th | q |
half | 2nd | h |
whole | 1st | w |
To indicate a dotted note value, modify the corresponding relative note by placing a dot (.) following the ordinal or symbolic representation. For example, a dotted eighth note would be represented as e. or 8 th . Additionally, the letter "t " preceding an ordinal or symbolic representation indicates that the note value is a triplet.
Another Common Music global variable, *standard-scale*, provides the default scale for note references.
Example 4.4.3
The Common Music global variable *chromatic-scale* represents the equal-tempered chromatic scale. The chromatic scale is defined over a range of ten octaves. References to individual pitches are by symbolic note name (A, B, C, D, E, F, G), accidental (N for natural [optional], S for sharp, or F for flat), and octave designation (00, 0, 1, 2, 3, 4, 5, 6, 7, 8). The Common Music global variable *standard-scale* has a default value of *chromatic-scale*. In this case, the default value of a global variable is the value of another global variable.
4.5 Getting Help in Common Music
On-line help is available in Common Music. Common Music's help facility accesses the Common Music Dictionary . To find some of the help topics available, type the Common Music command ? at the Stella command interpreter. Common Music displays the available help topics.
Example 4.5.2
Stella [Top-Level]: ?
You may get help on a particular subject by using the Common Music command help followed by the help topic.
Stella [Top-Level]: help mix
When you type help followed by a help topic, Common Music may be configured to launch a HTML viewer and open the requested page in the Common Music Dictionary .
The Macintosh port of Common Music offers a graphic interface to on-line documentation.
Example 4.5.3
Stella [Top-Level]: help
The Common Music help command opens the Help Window as seen in Figure 4.5.1.
Figure 4.5.1: The Help Window
You may type in a help topic at the cursor to get more information on that topic. In the case of Figure 4.5.2, we ask for help on *standard-tempo*.
Figure 4.5.2: Getting help on *standard-tempo*
Common Music is configured to use a HTML viewer that opens the requested page in the Common Music Dictionary .