GameMaker Music repeat

D

Deftera

Guest
How do I set that my game's BGM to repeat at a certain point in the beginning when it ends? I have this song which the beginning is a drum intro, but I don't want that to be the beginning when the song repeats
 
D

dannyjenn

Guest
You could split the music into two files such that the intro and the looping part are separate, and then you could do something like this:
Code:
// create event:
audio_play_sound(bgm_intro,0,false);
Code:
// step event:
if(!audio_is_playing(bgm_intro) && !audio_is_playing(bgm_loop)){
    audio_play_sound(bgm_loop,0,true);
}
I haven't really used sound much yet though, so I'm not sure how well this will work, or whether it will be perfectly seamless. There's probably some better way of doing this.
 
D

Deftera

Guest
You could split the music into two files such that the intro and the looping part are separate, and then you could do something like this:
Code:
// create event:
audio_play_sound(bgm_intro,0,false);
Code:
// step event:
if(!audio_is_playing(bgm_intro) && !audio_is_playing(bgm_loop)){
    audio_play_sound(bgm_loop,0,true);
}
I haven't really used sound much yet though, so I'm not sure how well this will work, or whether it will be perfectly seamless. There's probably some better way of doing this.
I don't want to seperate the audio into 2 files, it wastes disk space
 
This is a quick idea:

Code:
var track_length = audio_sound_length(BGM)   // Returns the audio length in seconds
var track_pos = audio_sound_get_track_position(BGM);   // Returns the current track position in seconds
var loop_pos = 30      // Whatever value (in seconds) you want the track to be restarted at

if (track_pos >= track_length) {
   audio_set_track_position(BGM,  loop_pos);
}
I didn't test it but It should work. If not, try to experiment with those functions.
 
D

Deftera

Guest
This is a quick idea:

Code:
var track_length = audio_sound_length(BGM)   // Returns the audio length in seconds
var track_pos = audio_sound_get_track_position(BGM);   // Returns the current track position in seconds
var loop_pos = 30      // Whatever value (in seconds) you want the track to be restarted at

if (track_pos >= track_length) {
   audio_set_track_position(BGM,  loop_pos);
}
I didn't test it but It should work. If not, try to experiment with those functions.
Are you sure that it actually works?

this line:
var track_pos = audio_sound_get_track_position(BGM); // Returns the current track position in seconds
The BGM thingy is asked to enter an INDEX instead of a SOUNDID, which I'm confused.

This line:
audio_set_track_position(BGM, loop_pos);
The command does not exist, I checked the documentation whether there is a command that uses the format in the brackets, but no, there isn't.
 
D

Deftera

Guest
Guess I should be posting some of my codes:

The sounds are chosen from my music menu object which is coded as obj_musicroom.

The create event:
Code:
menu_x = x;
menu_y = y;
button_h = 32;

//Buttons
button[0] = "1. Outbreak of the Phantoms"
button[1] = "2. Burning Frost"
button[2] = "3. Chilling Ember ~ Esper's Wrath"
button[3] = "4. Saturnine Necromancer ~ Shadows Darken the Soul"
button[4] = "5. Envious Shadows"
button[5] = "6. Magic-Colored Poison"
button[6] = "7. Slothful Magician ~ The Venom Sleeper"
button[7] = "8. Blessings of the Sea ~ The Sea God"
button[8] = "9. Tides of Gluttony ~ Disciple of Monday"
button[9] = "10. Graceful Plains of Dyrica"
button[10] = "11. Artemis's Pride ~ Graceful Hawkeye"
button[11] = "12. Everlasting Reaper"
button[12] = "13. Reaper of Avarice ~ Doomsday Clock"
button[13] = "14. Chromic Dissection ~ The Last Sin"
button[14] = "15. Lustful Blades"
button[15] = "16. Virtues of Eternity"
button[16] = "17. Heavenly Virtues ~ Tenrai's Virtue Battlefield"
button[17] = "18. Phantoms' Return to Homeland"
button[18] = "19. Ghastly Dream"
buttons = array_length_1d(button);

menu_index = 0;
last_selected = 0;
The Key Press event: (The button is Z)
Code:
switch(menu_index)
{
    case 0:
    audio_stop_all() audio_play_sound(phan1,1,false);
    break;
    case 2:
    audio_stop_all() audio_play_sound(esperwrath,1,true);
    break;
    case 4:
    audio_stop_all() audio_play_sound(envious,1,true);
    break;
    case 6:
    audio_stop_all() audio_play_sound(magician,1,true);
    break;
    case 8:
    audio_stop_all() audio_play_sound(tides,1,true);
    break;
    case 10:
    audio_stop_all() audio_play_sound(artemis,1,false);
    break;
    case 12:
    audio_stop_all() audio_play_sound(reaper,1,false);
    break;
    case 14:
    audio_stop_all() audio_play_sound(blades,1,false);
    break;
    case 16:
    audio_stop_all() audio_play_sound(tenrai,1,false);
    break;
}
The song whom I want it to repeat at a certain point instead of the intro is the one with the "tides" soundid
 
Guess I should be posting some of my codes:

The sounds are chosen from my music menu object which is coded as obj_musicroom.

The create event:
Code:
menu_x = x;
menu_y = y;
button_h = 32;

//Buttons
button[0] = "1. Outbreak of the Phantoms"
button[1] = "2. Burning Frost"
button[2] = "3. Chilling Ember ~ Esper's Wrath"
button[3] = "4. Saturnine Necromancer ~ Shadows Darken the Soul"
button[4] = "5. Envious Shadows"
button[5] = "6. Magic-Colored Poison"
button[6] = "7. Slothful Magician ~ The Venom Sleeper"
button[7] = "8. Blessings of the Sea ~ The Sea God"
button[8] = "9. Tides of Gluttony ~ Disciple of Monday"
button[9] = "10. Graceful Plains of Dyrica"
button[10] = "11. Artemis's Pride ~ Graceful Hawkeye"
button[11] = "12. Everlasting Reaper"
button[12] = "13. Reaper of Avarice ~ Doomsday Clock"
button[13] = "14. Chromic Dissection ~ The Last Sin"
button[14] = "15. Lustful Blades"
button[15] = "16. Virtues of Eternity"
button[16] = "17. Heavenly Virtues ~ Tenrai's Virtue Battlefield"
button[17] = "18. Phantoms' Return to Homeland"
button[18] = "19. Ghastly Dream"
buttons = array_length_1d(button);

menu_index = 0;
last_selected = 0;
The Key Press event: (The button is Z)
Code:
switch(menu_index)
{
    case 0:
    audio_stop_all() audio_play_sound(phan1,1,false);
    break;
    case 2:
    audio_stop_all() audio_play_sound(esperwrath,1,true);
    break;
    case 4:
    audio_stop_all() audio_play_sound(envious,1,true);
    break;
    case 6:
    audio_stop_all() audio_play_sound(magician,1,true);
    break;
    case 8:
    audio_stop_all() audio_play_sound(tides,1,true);
    break;
    case 10:
    audio_stop_all() audio_play_sound(artemis,1,false);
    break;
    case 12:
    audio_stop_all() audio_play_sound(reaper,1,false);
    break;
    case 14:
    audio_stop_all() audio_play_sound(blades,1,false);
    break;
    case 16:
    audio_stop_all() audio_play_sound(tenrai,1,false);
    break;
}
The song whom I want it to repeat at a certain point instead of the intro is the one with the "tides" soundid

Ok, my bad. I typed "audio_set_track_position()" instead of "audio_sound_set_track_position()".
As the function requires a soundid as argument, you could create a variable which stores the music is currently playing and then access it in the step event to change its playback position.

Code:
// CREATE EVENT
current_bgm = -1;
Code:
// KEY PRESS EVENT
The Key Press event: (The button is Z)
switch(menu_index)
{
    ...
    ...
    case 8:
    audio_stop_all(); current_bgm = audio_play_sound(tides,1,true);
    break;
    ...
    ...
}
Code:
// STEP EVENT

if (current_bgm != -1) {
   var track_length = audio_sound_length(current_bgm)   // Returns the audio length in seconds
   var track_pos = audio_sound_get_track_position(current_bgm);   // Returns the current track    position in seconds
   var loop_pos = 30      // Whatever value (in seconds) you want the track to be restarted at

   if (track_pos >= track_length) {
      audio_sound_set_track_position(current_bgm,  loop_pos);
   }
}
Check the advanced audio functions: https://docs.yoyogames.com/source/dadiospice/002_reference/game assets/sounds/index.html
 
D

Deftera

Guest
Ok, my bad. I typed "audio_set_track_position()" instead of "audio_sound_set_track_position()".
As the function requires a soundid as argument, you could create a variable which stores the music is currently playing and then access it in the step event to change its playback position.

Code:
// CREATE EVENT
current_bgm = -1;
Code:
// KEY PRESS EVENT
The Key Press event: (The button is Z)
switch(menu_index)
{
    ...
    ...
    case 8:
    audio_stop_all(); current_bgm = audio_play_sound(tides,1,true);
    break;
    ...
    ...
}
Code:
// STEP EVENT

if (current_bgm != -1) {
   var track_length = audio_sound_length(current_bgm)   // Returns the audio length in seconds
   var track_pos = audio_sound_get_track_position(current_bgm);   // Returns the current track    position in seconds
   var loop_pos = 30      // Whatever value (in seconds) you want the track to be restarted at

   if (track_pos >= track_length) {
      audio_sound_set_track_position(current_bgm,  loop_pos);
   }
}
Check the advanced audio functions: https://docs.yoyogames.com/source/dadiospice/002_reference/game assets/sounds/index.html
It's not working.... I tried to tamper with the step event by changing all "current_bgm" into "tides" or just change a few of them, but it won't loop to the 30 second mark of the song (didn't change cause I'm testing)
 
D

Deftera

Guest
Nevermind I give up, I tried so many ways and I can't even make it loop to the second
 
Last edited by a moderator:
Separate the intro from the looped section.
Play intro first. When intro is done playing, play looped section.

It shouldn't waste anymore disc space than you already have.
 
D

Deftera

Guest
Separate the intro from the looped section.
Play intro first. When intro is done playing, play looped section.

It shouldn't waste anymore disc space than you already have.
Read my code above, I tried it and it won't work (I did actually try it with seperate files)
 
Read my code above, I tried it and it won't work (I did actually try it with seperate files)
Assuming you have two files, intro_theme and full_loop.
intro_theme is the beginning part (that has been split)
full_loop is the part that loops fine

Play your intro theme:
Code:
audio_play_sound(intro_theme, 0, false);
In a step event that is running while that is playing:
Code:
if(!audio_is_playing(intro_theme) && !audio_is_playing(full_loop)){
  audio_play_sound(full_loop, 0, true);
}
 
Please keep AlexDerFerris Code. But try this in your step event instead:

Code:
if (audio_is_playing(tides)) {
    if (audio_sound_get_track_position(current_bgm) >= (audio_sound_length(current_bgm) - 0.02)) { 
        audio_sound_set_track_position(current_bgm, 12.5);
    }
}
The code checks if "tides" is playing. If "tides" is playing, the instance named "current_bgm" must be playing. Right before the current playing instance reaches the end (audio_sound_length(current_bgm) - 0.02) the new position is set (12.5 seconds in this case). if 0.02 isn't working, try 0.04 or something bigger instead.

Audio files actually never reach the end. At least in GMS 1.4. They loop or stop a millisecond before reaching the actual track length.

Alternatively you could also use soundbuffers to split the sound in memory.
 
D

Deftera

Guest
Assuming you have two files, intro_theme and full_loop.
intro_theme is the beginning part (that has been split)
full_loop is the part that loops fine

Play your intro theme:
Code:
audio_play_sound(intro_theme, 0, false);
In a step event that is running while that is playing:
Code:
if(!audio_is_playing(intro_theme) && !audio_is_playing(full_loop)){
  audio_play_sound(full_loop, 0, true);
}
This is exactly the one I have tried -.-
I already placed my code above, the 6th reply
 
D

Deftera

Guest
Please keep AlexDerFerris Code. But try this in your step event instead:

Code:
if (audio_is_playing(tides)) {
    if (audio_sound_get_track_position(current_bgm) >= (audio_sound_length(current_bgm) - 0.02)) {
        audio_sound_set_track_position(current_bgm, 12.5);
    }
}
The code checks if "tides" is playing. If "tides" is playing, the instance named "current_bgm" must be playing. Right before the current playing instance reaches the end (audio_sound_length(current_bgm) - 0.02) the new position is set (12.5 seconds in this case). if 0.02 isn't working, try 0.04 or something bigger instead.

Audio files actually never reach the end. At least in GMS 1.4. They loop or stop a millisecond before reaching the actual track length.

Alternatively you could also use soundbuffers to split the sound in memory.
It worked~ Thanks for the very help, now I shall write this in my notes
 
Top