How to fade out MIDI background music when entering a new room [SOLVED]

Started by rozojc, Thu 14/07/2005 00:13:46

Previous topic - Next topic

rozojc

--------------------------------
Edit:
Look at my last posting to see how it's done
--------------------------------
Here I am, third question today (sorry bout that)
Well, I did my homeowrk and search on the forum first, but I did not really find something easy to do what I'm trying to do....
Suppose I make Music 0 play when you enter the first room (which is really a splash screen), right? Once I go into the second room, the music continues to play, and obviously if I just use StopMusic() it stops abruptely. What I'd like it is to slowly fade out completely (as my second room does not have background music).
I tried with a script I saw when I searched the forum, but it used the Wait() command, thus halting the whole game while the music faded out, which is definitely not what I'd like. Then, I made the following function so that it would fade out in 5 seconds and then stop it:

function FadeMusicOut()
{
   int ini,dif;
   ini=GetRawTime();
   while(ini>=(GetRawTime()-5))
   {
      dif=GetRawTime()-ini;
      SetMusicVolume(3-dif);
   }
   StopMusic();
}
However, it seems that the while() loop also halts gameplay completely. Is there any way to fade out the music without having to halt the game?

A suspense adventure game where you play an anti-hero, check it out!

strazer

Quoteand obviously if I just use StopMusic() it stops abruptely.

To my knowledge, if you have "Crossfade music tracks" enabled in the General settings, the current music track fades out when using StopMusic().

rozojc

#2
Quote
To my knowledge, if you have "Crossfade music tracks" enabled in the General settings, the current music track fades out when using StopMusic().
Yep, but  Crossfade music tracks only works if you actually have a track in the next room (or at least that's what it seems. I don't have another track in the second room and the music does not fade)

Modification: maybe it's not because I don't have another background music in the second room, but I just read that it does not work if the music is in MIDI format (which it is)...

Any ideas?

A suspense adventure game where you play an anti-hero, check it out!

strazer

Quote from: rozojc on Thu 14/07/2005 01:08:58
Modification: maybe it's not because I don't have another background music in the second room, but I just read that it does not work if the music is in MIDI format (which it is)...

Yes, I had tested it with an mp3 file and it worked fine. Haven't worked with midi myself.

rozojc

Quote from: strazer on Thu 14/07/2005 01:15:18
Quote from: rozojc on Thu 14/07/2005 01:08:58
Modification: maybe it's not because I don't have another background music in the second room, but I just read that it does not work if the music is in MIDI format (which it is)...

Yes, I had tested it with an mp3 file and it worked fine. Haven't worked with midi myself.
Any ideas on how to do a workaround for MIDI background music?

A suspense adventure game where you play an anti-hero, check it out!

strazer

Your idea was good, but to make it non-blocking, you have to decrease the volume from inside the repeatedly_execute function instead of a loop. Too lazy to code right now.

rozojc

Quote from: strazer on Thu 14/07/2005 02:16:05
Your idea was good, but to make it non-blocking, you have to decrease the volume from inside the repeatedly_execute function instead of a loop. Too lazy to code right now.
Ok, I think I have an idea:
I'll try making a global variable that is actually going to be a flag and it's going to have 0 as a value most of the times. In the rooms where I want the fadeout, I'll make an interaction, that as soon as the player walks into the room, that variable changes its value to '1'. In the repeatedly_execute, I'll have an IF statement, cheking if the value is 1; when it is 1, it will decrease the volume step by step. Once the volume reaches the minimun (-3), it will stop the music, and change the variable back to '0' again... I'll post as soon as I try it, maybe it can be useful if anyone else wants to have a fadeout with MIDI music...

A suspense adventure game where you play an anti-hero, check it out!

rozojc

#7
Ok, I was able to do it, so, in case anyone is interested, here's a workaround so you can make MIDI background music FadeOut when entering a new room (which is not possible with the CrossFading option since it only works with mp3, ogg, etc).

Here's what I did:

In the global script (before anything) I added the following:

int FadeOutFlag=0,ActualTime,EnterRoomTime;

Inside the header script I add:

import int FadeOutFlag;
import int EnterRoomTime;
import int ActualTime;

Inside any room in which I want the FadeOut to happen I add an Interaction "When player enters screen" that runs the script:

FadeOutFlag=1;

And I also have to add an interaction "When player leaves room" with
//just in case player leaves room before fadeout
FadeOutFlag=0;
SetMusicMasterVolume(80);


Finally, inside the repeatedly_execute() I added:

     int TimeDif; //Difference in seconds between
                          // the actual time and the time when
                          //the player entered the room
     ActualTime=GetRawTime();//gets actual time

   if(FadeOutFlag==1) //AKA if the player entered the room
   {
      TimeDif=ActualTime-EnterRoomTime;//how many seconds
                                                               //has the player been in the room
      if(TimeDif>=11)//if player has been 11 seconds inside room
      {
         StopMusic(); //stop music, duh
         FadeOutFlag=0; //flag goes back to normal
         SetMusicMasterVolume(80);  //music volume back to normal
      }
      else
         SetMusicMasterVolume(80-(TimeDif*8)); //lower music volume
   }



A suspense adventure game where you play an anti-hero, check it out!

TerranRich

Status: Trying to come up with some ideas...

monkey0506

Just a thought, but you may want to change FadeOutFlag to a bool (AGS 2.7 and up), seeing as it should only hold the values of 0 or 1.  It makes it more idiot proof :D.  Other than that...nice code!

rozojc

Quote from: monkey_05_06 on Fri 29/07/2005 03:23:17
Just a thought, but you may want to change FadeOutFlag to a bool (AGS 2.7 and up), seeing as it should only hold the values of 0 or 1.  It makes it more idiot proof :D.  Other than that...nice code!
Yeah, it would work better (as in more "elegant") with a bool, it's just that this is actually the first time I script with AGS so I wan't quite sure on how to do it. In other words, I had just learned how ints worked and was too lazy to check out how booleans worked on AGS  ::)

Thanx for the "nice code" compliment (really) ;D

Oh, and to TerranRich: thank YOU, I do feel "satisfied" that my first bit of coding in AGS ends up in your FAQ :)

A suspense adventure game where you play an anti-hero, check it out!

monkey0506

Well I believe that bool is actually a hard coded enumeration like:

enum bool {
  false = 0,
  true = 1
  };

Of course I have never seen the source so I wouldn't know, but with boolean values you can use 0, 1, false, and true (false corresponding to 0, and true corresponding with 1).  That would prevent users accidentally typing "FadeOutFlag = 2" which not work right because the check reads "if (FadeOutFlag == 1)".  And you're welcome on the compliment.  But I'm not too liberal with them, so enjoy it while you can! :D

SMF spam blocked by CleanTalk