Using the GM:S Audio engine

GameMaker: Studio comes with two different sound engines for controlling the various audio in games: GM:S Audio and Legacy Sound. These systems are completely independent from each other and you can have one system or the other active in a game.

The GM:S Audio engine is the new, more robust sound system that was designed to allow a full 3D soundscape through the use of emitters and listeners. Emitters allow for the positioning in game space where sounds are going to occur. There are functions for adding falloff of the sounds, velocity to emulate movement, and more. Listeners give even more control by dictating how the sound is played based on where the player is in the game, including their orientation and velocity. If you do not declare a listener, then the sounds become universal. This will eventually become the primary audio engine in GameMaker: Studio, but because of the HTML5 audio issues, it doesn't work properly in all browsers.

The Legacy Sound engine is the original sound system that GameMaker used and as the name indicates, this engine is no longer being actively developed and many of the functions have already been made obsolete. It is a much simpler system with no 3D capabilities, though for most games this will be more than enough. The one big benefit this engine has is that the audio should work in all browsers.

We will be using the Legacy Sound engine throughout this book to ensure maximum capability, but we need to know how to use the GM:S Audio engine for the future. Let's test these features by creating a very simple demonstration of positional sound. We are going to create an object in our room and make it play a sound that can only be heard as the mouse approaches the location.

Using the GM:S Audio engine
  1. In order to select which system you are using, click on Resources | Change Global Game Settings. In the General Tab, there is a Use New Audio Engine checkbox; make sure you check this. If it is checked, it is using the GM:S Audio engine; if it is not, then it is using Legacy Sound.
  2. Create a new Sound and name it snd_Effect.
  3. Load Chapter 2/Sounds/Effect.wav. Make sure the Kind is set to Normal Sound.
  4. Create a new Object and name it obj_Sound.
  5. Create a new Script and name it scr_Sound_Create. First, we need to create an emitter and capture it in a variable:
    sem = audio_emitter_create();
  6. Next we will position the emitter to where our object is. The parameters for this function are: the emitter to apply this to and the X/Y/Z coordinates. We will use the object's X and Y, but since this is a 2D example, we will set the Z to 0:
    audio_emitter_position(sem, x, y, 0);
  7. We also want to have a falloff on the emitter so that the sound becomes louder as the listener approaches. The parameters we have are: the emitter, the distance for how far the sound should be at half volume, the total falloff distance, and the falloff factor:
    audio_emitter_falloff(sem, 96, 320, 1);
  8. The emitter is all set up; now let's play the sound on the emitter. The parameters for this function are: the emitter, the sound to play, whether it should loop, and its priority. We will want this to loop so that we can hear the sound:
    audio_play_sound_on(sem, snd_Effect, true, 1);
  9. This code is finished and should look like the following when all put together:
    sem = audio_emitter_create();
    audio_emitter_position(sem, x, y, 0);
    audio_emitter_falloff(sem, 96, 320, 1);
    audio_play_sound_on(sem, snd_Effect, true, 1);
  10. Add a Create event and drag a Control | Execute Script icon into the Actions with this script attached.
  11. The sound will play now, but it will not have a direction until we have a listener. We will move the listener position based on the location of the mouse on every step. Create a new Script and name it scr_Sound_Step.
  12. We only need one line of code for positioning the listener's X/Y/Z coordinates. The X and Y will be set to the mouse X and Y and once again Z is set to 0.
    audio_listener_position(mouse_x, mouse_y, 0);
  13. On the obj_Sound object, add a Step | Step event and drag an Execute Script icon into the Actions with the step script attached.
  14. Open up the room and place the instance of obj_Sound object into the center of the room.
  15. Run the game.

You should be able to hear the sound quietly and as you move the mouse around closer to the center of the screen the louder it should become. If you have surround sound or headphones, you will also notice that the sound moves from the left to the right channels. This is just a sample of what can be done with the GM:S Audio engine and it will be exciting to use once it works in all browsers.

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

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