Generator script on NoteOnEvent

I’ve shamelessly stolen the below script from @pianopaul to sweep a highpass filter from when I hit note D3. My only issue with this is that I have to hit D3 twice - the filter jumps to the frequency starting point on the first hit and starts sweeping on the second. Can I (read: “you” :smiley:) ) fix this somehow so that it starts to sweep immediately?
Still a noob with scripting, sorry if this is “too” simple…

`Var

FILTER : Widget // A knob triggered by the ramp

var
myRamp : Ramp // A generator that moves smoothly from 0.0 to 1.0 over some specified time
MTW : MidiInBlock
v_value : double
v_run : integer

initialization
SetGeneratorOneShot(myRamp, true) // Generator will only run once when triggered
myRamp.SetGeneratorLength(17000); // 17 seconds

v_run = 0
// Also, check the SetGeneratorCoarseness function
// which can be used to control how often we get called back
// Choose values wisely! You’re trading accuracy against CPU cycles

end

// Use this to trigger the ramp
On NoteOnEvent(m : NoteMessage) matching D3 from MTW
If v_run == 0 then
v_value := GetWidgetValue(FILTER)
v_run = 1
myRamp.EnableGenerator(True)
SetTimersRunning(true)
else
SetWidgetValue(FILTER, v_value)
v_run = 0
end
End

// This gets called by the ramp generator as time passes
on TimePassing(timeX : integer, amplitudeY : double) from myRamp
if v_run == 1 then
SetWidgetValue(FILTER, v_value - v_value*amplitudeY)
end
end`

Please add this:

on Activate
v_run = 0
v_value = GetWidgetValue(FILTER)
end

I just tested and it was working as it should.
The 1st press starts and the 2nd press jumps back to the start value.

I tested by pressing D3 in the Midi In plugin

I put “On Activate”… before this line:
On NoteOnEvent(m : NoteMessage) matching D3 from MTW
but I don’t notice any difference. Should it not go there? What does it do…?

What if I want to set the start value by D3 and begin sweeping by A3? Could you help me? I tried something I thought could work, and it compiled, but it also crashed GP…

Thanks for taking the time!

Uhm, could you please tell us exactly what you did that crashed GP? Can you reproduce that crash@? What version of GP are you using?

I’m on latest version 3.1.7. I think I wrote as below.

On NoteOnEvent(m : NoteMessage) matching D3 from MTW
SetWidgetValue(FILTER, v_value)
v_run = 0
end

On NoteOnEvent(m : NoteMessage) matching A3 from MTW
If v_run == 0 then
v_value := GetWidgetValue(FILTER)
v_run = 1
myRamp.EnableGenerator(True)
SetTimersRunning(true)
else
SetWidgetValue(FILTER, v_value)
v_run = 0
end

I guess the missing “else” bit in the first part made it crash?

Now I put
On NoteOnEvent(m : NoteMessage) matching D3, A3 from MTW
and that solves everything for me!

Sorry for taking up your time! I’ll try to refrain from posting questions before I’ve tried things more thoroughly.

No worries. We do care about crashes so appreciate the information. Can you be more specific about the crash? What exactly did you change?

This is what I put in into the original script, then hit Compile. I think I created an endless loop and it probably hung already when I hit Compile.

Did it crash or did it hang? Those are very different.

It hung. Had to kill it by Ctrl+Alt+Del.

Ok that makes more sense. It is absolutely possible to hang GP with a bad script. Much harder to crash it, that’s why I was concerned