<?xml version='1.0' encoding='UTF-8'?>

<?xml-stylesheet href="./_c74_tut.xsl" type="text/xsl"?>

<chapter name="Max Data Tutorial 3: Gesture Capture">

<setdocpatch name="03dGestureCapture" patch="03dGestureCapture.maxpat"/>
<previous name="datachapter02">Data Scaling</previous>
<next name="datachapter04">Cellblock</next>
<parent name="00_maxindex">Max Tutorials</parent>

<indexinfo category="Data" title="Gesture Capture">Recording and playing back captured input</indexinfo>

<h1>Data Tutorial 3: Gesture Capture</h1>

<h2>Introduction</h2>

<p>This tutorial focuses on data capture and playback – in essence, we will be performing sequencing.  However, unlike the sequencing found in the MIDI Tutorials, this is sequencing of arbitrary data (mouse movement, in this case). We will be expanding on our use of <o>coll</o>, this time collecting as well as reproducing data for an interesting drawing patch.</p>

<p>The heart of many performance tools is the ability to capture and play back movement, whether motion capture data, sensor data or mouse movement.  This tutorial will present the basis for capture and playback systems, and will introduce a few interesting design patterns as well.</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>Run the program, view the data</h2>

<p>Take a look at the tutorial patcher.  This is a fairly complex patch, and the whole thing serves one purpose: capturing mouse movement and turning it into an interesting drawing.  The interesting thing is that while receiving mouse movements and drawing the picture, the patch is also recording the movements.  Because this patch depends on regular timing regardless of other computer activity, you may want to turn on <b>Overdrive</b> under the <b>Options</b> menu.</p>

<p>An important hint: you don’t want to interact with this patch using the mouse, since it will tend to mess up your drawing.  Rather, notice the <o>key</o> object-based section just above the <o>lcd</o> object: this maps three keys (space, “r” and “p”) to three patch functions (“clear”, “record” and “playback”).  To begin, hit the “r” key (lower-case is important!) to begin the recording process.  Now, move you mouse around the screen – looping and large movements are especially effective. When you are done, hit the “r” key again to turn off recording.</p>

<p>Now you have an interesting, script-like drawing on the <o>lcd</o> display.  Hit the space bar to clear the display, then hit the “p” key to begin playback.  You will see the drawing occur: not only was the drawing captured, but the actual mouse movement (and timing) were captured and reproduced.</p>

<p>There are three <o>coll</o> objects in this patch, but all of them refer to the same data: the data in a <o>coll</o> named <m>thegest</m>. (The <m>-1</m> argument prevents Max from searching for a file of that name when the patch is loaded.) The copy at the right is most convenient to access, so double-click on it to reveal the captured data.  The contents of the <o>coll</o> shows our movement being tracked and captured in 20 millisecond intervals.  Let’s look at how this capture happened.</p>

<h2>The recording part</h2>

<bullet>Double-click the <m>record</m> <o>patcher</o> (<m>p</m>) object to open it.</bullet>

<p>The <b>RECORD</b> section of the patch is started with the big red <o>toggle</o> object.  When it is turned on, it resets the patch (via the <link name="select" type="refpage">sel</link> <m>1 </m>object connected to the right outlet of the <o>trigger</o>), then starts a <o>metro</o> that fires every <m>20</m> milliseconds. This determines the <i>sampling rate</i> of the mouse tracking, and also serves as a trigger for the other objects.  The <o>metro</o> sends a <m>bang</m> message to two objects: a subpatcher (the scaled version of <b>WTHITM</b> that we've been using) and an <o>int</o> (abbreviated "i") object.</p>

<p>The <b>WTHITM</b> abstraction should be familiar by now – it tracks the current mouse position, and outputs a set of X and Y coordinates scaled to the range given as arguments. In this case the subpatcher will output values in the range of <m>0</m>-<m>320</m> and <m>0</m>-<m>240</m> any time it receives a <m>bang</m> message.  In order to provide immediate feedback, the output of the subpatcher is sent to a drawing subpatcher that generates drawing commands for the <o>lcd</o> object.</p>

<p>The <o>int</o> object attached to the <o>metro</o>, and the <o>+</o> object connected to it, perform a counting function.  Incoming <m>bang</m> messages will output the current value (as seen in the <o>number</o> box).  However, the output value will also go to the <o>+</o> object, which will add one to the value and store it in the right inlet of the <o>int</o> (<m>i</m>) object to wait for the next <m>bang</m> message.  The result is that every <m>bang</m> message will output a value, but also increment it by <m>1</m> – sort of like a <o>counter</o> object, but with no upper limit.  This is a useful way to get an ever-increasing number in situations where you don’t know what the limits might be.</p>

<p>The values from these two objects (the current count and the scaled X and Y positions) are assembled with a <o>pack</o> object and then sent to the <o>coll</o> object - the first value in the list (the current count) acts as an index, telling <o>coll</o> to <i>store</i> the remaining values at that position.  As a result, the <o>coll</o> acts as a data recorder, taking the indexed values and storing them for later use. The collection of data continues until the record function is turned off, either by clicking on the red checkbox, or by hitting the “r” key.</p>

<p>Take a moment to look at what the <o>select</o> <m>1</m> object connected to the recording <o>trigger</o> does.  When the recording is begun, a <m>bang</m> gets sent to a second <o>trigger</o> object that, from right to left, turns off the "play" <o>toggle</o> box (stopping any playback), sends a <m>clear</m> message to the <o>lcd</o>, erases the contents of the <o>coll</o> (with another <m>clear</m> message), and zeroes the <o>int</o> object to reset the index counter.</p>

<h2>The playback part</h2>

<bullet>Double-click the <m>play</m> <o>patcher</o> (<m>p</m>) object to open it.</bullet>

<p>The playback part of the patch mirrors much of the recording section, except it doesn’t do any mouse tracking.  Rather, it just requests indexed values from the <o>coll</o> named  <m>thegest</m>, and uses the same drawing patch to recreate the mouse drawing movements. It is able to use the same incrementing integer technique used in the record section; this has the benefit of not requiring an upper limit to be defined, but a downside of never stopping or looping on its own.</p>

<p>Since the record function didn’t <i>timestamp</i> the captured data (it just indexed it with the output of the integer object), it means that we can manipulate the playback of the drawing by altering the speed at which we request the <o>coll</o>’s content.  The easiest way to do this is to change the speed of the playback <o>metro</o>.</p>

<p>Unlock the patch and add an integer <o>number</o> box in the area of the playback section.  Connect its output to the right inlet of the <o>metro</o> (the right inlet controls the output interval). Enter 2 into the <o>number</o> box, then hit “p” for playback.  The drawing will occur as before, but will run 10 times as fast.  Stop the drawing (hit "p") and change the <o>number</o> box to 40, hit the “p” key again, and the drawing will run at half speed.  As you can see, this data capture technique gives you a lot of flexibility at playback time.</p>

<p>Once you have an interesting data capture, you will probably want to save it for later use.  The <o>coll</o> at the far-right (outlined in gol) shows the simple method for saving and retrieving <o>coll</o> contents; a <m>read</m> message will allow you to import data from disk files, while a <m>write</m> message will take your captured data and store it to disk.  You can append an actual file name and path to the message if you have a specific file to read or write, while a read/write message with no arguments will open a file dialog.</p>

<p>Make a few drawings and save them using the <m>write</m> message to the <o>coll</o> on the right.  If you read them back in using the <m>read</m> message, you will be able to play back your saved drawings.  Notice that the data files created by <o>coll</o> objects are simply text files; you can open then in any text editor or import them into another program for viewing or manipulation.</p>

<h2>Summary</h2>

<p>In this tutorial, we’ve seen how to use the <o>coll</o> object as a data collection device and we've  learned how to design a counter with no upper limit. We’ve also seen how the <o>coll</o> can be used for disk storage and retrieval, allowing us to save and reuse our captured data.  Finally, this patch is a good example of a single data set being used by three <o>coll</o> objects, a method for data sharing that becomes very useful with large patches.</p>

<seealsolist>
<seealso name="coll">Store and edit a collection of different messages</seealso>
</seealsolist>

</chapter>
