ZX-81 MUSIC
by John Sylvester
from Your Computer, March 1982

The ZX-81 may not be renowned for its dulcet tones, but passable music
is well within its capabilities without hardware modifications. John
Sylvester shows how.


THE ZX-81 HAS one major failing when it comes to creating music - it
cannot use the video and create sound at the same time. Nevertheless,
you can obtain pleasing results with the notes which cover an
eight-octave range complete with semitones. The notes are at first
estimated, and then fine-tuned with the aid of an electric organ. This
is, of course, no guarantee that the final result is correct.

To hear the music generated, you can use a cassette recorder in record
mode and, if available, use the monitor facility or feed the cassette
output through a suitable amplifier.

The easiest wave-form to produce on a computer is that of a square
wave, since this is a simple switching pattern. The number of times
the wave-form is switched in one second denotes the frequency of the
output. Figure 1 [see ZX81MUSI.GIF] shows a square wave.

The time taken to complete one cycle is called the periodic time and
in figure 1 is shown by t1. t2 and t3 are the times during which the
wave-form is switched on and off. The periodic time is the reciprocal
of the frequency in hertz or cycles per second.

If the frequency is 50Hz, the periodic time is 2Oms., so t2 and t3
equal 1Oms. each. If the periodic time is increased, the frequency
decreases and conversely, if the periodic time is decreased, the
frequency increases.

Now that we have seen how to produce a frequency by switching on and
off, the question arises as to how to use the ZX-81 to achieve the
same effect. J Chalmers, in Software File, December issue, suggested a
simple circuit which would allow you to do this. However, the
capability is built into the ZX-81 and there is no need for extra
circuits.

Listening to a recording of a saved program you will notice that there
is a definite variation in frequency. So, if we can discover how this
is done, we can use the cassette port to produce our frequencies.

Without describing in detail the exact working of the cassette
circuitry, it is sufficient to know that the output to the cassette is
via port FF. This alone will not produce any response. After extensive
experimenting I found that port FE was also involved, so that the
instructions

	OUT (FF),A

turned the cassette port on and

	IN A,(FE)

turned it off. We now have the necessary data to produce the switching
required.

Listing 1 contains machine code to control the cassette output. From
the listing you will see that after output and input, there is a delay
which produces the time required for t2 and t3. The length of note is
determined by Time and this is decremented by the half-cycle count
after one complete cycle. This means for a given value in Time, the
duration of all notes is constant.

The machine code is placed in the Rem statement of Basic at line 1.
Listing 2 gives a Basic program to be used in conjunction with the
machine code. Lines 10 to 180 calculate the frequency of a given note
in a specific octave; lines 300 to 330 calculate the counts to give
the half-cycle delays necessary to produce the required frequencies.
To raise or lower the pitch of all octaves the value of 17E-6 in line
300 may be altered. All the counts calculated are stored as characters
in the array O$.

Lines 190 to 196 play an introductory tune after the initialisation of
the array. The user can specify notes, octaves and duration of the
notes in lines 200 to 260. Notes are entered using A to G; setnitones
are indicated by inverse A,B,D,E and G; a pause by entering a space at
the required point is performed in lines 1200 to 1220.

Octaves are indicated by 1 to 8, and the duration by 0 to F. Lines
1000 to 1110 constitute the subroutine which scans the input data.
They also retrieve the correct counts from the array and Poke them
into the locations in the machine code. The machine-code program is
then entered.

Lines 1500 to 1620 calculate the values of the semitones and Poke them
into the correct locations. Lines 2000 to 2065 aflow the user to play
direct from the keyboard. The octave is selected by pressing the keys
1 to 8, and the notes by keys A to G. If a semitone is required, the S
key is pressed before the note. The duration in this mode is set to 1
- line 2050. Press K to return to the normal mode.

You can use the machine-code routine to produce sound effects by
Poking the locations with the counts to achieve the correct frequency
and duration.

----------------------------------------------------------------------

Variable Use
O$	Array for holding the counts for the relevant frequencies
J	Octave pointer for array O$
N	Note pointer for array O$
X$	Octave string
N$	Note string
D$	Duration string
F	Calculation of frequency and MSD of count for frequency
X	String pointer used with X$, N$ and D$
F1	LSD of count for frequency
T	Pause count
SF	Semitone flag for direct-keyboard operation

Glossary

----------------------------------------------------------------------

