Playing speaker and Tandy programs
PC speaker and Tandy sounds (SND) · Sound and music · Sound effects
In brief
What the game does with the programs of an SND file once it has loaded it, through the PC speaker or the Tandy sound chip: how a sound starts and replaces the one playing, and how its notes, tempo and loops are played. The anomalies of these routines close the page.
How a program plays
When a sound is requested, the game reads its entry in the sound-effect table. A value of 255 plays nothing; any other value, bit 7 ignored, is the number of the program to start.1 The new program replaces the one playing at once: there is no priority and no queue, so a sound cut short by another is not resumed. An entry of 0 starts program 0, which in every file ends at once: it plays nothing but silences the sound in progress. The volume that the game passes with an environmental sound is ignored.
The driver reads the program on every interrupt of the system timer, which it sets to 291.3 interrupts per second; one interrupt in sixteen still reaches the game clock.2 A program is a stream of elements:
| First byte | Bytes | Element |
|---|---|---|
0x00 | 2 | A rest, then its duration. |
0x01–0x5f | 2 | Note 1 to 95, then its duration. |
0xe6 | 4 | A frequency in hertz (two bytes), then a duration. |
0xe2 | 2 | Set the tempo factor (0 counts as 1). |
0xe8 | 2 | Lower the tempo factor; not used by the files. |
0xe9 | 2 | Raise the tempo factor, at most 255; not used by the files. |
0xcd | 2 | Set the counter of loop A (0 counts as 1). |
0xce | 3 | Count loop A down; unless it is exhausted, jump back by the two-byte offset. |
0xd1 | 2 | Set the counter of loop B. |
0xd2 | 3 | Count loop B down and jump back, as 0xce. |
| any other | 1 | End of the voice; the files use 0xff. |
A note or a frequency sounds until the next one: the next element is read (duration + 1) × tempo factor + 2 interrupts later. Commands take effect on the same interrupt as the note that follows them. The tempo factor starts at 1 for each sound; the two loop counters are not reset and keep their value from the previous sound, but every loop of the original files sets its counter first.3
Notes follow an equal-tempered scale of 95 semitones: note 1 is about 18.4 Hz and note 95 about 4,187 Hz, the C five octaves above middle C. The two drivers take the notes from two tables of the executable, one of speaker timer divisors and one of Tandy chip divisors.4 The Tandy chip cannot go below about 109 Hz: notes 1 to 31 all play at that pitch. Taking note 56 as the A at 440 Hz and the usual clocks of the two chips, the speaker's notes lie within −0.6 to +13.3 cents of the scale, and the Tandy chip's, above that limit, within about 19 cents either way.
On the PC speaker (driver 2), the program plays on one voice.
On the Tandy chip (driver 3), the same program plays on three voices, as an echo: the second voice starts 35 interrupts (0.12 s) after the first and the third 70 interrupts (0.24 s) after it, each about 10 dB quieter than the one before. Each voice keeps its own position, tempo factor and loop counters, and the sound ends when all three have reached their end.
Anomalies
The last three are never reached with the original files.
Bug: the Tandy driver's test for bit 7 can never succeed. With driver 3,
the sound routine tests whether the table value is negative before
clearing its bit 7, and if so switches the output to the built-in speaker.
But the value is read as an unsigned byte, so it is never negative, and
the switch never happens. Every level file maps sound 119 to 130: with
driver 3 it plays program 2 on the Tandy voices, like any other sound. A
faithful reimplementation ignores bit 7 and keeps the Tandy output. (
ANOMALY-116)
A duration of 255 gives the shortest note. The driver adds 1 to the
duration in a single byte before multiplying it by the tempo factor, so
255 becomes 0 and the next element is read two interrupts later. The
original files use durations up to 64. (ANOMALY-117)
Bug: lowering the tempo factor never stops at 1. Command 0xe8
subtracts its operand and sets the factor to 1 only when the subtraction
both overflows and gives zero, which cannot happen; the factor can fall to
0 or wrap round. A factor of 0 reads each element two interrupts after the
previous one. No original file uses 0xe8. (ANOMALY-118)
A low frequency stops the game. Command 0xe6 divides the timer
clock by the frequency without checking it. On the speaker, any value
below 19 Hz gives a quotient too large for the division, and 0 divides by
zero; on the Tandy chip, 0 and 1 do. The processor then raises a division
error inside the timer interrupt. Run on the player's code with a changed
copy of a sound in memory, values 18 and below on the speaker, and 1 and 0
on the Tandy chip, raise it; 19 and 20 do not. The program's own handler of
that error prints "Divide error" and ends the program
with exit code 3, without putting back the timer interrupt that the sound
driver took over (inferred: the computer may then hang). The original
files use 20 Hz and more.5 (ANOMALY-119)
Sources
Locations refer to the game executable
reference; the comparative source is listed in the
technical references
(REF-SCUMM-023). The statements about the
original files come from decoding every program of the ten files of the
file inventory with the schema.