Tutorial 21: Controlling Data Flow
Introduction
In this tutorial, we will learn some techniques for regulating data flow within a Max patch, using the gate, switch and router to direct events. We will also examine user interface elements that can be used to manage this control, including the radiogroup and matrixctrl objects. These objects will be used to vary the drawing commands used for an interactive drawing routine.
Part of creating an interactive (or performance-based) Max patch is presenting options, and allowing for selection from among these options. The gate and switch objects control event routing from and to single data sources, while the router object performs routing among a matrix of connections. All of these routing objects are far more useful once we have a way to simply and obviously control the routing – hence, we use the radiogroup and matrixctrl objects as the “visible” switching controls for the actual control objects.
The old gate and switch
Open the tutorial.
Take a look at the patcher for this tutorial. This is a large-ish patch that implements two drawing routines. Let’s focus on the left-hand drawing system (labeled 1) first.
The metro at the top of the patch fires off bang messages that are sent to a patcher called mousedraw. The contents of this subpatch should look familiar – it scales the mouse movements of your screen geometry to the size of the lower-left lcd object (using our previously made WTHITM_scaled abstraction), then describes a dimensional list +/- 6 pixels from the calculated location. This is the sort of thing that we’ve done several times in previous tutorials. The output of this subpatcher is sent into the right inlet of a gate object, where it is sent to one of four potential prepend objects. These objects determine if a square or circle will be drawn, and if it will have a frame or a filled shape.
The purpose of the gate object is to select which of the four (if any) prepend message will receive the dimension list. The input into the left inlet of gate will route the events coming into the right inlet to the selected outlet; a setting of 0 will turn off output completely. Turn on the metro using the toggle object at the top. Using the number box connected to the left inlet, you can choose a number between 1and 4 to see the result in the lcd object’s display.
Connected to the input of the number box is a user interface object with five small circles; this is a radiogroup object, and it is used to select a single number among a discrete set of options. If you unlock the patch and check the radiogroup object’s inspector, you will see the option “Number of Items” is set to 5. This attribute determines the number of options provided. Returning to the patch and locking it, you can see that selecting one of the radiogroup options outputs the number of the selection option, starting with 0. This is a perfect way to control the output of gate, and can be used for quick selection of number of choices when doing patcher routing.
To the right of the gate-based drawing routine is a similar system based around a switch object. Stop the mouse drawing (by setting the radiogroup object to 0 - the top selection) and clear the lcd object (by hitting the space bar). Once the lcd is cleared, select one of the non-zero radiogroup options connected to the switch object. You will see a random-based automated drawing routine laying in circles on the lcd object. Option 1 uses a drawing routine based on the drunk object, while option 2 uses a routine based on the random object. If you want to stop the automated drawing, you can select the 0 option, and nothing is sent to the lcd object.
The switch object is the inverse of the gate object; where the gate object routes one input into many outlets, the switch object is used to choose the routing of many inputs into one output. In this case, both of the patcher objects are producing output messages, but only one subpatcher will have its messages sent to the prepend object. As with the gate object, the selection is controlled by the value sent to the left-hand input, and a value of 0 will prevent any messages from passing through the switch.
Using router
In some cases, you need to route many inputs into many outputs – and potentially all of them simultaneously. The drawing patch on the right hand side features a metro connected to three drawing patcher objects - the same three subpatchers from our previous patch, in fact. The are connected to the three right-hand inputs of the router object. There are five outputs from router object, the first four of which are attached to the familiar prepend objects that determine the drawing shape to be used. The number of inlets and outlets are determined by the arguments used to instantiate the router object. The key to making this patch operate is the grid-shaped user interface object connected to the leftmost inlet of router - this is the matrixctrl object, and it allows us to specify a two-dimensional "grid" of connections using a message format understood by the router object.
You will notice that the grid has three vertical lines (corresponding to the three inputs) and four horizontal lines (corresponding to the four outputs). If you click on the intersection of two lines, a small “puck” is displayed, implying that a connection has been made. So, for example, clicking on the top-left intersection “connects” the leftmost input to the leftmost output, causing the mousedraw patcher to produce framed rectangles (when the metro is turned on).
Turn on the metro with the toggle box labeled 2 and experiment with setting and unsetting different boxes in the matrixctrl object. Notice that one major difference between router and gate/switch is the ability to simultaneously route several inputs to several outputs. Select the top-center connection, and you will see the drunken painting of framed rectangles. Now, select the connection in the middle of the second row – the painted rectangle has a circle inside of it. You may need to clear the lcd (via the space bar) to see it more clearly. Try different combinations of input and output connections to see how the matrix selection affects the painting process.
If you look in the Max window, you can see how the matrixctrl passes messages to the router. The objects communicate using the system of inlet outlet state, where a state of 1 makes a connection and 0 breaks the connection. Unlike gate and switch, the input and output numbering begins at 0, not 1: a connection between the left inlet and the last outlet in our example, therefore, would be set by sending the message 0 3 1 to the router.
Conclusion
In this tutorial, we have seen a number of useful objects for routing messages within a subset of the logic in a patcher. The switch object selects from several inputs, based on the value sent to its left inlet. The gate is switch’s inverse – it will take a single input and route it to one of several outlets. If you need more complicated “matrix-like” routing, the router object can pass messages arriving at multiple inlets into any (or all) of a set of outlets.
The radiogroup object allows for a visual interface to objects like gate and switch. The matrixctrl object provides a visual interface for matrix-style routing. The combination of router and matrixctrl provides one of the most flexible (and useful) message routing systems available in Max.

See Also

Name Description
gate Pass the input out a specific outlet
switch Output messages from a specific inlet
router matrixctrl-compatible Max message router
radiogroup Radio button/check box user interface object
matrixctrl Matrix switch control