How to use variables in a sysex string?

Hi,

I'm making progress with OSC and when it works it is such a joy!

Yet I can't figure out how to use variables in a sysex string.
Putting something in a fader's script field like

send('/sysex', 'F0 43 15 F7') works fine, but

send('/sysex', 'F0 43 @{this} F7')

produces an error. The fader range is set to 0-127, decimals: 0
I don't know if it's matter of wrong syntax or something else.

SysEx uses hexadecimal system for its messages, so I think you'd have to somehow translate the variable on the fly. Maybe custom module would be the best for this.

You're right! I simply didn't think of that hex thing.

Maybe something akin to

@{this}.toString(16)

(which didn't work btw)

A custom module may indeed be the best way, though I was hoping to avoid having to write them.

Don't use @{} in script fields. Use get or getProp. Check out the documentation under scripting.

i encourage to use a custom module (i think for such things it is needed), you can take a look at my project where i did a lot of sysex conversion. GitHub - schoko11/KEKO-Kemper-Control: Control your Kemper by MIDI via Touchscreen
(maybe not that easy at first look, but you get a starting point) -> but be warned i am a beginner, don't expect too much.

Thanks for these suggestions. Much appreciated.

@ GeneralMidi: I had tried several things including the get() function. I got neither of them to work, which was a clear sign of my inexperience with both OSC and Javascript (I'm not a coder).

Today however, after several hours of trying things out and studying examples I got it working.
It may not seem like much yet for me it's a little victory.

I created a fader and put these lines in the script field:

var nn = get("this");  // nn gets the fader position (0..127)

send("/sysex", "F0 43 11 62 02");
send("/sysex", nn);
send("/sysex", “F7")

For 7-bit NRPN messages it is:

send("/control",2,98,0);
send("/control",2,99,1);
send("/control",2,38,get("this"))

No custom module needed :slight_smile:

@ abstrus: thanks for sharing your project with the community. Very informative. Writing a custom module myself without the required Javascript experience is a bit too daunting at the moment. Perhaps in the near future I'll learn a little more of Javascript. For now I'm already very pleased with what I can make OSC do.

2 Likes

Nice, I didn't realize you could send the sysex in multiple calls, good job figuring that out.

I think what you missed before was string concatenation.

It's also possible to send sysex with integers only instead of hexadecimal bytes:

send("/sysex", 240, 67, 17, 98, 2, nn, 247)
// parseInt("F0", 16) == 240, etc
1 Like

Thanks for this addition! Working with decimal numbers is even better.
Actually I started with a concatenated version, which didn't work at first because I made a silly syntax mistake.

Is there anything which can't be done when concatenated, while possible when split up in multiple calls? For instance:

var faderValue = get("this");
var offset = 16;

send("/sysex", "F0 66 10 32");
if (@{pitch} >= 60)
    {
    send("/sysex", faderValue);
    } else {
    send("/sysex", faderValue + offset)
    }
send("/sysex", "F7")

I guess with slightly more advanced code almost anything can be done within the send statement. Maybe only less readable.

send("/sysex", "F0 66 10 32", get("pitch") ? faderValue : faderValue + offset, "F7");

Aha, that's elegant - using the ternary operator. Nice to learn about it.
I suppose the code including the condition will be:

send("/sysex", "F0 66 10 32", (get("pitch") >= 60) ? faderValue : faderValue + offset, "F7");

You're right, I forgot it !