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 these controls, 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.
To open the tutorial patch, click on the green Open Tutorial button in the upper right-hand corner of the documentation window.
The old gate and switch
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 messages that are sent to a patcher called . 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 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 and to see the result in the lcd object’s display.
The input of the number box is connected to 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 . 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 . This is a perfect way to control the output of gate, and can be used for quick selection of options when doing message 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 left radiogroup object to - (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 uses a drawing routine based on the drunk object, while option uses a routine based on the random object. If you want to stop the automated drawing, you can select the 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 will prevent any messages from passing through the switch.
In some cases, you need to route many inputs into many outputs – and potentially all of them simultaneously. The drawing patch 2 on the right hand side features a metro connected to three drawing patcher objects - the same three subpatchers from patch 1, in fact. They 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.
The matrixctrl grid has three vertical lines that correspond to the three inputs of router and four horizontal lines that correspond 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 labeled 2 and experiment with setting and unsetting different connections in the matrixctrl object. Notice that one major difference between router and gate or 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 , , , where a state of makes a connection and breaks the connection. Unlike gate and switch, the input and output numbering begins at , not : a connection between the left inlet and the last outlet in our example, therefore, would be set by sending the message to the router.
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.
|gate||Pass input to an outlet|
|switch||Accept messages from a specific inlet|
|router||Route messages to multiple locations|
|radiogroup||Radio button or check box|
|matrixctrl||Matrix switch control|
|gswitch2||Send input to one of two outlets|
|gswitch||Select output from two inlets|