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
// create event:
audio_play_sound(bgm_intro,0,false);
// step event:
if(!audio_is_playing(bgm_intro) && !audio_is_playing(bgm_loop)){
audio_play_sound(bgm_loop,0,true);
}
I don't want to seperate the audio into 2 files, it wastes disk spaceYou 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);
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.Code:// step event: if(!audio_is_playing(bgm_intro) && !audio_is_playing(bgm_loop)){ audio_play_sound(bgm_loop,0,true); }
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);
}
Are you sure that it actually works?This is a quick idea:
I didn't test it but It should work. If not, try to experiment with those functions.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); }
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;
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;
}
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:
The Key Press event: (The button is Z)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 song whom I want it to repeat at a certain point instead of the intro is the one with the "tides" soundidCode: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; }
// CREATE EVENT
current_bgm = -1;
// 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;
...
...
}
// 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);
}
}
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)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; ... ... }
Check the advanced audio functions: https://docs.yoyogames.com/source/dadiospice/002_reference/game assets/sounds/index.htmlCode:// 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); } }
Read my code above, I tried it and it won't work (I did actually try it with seperate files)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.
Assuming you have two files, intro_theme and full_loop.Read my code above, I tried it and it won't work (I did actually try it with seperate files)
audio_play_sound(intro_theme, 0, false);
if(!audio_is_playing(intro_theme) && !audio_is_playing(full_loop)){
audio_play_sound(full_loop, 0, true);
}
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);
}
}
This is exactly the one I have tried -.-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:
In a step event that is running while that is playing:Code:audio_play_sound(intro_theme, 0, false);
Code:if(!audio_is_playing(intro_theme) && !audio_is_playing(full_loop)){ audio_play_sound(full_loop, 0, true); }
It worked~ Thanks for the very help, now I shall write this in my notesPlease keep AlexDerFerris Code. But try this in your step event instead:
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.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); } }
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.