Help with autosustainer

I’ve been using @LeeHarvey’s auto sustainer
I modified it to work with a keypress for on and keypress for off, this works fine.
I then thought it would be good to be able to define the two keys as parameters and am struggling, can any of you help or suggest a better way to do this, I thought below would work but its unhappy with the comparison
Ideally It would be good to have a widget set the key value linked to the parameter so it can be saved in songs or song parts

var

notes, nt2 : NoteTracker
hold : boolean = true
Kenable : MidiMessage

Enable("Enable") : parameter "off", "on" = "off"
stop("stop") : parameter "off", "on" = "off"
KeyEnable("KeyEnable") : parameter = 1.0

Initialization
   SetInfoMessage("AutoSustainer")
   SetDisplayMessage("V1.00 @LeeHarvey")
Kenable = MakeNoteMessage(Floor(KeyEnable),127)
   End

On NoteEvent(m : NoteMessage)
Kenable = MakeNoteMessage(Floor(KeyEnable),127)
Select
   (GetNoteNumber (m) == GetNoteNumber(Kenable) ) do Enable = "on" // C2
   (GetNoteNumber (m) == 38) do stop = "on";Enable="off"   // D2
   //Optionally include this for when none of the above matched

You’ll want to test for the state of that On/Off parameter using if/then statements.

There is also this modified script from @dhj. I haven’t looked to see how different it is from the one you’re using.

Initialization
   SetInfoMessage(<<<This scriptlet allows you to sustain (latch) notes automatically.
   
When you release all notes and start pressing new notes, the previously held
notes will be automatically stopped. You can explicitly stop all sustained 
notes from a widget by attaching it to the StopSustain parameter.

This version also provides a mechanism to have the notes stop automatically 
after some defined time delay specified in tenths of a second.
   >>>)
   
   SetDisplayMessage("Copyright (c) 2021 Deskew Technologies, LLC")
End


var
   StopSustain : Parameter = 0.0 // This will be a command parameter

   AutoStop : Discrete Parameter "Off", "On" = "Off"
   TimeDelay : Subrange Parameter 1 .. 200 = 10 // Time in units of tenths of a second

var
   Sustainer: AutoSustainer
 
   r : Ramp

on NoteEvent(m : NoteMessage)  
   AutoSustainer_Play(Sustainer, m)   
   if AutoStop == "On"
      then 
        // Stop the old ramp and restart it every time you play a note
        StopOneShotRamp(r)
        TriggerOneShotRamp(r, TimeDelay * 100, 20)
   End     
end


// Called when a parameter value has changed
On ParameterValueChanged matching StopSustain
    if StopSustain > 0.5
       then 
          AutoSustainer_Stop(Sustainer)
          StopSustain  = 0.0 // Reset for next time
    end      
End



On GeneratorEndCycle(time : integer) from r
          AutoSustainer_Stop(Sustainer)
          StopSustain  = 0.0 // Reset for next time
End

I’ve managed to get the parameter 0.0 to 1.0 to convert to an int using
ParamToMidi which gets the note number
Now I just need to find out how to display The rotary Knob in notes rather than float, is this possible ?

var

notes, nt2 : NoteTracker
hold : boolean = true
Kenable : integer
Kstop : integer

Enable("Enable") : parameter "off", "on" = "off"
stop("stop") : parameter "off", "on" = "off"
KeyEnable("KeyEnable") : parameter = 1.0
KeyStop("KeyStop") : parameter = 1.0
Initialization
   SetInfoMessage("AutoSustainer")
   SetDisplayMessage("V1.00 @LeeHarvey")
Kenable = ParamToMidi(KeyEnable)
Kstop = ParamToMidi(KeyStop)

Maybe using a string array for note names?
Just a thought.

1 Like

Converts values between 0.0 and 1.0 to note name between 0 and 127

Documented in the manual

T

2 Likes

I did try that but could not get it to work, not sure what I was doing wrong
the parameter changed from 0.0 to 1.0 but the note only moved from c-2 to c#-2
it converted the value to the correct note number in the script


I don’t know what “KeyEnable” does

The [notename] (under the covers) takes a value between 0.0 and 1.0, scales that number to an integer between 0 and 127 and then converts that number to a name

if you watch the video it shows the scriptlet KeyEnable climbing from 0.0 to 1.0 and the script is logging to the log windows the note number, so I was expecting the Widget to show the notes doing the converted name, the KeyEnable is simply the note that will enable the auto sustain when pressed.
Am I doing something wrong?

You are writing
Kenable = MakeNoteMessage(Floor(KeyEnable),127)
The Floor function is defined (see the system function list) as
Rounds x downward returning the largest integral value that is not greater than x

KeyEnable is defined as a floating point number between 0.0 and 1.0. Therefore the floor of every value below 1.0 is going to be 0. Hence there are only two values, 0 and 1

Also, I’m not quite sure why you are making the KeyEnable parameter into a MIDI message

worked perfect thanks