Scene sound (.SND, .WHD, .WAV)¶
A scene's sound is three files. The .SND member of the scene archive is the packed sound graph: every sound, wave, set, sequence, group, room effect, material property and controller record of the scene. Beside the archive on the disc are two loose files, <scene>.WHD (the wave header table) and <scene>.WAV (the wave bank, the scene's sound effects as PS2 SPU ADPCM). Long dialogue and music are not in the bank: their WHD records point into the disc-wide stream file PS2_Eng.str, whose name the SND carries.
The EE reads only the SND. InstallWaveHeaders and InstallWaves pass the WHD and WAV file names to the IOP synthesizer (SYNTHCD.IRX), and every start-sound command names a WHD record by its byte offset. The IOP module is not decompiled, so the WHD fields below are read from the data and from the port's mixer, which plays the files the way the IOP does (port/engine/audio/soundengine/_sdl/soundsdl.cpp, FindOrDecodeWave and DecodeSpuAdpcm).
tools/port/scene/snd.py parses and rewrites all three (SND: 71/71, WHD: 71/71, WAV: 71/71 identical; also in roundtrip.py). It encodes 16-bit PCM to SPU ADPCM and appends a new sound to all three files (add_sound). The corpus is build/scene-corpus/<ARCHIVE>/Scenes/**.SND with _disc.WHD and _disc.WAV in each archive directory.
See Scene archives for corpus extraction and verification.
Loading¶
ZEngineDataBase::AllocSequencereads the SND withGetSoundDatawhen a sound DLL exists. The call toInstallSoundstakes place before the scene geoms are created. The packed data stays in memory for the scene.- Then
GetStreamFilename(sound.cpp:215) reads the stream file name (PS2_Eng.strin all 71) throughSSoundDataInfo::m_StreamFilenameOffset, andInstallStreamWaves,InstallWaveHeaders(CalcCacheFileName("whd")) andInstallWaves("wav") queueSYNTH_CMD_LOAD_STREAM_WAVE,SYNTH_CMD_LOAD_WAVEandSYNTH_CMD_LOADwith each file's name and size (sound.cpp:155to 211).InstallWaveswaits for the IOP. - At run time a
ZSoundObjectholds a packed index (m_rSound).GetPlayableSound(sound.cpp:579) resolves sets to one of their entries.GetWave(sound.cpp:524) follows a Sound record to its Wave record.AddSourceCommandGeneric(sound.cpp:1436) copiesSWave::m_lHeaderOffset & ~1intoSStartSoundBase::m_lHeaderOffsetand skips the command when that is 0xCDCDCDCC or 0xCDCDCDCD (WAVE_HEADER_UNSET). Looping is the command'sm_bLooping(from the sound object), not a property of the wave data.
Common header¶
All three files start with 16 bytes: a word, the file size, then 3 and 4 (the same in all 213 files).
| File | Word 0 | Word 1 | Words 2, 3 |
|---|---|---|---|
.SND |
offset of SSoundDataInfo (ZPackedSoundFile::m_lSoundDataInfoOffset) |
file size | 3, 4 |
.WHD |
file size − 16 | file size | 3, 4 |
.WAV |
0 | file size | 3, 4 |
.SND: the packed sound graph¶
Every reference inside the SND is a byte offset from the file start, called a packed index. GetPackedDataFromIndex (sound.cpp:443) accepts an index from 1 to the data size. GetPackedObject returns the record if its m_Type is below 25, GetPackedController if below 0x21. Nothing is relocated at load, so a record keeps its offset for its whole life, and other files name records by that offset (see Cross-references).
Info block (SSoundDataInfo, 0x1dc bytes)¶
In all 71 files the info starts 480 bytes before EOF. The 0x1dc-byte SSoundDataInfo includes m_Controllers after its 0xd4-byte prefix. Four zero bytes follow it.
| Offset | Field | Meaning |
|---|---|---|
| 0x00 | m_lMappingMatrix |
offset of an n×n uint32 matrix of packed indices, n = number of SurfaceProperty plus ObjectProperty records; GetMapping (sound.cpp:487) reads entry [min(id1,id2)−1][max(id1,id2)−1] by m_lMaterialId (impact sounds between two materials) |
| 0x04 | m_StreamFilenameOffset |
offset of "PS2_Eng.str"; 0x10 in every file |
| 0x08 | m_DefaultGroup |
packed index of the default Group (zsndobj.cpp:364) |
| 0x0c | m_TypeCount[25] |
records of each ZAudioTypes::EType |
| 0x70 | m_TypeOffset[25] |
offset of each type's table of m_TypeCount uint32 record offsets (GetPackedObjectTypeAt, sound.cpp:478) |
| 0xd4 | m_Controllers |
33 entries indexed by ZAudioControllerTypes::EType, each holding a signed count and an offset |
Record counts over the 71 files: Sound 80,535, Wave 59,355, Seq 3,984, Set 3,360, SurfaceProperty 1,278, Def 918, RoomEffect 828, Group 671, ObjectProperty 611. The other 16 types have no records.
Records¶
Every record starts with ZPackedBase (audiotype.h, 0x14 bytes). Records are 16-byte aligned, and the compiler-fill byte 0xCD (MSVC's uninitialised heap) appears in their padding.
| Offset | Field | Meaning |
|---|---|---|
| 0x00 | m_Type |
ZAudioTypes::EType |
| 0x04 | m_Filler1 (u8) |
Wave: the WHD record's word 0 (equal in all 11,458 typed banked waves with headers); 0xCD in other records |
| 0x08 | m_ResourceNameOffset |
offset of the record's name, or 0; only read for debug output (GetResourceName) |
| 0x0c | m_lControllerCount (u16) |
number of controllers. On a Wave it is a flag word: bit 0 is SWave::IsStreamed (1 in all 46,744 streamed waves, 0 in the 11,458 banked ones) |
| 0x10 | m_lControllerOffset |
offset of m_lControllerCount uint32 controller offsets (GetControllerAtIndex) |
Type-specific fields, from src/audiolib/audiotypes/audiotype_*.h:
- Sound (type 1,
audiotype_sound.h, 0x2c bytes):m_lWaveOffset0x14 (the Wave it plays),m_fMinDist0x18,m_lPitch0x1c,m_InnerConeAngle0x1e,m_OuterConeAngle0x20,m_OuterConeVolume0x22,m_LPCutOffFreq0x24 (allint16),m_SourceType0x26 (ZAudioTypesvalue; most commonly 0 in 68,635, 6 in 1,033, 4 in 648),m_Volume0x27 (signed),m_lFlags0x28,m_iMaxDistanceModel0x29,m_lLoops0x2a (0 in 70,125, 1 in 559). Other source types include 2 and 5. Loop counts include 10 and 18. Group records occupy 0x4c bytes and Def records occupy 0x1c bytes, including their entries reference. - Wave (type 4,
audiotype_wave.h, 0x1c bytes):m_lHeaderOffset0x14 (WHD record offset; bit 0 isWAVE_FLAG_MONO, clear on the whole disc),m_fDuration0x18 (seconds,lSamples / lRateof the WHD record). 949 waves have header offset 0. - Set (type 2, 0x24 bytes):
m_Entries0x14 (TAudioTypeList: entry offsetuint32, count and capacityuint16),m_PlayedBitTable0x1c,m_PlayType0x20 (Linear 0, Random 1, Shuffle 2). The entries are packed indices of playable records. - Seq (type 3):
m_Keys0x14 (aTAudioTypeListof 12-byteSSequenceKey: type, time in ms, sound offset),m_MasterVolume0x1c,m_lLoops0x1d. - ObjectProperty (18):
m_lMaterialId0x14,m_lSize0x18. SurfaceProperty (19):m_lMaterialId0x14,m_lOcclusion0x18,m_fOcclusionLFRatio0x1c,m_fOcclusionRoomRatio0x20.
Two quirks of the type tables: 204 Wave-table entries (long music tracks) and all 828 RoomEffect entries point at records whose m_Type is 0. The music records are otherwise ordinary waves (streamed flag, a WHD offset, a duration of minutes), and Sound records name them. GetWave follows a Sound's m_lWaveOffset without testing the target's type, so they still play. The RoomEffect records are all zero bytes.
Writing¶
snd.parse decodes the header, the info block, every type table and the named fields of each record they list, and keeps the file image. snd.write packs those fields back over the image at their offsets. Since everything is addressed by offset and records may be shared, a new record is appended at the end and the old ones never move. New type tables are appended too, and m_TypeOffset/m_TypeCount point at them; the old tables stay as unreferenced bytes. check validates every table entry and every Sound's wave link.
.WHD: wave header records¶
The WHD has a 16-byte header followed by records of varying lengths. Of the 58,202 typed Wave entries with headers, 46,744 stream and 11,458 use a bank. The banked entries comprise 11,457 SPU ADPCM and one format-6 wave. Read a Wave’s record at m_lHeaderOffset. Most records occupy 0x40 bytes. Some music entries pair a stereo record (ID 0, flags 0x8280) with a mono alternative at +0x34 (flags 0x180).
Field names below follow WHD_FIELDS in snd.py. The mixer calls the word at 0x00 lFlags and leaves the tail at 0x28–0x3f unnamed.
| Offset | Field | Banked SPU entries (11,457) | Streamed entries (46,744) |
|---|---|---|---|
| 0x00 | lId |
equals SWave::m_Filler1 (small numbers, 0x0c to 0x46 in M00) |
0x4f40c0 |
| 0x04 | lEntry |
0 | 0 |
| 0x08 | wFormat |
0x70: SPU ADPCM | 6 (8-bit, mu-law in the port's decoder) or 1 (PCM). Three streamed entries use 0x80. |
| 0x0a | wDataInStreams |
0 | bit 7 set: the data is a byte range of PS2_Eng.str |
| 0x0c | lRate |
Sample rate in Hz, up to 22,050. Pitch-adjusted M00 records reach down to 4,640. | Usually 16,000 for 8-bit voice and 32,000 for 16-bit PCM. Read the rate in each record. |
| 0x10 | lBits |
4 | 8 or 16 |
| 0x14 | lPcmSize |
lSamples × 2 × lChannels |
|
| 0x18 | lSize |
ADPCM bytes in the bank, including one 16-byte lead block per channel | bytes in the stream |
| 0x1c | lChannels |
1 | 1 or 2 |
| 0x20 | lOffset |
byte offset in the .WAV file (counting its header) |
byte offset in PS2_Eng.str |
| 0x24 | lSamples |
samples per channel | |
| 0x28 | lStamp0, lStamp1 |
two words shared by many records (for example 0x405acb, 0x1d3bb28); not used by the port | |
| 0x30 | lExtra0..3 |
0 | small values (0x200004 and similar) in many records; unknown |
The port's mod overrides name a replacement by this record offset: assets/sounds/<scene>/<offset as six hex digits>.wav.
.WAV: the wave bank¶
After the header the bank is the concatenation of SPU ADPCM entries. Records may share data (6,701 bank records overlap another one), and there are no gaps between the others. Each banked entry is:
- one 16-byte lead block of zeros (counted in
lSize; the port skips it); ceil(lSamples / 28)blocks of 16 bytes: byte 0 isfilter << 4 | shift, byte 1 the flags, bytes 2 to 15 the 28 samples as 4-bit two's complement, low nibble first. A sample decodes to((nibble << 12) >> shift) + ((p1·c1 + p2·c2) >> 6), clamped to 16 bits, with (c1, c2) = (0, 0), (60, 0), (115, −52), (98, −55), (122, −60) for filters 0 to 4;- every block's flag byte is 0x02 and the last block's 0x03 (all 11,191 banked SPU records on the 0x40 grid). Since one-shots and loops look the same, the IOP must stop or repeat a voice from the start command's loop flag.
Every banked SPU entry that a Wave record names starts with the zero lead block.
Writing a new sound¶
snd.encode_adpcm(samples) writes the lead block and the blocks above. For each block it tries the five filters and 13 shifts against the decoder's reconstructed history and keeps the one with the least squared error. Re-encoding the decoded samples of a disc sound reproduces them exactly: for 60 M00 sounds all samples match, and 53 of the 60 byte streams are identical (the other seven pick an equivalent filter and shift).
snd.add_sound(snd, whd, bank, samples, rate, name):
- appends the ADPCM data to the bank at a 16-byte boundary;
- appends a WHD record before the zero tail. It copies a template banked record (
lId,lStamp*) and setslRate,lPcmSize,lSize,lChannels= 1,lOffsetandlSamples; - appends to the SND a name string, a Wave record (template
m_Filler1,m_lHeaderOffset= the new record,m_fDuration= samples / rate) and a Sound record. The Sound copies the template Sound's fields, setsm_lWaveOffsetand has no controllers. It then appends new Wave and Sound type tables; - returns the Sound's packed index. That is the value a geom property, a LOC subtitle or a script uses to play it.
read_pcm_wav(path, rate) reads a 16-bit PCM WAV file, averages stereo to mono and resamples linearly. selftest adds a tone or a given WAV to a real scene. It checks that the existing bytes of all three files are unchanged, re-parses them, and decodes the new sound with the port's algorithm: a 440 Hz + 1,250 Hz tone at 22,050 Hz comes back with an SNR of 49.7 dB (peak error 119), and a stereo 44.1 kHz file resampled to 22,050 Hz with 55.9 dB.
Cross-references¶
| From | Field | To |
|---|---|---|
.SND Wave |
m_lHeaderOffset |
.WHD record offset |
.WHD banked record |
lOffset |
.WAV byte offset |
.WHD streamed record |
lOffset |
PS2_Eng.str (named by the SND at 0x10) |
.LOC string |
GetSoundResourceId word |
.SND Sound record (all 43,620 non-zero ids on the disc) |
.ANM Animation::Header |
m_SoundIndex |
.SND Seq (3,671) or Set (one M10 roulette animation) |
.PRP sound object properties, scripts |
packed index | .SND records (see prp.md) |
.SGP |
room graph | which sound sources are audible (sgp.md) |
A minimal new scene¶
- An SND is optional: without it
GetSoundDataSizereturns −1,InstallSoundsis skipped andGetPlayableSoundfinds nothing. Whether the IOP accepts WHD and WAV files of only their 16 header bytes is untested. - To play sounds: an SND with a valid info block (the stream name at 0x10 even if nothing streams), the Sound and Wave records, their type tables, and a Group as
m_DefaultGroup(copy one from a scene). Copying a whole scene's three files and appending new sounds withadd_soundis the simplest route: the scene's existing Sound indices then stay valid for the geoms and LOC strings copied with it. - A WHD record per wave and the data in the WAV. The disc's banked rates lie at or below 22,050 Hz.
PC version¶
The Steam release keeps the same three files per scene: the .SND member in Scenes/**/<scene>.ZIP (68 archives) and the loose <scene>.WHD and <scene>.WAV beside it, plus one stream file, pc_eng.str, in the install's root. The port plays them unchanged: the engine reads the PC .SND as it is, and the sound DLL reads the PC .WHD, .WAV and pc_eng.str itself (port/engine/system/_sdl/pcdata/pcsound.{h,cpp}, port/engine/audio/soundengine/_sdl/soundsdl.cpp and pcstreamsdl.cpp). Nothing is converted to the PS2 layout. The PC survey covered 68 scenes. The decoder checks are in tools/port/tests/pc_sound_test.cpp.
.SND: the PS2 graph with its names¶
The PC .SND is the PS2 container with every record's resource name kept. The 68 files hold 27.1 MB against the PS2's 7.8 MB, and the extra bytes are names. In all 68 files the header is the PS2 one (info block offset, size, 3, 4), the info block (SSoundDataInfo) starts 480 bytes before the end of the file, and the 25 type tables use the same layouts. 147,409 of the 147,973 records name themselves through m_ResourceNameOffset, with strings such as audio:action/wipe_table/wipe_table_01#wave and audio:animations/resources/m10/.../m10_player_get_card_00#sound, stored just before the record. The PS2 build stores 0 there. The stream file name at 0x10 is pc_Eng.str instead of PS2_Eng.str. The quirks carry over: 204 Wave-table entries and all 828 RoomEffect-table entries point at records whose m_Type is 0.
Records were paired by their position in the type tables, and the typed fields of snd.py were compared. Offset-valued fields were compared through the pairing. 833,037 fields are equal, and so are 51,001 offsets, the controller types of 63,024 records and the default group of every file. The differences:
m_fDurationof 44,864 Wave records differs slightly, because the PC re-encoded the waves at other sample rates (0.11991 s against 0.11988 s). Each PC duration is the PC record's frames divided by its rate.- Some content was tuned:
Sound.m_Volumein 55 records,m_lControllerCountin 52 (one more controller on the PC), andm_iMaxDistanceModel,m_lFlags,m_lLoopsandm_fMinDistin a handful. - 11 files have one to seven more or fewer Sound, Wave or Set records (M05_main 2,893 against 2,900 Sounds, for example). The other 57 have the same counts.
- The PS2 build merges identical records. 11,623 PS2 table entries share a record that the PC keeps once per entry, so the two files cannot be mapped offset for offset.
The PC .SND must be used with the PC scene's other members, and the engine reads it unchanged. ZPackedData and GetPackedObject only follow offsets, the layouts are the PS2 ones, and the names are only read for debug output. The other members of a PC scene name SND records by their PC offsets:
| Reference | Into the PC .SND |
The same values against the PS2 .SND |
|---|---|---|
.ANM m_SoundIndex (3,672 non-zero) |
3,671 Seq records. The one exception, M10_Roulette#Roulette_Player_Win_1, names a Set on both discs |
3,615 are not Seq records |
.LOC sound ids (42,188) |
42,188 Sound records | 41,500 are not Sound records |
.PRP ZAUDIOREF properties (9,598) |
all packed objects (Sound 4,133, ObjectProperty 3,828, Base 835, Set 753, Seq 30, Group 17, Wave 2) |
Converting the .SND to the PS2 layout would therefore break every animation, subtitle and geometry sound reference. The level scripts in src/scripts hold no packed indices: they pass on what the engine gives them (Getsecuritydeviceloopingsound, script variables loaded from the PRP).
.WHD: names in front of PS2 records¶
The header is the PS2 one (size − 16, size, 3, 4), and the file ends in 16 zero bytes, in all 68 files. Each wave is a NUL-terminated source name padded to 16 bytes, followed by the 0x40-byte PS2 record. The record's word at 0x04 (lEntry, 0 on the PS2) is the offset of that name:
0x10 "Action/Wipe_Table_01.wav\0" ... (to 0x40)
0x40 23 00 00 00 10 00 00 00 11 00 00 00 b6 3e 00 00 lId, name offset, format 0x11 / flags 0, rate 16054
0x50 04 00 00 00 ca 7b 00 00 00 20 00 00 01 00 00 00 bits 4, PCM size, size 0x2000, channels 1
0x60 10 00 00 00 e5 3d 00 00 00 04 00 00 f9 07 00 00 offset 0x10 in the .WAV, 15,845 samples, block 0x400, 2041 per block
0x70 0 0 0 0
The SND Wave's m_lHeaderOffset points at the record, not at the name (0x40 here). The fields keep their PS2 meaning. The PC has its own format tags and uses the two PS2 "stamp" words:
| Records (56,864 distinct) | wFormat |
wDataInStreams |
Notes |
|---|---|---|---|
| 10,856 banked | 0x11, Microsoft IMA ADPCM | 0 | mono, lBits 4. Word 0x28 is the block size (0x400) and 0x2c the samples per block (2041). The data is a whole number of 0x400-byte blocks at a 16-byte aligned offset of the .WAV |
| 460 banked | 1, PCM | 0 | mono 16-bit, mostly the PC's 44.1 kHz weapon shots |
| 44,450 streamed | 0x11 | 0x80 | lOffset is the entry's offset in pc_eng.str |
| 617 streamed | 1 | 0x80 | 600 stereo, 17 mono |
| 277 streamed | 0x1000, Ogg Vorbis | 0x80 | stereo, lBits 0 |
| 204 streamed | 1 | 0x8280, 0x180 | composite music records: a stereo track (flags 0x8280, lId 0) and its mono alternative at +0x34 (flags 0x180), with both names before the pair. Word 0x38 of the first record is the offset of the second name. These are the PS2's 52- and 60-byte records |
A banked record's lId is below 0x100 and equals the Wave's m_Filler1 for all 11,327 banked waves, as on the PS2. A streamed record's lId is 0x4f4850 ("PHO"), where the PS2 has 0x4f40c0. 102 records, the mono alternatives, sit 4 bytes off the 16-byte grid. For a stereo record, lSamples counts both channels, on the PC and on the PS2. The SND's m_fDuration is frames divided by rate.
Banked rates run from 8,000 to 44,100 Hz (44,100 for 2,479 records, 22,050 for 2,283, and many pitch-adjusted rates in between). PS2 rates stay at or below 22,050 Hz.
.WAV: IMA ADPCM and PCM¶
The header is the PS2 one (0, size, 3, 4). The entries are back to back from 0x10, and there is no lead block. An IMA ADPCM block, mono, is 0x400 bytes. It starts with the first sample (int16), the step index (byte) and a reserved byte. Its 1,020 data bytes hold two 4-bit codes each, low nibble first, which makes 2,041 samples. The last block is padded, and the record's lSamples ends the wave. This is WAVE_FORMAT_IMA_ADPCM exactly. PcSoundDecodeBankedWave decodes 2,000 of the banked waves (26.8 million samples, one in five of the corpus) identically to FFmpeg's adpcm_ima_wav, fed the same bytes in a RIFF file. All 11,327 banked waves of the 68 scenes decode to exactly the record's lSamples and to the Wave's m_fDuration within 1 ms.
ENCVAG.DLL in the install exports EncVagInit, EncVag and EncVagFin, Sony's VAG encoder. The unpacked executable names neither the DLL nor its functions, so it is a leftover of IOI's tools, and no PC data is VAG.
pc_eng.str¶
pc_eng.str is the same IOISNDSTREAM version 9 container as PS2_ENG.STR (pcstreamsdl.h; docs/development/port/sound-and-movies.md, "PC streams"). Header word 0x20 is the platform: 1 on the PC, 2 on the PS2. It holds 3,414 entries (3,163 IMA ADPCM, 116 PCM, 106 Vorbis, 29 two-line codec 17). Every one of the 45,548 streamed wave references in the 68 PC scenes names an entry at its lOffset. The entry has the record's own name (case-insensitively), channels, rate and size, and a codec the port plays: 44,450 IMA ADPCM, 821 PCM, 277 Vorbis. The PC lookup is therefore by offset, with the WHD name to choose between the entries that share an offset.
Streams by name, PC against PS2:
- Only on the PS2:
Character/Expressions/Stripper/Stripper_01.wavto_03.wav(16 kHz). No PC scene references them. - Only on the PC:
Ambient/M13/OutSide/Ambience_Outside_Wind_M13_01.wav(Vorbis, stereo, 40,472 Hz).
How the port plays PC data¶
The data decides which path runs, not a setting:
- Wave records.
FindOrDecodeWavereads a record at the offset the engine sends, as before. A banked record withwFormat0x11 goes toPcSoundDecodeBankedWave(IMA ADPCM). SPU ADPCM also sayslBits4, so the tag decides; the PS2 never uses 0x11. Banked 16-bit PCM takes the existing PCM path. A streamed record with tag 0x11 or 0x1000 never falls through to the PS2 stream reader, which would play its bytes as PCM.PcSoundWaveHeaderNamereturns the name in front of a PC record. It returns 0 for a PS2 record, whoselEntryis 0. - Streams.
PcStreamsInstallreads the header of the stream file the engine installs, the name the.SNDcarries. A PC container (platform 1) switches to native mode: its own index is read, sorted by offset, andPcStreamsFind(lOffset, channels, rate, name)returns the entry at the record's offset with the record's name. A PS2 container keeps the old pairing by name with an optional PC install. The data is read from the install'spc_eng.stron the host when its header matches the engine's file. Otherwise it is read throughg_pSysFileunder the engine's name. Ifg_pSysFilecannot openpc_Eng.strand the PC install has it, the host file is used. In native mode a streamed record with no playable entry stays silent, because there is no PS2 data behind it. - The stream log line per scene becomes
pc audio: N of M streamed waves of <scene>.WHD playable from the PC stream file. A PC table cannot be walked as a series of records, so it is counted through the.SND's Wave table.
See Sound effects for the pairing of PS2 and PC waves.
Unknown¶
- The IOP side: how
SYNTHCD.IRXparses the WHD, whatlId,lEntry,lStamp0/1andlExtra0..3mean. - The packed headers for DefMaster, Layered, RoomEffect and eight controller types have no named payload fields.
- Whether every path through the engine and the IOP accepts a Sound record with no controllers (the tool's default). On the disc 17,798 of 70,689 Sound records have none.