Download Tutorials and Topics

Transcript
Loops: Ways to Perform Repeated Operations
Repeated Actions
Many aspects of music-making involve repeated actions. For example, we might count 25 measures of rests, hit a gong 4 times in succession, repeat the whole section a total of 3 times, etc. In
programming, repeated actions are called loops, because conceptually we think of the computer
completing an action, then looping back to the place in its stored program where it started and
performing the action again. A loop generally involves some sort of a check before or after each
repetition, to see whether the action should be performed again (without the check, the process
would continue endlessly).
In Max, a bang message can be used to signal that an event has occurred. In the example below, a
bang is sent each time the sustain pedal (controller number 64) is pressed down, or each time the
note Middle C (key number 60) is released.
Since bang is the generic indicator that something has happened, there is an object designed to
count bang messages, called counter. It counts the bang messages it receives in its inlet, and sends the
count out its outlet. You can set minimum and maximum output values for counter, and set it to
count up, down, or up and down. In the following example, counter counts from 1 up to 5, then
starts again at 1. The right outlet reports how many times the maximum (5) has been reached.
It can be useful just to send out a succession of numbers from a counter. For example, the numbers
could be used as addresses to get values from a table. Other times, in order to make the loop useful, there needs to be a unique event when a certain condition is met. Actually, counter has its own
built-in conditions and reactions, such as “when the maximum is reached, send the number of
times it has been reached out the right outlet, send the number 1 out the right-middle outlet, and
go back to the minimum,” but sometimes we may want another condition to cause a certain result.
344