Adventure Game Studio

AGS Support => Advanced Technical Forum => Topic started by: Intangible on Thu 30/06/2011 13:12:02

Title: Music nuances - don't start song over if new room uses the same song
Post by: Intangible on Thu 30/06/2011 13:12:02
I'm finding it a little difficult to get examples of audio use (most of them use the older approach that was phased out in the latest AGS release), so I thought I'd ask this directly.

I have three rooms, two of which use the same background music. Thus, I setup each room to play its background music when it loads:

// rooom_Load() for rooms 1 and 3
aRuinsExterior.Play(eAudioPriorityHigh, eRepeat);

// rooom_Load() for room2
aRuinsInterior.Play(eAudioPriorityHigh, eRepeat);

This seems to work well enough when I'm switching between room 2 and room 1 (or 3), since AGS automatically stops the current music before starting the new one. When I'm switching between room 1 and 3, however, the music is still stopped and restarted, even though both rooms have the same song. I'm sure that I'm just not using the new audio features properly, so I had a few questions:

1) (Most importantly), how should I use music in AGS so that it only restarts if it's a different song in the new room? Should I do a check to see which room I came from and act accordingly, or is there a more automatic way of handling this?
2) How does AGS know to stop the current music when I start playing a different song in the first place, without an explicit call to stop the other music? Does AGS only allow one repeating sound to play at a time, or something?
3) Do we have any options for making the music stop a little less abrupt, like a fade or something? I've seen scattered references to cross-fading tracks in the built-in help, but I can't seem to find an option for it in General Settings. I've seen forum examples of using "while" loops to fade music, although I was hoping there was a more "native" way of doing this.
Title: Re: Music nuances - don't start song over if new room uses the same song
Post by: Khris on Thu 30/06/2011 13:35:41
Cross fading:
  SetGameOption(OPT_CROSSFADEMUSIC, 2);   // 0 = no, 4 = fast

I believe rooms used to have a music setting as in you'd enter the number of the track. Thus AGS wouldn't restart the music if it was the same in the new room.

I didn't find a music setting but it's pretty easy to code that:

AudioChannel*bgm_channel;

void SetBGM(AudioClip*bgm) {

  if (bgm_channel != null && bgm_channel.PlayingClip == bgm) return;

  bgm_channel = bgm.Play(eAudioPriorityHigh, eRepeat);
}


Now use
  SetBGM(aRuinsExterior);
Title: Re: Music nuances - don't start song over if new room uses the same song
Post by: Intangible on Fri 01/07/2011 17:37:52
That did the trick. Thanks, Khris!