<?xml version='1.0' encoding='UTF-8'?>

<?xml-stylesheet href="./_c74_tut.xsl" type="text/xsl"?>

<chapter name="Max pattr Tutorial 1: Patcher Storage">

<setdocpatch name="01pPattrBasics" patch="01pPattrBasics.maxpat"/>
<previous name="interfacechapter02">Picture UI Elements</previous>
<next name="pattrchapter02">Autopattr Bindings</next>
<parent name="00_maxindex">Max Tutorials</parent>

<indexinfo category="Patcher Storage" title="Pattr Basics">Introduction to state management</indexinfo>

<h1><o>pattr</o> Tutorial 1: Patcher Storage</h1>

<h2>Introduction</h2>

<p>Max has a wide variety of objects for storing and managing data. Objects such as <o>table</o>, <o>funbuff</o>, <o>coll</o>, and <o>umenu</o> all store information that you can set manually or via a mechanism within a patch itself. The <o>preset</o> object, which also stores data, is useful for capturing and recalling the <i>states</i> of user-interface objects within a patcher, giving you the ability to rapidly restore settings to multiple parts of your patch in one step.</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>What is <o>pattr</o>?</h2>

<p>Often a project in Max will require a more sophisticated state management system than the basic data structures objects allow for. The <o>pattr</o> family of objects (<o>pattr</o>, <o>pattrhub</o>, <o>autopattr</o>, and <o>pattrstorage</o>) provides the functionality of the <o>preset</o> object with a more extensive set of state management features:<br/>

<ul>
<li>We can maintain sets of data from objects throughout a Max patch hierarchy. For example, with <o>pattr</o> you can control the state of objects inside of <o>patcher</o> and <o>bpatcher</o> objects all from the top level of the patch.</li>
<li>We can store any type of data used by any type of object (including data simply stored inside a <o>pattr</o> itself, without any other objects involved).</li>
<li>We can use <o>pattr</o> objects to remotely set and query the state of objects controlled by the <o>pattr</o> system from anywhere within the patch.</li>
<li>The <o>pattr</o> objects store groups of settings as XML files, allowing us to easily read and edit saved data outside of Max.</li>
<li>The <o>pattr</o> objects can recall the state of objects in a specific order, avoiding difficulties with, for example, the recalling of a <o>toggle</o> object that starts a process before all the variables are in place for the process to function correctly.</li>
<li>Not only can we store the states of many objects under a single address, we can also interpolate between these states, allowing for a seamless crossfade between multiple settings.</li>
<li>The <o>pattr</o> objects feature a high-level interface for viewing and managing the current state of controlled objects and the states that have been saved.</li>
</ul>
</p>

<p>In this tutorial, we’ll look at some of the basic features of <o>pattr</o> and <o>pattrhub</o>, showing how the objects can be used to communicate remotely with objects anywhere in a Max patch.</p>

<h2>Object Binding</h2>

<p>Take a look at the tutorial patcher. Turn on the <o>metro</o> object with the <o>toggle</o> object box labeled "start". You should hear some music if you double-click the <o>noteout</o> object and select a valid output synthesizer. The number box attached to the <o>metro</o> object controls the speed of the generated pattern. Click on the colored <m>message</m> boxes to the right of the patch and note that they change the value of that <o>number</o> box even though they are not connected directly to it.</p>

<p>Clicking in a colored <o>message</o> box changes the <o>metro</o> object’s time value.</p>

<p>The <o>pattr</o> object to the right of the <o>metro</o> object is controlling the value in the <o>number</o> box above it. In the language of patcher storage, we say that our <o>pattr</o> object is <i>bound</i> to the <o>number</o> box. When we send messages to that <o>pattr</o> object, it will at all times refer to the data stored in the <o>number</o> box, allowing us to ask the <o>pattr</o> object what the number is currently set to, as well as tell the <o>pattr</o> object to set the number to a specific value remotely.</p>

<p>Our <o>pattr</o> object has a <i>name</i> - <m>speed</m> - which is set by the object’s argument and allows other <o>pattr</o>-family objects to communicate with it. The <o>pattrhub</o> object at the bottom of the patch can receive messages and forward them to any <o>pattr</o> objects in the patch. By sending the message <m>speed 125</m> to the <o>pattrhub</o>, we are telling the object to find the <o>pattr</o> with the name <m>speed</m>. If it finds the relevant <o>pattr</o> object, it will instruct the object to send the message <m>125</m> to whatever object it is bound to (in this case, our <o>number</o> box).</p>

<p>Unlock the patch and click on the <o>number</o> box attached to the <o>metro</o> object. Select <b>Name…</b>  from the <b>Object</b> menu. The object inspector will open showing the <b>Scripting Name</b> of the <o>number</o> box is <m>temponum</m>.</p>

<p>Our <o>pattr</o> object is bound to our <o>number</o> box because the <m>bindto</m> attribute of our <o>pattr</o> object is set to the name of our <o>number</o> box (<m>temponum</m>). The act of <i>naming</i> Max objects allows us easier access to them via the <o>pattr</o> system. Note that the <o>pattrhub</o> object controls our <o>number</o> box via the name of the <o>pattr</o> object to which it is bound (<m>speed</m>), not the name of the <o>number</o> box itself (<m>temponum</m>).</p>

<p>Rename the <o>number</o> box to something else and close the Inspector. Try changing settings for the patch by clicking on the message boxes again.</p>

<p>Note that if we rename our referred object, our <o>pattr</o> object can no longer bind to it. In order to rebind it, we need to tell <o>pattr</o> to bind to our <o>number</o> box again.</p>

<p>Open the Inspector for the <o>number</o> box and name it back to <m>temponum</m>. Close the inspector and recreate the <o>pattr</o> object binding to <m>temponum</m> (either delete the object and undo the deletion, or create a new <o>pattr</o> object). Verify that it is receiving updates from the <o>pattrhub</o> object by clicking on the <o>message</o> boxes again.</p>

<p>Instead of recreating the <o>pattr</o> object, we could have sent our newly ‘unbound’ <o>pattr</o> object a <m>bindto</m> message with the name of the <o>number</o> box, setting the <m>bindto</m> attribute to a valid object.</p>

<h2>More Ways Than One</h2>

<p>Naming a Max object and explicitly binding a <o>pattr</o> to it with its <m>bindto</m> attribute is only one way to get a <o>pattr</o> to refer to an object in our patch. It has the advantage of requiring no connecting patchcords between the two objects, allowing them to communicate from remote parts of the patcher.</p>

<p>Open the subpatch <m>notes</m> by double-clicking the <o>patcher</o> object containing it. This subpatch contains eight <o>pattr</o> object-bound <o>number</o> box objects that define the four pitches in our pattern generator, as well as the number of beats between each trigger of the pitch. This is the part of the patch that allows us to construct a polyrhythmic pattern off of a single <o>counter</o> object (driven by the <o>metro</o> object) in the main patcher. </p>

<p>Click on the <o>message</o> boxes back in the main patcher with this window open and notice that the <o>number</o> box objects in the subpatch update correctly when you click on the different patterns.  Clicking on the <o>message</o> box updates the subpatch values, too!</p>

<p>In addition to binding objects to <o>pattr</o> through explicit naming and the bindto attribute, <o>pattr</o> objects can control other Max objects through patchcord connections. </p>

<p>The notes subpatch shows two possible ways to do this:<br/><br/>
1.	Using the <o>pattr</o> object itself to store the data (vals)<br/>
2.	Automatically <i>binding</i> a <o>pattr</o> object to an object using the <o>pattr</o> object’s middle outlet (mods).<br/></p>

<h2>Internal Storage</h2>

<p>If a <o>pattr</o> object is unbound, it will store any messages sent in its left inlet and recall them from its left outlet. Messages sent to an unbound <o>pattr</o> object from a <o>pattrhub</o> object will echo out of the left outlet as well.</p>

<p>Unlock the notes subpatch and attach a <o>slider</o> object to the left outlet of one of the <o>pattr</o> objects named <m>val_a</m>, <m>val_b</m>, <m>val_c</m>, or <m>val_d</m>. Recall the <o>pattr</o> objects’ states by clicking on the <o>message</o> boxes in the main patch. Scroll the <o>number</o> box attached to the <o>pattr</o> object. The <o>slider</o> object should move as well.</p>

<p>The <o>pattr</o> objects controlling our pitches are not bound to any single object, and are maintaining the values of the <o>number</o> box objects attached to them not by referring to the objects directly, but by receiving their values as messages. As a result, the <o>number</o> box objects attached to these <o>pattr</o> objects can be deleted and replaced with other objects (or more than one object). Because the <o>pattr</o> object outputs its values when changed (either directly or remotely) we can easily attach multiple objects to the <o>pattr</o> object to set and display values stored in the object.</p>

<p>The use of the <o>prepend</o> object with ‘set’ argument in <o>pattr</o> connections is to prevent the triggering of MIDI events when we recall the <o>pattr</o> object’s state. Unlike most Max objects, <o>pattr</o> objects can be connected to other objects that are directly connected to them in turn without causing a stack overflow.</p>

<h2>Automatic Binding</h2>

<p>In the same subpatch, try to connect a <o>slider</o> object to the middle outlet of one of the <o>pattr</o> objects labeled <m>mod_a</m>, <m>mod_b</m>, <m>mod_c</m>, or <m>mod_d</m>. The Max Console should print an error message and the program should prevent you from making the connection.</p>

<p>The middle outlet of <o>pattr</o> allows for the automatic binding of the <o>pattr</o> to a single object in the same patcher. The <o>number</o> box objects controlling the <o>%</o> objects are communicating with their respective <o>pattr</o> objects just as if we had named them and bound the <o>pattr</o> objects to them manually.</p>

<p>Look at the names for the <o>number</o> box objects by selecting <b>Name…</b> from the <b>Object</b> menu.</p>

<p>The <m>bindto</m> (middle) outlet of <o>pattr</o> objects automatically gives the referring object a name if it doesn’t have one already. The name is based on the class of object attached, and multiple objects of the same class in a patch will be given incrementing notation to uniquely identify them (e.g. <m>number</m>, <m>number[1]</m>, <m>number[2]</m>, etc.).</p>

<p>Back in the main patch, open the <m>durs</m> subpatch of the main patch by double-clicking the <o>patcher</o> object containing it. Observe that the two <o>pattr</o> objects in there (<m>min</m> and <m>max</m>) have been bound to two <o>number</o> box objects through their middle outlets.</p>

<h2>One Hub to Rule Them All</h2>

<p>Close the subpatchers and go back to the main tutorial patch. Look at the <o>message</o> boxes again and notice the notation used in them to refer to <o>pattr</o> objects inside subpatchers.</p>

<p>Because most of our <o>pattr</o> objects are inside subpatchers, the <o>pattrhub</o> object refers to them using a <i>colon-separated</i> notation that takes the form<br/><br/>

<m>subpatcher:pattr_name</m><br/>
</p>

<p>We can communicate to <o>pattr</o> objects in nested subpatchers by the same convention, e.g. a <o>pattr</o> named <m>yikes</m> in a patcher called <m>stuff</m> in a bpatcher called <m>things</m> would be accessed through <o>pattrhub</o> with the name:<br/><br/>

<m>things:stuff:yikes</m><br/>
</p>

<p>Using the <o>umenu</o> object in the main tutorial patch, select one of the <o>pattr</o> objects in the patch.</p>

<p>In addition to setting the state of <o>pattr</o> objects through <o>pattrhub</o>, we can also <i>query</i> the state of <o>pattr</o> objects, which will then tell <o>pattrhub</o> their internal state or the value of the object to which they’re bound. We do this by sending <o>pattrhub</o> the message <m>get</m> followed by the name of the <o>pattr</o> object we want to query, with no space in the message, e.g. <m>getspeed</m> will poll the state of the <o>pattr</o> named <m>speed</m>.</p>

<p>The <o>number</o> box to the right of the <o>umenu</o> reflects the current value of the selected <o>pattr</o>, output by the second outlet of <o>pattrhub</o> as a message beginning with the name of the queried <o>pattr</o>.</p>

<p>Change the value of the selected <o>pattr</o> by changing the <o>number</o> box next to the <o>umenu</o>. Check to see that the value changed by looking at the <o>number</o> box controlled by that <o>pattr</o>. Select some other <o>pattr</o> objects and change their values, familiarizing yourself with how the system allows you to query and set the states of objects anywhere in the patch.</p>

<h2>Summary</h2>

<p>The <o>pattr</o> objects provide a powerful way to manage data from multiple objects in a Max patch. The <o>pattr</o> object can maintain its own internal information (sent and returned through its leftmost inlet and outlet), or it can bind to a Max user interface object either by naming that object and using the <o>pattr</o> object’s bindto attribute, or by using the middle outlet of the <o>pattr</o> object to automatically name and bind to an object. Each <o>pattr</o> object can only bind to one object at a time. The <o>pattrhub</o> object allows you to control the state of all <o>pattr</o> objects within a patch, including those within subpatchers, which can be accessed through a hierarchical notation system of successive patcher names separated by colons (<m>:</m>) followed by the name of the appropriate <o>pattr</o> object. By sending a <m>get</m> message (followed by the name of a <o>pattr</o> with no intervening space) you can query the state of any <o>pattr</o> object within a patch from a <o>pattrhub</o>.</p>

<seealsolist>
<seealso name="pattr">patcher-specific named data wrapper</seealso>
<seealso name="pattrhub">make your <o>pattr</o> objects work for you!</seealso>
<seealso name="patcher">Create a subpatch within a patch</seealso>
<seealso name="preset">Store and recall the settings of other objects</seealso>
</seealsolist>

</chapter>
