Automatic Fade Out

I needed an automatic fade out in the song “Shine on you crazy diamond” when the organ plays a chord.
This chord should be faded out automatically, because I did not want to deal with a knob on my controller.
So I created this script, by pressing a specific key on my keyboard the sound volume fades out.

The trick is to press C1 and the fade out is triggered.
At the end of the fade out, the C1 can be triggered again and the volume is set to the original value.

@keyman, thx for cleaning up the script!

Here it is:

   MASTER : Widget // A knob widget used to control the volume

   myRamp  : Ramp // A generator that moves smoothly from 0.0 to 1.0 over some specified time
   MTW     :  MidiInBlock // The MidiIn Block from where the trigger note will come
   StartValue : double  // This remembers the initial value of the knob so that when we start again
                            // the knob will be reset to this value
   Running   : Boolean // Keep track of whether we are currently running the ramp

   SetGeneratorOneShot(myRamp, true) // Generator will only run once when triggered         
   myRamp.SetGeneratorLength(2000); // Two seconds

   Running = false // When we start up, the ramp is not running

// This callback will be triggered only when the note C1 is received
On NoteOnEvent(m : NoteMessage) matching C1 from MTW
   Print("Run state: " + Running) // Print true or false each time we're called depending
                                    // on whether we are currently responding to the ramp
   If not Running
         StartValue := GetWidgetValue(MASTER) // Get the initial level of the knob (NB make sure it's not already 0
                                                // otherwise nothing will happen
         Running = true                       // Remember that we are now running the ramp
         myRamp.EnableGenerator(True)         // Arm the ramp function generator
         SetTimersRunning(true)               // The ramp function generator will now start producing values
        SetWidgetValue(MASTER, StartValue)   // If we were running then reset the knob to the initial value again
        Running = false                      // and remember that we are now NOT running

    // This gets called by the ramp generator as time passes
On TimePassing(timeX : integer, amplitudeY : double) from myRamp
   Print(amplitudeY) // Just lets you see the value of the ramp as time passes
       if Running 

             // The amplitude of the ramp goes from 0.0 to 1.0 so if we multiply that amplitude
             // by the INITIAL start value of the knob, we generate a value that gets closer to the
             // actual StartValue over time
             // Therefore, subtracting that from the initial StartValue causes the
             // the actual value to reduce down to zero
             // Hence the knob slowly reduces to 0

             SetWidgetValue(MASTER, StartValue - StartValue * amplitudeY)   

Correction, as it was @dhj who “cleaned” and added comments to your script :wink:

I do by now have a better understanding of GP scripting. Thanks to you! and those you post some “magic” in the form of a script