Post

Engine - Audio System Writeup

November 19, 2026

Audio System

As a part of GAMES 6320 - Engineering II at the University of Utah MEAE program, I created an audio system that can be used to add, load, and play sounds in a custom game engine built upon the starting code provided in the course.

Instruction for Use

Downloadable Source Code

Below is the source code for my Audio and AudioBuilder Visual Studio projects. The Audio project is a static library that should be added to the Engine portion of your solution, and the AudioBuilder project is an executable that should be added to the Tools portion of your solution. More information on how to use these projects can be found below. Remember to add the relevant Properties as discussed in Assignment 01 on Canvas.

Audio Source .ZIP AudioBuilder Source .ZIP

Setting Up References

Audio References

In Visual Studio, right click on the Audio project and add the following references to the project:

  • Asserts
  • Assets
  • Concurrency
  • Logging
  • Math
  • Physics
  • Platform
  • Results

AudioBuilder References

In Visual Studio, right click on the AudioBuilder project and add the following references to the project:

  • AssetBuildLibrary
  • Audio
  • Platform
  • Results

Lua Assets

AssetBuildFunctions.lua

The AudioBuilder is responsible for building both the relevant WAV files and the Audio3DSettings Lua files. Be sure to add the following code to your AssetBuildFunctions.lua.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
NewAssetTypeInfo( "audio", {
    ConvertSourceRelativePathToBuiltRelativePath = function( i_sourceRelativePath )
        local relativeDirectory, file = i_sourceRelativePath:match( "(.-)([^/\\]+)$" )
        local fileName, extensionWithPeriod = file:match( "([^%.]+)(.*)" )
        return relativeDirectory .. fileName .. extensionWithPeriod
    end,
    GetBuilderRelativePath = function()
        return "AudioBuilder.exe"
    end
})

NewAssetTypeInfo("Audio3DSettings", {
    ConvertSourceRelativePathToBuiltRelativePath = function( i_sourceRelativePath )
        local relativeDirectory, file = i_sourceRelativePath:match( "(.-)([^/\\]+)$" )
        local fileName, extensionWithPeriod = file:match( "([^%.]+)(.*)" )
        return relativeDirectory .. fileName .. extensionWithPeriod
    end,
    GetBuilderRelativePath = function()
        return "AudioBuilder.exe"
    end
})

AssetsToBuild.lua

Your assets can then be added to your AssetsToBuild.lua file. Refer to the example below:

1
2
3
4
5
6
7
8
9
10
11
12
return {
    -- ...
    audio = {
        { path = "Audio/HeavyMetalMaster.wav" },
        { path = "Audio/2024-07-08_Spark_FullMix.wav" },
        { path = "Audio/2-pop_1frame_of_1khz.wav" },
    },
    Audio3DSettings = {
        { path = "Audio/Audio3DSettings.lua" },
    },
    -- ...
}

Custom Audio3DSettings.lua

Below is an example of a custom Audio3DSettings.lua file. This file can be used to define the 3D audio settings for your audio clips. View the comments in the example for more information on what each variable does. The values provided below are a great starting point. The most likely setting to change is the curveDistanceScaler, which should be adjusted to match your game’s scale.

1
2
3
4
5
6
7
8
return {
	-- channel radius is the stereo spread of a multichannel sound, 0.0 is mono, 1.0 is full stereo spread
    channelRadius = 0.5,
	-- curve distance scaler is how quickly the sound falls off with distance, should match your game's scale
    curveDistanceScaler = 10.0,
	-- doppler effect scaler, how much the doppler effect is applied
    dopplerScaler = 1.0,
}

Audio Format Requirements

Admittedly, I haven’t tested this extensively. Do your best to stick to the following:

  • Only WAV files are permitted
  • 16-bit or 24-bit
  • 44.1 kHz or 48 kHz
  • Mono or Stereo

Application Setup

iApplication.cpp

It is critical that you call the Initialize and CleanUp functions of the AudioSingleton in iApplication.cpp or in your cMyGame.cpp.

1
2
3
4
5
6
7
eae6320::cResult eae6320::Application::iApplication::Initialize_engine() {
    result = Audio::AudioSingleton::GetInstance().Initialize();
}

eae6320::cResult eae6320::Application::iApplication::CleanUp_engine() {
    result = Audio::AudioSingleton::GetInstance().CleanUp();
}

Using the Audio System

Globals

Below, you will find the declarations for the global variables used in the example code in later sections of this write-up.

1
2
3
4
5
6
7
8
9
10
11
12
std::string s_SparkPath = "data/Audio/2024-07-08_Spark_FullMix.wav";
eae6320::Audio::cAudioClip* s_SparkClip = nullptr;
eae6320::Audio::cAudioPlayer* s_SparkPlayer = nullptr;

std::string s_oneShotPath = "data/Audio/2-pop_1frame_of_1khz.wav";
eae6320::Audio::cAudioClip* s_oneShot = nullptr;

eae6320::Audio::cAudioPlayer3D* s_oneShotPlayer1 = nullptr;
eae6320::Audio::cAudioPlayer* s_oneShotPlayer2 = nullptr;

std::string s_oneShot3DSettingsPath = "data/Audio/Audio3DSettings.lua";
eae6320::Audio::sAudio3DSettings s_oneShot3DSettings;

Loading and Creating

cAudioClip

Loading a cAudioClip is fairly straightforward, following the format of most other assets in the engine.

1
2
3
4
5
6
7
8
// Declaration
static cResult Load(const std::string& i_path, cAudioClip*& o_audioClip);

// Usage
result = eae6320::Audio::cAudioClip::Load(s_SparkPath, s_SparkClip);
s_SparkClip->SetLoop(true);

result = eae6320::Audio::cAudioClip::Load(s_oneShotPath, s_oneShot);

cAudioPlayer and cAudioPlayer3D

Creating a new cAudioPlayer or cAudioPlayer3D is also straightforward. The Create function signature follows the same pattern as the Load function for cAudioClip. You must have a valid cAudioClip to create a new cAudioPlayer or cAudioPlayer3D. Notice that cAudioClip pointers can be shared between multiple cAudioPlayer and cAudioPlayer3D instances.

1
2
3
4
5
6
7
// Declaration
static cResult Create(cAudioPlayer*& o_audioPlayer, cAudioClip* i_clip);

// Usage
result = eae6320::Audio::cAudioPlayer::Create(s_SparkPlayer, s_SparkClip);
result = eae6320::Audio::cAudioPlayer3D::Create(s_oneShotPlayer1, s_oneShot);
result = eae6320::Audio::cAudioPlayer::Create(s_oneShotPlayer2, s_oneShot);

sAudio3DSettings

To load a custom sAudio3DSettings from a Lua file (see above), you can use the Load function of the sAudio3DSettings struct. While the signature is similar to the function above, it notably does not use the same pointer pattern. Instead, the values are directly loaded into the provided sAudio3DSettings struct. Then, you must assign the loaded settings to each cAudioPlayer3D instance.

1
2
3
4
5
6
// Declaration
static cResult Load(const std::string& i_path, sAudio3DSettings& o_attenuation);

// Audio3DSettings Initialization and Assignment
result = eae6320::Audio::sAudio3DSettings::Load(s_oneShot3DSettingsPath, s_oneShot3DSettings);
s_oneShotPlayer1->SetAudio3DSettings(s_oneShot3DSettings);

Update

If using cAudioPlayer3D, you must update the listener and each emitter every frame. The listener should probably be your camera, and the emitters should be attached to their respective actors. To update the listener, call Audio::UpdateListenerPositionAndOrientation and to update the emitters, call Audio::cAudioPlayer3D::SetPositionAndOrientation. Be sure to call Audio::cAudioPlayer3D::Update on each emitter each frame.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// Declarations
void UpdateListenerPositionAndOrientation(
	const Math::sVector& i_position,
	const Math::cQuaternion& i_orientation,
	const Math::sVector& i_velocity
);

void cAudioPlayer3D::SetPositionAndOrientation(
	const Math::sVector& i_position,
	const Math::cQuaternion& i_orientation,
	const Math::sVector& i_velocity
);

virtual void Update(const float i_elapsedSecondCount_sinceLastUpdate);

// Example Usage
void eae6320::cMyGame::UpdateSimulationBasedOnTime(const float i_elapsedSecondCount_sinceLastUpdate) {
    // Updating the listener
    eae6320::Audio::AudioSingleton::GetInstance().UpdateListenerPositionAndOrientation(
        m_mainCamera->m_rigidBodyState.position,
        m_mainCamera->m_rigidBodyState.orientation,
        m_mainCamera->m_rigidBodyState.velocity
    );

    // Updating emitters
    s_oneShotPlayer1->SetPositionAndOrientation(
        s_actor->m_rigidBodyState.position,
        s_actor->m_rigidBodyState.orientation,
        s_actor->m_rigidBodyState.velocity
    );

    s_oneShotPlayer1->Update(i_elapsedSecondCount_sinceLastUpdate);
}

Other cAudioClip Functions

Note that these settings apply to the clip itself, and will not affect any currently playing cAudioPlayer or cAudioPlayer3D instances. You must set these settings before calling Play() on the player.

1
2
3
4
5
// Declarations
void SetLoop(bool i_loop);
void SetVolume(float i_volume);
bool DoesLoop() const;
float GetVolume() const;

Other cAudioPlayer Functions

These are the basic functions for controlling the playback of a cAudioPlayer or cAudioPlayer3D.

1
2
3
4
5
// Declarations
cResult Play();
cResult Pause();
cResult Stop();
bool IsPlaying() const;

Cleanup

Since cAudioClip and cAudioPlayer are reference counted, you can handle them the same way you would handle any other reference counted object by simply calling DecrementReferenceCount() on them when you are done with them.

1
2
3
4
5
// Cleanup
if (s_SparkClip) {
    s_SparkClip->DecrementReferenceCount();
    s_SparkClip = nullptr;
}

Interface for Programmers

Google Slides Presentation (outdated): Engine Audio System Slides

Additional Thoughts

As the code architecture above demonstrates, I utilized all skills that I developed this system to build this system. While this currently only is supported on Windows, the interface is completely platform independent. Additionally, the code is fairly self-contained, relying on only a few other libraries within the engine.

I did struggle a little bit finding a good use for a human-readable lua asset. Other’s who have done audio systems have done human readable metadata files, but that honestly did not seem very useful for this scope. Instead, I decided to follow the model that Unreal Engine and other engines use for attenuation and 3D audio settings. I also struggled a bit with learning XAudio2, but it was surprisingly a bit easier than I expected.