FLOSS Game Dev

Enabling Creativity


Leave a comment

One Track Mind: Preparing Your Audio for 3D Games

Hello!

First off, I want to express my condolences to those who’ve suffered as a result of the Paris attacks that just happened on the 13th. As an American of French descent, my heart and spirit go out to you during this trying time.

Today we will be covering a very important aspect of 3D game development… Audio! If you’ve ever wanted to create a game so immersive that you feel like you’re actually there, you know that you’re going to need sounds to come from every possible direction of the aural spectrum.

Thankfully, most 3D Audio APIs already provide this functionality, and one of them (OpenAL) just happens to be cross platform! Joy! 😀

To begin with, it’s important that you understand the difference between stereophonic and monaural sound. Stereophonic sound utilizes two channels to create the illusion of depth, whereas Monaural sound only contains one channel (as indicated by the title of this article) and is only played from one position.

How does this factor into a 3D game?

OpenAL can handle both three-dimensional and two-dimensional audio tasks with relative ease. For your own purposes, though, you’ll most likely use stereophonic audio for background music and sound effects that are localized to the player’s user experience (going through menus, or anything not directly-related to the action on screen). You’ll use monaural audio, instead, for all in-game sound effects, and/or positional dialogue exchanged between the Player-Character (PC) and Non-Player Characters (NPCs), thereby creating a natural feeling of depth.

So, now that we understand which types of sound assets are best fit for 3D manipulation, why do they need to be Monaural in the first place?

OpenAL needs assets meant to be manipulated in 3D space to be monaural because only monaural tracks have the ability to be processed that way (i.e. have their “center” adjusted) by the API. A monaural track is, by design, intended to be broadcast from one point specifically. OpenAL, by utilizing its three-dimensional audio-processing capabilities, allows you to manually define what that point can be. In the case of stereophonic audio, on the other hand, the file in question has already been processed to create faux three-dimensional depth prior to being consumed by the API. Thus, wherever OpenAL tries to play stereophonic sound in 3D space, the result will sound the same, regardless.

My audio content pipeline is currently the following:

-Make music/audio in Linux MultiMedia Studio

-IF it’s music, compress it to .OGG when exporting

-IF it’s a sound-effect, don’t compress it (use .WAV)

-IF I have sound effects, open Audacity

–Open the .WAV sound effect files within Audacity (one at a time)

–Manually remove the second track of each .WAV sound effect file using (“Tracks” -> “Stereo Track to Mono”)

–Export each audio track with (“File” -> “Export Audio…”) and save it again as a .WAV file

At that point, your audio should be ready for 3D manipulation by any three-dimensional audio API! Time to celebrate! 🙂

As always, questions and comments are welcome.

Vive la France!  🙂


Leave a comment

Update: 8-18-15

Hello!

I’ve recently finished the implementation of my 3D Audio framework, which has been built on top of OpenAL. You can perform two-dimensional and three-dimensional audio tasks with relative ease.

I’m currently looking into creating a Networking solution for my framework at the moment. My plan is to utilize BSD sockets, and for this portion of the framework to be client-side only. There are numerous server-side solutions available, so there’s no need to add another wheel to the tire yard.

I know that part of the purpose of this blog is to educate readers about game development, and not simply write about getting a decent computer (and what software to put on it). I apologize for how long it’s taken me to write a tutorial about the subject. One of the reasons I’m making this framework is to create an easy-yet-comprehensive environment for aspiring game developers; so they can iterate quickly while still becoming educated about languages, patterns, and algorithms. At the end of the day, game development is just another form of software engineering.

As for the tools, I’ll endeavor to find suitable sources to whet your appetite in the meantime. Something like Linux MultiMedia Studio will require a comprehensive introduction in order to guarantee that a wide audience will begin to understand the application (Blender as well). I personally don’t think there’s much of a learning curve… but you never know.

In more familiar news :P, I bought a budget computer for under $400.00 USD that hits all of the bullet-points in my criteria! I’ll post a link to it in my next budget computer update.

Until next time! 🙂


2 Comments

A Code Snippet For You

Hello!

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);
  do{
    srcVolume -= rate;
    this->setVolume(srcVolume);
  }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! 🙂

Cheers.


Leave a comment

And then there was Audio…

Hello!

I’ve made some more progress with my game engine. I’m currently using OpenAL directly (without ALUT, I might add… >_>), and am trying to create a cross-platform threading interface in order to spawn sounds/music off of the Main Thread. This way it can focus on Drawing instead.

As I’ve begun learning about Windows threads and PThreads (POSIX), I’ve noticed that they both have practically identical interfaces. So, it made me wonder… what’s the point of Windows?

And of course, it hit me. Windows is a reaction to UNIX in the same way that GNU/Linux & FreeBSD are a reaction to UNIX & Windows. I realized that the redundancy served a purpose if it made computers more accessible, which it has.

Even if you’ve got a grudge against Microsoft, you cannot deny that they’ve helped make computers more mainstream. Today’s issues, however, stem from an overabundance of control/monetization (how Microsoft/Apple are acting) in the face of even more-accessible/free alternatives.

So, let me tie this back into a discussion about audio; where the redundancy actually made things less accessible (strangely enough).

At first, I tried using several 3rd-party tools, “CAudio” and “OpenAL Soft”, to name a few. One failed to compile, and the other had a complicated interface. At this point, I decided to simply roll my own wrapper around OpenAL (the implementation of which was far less dense & confusing than when compared to OpenGL, thankfully), and I haven’t looked back. This is why I preach the fundamentals to other computer scientists/software engineers I meet. If you become too reliant upon the work of others, you will eventually encounter an obstacle too daunting for shortcuts.

I implore you to at least make and effort to learn native tools so you can always understand what’s being abstracted, instead of immediately relying upon a 3rd-party solution. More often than not, the problem is something that can be easily overcome if you just do a little bit of homework.

With that said, don’t hesitate to reach out if you’re still trying to learn LMMS, Blender, or GIMP (or any other open-source tools)!

Cheers!

(P.S. I’m currently building my engine so that it will be compatible with Windows, GNU/Linux, & FreeBSD. Expect some articles on how to use FreeBSD in the near-to-distant future.)