Personal Computer News


Machine Code Sound

 
Published in Personal Computer News #093

Listen to D. J. Miles' advice on machine code sound and your MTX will soon be warbling 'Thanks for the memory…'

Assembly Tune

Listen to DJ Miles' advice on machine code sound and your MTX will soon by warbling "Thanks for the memory..."

The Memotech MTX series produces sound using the Texas Instruments SN76489A chip - the integrated circuit used to great effect by BBC Basic. MTX Basic, unfortunately, does not stretch to 14 parameter music envelopes like the Beeb, but it does offer frequency and volume control through the SOUND statement. Continuous music may be played into a sound buffer, but the maximum number of notes per channel allowed is only 256, and this fills more than 12K of RAM.

Machine code, on the other hand, can be used for sound with the advantage of making efficient use of memory. Data must be sent to the chip through output port 6 and strobed in via input port 3. The destination of this data is one of eight registers which control the frequency and colume for the three tone generators and the noise generator (see table 1).

Register Contents
0 Channel 0 Frequency
1 Channel 0 Volume
2 Channel 1 Frequency
3 Channel 1 Volume
4 Channel 2 Frequency
5 Channel 2 Volume
6 Channel 3 Shift Rate
7 Channel 3 Volume
Table 1

Volume is controlled by passing a nibble to the respective attenuators. A volume level of zero is the loudest, and 14 is the softest. If the bit pattern representing 15 is sent, the sound is switched off. It is not necessary to program the volume of the channel whenever the frequency is changed (as in Basic) but no sound is output if no level has been set.

Frequency is altogether different. The tone channels require ten bits of information to produce a sound. This data is related to the frequency produced by the formula: Frequency = 4,000,000 / (32*Data).

Note Frequency (Hertz) Data
C 264 475
C# 278 450
D 294 425
Eb 312 400
E 334 375
F 358 350
F# 370 338
G 400 313
G# 416 300
A 454 275
Bb 476 263
B 500 250
Table 2

A list of notes, frequencies and their equivalent data numbers is given in table 2. It's useful to remember that doubling these numbers produces the same notes one octave lower, and halving these numbers raises the scale by one octabe. Sound is a handy subroutine which simplifies the task of transferring bytes to the sound processor. It must be entered with Z80 register C containing the chip's destination register and register pair HL containing the data to send. Program 1 demonstrates how it is used. It emits a simple 'laser-gun' noise by repeatedly changing the frequency of a tone channel.

Program 2 proves how simple it is to play tunes with this subroutine by playing a few bars of a familiar tune. It occupies only 200 bytes of memory, which is about 14 times more efficient than the equivalent Basic program using a sound buffer.

The data for the tune is held after label START in the format note, length, note, etc. Any suitable music could be placed at that address so long as it ends with the number 255) which tells the program to return to Basic. There is, of course, no limit to the length of the music, and the program could easily be adjusted to perform some other task during the delay between notes.

D. J. Miles