Analyzing GTA Vice City Source Code, Part 1: Audio
Tags: architecture, gamedev, designpatterns
Published: 2023-06-24 17:58:34

I'm reading the GTA Vice City code that was decompiled by a group of people for fun (available publicly at [github.com/halpz/re3](github.com/halpz/re3)). Minimal code was added merely to fix the bugs, so it's really authentic. The code base is really huge and fun to wander in.

I'm going to share my design and architecture analysis on the code base by asking questions about each episode's subject and providing answers in a way that you don't need to read the whole code.

This one is about audio-related stuff in the code. If there are further questions that you come up with, let me know and I'll append the answers later!

### 1- How is a certain sound asset addressed in the gameplay logic?
There are gigantic enums that consist of logical name for all of the sounds used which involve SFX, music, dialogues, mission audio (sequentially), etc.  Even for the player's mood, there's an enum indicating the respective sound effect. 

Random fun fact: Player mood, for example, has effect on the sound effect of the guns.

### 2- How are the sound asset files loaded?
The address for the literal WAV sound files are hardcoded. There's a class called Sample Manager which contains the addresses which are stored as static char arrays referred to as "Name Tables" (so it's basically an array of char[MAX_STR_SIZE] as for the string of the file address.)
The items of enum that was mentioned previously match one-to-one to the files in name tables. When a sound is prompted to be played, the enum item is casted to int, and used to access the name table item. The given file address is fed to the sound player function calls (such as AIL_open_stream())

### 3- How is a sound actually prompted to be played?
Basically using the central command bus of the whole game. For each type of sound, certain commands get sent containing payloads indicating the sound type item.

### 4- How are the sequential sounds played? (Such as dialogues)
Simply by playing single sounds and increasing the indexes sequentially and sending commands from the logic to the bus in order to preload a certain mission audio files and then playing certain slot of the sequence.

### 5- How does the radio work?
In the code, it's referenced as "music" rather than radio. The term radio is actually is used for the police radio (like the one that plays when you get into police vehicle.) When you get into taxi/police car, instead of normal music, the sound of the police/taxi radio plays.
Playing music in the vehicles is one of the MusicManager class' duties. MusicManager is a global static singleton instance. The term music is used as some audio asset that is not a SFX. Since there's always one music playing at the time, there's a general method for playing the current track, referred to as front end track which gets a track id as the input. This method is called several times because many differnt conditions in the game start any music; such as you literally starting the radio, mission ending, you changing sound/radio settings in the menu, etc.
Bonus:
- Fun fact, there's a method for retreiving player's favorite radio station (by keeping track of the station with max amount of listenting history). It'll be used to be set as the default station when you get into a vehichle. 
- Music manager also manages the background music for places, such as the Malibu club.

### 6- How are the sound settings in the menu applied in the gameplay?
There is a massive part of code which deals with the front end stuff, for example the menu UI. So, drawing sprites, getting mouse, joystick, and keyboard inputs happens in there. And when you press the respective buttons to for example, mute something, or change active stations, within the front end code, the method of singleton object of music manager gets called.

### 7- How does the vehicle gear system coordinate with SFX?
(First of all, there is a lot more to vehicles. I'll talk about them later)
For everything that's dynamic with sounds, such as vehicle gear, or when it rains on them, or reverb in general, etc, these things are handled by the AudioLogic. There's a method that handles the vehicle engine sounds. This gets called by the system in charge of updating physical sound objects. The mentioned method considers a bunch of parameters such as engine sound type, current gear, is the vehicle slowing down, etc, and manipulates a sample to be played in a sound channel. The same thing happens when you're in the reverse gear. 
The vehicle engine sound is processed alongside the other properties of the vehicle such as for example, whether it has as a flat tyre and its implications. I will talk about vehicles thouroughly later.

### 8- How does the dynamic reverb work?
In different occassions, within the audio logic, a method is called to [de]activate the reverb to toggle the reverb activation state of the next samples which the the audio manager will going to ask the sample manager to play. The wetness amount isn't dynamic.

### 9- How are the sounds literally put on the device's speakers?
All logic and gameplay aside, there is a sample manager that is in charge of everything "sound rendering" if you want to call it that: such as setting channel volume, channel pan, sound stream management. Sample Manager by itelf is a base class with OpenAL and Miles (Miles Sound System Dynamic Library) implementation child classes. The mentioned children are the lowest level the code gets to the physical speakers of your device. The libraries provide methods for interacting with sound drivers.


## NEXT EPISODE:
The next part will be about everything concerning vehicles. If you have specific things you want to know regarding vehicles in the game, let me know and I'll include them!