Mapping one range of values to another needed range of values is a crucial technique in computer music. In this example, we want to map MIDI data values that range from 0 to 127 into a useful range for controlling the amplitude—and thus the loudness—of a sound in MSP.
This patch uses metro, counter, and table to read through MIDI values to create an ascending and descending major scale.
When you play a note with MIDI, you usually want the note to sustain as long as the key is held down, then you want it to turn off (either immediately or gradually) when the key is released (when the note-off message is received). Because MIDI is designed to function in real time, in live performance, there is no duration information contained in a note-on message. The duration can only be known once the key has been released.
To get the values of only one particular continuous controller, use the ctlin object, or use the route object to parse the controller data coming out of midiparse. To change one range of values into another, use simple math (usually one multiplication and one addition will be enough), or use the scale object.
Incoming MIDI control values (0 to 127) from a ctlin object can be scaled with the scale object to cover any desired pitch range (in terms of MIDI pitch number), and that pitch range can then be converted to frequency in Hertz with the mtof MIDI to frequency object.
This example shows how you can change the length of a ui object (a slider object in this case) with the patching_rect message. You can use a scale object to get the range of width values you want, and control it with a single float, and then use the ‘$1’ argument in a message box to insert the width value into the patching_rect message.
The most direct way to convert one range of numbers into a different range of numbers is a process called linear mapping. For each number in a source (input) range, find the corresponding number in a destination (output) range. The process is to multiply the input value by the size of the destination range (destination maximum minus destination minimum) divided by the source range (source maximum minus source minimum), then add the destination minimum to that.
To translate numbers that occupy a particular range into an equivalent set of numbers in a different range, one common and useful technique is "linear mapping". The term "mapping" refers to making conceptual connections between elements of one domain and elements of another, and "linear" mapping refers to using a mapping function that is a straight line––that is, such that numbers in one domain are mapped to an exactly equivalent position in the new domain. This is a very common and useful operation in media programming.
Here are three ways of generating MIDI notes. Admittedly they don't result in very interesting music, but they show ways how numbers can be converted for usage as pitch information.
The slider object has some attributes that have a significant effect on its output: floatoutput, size, min[imum] and mult[iplier]. By default slider uses only integer values, with a size of 128 (i.e., ranging from 0 to 127). For an integer slider, the range will be size different integers from 0 to size-1, whereas for a float slider the input range will be the full range from 0. to size., inclusive.