-
-
Save drakeirving/d7ce1bf229395509025c to your computer and use it in GitHub Desktop.
PlaySyncBGM
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*---------------- | |
Plays music that skips based on lag in order to synchronize with gameplay. | |
Allows `skip_param` desynced frames to build up before freezing audio for `skip_param` frames. | |
Setting `skip_param` to low numbers (< 10, depends on framerate stability) can cause audio to fail to restart properly. | |
Slightly inaccurate due to the millisecond resolution of GetStageTime and possible stop/start overhead. | |
----------------*/ | |
function PlaySyncBGM(path, loop_start, loop_end, skip_param){ | |
let bgm = ObjSound_Create(); | |
ObjSound_Load(bgm, path); | |
ObjSound_SetRestartEnable(bgm, true); | |
ObjSound_SetSoundDivision(bgm, SOUND_BGM); | |
if(loop_start >= 0 && loop_end > loop_start){ | |
ObjSound_SetLoopEnable(bgm, true); | |
ObjSound_SetLoopTime(bgm, loop_start, loop_end); | |
} | |
ObjSound_Play(bgm); | |
task run{ | |
let t1 = GetStageTime(); | |
let t2; | |
let dt; | |
let sdt = 0; | |
let c = -1; | |
while(!Obj_IsDeleted(bgm)){ | |
yield; | |
if(!ObjSound_IsPlaying(bgm)){ | |
if(c >= 0){ c++; } | |
if(c >= skip_param){ | |
c = -1; | |
ObjSound_Play(bgm); | |
} | |
} | |
t2 = GetStageTime(); | |
dt = (t2-t1)/1000; | |
sdt += dt - 1/60; | |
t1 = t2; | |
if(sdt*60 > skip_param/2){ | |
sdt -= skip_param/60; | |
ObjSound_Stop(bgm); | |
c = 0; | |
} | |
} | |
} | |
run(); | |
return bgm; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment