Terrapin Resources

Terrapin Logo 4 has been discontinued! Our new Terrapin Logo version is much more powerful and modern; we strongly recommend that you update to our latest release of the Logo programming language.

Chapter 5: Music and Noise

Music and noise are not usually considered together. However, in Terrapin Logo, the command that creates a masterful symphony is also used to make a dog bark. This chapter explores the simple, yet powerful, musical capabilities of Terrapin Logo. If you are familiar with musical notations, you can skip the introduction and begin with the Music in Logo section. If you don’t know a G-clef from a G-sharp, then start with An Introduction to Music for a brief lesson. Either way, check out Noise and Other Sounds to find out how to add special effects to your Logo procedures.

If you just want to hear the music that others have written, then try some of the procedures in the Music folder. The Piano procedure lets you “mouse around” with an electronic keyboard. If you create a tune you like, make a list of the notes so you can PLAY it back later.

An Introduction to Music

Music is simply a variety of tones of high and low pitches of short and long duration played at fast and slow tempos in patterns of rhythm that sound pleasant to the ear. All of these things are captured in the written language of music. The pitches are written on a staff of five lines and four spaces. As you move up the staff, the pitches get higher; as you move down the staff, the pitches get lower. There is such a wide range of pitches that one staff is not enough.

The grand staff is two staves connected together at their ends but separated from each other by one line and two spaces (usually very much further apart than the illustration). The line between them is never shown in full; when it is needed, a short ledger line is drawn. The top staff is called the treble clef staff. The symbol on the left is called the treble clef or the G-clef (the curly part is wrapped around the G line). The bottom staff is called the bass clef staff. Its symbol is called the bass clef or the F-clef (the two dots are above and below the F line).

Each line and space of the grand staff is identified by a letter. The line in the middle is called middle C. It’s not only the middle of the grand staff, it also represents the middle C key on a piano keyboard. Piano keys are arranged in a repeating pattern of twelve keys - seven white keys and five black keys. The black keys are arranged in groups of two and three. The C key is always the white key to the left of the group of two black keys.

The seven white keys are labeled with the first seven letters of the alphabet. However, since the group starts with middle C, the pattern continues with D, E, F, G and then begins again with A and ends with B. With more patterns together, you can see how the grand staff and the piano relate to each other. Keys to the right of middle C relate to the lines and spaces going up the treble clef staff. Keys to the left of middle C relate to the lines and spaces going down the bass clef staff.

Of course, you can see that the letters are repeated many times on the grand staff. Each group of eight letters is called an octave. For example, starting with middle C and going up to the C in the third space of the treble clef staff is one octave. The upper C is also the start of another octave that goes beyond the top line. This works well for musicians and composers, but not too well for computers. In Terrapin Logo, an octave is really seven letters. Octave 4, for example, is from middle C through the B on the middle line of the treble clef staff.

Shifting octaves on a piano keyboard is simply a matter of moving to the next-higher or next-lower repeating pattern of keys to raise or lower the pitch of the sound of music. In Logo, it’s simply a matter of changing the octave number, as you’ll see later.

What about the black keys? They actually are two keys in one. For example, the black key between middle C and D is sometimes played in place of C in order to get a slightly higher sound. With this kind of adjustment, the black key is called a sharp - in this case, it would be called C-sharp. On the staff, a sharp symbol (#) would show that a higher-sounding adjustment is needed. When a slightly lower sound is needed, the black key is called a flat - in this case, it would be called D-flat. On the staff, a flat symbol (b) would show that a lower-sounding adjustment is needed.

If a sharp or a flat is meant to be played in place of every occurrence of the same note, then you will see the sharp or flat symbol next to the clef symbol at the beginning of the staff. This is called a key signature. It may not be where you think it should be, but remember that some letters appear in more than one place on the staff. It is usually the upper letter’s line or space that is marked with the sharp or flat symbol. However, sometimes the sharp or flat is meant to be played only one time. In that case, the sharp or flat symbol will appear on the line or the space immediately before the note. The separate note marking is called an accidental. (Not an accident!)

A musical scale is a special set of notes in an octave. There are many different scales but the one you are probably most familiar with is the major scale. It has the cheery sounding syllables you may have heard: Do, Re, Me, Fa, So, La, Ti, Do. You can name the notes in any major scale using these syllables. The C major scale is the simplest to play around with because it uses the notes C, D, E, F, G, A, B and the next-higher C. On a piano keyboard, these notes are the eight consecutive white keys beginning with middle C.

The D major scale starts on D above middle C. If you play just the white keys up to the next D, it will not have the same Do-Re-Mi tune. You need to use two black keys (F-sharp and C-sharp) to make it sound right.

Time is important in music. Time is counted in beats per measure and is indicated on the staff with a time signature. Typical time signatures are three-quarter time and four-quarter time. Another way to say them is three-four time and four-four time. The time signature appears on the staff after the key signature, if there is one. There may not be a time signature at all; in that case, four-four time is assumed.

The upper number of the time signature tells you how many beats there are in each measure. The measures are marked off with a vertical bar through the staff. The words “measure” and “bar” really mean the same thing. When people say “Hum a few bars of that tune,” they really mean “Hum a few measures of that tune.” The beat count sets a basic pattern of rhythm for the music. Three-quarter time is a pattern of counting “One, two, three” in each measure. Four-four time is a pattern of counting “One, two, three, four” in each measure. People often tap their feet to the beat of music; it helps to keep in step with the rhythm.

The lower number of the time signature tells you which type of note gets a full beat. You can think of the lower number as the denominator of the fraction that identifies the note: 4 means quarter note. Although the time signature looks somewhat like a fraction, it is not a fraction; each number has its own distinct meaning. (It doesn’t help that the lower number relates to note fractions.)

Musical notes are a representation of time. They tell you how long to play the pitch represented by the line or space where the note appears on the staff. Musical notes have a relationship to each other that is very much like fractions. There is a wide range of notes, but they are all directly related to the whole note. Unfortunately, there is not a precisely defined duration for the whole note. Some music is played really fast and upbeat while other music is played rather slowly. However, the fractional relationship between notes does not change. A whole note is always equal in time to two half notes; a half note is always equal in time to two quarter notes; and so on. In the following example, each measure shows an equal amount of time.

By using different combinations of notes, the rhythm of the music can change from the steady counting pattern, but the total time of each measure is the same (with few exceptions). For example, in three-four time, three quarter notes could be replaced by six eighth notes but the total time is still a total of three quarter notes. However, the pattern of counting would be faster because there are more notes: “One-and, two-and, three-and.”

A dotted note is equal in time to itself plus half of itself. A dotted whole note is equal to a whole note plus a half note; a dotted half note is equal to a half note plus a quarter note; and so on. In three-four time, for example, a dotted half note is equal to three quarter notes - in other words, that one note is played for the entire time of the measure.

The speed at which a piece of music is played is called the tempo. It’s a measure of the number of quarter notes per minute that should be played. In Logo, the tempo starts out at 120 quarter beats per minute so that a whole note plays for about two seconds. You can easily change it to find the tempo that suits you best. Ready to play?

The PLAY command is all you need to know to make music in Logo. Type:

The input to the PLAY command is a list of special musical commands that control your computer’s synthesizer. The musical commands only work inside the list given to PLAY. The R command resets the synthesizer to its default settings. You probably guessed (correctly!) that the other commands in the example are musical notes. The note commands are the letters A through G, but there is more to a note than just its letter and there is more to music than just notes.

The musical commands are somewhat different than regular Logo commands. Instead of separate inputs, they have suffix modifiers attached after the command - there is no space between the command and the modifier. After a reset command, Logo plays quarter notes on an acoustic grand piano in the octave that contains middle C. You can change the instrument with the I command. It uses a suffix modifier that must be a number. Type:

The trombone is one of eight brass instruments that Logo can play. Look in the online help under PLAY to see the complete list of instruments. There are many instruments to choose from, but what could be better than learning on an acoustic grand piano? It’s the default instrument if no modifier is attached to the I command, as well as Logo’s default instrument. Type:

The default length for notes can be changed with the L command. It uses a suffix modifier that must be a number. The number 1 is for whole notes, 2 is for half notes, 4 is for quarter notes, and so on. With no suffix, L defaults to 1 (whole notes). All of the notes played after an L command are played for the new length. Type:

The L command works fine when there is a long sequence of notes of the same length, but it would be awkward if that was the only way to change note lengths. Individual note lengths can be changed with a prefix modifier that attaches to the front of the command. The note modifiers are only in effect for the duration of the note to which they are attached. The numbers used are the same as those for the L command. Type:

The C plays a half note while D and E play eighth notes because the L8 command from the last example is still in effect. Note length prefixes must be whole numbers. You can not use a value like 2.5, even though that is the length of a dotted half note. Fortunately, Logo knows a little about music and has provided just what you need for dotted notes

  • the dot suffix. Type:

The dot suffix makes the note play one and a half times as long as it would normally play. As with any note modifier, the dot only affects the note to which it is attached and it must be the last modifier of a note command.

The notes you played so far represent the white keys on the piano keyboard. The black keys are played for sharps and flats. Logo knows about them, too. The sharp modifier is the pound sign character (#) which looks very much like a real musical sharp symbol. The flat modifier is the letter b which is as close as you can get to the real flat symbol. You don’t have to make it a lowercase b, but it looks better. Type:

The D# and Eb play the same tone. The R command resets the synthesizer to play quarter notes in octave 4. Octave values raise or lower the pitch of the notes. You can change it with the O command. (That’s the letter O, not the number zero. Type carefully because they are close to each other on your computer keyboard. With syntax highlighting turned on, you’ll see a different color.) Type:

All of the notes following the O command are played in octave 3 now. You can change a note to a different octave by attaching the octave number as a suffix. Type:

The D note plays in octave 4 while C and E play in octave 3 because the O3 command is still in effect. Octave numbers are from 0 to 6, but not every instrument can play notes in every octave. The musical range of a tuba is quite different from a flute. You should adjust the octave to match the instrument you are playing to get the best sound.

You can also make a relative change to the current octave by using the sharp or flat modifier instead of using a specific number. However, it’s not as simple as changing the octave of a note with a modifier. You might need to raise the octave before playing a note and then lower it again before the next note. And, you have to remember that a relative octave change remains in effect for all of the following notes. Type:

The C plays in octave 3 and then the O command raises the octave to one level higher. If you run this PLAY command again, the octave will be raised again. To make this work like the previous example, you need to lower the octave after the D. Type:

With this example, you can change the first O command to any octave you want. The D will play one octave higher while C and E will play in the same octave. If you use relative octave changes everywhere, you can raise or lower an entire piece of music with one simple change. This technique could be a real time saver if you want to experiment with different instruments.

A single note can get quite busy with modifiers but remember, the length is a prefix before the note letter and the dot must be last. The following examples play the same notes. Type:

The speed at which music is played is called the tempo. It is measured in quarter notes per minute. You can adjust the tempo with the T command to make a tune play faster or slower. Type:

The R command resets the tempo value to 120 quarter beats per minute. The T240 command makes the music play twice as fast as it did before. The new tempo value remains in effect until you change it with another T command or reset the synthesizer. The tempo of a song is often written in Italian above the first staff of sheet music. Some common terms and their meanings are: largo (very slow), lento (slowly), andante (moderately), allegro (fast), and presto (very fast). Music usually sounds better when it’s played at the speed for which it was written.

The way you press a key makes a difference in the sound it generates. So far, Logo has been playing legato or very smoothly. Some music sounds better when you strike a key quickly rather than holding it down. This is called playing staccato. You can adjust the key striking in Logo with the S command. It uses the suffix numbers from 0 to 99. The default value is 10 which plays a perfect legato. A higher number represents a faster striking speed. Type:

The S command does not change the tempo of the notes, just the way in which the notes are played. All of the notes played now will be played staccato until another S command changes it or the synthesizer is reset. On sheet music, notes that should be played staccato have a small dot above or below them. There is no staccato note modifier for individual notes.

The volume of your Logo music can be controlled with the V command. With no modifier, it turns the sound off. The values for volume are from 0 (off) to 127 (loudest). Of course, there may be other ways to control the volume of your computer, like a volume control knob. On sheet music, you may see symbols that indicate how loudly or softly the music should be played. Some common symbols and their Italian names and meanings are: mp (mezzo-piano means quite soft), p (piano means soft), f (forte means loud), and ff (fortissimo means very loud). What you call a piano today used to be called a pianoforte. It was one of the first keyboard instruments that could play softly or loudly.

There are times when no sound should be made, but that’s not the same as turning off the volume control. In musical terms, it’s called a rest. In Logo, it’s called a pause and the command letter is P. Rests in music are like silent notes - they have a length of time to play nothing that is equal to the length of time to play a note. There are whole rests, half rests and so on. (In Logo, it’s whole pauses, half pauses, and so on.) The length modifiers for pauses are the same as those for notes, including the dot. Type:

Pauses are used to fill in the time so that the beat count remains the same. It’s not a skip to the next note - it’s a time for the sound of silence as the beat goes on.

It’ the only way to Carnegie Hall!

Playing Simple Songs

Playing a simple tune is just a matter of making a list of all the notes and giving it to PLAY. Here is the “sheet music” for the song called “Frere Jacques (Are You Sleeping?)”. You should recognize the first three notes.

To play the first measure, type:

The second measure is the same as the first. You can put them together like this:

One long list of notes works fine for Logo, but it might become a problem for you later on. Suppose you made a mistake somewhere in the middle of a long song. Maybe you played a low note when you should have played a high note. How are you going to find it? Music is written in measures, so why not use the bar symbol? !

The exclamation point (or bar) is just a marker for separating the notes in one measure from another measure. It’s a big help to know where you left off when you enter the notes from sheet music. If you continue adding notes, the PLAY list will eventually scroll out of sight. With multiple command lines, it will be difficult to use the up-arrow key to play the lines to make the tune sound right. And, of course, you know that what you type in the Listener will not be around the next time you load Logo. Type:

Press the Tab key to move the cursor into the Commands text box and type in the procedure below.

Click on the Define button to close the Define a Procedure window and then type:

The next measure has a sequence of four eighth notes and two quarter notes. You have to make a choice about how to identify them. You can use the L command before and after the eighth notes, or use the 8 prefix on each note. Either way will work, but remember, the L command changes all of the following notes to the new length. If you use L8, remember to change back to quarter notes. Add the fifth and sixth measures to your FRERE procedure. Here is an example:

The last two measures look as simple as the third and fourth. However, the G note is one octave lower than the C notes. You have another choice to make. The default octave is 4, so you could use the octave modifier after G, as in G3. This will work properly, but it means that your tune will only sound right when it’s played with the default octave 4. A relative octave change requires a bit more typing, but it allows for more flexibility later on. Add the last two measures to your FRERE procedure. Here is an example:

Click the Define button and then play the entire song. Type:

Where are the bells?

To pick up the tempo, type:

Need a higher pitch?

Want the music to play in the background?

Music is often written with phrases of melody, much like the way people write phrases in sentences. Musical phrases may not always start and end at the bar, and they often extend through many measures. Here is the first staff of another simple song, called “Home on the Range.”

If you played each measure separately, it wouldn’t sound right because the melody creates phrases that end in the middle of the third and fifth measures. You can still use the bar symbol to match the sheet music, but the lists of notes will be easier to follow if each list is a complete phrase. Here is an example you can put in a procedure (call it COWBOY since this is a cowboy song):

If you entered 2D5 in the last line, you’ll get an error message when you play it. Unfortunately, the letters D and E are used for scientific notation of numbers. When either the letter D or E is preceded by a number and followed by another number, the character sequence is interpreted as a number. Fortunately, with syntax highlighting turned on, you’ll see a different color. Be aware of this as you enter D and E notes. Notes like 2D, E5, 2D5., 2E#5, and 2D5# work fine.

The next two phrases of this song are the same as the first two phrases. The last phrase is a little different than the third phrase. Here are the words to the song, in phrases:

Where seldom is heard,
A discouraging word,
And the skies are not cloudy all day.

The note sequence for the last phrase is:

However, they are not all quarter notes and some are of a different length in this phrase than they were in the third phrase. See if you can get it to play correctly. Use the blank staff to finish your composition.

Playing Chords

The music you’ve played so far has been with one note at a time. There is a lot of sheet music that has arrangements for songs that require only one note at a time. Some of it is simplified from much more complicated arrangements. However, it makes good music and it’s the easiest to play, whether in Logo or on a real instrument. A musical chord is more than one note - usually three or four - played together. On a piano keyboard, you would press all of the keys of a chord at the same time. On sheet music, chords appear on the staff with the notes stacked on top of each other.

To play a chord in Logo, put all of the notes for the chord in a list of its own inside the PLAY list. All of the notes start to play at the same time, but the length of the chord is determined by the longest note length specified (either explicitly or by default). Type:

This plays one C chord, not three separate notes. One of the first songs that a beginner learns to play with two hands is called “Chopsticks.” Define the CHOPSTICKS procedure and then play it on any instrument you want to hear. You will have to use a different default octave for some instruments. Try CHOPSTICKS on tubas or sitars. Type:

A chord is often played at the same time as a separate note in the melody. To do that, include the extra note with the chord notes. Type:

This plays the first two measures of an earlier song with some chords played with the first and third notes of each measure. The separate D and C notes play normally. To make the chord sound sustain itself, you can use the plus sign symbol (+) at the beginning of the music. The plus sign symbol (+) causes notes or chords to be played for as long as the instrument can maintain them. On a piano, the sound fades rather quickly. Some instruments can maintain a sound forever - you stop it with a pause or reset command. Put a plus sign in the last example:

Even though D and C are still separate notes, the sound of the chord is still heard when they are played.

Chords are often played as the accompaniment to the melody of a song. Define the MASTERPIECE procedure and then get a printed copy of the online help for PLAY so you can follow along with the instrument selections. If you can spare about ten minutes, enter the following line to hear each of 119 instruments take turns playing their own MASTERPIECE. Make a note of the instruments you like the best.

Type:

If you can only spare about a minute and a half, then change the last 1 to 8 - this will play one instrument in each category. The FOR statement runs its list of instructions using N as a counter. The LIST command builds a list containing the R and I musical commands. The WORD command builds the musical I command with the value of N as a new suffix each time through the FOR loop. The last number is an increment parameter. Changing it to 8 will play instruments 0, 8, 16 and so on.

The music lesson is now over. What could be more appropriate than to say “fini” with music?

Noise and Other Sounds

One sure way to make noise is to strike random keys on a piano as you walk by. Type:

When you walk by in the opposite direction, how can you resist? Type:

These “cartoon cat chases” make a fun noise. Did you get the sense of a cartoon character running away from you and then coming back? Try your own original composition.

Logo has better ways of making noise - sound effects. The instrument numbers 120 through 127 play different noises. Experiment with them. Try using a very slow tempo so the “notes” last a long time. It’s not music, but to play sound effects, use the notes in the same way. Type:

The crowd loves your music! The + symbol forces the applause to be maintained until either a pause or a reset.

The most fun sounds are specially recorded waveform sound files. The Sounds folder is full of them. Type the following command line - notice the quotation mark:

When the input to the PLAY command is a word, Logo looks for a sound file to play instead of musical commands. Here is the beginning of a simple “repeat after me” game called SIMON. Define the procedures and then type SIMON to start it. A random sequence of sounds is played when you click on the small turtle. See if you can repeat the sequence. The PPROP command means “put property.” You’ll learn more about properties in Chapter 6 - Property Lists.

You can define your own shapes to go with the sounds or drag a picture to a turtle from the Toolbox. Use the “Sparrow2” from the Birds panel, the “Bee” from Animals, the “Telephone” from Other Pictures, and the “Beetle” from Vehicles. All of these multi-colored images are called bitmaps. You’ll learn more about them in Chapter 7 - Bitmaps. When you drop a bitmap onto a turtle, the silhouette of the image becomes the turtle’s shape because turtles are monochrome (one color). If you really want a barking dog, try using the “Goat” image in Animals - and a little imagination.

Just for fun (or for serious research?), play SIMON with just the turtle shapes to see how well you do. Later on, put the proper shapes in place and play again. Does your ability improve with appropriately shaped images? Later still, scramble the shapes so that the bird buzzes and the telephone honks. Does your performance go down the drain? There’s a sound for that!