The Open-Source Game Development Pipeline

Providing the means and education to create games using free/libre open-source tools.

A Code Snippet For You



I’ve finished adding a feature to my game engine framework that I think will be useful for other developers to see the implementation of. It pertains to fading audio out over a set duration.

The code is as follows:

void AudioNode::fadeOut(float seconds){
  ALfloat srcVolume;
  alGetSourcef(this->source[0], AL_GAIN, &srcVolume);
  double rate = srcVolume / ((double)seconds * CLOCKS_PER_SEC);
int sleepRate = srcVolume / (seconds * 1000);
    srcVolume -= rate;
  }while(srcVolume > 0.0f);

The algorithm took me a moment to put together, but basically, what you’re doing is taking the desired duration of the fade effect (to zero gain), multiplying it by the processor clocks per second, and then using it as the divisor of the original gain of the audio source.

Note that this code is implemented in C++ and utilizing OpenAL. You’ll need to import the OpenAL headers and the “time.h” header to achieve this effect. “setVolume()” is a method that contains the OpenAL API call for setting the gain of the audio source (which should be relatively easy to look up).

Hope this was useful! 🙂



2 thoughts on “A Code Snippet For You

  1. I apologize since I don’t know OpenAL very well, but just from a C++ perspective I feel that the do/while loop you have would take a lot of cycles. Normally I would expect to decrease the volume then wait a little while so other threads can do their work, then decrease it again, but in your code it just keeps running without giving other threads a chance. But maybe in the environment you are running in it doesn’t matter?

    • An astute observation!

      I haven’t baked any threads into the framework I’m writing in order to keep it simple. But, inside of my project code (which I use to test the components I’m developing), all of the audio activity is threaded. It really comes down to a matter of scope.

      But, you do bring up a good point! Depending upon the current source’s gain, or length of the duration passed into the method, there could be a slight latency in the responsiveness of the application if it’s not properly threaded at the scope of the caller invoking the AudioNode. The purpose of my article, though, was more about the algorithm itself; how to calculate seconds when there are many computers with different processor speeds, and then determining the rate at which to reduce the gain. At this point, implementation (whether or not threads are baked in, and where) becomes relative.

      I hope that answers your question. 🙂 Also, thank you for expressing an interest in the blog! Discussions like this are not only thought-provoking for myself, but I hope that they can be utilized for the education of other readers, too.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s