GameMaker has a complete audio engine that is based on the *.ogg, *.mp3 and *.wav sound formats.
Sounds of these types added to the IDE can be used in your game using the audio functions shown below.
For things more complex than basic sound effects, or playing a single piece of music, you can refer to the advanced audio functions which let you modify how a sound is played. There is also a selection of more specialised functions dedicated to streaming audio, positioning audio (to give 3D sound), grouping audio, synchronised audio and audio effects.
IMPORTANT When using audio on the HTML5 target, you should be aware that not all browsers support Web Audio and so may not play any sound for your project when run. You can get a general idea of Web Audio support from the following link: Can I Use Web Audio?.
The most straightforward way to play a sound is using audio_play_sound:
audio_play_sound(snd_Explode, 10, false);
At a minimum you have to provide the Sound Asset, a priority for the new sound instance and whether the sound should loop.
Another way to play sounds is using audio_play_sound_ext. This function is identical to audio_play_sound but allows you to pass various audio parameters in a struct. Using this function, you only need to provide a Sound Asset through the "sound" key of the struct:
audio_play_sound_ext({ sound: snd_Explode });
Here the priority will default to 0 and the sound will not be looped. You can supply additional keys in the struct to change the properties of the sound.
NOTE An Audio Playback Ended event is triggered for the sound when it stops playing.
The functions audio_play_sound and audio_play_sound_ext create a new instance of a sound (also called a "voice") and return its ID:
ins_sound = audio_play_sound(snd_Explode, 10, false);
You can use this sound instance ID in functions to change the audio properties at the instance level (or pass them directly as optional arguments). This will only change the properties for that particular sound instance.
In case you pass a sound asset ID to the functions, the audio property at the asset level will be set to the given value.
NOTE audio_play_sound_at and audio_play_sound_on work in the same way and also return the ID of the sound instance they create.
TIP The maximum number of sound instances/voices is 128 by default. This can be changed using the function audio_channel_num.
Every sound has basic properties that are applied on different "levels". Depending on the property, it either acts as a multiplier, or overrides the value set at a previous level.
These audio properties are:
For an overview of all audio properties, levels and how their values are calculated, see Audio Properties Overview.
NOTE The optional parameters that you can pass to the audio_play_sound_* functions set the instance-level value of the property.
A sound can be looped by setting it to loop. This can either be done when playing the sound using any of the audio_play_sound_* functions:
ins_sound = audio_play_sound(snd_Engine, 100, true);
or afterwards, when the sound is already playing, using the function audio_sound_loop:
audio_sound_loop(ins_sound, true);
By default a sound loops from its start to its end. The start and end position can be changed using audio_sound_loop_start and audio_sound_loop_end respectively, also while the sound is playing (i.e. ins_sound refers to a sound instance for which audio_is_playing returns true).
When the track position reaches the loop end position if a sound is set to loop, it continues playing from the start position.
IMPORTANT The loop end position must be past the loop start position. The exception to this is the value 0.0 for loop end, which marks the end of the sound (i.e. audio_sound_length).
See: Audio Loop Points
Finally you can add effects to the audio that you play, such as reverb, echo, delay, etc.
Audio effects are created using audio_effect_create and then assigned to one of the effects slots of an audio bus. The audio bus that you can always assign effects to is the main audio bus audio_bus_main.
See: Audio Effects
If you need positional audio or audio that should play in a three-dimensional environment, you can use Audio Emitters and Audio Listeners.
NOTE Audio Emitters introduce another level for the gain, pitch and listener mask parameters.
All sound assets, except for streamed audio, belong to a single audio group. Sounds in an audio group can be loaded and unloaded at the same time.
Audio groups have their own gain factor (see audio_group_set_gain / audio_group_get_gain)
NOTE All sounds are by default added to an audio group audiogroup_default.
GameMaker supports loading streamed audio directly from .ogg files. For this you can use audio_create_stream and audio_destroy_stream.
For audio that should play perfectly in sync on a per sample level, GameMaker has sync groups. See Audio Synchronisation for more information on this.
When creating games for the HTML5 target platform, the audio engine requires Web Audio support, and this in turn means that sometimes your audio won't play when or how you expect it. This is because the Web Audio context may not be running or may stop running when your game is being played. What causes this varies greatly between browsers, and even between different versions of the same browser, and so detecting when the web audio context status changes is very important, e.g.: to detect when the context changes and pause/start looping audio like background music.
To help with this issue, GameMaker has two separate ways to detect the change in Web Audio context status, either using the following function:
or using the Asynchronous System Event, which will be triggered whenever the Web Audio status changes. In this event you will get the built-in async_load DS map populated with the key "event_type" which in turn will hold the string "audio_system_status" if it is an audio event. When this key exists, there will also be a further "status" key which will be either "available" or "unavailable".
For more information please see the section:
NOTE This event will be triggered on all platforms, but on everything except HTML5 it will only be triggered once on Game Start when the audio engine is first initialised.
To check if audio can be played using Web Audio, you can use audio_sound_is_playable.