Midi.note with switch function question

48 views
Skip to first unread message

Sebastian Ismén

unread,
Dec 17, 2019, 3:54:15 PM12/17/19
to Lightjams


Hi,
So a friend of mine helps me quite alot to get started with lightjams and we are programming a project for my midi controller (Apc40 Mk2). 
We were trying to create a source with 5 different attributes with switch. Here's the line:
gmem(2, switch(midi.note.latest(1, 1, 33), 1, 0, 9, triangle(select(midi.control(1, 49), 32, 16, 8, 4, 2, 1, 0.5, 0.25, 0.125)), 17, sawtooth(select(midi.control(1, 49), 16, 8, 4, 2, 1, 0.5, 0.25, 0.125, 0.125)), 25, randorganic(select(midi.control(1, 49), 16, 8, 4, 2, 1, 0.5, 0.25, 0.125, 0.125)), 33, latch(pulse(select(midi.control(1, 49), 16, 8, 4, 2, 1, 0.5, 0.25, 0.125, 0.125)), rand()), last))
And we typed this into the X-bar as moving attribute:
gmem(1, switch(midi.note.latest(1,2,34), 2, 50, 10, triangle(select(midi.control(1, 50), 32, 16, 8, 4, 2, 1, 0.5, 0.25, 0.125)), 18, sawtooth(select(midi.control(1,50), 16, 8, 4, 2, 1, 0.5, 0.25, 0.125, 0.125)), 26, randorganic(select(midi.control(1,50), 16, 8, 4, 2, 1, 0.5, 0.25, 0.125, 0.125)), 34, latch(pulse(select(midi.control(1,50), 16, 8, 4, 2, 1, 0.5, 0.25, 0.125, 0.125)), rand()), last))
If I press a midibutton that will affect the power bar the X bar stops working and vice versa.
Can I write something similar to that do we need to retype the entire thing?

/s

Sebastian Ismén

unread,
Dec 17, 2019, 4:25:51 PM12/17/19
to Lightjams
For some reason the entire second code got 100% green.. Here it is:
gmem(1, switch(midi.note.latest(1,2,34), 2, 50, 10, triangle(select(midi.control(1, 50), 32, 16, 8, 4, 2, 1, 0.5, 0.25, 0.125)), 18, sawtooth(select(midi.control(1,50), 16, 8, 4, 2, 1, 0.5, 0.25, 0.125, 0.125)), 26, randorganic(select(midi.control(1,50), 16, 8, 4, 2, 1, 0.5, 0.25, 0.125, 0.125)), 34, latch(pulse(select(midi.control(1,50), 16, 8, 4, 2, 1, 0.5, 0.25, 0.125, 0.125)), rand()), last))

Mathieu

unread,
Dec 17, 2019, 9:42:00 PM12/17/19
to Lightjams
I'll attempt an answer but I may need more info to fully understand.

To reuse parts of your big formulas and avoid typing them multiple times all over the places, you can use sources on a dedicated grids and assign the sub formulas to their power sliders and store the result using gmem. Then use grecall in your big formula. Alternatively, use the global sliders (ctrl+k) and assign them your sub formulas. Then use the slider function in your big formula.

clava aski

unread,
Dec 18, 2019, 3:55:05 PM12/18/19
to Lightjams
Hey!
I'm working with Sebastian on this. The issue we have is with the switch(midi.note.latest). Our power parameter is using switch(midi.note.latest(1, 1, 33) and the X parameter uses switch(midi.note.latest(1, 2, 34). As you can see they overlap but they use different midi notes to select the different functions, so we should be able to have a function running on the power parameter while selecting another function for the X parameter. I just tested the same with shorter formulas on another midi pad and the result is the same. When we press a button for the power parameter the wave in our X parameter just stops, and vice versa. It seems to be a bit erratic as well, sometimes it works and sometimes not. 

test:
switch(midi.note.latest(9, 49, 51), 49, sin(1), 51, triangle(2), last)
switch(midi.note.latest(9, 50, 52), 50, sin(1), 52, triangle(2), last)
Tried it with and without "last" as defaultfunction. If we replace the sinus and triangle waves with simple values (0-100) everything works fine. 

Mathieu

unread,
Dec 18, 2019, 4:35:40 PM12/18/19
to Lightjams
Ok, that's interesting. If you press note 50 since there's no case for it in the switch for the power, the "last" variable value is used. The "last" is just a value, not a function. So what you see is the same value being returned until you press note 49 or 51. What you want to do is to specify a function for all notes from 49 to 51. If you have a gap, then the 'last' value is returned.

Mathieu

unread,
Dec 18, 2019, 5:17:59 PM12/18/19
to ligh...@googlegroups.com
Here's a way to do what you expect by adding even more switch fun :)

What you seem to want to do is to only handle note 49 and 51 for the power and ignore note 50 (do the same for the X but with the other notes). This is what this formula does:

switch(midi.note.latest(9, 49, 51), 49,49, 51,51)

You can test it and you'll see that once you've pressed note 49 or 51, note 50 is ignored. This uses the property of the switch function that returns the last value when there's no match.

Then the trick is to use the output of this formula in your other switch like this:

switch(switch(midi.note.latest(9, 49, 51), 49,49, 51,51), 49, sin(1), 51, triangle(2), last)

You can remove the "last", which does nothing in this case since it's the default behaviour:

switch(switch(midi.note.latest(9, 49, 51), 49,49, 51,51), 49, sin(1), 51, triangle(2))

There's probably a more elegant way to do this, but I guess this should help you get started.


clava aski

unread,
Dec 18, 2019, 5:24:15 PM12/18/19
to Lightjams
Ah ok, so "last" cannot return a function if i understand you correctly? We worked it out by using two sliders with:

switch(midi.note.latest(9, 49, 51), 49, 0, 51, 1) 
on the first slider and then
switch(slider(1), 0, sin(1), 1, triangle(2))
on the source. And the other midi.notes on the other slider etc

clava aski

unread,
Dec 18, 2019, 5:34:37 PM12/18/19
to Lightjams
"[defaultValue]: Optional result if no match is found. If no default value is provided, then the last result is returned."
Copied from the formula list. I've always been reading "the last result" as if it meant "the last function in the formula" :D 

If I try the same thing as before but without the "last" in the switch formulas the result is the same though, i guess that means that the switch function has a built in "last" function which is only able to return a variable as well?

Den onsdag 18 december 2019 kl. 22:35:40 UTC+1 skrev Mathieu:

clava aski

unread,
Dec 18, 2019, 5:38:24 PM12/18/19
to Lightjams
Interesting!

Mathieu

unread,
Dec 18, 2019, 5:43:49 PM12/18/19
to Lightjams
The last variable is the last value of the whole formula. The switch maintains its own last value, which can be different than the last variable of the whole formula.

clava aski

unread,
Dec 18, 2019, 5:48:58 PM12/18/19
to Lightjams
Yes, though it cannot be a function, even if the last value of the switch is a function, correct?

clava aski

unread,
Dec 18, 2019, 5:50:37 PM12/18/19
to Lightjams
(sorry if my answers seem a bit erratic, im starting to get confused by all the variables, functions, values and formulas)

Mathieu

unread,
Dec 18, 2019, 6:01:17 PM12/18/19
to Lightjams
Let say the last number instead of value :)

clava aski

unread,
Dec 18, 2019, 6:17:18 PM12/18/19
to Lightjams
Hehe :D
So, the switch will return a number if there is no matching case, even if the last value was a wave or another function? Wouldn't it make more sense for it to return a function if the last value was a function?

To clarify what I wrote before; I've been thinking that "then the last result is returned" means "the last word written in the formula"... mostly cause I'm an occasional moron :P Now it makes more sense.
Reply all
Reply to author
Forward
0 new messages