How an AdLib note sounds
Playing AdLib programs · AdLib music and sounds (ADL) · Sound and music
In brief
Once a program reaches a note, the AdLib sound driver turns it into the frequency, octave and levels of a voice of the card, with the instrument and the offsets of its channel. Up to two effects, a slide or a vibrato and a register cycle, then change these registers on every tick of the driver. The anomalies of the notes and effects close the page; the original game reaches none of them.
How a note sounds
A note byte gives a semitone in its low four bits and an octave in its high
four bits; the channel's base note and base octave are added, a semitone
past 11 moving to the next octave. The semitone selects a 10-bit frequency
number from a table of twelve, the channel's base frequency is added, and
the octave becomes the AdLib block. A pitch bend set by 0xb9 then adds
(or, when negative, subtracts) a value read from one of fourteen 32-byte
tables, chosen by the note's low four bits and indexed by the bend.1
The note sounds until its duration runs out, or earlier when the step
counter reaches the note spacing set by 0x83 or the fraction of the
duration set by 0xa4.
Each voice has two operators. The level written for the second one is its
instrument level plus three offsets of the channel (set by 0x9e/0xb0,
0xac/0xad and 0xab), minus a share of the note's velocity when
0xc9 is in use; the first operator gets the same offsets only when the
instrument adds the two operators. The sum is kept within 0 (loudest) to 63
(silent).2
Commands 0xb5 and 0xbc add a random amount, masked by their operand, to
the sound registers of a note and to each duration. Both draw from one
stream, shared by all channels: x becomes x + 0x9248, rotated right by
3 bits in 16 bits. x is set to 0x1234 when the driver starts and when it
shuts down, so the stream is the same on every run; it repeats after 63,331
draws.1
Effects
Each channel runs up to two effects, every tick of the driver, whether or
not it takes a step. A slide and a vibrato share the first place, so each
replaces the other; 0x92 and 0xb6 remove it, 0xa7 removes the second
effect.3 An effect advances each time its rate, added to an 8-bit
counter, carries past 255.
| Command | Effect | Operands |
|---|---|---|
0x91 | Slide. Adds a signed step to the 10-bit frequency number. A rising slide that passes 733 halves the number and raises the octave; a falling slide that reaches 388 doubles it and lowers the octave. | rate, step (high byte first) |
0x95 | Vibrato. After a delay counted in ticks from each note, moves the frequency number by a step, reversing the direction after h + 1 steps and then every 2 h. The step is the note's frequency number shifted right by 9 − depth, so a depth above 9 gives no vibrato. | rate, depth, h, delay |
0x8d | Register cycle. Writes, into one register of the channel's first operator, the entries of a table from last − 1 down to 0, then last, and so on. | rate, last, register, table address |
Three more mechanisms are not used by the original files:
- Beat (
0x9c,0x9d).0x9cstarts a shared beat counter that advances by one every (operand / 2) carries of a counter that adds the global tempo every tick.0x9dholds its channel, one step at a time, until the beat bits selected by its operand go from all clear to not all clear.4 - Pulsed channel 0 (
0xbf,0xc0).0xbfmakes the system timer switch channel 0's note on and off at a rate taken from a pair of driver tables; with a first operand of 2 it also takes the first rate and sets the low byte of channel 0's frequency from them.0xc0stops it, as do silencing channel 0 and starting a program on it.5 - Driver entries the game never calls. The driver also offers a player of 4-bit delta-coded samples, which writes each sample into the level register of channel 0's first operator about 15,900 times a second, and routines to choose the pulse table position, pause the sample player, read a channel's remaining duration and select the program table. Only the start, stop and queue entries are used by the game.6
Anomalies
The test for a third note byte also reads the last velocity. After
command 0xc9 with a non-zero operand, every note takes a third byte, a
velocity. The test that decides it checks the two 0xc9 operands and the
velocity read last, so a channel that read a non-zero velocity keeps
reading third bytes after 0xc9 0 0, until it reads a zero velocity. No
original file uses 0xc9, and the channel state is cleared when a program
starts, so all notes of these files are two bytes long.
(ANOMALY-100)
Bug: a slide that lands on a multiple of 1,024 corrupts the driver's
stack. After halving or doubling the frequency number, the slide checks
whether its low ten bits are 0, to add or remove one. It saves the number,
tests it, and restores it, but in that case it restores it, adjusts it and
then removes one more word from the stack: the adjustment is lost, the
routine takes its own return address as the frequency, writes it to the
voice, and returns to the address of the channel's state, running data as
code. A rising slide of step 2,048 reaches this case after about ten steps:
run on the driver's code, program 87 of a level file reaches it on its 16th
tick and the driver then executes the state of channel 4 until it meets an
invalid instruction (inferred: on a real computer, a crash or hang inside
the timer interrupt).7 Each level file holds
such a slide, in program 87, but no sound number selects that program and
no program starts it. A faithful reimplementation keeps the frequency
number unchanged in this case.
(ANOMALY-122)
A level sum of 128 or more gives full volume. The four parts of an
operator level are added in one byte, then a value from 64 to 127 is set to
63 (silent) but a value from 128 to 255 is taken as negative and set to 0,
the loudest. Running every sound of every level file on the driver's code,
at a distance and during a fade, only sound 114 reaches it: played seven
blocks away or more while a fade (sound 2) is raising its channel's offset,
its level adds up to 153 and is written as 0 until the next fade step. The
game plays sound 114 only at once, when the party casts the spell whose
definition names it, and none of its sounds heard at a distance (monsters,
scripts, doors, magic) is 114, so the original game does not reach this
case. A faithful reimplementation reproduces the wrap only to match the
driver exactly.7
(ANOMALY-123)
The pitch bend reads past its tables. The bend table is chosen from the
note's raw low four bits, plus two for an upward bend, among fourteen
tables of 32 bytes, and indexed by the size of the bend. Notes whose low
four bits are 12 to 15 (upward) or 14 and 15 (downward), and bends larger
than 31 either way, read beyond the tables. The original files bend by 1 or 4 only, and never play
such a note while a bend is set. A reimplementation does not need these
cases.
(ANOMALY-124)
Sources
Locations refer to the game executable
reference; the comparative sources are listed in the
technical references
(REF-MW-015,
REF-SCUMM-012). The statements about the
original files come from decoding every program of the ten files of the
file inventory with the schema.