<?xml version='1.0' encoding='UTF-8'?>

<?xml-stylesheet href="./_c74_tut.xsl" type="text/xsl"?>

<chapter name="Max Basic Tutorial 18: Data Collections">

<setdocpatch name="18mDataCollections" patch="18mDataCollections.maxpat"/>
<previous name="basicchapter17">Data Structures and Probability</previous>
<next name="basicchapter19">Timing</next>
<parent name="00_maxindex">Max Tutorials</parent>

<indexinfo category="Basics" title="Data Collections">Using databases</indexinfo>

<h1>Tutorial 18: Data Collections</h1>

<h2>Introduction</h2>

<p>This tutorial covers two of the most useful objects in the Max world: <o>coll</o> and <o>route</o>.  The <o>coll</o> object allows for the indexed collection of data; you can think of it as a small and fast database for message data.  The <o>route</o> object is also tied to the concept of indexing – it will send data out of a specific outlet based on the index (first entry) of a list.</p>

<p><i>Indexed</i> data is very useful for complex routing of information.  Often data is already indexed for you: MIDI controllers normally index the value with the controller number, while note information is often indexed by the MIDI channel.  Using indexing for data can also help when sending messages throughout a complex patch – by creating an indexing system, you can send data around indiscriminately, and have the <o>route</o> object find and decode the data as necessary.</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>Basic <o>coll</o> handling</h2>

<p>There are several small- to medium-sized patches scattered throughout the interface for this tutorial.  We will be starting with the patch at the top-left side.</p>

<p>The first patch (labeled <b>1</b>) introduces the <o>coll</o> object.  Three <o>message</o> boxes are connected to it.  If you click all three, you will load the <o>coll</o> object with three value sets.  The easiest way to see what is stored in the <o>coll</o> is to double-click on it; it will open an editing window that will show you that the first number was stored as an <i>index</i>, and the second number was stored as a <i>value</i>.  To programmatically retrieve a value, you send an index value into the inlet – the output will be the value (or values) stored at that index location.  If you use the <o>number</o> box to send a <m>0</m> to the <o>coll</o> object, it will output <m>20</m> (the value at index location 0); <m>1</m> and <m>2</m> will output <m>-50</m> and <m>33</m> respectively.  This is the most basic, and most common, use of the <o>coll</o> object – as a storage mechanism for indexed value sets.</p>

<p>The patch labeled <b>2</b> is slightly different in that it uses <i>symbols</i>, rather than numbers, as the indices.  Click on the three <m>store</m> <o>message</o> boxes, and again double-click on the <o>coll</o> to see the stored data. The data is similar to the first <o>coll</o>, but the indices are symbols (words) rather than numbers. Now, if you click on the <o>message</o> boxes to the left (<m>foo</m>, <m>bar</m>, <m>biz</m>), the <o>coll</o> object will output the correct value for each of the symbols.</p>

<p>The third patch (labeled <b>3</b>) has already-stored data in it.  If you select any number between <m>0</m> and <m>3</m>, the <o>coll</o> will output the data for that index.  In this case, rather than a single number, the <o>coll</o> is storing a string of symbols (in this case, the opening soliloquy of Shakespeare's <i>Richard III</i>); all of the symbols are output as a list.  Prepending a <m>set</m> message to the list allows us to display the value string in a <o>message</o> box.</p>

<p>In order to store the data within the patcher (as we’ve done with this example), you have to set an attribute in the <o>coll</o> object's Inspector.  Unlock the patcher, and open the Inspector for the <o>coll</o> in patch 3.  You will notice that the attribute labeled “Save Data With Patcher” is selected.  When this attribute is set, any information stored in the <o>coll</o> will be written to the patcher file when it is saved, and is immediately accessible when the patch is opened again.</p>

<h2>Using <o>coll</o> for drawing and playing</h2>

<p>The center section of our patch (section <b>4</b>) uses a <o>coll</o> to control both a drawing and a MIDI playback function.  In this case, the <o>coll</o> is loaded with some pre-defined data; however, instead of having been stored in the patcher, this data is loaded from an external file containing data from an EEG readout.  The filename is “eeg.txt”, and that filename is used as the argument for the <o>coll</o>, so the data is read into the <o>coll</o> when it is first initialized.</p>

<p>At the top of the patch is a simple little self-incrementing counter system.  Each time a number is generated, it increments itself by an amount determined by the upper-right <o>number</o> box.  By default, the increment is one, since that is what the <o>+</o> object contains.  Playing through the data set at this speed, however, can take quite a while, as our data file contains many thousands of lines.  If you change the <o>number</o> box to <m>15</m> (meaning that you increment the steps by <m>15</m> at each <m>bang</m> generated by the <o>metro</o> ), you will see the EEG pattern much more clearly.  The output of this number generator is used to retrieve the indexed data from the <o>coll</o>, and is sent to both the <b>drawit</b> subpatcher and a simple MIDI player (<o>makenote</o>/<o>noteout</o>).  The MIDI player just uses the number (scaled to the <m>0-127</m> range of MIDI) as the MIDI note, while the <b>drawit</b> subpatcher uses the incoming value as the vertical offset of a scrolling drawn circle.  Start the <o>metro</o> (with the <o>toggle</o>) and notice how different increment values for the counting change the way the EEG data unfolds.  If you need to restart the file at the beginning, the <o>message</o> box labeled <m>0</m> will reset the <o>int</o> box to <m>0</m> again.</p>

<p>Storing data into a <o>coll</o> for later use is the best way to deal with large datasets. This EEG capture, which represents over 18,000 data points, would cause your patcher to be very large if it was stored in the patcher's file.  Instead, the data can be captured into a <o>coll</o>, saved in a text file (using the <m>write</m> message), and read into the <o>coll</o> at runtime (either with the file name as an argument, or using the <m>read</m> message).</p>

<p>The next patch segment, labeled <b>5</b>, shows a few more tricks that <o>coll</o> has up its sleeve. This <o>coll</o> has a name – <m>cues</m> – that manages 8 different cues handled by the rest of the patch (double-click the <o>coll</o> object to look at what's stored inside).  You can use the connected <o>number</o> box to fire off the messages, but you can also use the three <o>message</o> boxes to walk through the cues.  The <m>start</m> message moves you to the beginning of the <o>coll</o> contents, while the <m>next</m> and <m>previous</m> messages move forward and backward through the cues. This allows you to step through the <o>coll</o> in sequence, even if the entries are not numbered sequentially, and will automatically loop back to the start when the end is reached.  This is the easiest way to implement a sequential step-through of a <o>coll</o>’s contents.  Without worrying about the <o>route</o> object just yet, step through the sequence using the <m>start</m>, <m>next</m> and <m>previous</m> messages and observe how different actions are triggered by the output of the <o>coll</o> object.</p>

<p>There is also a small patch just to the right, where another <o>coll</o> is also named “cues”.  If you double-click on this <o>coll</o>, you will see that it has the same contents.  In fact, because it is <i>named</i> the same as the previously viewed <o>coll</o>, it is actually sharing the contents of the <o>coll</o> – much like the <o>value</o> object shared data through a naming convention.  Using named colls that share their information allows you to have easy access to a dataset in different places in your patch, or even within subpatchers.</p>

<h2>Message routing with <o>route</o></h2>

<p>The small patch in the lower-right shows the functionality of the <o>route</o> object: as you click on the <o>message</o> boxes, the messages are matched based on their identifier (the first item in their list); if the identifier matches one of the arguments to the <o>route</o> object, the message is stripped of its identifier and the remaining data (in this case, an integer) is sent out the outlet.  The <o>route</o> object has one outlet for each argument, as well as a final outlet for items that don't match.  This patch shows what happens if an unrecognized message is received: the <m>zeppo</m> message is not enumerated in the <o>route</o> object, and therefore cannot be routed.  Rather than ignoring the message, it sends the entire message out the right outlet – possibly to be used by another <o>route</o> object, or perhaps to be sent (as we have in this case) to the Max Console with the <o>print</o> object.</p>

<p>Patch segment <b>5</b> also uses the <o>route</o> object to take messages from the <o>coll</o> and parse them to perform actions on different objects in the patch.  As we saw inside the <o>coll</o>, the data contains a symbol (<m>m1</m>, <m>m2</m>, <m>m3</m> and <m>pick</m>) and a data item (<m>0/1</m> for <m>m1-m3</m>, and <m>bang</m> for the <m>pick</m> symbol). When this message is received by the <o>route</o> object, it uses the <i>first</i> element (the symbol) to determine the output routing, and sends the rest of the message through that outlet.  Hence, a message that begins with <m>m1</m> will be routed through the first outlet, while a message that begins with <m>pick</m> will be routed through the fourth.</p>

<p>If we use the start and next messages to walk through the cues <o>coll</o>, we will see that a sequence of actions is performed based on the first segment of the message.  The <o>metro</o> objects are started and stopped, and the <o>random</o> object is triggered twice.  You can see that the <o>route</o> object will send tagged message contents where they will be useful.</p>


<h2>Summary</h2>

<p>The <o>coll</o> object is a powerful way to work with indexed messages and data; the object provides methods to get specific messages or to step sequentially through the contents.  You can share the contents between named <o>coll</o> objects – letting you have access to this data throughout your patch; <o>coll</o> can also read its data from external files.  We also saw how the <o>route</o> object allows you to track and move messages based on the contents of the message, helping you to route the data that you need to different objects based on an index.</p>

<seealsolist>
<seealso name="coll">A collection to store data in many "bunches"</seealso>
<seealso name="route">Direct messages and numbers to different outlets</seealso>
</seealsolist>

</chapter>
