Adventure Game Studio

AGS Support => Advanced Technical Forum => Topic started by: eri0o on Tue 19/12/2017 23:15:23

Title: Getting true music position in ms and questions
Post by: eri0o on Tue 19/12/2017 23:15:23
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 (ags) Select

AudioChannel *acMusic;

//fix to report correct music position, game in 60fps
int frame;
int acMusPreviousMs;
bool startedRepeat;
int frameWhenRepeatingStarted;
int actualACMusMs;

void playMusic(AudioClip * musicClip){
  acMusic = musicClip.Play(eAudioPriorityHigh, eRepeat);
}


void repeatedly_execute_always(){
  if(IsGamePaused()==1 || acMusic==null){
    return; 
  }

  frame++;
 
  if(acMusic.PositionMs==acMusPreviousMs){
    if(!startedRepeat){
      frameWhenRepeatingStarted=frame-1;
    }
    startedRepeat=true;
    actualACMusMs = acMusPreviousMs+(frame-frameWhenRepeatingStarted)*16;
  } else {
    startedRepeat=false;
    actualACMusMs= acMusic.PositionMs;
  }
 
  acMusPreviousMs = acMusic.PositionMs;
}


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?
Title: Re: Getting true music position in ms and questions
Post by: eri0o on Wed 20/12/2017 01:18:25
Whoa! PlayQueued is a nice command! Thanks! :D
Title: Re: Getting true music position in ms and questions
Post by: Crimson Wizard on Wed 20/12/2017 01:30:53
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.
Title: Re: Getting true music position in ms and questions
Post by: Snarky on Wed 20/12/2017 01:35:24
Quote from: eri0o on Tue 19/12/2017 23:15:23
if I have crossfade activated, how should I force crossfade to not happen then?

Code (ags) Select
SetGameOption(OPT_CROSSFADEMUSIC, 0);
Title: Re: Getting true music position in ms and questions
Post by: eri0o on Wed 20/12/2017 02:14:04
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... :/