AGS Awards nominations close at 13:59 GMT on Saturday 10 February 2018. You haven't yet nominated, so you've got 17 days and 4 hours left to play the games and decide which to nominate!

Author Topic: Getting true music position in ms and questions  (Read 199 times)

I noticed the music position in ms is not updated every frame. Is this intentional?

I came up with the following code to fix this, where actualACMusMs has the true music position:

Code: Adventure Game Studio
  1. AudioChannel *acMusic;
  2.  
  3. //fix to report correct music position, game in 60fps
  4. int frame;
  5. int acMusPreviousMs;
  6. bool startedRepeat;
  7. int frameWhenRepeatingStarted;
  8. int actualACMusMs;
  9.  
  10. void playMusic(AudioClip * musicClip){
  11.   acMusic = musicClip.Play(eAudioPriorityHigh, eRepeat);
  12. }
  13.  
  14.  
  15. void repeatedly_execute_always(){
  16.   if(IsGamePaused()==1 || acMusic==null){
  17.     return;  
  18.   }
  19.  
  20.   frame++;
  21.  
  22.   if(acMusic.PositionMs==acMusPreviousMs){
  23.     if(!startedRepeat){
  24.       frameWhenRepeatingStarted=frame-1;
  25.     }
  26.     startedRepeat=true;
  27.     actualACMusMs = acMusPreviousMs+(frame-frameWhenRepeatingStarted)*16;
  28.   } else {
  29.     startedRepeat=false;
  30.     actualACMusMs= acMusic.PositionMs;
  31.   }
  32.  
  33.   acMusPreviousMs = acMusic.PositionMs;
  34. }
  35.  

I did this because I am experimenting with some music in my game that is active, like, I switch music depending on changes on the environment, and needed to correctly position the music when switching.

Also, imagine a graph:

(music 1)--->>(music 2)<< loop music 2 forever

How would you detect music 1 has ended and you need to proceed to music 2? Also if I have crossfade activated, how should I force crossfade to not happen then?

Re: Getting true music position in ms and questions
« Reply #1 on: 20 Dec 2017, 01:18 »
Whoa! PlayQueued is a nice command! Thanks! :D

Crimson Wizard

  • AGS Project Tracker Admins
    • Best Innovation Award Winner 2013, for spearheading the AGS 3.3.0 project
    •  
    • Lifetime Achievement Award Winner
    •  
Re: Getting true music position in ms and questions
« Reply #2 on: 20 Dec 2017, 01:30 »
To be fair, PlayQueued queue sound only if there is 1 channel reserved for that audio type, otherwise it will start immediately. IDK what's your current setup is, depending on it this function may be useful or not.

I've got an impression that it was not intentionally designed for the described scenario, but rather for scenario where sound must play either immediately or after previous (presumably short) sound has finished.

Snarky

  • Global Moderator
  • Mittens Earl
  • Private Insultant
    • I can help with proof reading
    •  
    • I can help with translating
    •  
Re: Getting true music position in ms and questions
« Reply #3 on: 20 Dec 2017, 01:35 »
if I have crossfade activated, how should I force crossfade to not happen then?

Code: Adventure Game Studio
  1. SetGameOption(OPT_CROSSFADEMUSIC, 0);

Re: Getting true music position in ms and questions
« Reply #4 on: 20 Dec 2017, 02:14 »
Nice one! Thanks Snarky!

Hey Crimson Wizard! Yeah, I am constantly going back and forward with using two channels and custom crossfade and a single channel with in engine crossfade. Also I think I may have a small hitch in my loop, I am not sure if it's from the music file or the engine yet... :/