Difference between revisions of "SFX (SA)"

From GTAMods Wiki
Jump to navigation Jump to search
(More up-to-date SFX directory)
(Added structures and (hopefully) helpful code (in Go). Also cleaned up content (except in BankSlot.dat))
Line 6: Line 6:
  
 
== File Formats ==
 
== File Formats ==
The SFX config formats are pure dumps of game structures, and so contains a lot of irrelevant data. They are stored in the <code>audio/CONFIG</code> directory and have a <code>.dat</code> extension.
+
The SFX config formats are pure dumps of game structures, so contain a lot of irrelevant data. They are stored in the <code>audio/CONFIG</code> directory and have the extension <code>.dat</code>.
  
 
=== PakFiles.dat ===
 
=== PakFiles.dat ===
This file is used to ''enumerate'' [[#SFX Packages|SFX Packages]], it's structure is just a list of file names.
+
<code>PakFiles.dat</code> is a list of package names. Each name takes up 52 bytes.
  
repeat (any amount) times:
+
<syntaxhighlight lang="go">
    char[52]     File Name (relative to the <code>audio/SFX</code> directory)
+
// package count = file size / 52
 +
type PackageList struct {
 +
Packages []string
 +
}
 +
</syntaxhighlight>
  
The first named package has the index 0, the second named package the index 1 and so on.
+
The package names are the same as their file names inside the <code>audio/SFX</code> directory. In other files and also in the game, each package is referred to by its 0-based index in the package list (0, 1, 2, etc.).
  
 
=== BankLkup.dat ===
 
=== BankLkup.dat ===
This file is used to ''enumerate'' global bank numbers with it's respective SFX Package.
+
<code>BankLkup.dat</code> contains an array of metadata structures for all banks in all packages.
  
repeat (any amount) times:
+
<syntaxhighlight lang="go">
    uint8_t    SFX Package index (as specified in [[#PakFiles.dat|PakFiles.dat]])
+
// 12 bytes
     uint8_t[3] Unused      (padding)
+
type BankMeta struct {
    uint32_t    Bank offset (where this bank header starts on the package file)
+
// Index of package in PakFiles.dat
    uint32_t    Bank size  (the size of this entire bank without the header)
+
PackageIndex uint8
 +
Padding     [3]uint8
  
The game uses global indices to lookup for banks internally, so changing the order of the enumeration will cause problems.
+
// Bank header location in package file.
 +
BankHeaderOffset uint32
  
=== SFX Packages ===
+
// Total size of sounds in bank.
Those are the files responsible for storing all the raw sound data. They are at the <code>audio/SFX</code> directory with no extension.
+
BankSize uint32
 +
}
 +
</syntaxhighlight>
  
The file structure is basically many bank headers (at ''any offset'') followed by it's sound buffers. The offset where each bank header is located is specified by the file [[#BankLkup.dat|BankLkup.dat]].
+
Note that the game uses hardcoded indices to reference specific banks, so changes in order can lead to problems.
  
uint16_t        Number of sounds in this bank (maximum of 200)
+
=== Packages ===
uint16_t        Unused (padding)
+
Package files contain banks, which contain sound data (including the raw PCM data). Packages are in the <code>audio/SFX</code> directory and have no file extension.
repeat (400) times:
 
    uint32_t    Sound buffer offset (relative to the end of this header)
 
    uint32_t    Loop offset (in samples)
 
    uint16_t    Sample rate
 
    uint16_t    Sound headroom
 
  
Following this header are the buffer for the sounds in this bank. Each buffer should be in raw ''Mono PCM 16-bits'' format.
+
Each bank stored in a package file has two parts: the header and the PCM buffers. The PCM buffers follow the header.
 +
Most of the bank header is made up of sound metadata, which has the following structure:
 +
<syntaxhighlight lang="go">
 +
type SoundMeta struct {
 +
// Offset of the PCM buffer from the end of the bank header.
 +
BufferOffset uint32
  
Even though there is space in the header for 400 sounds, only 200 are allowed.
+
// Where the start of the loop is (in samples).
 +
LoopOffset int32
 +
 
 +
// Sample rate (measured in Hz).
 +
SampleRate uint16
 +
 
 +
// Audio headroom. Defines how much louder than average
 +
//  this sound effect is expected to go.
 +
Headroom int16
 +
}
 +
</syntaxhighlight>
 +
The <code>Headroom</code> value tells the game how loud the sound will be. This allows the game to adjust what volume to play the sound at in order to prevent the audio clipping. A value of 0 tells the game that the sound is about average volume, with negative values meaning the sound is quieter and positive values meaning it's louder.
 +
 
 +
The actual bank header structure is as follows:
 +
<syntaxhighlight lang="go">
 +
// 4084 bytes (including SoundMeta array)
 +
type BankHeader struct {
 +
// The number of sounds in the bank. Must be <= 200.
 +
NumSounds uint16
 +
Padding  uint16
 +
 
 +
// Sound metadata.
 +
Sounds [400]SoundMeta
 +
}
 +
</syntaxhighlight>
 +
The <code>BankHeader</code> structure's location is specified by the <code>BankHeaderOffset</code> value of the bank's <code>BankMeta</code> structure.
 +
Note that even though there is space in the header for 400 sounds, only 200 are allowed.
 +
 
 +
The audio sample buffers are all in ''signed 16-bit mono PCM'' format. The offset of a sound's PCM buffer can be calculated with the following function:
 +
<syntaxhighlight lang="go">
 +
func (sound *SoundMeta) getPCMOffset(bankInfo *BankMeta) uint32 {
 +
// BankHeaderOffset is the offset of the start of the header,
 +
//  so the end of the header is 4804 bytes after.
 +
return bankInfo.BankHeaderOffset + 4804 + sound.BufferOffset
 +
}
 +
</syntaxhighlight>
 +
 
 +
Since the <code>SoundMeta</code> structure does not specify the size of the buffer, it must be calculated.
 +
In the following code, <code>bankAudioSize</code> is <code>BankMeta.BankSize</code> for the current bank.
 +
 
 +
<syntaxhighlight lang="go">
 +
func calculateBufferSize(bankHeader *BankHeader, bankAudioSize uint32, soundIndex int) uint32 {
 +
sound := bankHeader.Sounds[soundIndex]
 +
 
 +
var length uint32
 +
if soundIndex >= int(bankHeader.NumSounds-1) {
 +
// This is the final sound in the bank, so we need to use the size of the bank
 +
//  to calculate the length of the sound's buffer.
 +
length = bankAudioSize - sound.BufferOffset
 +
} else {
 +
// length = this sound offset - next sound offset
 +
// This works because the buffers are stored contiguously in the file.
 +
length = bankHeader.Sounds[soundIndex+1].BufferOffset - sound.BufferOffset
 +
}
 +
 
 +
return length
 +
}
 +
</syntaxhighlight>
  
 
=== BankSlot.dat ===
 
=== BankSlot.dat ===
Line 49: Line 114:
 
Each bank slot is used by a specific audio class of the game, for example 10 slots are used for vehicle banks, one slot for the bank of explosions, 4 slots for script speeches (which stores single sounds), and so on.
 
Each bank slot is used by a specific audio class of the game, for example 10 slots are used for vehicle banks, one slot for the bank of explosions, 4 slots for script speeches (which stores single sounds), and so on.
  
uint16_t    Number of Slots (''must be 45'')
+
The <code>BankSlot.dat</code> file's structure is
repeat (Number of Slots) times
+
 
    uint32_t    Sum of all buffer size before this slot (ignored)
+
<syntaxhighlight lang="go">
    uint32_t    Buffer size for this slot
+
type SlotFile struct {
    uint32_t    Unknown (related to feet sounds)
+
// Always 45
    uint32_t    Unknown (related to feet sounds)
+
NumSlots uint16
    byte[4804] Ignored
+
Slots   [45]Slot
 +
}
 +
</syntaxhighlight>
 +
 
 +
where <code>Slot</code> is
 +
 
 +
<syntaxhighlight lang="go">
 +
type Slot struct {
 +
// Sum of all buffer sizes before this slot (i.e. the offset).
 +
BufferOffset uint32
 +
 
 +
// Buffer size for this slot.
 +
BufferSize uint32
 +
 
 +
// {-1, -1} on disk. Related to feet sounds?
 +
Unknown [2]int32
 +
 +
Ignored [4804]byte
 +
}
 +
</syntaxhighlight>
  
Each slot owns a buffer capable of storing some amount of sound data, if this size is lower than the size of the data stored in the slot, sound artifacts will occur in-game.
+
Each slot owns a buffer capable of storing some amount of sound data. If this size is lower than the size of the data stored in the slot, sound artifacts will occur in-game.
  
 
By default, those buffer sizes are arbitrary values, but it can safely be the size of the highest bank stored in the slot or the size of the highest sound stored in the slot, depending if the slot is used to store a sound or a bank.
 
By default, those buffer sizes are arbitrary values, but it can safely be the size of the highest bank stored in the slot or the size of the highest sound stored in the slot, depending if the slot is used to store a sound or a bank.

Revision as of 15:05, 28 August 2020

San Andreas introduces a different method of dealing with SFX compared to it's antecessor, now not only sound indices are available but packages and banks.

Packages are a way to organize the SFX data in different files and banks are a way to organize SFX data in one package. Each bank contains up to 200 sounds. Normally the entire bank is loaded at once, but depending on some circumstances the game might load only a single sound from the bank.

File Formats

The SFX config formats are pure dumps of game structures, so contain a lot of irrelevant data. They are stored in the audio/CONFIG directory and have the extension .dat.

PakFiles.dat

PakFiles.dat is a list of package names. Each name takes up 52 bytes.

// package count = file size / 52
type PackageList struct {
	Packages []string
}

The package names are the same as their file names inside the audio/SFX directory. In other files and also in the game, each package is referred to by its 0-based index in the package list (0, 1, 2, etc.).

BankLkup.dat

BankLkup.dat contains an array of metadata structures for all banks in all packages.

// 12 bytes
type BankMeta struct {
	// Index of package in PakFiles.dat
	PackageIndex uint8
	Padding      [3]uint8

	// Bank header location in package file.
	BankHeaderOffset uint32

	// Total size of sounds in bank.
	BankSize uint32
}

Note that the game uses hardcoded indices to reference specific banks, so changes in order can lead to problems.

Packages

Package files contain banks, which contain sound data (including the raw PCM data). Packages are in the audio/SFX directory and have no file extension.

Each bank stored in a package file has two parts: the header and the PCM buffers. The PCM buffers follow the header. Most of the bank header is made up of sound metadata, which has the following structure:

type SoundMeta struct {
	// Offset of the PCM buffer from the end of the bank header.
	BufferOffset uint32

	// Where the start of the loop is (in samples).
	LoopOffset int32

	// Sample rate (measured in Hz).
	SampleRate uint16

	// Audio headroom. Defines how much louder than average
	//  this sound effect is expected to go.
	Headroom int16
}

The Headroom value tells the game how loud the sound will be. This allows the game to adjust what volume to play the sound at in order to prevent the audio clipping. A value of 0 tells the game that the sound is about average volume, with negative values meaning the sound is quieter and positive values meaning it's louder.

The actual bank header structure is as follows:

// 4084 bytes (including SoundMeta array)
type BankHeader struct {
	// The number of sounds in the bank. Must be <= 200.
	NumSounds uint16
	Padding   uint16

	// Sound metadata.
	Sounds [400]SoundMeta
}

The BankHeader structure's location is specified by the BankHeaderOffset value of the bank's BankMeta structure. Note that even though there is space in the header for 400 sounds, only 200 are allowed.

The audio sample buffers are all in signed 16-bit mono PCM format. The offset of a sound's PCM buffer can be calculated with the following function:

func (sound *SoundMeta) getPCMOffset(bankInfo *BankMeta) uint32 {
	// BankHeaderOffset is the offset of the start of the header,
	//  so the end of the header is 4804 bytes after.
	return bankInfo.BankHeaderOffset + 4804 + sound.BufferOffset
}

Since the SoundMeta structure does not specify the size of the buffer, it must be calculated. In the following code, bankAudioSize is BankMeta.BankSize for the current bank.

func calculateBufferSize(bankHeader *BankHeader, bankAudioSize uint32, soundIndex int) uint32 {
	sound := bankHeader.Sounds[soundIndex]

	var length uint32
	if soundIndex >= int(bankHeader.NumSounds-1) {
		// This is the final sound in the bank, so we need to use the size of the bank
		//  to calculate the length of the sound's buffer.
		length = bankAudioSize - sound.BufferOffset
	} else {
		// length = this sound offset - next sound offset
		// This works because the buffers are stored contiguously in the file.
		length = bankHeader.Sounds[soundIndex+1].BufferOffset - sound.BufferOffset
	}

	return length
}

BankSlot.dat

A bank slot is capable of storing some SFX data, this data can be either one entire bank or one single sound from a bank. There is a total of 45 bank slots.

Each bank slot is used by a specific audio class of the game, for example 10 slots are used for vehicle banks, one slot for the bank of explosions, 4 slots for script speeches (which stores single sounds), and so on.

The BankSlot.dat file's structure is

type SlotFile struct {
	// Always 45
	NumSlots uint16
	Slots    [45]Slot
}

where Slot is

type Slot struct {
	// Sum of all buffer sizes before this slot (i.e. the offset).
	BufferOffset uint32

	// Buffer size for this slot.
	BufferSize uint32

	// {-1, -1} on disk. Related to feet sounds?
	Unknown [2]int32
	
	Ignored [4804]byte
}

Each slot owns a buffer capable of storing some amount of sound data. If this size is lower than the size of the data stored in the slot, sound artifacts will occur in-game.

By default, those buffer sizes are arbitrary values, but it can safely be the size of the highest bank stored in the slot or the size of the highest sound stored in the slot, depending if the slot is used to store a sound or a bank.

Scripting

Sounds can be used in SCM with the following opcodes:

  • 018C – Plays a sound on a specific location
  • 018E – Stops the played sound
  • 03CF – Loads a sound
  • 03D0 – Checks if sound has been loaded
  • 03D1 – Plays the loaded sound
  • 03D2 – Checks if the loaded sound has finished
  • 097A – Plays a audio event
  • 097B – Plays a audio event in an object
  • 09F1 – Plays a audio event in a character
  • 09F7 – Plays a audio event in a vehicle
  • 09D6 – Makes a character say a scripted speech

Tools

  • San Andreas Audio Toolkit – Allows you to modify SFX Packages – It contains a bug related to bank slots causing sound artifacts
  • GTA Net GTAForums: Mod Loader – Allows you to easily change SFX data and fixes the sound artifacts on big sounds.
  • GTA Net GTAForums: Dynamic SFX – Fixes sound artifacts caused by sounds bigger than the original.

See also

External links