Classic Computer Magazine Archive COMPUTE! ISSUE 41 / OCTOBER 1983 / PAGE 224

PROGRAMMING THE TI

C. Regena

Playing Music On The TI

Musicians, rejoice! Here is a computer that plays music. You can play a single tone to tune an instrument or get a pitch for a song, or you can play complex rhythms with three-part harmony. Compose to your heart's content and let the computer perform your masterpiece.

Nonprofessional musicians – if you'd like to learn more about music, your computer can be a great teacher. Learn the notes on a keyboard, learn to read music, learn definitions, or learn to distinguish chords. The computer with music capabilities makes learning fun.

For those of you who prefer not to write your own programs, the TI Music Maker command module is available. Here's a quick review. You may compose music by choosing various types of notes or rests (quarter, eighth, half, etc.) and placing them on the staff. Choose notes for accompaniment if you wish. Build a song a measure at a time. The computer makes sure the timing works out correctly. Oh yes, you can choose your key signature, time signature, and tempo. At any time you can play or edit your composition, then save it on cassette or disk if you like.

Another section of the module is made especially for nonmusicians. You may draw lines up and down the screen at different levels for a "sound graph," then hear the computer play relational tones. Add second and third voices if you wish. This command module is really quite versatile with many options and can help you learn about music.

CALL SOUND

To program your own music on the TI, use the CALL SOUND statement. The basic form is

CALL SOUND (duration, frequency, volume)

The duration is a numeric expression (number, variable, or algebraic expression which will evaluate to a number) which is the number of milliseconds you wish to play the tone. For example, 1000 would be one second. The number may be from 1 to 4250 or from -4250 to -1.

The frequency is a numeric expression that indicates what tone to play. The frequency is the cycles per second and may be from 110 to 44733, which is from low A on the bass staff to out-of-human-hearing range. The "Musical Tone Frequencies" table in the Appendix of the User's Reference Guide lists the musical notes with the corresponding frequencies. Note that you can specify numbers that are between the normal musical tones.

The volume is a numeric expression that indicates loudness. The volume may vary from 0 to 30, where 0 is the loudest. The volume also depends on the audio setting of your monitor or television, but you can control relative volumes of the tones with this parameter.

Try this command:

CALL SOUND (500, 440, 2)

The computer plays the tone of A (440) for 500 milliseconds (half a second) at a volume level of 2.

Now, if you want to tune your band instrument, just run this program.

100 CALL SOUND (4250, 440, 0)
110 GOTO 100

You may specify one, two, or three notes to be played in one CALL SOUND statement. Each statement has one duration, then a frequency with a volume for each note desired. Here is an example of the three notes in the C major chord:

CALL SOUND(1000, 262, 6, 330, 4, 392, 2)

The chord will play for 1000 milliseconds. The notes played are C at a volume 6, E at a volume 4, and G at a volume 2. Try a few chords with different frequency and volume numbers.

If you play a solo instrument, you might enjoy programming the computer to play the accompaniment chords. Tune your instrument with the computer, then you can play with the computer as your accompanist.

Using Sheet Music

If you use three tones in the CALL SOUND statement, they may be in any order. I like to use the first frequency and volume as the melody tone, then the second and third frequencies and volumes as the accompaniment tones. This way I can keep track of which number is the melody. Also, if I start to run out of memory in a piece, I can go back to the CALL SOUND statements and delete accompaniment tones by keeping only the first frequency and volume in each statement.

You may work from a copy of written music to try out the musical capabilities of the TI. The top note is usually the melody. You may choose any two notes written directly under the melody note for the accompaniment or the other two notes in your CALL SOUND statement. To emphasize the melody, use a louder volume for the melody note and softer volumes for the accompaniment notes. For example:

CALL SOUND(400, 262, 1, 196, 6, 159, 8)

If you have two CALL SOUND statements together which specify the same frequencies and volumes, the notes may sound like one long note rather than two separate notes. To make the notes sound distinct, just change the volume number for one of the notes:

300 CALL SOUND (200, 262, 2, 196, 6, 165, 8)
310 CALL SOUND (200, 262, 3, 196, 6, 165, 8)

To make a bass note sound tied or held while two different melody notes are played, keep the frequency and the volume numbers the same in both statements:

500 CALL SOUND (300, 262, 2, 165, 8)
510 CALL SOUND (300, 330, 2, 165, 8)

Other statements may be executed while a note is being played. You may define graphics, draw graphics, or make calculations between CALL SOUND statements. This feature allows you to have fun choreographing pictures with music to present a musical dramatization. You do need to experiment so you don't get too many statements between the music statements or there will be gaps in the music.

A note will keep playing for its specified duration, and the computer will execute statements until either the duration runs out or another CALL SOUND statement is encountered. If another CALL SOUND statement needs to be executed, the computer waits until the first duration is finished before starting the next sound. If you prefer to have the computer go ahead with the next sound statement, use a negative number for the second statement's duration. Here is an example.

100 CALL SOUND (200, 392, 2)
110 CALL SOUND (200, 330, 2)
120 CALL SOUND (200, 262, 2)
130 CALL SOUND (200, 330, 2)
140 CALL SOUND (400, 392, 2)
150 END

The computer starts with the tone of G and plays for 200 milliseconds. Next the tone of E plays for 200 milliseconds, then C for 200 milliseconds, then E for 200 milliseconds, then G for 400 milliseconds. During the last note the program will end, but the note will keep playing for the 400 milliseconds.

Now change to negative durations in lines 110–140:

100 CALL SOUND (200, 392, 2)
110 CALL SOUND (-200, 330, 2)
120 CALL SOUND (-200, 262, 2)
130 CALL SOUND (-200, 330, 2)
140 CALL SOUND (-400, 392, 2)
150 END

This time, the computer starts by playing G. As soon as the computer comes to line 110, a CALL SOUND statement with a negative duration, the computer immediately starts the new sound – no matter what the previous duration was. Line 140 starts the sound of G as soon as the computer comes to that statement, then continues the sound for 400 milliseconds since there is not a following sound statement with a negative duration. Try running these two programs to hear the difference.

A technique I like to use in programming music is to use a variable name for the duration, and specify the numeric value of that duration variable near the beginning of the program. For example, I often use T for "tempo" or "time" or M for "metronome marking" or N for "note." If I use T to represent the duration for a quarter note, then T/2 would be an eighth note and 4 * T would be a whole note. You can get exact timing in your music and let the computer calculate the durations.

Note: Avoiding using Q for "quarter note," especially on the TI-99/4, because the key combination of SHIFT Q is "quit." This is comparable to the FCTN (quitting on the TI-99/4A). An accidental SHIFT Q will wipe out your program and return to the title screen. With a shifted parenthesis before the variable and a shifted comma after the variable, it's too easy to get an accidental SHIFT Q.

Variable Durations

Another advantage to using a variable duration is that you can write your song in terms of the variable, then change the tempo of the song by changing only one line (the line defining the duration) rather than each CALL SOUND statement. Here is a short example.

100 T = 400
110 CALL SOUND (T, 262, 2)
120 CALL SOUND (T, 294, 2)
130 CALL SOUND ( 2 * T, 330, 2 )
140 CALL SOUND ( 3 * T / 4, 349, 2 )
150 CALL SOUND ( T / 4, 392, 2 )
160 CALL SOUND ( T / 2, 440, 2 )
170 CALL SOUND ( T / 2, 494, 2 )
180 CALL SOUND ( T * 4, 523, 1 )
190 END
100 Duration of quarter note = 400
110 Quarter note
120 Quarter note
130 Half note
140 Dotted eighth note
150 Sixteenth note
160 Eighth note
170 Eighth note
180 Whole note

RUN the program. Now change line 100 to T = 800. The song is twice as long, but each note stays in the exact proportion. Change line 100 to T = 200. The song is faster, but still in proportion.

If you need to learn a song with a difficult rhythm, program the computer to play the song. Use a variable such as T for the duration. You can set the duration to a slower note, then as you learn the song you can speed it up by changing just the one line.

You may prefer to use variables for the different kinds of notes in this manner:

100 T = 400
110 E = T / 2
120 H = T * 2
130 CALL SOUND ( H, 523, 2 )
140 CALL SOUND ( E, 494, 3 )
150 CALL SOUND ( E, 440, 3 )
160 CALL SOUND ( T, 392, 2 )
100 Quarter note duration
110 Eighth note
120 Half note

You may also want to set up a list of variables for the note names before you use them in CALL SOUND statements:

100 T = 400
110 C = 262
120 D = 294
130 E = 330
140 CALL S0UND ( T, E, 2 )
150 CALL S0UND ( T, D, 2 )
160 CALL SOUND ( T, C, 2 )

You may also use a variable for the volume, such as CALL SOUND(T, D, V).

Just as in other programming, you can use FOR-NEXT loops and GOSUB and GOTO statements to help write your music. For example, if you have a musical phrase between repeat bars, you can use a FOR-NEXT loop to play it twice. If you have a common phrase used several times within a song, use a GOSUB procedure.

Beethoven Medley

The following program, "Ludwig," illustrates the use of CALL SOUND statements to create a medley of familiar Beethoven pieces. Line 120 sets the duration of a quarter note to 400 milliseconds for the first tune, an excerpt from "Ode to Joy" of the Ninth Symphony. Lines 170–660 play this melody. In between the CALL SOUND statements are graphics statements. Lines 180–340 define graphics characters and colors, then later CALL HCHAR and CALL VCHAR statements draw a picture. The CALL SOUND statements in lines 170–400 illustrate the "tied" bass note, or a bass note held while two melody notes are played. Most of the notes are quarter notes, but line 610 has a dotted quarter note, line 650 has an eighth note, and line 660 has a half note.

Line 860 resets the duration variable T to 200 milliseconds. This time T represents an eighth note for phrases from "Ecossaises." The excerpt here is taken from music that is within repeat bars but has a first ending and a second ending. The common part of the repeat is in the subroutine at lines 1860–2230. Line 890 GOSUB 1860 plays the common phrase, then lines 920–980 play the first ending. Line 1010 repeats the common phrase with GOSUB 1860, then lines 1040–1100 contain the second ending.

Lines 1260–1420 play the third melody, "FurElise." This example shows GOSUB commands within a FOR-NEXT loop. The subroutine for the common notes is contained in lines 2240–2420.

The final melody (lines 1430–1840) is an excerpt from the second movement of Beethoven's Fifth Symphony. Line 1430 defines the new duration T to be 800 milliseconds for an eighth note at an andante tempo. U is defined as three-fourths of an eighth note, or a dotted sixteenth note. T/4 is used for a thirty-second note. Character 128 is defined as a graphic musical note, and the embedded CALL HCHAR statements among the CALL SOUND statements place the notes on the screen.

Line 1850 (GOTO 1850) holds the picture on the screen. Press CLEAR (FCTN 4 on the TI-99/4A or SHIFT C on the TI-99/4) to stop the program.

If you prefer to save the typing time, you can obtain a copy of this program by sending $3, a stamped, self-addressed mailer, and a blank tape or disk to: REGENA, P.O. Box 1502, Cedar City, UT 84720. Please specify the name of the program.