Slider and touch variable in a script : touch is sent before value?

I'm trying to use a slider to trigger MIDI note : the value of the slider controls velocity
touching the slider triggers the note

it seems to work with this script :

if (touch !== undefined) {
send('/note', 1, 62, touchvalue127)
}

...but something goes wrong :
When I touch the slider a note is triggered, but the velocity used is the previous position of the slider (before the slider was touched), not the current value of the slider. (yes my slider has "snap": "true" )

When I release the slider, it sends a note off message (velocity = 0) (= OK)

Looks like the script is triggered by the touch variable before the value is evaluated...
Is it the expected behavior ?
What would be the solution to make this work is the expected order ?

(this script cannot be triggered by a change in value, .. because value doesn"t necessary changes when user touches, releases and touches again the slider..)

( I have the same issue with a XY widget)

Mathieu

Yes, the touch information may be needed before any value change. The good news is that you can hack it your way:

if (touch !== undefined) {
  setTimeout(function(){
    send("/note", 1, 62, get("this"))
  })
}

When no delay is specified, setTimeout runs the provided function when current tasks are done.

wonderful :slight_smile:

and to make it generate a note-off on release, this seems to work :

if (touch !== undefined) {
  setTimeout(function(){
    send("/note", 1, 62, get("this")*touch)
  })
}

(otherwise a note-on is sent when touch == 1, and another one is sent when touch == 0 )

Thanks

Mathieu

Nice indeed !

Hello

I'm trying the method described above with a matrix of sliders.

I encounter 2 issues :

if I try to use the exact same code in matrix props,
get("this")
doesn't work. (the script is invalid, and all props return to default)
If instead I use :
"script": "if (touch !== undefined) { setTimeout(function(){send('/note', 1, #{$+0}, value * touch) })}"
I can send MIDI notes, but I get the issue reported in the first post : the velocity used is the previous position of the slider (before the slider was touched)

(please open attached session file)

help please :slight_smile:

matrix of sliders touch variable issue.json (3.6 KB)

It works if you use single quotes instead of double quotes (which conflict with the surrounding double quotes around your script)..

of course !
I never knew where and why I should use single or double quotes... now I start to understand :smiley:

Thanks for your help

all the best

Mathieu