<?xml version='1.0' encoding='UTF-8'?>

<?xml-stylesheet href="./_c74_tut.xsl" type="text/xsl"?>

<chapter name="Max pattr Tutorial 2: Automatic Bindings and Storage">

<setdocpatch name="02pAutopattrBindings" patch="02pAutopattrBindings.maxpat"/>
<previous name="pattrchapter01">Pattr Basics</previous>
<next name="javascriptchapter01">Basic JavaScripting</next>
<parent name="00_maxindex">Max Tutorials</parent>

<indexinfo category="Patcher Storage" title="Autopattr Bindings">Advanced patcher storage</indexinfo>

<h1><o>pattr</o> Tutorial 2: Automatic Bindings and Storage</h1>

<h2>Introduction</h2>

<p>The Max <o>pattr</o> object can be used to help you manage data, either by binding to an object and referencing its data or by storing the data itself. The <o>pattrhub</o> object allows you to remotely query and set the data stored in <o>pattr</o> objects throughout a patcher hierarchy. In this tutorial, we’ll look at two powerful extensions to the <o>pattr</o> family: <o>autopattr</o>, which simplifies the binding of large groups of objects to the <o>pattr</o> system, and <o>pattrstorage</o>, which provides an extensive system for storing and recalling ‘states’ of data stored in multiple <o>pattr</o> 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>Automatic Binding</h2>

<p>Take a look at the tutorial patcher.  Start the patch by clicking the <o>toggle</o> box attached to the <o>metro</o> object in the upper-left corner of the patch. The patch creates an algorithmic drawing based on the parameters stored in the <o>rslider</o> object and <o>number</o> box objects attached to the <o>patcher</o> named <m>drawing</m>. The <o>message</o> box that sends a <m>bang</m> to the <o>receive</o> object named <m>reset</m> allows you to clear the <o>lcd</o> display and reset the <o>counter</o> objects in the drawing subpatch.</p>

<p>The object called <o>autopattr</o> in the lower-right corner has a number of <o>message</o> box objects connected to it that contain different example settings for the drawing controls. The <o>autopattr</o> object has much of the same functionality as <o>pattrhub</o>, allowing you to send and query values from objects bound to the <o>pattr</o> system within a patch.</p>

<p>Unlock the patch and select one of the drawing interface controls other than the <o>toggle</o> object (i.e. the <o>rslider</o> or <o>number</o> box objects at the top). Under the <b>Object</b> menu, select <b>Name…</b>.</p>

<p>Note that our four ‘control’ objects in the patch have names (<m>range</m>, <m>angle</m>, <m>offset</m>, and <m>desynch</m>). If you create a group of user-interface objects and give them names, an <o>autopattr</o> object will automatically bring those objects into the <o>pattr</o> system, exactly as if they had <o>pattr</o> objects all bound to them individually. In this sense, <o>autopattr</o> also functions as a set of <o>pattr</o> objects, allowing you to include multiple objects for patcher storage with one object.</p>

<p>The default behavior of the <o>autopattr</o> object is to bind only <i>named</i> user interface objects in a patcher when the object is created. The <m>autoname</m> attribute to <o>autopattr</o>, when set to <m>1</m>, will automatically name all user interface objects in the patcher and bind them to the <o>pattr</o> system. Like the Max preset object, the <o>autopattr</o> object will exclude any objects connected to its second inlet or, if objects are connected to its left inlet, allow you to only specify a few objects explicitly for naming and binding into the <o>pattr</o> system.</p>

<p>Because the <o>autopattr</o> object combines many of the features of the <o>pattr</o> and <o>pattrhub</o> objects, it allows you to rapidly include large numbers of Max objects with a single object, while allowing you to query and set their data through the same object.</p>

<h2>Storing States</h2>

<p>Click on the <o>message</o> box labeled <m>clientwindow</m> attached to the <o>pattrstorage</o> object. The window that appears gives you a tabulated list of all the <o>pattr</o> associated objects in the patch, as well as some information about them. Change the settings in some of the control objects for the drawing machine and watch the window update to reflect the new settings.</p>

<p>The <o>pattrstorage</o> object provides a number of features for high-level parameter management. The client window shows the <i>names</i> of all the objects bound to <o>pattr</o> in the patch (including those controlled by <o>pattr</o> objects or <o>autopattr</o> objects). It shows their current values in the last column. The middle two columns show the recall <i>priority</i> and <i>interpolation</i> mode for the objects, which can be changed to fine-tune the order in which the objects are recalled to a saved state, and how objects respond to an in-between state (see below).</p>

<p>Click on the <o>message</o> box labeled <m>storagewindow</m> attached to the <o>pattrstorage</o>. A window with a list of the control objects should appear. Create a setting you like with the four objects that control the drawing machine. Once you have a setting you’re happy with, click the <o>button</o> object labeled "store a preset" attached to the <o>int</o> object above the <o>pattrstorage</o>. This will send the message <m>store 1</m> to <o>pattrstorage</o>.</p>

<p>Sending a <m>store</m> message, followed by an integer value, tells <o>pattrstorage</o> to <i>capture</i> the current state of all bound objects and save them as a preset (or state) in a numbered slot inside the object. The slot will then receive a column in the storage window, showing us the number of the slot as a column header, with all the appropriate values tabulated below it. State <m>0</m> is a special slot that allows you to save a state temporarily without having it save in a file (see below).</p>

<p>Now that you’ve saved a preset, change one of the <o>pattr</o>-bound objects. Watch how the storage window responds.  When you make a change to an object that deviates from the most recently saved or recalled preset slot, the value for the appropriate object becomes <i>italicized</i> in the storage window.</p>

<p>Create some more preset states by making changes to the control objects, then storing their settings in <o>pattrstorage</o> by incrementing the <o>number</o> box attached to the <o>int</o> object and clicking the <o>button</o>. Note that if you re-use a slot number (e.g. you save a state under slot <m>2</m>, then save again) the old settings are overwritten.  Also note that the most current state (stored or recalled) is shown with a colored highlight, while non-current states appear unhighlighted.</p>

<p>Click on the <o>number</o> box labeled ‘recall a preset’. Set it to one of the values you’ve stored. The user interface objects should recall to their saved values, and the slot column representing that state in the storage window should go to boldface.</p>

<p>Sending an integer to <o>pattrstorage</o> will recall the slot corresponding to that integer. Attempting to recall a slot that has never been saved is harmless, and <o>pattrstorage</o> will ignore you.</p>

<h2>Interpolation</h2>

<p>Click on the floating-point <o>number</o> box labeled ‘interpolate between presets’. Type in a value between two adjacent slots you’ve saved (e.g. if you’ve saved a state under <m>1</m> and a state under <m>2</m>, type 1.5). The <o>pattr</o>-bound objects will go to an <i>interpolated</i> value between those two states. Drag with the mouse in the <o>number</o> box so that you effect a smooth transition from one state to another. The controlled objects will smoothly interpolate as well.</p>

<p>Floating-point values sent to <o>pattrstorage</o> will interpolate values between saved states. Non-adjacent states can be interpolated as well, using the message <m>fade a b n</m>, where <m>a</m> and <m>b</m> are the slot numbers and <m>n</m> is a value from <m>0.0</m> (100% <m>a</m>) to <m>1.0</m> (100% <m>b</m>). Note that interpolated values will italicize both of the participating slots in the storage window (the highlight color will fade from the <m>a</m> to the <m>b</m> slot, as well).</p>

<h2>Saving and Loading Files</h2>

<p>After you’ve saved a couple of states into preset slots, click on the <o>message</o> box labeled <m>write</m> attached to the <o>pattrstorage</o> object. A file save dialog will appear, asking you to save a file with the suffix <b>.xml</b>. Save the file somewhere and open it in any application that can read text files (Max, for example). Look over the file.</p>

<p>The <o>pattrstorage</o> object reads and writes collections of preset states as <b>XML</b> (extended markup language) files, a format which uses nested tags as identifiers for different types of data in a file.</p>

<p>An example XML file from our tutorial patch could look something like this:<br/><br/>

&lt;?xml version="1.0" encoding="iso-8859-1" standalone="yes"?&gt;<br/>
&lt;<o>pattrstorage</o> name = "u518000001"&gt;<br/>
	&lt;slot number = "1"&gt;<br/>
		&lt;<o>pattr</o> name = "angle" value = “60.” /&gt;<br/>
		&lt;<o>pattr</o> name = “desynch” value = “7” /&gt;<br/>
		&lt;<o>pattr</o> name = “offset” value = “0.” /&gt;<br/>
		&lt;<o>pattr</o> name = “range” value = “50 100" /&gt;<br/>
	&lt;/slot&gt;<br/>
	&lt;slot number = “2”&gt;<br/>
		&lt;<o>pattr</o> name = “angle” value = “33.5” /&gt;<br/>
		&lt;<o>pattr</o> name = “desynch” value = “39” /&gt;<br/>
		&lt;<o>pattr</o> name = “offset” value = “8.8” /&gt;<br/>
		&lt;<o>pattr</o> name = “range” value = “50 147" /&gt;<br/>
	&lt;/slot&gt;<br/>
&lt;/<o>pattrstorage</o>&gt;<br/>
</p>

<p>The first relevant tag that encompasses all our data defines the information as belonging the a <o>pattrstorage</o> object. The &lt;<o>pattrstorage</o>&gt; tag also tells us the name of the <o>pattrstorage</o> argument, which is randomly generated unless we set it explicitly with an argument to the object.</p>

<p>Between the &lt;<o>pattrstorage</o>&gt; tags, we have &lt;slot&gt; tags defining the saved states. Note that slot <m>0</m> is not saved in the file. Within each &lt;slot&gt;, we have &lt;<o>pattr</o>&gt; tags that list the bound objects by name and list their preset value for that state.</p>

<p>Make a change to the numerical data in one of the slots and save the XML file. Be careful to preserve the quotation marks around the values. Read it back into the patch by clicking the <m>read</m> <o>message</o> box and selecting the file. The storage window will update to the new preset value, which you can then recall.</p>

<p>Because XML is a straightforward, human-readable format, it’s quite simple to edit preset files saved using <o>pattrstorage</o>. We could even use a piece of software (e.g. Max) to automatically generate preset files for our patches once they’ve been set up to use <o>pattrstorage</o>!</p>

<h2>Summary</h2>

<p>The <o>autopattr</o> object functions as a hybrid of the <o>pattr</o> and <o>pattrhub</o> objects, allowing you to automatically bind multiple user interface objects to the <o>pattr</o> system in one easy step. The object allows you to automatically name objects for inclusion (through the autoname attribute), as well as explicitly include or exclude objects through the first two outlets, respectively. The <o>pattrstorage</o> object allows you to view the current settings of <o>pattr</o>-bound objects, as well as save states of settings into numerical slots (through the <m>store</m> message) and recall them (by sending an integer to the object). Furthermore, you can interpolate between preset slots, as well as save and recall your saved states in an easy-to-read XML format.</p>

<seealsolist>
<seealso name="autopattr">freeing <o>pattr</o> users from the burdens of thought</seealso>
<seealso name="pattrstorage">view/modify <o>pattr</o> data, store and recall presets</seealso>
</seealsolist>

</chapter>
