Delay the second send function

I can't figure out how to use the setTimeout() function and my brain is about to explode. So, I give up.
Can you please describe what you mean exactly by the word callback? In documentation you say that it's a function to be executed. What function? The one that needs to be delayed? (ie. the send() function?)

} if (value === 1 && option_1 === 127) {
send('/note', 6, myString1, 127)
send('/note', 2, 85, 127) // this needs to be delayed
}
(in context)
var num = getProp('this', 'id').replace(/[^0-9]/g,'');
var option_1 = get("option_1")
var option_2 = get("option_2")
let myString1 = parseFloat(num)-1;

// (when the push button is 1)
if (value === 1 && option_1 === 1) {
send('/note', 6, myString1, 127)

} else if (value === 1 && option_1 === 127) {
send('/note', 6, myString1, 127)
send('/note', 2, 85, 127) // this needs to be delayed


// (when the push button is 0)
} else if (value === 0 && option_2 === 1) {
send('/note', 2, 86, 127)

} else if (value === 0 && option_2 === 2) {
send('/note', 2, 87, 127)

} else if (value === 0 && option_2 === 3) {
send('/note', 2, 88, 127)
}

Here's what I've tried:

(copiable code)
var num = getProp('this', 'id').replace(/[^0-9]/g,'');
var option_1 = get("option_1")
var option_2 = get("option_2")
let myString1 = parseFloat(num)-1;

// I've created a function
function actNP() {
send('/note', 2, 85, 127)
}

if (value === 1 && option_1 === 1) {
send('/note', 6, myString1, 127)

// And I've referenced it here:
} else if (value === 1 && option_1 === 127) {
send('/note', 6, myString1, 127)
setTimeout(1, actNP(), 50)

} else if (value === 0 && option_2 === 1) {
send('/note', 2, 86, 127)

} else if (value === 0 && option_2 === 2) {
send('/note', 2, 87, 127)

} else if (value === 0 && option_2 === 3) {
send('/note', 2, 88, 127)
}

Haha this is timeout saturday :grin:

Try this:

setTimeout(actNP, 50)
1 Like

Tried it. No luck. I think I need to use the custom module for this...

This works:

setTimeout(function(){ 
console.log("Hello!")
}, 1000);

This doesn't:

setTimeout(function(){ 
send('/note', 2, 85, 127)
}, 1000);

EDIT:
It seems that MIDI doesn't work with the setTimeout() function...

This doesn't

It does work as long as a the widget's target is defined.

Then I must be doing something wrong...

var num = getProp('this', 'id').replace(/[^0-9]/g,'');
var option_1 = get("option_1")
let myString1 = parseFloat(num)-1;

if (value === 1 && option_1 === 0) {
send('/note', 6, myString1, 127)
}

else if (value === 1 && option_1 === 1) {
send('/note', 6, myString1, 127)
}

else if (value === 0 && option_1 === 1) {
setTimeout(function(){ 
send('midi:SessionKiano', '/note', 2, 85, 127) // this doesn't
console.log("Hello!") // this works
}, 1000);
}

This works (Cubase receives: note 85, on channel 2, at velocity 127):

if (value === 1) {
send('midi:SessionKiano', '/note', 2, 85, 127)
}

I just copy-pasted your code in a button widget with an input widget named "option_1" set to 1, the midi message is sent as expected after 1s delay when the button's value is set to 0.

timeout_send.json (3.3 KB)

I have simplified the code and made a video:

(the json in the video)
timeout - not working.json (3.1 KB)

It works fine here, please check the server's console for debug messages with debug enabled)

osc

Copied text from the server's console:

After starting the server
(INFO) Server started, app available at 
    http://127.0.0.1:8080
    http://192.168.43.2:8080
(INFO, MIDI) Inputs
    -1: Void (bypass)
    0: Network Session 1
    1: Network Session 2
    2: IAC SessionKiano
    3: IAC SKiano-A
    4: IAC SKiano-B
    5: IAC SKiano-C
    6: Lexicon Lambda
    7: UMA25S
    8: Bome MIDI Translator 1
(INFO, MIDI) Outputs
    -1: Void (bypass)
    0: Network Session 1
    1: Network Session 2
    2: IAC SessionKiano
    3: IAC SKiano-A
    4: IAC SKiano-B
    5: IAC SKiano-C
    6: Lexicon Lambda
    7: UMA25S
    8: Bome MIDI Translator 1
(DEBUG, MIDI) device "Network Session 1" created
(DEBUG, MIDI) device "Network Session 1" connected to input port 0 (Network Session 1)
(DEBUG, MIDI) device "Network Session 1" connected to output port 0 (Network Session 1)
(DEBUG, MIDI) device "Network Session 2" created
(DEBUG, MIDI) device "Network Session 2" connected to input port 1 (Network Session 2)
(DEBUG, MIDI) device "Network Session 2" connected to output port 1 (Network Session 2)
(DEBUG, MIDI) device "SessionKiano" created
(DEBUG, MIDI) device "SessionKiano" connected to input port 2 (IAC SessionKiano)
(DEBUG, MIDI) device "SessionKiano" connected to output port 2 (IAC SessionKiano)
(DEBUG, MIDI) device "SKiano-A" created
(DEBUG, MIDI) device "SKiano-A" connected to input port 3 (IAC SKiano-A)
(DEBUG, MIDI) device "SKiano-A" connected to output port 3 (IAC SKiano-A)
(DEBUG, MIDI) device "SKiano-B" created
(DEBUG, MIDI) device "SKiano-B" connected to input port 4 (IAC SKiano-B)
(DEBUG, MIDI) device "SKiano-B" connected to output port 4 (IAC SKiano-B)
(DEBUG, MIDI) device "SKiano-C" created
(DEBUG, MIDI) device "SKiano-C" connected to input port 5 (IAC SKiano-C)
(DEBUG, MIDI) device "SKiano-C" connected to output port 5 (IAC SKiano-C)
Button_1 (just send)
(DEBUG, OSC) Out:  { address: '/button_1', args: [ { type: 'f', value: 1 } ] } To: 192.168.43.2:8080
(DEBUG, OSC) In:  { address: '/button_1', args: 1 } From: 192.168.43.2:8080
(DEBUG, MIDI) out: NOTE_ON: channel=2, note=85, velocity=127 To: midi:SessionKiano
(DEBUG, MIDI) in: NOTE_ON: channel=2, note=85, velocity=127 From: midi:SessionKiano
(DEBUG, OSC) In:  { address: '/note', args: [ 2, 85, 127 ] } From: midi:SessionKiano
(DEBUG, OSC) Out:  { address: '/button_1', args: [ { type: 'f', value: 0 } ] } To: 192.168.43.2:8080
(DEBUG, OSC) In:  { address: '/button_1', args: 0 } From: 192.168.43.2:8080
Button_2 (with timeout)
(DEBUG, OSC) Out:  { address: '/button_2', args: [ { type: 'f', value: 1 } ] } To: 192.168.43.2:8080
(DEBUG, OSC) In:  { address: '/button_2', args: 1 } From: 192.168.43.2:8080
(DEBUG, OSC) Out:  { address: '/button_2', args: [ { type: 'f', value: 0 } ] } To: 192.168.43.2:8080
(DEBUG, OSC) In:  { address: '/button_2', args: 0 } From: 192.168.43.2:8080

EDIT: I'm checking now to see if my Windows machine behaves differently.

Also, this is the content of my midi field (copied from the server's configuration form):

mtc list sysex "Network Session 1:Network Session 1,Network Session 1" "Network Session 2:Network Session 2,Network Session 2" SessionKiano:SessionKiano,SessionKiano SKiano-A:SKiano-A,SKiano-A SKiano-B:SKiano-B,SKiano-B SKiano-C:SKiano-C,SKiano-C path=/usr/local/bin/python3

Easier to read:

→ mtc list sysex 
→ "Network Session 1:Network Session 1,Network Session 1" 
→ "Network Session 2:Network Session 2,Network Session 2" 
→ SessionKiano:SessionKiano,SessionKiano 
→ SKiano-A:SKiano-A,SKiano-A 
→ SKiano-B:SKiano-B,SKiano-B 
→ SKiano-C:SKiano-C,SKiano-C 
→ path=/usr/local/bin/python3

EDIT 2: On windows it behaves the same:

Ok I found the cause of your issue: you are sending every message back to the osc server directly because of the send option set to 192....:8080. When the button is pushed, the timer starts, then it receives its value again and the timer restarts, but this time the action is triggered by an incoming osc message and not by a user interaction, which prevents the message from being sent (that's by design, widgets do not send feedback messages).

I don't see any good reason to keep this feedback loop, clearing the send option will fix your problem.

path=/usr/local/bin/python3

This option should not be set if you're using the windows package from this site (python is not required since v1.7).

1 Like

Damn, I just installed Ubuntu linux (in a virtual machine). I thought it might be the time to give it a try (since both macOS and winOS have failed me with this O-S-C issue).
Also thanks for the bonus tip (about python)! You're too kind.

It's never too late to try linux :slight_smile:

1 Like