Sound routines
Routines · Data · Address index
Sound effects and music: the sound file, the AdLib, speaker and Tandy output, and the channels and effects of the drivers. The routines page explains how the entries are written.
0x154e5, 0x1554a, 0x1555a, 0x154f6, 0x149b5, 0x157fe — AdLib output
- Role:
0x154e5writes value AL to register AH of the chip through ports0x388and0x389, waiting 4 reads of the port after the register (0x1555a) and 23 after the value (0x1554a).0x154f6limits a level to 0–63.0x149b5resets the music driver: it stops the sound, sets the random stateDS:0x43c1to0x1234and clears registers 1, 8 and0xbd.0x157feis the random draw of the music. - Used by: How an AdLib note sounds › How a note sounds
0x147f1, 0x14844, 0x146e0, 0x153f9, 0x1475f, 0x2cfe2, 0x2d044, 0x2cc79 — Sound driver timers
- Status: the rates are computed from the divisors with the standard
1 193 182 Hz input clock. Reading driver 1 as an AdLib driver is inferred
from its ports
0x388and0x389. - Role: The sound start-up at
0x2cbfbselects a driver from DS:0x638a. Driver 1:0x147f1(called at0x2cc15) installs the INT 08h handler0x146e0and sets the system timer divisor to 1365 (874.1 Hz);0x146e0runs the music every 12 interrupts and passes one interrupt in 48 to the BIOS handler (0x14742–0x14751), a tick rate of 18.2109 Hz;0x14844(called at0x2cc55) restores the vector and divisor 65 536. Drivers 2 and 3:0x2cfe2installs0x2cc79with divisor 4096 (291.3 Hz);0x2cc79passes one interrupt in 16 (0x2cc9f–0x2ccae), 18.2065 Hz;0x2d044restores the vector and divisor. On every interrupt0x2cc79also counts interrupts inDS:0x6c46(0 to 291) and, at each wrap, a byteDS:0x6c48from 0 to 59 (0x2cc80–0x2cc99; no reader of either was found), then runs the sound (0x2cc9c).0x2cfe2also sets the speaker timer to mode 3 (0x2d024), saves port0x61inDS:0x6c18and sets up one voice, or three when the Tandy flag is set;0x2d044silences the voices. Driver 1 also has an entry0x153f9, listed in its entry table at0x141be, that plays a sampled sound through0x1475fwith divisor 75 (15.9 kHz);0x1475fnever passes an interrupt to the BIOS, which would stop every tick counter while it plays, and restores divisor 1365 at the end (0x147ba). It reads two 4-bit deltas per byte (bit 3 the sign, bits 0–2 the size), adds each to a running value that starts at 8, and writes the result to the level register of channel 0's first operator (0x14771–0x147a1), after setting that voice up (0x153ff–0x15442). No instruction of the game or of the driver calls0x153f9: no far call to it, and its entry table is never read. These are the only places that program the system timer. While command0xbfhas madecs:0x43bcnon-zero,0x146e0also counts downcs:0x43beon every interrupt and, at 0, flips the key-on bit of channel 0 (0x146e9–0x14726) and reloads the count fromcs:0x43bd. - Used by: The game clock › Ticks and the two counters, The ending, The introduction, How an AdLib note sounds › Effects, Playing speaker and Tandy programs › How a program plays, AdLib music and sounds (ADL) › In the original files
0x2ca9a — Play a sound effect
- Role: Plays sound effect n with no block, through
0x2caae, which does nothing when the sound option (DS:0x65fa) is off, whenDS:0x638ais zero, when n is negative or 120 or more, or when entry n of the table atDS:0xef58is 255 (0x2cadb). Any other entry is handed to the sound driver. With driver 1 (0x2caf6–0x2cbac), it first waits whilecs:0x43b0is non-zero and restores bytes 1 and 3 of the previously adjusted program (saved in DS:0x61b4,0xef56,0xef54); then, when a volume is given (not −1) and the program is neither 0 nor 255 (0x2cb39–0x2cb47), it lowers bytes 1 (priority) and 3 (the operand of the program's first command) according to the volume (0x2cb49–0x2cba1): t = ((byte 3 × 4) xor0xff) × volume, kept to 16 bits (0x2cb6d–0x2cb74), byte 3 = (t >> 10) xor0x3f(0x2cb77–0x2cb87) and byte 1 = t >> 11 (0x2cb8f–0x2cb9c). It then queues the program (0x2cba5). With drivers 2 and 3 (0x2cbae–0x2cbf2) the volume is ignored. Driver 3 (DS:0x638c= 1) first tests the entry for a negative value (0x2cbb5): if it were, it would clear the Tandy flag and call0x2d0c2, switching the output to the speaker; but the entry was loaded as an unsigned byte (0x2cadb–0x2cae1), so the branch0x2cbb9–0x2cbc5can never run (bug, ANOMALY-116). For a positive entry with the flag clear it sets the flag and calls0x2d0c2(0x2cbca–0x2cbda). Both drivers then clear bit 7 of the entry (0x2cbdf), take the program's address from the sound data and start it (0x2cbe4–0x2cbf2). The spell start plays the definition'ssound_effectwith it. Direct calls with a constant number include 79 for the spell warnings (0x19956, call at0x1996b) and for an Aid cast on an aided target (0x2478a,0x247cb), and, in overlay 252 at file offsets, 57, 53 and 55 at the start of the non-player character dialogues 0, 1 and 2 (0x4af63,0x4af9b,0x4b029) and 54 in the dream sequence (0x4bcee). - Comparative source: REF-SCUMM-013,
snd_playSoundEffect. - Used by: Play a sound › Behavior, Play a sound › The sound-effect table, Scripted characters, flags and messages › Messages, dialogue, sound and timing, Level 4: reference tables › Sounds, Playing AdLib programs › How a program plays, Sound effects, AdLib music and sounds (ADL), The ending, The introduction, Anomalies and bugs, Playing speaker and Tandy programs › How a program plays, AdLib music and sounds (ADL) › In the original files, How an AdLib note sounds › Anomalies
0x2c9f9 — Load the sound file
- Range:
0x2c9f9–0x2ca99 - Role: Loads a sound file by base name. With sound driver 1 (
DS:0x638a= 1) it opens%s.ADL(DS:0x61bd), with drivers 2 and 3%s.SND(DS:0x61b6); with any other value it loads nothing. It reads the first 120 bytes of the file into the sound-effect table (0x2ca53–0x2ca65), then up to0x3b10bytes of the rest into the buffer that0x1025areturns (0x2ca68–0x2ca7c), closes the file and has the driver relocate its pointers (0x102aa, call at0x2ca87). The layout of the data is described by the ADL schema and the SND schema; the second pointer table of a.SNDfile is relocated too but never read. It uses the file helpers. Callers: the start-up at0x18c38, withINTRO, and the sublevel reader of the INF loader at overlay 250 file offset0x48a2b, with the record'ssound_name_slot, at every level load (read the door records follows it). - Used by: Play a sound › The sound-effect table, Level 4: reference tables › Sounds, AdLib music and sounds (ADL) › Reading the file, Sound and music, The ending, The introduction, Level data (INF), PC speaker and Tandy sounds (SND)
0x2cbfb, 0x2cc3e — Start and stop the sound driver
- Role:
0x2cbfb(driver) stores its argument in the sound driver and starts that driver: driver 1 through0x147f1; drivers 2 and 3 through0x2cfe2, driver 2 clearingDS:0x638cand driver 3 setting it and the Tandy flag, then calling0x2d0c2(0x2cc1c–0x2cc37). The start-up calls it at0x18a76with the value read fromEOSPREFS.DAT.0x2cc3estops the current driver —0x14844for driver 1,0x2d044for driver 2,0x2d0d5then0x2d044for driver 3, which also clears the Tandy flag — and sets the sound driver to 0. - Used by:
0x2d082, 0x2d0ad — Start or stop a speaker or Tandy sound
- Role:
0x2d082(far pointer) stores the address of a program of the SND file inDS:0x6a94and sets the start request, playing and busy bytesDS:0x6c1c,0x6c1band0x6c44; the sound run starts it on the next interrupt, abandoning any sound in progress. There is no priority test. Its only caller is the sound-effect player (0x2cbed).0x2d0adsets the stop requestDS:0x6c1d, which ends every voice at its next step; no call to it was found. - Used by: Playing speaker and Tandy programs › How a program plays, Play a sound
0x2ccbf — Run the speaker or Tandy sound
-
Range:
0x2ccbf–0x2cf31 -
Role: Called by the timer interrupt 291.3 times per second; works on the sound state. A start request (
0x2ccbf–0x2cd1a) copies the program address toDS:0x6a88and resets three voices: position 0, waiting for an element, not finished, tempo factor 1; it leaves the loop counters as they are. It sets the start delays of voices 1 and 2 to 35 and 70 interrupts (0x2cd15). Then, while a sound plays, each voice up to the voice count (DS:0x6c19) waits out its start delay (0x2cd39–0x2cd5c) and its countdown, or reads elements (0x2cd69–0x2cebc):0x00–0x5f: a note. Its frequency comes from the speaker or Tandy note table, as the Tandy flag selects; 0 silences the voice. The countdown is (duration + 1) × tempo factor − 1 (0x2cda9–0x2cdb6); theinc alat0x2cdafwraps a duration of 255 to 0 (ANOMALY-117).0x60–0x7fand unknown bytes: end of the voice (0x2cd7e,0x2cebc).0xcd,0xd1: loop counters A and B (DS:0x6c3a,0x6c3e);0xce,0xd2: count down and jump back by a word; a signed overflow of the jump ends the voice (0x2cdc6–0x2ce3b).0xe6: frequency in hertz, divided into 1 193 180 for the speaker or 111 860 for the Tandy chip (at most 1023), without a check of the divisor (0x2ce4a–0x2ce5b, ANOMALY-119).0xe2: tempo factor, 0 read as 1;0xe8: subtract, with a floor of 1 applied only when the result overflows and is zero, which cannot happen (0x2ce8e–0x2ce96, bug, ANOMALY-118);0xe9: add, 255 on a signed overflow.
The countdown runs down one step per interrupt; at −2 the voice reads its next element on the following interrupt (
0x2cec3–0x2cede). A voice that ends, or every voice on a stop request, is silenced (0x2ceea). When all voices have ended, the sound is over (0x2cf05–0x2cf26). -
Comparative source: REF-SCUMM-023,
PCSpeakerDriver::update. -
Used by: Playing speaker and Tandy programs › How a program plays, Anomalies, tempo floor, frequency division, Anomalies and bugs, Level 4: reference tables › Sounds
0x2cf32, 0x2cf5c, 0x2cf81 — Speaker and Tandy output
- Role: Voice v on, off and frequency. With the
Tandy flag clear they drive the PC speaker: frequency
as the divisor of system timer channel 2 (port
0x42), on and off through bits 0 and 1 of port0x61. With the flag set they write to the Tandy sound chip at port0xc0: a 10-bit divisor for tone channel v, and its attenuation, 15 minus the voice level when on, 15 (silent) when off. - Used by: Playing speaker and Tandy programs › How a program plays
0x2cfb5, 0x2cfd2, 0x2d0c2, 0x2d0d5 — Speaker and Tandy voices
- Role:
0x2cfb5sets the voice count to 3, silences four channels and sets bits 5 and 6 of port0x61;0x2cfd2sets the voice count to 1 and restores port0x61as saved at start-up, speaker off.0x2d0c2calls0x2cfb5;0x2d0d5calls both, in that order. - Used by:
0x2a300, 0x2a314 — Environmental sound
- Role:
0x2a314(sound, block) first plays the sound left pending by the previous request, if any, then keeps the new sound pending in DS:0x5e36when the distance from the party to the block is 15 or less and clears it otherwise (0x2a33b–0x2a35f). The level kept with it in DS:0x5e38is (15 − distance) × 16 (0x2a362–0x2a376); block 0 is not measured and counts as distance 0. It does nothing while DS:0x65fais zero.0x2a300passes the party's block. The game loop calls0x2a300(0)at the end of each pass, which plays any pending sound. - Comparative source: REF-SCUMM-014,
runLoop(snd_processEnvironmentalSoundEffect,snd_updateEnvironmentalSfx). - Used by: The game loop › One pass of the game loop, Obstacles and other monsters › The move check, Play a sound › Behavior, Scripted characters, flags and messages › Messages, dialogue, sound and timing, Values passed on to other routines › The routines, Sound effects, Scripted walls and doors, Level data (INF)
0x10251, 0x1025a, 0x10260, 0x1027a, 0x10293, 0x102aa — Sound data access
- Range:
0x1024c–0x102c5(segment0x1000, the segment of sound driver 1) - Role: Small far routines around the driver's copy of the sound file.
0x10251returns the music marker bytecs:0x250; the introduction and the ending read it.0x1025areturns the far pointer0x1000:0x2c6of the sound data buffer, into which0x2c9f9reads the file.0x102aathen adds0x2c6to the 500 words at the start of the buffer, the program and instrument pointers, so that they become driver offsets.0x10260(program, offset, value)writes a byte of a program,0x1027a(program, offset)reads one, and0x10293(program)returns a program's far pointer. The two words atcs:0x24c(0x2c6,0x2c6) are the program-table bases that driver entry0x1495fselects. - Comparative source: REF-SCUMM-012,
sound/sound_pc_v1.cpp,SoundPC_v1::loadSoundFile. - Used by: AdLib music and sounds (ADL) › Reading the file, Playing AdLib programs › Music markers, The introduction › Music and sounds, The ending › Music and sounds, PC speaker and Tandy sounds (SND)
0x14886 — Queue a program
- Range:
0x14886–0x148a5(driver 1 entry 3) - Role:
0x14886(program)stores the program number in the 16-slot request queue at the write positioncs:0x43f9and advances it by one, modulo 16. The number is stored as a word (0x14895,mov [bx+0x43fd],ax) although the slots are one byte apart, so its high byte goes into the next slot, and from slot 15 into the first byte of channel 0's state (ANOMALY-97). Nothing checks for a full queue: a sixteenth pending request makes the write position equal to the read position tested at0x148b1(ANOMALY-98). - Comparative source: REF-SCUMM-012,
sound/drivers/adlib.cpp,AdLibDriver::startSound. - Used by: Playing AdLib programs › How a program plays, AdLib music and sounds (ADL), Anomalies and bugs
0x1486e, 0x148a6 — Music tick and start the queued programs
- Range:
0x1486e–0x14885,0x148a6–0x14917 - Role: The music tick
0x1486e, run by the timer handler0x146e0every 12th interrupt (about 72.8 times a second), calls0x148a6, then0x14a09, then the beat counter0x15507.0x148a6takes each pending request from the queue, finds the program through the pointer table (cs:0x43de), and reads its channel and priority bytes. If the priority is at least the channel's current one, it clears the channel's state (0x1560f), sets the priority and the command pointer, sets the step counter and the tempo so that the program runs on the next tick, and, except for channel 9, prepares the voice (0x156ae). Channel 0 also clearscs:0x43bc. - Comparative source: REF-SCUMM-012,
sound/drivers/adlib.cpp,AdLibDriver::setupPrograms. - Used by: Playing AdLib programs › How a program plays
0x14a09 — Run the channels
- Range:
0x14a09–0x14aea; command handlers0x14aeb–0x152b3; instrument loader0x15642–0x156ad - Role: Runs only while the driver is enabled (
cs:0x43b9). For channels 9 down to 0, adds the channel tempo to its 8-bit counter (the global tempocs:0x43bbfirst replaces it when tempo reset is set); a carry is a step. When the step counter reaches 0, it reads commands (0x14a76) until one sets a non-zero duration. A byte below0x80is a note followed by its duration; a third byte follows when any of the channel bytes0x32,0x33or0x34is non-zero (0x14a86,0x14a8e: two overlapping word tests; byte0x34holds the last third byte,ANOMALY-100). A byte of0x80or more is masked with0x7fand, up to0x70(0x14ac6), dispatched through the command table; the table ends at index0x49, so bytes0xca–0xf0take handler addresses from the data that follows it (ANOMALY-99); above0x70the channel stops (0x14b8b). Command0xc7(0x1529d) writes its operand to the music marker byte.0x15642writes the 11 bytes of an instrument to the AdLib registers of the channel. The operand lengths of every command are given in the ADL schema. The note routines, operator levels and effect routines are described separately. - Comparative source: REF-SCUMM-012,
sound/drivers/adlib.cpp,AdLibDriver::executePrograms,AdLibDriver::_parserOpcodeTable. - Used by: Playing AdLib programs › How a program plays, Playing AdLib programs › Music markers, The ending, The introduction, Anomalies and bugs, AdLib music and sounds (ADL) › In the original files, How an AdLib note sounds › Effects
0x152b4, 0x15369, 0x15399, 0x153c1 — Note frequency, key on and off, duration
- Range:
0x152b4–0x15368,0x15369–0x15398,0x15399–0x153c0,0x153c1–0x153f8 - Role:
0x152b4turns a note byte into AdLib registers0xa0and0xb0of the channel: the low four bits plus the base note (channel byte0x12) give a semitone, wrapped into 0–11 with a change of octave (0x152c9–0x152da); the high four bits plus the base octave (0x07) give the block; the semitone selects a frequency number from the frequency table, to which the base frequency (0x24) is added. A non-zero pitch bend (0x42) then adds, for an upward bend, the byte at the bend's index in bend table (raw low four bits + 2), or subtracts, for a downward one, the byte at the bend's size in table (raw low four bits) (0x152f9–0x15340); neither index is bounded (ANOMALY-124). The key-on bit is kept.0x15369sets the key-on bit, computes the vibrato step as the frequency number shifted right by 9 − depth and restarts the vibrato delay.0x15399clears the key-on bit, except on channel 9 and, while the rhythm mode is on, on channels 6–8.0x153c1sets the duration: plus a random amount masked by channel byte0x36when that byte is non-zero, and returns; otherwise, when the fraction set by0xa4is non-zero, it also sets the note-off point (0x21) to the duration / 8 times that fraction. The random draw0x157feadds0x9248to the wordcs:0x43c1(seed0x1234) and rotates it right by 3. - Comparative source: REF-SCUMM-012,
AdLibDriver::setupNote,noteOn,noteOff,setupDuration. - Used by: How an AdLib note sounds › How a note sounds, Anomalies and bugs
0x15599, 0x155d1, 0x1556a — Operator levels
- Range:
0x15599–0x155d0,0x155d1–0x1560e,0x1556a–0x15598 - Role:
0x15599computes the level of the channel's second operator: the instrument level (channel byte0x2f, low six bits) plus the level offsets at bytes0x20,0x00and0x30, minus the high byte of the velocity (0x34) shifted left by (0x33+ 1) when0x33is non-zero; the instrument's two high bits are kept.0x155d1does the same for the first operator from byte0x2e, adding the offsets only when the instrument adds both operators (byte0x31), and using0x32for the velocity. Both add in one byte and then keep a value up to 63, set a value from 64 to 127 to 63 and a value from 128 to 255 to 0 (0x155ba–0x155c6,0x155f8–0x15604) (ANOMALY-123).0x1556awrites them to registers0x43and, for an additive instrument,0x40of the channel's operators. - Comparative source: REF-SCUMM-012,
AdLibDriver::calculateOpLevel1,calculateOpLevel2,adjustVolume. - Used by: How an AdLib note sounds › How a note sounds, AdLib music and sounds (ADL), Anomalies and bugs
0x156f7, 0x156f8, 0x15751, 0x1577a — Effect routines
- Range:
0x156f7(oneret),0x156f8–0x15750,0x15751–0x15779,0x1577a–0x157fd - Role: The two effect slots of a channel (bytes
0x29and0x2b) hold the address of one of these routines; run the channels calls both on every tick (0x14ad9–0x14ae0), and a channel reset puts0x156f7, which does nothing, in both. Commands0x95and0x91fill the first slot,0x8dthe second.0x156f8, vibrato: while the delay (0x1f) is non-zero, decrements it. Otherwise adds the rate (0x1d) to a counter (0x1e); on a carry, decrements the half-period count (0x1a), negating the step (0x17) and reloading the count from0x1bwhen it reaches 0, and adds the step to the frequency number, written to registers0xa0and0xb0.0x15751, register cycle: adds the rate (0x39) to a counter (0x3a); on a carry, decrements the index (0x3c), reloading it from0x3bwhen it goes below 0, and writes the table byte at that index (table address0x3e) to register0x3d+ the first operator's offset.0x1577a, slide: adds the rate (0x13) to a counter (0x14); on a carry, adds the signed step (0x15) to the 10-bit frequency number. A rising sum of0x2deor more is halved and the block raised; a falling sum of0x184or less is doubled and the block lowered (the block wraps within 0–7). Then, to avoid a zero number,0x157a9–0x157b4and0x157c5–0x157d0push the number, test its low ten bits, and on 0 pop it, add or subtract 1 and pop a second word: the adjustment is discarded and the stack loses a word (ANOMALY-122). The number and block are written to registers0xa0and0xb0.
- Comparative source: REF-SCUMM-012,
AdLibDriver::primaryEffectSlide,primaryEffectVibrato,secondaryEffect1. - Used by: How an AdLib note sounds › Effects, AdLib music and sounds (ADL), Anomalies and bugs
0x15507 — Beat counter
- Range:
0x15507–0x15525 - Role: Called at the end of every music tick. Adds the global tempo
to the counter
cs:0x43d7; on a carry decrementscs:0x43d6and, at 0, reloads it fromcs:0x43d5and increments the beat countercs:0x43d8. Command0x9csets these counters;0x9dwaits oncs:0x43d8(0x14ca8–0x14cd2, with the phase flagcs:0x43d9). - Used by: How an AdLib note sounds › Effects
0x14918, 0x14948, 0x1495f, 0x1497c, 0x15526, 0x1552b, 0x156ae, 0x1560f — Other driver 1 entries and channel set-up
- Role: Of the far entries listed in the driver's table at
cs:0x41be, the game calls only start-up (0x147f1), shut-down (0x14844) and the queue, each by a direct far call; no instruction reads the table. The others are never called:0x14918(position)sets the pulse rate and channel 0's frequency low byte from the tables chosen by0xbf;0x14948(flag)sets the sample player's running flagcs:0x43f8;0x1495f(n)selects program-table base n fromcs:0x24c;0x1497c(selector)returns the driver segment, the program table, the instrument area0x4baor the table0x58a2;0x15526returns the sample position;0x1552b(channel)returns a channel's remaining step count. Two near routines prepare a channel when a program starts:0x1560fclears bytes 3 to0x42of its state (the level offset at byte 0 survives), then sets the tempo to 255, the priority to 0, the note spacing to 1 and both effect slots to the empty routine;0x156aesets the attack, decay, sustain and release of both operators to0xffand writes 0, then0x20(key on, frequency 0) to register0xb0of the channel, unless the rhythm mode holds channels 6–8. - Used by: How an AdLib note sounds › Effects