Audio Loop Points allow you to loop a part (or section) of an audio asset.
While you can manually control audio playback using audio_sound_get_track_position and audio_sound_set_track_position, this will never be accurate for looping a section, as audio is processed at a much higher rate, e.g. 44100 or 48000 times a second (the sample rate), which is a lot more than the default game speed of 60. This means that many samples may have already played between the time you call one function and the next.
By using the functions provided for audio looping, the looping is performed on the audio thread, at audio rates.
NOTE You cannot use buffer-based audio queues or audio sync groups with the audio looping functions. All other kind of audio can be used without problems, including buffer sounds and streams.
Here are the GML Code functions for audio looping:
Every sound asset and sound instance has a single loop section, defined by a start and end time in seconds. The part of the sound between this start and end will play looped if the sound is set to loop.
Every sound that you play using any of the audio_play_sound_ functions gets its default loop start and loop end time from the asset when played, e.g.:
audio_sound_loop_start(snd_loop, 2);
audio_sound_loop_end(snd_loop, 6);
ins_sound = audio_play_sound(snd_loop, 100, true);
audio_sound_loop_end(ins_sound, 10);
By default, a sound is set to loop from the beginning to the end. This corresponds to a start value of 0.0 seconds and an end value of 0.0 seconds (corresponding to audio_sound_length).
The loop end position must be after the loop start position, except when they are both set to 0.0 (i.e. loop the sound from start to end).
Setting the loop state of a sound instance can be done in two ways. The first is when playing the sound using audio_play_sound or any of the other audio_play_sound_ functions:
ins_sound = audio_play_sound(snd_loop, 100, true);
The second is by calling the function audio_sound_loop on an already playing sound:
audio_sound_loop(ins_sound, true);
NOTE When a sound's track position reaches the loop end position GameMaker checks whether it should loop. If at that moment its loop state is set to true, the sound will jump back to the loop start position. If not, it will continue playing the sound until the end, unless you change either the track position or the loop end.
The following are a couple of ways in which you can use audio loop points to loop audio.
In this situation you have an audio file with an intro, a looping mid-section and an outro. The audio is played from the start, plays the intro part first, then moves to the looped part and keeps playing this looped until the audio is set to no longer loop. At that point the looped part will finish playing a last time to then transition to the outro part.
audio_sound_loop_start(snd_music, 10);
audio_sound_loop_end(snd_music, 20);
ins_sound = audio_play_sound(snd_music, 100, true); // Play the sound looped
Once the sound is in the looping part, the sound instance can then be set to no longer loop:
audio_sound_loop(ins_sound, false);
After changing this, the remainder of the looping part will be played, followed by the outro part.
NOTE Once the sound has stopped playing ins_sound will no longer refer to a valid sound instance and you will need to play a new one using audio_play_sound.
Every sound has one loop section. You can add multiple loop sections yourself by storing them in a struct and manually applying the start and end of the loop section you want to use at any given moment:
loops = [
{start: 0, end: 10},
{start: 10, end: 20},
{start: 20, end: 30}
];
section = 2;
audio_sound_loop_start(snd_several_sections, loops[section].start);
audio_sound_loop_end(snd_several_sections, loops[section].end);
ins_sound = audio_play(snd_several_sections, 200, true);
Using the BPM of a piece of music you can calculate the time one "beat" takes. This way you can loop parts of a sound that correspond precisely to beats or bars in the music:
var _bpm_to_sec = time_bpm_to_seconds(126);
var _beats_per_bar = 4;
var _bars = 4;
audio_sound_loop_start(snd_bgm, _bars * _beats_per_bars);
This code sets the loop start position for a sound asset snd_bgm to the start of the fifth bar.
See: