Classic Computer Magazine Archive ANTIC VOL. 2, NO. 8 / NOVEMBER 1983


RANDOM MUSIC

Can an ATARI make a melody?

by CHARLES JACKSON


Can a computer generate melodies? It can make a series of notes, just as a bird can sing without producing a melody, but can even a bird generate a "good" melody? While considering such abstruse points, another question to ask is why anyone in their right mind would want to make a computer write melodies. Music is a form of human expression! One answer is that by simulating melodies, we might gain insight into what makes a melody, and knowing this we might be able to write better melodies.

You will be pleased to know that without buying additional plug-ins or other expensive equipment, without requiring musical ability beyond an interest in learning, and without an understanding of higher mathematics or physics, the built-in sound functions of the ATARI make it possible to answer these questions.

This article investigates ways that your ATARI can generate simple melodies. First, some short examples will show you how to generate random melodics. You can insert these short examples into programs or games that you have written. The "melodies" generated this way sound like ones in popular commercial games, like Protector by Synapse Software. However, let's be realistic from the start; the sounds from random music generation will never spice up your programs or games as much as Jerry White's Music Box from Program Design, Inc.

Listening to the short examples provides ideas for a second, more detailed way to generate melodies. Surprising results can be made by the random music generation routines described here; the melodies will never make the top ten, but some sound as good as "dentist's office music." (This may or may not be a real achievement.) Some of the internal workings of the more detailed program (Listing 1) are described at the end of the article, but try to use the program and listen to the types of melodies that you can generate before worrying about the technical aspects of random number generation.

The ATARI generates sound by the use of the BASIC command:

SOUND A,B,C,D

where A is the channel or voice (0-3), B determines the pitch (B = 0 to 255), C sets the tone quality (try 10 and 14 for pure tones and other even numbers less than 16 for special effects - especially 6,8,12), and D sets the volume (10 is typical). The frequency can be changed by poking a pitch value (B = 0 to 255) into locations 53760 for channel 0, 53762 for channel 1, 53764 for channel 2, and 53766 for channel 3.

By using either the built-in random number generator located at location 53770 or the BASIC command RND(X), one simple statement allows the generation of random melodies. Location 53770 always contains some number between 0 and 255, so it can easily be used in the ATARI sound command. The ATARI BASIC command RND(X), where X is a number or a variable, returns a decimal fraction between zero and one. As shown below, for use in a sound command RND(X) must be multiplied by a number less than or equal to 255.

The two random number generators can be demonstrated in short one-line programs:

1 SOUND 0,255*RND(0),10,10:WAIT = 2^3:GOTO 1
1 SOUND 0,PEEK(53770),10,10:WAIT = 2^3:GOTO 1

Enter the short one-line programs and listen to the melody. WAIT = 2^3 is just a way to make the notes go slower -- try removing it! While you are at it, try changing the value of C to the even numbers between 0 and 16. By the way, hit the [BREAK] key to end the program.

The SOUND commands take a lot of computer time to execute, so if you want to insert a bit of random music into your programs without making a funeral march, a faster way of changing notes is needed. All you have to do is to start a note and then POKE a new one every now and then. First, turn on the sound in the program by using a SOUND command, for example:

1 SOUND 0,1,10,10

Then enter your program. Somewhere in your program, perhaps in a FOR/NEXT loop or a statement that is used often, insert the following line:

XXX POKE(53760),PEEK(53770)

The XXX is whatever statement number you decide on, the PEEK value returns a random number, and the POKE location is for the pitch of voice number 0. If the music is too fast try placing the statement elsewhere, and if it moves too slowly, try inserting another statement.

One problem with this random music is that it doesn't even use the notes of the scale; it could even be called noise. Maybe choosing random musical notes (like hitting the keys of a piano at random) would sound better than choosing random pitches. Try this:

1 F = (2^(INT(24*RND(0))/12))/512:B = (1-2*F)/(2*F):SOUND 0,B,10,10: GOTO 1

(the complicated expressions for the pitch B choose notes on a piano keyboard). This isn't very melodious, but it is an improvement musically. Also, the intervals are just too big. Most melodies are meant to be sung, but not many people can sing a very low note followed by a very high one. Restricting the range of notes improves the sound of the melodies, as Listing 1 shows.

One way to circumvent the problem of large intervals is to choose the intervals at random, rather than choosing the pitches at random. This way of choosing numbers describes the motion of a small particle in a liquid, called Brownian motion. It is also called a random walk: take a step or two, stop, choose a new direction at random, and take a few steps. Keep on repeating this until you are convinced that you are moving at random. Brownian motion describes many things, such as the prices on the New York Stock Exchange, and it can also generate melodies called brown music.

Another problem with these simple melodies is that all of the note values are the same, so there is no rhythm. Varying the rhythm would help make a more musical sound, but leads to so many interesting possibilities that I have to leave it for you to investigate.

Finally, the melody goes nowhere. This is a fundamental problem with random music generation programs. Two physicists, Mandlebrot and Voss, have found that the way you generate random numbers can get around this fundamental problem. Mandlebrot and Voss call random music "white music" because a random series of colors is white to our eyes, so a random pattern of sounds should seem white to our ears. If you have a color TV set, turn it on to channel 3 with your ATARI turned off -- notice the white (or grey) color? The static you hear is another form of white noise or music.

These two physicists from IBM have found that most music, from Ukrainian folk melodies to Indian sitar music, from classical music to rock 'n' roll, can be described as "1/F" (one-over-F) music. In their terms, 1/F music occurs between white music and brown music. 1/F music is easy for a computer to generate, as shown in Listing 1. Voss described a method for producing 1/F music in Scientific American in April 1978. While white music chooses pitches at random, and brown music chooses intervals at random, 1/F music chooses both pitches and intervals at random.

Listing 1 generates these types of music (white music, brown music, and 1/F music). The intervals can be modified and different scales can be heard by changing the DATA statements. You may wish to try using a different tempo (but it is hard to get each type of music to have the same tempo because the different subroutines take different times), or to modify the note durations or rhythms. The program shows a rough outline of the melody on the screen while the music is being played.

Even the 1/F music program is really quite short. To produce more complex computer-generated music would require longer programs. One interesting example is the program for generating four-part harmony in the Atari Music Composer documentation. This program uses rules to assure good harmony, and it chooses notes at random until a good note turns up. (Another words, it generates white music and uses rules to make it more "musical".) It would be a large project, but using a 1/F note generator might make for a better sounding harmony.

After typing the program into your ATARI, try to decide which type of music is more musical. Can you tell one type of music from the other? Does the graph on the TV screen resemble the melody? Try the program on friends and see what they say. I'll leave these questions for you to answer - is the ATARI making music? Is it good music?

PROGRAM NOTES

The main features of the program are that the melody contour is displayed on the screen, and the tempo is made clearer or with a click (or attack) generator. Finally, three types of scales can be used: the usual diatonic scale, in this case C major; the pentatonic scale, often used in folk melodies; and the harmonic scale, often called bugle notes. Pitches from the DATA statement with the lowest BASIC statement number will be played, so change the line numbers around if you want to hear different scales. Try your own scales, or even a short melody!

The 1/F generator was described in Martin Gardner's feature column in Scientific American in April of 1978. Imagine three colored dice -- our program (Listing 1) uses seven. Initialize their values by rolling them all. Put them in order, blue, green, red. Give each color a binary digit. Let red be the units, green be the two's, and blue be the fours.

Keep count of the notes in binary form. When a binary digit changes from 0 to 1, or from 1 to 0, then roll the die associated with that digit. After each roll for a given note, add up the total of the dice to find which pitch is to be played.

Note #     Binary#
0           000        Roll all three dice
1           001        Roll only red die
2           010        Roll green and red dice
3           011        Roll only red die 
4           100        Roll all three dice 
5           101        Roll only red die

The above example shows how 1/F music is in betweenn white music and brown music. Sometimes (notes number 0 and 4) all the dice are rolled and the new note is random in the sense of white music, and other times (notes number 1 and 5) only one die is rolled and the interval is random in the sense of brown music.

In Lising 1, there are seven dice. DICE(I) holds the binary bit for each die calculated in statement 325. OLDICE(I) is the binary bit from the last note. When a bit changes between OLDICE and DICE, then that die is rolled (statement 330), and VALDICE(I) is changed. The total of all the dice (statement 340) determines the pitch from array P(1). After each type of music is played, a GOTO 4000 statement leads to the final statement, which returns to Graphics 0 and lists the program. This last statement should help in your debugging stages, and it provides a long note to end the melody.

Charles Jackson is a recent graduate of UCLA who bought his ATARI 800 to help him with class work (reports, calculations, etc. ). He also discovered it could help him play four-part harmonies.

Listing: RNDMUSIC.BAS Download