<?xml version='1.0' encoding='UTF-8'?>

<?xml-stylesheet href="./_c74_tut.xsl" type="text/xsl"?>
<chapter name="Tutorial 16: Using Named Jitter Matrices">
<setdocpatch name="16jNamedMatrices" patch="16jNamedMatrices.maxpat"/>

<previous name="jitterchapter15">Image Rotation</previous>
<next name="jitterchapter17">Feedback Using Named Matrices</next>
<parent name="jitindex">Jitter Tutorials</parent>



<h1>Tutorial 16: Using Named Jitter Matrices</h1>
<p>In this tutorial we'll learn how to use the <m>name</m> attribute of the <o>jit.matrix</o> object to write matrix data from multiple sources into the same matrix. We'll also look at how to scale the size of matrices when they are copied into a new matrix, and how to use the Max low-priority queue to de-prioritize Max events in favor of more time-consuming tasks .</p>


<p>The tutorial patch is divided into five colored regions. The middle (light blue) region contains two <o>jit.movie</o> objects. A <o>loadbang</o> object reads two movies (<i>rain.mov</i> and <i>traffic.mov</i>) into the <o>jit.movie</o> objects when the patch is opened:</p>

<illustration><img src="images/jitterchapter16a.png"/>Reading in the movies</illustration>
<p>Unlike the tutorial patches we've looked at before, the <o>jit.movie</o> objects in this patch use <o>send</o> and <o>receive</o> objects to communicate with the rest of the patch. The <o>receive</o> objects named <m>m1</m> and <m>m2</m> forward messages to the two <o>jit.movie</o> objects. The output matrices of the two objects are then sent (using <o>send</o> objects) to <o>receive</o> objects named <m>movie1</m> and <m>movie2</m> elsewhere in the patch.</p>

<p>The yellow region at the top of the patch contains the <o>metro</o> object that drives the Jitter processes in the patch:</p>

<illustration><img src="images/jitterchapter16b.png"/>The <o>metro</o> object drives one of the two <o>message</o> boxes</illustration>
<bullet>Click the toggle box to start the <o>metro</o>. The three <o>jit.pwindow</o> objects in the patch will start to display an image.</bullet>
<h2>Order of Importance</h2>
<p>The <o>metro</o> object goes through a <link name="gswitch2" type="refpage">Ggate</link> object and an object called <o>jit.qball</o> (about which we will have something to say later) into a <o>gate</o> object. The <m>bang</m> messages sent by the <o>metro</o> are routed by the <o>gate</o> to one of two <o>message</o> boxes. Our final output matrix (in the <o>jit.pwindow</o> at the bottom of the patch) will change depending on which <o>message</o> box gets a <m>bang</m>.</p>

<bullet>Click the two <o>message</o> boxes attached to the left inlet of the <o>gate</o> (<m>1</m> and <m>2</m>). Notice how the <o>jit.pwindow</o> at the bottom changes:</bullet>

<illustration><img src="images/jitterchapter16c.png"/>The final output matrix changes depending on the message ordering of the patch.</illustration>

<p>The two <o>message</o> boxes both send <m>bang</m> messages to the same three named receive objects (<m>m1</m>, <m>m2</m>, and <m>output</m>). The difference between the two is the order in which they send the messages. The lefthand <o>message</o> box (driven by the <o>metro</o> when the <o>gate</o> is set to 1) sends the first <m>bang</m> to the <o>jit.movie</o> object (with the rain movie). Finally, the <o>jit.matrix</o> object at the bottom of the patch receives a <m>bang</m>, causing our final matrix to be sent out. The righthand <o>message</o> box (driven by the <o>metro</o> when the <o>gate</o> is set to 2) reverses the order of the two <m>bang</m> messages driving our <o>jit.movie</o> objects (the left <o>jit.movie</o> outputs a matrix first, followed by the right <o>jit.movie</o> object).</p>

<p>The order in which these messages occur only becomes relevant when we look into what happens between the two <o>jit.movie</o> objects and the final <o>jit.pwindow</o> object.</p>
<div>
<techdetail>Important Note: If you are ever unsure about the order in which things are happening in your Max patch, you can do a <i>trace</i> of the patch to see the way in which your patch executes. If you set a break-watchpoint in a patchcord  you can step through the patch with the <b>Auto Step</b> command to see how it executes. However, this will not work well with a <o>metro</o> running, because the metro bangs will keep restarting the process. It is best to add a <o>button</o> at the  <o>metro</o> location and use it for debugging.</techdetail>
</div>

<h2>What's in a Name?</h2>
<p>Once our <o>jit.movie</o> objects receive their <m>bang</m> messages, they output a matrix to the <o>send</o> objects below them, which in turn pass their matrices over to <o>receive</o> objects named <m>movie1</m> and <m>movie2</m>. The <o>receive</o> objects (in two identical regions at the right of the patch) are connected to <o>jit.pwindow</o> objects as well as two <i>named</i> <o>jit.matrix</o> objects:</p>

<illustration><img src="images/jitterchapter16d.png"/>The named <o>jit.matrix</o> object</illustration>

<p>Both of the <o>jit.matrix</o> objects at the right of the patch (as well as the <o>jit.matrix</o> object at the bottom of the patch above our final <o>jit.pwindow</o>) have a <m>name</m>. The <m>name</m> attached to all three of these objects is <m>composite</m>. The result of this is that all three of these <o>jit.matrix</o> objects share the <i>same</i> matrix data, which is contained in a Jitter matrix called <m>composite</m>.</p>

<p>Once we know that our two <o>jit.movie</o> objects write data into the same Jitter matrix (via two separate <o>jit.matrix</o> objects sharing the same <m>name</m>) we can understand why the ordering of the <m>bang</m> messages is important. If the left <o>jit.movie</o> sends out its matrix first, it writes data into the <m>composite</m> matrix, followed by the right <o>jit.movie</o>, which writes data into the same matrix. If the two matrices write to any cells in common (see below), the matrix that gets there last will <i>overwrite</i> any data that was in those cells before.</p>


<h2>The Destination Dimension</h2>
<p>The two <o>jit.matrix</o> objects on the right of the tutorial patch have their <m>usedstdim</m> attribute set to <m>1</m>. This allows us to scale the matrices sent by our <o>jit.movie</o> objects so that they only write to a certain region of the <m>composite</m> Jitter matrix.</p>

<bullet>Play around with the <o>number</o> boxes labelled <m>x origin</m>, <m>y origin</m>, and <m>scale</m> connected to the two subpatchers labelled <link name="patcher" type="refpage">p</link> <m>coords</m>. Notice how you can move and resize the two images from the <o>jit.movie</o> objects in the <m>composite</m> matrix.</bullet>
<illustration><img src="images/jitterchapter16e.png"/>Picture within a picture</illustration>

<p>The subpatches <link name="patcher" type="refpage">p</link> <m>coords</m> contain identical helper patches to format the <m>dstdimstart</m> and <m>dstdimend</m> attributes for the <o>jit.matrix</o> objects. These attributes specify the upper left and lower right coordinates, respectively, to use when copying data into our <m>composite</m> Jitter matrix. The <m>usedstdim</m> attribute simply tells the <o>jit.matrix</o> object to use those attributes when copying data. When <m>usedstdim</m> is set to <m>0</m>, the incoming matrix is scaled to fill the entire matrix referred to by the <o>jit.matrix</o> object.</p>

<illustration><img src="images/jitterchapter16f.png"/>Scaling the input matrices before the are written into our shared matrix</illustration>

<p>The three numbers that we send into the subpatches get formatted by the Max objects inside to generate a list that describes the upper left and lower right areas of the output matrix which we want to fill with our input matrix. The <o>message</o> box before the outlet uses <m>$</m> substitution to replace the relevant arguments for the attributes with numbers from the list.</p>

<p>The last thing that happens after our two matrices have been written into the <m>composite</m> matrix is that a <m>bang</m> is sent to the <o>receive</o> object named <m>output</m>:</p>

<illustration><img src="images/jitterchapter16g.png"/>The final result</illustration>

<p>The region at the bottom of the tutorial patch contains a third named <o>jit.matrix</o> object. The <m>bang</m> sent by the <o>metro</o> goes through a <o>trigger</o> object that sends a <m>bang</m> to the <o>jit.matrix</o> (causing it to output its matrix to the <o>jit.pwindow</o>) followed immediately by a <m>clear</m> message. The <m>clear</m> message erases (zeroes) all the cells in the Jitter matrix named <m>composite</m>. If we didn't clear the matrix, changing the <m>dstdimstart</m> and <m>dstdimend</m> attributes of any of the <o>jit.matrix</o> objects could result in cells left over from a previous output location of our movies.</p>

<h2>Jumping the Queue</h2>
<p>The <o>jit.qball</o> object at the top of the patch provides an invaluable service in the event that Max can't keep up with our demands. The <o>metro</o> object (which is sending out <m>bang</m> messages every 50 milliseconds) is driving three separate operations (writing the two matrices from the <o>jit.movie</o> objects into our named Jitter matrix, as well as displaying the data and clearing the matrix so we can start over). The <o>jit.matrix</o> object writes data into its internal Jitter matrix (in this case our named <m>composite</m> matrix) in a way that allows it to be <i>usurped</i> by a subsequent message. It also allows other Max events that are scheduled at a higher priority to happen while it works on a task. This makes it possible to display the matrix (or write more data into it) before the previous operation has finished, causing flicker and other unexpected results. The <o>jit.qball</o> object places messages input into the object at the back of Max's low priority queue where they too can be <i>usurped</i> by another message. This way, if <o>jit.qball</o> gets a <m>bang</m> from the <o>metro</o> object before all the current Jitter tasks are complete, it will wait until everything else in the low priority queue is finished before sending out the <m>bang</m>. Similarly, if another <m>bang</m> comes along before that first <m>bang</m> has been sent (i.e. if it takes more than 50 milliseconds for the rest of the patch to do everything), the first <m>bang</m> will be usurped (jettisoned) in favor of the second. This allows you to set a maximum hypothetical rate of events in a Max patch without having to worry about events accumulating too rapidly for the objects in the patch to keep up.</p>

<bullet>Click on the <link name="gswitch2" type="refpage">Ggate</link> object labeled <m>jit.qball bypass switch</m> so that the output of the <o>metro</o> object bypasses the <o>jit.qball</o> object. The composite image in the <o>jit.pwindow</o> at the bottom will start to flicker, indicating that messages are arriving out of order.</bullet>
<p>Normally, sending a <m>bang</m> to a Jitter object will usurp an already pending event in that object (e.g. a <m>bang</m> that has already arrived but hasn't been dealt with yet by the object). However, the <o>jit.qball</o> object gives us this kind of control over multiple chains of Jitter objects, automatically usurping events ("dropframing") to guarantee that messages arrive in the right order.</p>

<techdetail>
	<m>Note:</m> <o>jit.qball</o> has been supplanted by <o>qmetro</o> in most applications. <o>Qmetro</o> is a <o>metro</o> object with the <o>jit.qball</o> mechanism built in.
</techdetail>

<h2>Summary</h2>
<p>By giving a single name to multiple <o>jit.matrix</o> objects, you can write into and read from a common Jitter matrix in different parts of your patch. You can scale a Jitter matrix while copying it into the internal matrix of a <o>jit.matrix</o> object by using the <m>dstdimstart</m> and <m>dstdimend</m> attributes and by setting the <m>usedstdim</m> attribute to <m>1</m>. The <o>jit.qball</o> object allows you to de-prioritize Max events by placing them in the low-priority queue where they can be usurped by subsequent events if there isn't enough time for them to execute.<i/></p>

	<seealsolist>
		<seealso name="jit.matrix">The Jitter Matrix!</seealso>
		<seealso name="jit.pwindow">In-Patcher Window</seealso>
		<seealso name="jit.qball">Convert messages at scheduler time to low priority</seealso>
		<seealso name="jit.movie">Play or edit a movie</seealso>
	</seealsolist>
</chapter>
