
The Einstein has a sound chip with three voices, which can each hold a note while the program gets on with something else. SuperForth's sound words are on the disc, in the smallest extension of all: one screen.
NEWFILE SOUNDS
1 LOAD
The three voices are called A, B and C. To hear one you have to do three things.
Switch the voice on. CHANNEL takes six flags, 1 for on and 0 for off. The last three are the tones, for C, B and A in that order. (The first three switch on noise instead of a tone; this course does not use them.) So tone on voice A alone is:
0 0 0 0 0 1 CHANNEL
Set the pitch. FREQA takes the pitch in hertz - vibrations a second. 440 FREQA is the A that orchestras tune to. Double the number and the note goes up an octave.
Set the volume. VOLA takes 0 to 15. 0 VOLA is silence.
The note then carries on sounding until you change it or turn it down. SuperForth does not wait for it; the chip holds the note by itself. That is why a tune needs a fourth ingredient: something to make the program wait while each note lasts.
An empty loop does it:
3000 0 DO LOOP
That takes about a fifth of a second. It is crude, and honest: the machine counts to three thousand and does nothing else.
NEWFILE SOUNDS
1 LOAD
: NOTE FREQA 12 VOLA
3000 0 DO LOOP ;
: TUNE 0 0 0 0 0 1 CHANNEL
262 NOTE 294 NOTE 330 NOTE
262 NOTE 0 VOLA ;
TUNE
Starting from
A fresh boot, with SuperForth just started - and, in MAME, with its sound and your computer's turned on.
What you should hear
Four quick notes, a fifth of a second each: C, D, E, and C again. It is the opening of Frere Jacques. Then silence, because TUNE finishes with 0 VOLA. Leave that off and the last note sounds for ever.
| Note | Hertz | Note | Hertz |
|---|---|---|---|
| C | 262 | G | 392 |
| D | 294 | A | 440 |
| E | 330 | B | 494 |
| F | 349 | C | 523 |
Voices B and C have their own words: FREQB and VOLB, FREQC and VOLC. Turn on more than one tone in CHANNEL and you can play chords:
0 0 0 1 1 1 CHANNEL
262 FREQA 330 FREQB 392 FREQC
10 VOLA 10 VOLB 10 VOLC
NOTE to 9000. How long is each note now?0 VOLA off the end of TUNE. How do you stop the noise?25.1 Finish the first line of Frere Jacques: C D E C, C D E C.
25.2 Define SIREN to sweep the pitch from 300 up to 900 and back, using two loops and +LOOP.
25.3 Define BEEP ( hz -- ) so that a game could call it, and use it in the guessing game from S20: a low beep for a wrong guess, a high one for the right one.
Worked solutions are in Appendix II.
| Symptom | Cause |
|---|---|
| Silence | One of the three steps is missing: CHANNEL, a pitch, and a volume above 0. Or, in MAME, its sound or your computer's is off. |
| A note that never stops | Nothing turned it down. 0 VOLA. |
| A tune that is over in a blink | No waiting between the notes. |
FREQA? | The sounds are not loaded. |
With SOUNDS loaded, CHANNEL switches voices on with six flags, FREQA sets voice A's pitch in hertz and VOLA its volume from 0 to 15. A note sounds until it is turned off, so a tune is notes separated by waiting, with a 0 VOLA at the end.
S26, Decimal Points - numbers that are not whole, and what they cost.