logo_jazz jp


storing

this tutorial looks at basic scripting techniques used to implement presets functions on your lemur, either through separate store and recall commands (2 distinct groups of buttons) or through simultaneous store and recall commands (a single group of buttons)



storing_presets

Let’s begin with a relatively straightforward situation. We’re using a MultiSlider object and we’d like to be able to store its current values as presets by pressing the red Pads object, and recall those values independently by pressing the green Pads object. In addition, we’d also like to have an indication that a “memory slot” is filled by highlighting the corresponding Pad, and have a Reset button to empty the memory slots (This is actually a simpler version of the Presets module, available in the v2.0 Example Package’s modules library. All the examples used in this tutorial including this first example can otherwise be downloaded here.)

presets

Now the first thing to do once we’ve clearly conceived how our Presets module should work, is to create a “memory space” that can be referred to anywhere in the scripts to store the MultiSlider’s values. To that end we’ll simply create a User defined variable at the Container’s level, which we’ll call presets, and leave it completely empty (it doesn’t matter where you create it, but creating it at the Container or Project’s level is tidier).

Be aware that our empty variable, according to the JazzEditor’s framework, will actually give us room to store a vector containing up to 256 single values. Given that our MultiSlider here is made of 16 sliders, we have room to store up to 16 presets (16 sliders x 16 presets = 256 single values) per empty variable.

We’ll also create a second empty User defined variable, called presets_light, to store the state of our 16 memory slots (a vector containing 16 zeroes and ones – empty or filled). The presets_light variable will then be typed in the light parameter field of the Pads objects.

presets


At this point a small diagram could help us grasp the concept before attempting to code the instructions with a script. The idea is that when one of the red pads is pressed, the values generated by the MultiSlider (MultiSlider.x, a vector containing 16 values between 0 and 1) are transferred to the corresponding space inside the presets variable. For example, when the first pad (labelled 0) is pressed, values of presets[0] to presets[15] will be set to those displayed by MultiSlider. When the second pad is pressed (labelled 1), values of presets[16] to presets[31] will be set to those displayed by the MultiSlider and so on. At the same time, the value of presets_light should be updated each time the state of the Pads changes.

presets={ 0.9,0.8,0.7,0.7,0.6,0.6,0.5,0.4,0.4,0.3,0.3,0.2,0.2,0.1,0.1,0.0, 0.2,0.4,0.5,0.6,0.8,0.9,1.0,1.0,0.8,0.8,0.7,0.5,0.3,0.3,0.2,0.1 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0, …}
arrow arrow
presets presets presets
pad pad pad
arrow arrow
presets_light={ 1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }



In the JazzEditor, the script used to make this happens is called action, and is associated with the red Pads object named Store:

script
script

The script’s execution mode is set to On Expression x, triggered when the value goes from 0 to positive, that is, each time one of the red Pads is pressed. We then begin the code by declaring a script variable that we’ll call index, and define it as being equal to the expression firstof(x).

As you probably already know, the firstof function returns the position of the first non-null item in a vector or the size of the vector if the vector only contains null items. In other words, when the first pad (labelled 0) is pressed, x={1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} and firstof(x)=0. When the second pad is pressed (labelled 1) is pressed, x={0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0} and firstof(x)=1. (The fact that the firstof function returns the size of the vector if the vector only contains null items can be ignored here, as our declared variable index will only be active when the script is being executed, and the script will only be executed when a Pad is pressed).

We then use our index variable to tell the Lemur where to store the current MultiSlider’s values within our memory slot. More specifically, we indicate the position within the presets array from where the MultiSlider’s values (a vector containing 16 values) should be written. The next line of code sets a 1 in the array presets_light in order to light up the corresponding pad. Imagine for instance that the fifth pad is pressed, in which case x={0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0}, and index=5. The MultiSlider’s values will be stored from position 80 ([index*16]) in the presets array, while the fifth element of presets_light will be set to 1 (presets_light={0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0}), and the 5th red pad will light up.

Now let’s have a quick look at the script in charge of loading the presets:

script
script

Here a loop structure is used to tell each individual slider where to get its value from within the presets array. We begin by declaring two script variables: index, which will give us the index of the green pad being pressed, and i, which will define the loop’s initial value, test and counting expressions.

The initializer i=0 sets up the initial value of the variable i to 0, which means that the first time the line of code within braces will be executed for MultiSlider.x[0], setting up the value of the first slider to presets[index*16+0]. That means that if the first green pad is pressed (index=0) for instance, MultiSlider.x[0]=presets[0], and if the second green pad is pressed (index=1),MultiSlider.x[0]=presets[16] as we jump to the second “group” of values within the array. The variable i is then incremented by the counting expressioni ++ to i=1, and the code within braces repeated for MultiSlider.x[1]. The variable will be incremented and the code repeated as long as i is smaller than 16 (the size of the MultiSlider), which means for each slider up to MultiSlider.x[16].

Now to conclude this first example, we’ll take a quick look at the script associated with the Reset button:

script

Here the stretch function is used to set all the values of the presets and presets_light arrays. It’s a nice and easy way to say presets_light={0,0,0,0,0,0,0,0,0,0,0,….} and presets={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0….}



storing_presets2


Now for this second example we’d like to use a single group of switches in radio mode to store and recall our presets. What happens here is that the current values of the MultiSlider will be saved in the “memory slot” corresponding to the selected switch precisely when a different switch is pressed (and a different preset recalled).

script


As in the first example, we’ll create an empty User defined variable called presets which will serve as a “memory space” to store the MultiSlider’s values, and a second variable called presets_light to highlight the “memory slots” in use. In addition, as you’re about to see, we’ll also need a third variable to keep the latest switch selection in memory. We’ll call this variable lastposition.

script


Now let’s have a look at the script in charge of it all. It’s called action(), and it’s associated with the Switches object.

script

The script’s execution mode is set to On Expression x, so it will be executed each time a new switch is pressed. As in the first example, we begin by declaring a script variable that we call index. Now remember that when we press on a new switch, we want the lemur to store the values that we’ve just set on the MultiSlider in the “memory slot” corresponding to the switch we’re leaving (=lastposition). Translated in code, this means setting the values of the presets array starting at position given by lastposition and multiplied by the number of sliders, in other words presets[lastposition*16]=MultiSlider.x;. The intial value of lastposition will be 0.

The next line of script is similar: here we tell the element of the array presets_light corresponding to the switch we’re leaving to light up to 0.5 (the light parameter field of the Switches object is defined by presets_light). This is the “storing” stage of the script. The next stage is to load the values corresponding to the switch we’re jumping to (these values will be zeroes if the “memory slot” is empty). Here we use the exact same lines that we used for the green pads in the first example: a loop structure that will fetch the correct value for each slider.

The final line of the script updates the value of the variable lastposition with the index of the switch we’ve just pressed, so that next time the script is executed (next time we press a switch), the value of lastposition will still be the index of the “switch we’re leaving”.



Download this example
Go back to workshops


© Copyright JazzMutant 2016 12-25-2016