Initializing FMOD

The first code that we need to add is the code that will initialize the audio engine. Just like we must initialize OpenGL, the code will set up FMOD and check to see if there are any errors along the way.

Open RoboRacer2D.cpp and add the following code to the variable declarations area:

FMOD::System* audiomgr;

Then add the following function:

bool InitFmod()
{
  FMOD_RESULT result;
  result = FMOD::System_Create(&audiomgr);
  if (result != FMOD_OK)
  {
    return false;
  }
  result = audiomgr->init(50, FMOD_INIT_NORMAL, NULL);
  if (result != FMOD_OK)
  {
    return false;
  }
  return true;
}

This function creates the FMOD system and initializes it:

  • First, we define a variable to catch FMOD error codes
  • The System_Create call creates the engine and stores the results in audiomgr
  • We then initialize FMOD with 50 virtual channels, normal mode, and

Finally, we need call the InitAudio function. Modify the GameLoop function, adding the highlighted line:

void GameLoop(const float p_deltatTime)
{
  if (m_gameState == GameState::GS_Splash)
  {
    InitFmod();
    BuildFont();
    LoadTextures();
    m_gameState = GameState::GS_Loading;
  }
  Update(p_deltatTime);
  Render();
}

Virtual channels

The most significant feature that FMOD provides for us is virtual channels. Each sound that you play has to have its own channel to play on. The number of physical channels to play audio varies from device to device. Early sound cards could only handle two to four channels of sound at a time. Modern sound cards may be able to handle eight, sixteen, or even more.

It used to be up to the developer to make sure that the number of sounds playing at any one time did not exceed the number of channels on the hardware. If the game triggered a new sound and no channel was available, then the sound wouldn't play. This led to choppy, unpredictable audio.

Fortunately, FMOD handles all of this for us. FMOD uses virtual channels, and allows you to decide how many virtual channels you want to use. Behind the scenes, FMOD decides which virtual channels need to be assigned to a hardware channel at any given time.

In our code example, we initialized FMOD with 50 virtual channels. This is actually way more that we will use in this game, but it wouldn't be outrageous for a full game. When considering how many virtual channels to assign, you should think about how many audio sources will be loaded at any particular time. These sounds won't all be playing at one time, just available to play.

Channel priority

FMOD can't make your hardware play more simultaneous sounds than it has physical sound channels, so you may wonder why you would ever assign more virtual channels than there are hardware channels.

The first answer to this question is that you really don't know how many hardware channels will be available on the system where a player is actually playing your game. The use of virtual channels takes this concern away from you.

The second answer is that virtual channels allow you to design your audio as if you really had 50 (or 100) channels available to you. FMOD then takes care of managing those channels behind the scenes.

So, what happens if your game needs to play a ninth sound and there are only eight physical channels? FMOD uses a priority system to decide which of the current eight channels is no longer needed. For example, channel seven may be assigned to a sound effect that is no longer playing. FMOD then assigns channel seven to the new sound that wants to play.

If all physical channels are actually playing a sound right now and FMOD needs to play a new sound, then it chooses the channel with the lowest priority, stops playing the sound on that channel, and plays the new sound. Factors that determine priority include:

  • How long ago the sound was triggered
  • Whether a sound is set to loop continuously
  • The priority assigned by the programmer using the Channel:setPriority or Sound::setDefaults functions
  • In 3D sound, how far away the sound is
  • The current volume of the sound

So, you can still end up with sounds that drop out if your sound design exceeds the number of simultaneous, physical channels. But FMOD does its best to limit the impact this will have.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.216.231.245