Ramp switches value

Discuss Lemur and share techniques.
Keithlostracco
Newbie
Posts: 11
Joined: 12 Dec 2011 01:44

Ramp switches value

Post by Keithlostracco »

I'm using switches to control sends in ableton via midi. Basically a switches object with 30 switches is opening closing 30 sends. I found after connecting some of the switches that I was getting clicks from the send opening so quickly so I've been attempting to write a script that will ramp up a value (0 to 127)when a switch is turned on and ramp down when it is turned off. I've tried a few to different techniques either using a for loop and the time variable but haven't found a way yet. It seems to me that it should be a relatively simple script but the for loop is not acting how I would expect. Would anyone have an idea on how to go about this?

thanks
Keith
Keithlostracco
Newbie
Posts: 11
Joined: 12 Dec 2011 01:44

Re: Ramp switches value

Post by Keithlostracco »

So I have somewhat of solution but it is far from ideal. I'm using switches to trigger faders and then I have a script running every frame and a couple of if statements for every switch/fader. I wouldn't mind using faders to actually send the controller data (and the switches to trigger the faders) but the amount of scripts I would have to write would be way to much to execute every frame. I would need about 12 scripts with 10-60 if statements in each to control the 240 or so sends in our set. Maybe with this example below someone might have an idea. It would be so much better if I could use a for loop (and execute on expression) to change the value over time but a loop statement seems to cycle through all the values in a vector not cycle over one of the values many times.

thanks
Keith
Attachments
autofader.jzml.zip
(1.19 KiB) Downloaded 89 times
mbncp
Regular
Posts: 87
Joined: 08 Dec 2011 07:25

Re: Ramp switches value

Post by mbncp »

You can use the OnFrame function like a for loop, just use some global variable.

Best is to use the array(vector) function to detect changes and then send the midi from the script.

I'll show you tomorrow how to handle that, as the array part can be a little tricky.
Keithlostracco
Newbie
Posts: 11
Joined: 12 Dec 2011 01:44

Re: Ramp switches value

Post by Keithlostracco »

Thanks for taking a look. When you say use the OnFrame function are you talking about the way the script is executed? Do you mean I can get the OnFrame script to stop running by relating to a global variable?
Best is to use the array(vector) function
Do you mean nonnull? I can't find an array(vector function in the user manual.

I guess you might have an example tomorrow to show what you mean.

thanks for helping me out

cheers
Keith
mbncp
Regular
Posts: 87
Joined: 08 Dec 2011 07:25

Re: Ramp switches value

Post by mbncp »

You can give it a try:
SwitchRamp.jzml.zip
(1.63 KiB) Downloaded 128 times
You can expand the switches to your will (no code to change), but for testing it's easier to have just a few.
I assume that you use a range of CC so set the CC variable to the first CC you plan to use.
Also the midi channel is set to 1, change it if needed.

To see what's going on I put a Manual switch, in this case you have to hit the Trigger button to call the OnFrame function.

There is also a Reset button that just calls the OnLoad function of the switches grid, no CC are sent , but this can be easily changed.

Currently the ramp is linear, maybe a curved ramp would be more appropriate.

The monitor dip lays the current ramp values, 0 means that we reached the destination, a negative value means that the ramp is going down and a positive value shows that the ramp is going up.
Note that if you hit a switch twice and before it reach it's min or max value, the ramp will continue from where it is, but in the other direction.
Keithlostracco
Newbie
Posts: 11
Joined: 12 Dec 2011 01:44

Re: Ramp switches value

Post by Keithlostracco »

Awesome it works perfectly. Later today when I have more time I'll look at it closer so I can understand more clearly what you've done. One thing I realized is that I need the switches to update their state when Ableton changes after we select a new kapture preset. I was thinking of writing another script triggered On Midi to update the switches value. Do think that would be that way to go about this?

nice work!

thanks
Keith
mbncp
Regular
Posts: 87
Joined: 08 Dec 2011 07:25

Re: Ramp switches value

Post by mbncp »

Yes, add an OnMidi script to the switches, set to controller, port_0, set the cc range and correct channel.

Then we need to prevent to trigger our own cc. I think the code should be like this:

Code: Select all

decl n = MIDI_ARGS[0] - CC;
if(n < 0 || n >= sizeof(x)) return;

if(ramp_x[n] != 0) ramp_c--;
ramp_x[n] = 0;
prev_x[n] = MIDI_ARGS[1] == 0 ? 0 : 1;
x[n] = prev_x[n];
btw, I'm not sure this is documented but is often used in C.

variable = expression ? true : false;
so writing :
x = i > 10 ? 1 : 0;

is the same as

If(i > 10)
{
x = 1;
}
else
{
x= 0;
}
Keithlostracco
Newbie
Posts: 11
Joined: 12 Dec 2011 01:44

Re: Ramp switches value

Post by Keithlostracco »

The On Midi script worked also. Now Ableton can set the switch state. Thanks for the coding tip I had the feeling that the Lemur manual didn't have everything.

I am noticing that sometimes when pressing the switch in the middle of a fade the the value will jump. I can't get it to happen consistently though. No big deal I can live with that no problem.

I added another switches object that changes the ramp rate based on tempo. For the tempo to work you need to run Ableton with the Mu M4L device on a track to get the tempo value(the Mu doesn't have to be synced to the Lemur though its just sending an OSC tempo message). If your not running ableton or M4L you just have to change the tempo variable to the desired tempo.

Awesome - hands free fade in/out or crossfades on any send!

cheers
Keith
Attachments
SwitchRamp2.jzml.zip
(1.87 KiB) Downloaded 114 times
mbncp
Regular
Posts: 87
Joined: 08 Dec 2011 07:25

Re: Ramp switches value

Post by mbncp »

Yep that's cool, I still have to implement it in my main template, dealing with Live as well.

Not sure what's going on with the jumping thing, normally it shouldn't, I'll check this later.
Keithlostracco
Newbie
Posts: 11
Joined: 12 Dec 2011 01:44

Re: Ramp switches value

Post by Keithlostracco »

Hi sorry to bug you again but I'm trying to modify the script so it can send 2 values, a rising value and a diminishing value. I'm part way there and it works with the first value rising and the second diminishing but when I press the switch a second time it the first value diminishes properly but the second value just jumps to full level when the first is done its cycle. I'm pretty sure it's because of the first part of the if statement but I can't figure exactly what to do. Here is the modified code;

Code: Select all

decl c, n, r, s;
if(ramp_c < 1) return;

c = sizeof(x);
r = ramp_x;
n = firstof(r);

while(n < c)
{
	s = ramp_x[n] < 0 ? -1 : 1;
	ramp_x[n] = ramp_x[n] - (s*Speed.y);
	if( (s > 0 && ramp_x[n] <= 0) || (s < 0 && ramp_x[n] >= 0) )
	{
		ctlout(0,CC+n, s > 0 ? 127 : 0, CC_Chn);
		ctlout(0,CC2+n, s > 0 ? 0 : 127, CC_Chn);
		ramp_x[n] = 0;
		ramp_c--;
	}
	else 
	{
		ctlout(0,CC+n, floor(abs(s > 0 ? (1-ramp_x[n])*127 : ramp_x[n]*127)), CC_Chn);	
		ctlout(0,CC2+n, floor(abs(s > 0 ? ramp_x[n]*127 : (1-ramp_x[n])*127)), CC_Chn);	

	}
	r[n] = 0;
	n = firstof(r);
}
any ideas?

thanks
Keith
Post Reply