Audio system
The game plays music and effects as sampled instruments in SPU memory, driven by a sequencer or by direct key-on, and streams recorded speech from the disc through the CD controller without touching main RAM. This page covers the runtime between the disc and the speakers. Formats, loader steps and RAM windows: audio formats, loading, memory.
Data to SPU
Section titled “Data to SPU”Music and effects pass through the EMI loader (entry types 6 to 10: header, body, cue table, sequence) into bank windows and SPU RAM; a sequencer (SsSepPlay) or SND_PlayCue (SsUtKeyOnV) then drives the 24 SPU voices. XA skips the loader: the CD controller (CdlReadS, filter) mixes it into the audio output with CdMix, so it never touches main RAM. SND_SetCdVolume (exe/slus_004_22@0x80163EB0) sets that mix.
Initialization
Section titled “Initialization”SYS_StartFrontend calls SND_Init (exe/slus_004_22@0x8015CD00), SND_SetBankLayout(0) and later SND_Start (exe/slus_004_22@0x8015CE70) (boot).
SND_InitcallsSsInit,SsSetTableSize(D_80148A50, 2, 4),SsSetTickModein 60 Hz mode, turns room reverb on, and callsSsSetAutoKeyOffMode(0)andSsSetReservedVoice(0x10). It sets the seven VAB idsD_80148FB0..FB6to 0 through 6 and the seven SEP ownersg_SelectionFxVoices[0..6](0x80148FC0) to-1. It then callsSpuSetVoiceAttrto set volume0x3FFFon voices 16 to 23.- The tick mode argument selects a 60 Hz sequencer step, the NTSC frame rate
[inferred]. - The first
SsSetTableSizeargument is the sequence table.Battle_LoadBattleArchiveandfunc_80197F48save and restore 1,376 bytes of it: the table is 1,376 bytes at0x80148A50. SND_StartcallsSsStart2and sets the master volume to0x7Fand the serial (CD) volume to0x6E.SND_Exit (exe/slus_004_22@0x8015CEBC)keys off all voices, closes the seven VABs and SEP owners, turns reverb off and callsSsEnd.
Seven bank records (20 bytes each, at 0x8014677C) tie a VAB id to a RAM window, an SPU address and a cue table. SND_SetBankLayout (exe/slus_004_22@0x80161808) fills them from one of three static layouts. It lays two RAM windows per bank back to back from 0x8011A000: a header window (D_8018239C[n][bank]), then a sequence window (D_801823F0[n][bank]). The SPU address comes from D_80182348[n][bank]. The cue table pointer is 0x801486A0 + 124 * bank.
The active layout sets the SPU RAM partition:
| Layout | Set by | Banks in use | When |
|---|---|---|---|
| 0 | SYS_StartFrontend, Game_StartSelectedGame and Game_LeaveTitle (emi/etc/game/01), Battle_LoadEndOverlay and Battle_ReloadPartyBank (emi/battle/battle/03) |
3 | Menus, field, after a battle |
| 1 | Battle_LoadPartySprites (emi/etc/game/00) |
7 | Battle |
| 2 | Game_BeginTitle (emi/etc/game/01@0x801D0C90) |
2 | Title screen |
Bank 0 carries the music in every layout, with a different window size in each (budgets).
An EMI audio entry reaches its bank record as follows:
- Type 6 (
EMI_LoadVabHeader) copies the VAB header (VH) into the bank’s header window. If the bank already owns a SEP (g_SelectionFxVoices[bank] != -1), it callsSsUtAllKeyOff(0)andSsSepClose, and resets the owner to-1. - Types 9 and 10 (
EMI_LoadSequence) copy the SEP package into the bank’s sequence window and set record flag bit 1 (value 2). - Type 8 (
EMI_LoadSoundCues) copies the cue table into the bank’s cue chunk. - Type 7 (
EMI_LoadVabBody) sets SPU DMA mode, callsSsVabClose, and callsSsVabOpenHeadSticky(header window, vab id, spu address). Ring state 4 then runsEMI_UploadVabBody:SsVabTransBodyPartlypushes each ring sector of the body to the SPU, andSsVabTransCompletedwaits for the DMA. - After the transfer, when the record flag is set,
SsSepOpenJ(sequence window, vab id, 4)opens the package with four sequences and stores the result ing_SelectionFxVoices[bank]. The flag clears. A result of-1sets the loader error state.
The entry’s load argument says which bank record it targets. The BGM archives hold types 6, 10 and 7 only, and their BgmTrack.sep_slot is 0 in all 165 rows, so music lives in bank 0. The bank targets of the sound-effect archives (the common and battle effect archives) are not traced [unknown].
The table g_BGMTracks (0x80181EB8) has 165 rows of 4 bytes: a u16 bank_slot, then sep_slot and sequence bytes. All 165 rows use sep_slot 0. Their bank_slot values are 81 distinct archives from 0xD1 to 0x121, the BGM family. sequence runs 0 to 3, since a SEP package holds four sequences.
SND_StartBGM (exe/slus_004_22@0x80161B24)(id, level, shape) is the field-side entry:
SND_RequestBGM (exe/slus_004_22@0x80161BBC)(id)compares thebank_slotofidwith that ofg_SndBgmId. If they differ, or nothing is active, it callsEMI_LoadArchive(bank_slot)and returns 1. If they match, the archive is already in bank 0.- If it returned 1,
SND_StartBGMpollsEMI_IsReadywithCallbackWait(1). - If
g_SndBgmId != id,SND_PlayBgm (exe/slus_004_22@0x80161C20)runsSND_PlaySequence(sep_slot, sequence, 0, 0), thenSND_FadeInSequence(sep_slot, sequence, level, shape), storesD_80143F20 = level, and setsg_SndBgmId.
Two tracks of one archive share the load: the second SND_RequestBGM returns 0 and only the sequence changes. 0xFF means no track. SND_FadeOutBgm (exe/slus_004_22@0x80161CD0) fades out and stops the track’s sequence.
Five sources start a track:
| Source | Code |
|---|---|
| Area arrival | func_8019FAA0 (emi/etc/game/00) calls SND_StartBGM(g_GamePrevSelection, 100, 8). 0xFF clears g_SndBgmId. |
| Area track id | func_801A0380 writes g_GamePrevSelection from byte 6 of the area’s zone rectangle at the player’s cell, or through a flag-conditioned list when byte 7 is nonzero (D_80181E40). It keeps the active track when g_GameFlags & 0x80. |
| Event script | func_801A96E4 (emi/etc/game/00) opcodes 0xB4 (SND_PlayBgm), 0xB5 (SND_FadeOutBgm) and 0xBE (SND_StartBGM). |
| Scenario and area code | Direct SND_PlayBgm, SND_RequestBGM and SND_FadeOutBgm calls in scena00, scena16 and area overlays. |
| Title | Game_BeginTitle calls SND_PlayBgm(0x8D, 100, 8). |
g_GamePrevSelection holds the track of the area about to load: func_801A0380 writes it and func_8019FAA0 reads it. The zone rectangle is described in area.
When the area changes to a different track, Game_UpdateField case 5 calls SND_StartDecrescendo on the old track (level 100, shape 16) and SND_StopSequence after the transition effect. func_801985C8 does the same for the other exit path.
Music across a battle
Section titled “Music across a battle”A battle replaces bank 0, then restores it.
Battle_LoadBattleArchive(emi/etc/game/00, battle entry) checksg_BattleOptions & 0x40(keep the field music). When it is clear, it sets bit0x40ofg_EmiStreamFlags, whether or not a track is active. When a track is active it also pauses the field sequence withSsSepPause, copies the 1,376-byte sequence tableD_80148A50intoD_8018E268, and stops the sequence. It then streams the battle archive (slot 6 or 7, or the boss slot).EMI_UploadVabBodysees bit0x40and the bank match (D_80146855andD_80146856are 0, 0) and callsSND_PlaySequence(0, 0, 100, 100)as soon as the battle bank is open. The same call restarts the field track after a battle whenBattle_ReloadMusicBankstored its owner and sequence inD_80146855andD_80146856. The battle music is the SEP package inside theBATTLEorBOSSarchive.- At the battle end
Battle_ReloadMusicBank(emi/battle/battle/03) waits for the loader and runs the end hook. When the keep-music option (g_BattleOptions & 0x40) is clear it callsSND_StopAllSequences, setsg_EmiStreamFlagsbit0x20, and, when the result has bit0x10, bit0x40too. It then re-streams the field track’s archive (g_BGMTracks[g_SndBgmId].bank_slot). Bit0x20makesEMI_NextEntryskip every entry of type below 6, so only audio entries load. func_80197F48(emi/etc/game/00, battle exit) copiesD_8018E268back intoD_80148A50and callsSsSepReplay,SND_SetSequenceVolumeandSND_FadeInSequenceon the field track.
Steps 3 and 4 resume the field track, from a reloaded bank or from the restored table. Which path wins when both run is [unknown]. The bits of g_EmiStreamFlags are 0x20 for audio-only reload and 0x40 for auto-play on load. See battle for the entry and exit states.
Sound effects
Section titled “Sound effects”A cue is a 16-bit id: bank in bits 8 to 11, record index in bits 0 to 7. SND_PlayCue (exe/slus_004_22@0x8015DF18)(id) stores bank and index, then calls g_SndCueReaders[bank], one of seven readers (SND_ReadCueBank0, SND_ReadCueBank1, SND_ReadCueBank2, SND_ReadCueBank3, SND_ReadCueBank4, SND_ReadCueBank5, SND_ReadCueBank6). The reader decodes the 4-byte cue record (voice, program, tone) into up to four voice slots. SND_PlayCue then calls SsUtKeyOnV once per slot (modes 0 to 3) and, when D_8018B3F4 == 0x80, SsUtSetDetVVol with the volume cells D_8018B3D8 and D_8018B3DC. The record layout and bank readers are in audio formats.
- A cue’s bank must be 0 to 6. The table has seven entries and
SND_PlayCuedoes not range-check a larger bank nibble. - Event scripts reach cues through opcodes
0xB0to0xB3(SND_PlayCue(byte | bank << 8), banks 0 to 3) and0xBC(SND_QueueCueVolume, a 16-bit id), both infunc_801A96E4.SND_QueueCueVolume (exe/slus_004_22@0x8015D7C8)loads left and right volume fromD_80182288, then callsSND_PlayCue. - The volume cells come from
D_80182288and the tables after it (D_80182290toD_80182308): 16-bit0xXXFFpairs, a stepped ladder. Their use beyondSND_QueueCueVolumeis not traced[unknown]. SND_QueueSelectionFxclears the four voice-slot cells to-1, callsSND_PlayCue, and packs the first four voice numbers (5 bits each) into the caller’s actor record at+0x70, setting flag bit 3. Actors keep the voices they own.
Sequence control of effect packages uses SND_StopSequence, SND_SetSequenceVolume, SND_StopAllSequences (stops the four sequences of every live owner) and SND_FindFreeFxVoice (scans the four sequences of owner 0 with SsIsEos; its outer loop ends after index 0). Event opcodes 0xB8 to 0xBB call the sequence functions with script bytes.
Voices and loudness
Section titled “Voices and loudness”SND_UpdateVoiceStatus (exe/slus_004_22@0x8015D044)storesSpuGetKeyStatus(1 << i)fori = 0..23intoD_8018E140each frame, called fromSYS_Main.- The PlayStation SPU has 24 voices.
SsSetReservedVoice(0x10)reserves 16 of them for the library, and voices 16 to 23 are prepared as the effect voices[inferred]. D_80144954is a duck gate. While it is nonzero,SND_PlaySequencestarts sequences at volume 0 and the fade-in and decrescendo calls return.SYS_StartFrontendandfunc_801E18D0(emi/etc/start/08) clear it. Who sets it is not traced[unknown].g_SndMonoselects mono: when it is nonzero,SND_SetCdVolumehalves all fourCdMixbytes, so each output gets half the level on both channels. Who sets it is not traced[unknown].
XA streams
Section titled “XA streams”XA plays speech and long effects from three disc files, selected by a key. SND_StartXAStream (exe/slus_004_22@0x801636A0)(key, _) sets g_EmiServiceMode = 2 (XA mode in EMI_Update) and g_EmiStreamState = 0 (so EMI_IsReady is false). It clears the request block, calls SND_ResolveXAStream (exe/slus_004_22@0x80163744)(key), converts the start LBA with CdIntToPos, and installs SND_StoreXASyncResult as the CD sync callback. The second argument is unused.
SND_ResolveXAStream turns the key into a file, a channel and a sector range:
- The group is
key >> 12and the index iskey & 0xFFF. The file isg_EmiSlotLbas[D_80183230[group]], that is loader slot0x2A9(group 0),0x1B2(group 1) or0x2AA(group 2). - Each group has a
u16table (D_8018323C[group]:D_801839C4,D_801832C4,D_80183298). The code walksindexentries. An entry with bit 15 set skips one extrau16and adds 1 to a channel counter (D_80146835). - The start sector is
base + (entry & 0x7FFF) * scale + channel, withscale8 for group 0 and 16 for the others. The stop bound is the same expression for the next entry, minus 150 sectors (0x96). 150 is the CD pregap offset of the position conversion, one table entry is a track start in interleaved sectors, and the counter is the XA channel[inferred]. CdlSetfilteris sent with file1and that channel[inferred]from the parameter block atD_80146834.
SND_UpdateXA (exe/slus_004_22@0x801638B0) runs one state of g_XaStateFuncs[state] per frame, called by EMI_Update while g_EmiServiceMode == 2, then increments a timer D_8014681C:
| State | Function | Action |
|---|---|---|
| 0 | SND_XaStateIdle |
Clear timer and status. |
| 1 | SND_XaStateSeek |
CdlSetmode double speed, real time, subheader filter (CdlModeSpeed|CdlModeRT|CdlModeSF), CdSeekP. |
| 2 | SND_XaStateSeekResult |
A negative status retries state 1 or goes to 8. Success goes to 3. |
| 3 | SND_XaStateRead |
CdlSetfilter, CdlReadS. |
| 4 | SND_XaStateReadProgress |
A negative status retries 3 or goes to 8. Success goes to 5. |
| 5 | SND_XaStateReadResult |
CdlGetlocL every second call. When the position reaches the stop bound, go to 6. |
| 6 | SND_XaStatePause |
SND_FadeCdVolume(0), CdPause. |
| 7 | SND_XaStatePauseResult |
Success: state 0, g_EmiStreamState = 3, clear the sync callback. |
| 8 | SND_XaStateWaitSync |
Wait for CdSync, then back to 6. |
A timer above 181 ticks returns to state 1 and counts a retry. The sixth retry, or a CD error bit, sends the machine to state 8. Streaming ends by stopping at the stop bound, not by an end marker in the data. State 7 sets the loader’s ready byte to 3, so EMI_IsReady is false for the whole stream, and none of the callers waits for it first (loading).
XA callers are the scene scripts (opcode 0x89 in func_801AB458, key from the script), Scene_StartShopSave (key 0), func_801A83DC (key 9), the shop and save screens (key 0), commu00 (keys 8 and 10), and battle (func_801DDF50 in emi/battle/battle/03, key 0x1000 + n from an effect table).
Data and limits
Section titled “Data and limits”| Limit | Value | Source |
|---|---|---|
| Track ids | 165, 81 archives 0xD1 to 0x121, sequence 0 to 3 |
g_BGMTracks |
| Sequences per package | 4 | SsSepOpenJ(.., 4), SND_FindFreeFxVoice |
| Open SEP packages | Seven owner slots, SsSetTableSize(.., 2, 4) |
g_SelectionFxVoices[7], SND_Init |
| Concurrent sequence limit | 2 packages by the table size [inferred]; the arguments’ meaning is the PsyQ contract |
SND_Init |
| Sequence table | 1,376 bytes at 0x80148A50 |
Battle_LoadBattleArchive, func_80197F48 |
| VAB banks | 7 (ids 0 to 6) | SND_Exit, SND_SetBankLayout |
| SPU voices | 24 polled, 16 reserved, effect voices 16 to 23 prepared [inferred] |
SND_UpdateVoiceStatus, SND_Init |
| Cue banks and records | Bank nibble indexes 7 readers. 31 records per bank chunk (124 bytes). A disc entry has at most 24 records. | g_SndCueReaders |
| Voices per cue | 1 to 4 SsUtKeyOnV calls (layer bits) |
SND_PlayCue |
| XA files | Three: slots 0x2A9, 0x1B2, 0x2AA. Keys use 12 bits of index. |
D_80183230, SND_ResolveXAStream |
| XA timeout | 181 ticks, 6 retries | SND_UpdateXA |
| Volume | Master 0x7F, serial 0x6E |
SND_Start |
RAM and SPU budget per layout
Section titled “RAM and SPU budget per layout”Each bank has a header window and a sequence window in main RAM (sizes in bytes, from D_8018239C and D_801823F0) and an SPU start address (D_80182348). The SPU body of a bank may extend up to the next bank’s start. The spans are that subtraction [inferred].
| Layout | Bank | Header window | Sequence window | SPU start | SPU span to next bank (bytes) |
|---|---|---|---|---|---|
| 0 | 0 | 10,784 | 50,080 | 0x1010 |
250,000 |
| 0 | 1 | 4,128 | 0 | 0x3E0A0 |
70,000 |
| 0 | 2 | 4,640 | 4,096 | 0x4F210 |
to the end of SPU RAM, at most 200,176 |
| 1 | 0 | 9,760 | 36,192 | 0x1010 |
250,000 |
| 1 | 1 | 3,616 | 0 | 0x3E0A0 |
82,000 |
| 1 | 2 | 3,104 | 0 | 0x520F0 |
36,992 |
| 1 | 3 | 4,128 | 0 | 0x5B170 |
31,008 |
| 1 | 4 | 4,128 | 0 | 0x62A90 |
30,992 |
| 1 | 5 | 4,128 | 0 | 0x6A3A0 |
31,008 |
| 1 | 6 | 6,688 | 0 | 0x71CC0 |
to the end, at most 58,176 |
| 2 | 0 | 12,320 | 54,176 | 0x1010 |
440,000 |
| 2 | 1 | 4,640 | 0 | 0x6C6D0 |
to the end, at most 80,176 |
The round spans (250,000, 70,000, 82,000, 440,000) are design budgets [inferred].
Limits for an edited audio asset:
- VAB header (VH). A VH is 2,592 + 512 *
psbytes (0xA20 + ps * 0x200) (audio formats). The header window sets the program countps. Bank 0 allowsps <= 16in layout 0,<= 14in layout 1 and<= 19in layout 2. In layout 0, bank 1 allows 3 and bank 2 allows 4. The loader functions copy sectors with no size check that this reading found[inferred]. - VAB body (VB). It must fit the SPU span of its bank and layout above. SPU RAM is 512 KiB (
0x80000) and the table starts at0x1010. The reverb work area also occupies the top of SPU RAM in room mode, which is the PsyQ contract[inferred]. The loader reports an error only whenSsVabOpenHeadStickyorSsSepOpenJreturns -1. - SEP package. It must fit the bank’s sequence window (bank 0: 50,080 bytes in layout 0, 36,192 in layout 1, 54,176 in layout 2) and hold 4 sequences. A track archive replaces bank 0, so battle music (
BATTLE,BOSSarchives) uses the layout 1 limit[inferred]. - Cue table. At most 24 records per disc entry and 31 per bank chunk. Each record’s voice field is 0 to 23.
- Track ids. A new track needs a new row in
g_BGMTracks(executable data, 165 fixed rows) and the archive slot in the disc tableg_EmiSlotLbas(disc and files). Enlarging the table is a code change; see the guide to editing sound. - XA. A key resolves to a table entry, a file and a channel. New speech needs table entries and sectors at the matching positions. The channel interleave factor is 8 (group 0) or 16 (other groups)
[inferred].
Open questions
Section titled “Open questions”- Bank targets of the common and battle effect archives, and which archives fill banks 1 to 6. Settled by: reading the load arguments in those archives’ type-6 and type-8 entries.
- Whether the loader checks any window or SPU size. Settled by: tracing
EMI_CopySectorDataand the ring stage for a bound against the window sizes. - Concurrent open SEP count (the meaning of
SsSetTableSize). Settled by: PsyQ sound library documentation, or a trace ofSsSepOpenJreturning -1. - Which of
Battle_ReloadMusicBankandfunc_80197F48resumes the field track. Settled by: tracing the order of both with that option clear and result bit0x10set. - Writers of
D_80144954(duck gate) andg_SndMono(mono). Settled by: searching overlays andemi/etc/start/08for stores to0x80144954and0x80144951. - Meaning of the cue volume ladders
D_80182290toD_80182308. Settled by: finding the readers of those tables. - XA group sizes, group 2 callers and the content of each table entry. Settled by: reading the three tables against the XA file sector map.
- The seven bank readers’ per-bank differences. Settled by: reading
SND_ReadCueBank0toSND_ReadCueBank6. - Opcode
0xBD(func_8015D820) in event scripts. Settled by: readingfunc_8015D820.
Sources and confidence
Section titled “Sources and confidence”- Confidence: medium. The loading path, the music and battle handling, the cue path and the XA state machine are read in the code. Window and span limits, the reserved-voice meaning and several writers are
[inferred]or[unknown]. - Evidence: the sound functions in
exe/slus_004_22(SND_Init,SND_SetBankLayout,SND_StartBGM,SND_PlayCue,SND_ResolveXAStream,SND_UpdateXA), the audio entry loadersEMI_LoadVabHeader,EMI_LoadVabBody,EMI_LoadSequenceandEMI_LoadSoundCues, the battle entry and exit inemi/etc/game/00andemi/battle/battle/03, and the static tablesg_BGMTracks,D_8018239C,D_801823F0andD_80182348. Library behavior follows the PsyQ contract.
Related
Section titled “Related”- Audio formats: the VAB, SEP, cue and XA layouts that this runtime reads.
- Loading: the loader steps that feed the audio code.
- Memory map: the RAM windows for the banks.
- Battle flow: the entry and exit states around the battle music.
- Areas and the field: arrival music and the zone list.
- Edit sound and music: how to change tracks and banks within these limits.
- STR and XA playback: the sector layouts and cue tables that the XA path reads.