Making a Basic FMOD Audio Engine in C++12 Apr 2016
In my previous post, Setting Up Xcode and Visual Studio for FMOD Development, I talked about getting your IDE environment ready for creating an FMOD audio engine implementation. In this tutorial I’ll go over creating a basic audio engine that you can use in your C++ projects to add quick and easy dynamic audio. This engine will be able to handle both single audio files and FMOD Studio Events to give you lots of flexibility. So let’s get to the code.
I will say, like my last tutorial, this isn’t a tutorial on how to use FMOD Studio. While you’ll be able to use FMOD Studio events with this audio engine, this tutorial is solely concentrating on the programming side of FMOD. If you want a good tutorial on FMOD Studio you should check out the tutorial series from my friend Chris Prunotto over on his site SoundGuyChris.com.
Also a big shout out to Guy Somberg who inspired me to write these tutorials and actual originally created a lot of this code in his GDC talks. If you have a chance I highly recommend you checking them out on the GDC Vault.
Table of Contents
This basic engine is going to be broken down into two major parts: an Implementation struct that will handle the basic calls to the FMOD API and an AudioEngine class that will handle all the logic for loading, unloading, playing, stopping, and changing sounds. When you implement this audio engine into your project the only thing you’ll be interacting with is the AudioEngine class. So Let’s get started with the header file.
Now I do want to say I’m not an expert at C++ by any standard. But this being said I’ll try to go over everything I did and explain it the best I can. If you have any tips or questions feel free to leave them in the comments section at the bottom of the page. I’d love to get your feedback.
So to start off let’s create an AudioEngine.h file. This will hold all the declarations of our structs and class that we’ll use in this engine. To start out let’s put an #ifndef statement at the top of our header and close it off like this:
What this is doing is checking if AUDIO_ENGINE_H has been defined before. If it hasn’t then go ahead and include all the information in our header. This is to prevent multiple definitions of the objects in our header. If we were to include this header into two different files in our project it would only show up once to the compiler. This is helpful to prevent some weird possible bugs when we compile our code. If you want to know more about this you can check out this site.
So now what do we need to include in our header. Well for starters we need the FMOD Studio headers which are “fmod_studio.hpp” and “fmod.hpp”. Both of these headers are where we’ll get all our calls to the FMOD API. Now for engine itself we’ll need a few standard library things which are: <map>, **
One little line you’ll want to include after your Includes is:
This will save you a lot of writing Std:: in front of basic items like strings and maps.
Now that our file knows what we are going to be using let’s start creating some basic things in our code the next thing you’ll want to create is a struct called Vector3. A struct is basically a container that we can predefine what variables are going to be in it. We need this Vector3 struct to place sound in 3D space if our projects require that. So after the using namespace std; (and before #endif) we’ll create our struct:
The Implementation struct is going to contain most of our calls to the FMOD API. We separate these calls and the actual audio engine class itself to try and prevent any weird bugs from popping up. The struct is going to contain the code for initializing and shutting down the FMOD engine as well as hold instances of both the Studio and Low-Level system objects for FMOD. Implementation will also hold a map of all the sounds and events we’ve played in our projects. A map is just similar to an array or vector except that all objects are linked to a key. In this case the file name of our event/sound will be the key which will return either the sound or event. And the last thing the struct will do is call an update to FMOD to update the status of all events and sounds. The Implementation struct looks like this:
The last thing in the header is the deffinition of the audio engine. The engine class will do calls to the Implementation struct to start, stop, and update FMOD. The engine will also handle basic things like loading, playing, stoping, and updating information on sounds and events. We’ll go over each function in more detail when we write the logic behind them. For now the Audio Engine class should look like this:
And that’s it for the header file. If you need to look at he full header file you can view it on my Github. Now we can get to writing the good stuff. What is actually going to make our audio engine tick.
Audio Engine Source Code
Now we can start getting to work on the real task here which is getting sound happening in our project. The header file was us just telling the program what functions and variables can exists where are source file is what contains the actual logic. Let’s start by creating an AudioEngine.cpp file and we’ll jump right in.
The first thing we need to do is tell the file that we are using the AudioEngine.h header file. To do with we write:
This is where we will initialize the underlying FMOD system that will allow us to play sounds. We’ll start with the Implementation constructor which creates the FMOD Studio and Low-Level systems and set’s it’s variables.
CAudioEngine::ErrorCheck is just a way for us to check that all FMOD calls are successful and we’ll cover that later. As you can see the first thing we do is create the FMOD Studio System that handles all events and sounds. We then initialize the system and that takes in the number of channels, then flags that can change the way the system runs. The FMOD_STUDIO_INIT_LIVEUPDATE is a really cool feature where you can connect to your game with FMOD Studio and live mix the audio. Then to allow us to handle things at a lower level we call getLowLevelSystem which gives us the Low-Level system.
Next we create the deconstructor which cleans up FMOD and makes sure we don’t leave anything behind. It’s super simple and we just unload all assets and then shutdown FMOD.
Now we work on the update function of the Implement struct. In this function we check if a channel has stopped playing, if it has, we destroy it so we can clear up a channel to use. Other than that we just call the update function on the FMOD system to update the event sounds.
And then finally we create an instance of the Implementation so that we can actually use it.
The first two functions we’ll add are the Init and Update functions which are super simple. We just need to create the Implementation and call its update.
Now we need to be able to load sounds. We’ll take in the filename along with some parameters about streaming, looping, and whether or not it’s a 3D sound and then load that sound and store it in our sound map.
Now we need to be able to unload sounds to free up memory. Luckily unloading is a lot easier. We just take in the file name, look it up in the sound map and release the sound.
Now we come to the largest function we have which is PlaySounds. Even though it’s a little large it’s actually quite simple. We first see if we have that sound in our sound map. If not we then load it. If we still can’t find it then that means something went wrong and we can’t play the sound. If we found the sound just fine then we create a new channel to house the sound and tell the sound to play, but we start the sound paused. This is so we don’t get a pop in the audio when we set parameters. If the channel was set right then we update all the possible parameters like volume and position and then unpause the sound. And finally we return the channel id encase we refer to it later.
The next two functions are simple functions that allow us to set the volume and position of a sound. We just tell it what channel to change and what to change it to.
Now we’ll work on loading and playing events. For me events are super powerful tools so I prefer them over programmer sounds. The first thing we have to do is load the banks. Banks are what stores all the sounds and information for each event. We load them much like we loaded the sounds.
The next thing we have to do is load events. Each event stored in a bank has to be loaded separately which helps save memory. We load Events like everything else except we load it in two parts: EventDescription and EventInstance. The description is the information and the instance is what actually plays the sound.
Now let’s go and play the event. We just look to see that the event has been loaded, if it hasn’t we load it, and then tell it to play.
We do the same thing for stopping the event except we don’t care if it’s loaded.
It’s always important to see if an event is playing or not. To do this we have to get the playback state of the event which tells us if it’s currently being played.
The next two functions really let events shine. It’s the ability to get and set parameters of events dynamically. This allows sound designers to create soundscapes for any instance. To do this we just get the event then pass it a string of what parameter we want to see or change and then act accordingly.
Now we have three functions that we’ve actually being using just to reduce how much code we have to write. These are simple math functions to convert from linear volume to dBs and converting from our Vector3 to FMOD’s Vector3.
We also have a little function that does FMOD error checking for us. This has come in super handy when something won’t load right or the projects sound isn’t happening right.
And finally we just need a function to clean everything up and this just deletes the Implementation.
And that’s it!! In under 300 lines of code we have a flexible working audio engine for your projects. Now from here the sky’s the limit. Audio is a huge part of any project so you as a programmer should treat it just as importantly as graphics and gameplay. Hopefully this tutorial has helped a bit in that process. Stay tuned for my next tutorial which will be on adding a state machine and some advanced features to our simple audio engine. Feel free to comment down below if you have any tips, tricks, or need help with anything.
Thanks for reading!