User Tools

Site Tools


logo:tutorial:chapter5

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:

PLAY [R C D E]

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:

PLAY [I57 C D E]

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:

PLAY [I C D E]

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:

PLAY [L8 C D E]

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:

PLAY [2C D E]

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:

PLAY [2C. D E]

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:

PLAY [R C D# Eb]

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:

PLAY [O3 C D E]

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:

PLAY [C D4 E]

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:

PLAY [C O# D E]

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:

PLAY [O3 C O# D Ob E]

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:

PLAY [R C 8D5#. 8E5b.]
PLAY [R C 8D#5. 8Eb5.]

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:

PLAY [R T240 C D E]

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:

PLAY [R S77 C D E]

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:

PLAY [C P D 2P E]

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.

PLAY [P R A C T I C E !]

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:

PLAY [C D E C]

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

PLAY [C D E C C D E C]

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? !

PLAY [C D E C ! C D E C !]

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:

TO FRERE

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

FRERE.lgo
TO FRERE
	PLAY [C D E C ! C D E C !]
	PLAY [E F 2G ! E F 2G !]
END

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

FRERE

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:

REPEAT 2 [ PLAY [L8 G A G F L4 E C !] ]

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:

REPEAT 2 [ PLAY [C Ob G O# 2C !] ]

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

FRERE
FRERE.lgo
TO FRERE
	PLAY [C D E C ! C D E C !]
	PLAY [E F 2G ! E F 2G !]
	REPEAT 2 [ PLAY [L8 G A G F L4 E C !] ]
	REPEAT 2 [ PLAY [C Ob G O# 2C !] ]
END

Where are the bells?

PLAY [I14] FRERE

To pick up the tempo, type:

PLAY [T240] FRERE

Need a higher pitch?

PLAY [O5] FRERE

Want the music to play in the background?

LAUNCH [FRERE]

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):

PLAY [R D ! D G A ! 2B ]
PLAY [8G 8F# ! E C5 C5 ! 2C5 ]
PLAY [8B 8C5 ! L2 D5 L8 G G ! L4 G F# G ! 2A. ! ]

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:

PLAY [C C ! B A G ! F G A ! G !]

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:

PLAY [R [C E G] ]

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:

CHOPSTICKS.lgo
TO CHOPSTICKS
	PLAY [L8 S50]
	REPEAT 6 [PLAY [[F G]]]
	REPEAT 6 [PLAY [[E G]]]
	REPEAT 6 [PLAY [[D B]]]
	PLAY [[C O# C OB] P [C O# C OB] [C O# C OB]]
END
PLAY [R I58 O1] CHOPSTICKS
PLAY [R I104] CHOPSTICKS

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:

REPEAT 2 [ PLAY [R [O3 C E G O4 4C] D [O3 C E G O4 4E] C] ]

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:

REPEAT 2 [ PLAY [R + [O3 C E G O4 4C] D [O3 C E G O4 4E] C] ]

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.

MASTERPIECE.lgo
TO MASTERPIECE
	PLAY [T180 O4 8E 8D !]
	PLAY [O3 [C E G G4] G C4 [G B D F4] !]
	PLAY [L8 [G B D F E4] D4 E4 F4 !]
	PLAY [[G B D F C4] D4 E4 F4 !]
	PLAY [L4 [G B D F4] 8G4 8G4 G4 [F A C C4] !]
	PLAY [[C E G C4.] 8F4 E4]
	PLAY [R]
END

Type:

(FOR "N 0 118 [PRINT N PLAY LIST "R WORD "I :N MASTERPIECE] 1)

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?

PLAY [R L8 I10] REPEAT 2 [ PLAY [E 4G. E 4G. E C D E F G 4A] ]

Noise and Other Sounds

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

PLAY [R S77 L8 O2]
REPEAT 5 [ PLAY [ [C D E F G] O#] ]

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

REPEAT 6 [ PLAY [ [C D E F G] Ob] ]

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:

PLAY [R I126 + C I C D E C ! C D E C] WAIT 500 PLAY [R]

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:

PLAY "DOG

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.

SIMON.lgo
TO SIMON
	CLEARSCREEN SETTURTLES 5 SETTSIZE 2
	TELLALL 1 4 PENUP LEFT 45
	EACH [RIGHT WHO * 90 FORWARD 100 SETHEADING 0]
	SETTSIZE 5 SHOWTURTLE PPROP 0 "RUN [GUESS]
	PPROP 1 "RUN [JOB 1]
	PPROP 2 "RUN [JOB 2]
	PPROP 3 "RUN [JOB 3]
	PPROP 4 "RUN [JOB 4]
	GUESS
END

TO GUESS
	REPEAT RANDOM 5 [RUN LIST "JOB RANDOM 4]
END

TO JOB :TURTLE
	TELL :TURTLE
	SETPC ITEM :TURTLE [CYAN GOLD PINK PURPLE]
	RUN ITEM :TURTLE [
		[PLAY "BIRD4] [PLAY "WASP]
		[PLAY "TELEPHONE] [PLAY "CARHORN]
	]
	SETPC ITEM :TURTLE [BLUE ORANGE RED MAGENTA]
END

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!

logo/tutorial/chapter5.txt · Last modified: 2018/08/27 03:39 (external edit)