<?xml version='1.0' encoding='UTF-8'?>

<?xml-stylesheet href="./_c74_tut.xsl" type="text/xsl"?>

<chapter name="Max Basic Tutorial 21: Controlling Data Flow">

<setdocpatch name="21mControllingDataFlow" patch="21mControllingDataFlow.maxpat"/>
<previous name="basicchapter20">Presentation Mode</previous>
<next name="basicchapter22">Designing Equations</next>
<parent name="00_maxindex">Max Tutorials</parent>

<indexinfo category="Basics" title="Controlling Data Flow">Routing messages</indexinfo>

<h1>Tutorial 21: Controlling Data Flow</h1>

<h2>Introduction</h2>

<p>In this tutorial, we will learn some techniques for regulating data flow within a Max patch, using the <o>gate</o>, <o>switch</o> and <o>router</o> to direct events.  We will also examine user interface elements that can be used to manage these controls, including the <o>radiogroup</o> and <o>matrixctrl</o> objects.  These objects will be used to vary the drawing commands used for an interactive drawing routine.</p>

<p>Part of creating an interactive (or performance-based) Max patch is presenting options, and allowing for selection from among these options. The <o>gate</o> and <o>switch</o> objects control event routing from and to single data sources, while the <o>router</o> 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 <o>radiogroup</o> and <o>matrixctrl</o> objects as the “visible” switching controls for the actual control objects.</p>

<p>To open the tutorial patch, click on the <b>Open Tutorial</b> button in the upper right-hand corner of the documentation window.</p>

<h2>The old <o>gate</o> and <o>switch</o></h2>

<p>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 <b>1</b>) first.</p>

<p>The <o>metro</o> at the top of the patch fires off <m>bang</m> messages that are sent to a <o>patcher</o> called <m>mousedraw</m>.  The contents of this subpatch should look familiar – it scales the mouse movements of your screen geometry to the size of the lower-left <o>lcd</o> object (using our previously made <b>WTHITM_scaled</b> 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 <o>gate</o> object, where it is sent to one of four potential <o>prepend</o> objects.  These objects determine if a square or circle will be drawn, and if it will have a frame or a filled shape.</p>

<p>The purpose of the <o>gate</o> object is to select which of the four (if any) <o>prepend</o> message will receive the dimension list. The input into the <i>left</i> inlet of <o>gate</o> will route the events coming into the <i>right</i> inlet to the selected outlet; a setting of <m>0</m> will turn off output completely. Turn on the <o>metro</o> using the <o>toggle</o> object at the top.  Using the <o>number</o> box connected to the left inlet, you can choose a number between <m>1</m>and <m>4</m> to see the result in the <o>lcd</o> object’s display.</p>

<p>The input of the <o>number</o> box is connected to a user interface object with five small circles; this is a <o>radiogroup</o> object, and it is used to select a single number among a discrete set of options.  If you unlock the patch and check the <o>radiogroup</o> object’s inspector, you will see the option <i>Number of Items</i> is set to <m>5</m>.  This attribute determines the number of options provided.  Returning to the patch and locking it, you can see that selecting one of the <o>radiogroup</o> options outputs the number of the selection option, starting with <m>0</m>.  This is a perfect way to control the output of <o>gate</o>, and can be used for quick selection of options when doing message routing.</p>

<p>To the right of the <o>gate</o>-based drawing routine is a similar system based around a <o>switch</o> object.  Stop the mouse drawing by setting the left  <o>radiogroup</o> object to <m>0</m> - (the top selection) and clear the <o>lcd</o> object by hitting the space bar. Once the <o>lcd</o> is cleared, select one of the non-zero <o>radiogroup</o> options connected to the <o>switch</o> object. You will see a random-based automated drawing routine laying in circles on the <o>lcd</o> object. Option <m>1</m> uses a drawing routine based on the <o>drunk</o> object, while option <m>2</m> uses a routine based on the <o>random</o> object.  If you want to stop the automated drawing, you can select the <m>0</m> option, and nothing is sent to the <o>lcd</o> object.</p>

<p>The <o>switch</o> object is the inverse of the <o>gate</o> object; where the <o>gate</o> object routes one input into many outlets, the <o>switch</o> object is used to choose the routing of many inputs into one output. In this case, both of the <o>patcher</o> objects are producing output messages, but only one subpatcher will have its messages sent to the <o>prepend</o> object.  As with the <o>gate</o> object, the selection is controlled by the value sent to the left-hand input, and a value of <m>0</m> will prevent any messages from passing through the <o>switch</o>.</p>

<h2>Using <o>router</o></h2>

<p>In some cases, you need to route many inputs into many outputs – and potentially all of them simultaneously. The drawing patch <b>2</b> on the right hand side features a <o>metro</o> connected to three drawing <o>patcher</o> objects - the same three subpatchers from patch <b>1</b>, in fact.  They  are connected to the three right-hand inputs of the <o>router</o> object. There are five outputs from <o>router</o> object, the first four of which are attached to the familiar <o>prepend</o> objects that determine the drawing shape to be used. The number of inlets and outlets are determined by the <i>arguments</i> used to instantiate the <o>router</o> object. The key to making this patch operate is the grid-shaped user interface object connected to the leftmost inlet of <o>router</o> - this is the <o>matrixctrl</o> object, and it allows us to specify a two-dimensional "grid" of connections using a message format understood by the <o>router</o> object.</p>

<p>The <o>matrixctrl</o> grid has three <i>vertical</i> lines that correspond to the three <i>inputs</i> of <o>router</o> and four <i>horizontal</i> lines that correspond to the four <i>outputs</i>. 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 <b>mousedraw</b> <o>patcher</o> to produce framed rectangles when the <o>metro</o> is turned on.</p>

<p>Turn on the <o>metro</o> with the <o>toggle</o> labeled <b>2</b> and experiment with setting and unsetting different connections in the <o>matrixctrl</o> object.  Notice that one major difference between <o>router</o> and <o>gate</o> or <o>switch</o> is the ability to <i>simultaneously</i> 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 <o>lcd</o> (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.</p>

<p>If you look in the <b>Max</b> window, you can see how the <o>matrixctrl</o> passes messages to the <o>router</o>.  The objects communicate using the system of <m>inlet</m>, <m>outlet</m>, <m>state</m>, where a state of <m>1</m> makes a connection and <m>0</m> breaks the connection.  Unlike <o>gate</o> and <o>switch</o>, the input and output numbering begins at <m>0</m>, not <m>1</m>: a connection between the left inlet and the last outlet in our example, therefore, would be set by sending the message <m>0 3 1</m> to the <o>router</o>.</p>

<h2>Summary</h2>

<p>In this tutorial, we have seen a number of useful objects for routing messages within a subset of the logic in a patcher.  The <o>switch</o> object selects from several inputs, based on the value sent to its left inlet. The <o>gate</o> is <o>switch</o>’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 <o>router</o> object can pass messages arriving at multiple inlets into any (or all) of a set of outlets.</p>

<p>The <o>radiogroup</o> object allows for a visual interface to objects like <o>gate</o> and <o>switch</o>.  The <o>matrixctrl</o> object provides a visual interface for matrix-style routing. The combination of <o>router</o> and <o>matrixctrl</o> provides one of the most flexible (and useful) message routing systems available in Max.</p>

<seealsolist>
<seealso name="gate">Traffic control</seealso>
<seealso name="switch">Select among alternative inputs</seealso>
<seealso name="router"><o>matrixctrl</o>-compatible Max message router</seealso>
<seealso name="radiogroup">A group of radio buttons or check boxes for dialog selection</seealso>
<seealso name="matrixctrl">Switching matrix control</seealso>
<seealso name="gswitch2">UI version of gate</seealso>
<seealso name="gswitch">UI version of switch</seealso>
</seealsolist>

</chapter>
