My script is not working

Hi all,

I want my panel color to change when midi is coming in.

I tested like this:

Panel dynamic color: ${osc(“/shared/keys”) ? “blue-100” : “green-500”}

now i used a button which sends following osc messages

on press: /shared/keys true

on release: /shared/keys false

If I now press the button, the color of the panel changes. But now I want to have the change happen when a midi note is coming in. So I went to the midi settings and went to the device (aic driver in this case) and set it to script, so that all midi notes should trigger.

the script i used:

if(midi.type === "note-on" && midi.channel === 1) {
  sendOsc("/shared/keys true");
}
if(midi.type === "note-off" && midi.channel === 1) {
  sendOsc("/shared/keys false");
}

but now it does not work. I want the panel to quickly flash and go out again, so that I see that the midi note is coming.

the midi setting is flashing green btw, so it is clearly reciving my midi signal.

Thanks for any help!

Hey @FAster77,

when using sendOsc, the parameters need to be separated. This should work:

if(midi.type === "note-on" && midi.channel === 1) {
  sendOsc("/shared/keys", true);
}
if(midi.type === "note-off" && midi.channel === 1) {
  sendOsc("/shared/keys", false);
}

You can also simplify this by using the setShared function:

if(midi.type === "note-on" && midi.channel === 1) {
  setShared("keys", true);
}
if(midi.type === "note-off" && midi.channel === 1) {
  setShared("keys", false);
}

Let me know if this works for you :slight_smile:

Hey @leolabs ,

Thank you very much. I replaced it with the setShared funciton and it works super well.

But I found my setup with note on and off not so usefull when playing longer notes as I only want the midi to shortly flash.

so now i wrote this:

// channel 1
if(midi.type === "note-on" && midi.channel === 1) {
  setShared("ch1", true);
  await sleep(30)
  setShared("ch1", false);
}

This works well until i trigger 2 notes shorter apart than 30ms. What happens then is the ch1 stays stuck at true and no false command is sent in the end. Why does that happen? I thought because the last note would also send the false command, it should also be turned off. How does ableset handle situations like this and what would be the correct script to prevent that?

Thanks again!

Hey @FAster77,

Is that your entire script or just a part of it? From what I see this should run as expected, but maybe the duration between setting the variable to true and setting it to false is too short. Could you try increasing it to maybe 100ms and check if that helps?

In theory, any delay time should work, but maybe there’s a bug there that I haven’t discovered yet.

I’m looking forward to your reply! :slight_smile:

Hi @leolabs , thanks for your reply.

This is the whole script:

// channel 1
if(midi.type === "note-on" && midi.channel === 1) {
  setShared("ch1", true);
  await sleep(30)
  setShared("ch1", false);
}

// channel 2
if(midi.type === "note-on" && midi.channel === 2) {
  setShared("ch2", true);
  await sleep(30)
  setShared("ch2", false);
}

//channel 3 Drum Pad
if(midi.type === "note-on" && midi.channel === 3 && midi.note >= 37 && midi.note <= 43) {
  setShared("ch3dp", true);
  await sleep(30)
  setShared("ch3dp", false);
}

// channel 3 Kick
if(midi.type === "note-on" && midi.channel === 3 && midi.note === 36) {
  setShared("ch3k", true);
  await sleep(30)
  setShared("ch3k", false);
}

// channel 3 Snare
if(midi.type === "note-on" && midi.channel === 3 && midi.note === 48) {
  setShared("ch3s", true);
  await sleep(30)
  setShared("ch3s", false);
}

// channel 3 Bar Trigger
if(midi.type === "note-on" && midi.channel === 3 && midi.note === 44) {
  setShared("ch3bt", true);
  await sleep(30)
  setShared("ch3bt", false);
}

// channel 4
if(midi.type === "note-on" && midi.channel === 4) {
  setShared("ch4", true);
  await sleep(30)
  setShared("ch4", false);
}


I tried it first with 10ms, but that was to fast to really see the color. Afterwards I tried 300ms, but then it just stayed on and didn’t turn off again, which was strange. I figured 30ms was the sweet spot.

Hope this helps!

And btw, ableset 3 is so awesome. Canvas and scripting is a game changer. We had our first show with it this saturday and it stream lined the sound check process a lot!

Hey @FAster77, thank you for providing the script!

The issue lies in that each script can only run once at a time. If the script is triggered again while it’s running, the running script is cancelled and then run again with the new MIDI data.

This is mainly to prevent lots of scripts running at the same time but comes with a few caveats, like the issue you’re seeing here. Basically, the script triggers the first setShared and is then cancelled because the next MIDI note came in.

The easiest workaround would be to place all resets at the bottom of the script so they always run:

// channel 1
if(midi.type === "note-on" && midi.channel === 1) {
  setShared("ch1", true);
}

// channel 2
if(midi.type === "note-on" && midi.channel === 2) {
  setShared("ch2", true);
}

//channel 3 Drum Pad
if(midi.type === "note-on" && midi.channel === 3 && midi.note >= 37 && midi.note <= 43) {
  setShared("ch3dp", true);
}

// channel 3 Kick
if(midi.type === "note-on" && midi.channel === 3 && midi.note === 36) {
  setShared("ch3k", true);
}

// channel 3 Snare
if(midi.type === "note-on" && midi.channel === 3 && midi.note === 48) {
  setShared("ch3s", true);
}

// channel 3 Bar Trigger
if(midi.type === "note-on" && midi.channel === 3 && midi.note === 44) {
  setShared("ch3bt", true);
}

// channel 4
if(midi.type === "note-on" && midi.channel === 4) {
  setShared("ch4", true);
}

await sleep(30);

setShared("ch1", false);
setShared("ch2", false);
setShared("ch3dp", false);
setShared("ch3k", false);
setShared("ch3s", false);
setShared("ch3bt", false);
setShared("ch4", false);

Let me know if this works for you :slight_smile:

Hi @leolabs

thanks for your suggestion, but somehow I doubt that this helps, I’ll try it tho.

But my problem does not come from triggering 2 different types.

If I triggered ch1 and then suddenly trigger ch2, I think then I would run into this issue that you are describing, when the second script trigger only looks for ch2 and the ch1 got canceled. although I might have this issue, I did not notice it yet.

Also with the await command in the end I would run into the issue, that the midi note offs would also trigger the script. So If I trigger a note and then a second one, and the note off of the first one comes slightly after the note on of the second, the script would interrupt and not show a new signal.

But I had this issue when triggering ch1 only. For clarification, ch1 here is a midi keyboard. And when I press to notes really fast the setShared would get stuck at true.

And I don’t understand this because as you mentioned, the script should restart. So what should happen would be:

setShared ch1 → True

script gets reset by second note

setShared ch1 → True (which has no impact at this point

wait 30ms

setShared ch1 → False

So in any situation, the setShared ch1 should end up False in the end.

Thanks again!