[directmusic] Re: Fade out and stop

  • From: "Ciaran Walsh" <ciaran@xxxxxxxxxxxxxx>
  • To: <directmusic@xxxxxxxxxxxxx>
  • Date: Thu, 10 Oct 2002 12:54:19 +0100

I posted this back in June in response to another question on the subject -
the script example is roughly what you need I think and is pretty


For POW (PC and Xbox) I needed to use a lot of fades in and out for
conversations, cut
scenes etc, many of them events that were skippable. This meant I had the
problem of a fade in being triggered when a fade out was still playing,
causing an unpleasant stuttering as the volume controllers clashed. The
system I devised to solve this was very similar to what Bjorn has done in
his script but on a slightly larger scale as it's designed to accomodate
lots of different circumstances.

Basically I created what I call my FadeManager script which uses a set of
timer segments, variables and routine calls to keep track of the fades,
queue up any new fades triggered during existing fades and ensure there are
no clashes. Other than having a built in means of cutting off fades while
they're playing (any chance of this MS people?) it's the best way I could
think of. Here's a bit of sample script:

note: another script sets FType and calls FadeManager

Dim FType               ' Fade Type, in this case Fade In, Fade Out Short or 
Fade Out
Dim FT          ' true or false - whether a Fade Timer is currently playing
Dim FWaiting    ' true or false - whether there is a fade in the queue
Dim FadeVol             ' result of GetMasterVolume, used in FadeManager 
routine to
establish Fade state

sub StartFT
        if FType = 1 then
                SetMasterVolume 0, 1500
                FadeTimer2.play IsSecondary     ' segment with call to FTOff 
after 1500ms
                FT = 1                          ' a Fade Timer is playing
        elseif FType = 2 then
                SetMasterVolume -9600, 1500
                FadeTimer2.play IsSecondary
                FT = 1
        elseif FType = 3 then
                SetMasterVolume -9600, 3000     ' segment with call to FTOff 
after 3000ms
                FadeTimer1.play IsSecondary
                FT = 1
        end if
end sub

sub FTOff
        FT = 0                                  ' Fade Timer has finished
        if FWaiting = 1 then                    ' if there's something in the 
                StartFT                         ' start next Fade Timer
                FWaiting = 0
        end if
end sub

sub FadeManager
        FadeVol = GetMasterVolume               ' what is current Volume
        if FadeVol = 0 then
                if FType = 1 then
                        FT = 0                  ' if the last fade was a Fade 
In it's finished
                end if
        elseif FadeVol = -9600 then
                if FType > 1 then
                        FT = 0                  ' if the last fade was a Fade 
Out it's finished
                end if
        end if
        if FT = 1 then                          ' fade must be in progress
                FWaiting = 1                    ' queue the new fade
        elseif FT = 0 then                      ' fade must not be in progress
                StartFT                         ' start the new fade now
        end if
end sub

I've tried  very hard to break this and it always seems to work! Pretty good
example of the kind of control you can have with scripting I reckon....


-----Original Message-----
From: majordomo-owner@xxxxxxxxxxxxxx
[mailto:majordomo-owner@xxxxxxxxxxxxxx]On Behalf Of Bjorn Lynne
Sent: 29 June 2002 22:59
To: directmusic@xxxxxxxxxxxxxx
Subject: [directmusic] Script this scenario

Hi folks,

Okay I have this thing I want to do, and I can't get the scripting to do it.
But I've only recently got started with scripts, so please bear with me.
Here's what I want to do:

I have 2 different background tracks to play (there will be 10, but I've
only got 2 so far), they each have their own segments: "background1",
"background2", etc, and they loop. I'm playing them as primary segments,
adding other stuff using secondary segments.

I want the game programmer to be able to call a single routine in my script
that will:

1) Fade out the currently playing primary segment
2) Pick a new background track at random
3) Start playing the new background track

I don't want any crossfading. I simply want the currently playing primary
segment to fade out, then stop, then pick a new one at random, and start
playing it.

I thought this was going to be pretty simple, but I can't get my scripting
to work. I've managed to get the fade-out  and stop to work okay, by using a
dummy "FadeOutDelay" segment that plays nothing for a while, then triggers
my "StopAll" script, and then triggers my "PlayRandomBackgroundMusic"
script. The fadeout works, but it never starts playing a new one (at least
not so I can hear it). Here is my script so far; any input would be greatly

Dim MyRandomValue
Dim background1playing
Dim background2playing
Dim orientalplaying

Sub FadeOutAndStop
 SetMasterVolume -9600, 8000
 FadeOutDelay.play AtImmediate + IsSecondary
'(as I mentioned, the FadeOutDelay calls StopAll, then calls
End Sub

Sub PlayRandomBackgroundMusic

 SetMasterVolume 0
 MyRandomValue = Rand (2)

 If (MyRandomValue = 1) Then
  background1.play IsPrimary
  background1playing = 1

 ElseIf (MyRandomValue = 2) Then
  background2.play IsPrimary
  background2playing = 1
 End If
End Sub

Sub StopAll
 If (background1playing = 1) then
  background1.stop AtImmediate
  background1playing = 0
 End If

 If (background2playing = 1) then
  background2.stop AtImmediate
  background2playing = 0
 End If

End Sub

-----Original Message-----
From: directmusic-bounce@xxxxxxxxxxxxx
[mailto:directmusic-bounce@xxxxxxxxxxxxx]On Behalf Of Scott Selfon
Sent: 09 October 2002 00:40
To: directmusic@xxxxxxxxxxxxx
Subject: [directmusic] Re: Fade out and stop

Yup, DX Audio script routines are meant to be designed to quickly
execute - so putting in loops or sleep are not generally recommended
(and can in some instances cause the rest of the DirectMusic performance
thread to become unhappy). This causes something like fading out and
stopping music often a bit more tricky.=20

One solution is that at the same time that you fire off the fade out
(either as you did with SetMasterVolume - although this globally fades
out *everything*, not just the primary piece of piece - or using
audiopath.SetVolume), you fire off a secondary segment that is roughly
the same length as the fade out, or perhaps a little longer. That
secondary segment just has a script track, and has a single script event
towards the end that fires off your dmAllStop routine.

I ramble on and on about the topic in more detail in this white paper:


Hope this helps!

-----Original Message-----
From: Aaron R Leiby [mailto:aleiby@xxxxxxxxxxxxx]=20
Sent: Friday, October 04, 2002 9:36 PM
To: directmusic@xxxxxxxxxxxxx
Subject: [directmusic] Fade out and stop

Simple question:

How might I create a script method that fades the music out and then

Individually I can do:

sub dmLowVolume
    SetMasterVolume -1000, 3000
end sub

sub dmAllStop
    select case PlayFlag
    case 1
    end select
end sub

So that would lead me to believe I would simply need to do:

sub dmFadeout
    sleep 3000
end sub

Of course things never work as I expect.  :)

For future reference, where's the best place to look for what VB
supports and what it does not?



Other related posts: