TGB/MiniTutorials/Super Simple Audio

From TDN

The purpose of this tutorial is to show you how to create a simple audio engine for your game. Don't worry, it's not as scary as it sounds! Why use an audio engine, you may ask? With an audio engine, you will no longer need to create profiles for every audio file you use, you can change volume on the fly, and you can control what happens when audio starts and stops.

Contents

Create Your Audio Templates

First, let's create your audio templates. A template represents an audio category. We are going to create three categories. One for background music (BGM), background sound (BGS), and sound effects (SE).

  1. Navigate to your project's gameScripts folder.
  2. Create a text file called audio.cs.
  3. Open 'audio.cs in a text editor, and paste in the following code:
    // background music
    new AudioDescription(BGM)
    {
       volume   = 1.0;
       isLooping = true;
       //isStreaming = true;
       is3D     = false;
       type     = $GuiAudioType;
    };
    
    // background sounds
    new AudioDescription(BGS)
    {
       volume   = 0.2;
       isLooping = true;
       isStreaming = true;
       is3D     = false;
       type     = $GuiAudioType;
    };
    
    // sound effects
    new AudioDescription(SE)
    {
       volume   = 1.0;
       isLooping = false;
       isStreaming = false;
       is3D     = false;
       type     = $GuiAudioType;
    };
    
  4. Save audio.cs.

Create Your Audio Profile Generator

Now, lets create a function that generates an audio profile on the fly. An audio profile is an object that contains information about the music or sound that you want to play. For example, the filename for the music and the description (the name of the audio template you are using with the music)

  1. At the bottom of audio.cs, add the following lines of code:
    function getAudio(%name, %type)
    {
       if (isObject(%name))
          return;
       
       new AudioProfile(%name)
       {
          filename = "~/data/audio/" @ %name @ ".ogg";
          description = %type;
          preload = false;
       };
    }
    
  2. Save audio.cs.

Create Your Audio Generators

Next, let's create a couple of functions that will play our audio. I've created one for background music, background sound, and sound effects.

  1. At the bottom of audio.cs, add the following lines of code:
    function playBGM(%sound, %volume)
    {
       // get profile for background music and set default volume
       getAudio(%sound, "BGM"); 
       alxSetChannelVolume($GuiAudioType, %volume);  
    
       // if another bgm is playin, turn it off
       if ($bgm)
          $bgm = alxStopAll(); 
             
       // play bgm
       $bgm = alxPlay(%sound);
    }
    
    function playBGS(%sound, %volume)
    {
       // get profile for background sound and set default volume
       getAudio(%sound, "BGS"); 
       alxSetChannelVolume($GuiAudioType, %volume);  
      
      // if another background sound is playing, turn it off. 
       if ($bgs)
          $bgs = alxStopAll();
             
       // play background sound
       $bgs = alxPlay(%sound);
    }
    
    function playSE(%sound, %volume)
    {
       // get profile for sound effect
       getAudio(%sound, "SE");    
       
       // play sound
       SE.volume = %volume;
       $se = alxPlay(%sound);
    }
    
  2. Save audio.cs and close.

How to use in your code

Now that you have your audio engine set up, you are ready to use it in your game. To use the audio engine, use these calls in your game:

  1. playBGM(%music, %volume)
  2. playBGS(%sound, %volume)
  3. playSE(%soundeffect, %volume)
For example:
playBGM("village-theme", 1.0);
playBGS("rain", 0.5);
playSE("footsteps", 1.0);