SOLVED Looping music often glitches


Hello all,

In the past, I have had relative success with looping music using a process similar to the one below.

The following script is executed by a single persistent object, every step without exception. I have verified that only one instance of this object exists at any given time.

The game also runs at 60fps, if it matters.

What is weird is that on the first loop of any given song, when the track position is reset, it only seems to go half the difference between loop_end and loop_start.

I also notice that while I run debugging diagnostics that the track position tends to flicker back and forth slightly while the track is playing.

Here is my script for looping the music.

Tracks the audio track position in the presently playing BGM. Depending on what the
current music is supposed to be, the track is automatically looped at the appropriate

if World.current_music_id == noone {

var loop_start, loop_end;

switch(World.current_music) {
    case "INITIAL":
        loop_start = 9.251;
        loop_end = 86.466;
    case "MENU":
        loop_start = 32.030;
        loop_end = 96.030;
    case "RECOUNT":
        loop_start = 33.721;
        loop_end = 106.548;
    case "WIN":
        loop_start = 28.681;
        loop_end = 85.994;
        show_error("loop_music(): no start and stop values defined for current_music = " + World.current_music, true);

var dur = loop_end - loop_start;
var tpos = audio_sound_get_track_position(World.current_music_id);
if tpos >= loop_end {
    audio_sound_set_track_position(World.current_music_id, tpos - dur);
I am using Game Maker Studio v1.4.

[EDIT] One more data point to note: when the first loop gets executed, for some reason it always gets set to about the ballpark of 2 * loop_start. This leads me to believe that maybe my math is off, but I just need another set of eyes to tell me what I'm doing wrong.

[EDIT 2] I found a solution. Apparently there is too much latency between whatever controls the audio and what controls the I/O to the calling object. I put in a buffer of one second after every time the loop is executed, and that alone seemed to prevent the over-correction of the track position.
Last edited: