SCI Specifications: Chapter 4 - The Sound subsystem

From SCI Wiki
Jump to navigationJump to search

The SCI0 Sound Resource Format

by Ravi Iyengar

Revision 10, Mar. 11, 2002

Preface

Sierra's SCI0 sound resources contain the music and sound effects played during the game. With the introduction of SCI, the company took advantage of new sound hardware which allowed for far better music than the traditional PC speaker could ever create. Sierra chose two devices to specifically target: the MT-32, and the Adlib. The MT-32 is a MIDI synth while the Adlib is a less expensive card based around the OPL2, a non-MIDI chip. Anyone interested in Sierra music and its history can find information at Quest Studios. Music is stored as a series of MIDI events, and the sound resource is basically just a MIDI file. The MIDI standard and device implementations are not covered here in detail, but specifications should be readily available elsewhere.

SCI0 Sound resources can also contain digital samples. The KQ1 SCI remake, SQ3, and Mixed Up Mother Goose (SCI0 version) are a few games that include digital sound effects. [1] These files still contain MIDI data, but the wave data is appended at the end. The MIDI data is an approximation of the sound effect for hardware that can't play digital sound.

Some people prefer the one-based numbering system for channel and program numbers. I personally prefer the zero-based system, and use it here. If you're familiar with channels 1-16, be aware that I will call them 0-15. My intention is not to be deviant from other programs but to be more accurate in representing the way information gets stored. The same is true for programs 0-127 as opposed to 1-128. For whatever reason, convention already holds that controls be numbered 0-127, so nothing in my treatment of them should be abnormal.

Sierra changed its sound file format in the switch to SCI1. I refer only to SCI0 sound files in this specification. Hybrid interpreters such as the one used for Quest for Glory II are also excluded. Finally, SCI games written for non-DOS systems may have different formats. This document applies to Sierra's IBM games.

Sound Devices

A gamers sound hardware greatly affects how music will sound. Devices used by SCI0 can be broken into general categories:

MIDI Synths

These will generally give the best sound quality. MIDI synths are polyphonic with definable instruments through patch files and full support for MIDI controls. The General MIDI standard had not been written when Sierra began writing SCI games, and as far as I know no SCI0 game uses a GM driver or includes a GM track. This means that synths had to be individually supported.

Non-MIDI Synths

cards. Synths are polyphonic with definable instruments through patch files, but drivers must be written to interpret MIDI events and turn them into commands the hardware will recognize. Support for most sound controls gets lost in the process. Furthermore, drivers must map logical, polyphonic MIDI channels to physical, monophonic hardware channels. A control (4Bh) was introduced for this purpose and will be discussed later.

Beepers

Beepers produce very poor music and don't support instrument definitions, but all PC users have one so supporting them covers people without special sound hardware. The most common device is the PC speaker, which is monophonic. Another is the Tandy speaker with 3 channels. Drivers must interpret MIDI events, but need only concern themselves with basic functionality. Interpreting the MIDI events is also made easier because each channel is monophonic. To play a chord on the Tandy, for example, each voice must be put in a separate MIDI channel.

Wave Devices

Wave devices play digital sound data. Sierra's versions of SCI never used them to play music, but they could be used in conjunction with one of the above devices to add special sound effects to a game.

With such a diverse group of devices to support, Sierra put a lot of the work on the shoulders of the drivers. Functions for loading patch files, handling events, pausing, etc. are all in the drivers. The interpreter calls them as needed but does not concern itself at all with how they get implemented.

Listed here are devices supported by the SCI0 interpreter with a little information about each. There could very well be other hardware not listed here, so please send in any missing information.

Device Name Driver Patch Poly Flag
Roland MT-32 mt32 001 32 01h
AdLib adl 003 9 04h
PC Speaker std 1 20h
Tandy 1000 or PCJr jr 3 10h
Tandy 1000 SL, TL tandy 3 10h
IBM Music Feature imf 002 8
Yamaha FB-01 fb01 002 8 02h
CMS or Game Blaster cms 101 12 04h
Casio MT540 or CT460 mt540 004 10 08h
Casio CSM-1 007
Roland D110, Roland D10, Roland D20 000

(thanks to Shane T. for providing some of this). Blank fields are unknown, not unused.

∗ when asked which patch to load, the PC and Tandy speaker drivers return FFFFh, which is a signal that they do not use patches
† the imf driver almost certainly uses 02h for the play flag, but I haven't confirmed this

The driver column holds the file name of each driver without the .drv extension. The patch column specifies which patch resource each driver requests. The poly column is the maximum number of voices which can be played at once according to the driver. The flag column gives each device's play flag. Play flags, explained in the header section, determine which channels a device will play.

File Format

Sound files follow the same format as all extracted SCI0 resources. The first two bytes of the file contain a magic number identifying the resource type. The rest of the file contains a dump of the uncompressed data. The identifier is the resource type (04h for sound) OR-ed with 80h and stored as a word. The result will be 84h 00h in extracted sound files.

The sound resource data itself is a header with channel initialization followed by a series of MIDI events.

Header

The header provides the sound driver with 2 pieces of information about each channel. The first is a byte which specifies how many voices each logical MIDI channel will be playing. For MIDI synths, this information is not really necessary and is probably ignored. The same goes for beepers. This byte is only useful for non-MIDI synths which must know how many hardware channels each logical MIDI channel will need. This value is only an initial setting. Sound files can request changes to the mapping later with control changes. Requesting more hardware channels than are actually available can cause errors on some drivers.

The second byte describes how the user's sound hardware should treat the channel. It is the combination of bit flags which may be OR-ed together. If the appropriate bit is set for the currently selected sound device, the channel will be played. If it is not, the channel will be silent. The driver decides which bit it will use as the play flag, and the table under Sound Devices lists the flag used by each driver. Drivers ignore the first byte (used to request hardware channels) on MIDI channels they don't play.

The MT-32 always plays channel 9, the MIDI percussion channel, regardless of whether or not the channel is flagged for the device. Other MIDI devices may also do this.

A byte at the beginning of the file, before channel initialization, specifies whether the resource contains a digital sample or not. A value of 0 means that there is only MIDI data. A value of 2 means that there is a digital sample at the end of the file. In this case, only the first 15 MIDI channels have header bytes. The two header bytes for the last channel is replaced with an offset to the digital sound effect. The offset is stored in big-endian order in the resource. If present, it points to the last byte before the digital sample header. If the offset is 0, the file must be searched for the status FCh, and the digital sample header will come next. There may be two FCh bytes in a row, in which case both will come before the digital sample header. The digital sample header is discussed in more detail in the digital sample section.

The header format:

1 byte - digital sample flag (0 or 2)
2 bytes - initialization for channel 0
2 bytes - initialization for channel 1
.
.
.
2 bytes - initialization for channel 15 OR offset to digital sample

The header is always 33 bytes long.

Events

The actual music is stored in a series of events. The generic form for an event is:

<delta time> [byte - status] [byte - p1 [p2]]

Delta time is the number of ticks to wait after executing the previous event before executing this event. Ticks occur at 60 Hz. The delta time value is usually a single byte. However, longer delays can be produced by using F8h any number of times before the delta time value. Each F8h byte causes a delay of 240 ticks before continuing playback. For example, the sequence F8 F8 78 FC waits 600 ticks then stops the sequence because of the FCh status. The fact that F8h waits F0h ticks makes me think that E9h is the largest technically allowable delta time.

The delta time must be present in most events. The only exception is when FCh is the status, because FCh is a real-time message. Sierra's resources seem to have always provided a delta time, though. Note also that FCh cannot be used as a delta time value - it will be interpreted as a stop sequence status.

The status byte is basically a command. The most significant bit is always set. This feature is important because the status byte will not always be present. A missing status byte is known as running status mode and the last status gets repeated with the new parameters. Parameters will never have their most significant bits set.

The generic form for a status byte is (in bits) 1xxxcccc. The lower nibble usually specifies a channel. The upper specifies a status.

Status Reference

8x n v

Note off: Stop playing note n on channel x, releasing the key with velocity v. If a hold pedal is pressed, the note will continue to play after this status is received and end when the pedal is released.

9x n v

Note on: Play note n on with velocity v on channel x. Playing a note with velocity 0 is a way of turning the note off.

Ax n p

Key pressure (after-touch): Set key pressure to p for note n on channel x. This is to modify key pressure for a note that is already playing.

Bx c s

Control: Set control c to s on channel x. This can be confusing because there isn't just one meaning. Changing the settings on different controls will, of course, have different outcomes.
Controls which handle any value are continuous controllers. They have a continuous range. Controls which are only on/off are switches. Their defined values are 01h (OFF) and 7Fh (ON).
Listed in this reference are the non-standard MIDI controls I've found in Sierra SCI0 sound files. Standard controls are not listed here. Not all drivers support all controls.
Control Reference
4Bh
Channel mapping: When a channel sets this control, it tells the driver how many notes it will be playing at once, and therefore how many hardware channels it occupies.
4Ch
Reset on PauseSound: An on/off switch where a value of zero is off and a non-zero value is on. Note that this is not the same as for standard MIDI control switches. When this control is on, calling the sound driver's PauseSound subfunction will reset the sound position to the beginning. The initial value is set to off when a sound gets loaded.
4Eh
Unknown: Experiments in setting and clearing it show that a value of 0 will cause notes to be played without regard for the velocity parameter while a value of 1 will enable velocities.
50h
Reverb: I know little about this myself. Rickard Lind reports that it exists in the MT-32 driver and supports parameter values 0-10 (possibly 0-16?).
60h
Cumulative cue: The interpreter can get cues from the sound file, which sets the Sound object's signal property. When a sound gets loaded, the initial cue is set to 127. When a CC60 occurs, the new control value is added to the current cue. If the cue were 130, for example, a CC60 5 on any channel would make the new cumulative cue equal 135.

Cx p

Program change: Set program (patch / instrument / ect.) to p for channel x. This is a simple instrument change.
Channel 15, however, includes two special cases of this status. The first relates to communication with the game interpreter. If p is less than 127 then the signal property for the game interpreter's Sound object gets set to p, triggering a non-cumulative cue.
If p is equal to 127, then the current position within the sound resource is remembered as the loop point. Normally the driver loops to the beginning of the sound when the sequence ends. If an explicit loop point is set, the sound will be replayed from the marked point instead.
The actual time of the loop point is better explained with a short diagram:
0x10 0x91 0x20 0x20 play a note on channel 1
0x05 0x91 0x20 0x00 stop the previous note
0x00 0x92 0x30 0x10 play a note on channel 2
[restart here]
0x00 0xCF 0x7F set loop point
0x00 0xC8 0x05 change to program 5 on channel 8
0x00 0xCF 0x13 set signal to 19
0x20 0xFC end of file, loop to marked location
In both situations (p < 127 and p = 127), no actual program change takes place. Channel 15 is used for control, not playing music.

Dx p

Pressure (after-touch): Set key pressure to p on channel x. This is similar to Ax but differs in its scope. Message Ax is applied on a per-note basis while message Dx is applied to an entire channel.

Ex t b

Pitch wheel: Set the pitch wheel to tb. The setting is actually a 14 bit number with the least significant 7 bits stored in b and the most significant 7 bits stored in t. The range of values is 0000h to 3FFFh. A value of 2000h means that the pitch wheel is centered. Larger values raise pitch and smaller values lower it.

F0

Begin SysEx: Starts a system exclusive data block. The block must terminate with F7h.

F7

End SysEx: Ends a system exclusive data block. Normal sound data resumes at this point.

FC

Stop Sequence: This is a system real-time message which tells the sound driver to stop the current sound. The sound object's signal property gets set to FFFFh and the position moves to the loop point, which defaults to the beginning. Drivers allow this message to occur without a delta time, but I haven't seen any examples.

Digital Samples

The digital sample header is 44 bytes long. Offset 14 in the header contains the frequency as a short integer. Offset 32 contains the sample length, also as a short integer. Other fields in the header are unknown (to me) at the time of writing, but aren't critical to playback.

The wave data comes immediately after the header, stored in unsigned 8 bit PCM format.

Mapping instruments in FreeSCI

The Patch.002 resource

As Ravi describes in his description of the patch resources (which have not been included here), one of the major problems with SCI sound support is the lack of General Midi (GM) support in the earlier games. Since those were written before GM was conceived, this can hardly be considered to be Sierra's fault; but this fact doesn't help when it comes to supporting the games in a portable manner.

Unfortunately, almost every SCI0 game uses an individual instrument mapping scheme. This means that there are only two options to generate GM music from the original SCI sound resources: Either create a manual mapping for each game, or abuse existing data from the game for this purpose. Obviously, the latter way would be either impossible or much easier.

So, the solution would be to use an existing instrument mapping scheme. Those mapping schemes are stored in the patch resources, and, as such, easily accessible to an SCI engine. As those patch files are driver dependent (which, in turn, are hardware dependent), most of the patch data is unusable. The Adlib data, for example, will only work for an OPL-2 FM synthesizer chip or one of its successors, the MT-32 data (which consists of one massive sysex block) won't help anyone without an MT-32 or LAPC-1, and so on. So, to recycle this hardware-dependent data, two new possibilities remain: Either extract and interpret the patch data using a portable software synthesizer (such as timidity), or extract instrument names and map those to GM instruments. The first approach would, of course, yield the better results (at the cost of computation power); but the only software emulator for a specific sound system I've seen so far was an OPL-2 emulator. So the alternative, extracting a text ID of each instrument and using it to map this instrument to a GM instrument, looks much more promising.

Now, most SCI0 games come with a patch.002 resource, which is used by the IBM Music Feature card and Yamaha FM-01 sound synthesizers (both of which appear to use frequency modulation). This is the only patch file that includes text descriptions of most of its instruments. Note this, not all instruments have name representation. This means that some of them can't be mapped and have to be silenced; but those instruments are either used for sound effects only or not used at all, so this isn't critical.

Using those 7-letter instrument names, it is now possible to build a small database of instruments, which, subsequently, can be mapped to GM instruments.

The file structure is relatively simple (for this purpose): Every patch.002 consists of either one or two instrument banks carrying 48 instruments each. Every instrument has a fixed block size of 0x40 bytes; each block starts with the 7-letter description of the instrument or seven blanks if none is available.

If two banks are present, the second bank is separated from the first one by a two-byte sequence (0xab, 0xcd). Keeping this in mind, it is trivial to extract the instrument names of the 48 or 96 instruments.

Percussion instruments

Percussion instruments are treated specially in the MIDI standard. MIDI channel 10 (or 9, if you count from 0 to 15 like most people do) is reserved for percussion instruments and some special effects; each key for this channel represents either nothing or one fixed percussion instrument.

At first glance, this might lead to an additional problem of mapping those percussion instruments. Fortunately, the General Midi standard extends on the MT-32 percussion mappings, which are used in SCI0, so that channel 9 can be left completely untouched in the process of instrument mapping.

Revision history

Revision 10 - Mar. 11, 2002

  • Added section on digital samples (thanks to the FreeSCI developers, Rickard Lind especially)
  • Added wave devices to the hardware category list
  • Updated header section to cover the header for PCM resources
  • Added more play flags to the sound driver table
  • Fixed a typo in the sound driver table where I accidentally called the "Yamaha FB-01" the "Yamaha FM-01"

Revision 9 - Jul. 4, 2001

  • Changed StopSound to PauseSound for control 4Ch
  • Updated URL for SCI messageboard
  • Added web links for more SCI information[Note 1]
  • Did a little proofreading and editing

Revision 8 - Dec. 21, 2000

  • Added suggested limit on delta time values
  • Fixed hex notation (sometimes listed NNh, sometimes 0xNN)
  • Removed notice about early revisions' mistake describing the header's channel mapping byte
  • Added note about control 50h (thanks to Rickard Lind)
  • Listed MT-32 play flag
  • Added notice about the special case of channel 9 to the header section

Revision 7 - Jan. 7, 2000

  • Added information about F8h delta times (thanks to Rickard Lind for bringing these to my attention)
  • Reorganized Fx status information
  • Fixed major error in description of loop points (sorry)
  • Fixed typos

Revision 6 - Sep. 17, 1999

  • Added information about cues
  • Updated control 60h information
  • Added information about loop points
  • Updated control 4Ch information
  • Cleaned up control reference introduction

Revision 5 - Jul. 5, 1999

  • Rewrote much of the specification, trying to focus less on explaining MIDI and more on explaining sound resources
  • Removed information about standard MIDI controls
  • Added driver table
  • Expanded sound device section
  • Completed header information

Revision 4 - Jun. 19, 1999

  • Fixed the list of changes in Revision 3 (was incomplete)
  • Expanded the introductory blurb about controls
  • I began working with a disassembly of ADL.DRV, and am hoping to use it to complete this specification. The next revision should be more interesting than this one.

Revision 3 - May 4, 1999

  • Removed the "compatible games" list. I haven't found a non-compatible SCI0 game yet, which makes the list quite useless.
  • Verified that SCI1 sound resources are different.
  • Tidied the "About the output medium" section. Does that term "output medium" sound wordy or unclear? I don't really like it, but I didn't want to beat "sound device" to death.
  • More information about the header
  • Modified the explanation for message FCh.
  • Changed most references to status bytes as "commands" with "messges" to stay more consistent with MIDI terminology.
  • Added midi.org as a source for more MIDI information
  • Removed labels like "tentative" and "incomplete" as things become more concrete -- not complete yet, but getting there.
  • More information about controls

Revision 2 - Jan. 16, 1999

  • Got rid of the HTML. I originally intended to post this as a message on the webboard, but ended up distributing the file. If I'm going to distribute it as a file, there's no need to bother with the HTML since I can do all my formatting as plain text.
  • I found references to command 8x in the 1988 Christmas Card, so my comment about not seeing one got removed. To date, I haven't seen any examples of commands Ax or Dx.
  • Expanded the header section.
  • Added information about controls.
  • Added information about the output mediums.
  • Tried to be more consistent with terminology

Revision 1 - Dec. 29, 1998

  • First release of the specification

 

Notes
  1. Editor's note: These are not included in the FreeSCI documentation version

 

References
  1. MusicallyInspired 15:50, 3 December 2010 (PST)

 

< Prev.: Chapter 3 - The Graphics subsystemNext: Chapter 5 - The SCI Virtual Machine >