The aim of this tutorial is to explore some of the most commonly used internal functions one may need or come across when working with vectors, starting with the firstof, the stretch and the set functions.
|
When working with vectors, it is very often the case that we need a single, readily usable value, out of the strings of zeroes and ones that we get from rows of switches, pads, multisliders and the likes.
The firstof function can be a great help when dealing with those, and particularly with rows of switches in radio mode.
Let’s take a row of 10 radio switches for instance, and use a Monitor object to check out its variable Switches.x, which is a vector, you’ll get something like {1,0,0,0,0,0,0,0,0,0} if the first switch is activated, {0,1,0,0,0,0,0,0,0,0} if the second switch is activated and so on.
The Parser reference tells us that 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. If we apply it to our row of 10 switches with the first switch activated, firstof(Switches.x) will give us 0. If the second switch is activated, firstof(Switches.x) will give us 1 and so on.
|
|
A simple application of the firstof function would be a module made of rows of radio switches that send Midi Program Change messages. Let’s say we need two rows of ten radio switches for instance, Switches.x and Switches2.x, that sends MIDI Program Change messages between 0 and 99. We want the user to be able to select a specific Program Change by inputting the corresponding digits: i.e, for Program Change 35, the user selects 3 in the first row of switches and 5 in the second. To do that we’ll create an expression called out, and enter 10*firstof(Switches.x) + firstof(Switches2.x) in the script field. We may also add a Monitor object for displaying the program change values, in that case we’ll simply enter the expression’s name in the value field of the Monitor object.
|
|
|
A second, very typical application of the firstof function, again in conjunction with radio switches, would be to determine the Midi channel number of Custom Midi messages.
In the example to the right, the row of four large purple switches, named Channels.x, is used within the Channel field of the Custom Midi together with a firstof function to set the value of the Midi channel. If the second button is pressed for instance, the channel number will be firstof(Channels.x)+1, which gives us 2. That way we may send the data from a single object to one of four different Midi channels.
|
|
|
Similarly, the firstof function could be used to set the controller number of a Custom Midi message.
In the example below, the num expression was added to the rows of Switches named Tracksend in the Project Browser. Tracksend.num here is simply equal to firstof(Tracksend.x), so that if the switch labelled 0 is activated, the controller number will be 100+0 = 100, if the switch labelled 1 is activated, the controller number will be 100+1 = 101, and so forth.
|
|
|
|
The stretch(a, size) function, as its name suggests, stretches a value or a range of values (a), and returns a vector. If (a) is a single value, the function returns a vector with (size) items containing that value. For instance stretch(0, 5) returns {0,0,0,0,0}, stretch(3, 10) returns {3,3,3,3,3,3,3,3,3,3}. If (a) is a range, the function will stretch that range over the chosen size of the vector. Say, if (a) ranges between 0 and 5, and size is 6, the expression stretch({0,5},6)will return {0,1,2,3,4,5,6}.
This function can save a great deal of time when in need of creating large vectors. In the example below, a stretch function is applied to the light parameter of a row of pads in order to obtain an even shade effect. Effectively, entering the stretch({-0.6,1},14) expression means creating a vector containing 14 values evenly distributed between -0.6 and 1, which amounts to something like {-0.600, -0.477, -0.354, -0.231, -0.108…up to 1.000} .
|
|
|
The stretch function is often used in conjunction with the set function to highlight an element within a vector. Since the set(a, value, position) function takes a vector a, and change the item at position to value, it could be used to switch on and off consecutive leds for instance. Imagine a couple of pads whose function is to toggle between various effects, and at the same time switch the position of a lit led in a row of leds to reflect this change.
|
|
|
|
We know that stretch(0,10) is a shortcut for a vector containing 10 times 0: {0,0,0,0,0,0,0,0,0,0}, which here defines the initial value of a row of ten leds. Now the expression set(stretch (0,10), 1, Pads.plusminus) will replace one of the zeroes with the value 1, according to the Pads.plusminus variable. In this case the Pads.plusminus variable is a value between 0 and 9, to which 1 is added or subtracted each time a pad is pressed.
|
Another similar case is a combination of the stretch and set functions used to highlight the current step of a stepsequencer. In the example below a multislider object is used to display the velocity of 8 steps in a stepsequencer interface. The set function is applied to the light parameter of the object to vary the light in relation to the current step of the sequencer, given by the variable pos4 (sent by the sequencer at a rate corresponding to the tempo of the song). So when pos4 equals 0, the light expression will return {1,0,0,0,0,0,0,0} and the first slider will be highlighted, when pos4 equals 1, the light expression will return {0,1,0,0,0,0,0,0} and the second slider will be highlighted.
|
|
|
|