Can I read and set the value for my fader from a received midi message?

Hey guys!

I have a fader here and I'm currently switching it so that I can use one fader to control multiple things.

here's my code:

if(@{CSSCloseSwitch} == 1){
  console.log("1");
  send("/control", 1, 60, value);
}

if(@{CSSCloseSwitch} == 2){
  console.log("2");
  send("/control", 2, 61, value);
}

if(@{CSSCloseSwitch} == 3){
  console.log("2");
  send("/control", 3, 62, value);
}

if(@{CSSCloseSwitch} == 4){
  console.log("2");
  send("/control", 4, 63, value);
}

if(@{CSSCloseSwitch} == 5){
  console.log("2");
  send("/control", 1, 65, value);
}

if(@{CSSCloseSwitch} == 6){
  console.log("2");
  send("/control", 1, 60, value);
  send("/control", 2, 61, value);
  send("/control", 3, 62, value);
  send("/control", 4, 63, value);
  send("/control", 5, 65, value);
  send("/control", 6, 61, value);
  send("/control", 7, 61, value);
}

The 1~5 value represent from Violin to Bass
And the 6 value is a overall control(sending to all channels and CCs)

The problem is, my fader wouldn't "store/read" any value in this case, it's just sending values.

But I want the value of the fader to stay the same as set before once I switch it back.

For instance, I set VLN II's value to 50, and then I switch it to VLN I, and change the value to 100. Then when I switch it back to VLN II, the value should jump to 50 as set before.

My first approach is : store the values to a custom variable like: I'll add "var vln2 = [the sent value]" to the if statement of if(@{CSSCloseSwitch} == 2) and so on.

But I don't know how excatly can I store the currently sent value to a variable.

So my second thought is : I can store the returning value(using "midi send" in Cubase can loop the midi message sent by OSC back) in a variable and then set the currently value to the variable value.

But I still don't know how to read a received value in OSC. And I fear there might be a "feedback" problem:

read&store the received value(best if I can store only the "value" part) to var vln2

if(@{CSSCloseSwitch} == 2){
  set value to var vln2
  console.log("2");
  send("/control", 2, 61, value);

So that's basically it!

In short, I just want a fader switching between different "modes" and would never lose the value of every "mode".

Does anyone have the solution for this?

Thanks in advance :kissing_cat: :kissing_cat:

Fader_Store_Values.json (7.9 KB)

BTW this is my file :face_holding_back_tears:

The "console.log" doesn't matter, it's just a thing I added for test :joy:

Quite tricky to do :sweat_smile:.
Here's my approach:
Fader_Store_Values.json (9.2 KB)
Warning: it's not recommended using @{widget_id} syntax in any scripting prop. Use get() function instead, as stated in the docs.
So, I created a variable called close_mic_state as an object. Each key represents a string instrument.
When you change the instrument group, it sets the fader to the corresponding stored value.
I've changed the fader as well and moved things to a better workflow.
It's quite complicated to explain each nuance, but take a look and I'll try to answer any doubts you have.
Cheers.

Thanks man!

I've tried out the get() function, but it says "not defined" so I'm using the @{} :tired_face:

I've used it in the session I uploaded above. Take a look, it works.

// widget id must be in quotes 
get("widget_id")

Thanks for the session file!

I got most of them figured out, but there're still a few questions:

  1. What is a locals.faderState? Is it a variable? How is it different from just creating a variable using var fader.State?

  2. Is the local.faderState.vln creating a new variable or does it have a certain connection to the local.faderState variable?

  3. What is this line of code doing?

Object.keys(locals.faderState).forEach(k => locals.faderState[k] = value)

locals is a default object created internally by OSC for each widget.
In the onTouch prop, when you touch the fader, it stores the variable close_mic_state with all mic states locally in the fader widget. This avoids having to read this value from the variable widget all the time in the onValue prop.

locals.faderState is an object inside the locals object and .vln1 creates a new variable inside of it. Basically, it makes a copy of the close_mic_state, set the new values and updates the close_mic_state widget.

This code just loops through all the instrument groups and set them to the same value. This only happens when All options is selected. It's just another way of setting each value without having to write the code line by line.

Thank you again! :face_holding_back_tears:

I'm currently doing a "Switch" version of this.

But it seems like the original code you made for faders won't work with switch :sob:

I want every patch(articulations) can store values and be controlled by the "VLN I VLNII" drop down just like the faders.

Switch.zip (290.2 KB)

Here's my file.