SPECTRUM MUSICMAKER
by R. Newman
from Your Computer, February 1984

IN COMPARISON WITH the other features of the ZX Spectrum computer, its sound
capabilities are often underused. When compared to the facilities offered by
other micros the single channel BEEP of the Spectrum does seem rather tame.

However, it is worthwhile to learn how to make the most of the BEEP
command, since the inclusion of sound or music in your programs can add a
great deal to their impact.

For anything other than very short tunes, the amount of work involved is
prohibitive. It would be very much easier if you could actually play the tune
into the memory of the computer, using the Spectrum keyboard in the same way
as a piano keyboard. The Spectrum then becomes a musical instrument upon
which you can compose music, storing it as you play, and then sit back and
listen as the computer plays it for you. Once you are satisfied, you could
save the stored music in the memory to tape so that it could be included in
another program.

It was with this idea in mind that I wrote the program called Spectrum
Musicmaker in listing 1. This program runs on a 48K Spectrum, and allows you
to play music over a range of four and a half octaves. The bottom two rows of
keys operate like a piano keyboard, with CAPS SHIFT to SPACE playing white
notes, and A to L giving the black notes. The number keys 1 to 4 are used
to change the pitch of the keys in one octave steps.

If you wish to store your music as it is being played, it is put into memory
between addresses 35000 and 65000, which can hold up to 15000 notes - at
least 30 minutes of music. Each note that you play, or each pause between
notes, is stored as two bytes in the memory.

The first byte takes values from 0 to 53, where a value of 0 means that no
key was being pressed, and values from 1 to 53 correspond to BEEPs of pitch
-12 to 40. The second byte tells how long the note - or rest - lasts for, and
a value of 40 corresponds to about 1 second. You can stop playing at any
time and listen to a playback of your music, either at normal speed or at a
faster or slower tempo, and then carry on playing by adding to the end or
recording over what you have already stored.

In addition, the computer can he made to play back in a loop - replaying the
same piece over and over again. This could be useful if you have another
musical instrument and use the Spectrum to set up a backing track to which
you can play an accompaniment.

All or part of the music memory can be saved to tape, and later reloaded to
be replayed either by the "Musicmaker" program, or by a small subroutine -
listing 5 described later in this article - which you can include in your own
programs.

The Musicmaker program uses a machine code routine when the keyboard is being
played as a musical instrument; the routine is disassembled in listing 4. The
machine code scans the keyboard, plays the correct note if a key is being
pressed, counts how long a key is held down, stores the note and its length
in memory, updates the memory counter displayed on the TV screen and indicates
what octave you are currently playing in, changing this if one of the keys 1 to
4 are pressed.

The usual BREAK key is disabled while the machine code is running, and you
return to the main program by pressing key 0.

To make a copy of the program, you should first type in the BASIC program in
Listing 1. Do not RUN the program yet, because it needs the machine code
which you have not yet entered. Save the BASIC program with the command:
	SAVE "Musicmaker" LINE 70

After Verifying the program you can enter the machine code. Erase the main
program with NEW, and then type in and RUN the loader program in listing 2.
Input the machine code from the dump in listing 3. When you have finished, the
machine code should be SAVEd after the main program on your tape with the
command:
	SAVE "beepcode" CODE 34273,308

You can now rewind the tape and LOAD "Musicmaker". The program will auto-run
and load the machine code. Before the screen displaying the main menu of
options appears, there will he a short delay while the program clears the music
memory and POKEs data for the machine code routine. This delay only occurs
when the program is RUN for the first time.

If you accidentally break out of the program back to BASIC without having
saved your music to tape, you can simply type RUN to restart without losing
the contents of the music memory.

The memory counter at the top of the screen shows you what part of the memory
you are recording into or replaying from. If you are using the program to
store a tune which you want to be able to use in one of your own programs,
you will need to make a note of its value at the beginning and end of your
tune.

You can then select the "Save to Tape" option in the main menu and tell the
program that you only want to save part of the memory. When you have input
the start and end counter values of your tune, the program will tell you the
actual memory address from where your code is stored, and the number of bytes
as it saves your code.

In your own program you are unlikely to want to store the music code in these
same memory locations. For example, if your BASIC program is longer than the
Musicmaker BASIC program, you might have to store the music code at a higher
memory address. Since the music code is just data, it can easily be relocated
to any address. In your program you simply load it in using:
	LOAD "music" CODE new-start-address,number-of-bytes

Before doing this, you will of course need to have reset RAMTOP to a suitable
value using the CLEAR command so that the music code cannot be overwritten by
the BASIC system. In your program, the tune can he replayed by using the
subroutine in listing 5. Before calling it, you should assign variables MS
and NB. MS is the start address of the music codes and NB is the number of
bytes.

The volume from the Spectrum's built-in speaker is not very loud. However,
with some cassette recorders you can amplify the sound as follows. Connect
the MIC lead from computer to recorder, and disconnect the EAR lead. Remove
any tape from the recorder, and then press PLAY. You should then find that
the sound from the Spectrum is amplified through the loudspeaker of the
recorder.

If you would prefer to save yourself the trouble of typing in the program and
machine code, I can supply you with a cassette, price 3 from [address
removed].
