Hi scripters! I just got going with scripting and I’m loving it, despite my very old, out of date, and never-very-advanced coding skills. Here’s my issue: In trying to free up some screen real estate on my iPad (using it as a control surface to control GP via TouchOSC), I decided to get rid of all my ‘return from fx’ faders, since I really only need 2 settings, full on, or off, but I need the ‘off’ to happen as a long slow fade out. Hence, I borrowed from @pianopaul and @tonycore’s lovely existing fade-out scripts that they shared here, and slightly modified it for my own needs. So far, so good, it works exactly as intended! (Thanks for sharing those scripts, guys!). The only problem is, I have 16 instances of this that I need, and I’ve been struggling to figure out a way to make the code elegant and compact so that I can avoid simply copying and pasting 16 times (with appropriate variables) to get it to work across all 16 channels. I tried my best to apply arrays and then index into them using for loops, but I just can’t get it to work. I’m a terrible programmer, but I think I know enough to know that there has to be a way to do this - maybe using a custom function??? I’ll post here what I have so far, which is working fine, which is the first 3 iterations (using the old copy-paste method). I was at least able to save a little space by using arrays and indexing in the ‘initialization’ section. But I can’t figure out how to do something similar in the callback and time-passing sections. Any help or suggestion would be greatly appreciated! TIA.
Here’s the code, sorry it’s long but that’s what I’m trying to solve. This code compiles and runs perfectly, and as I said I could keep copy-pasting out to 16, but if there’s a cleaner, more compact way, I’d prefer it (and I’ll learn something for future scripts, hopefully this helps others learn as well):
Var
ReturnFaderFX1, ReturnFaderFX2, ReturnFaderFX3 : Widget // FX return fader. which will be moved by ramp function
ReturnFaderFX : Widget Array = [ReturnFaderFX1, ReturnFaderFX2, ReturnFaderFX3]
FadeOutButtonFX1, FadeOutButtonFX2, FadeOutButtonFX3 : Widget // FX return fader fade out button, to be used to trigger fade out slowly (on), and reset to full volume (off)
FadeOutButtonFX : Widget Array = [FadeOutButtonFX1, FadeOutButtonFX2, FadeOutButtonFX3]
BypassButtonFX1, BypassButtonFX2, BypassButtonFX3 : Widget // FX Bypass Button
BypassButtonFX : Widget Array = [BypassButtonFX1, BypassButtonFX2, BypassButtonFX3]
myRamp1, myRamp2, myRamp3 : Ramp // A generator that moves smoothly from 0.0 to 1.0 over some specified time
Ramps : Ramp Array = [myRamp1, myRamp2, myRamp3]
StartValue1, StartValue2, StartValue3 : double // This remembers the initial value of the fader so that when we start again
// the fader will be reset to this value
StartValue : double Array = [StartValue1, StartValue2, StartValue3]
Running1, Running2, Running3 : Boolean // Keep track of whether we are currently running the ramp
Running : Boolean Array = [Running1, Running2, Running3]
i : integer
initialization
For i = 0; i < 3; i = i + 1 Do
SetGeneratorOneShot(Ramps[i], true) // Generator will only run once when triggered
SetGeneratorLength(Ramps[i], 10000); // 10 seconds
Running[i] = false // When we start up, the ramp is not running
End
end
// 1) This callback will be triggered only when the FadeOutButtonFX1 is engaged, 'on' will fade out, 'off' will return to preset default level
On WidgetValueChanged (newValue : Double) from FadeOutButtonFX1
If not Running1
then
StartValue1 := GetWidgetValue(ReturnFaderFX1) // Get the initial level of the fader (NB make sure it's not already 0)
// otherwise nothing will happen
Running1 = true // Remember that we are now running the ramp
EnableGenerator(myRamp1,true) // Arm the ramp function generator
SetTimersRunning(true) // The ramp function generator will now start producing values
else
SetWidgetValue(ReturnFaderFX1, StartValue1) // If we were running then reset the fader to the default level again
Running1 = false // and remember that we are now NOT running
SetWidgetValue(BypassButtonFX1, 0) // Once we reset the fader to default level, we 'unbypass' the plugin, ready to start fresh
End
End
// 1) This gets called by the ramp generator as time passes
On TimePassing(timeX : integer, amplitudeY : double) from myRamp1
if Running1
then
// 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(ReturnFaderFX1, StartValue1 - StartValue1 * amplitudeY)
if GetWidgetValue(ReturnFaderFX1) < 0.01 then
SetWidgetValue(BypassButtonFX1, 100) //Once fader goes to zero, we bypass the plugin to 'clear' out any lingering sound before resetting to default level
end
end
End
// 2) This callback will be triggered only when the FadeOutButtonFX2 is engaged, 'on' will fade out, 'off' will return to preset default level
On WidgetValueChanged (newValue : Double) from FadeOutButtonFX2
If not Running2
then
StartValue2 := GetWidgetValue(ReturnFaderFX2) // Get the initial level of the fader (NB make sure it's not already 0)
// otherwise nothing will happen
Running2 = true // Remember that we are now running the ramp
EnableGenerator(myRamp2,true) // Arm the ramp function generator
SetTimersRunning(true) // The ramp function generator will now start producing values
else
SetWidgetValue(ReturnFaderFX2, StartValue2) // If we were running then reset the fader to the default level again
Running2 = false // and remember that we are now NOT running
SetWidgetValue(BypassButtonFX2, 0) // Once we reset the fader to default level, we 'unbypass' the plugin, ready to start fresh
End
End
// 2) This gets called by the ramp generator as time passes
On TimePassing(timeX : integer, amplitudeY : double) from myRamp2
if Running2
then
// 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(ReturnFaderFX2, StartValue2 - StartValue2 * amplitudeY)
if GetWidgetValue(ReturnFaderFX2) < 0.01 then
SetWidgetValue(BypassButtonFX2, 100) //Once fader goes to zero, we bypass the plugin to 'clear' out any lingering sound before resetting to default level
end
end
End
// 3) This callback will be triggered only when the FadeOutButtonFX3 is engaged, 'on' will fade out, 'off' will return to preset default level
On WidgetValueChanged (newValue : Double) from FadeOutButtonFX3
If not Running3
then
StartValue3 := GetWidgetValue(ReturnFaderFX3) // Get the initial level of the fader (NB make sure it's not already 0)
// otherwise nothing will happen
Running3 = true // Remember that we are now running the ramp
EnableGenerator(myRamp3,true) // Arm the ramp function generator
SetTimersRunning(true) // The ramp function generator will now start producing values
else
SetWidgetValue(ReturnFaderFX3, StartValue3) // If we were running then reset the fader to the default level again
Running3 = false // and remember that we are now NOT running
SetWidgetValue(BypassButtonFX3, 0) // Once we reset the fader to default level, we 'unbypass' the plugin, ready to start fresh
End
End
// 3) This gets called by the ramp generator as time passes
On TimePassing(timeX : integer, amplitudeY : double) from myRamp3
if Running3
then
// 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(ReturnFaderFX3, StartValue3 - StartValue3 * amplitudeY)
if GetWidgetValue(ReturnFaderFX3) < 0.01 then
SetWidgetValue(BypassButtonFX3, 100) //Once fader goes to zero, we bypass the plugin to 'clear' out any lingering sound before resetting to default level
end
end
End