<?xml version='1.0' encoding='UTF-8'?>

<?xml-stylesheet href="./_c74_tut.xsl" type="text/xsl"?>
<chapter name="Tutorial 9: More Mixing">
<setdocpatch name="09jMoreMixing" patch="09jMoreMixing.maxpat"/>

<previous name="jitterchapter08">Simple Mixing</previous>
<next name="jitterchapter10">Chromakeying</next>
<parent name="jitindex">Jitter Tutorials</parent>



<h1>Tutorial 9: More Mixing</h1>
<p>
	This tutorial explores some crossfade techniques that can be acheived with <o>jit.scalebias</o> and <o>jit.op</o>. Although this is a more complex method than the simple <o>jit.xfade</o> approach, it can provide more flexibility. 
</p>


<h2>Mixing and Crossfading Made Explicit</h2>
<p>In the previous chapter we explained how the <o>jit.xfade</o> object uses scaling (multiplication) and addition to mix two matrices together in varying proportions. In this tutorial we'll use the <o>jit.scalebias</o> and <o>jit.op</o> objects to perform those mathematical operations ourselves.</p>

<p>This will provide us with a few advantages. First, it will allow us to demonstrate the mathematics of mixing and crossfading explicitly. Second, it will allow us to demonstrate how <o>jit.op</o> can perform math operations using two matrices as its inputs. (In <i>Tutorial 3</i> we showed <o>jit.op</o> in action with scalar values operating on a single matrix.) Third, it will allow us to specify balance settings (scaling factors) for the two matrices independently, providing more flexibility than the <o>jit.xfade</o> object. Finally, because <o>jit.op</o> can implement so many different kinds of math operations, we can try other means of combining matrices to see different visual effects.</p>

<h3>Mixing Revisited</h3>
<bullet>Open the tutorial patch.</bullet>
<illustration><img src="images/jitterchapter09a.png"/>Multiplication and addition to mix /crossfade matrices (replicating <o>jit.xfade</o>)</illustration>
<p>Here you see each of two different videos being scaled down (darkened) by some factor between 0 and 1 with <o>jit.scalebias</o>. Below that, you see a slightly new wrinkle in the use of <o>jit.op</o>: the input to <i>both</i> inlets is a matrix. When we do this, <o>jit.op</o> performs the specified math operation on every single value individually, pairing each value in the left matrix with the corresponding value in the right matrix. This lets us add all the values in the two matrices, effectively mixing the images. </p>

<p>The result of these multiplications and this addition is comparable to what the <o>jit.xfade</o> object performs internally. You can verify this by using the controls in the top right part of the patch&#x2014;which are nearly identical to those of the previous chapter&#x2014;to crossfade the videos.</p>

<bullet>Start the <o>metro</o> and use the <i>Mixer</i> slider to perform a crossfade from video A to video B.</bullet>
<p>Note that we send the crossfade value directly as the <m>scale</m> attribute for the B video, and at the same time we use a <o>!-</o><m> 1</m> object to scale the A video by 1 minus that value. That way, the sum of the two scaling factors always equals 1, as it does in <o>jit.xfade</o>.</p>

<h2>Combine Matrices Using Other Operators</h2>
<p>Addition is perhaps the most obvious operation to perform with two matrices, but it's not the only one possible. By changing the <m>op</m> attribute of the <o>jit.op</o> object, we can try out many other operations to see what kind of visual effect they create.</p>

<bullet>Set a very gradual crossfade time in the <i>Transition Time</i> <o>number</o> box (say, <m>10000</m> ms). Choose an operator other than <m>+</m> in the <i>Operator</i> pop-up menu. Now click on the <i>Go To</i> switch to begin the crossfade. You can see how that particular operator looks when implemented with two video matrices.</bullet>
<p>The pop-up menu contains a few of the many operators provided by <o>jit.op</o>. Here's a brief description of each operator in the menu.</p>
<ul>
<li><m>+</m> Add the values of B to A.</li>
<li><m>-m</m> Subtract the values of  B from A, then perform a modulo operation to wrap the result back into the desired range.</li>
<li><m>max</m> Use whichever value is greater, A or B.</li>
<li><m>absdiff</m> Subtract the values of  B from A, then use the absolute value of that difference.</li>
<li><m>|</m> "Bitwise Or"; using binary number values, whenever a bit is 1 in either A or B, set it to 1 in the result.</li>
<li><m>^</m> "Bitwise Exclusive Or"; using binary number values, whenever the bits of A and B are not the same, set that bit to 1 in the result, otherwise set the bit to 0.</li>
<li><m>&gt;</m> If the value in A is greater than the value in B, set the result to 1 (or <i>char</i> 255), otherwise set it to 0.</li>
<li><m>&lt;</m> If the value in A is less than the value in B, set the result to 1 (or <i>char</i> 255), otherwise set it to 0.</li>
<li><m>&gt;p</m> If the value in A is greater than the value in B, use the A value in the result, otherwise set it to 0.</li>
<li><m>&lt;p</m> If the value in A is less than the value in B, use the A value in the result, otherwise set it to 0.</li>
</ul>

<p>If you want to see what other operators are available, check the <i>Object Reference</i> documentation for <o>jit.op</o>.</p>

<bullet>If you'd like, you can drag directly on the <o>number</o> boxes above the <o>jit.scalebias</o> objects, to set the balance levels independently (i.e. differently from the way our crossfade scheme sets them). You can also try values that exceed the 0 to 1 range.</bullet>
<h2>jit.scalebias vs. jit.op @op *</h2>
<p>We chose to use the <o>jit.scalebias</o> object in this patch to perform the scaling multiplications instead of using <o>jit.op</o> with the <m>*</m> operator. Why? When <o>jit.op</o> is performing operations on <i>char</i> data (as we are doing in this patch), it limits its <m>val</m> attribute to the range 0.-1. (when specified as a float) or 0-255 (when specified as an int). In cases where we want to multiply <i>char</i> data by some amount from 0. to 1., <o>jit.op</o> is just fine. But if we want to multiply <i>char</i> data by some other amount, then <o>jit.scalebias</o> is the correct object to use because it permits <m>scale</m> factors that exceed the 0 to 1 range. <o>jit.scalebias</o> is only for handling 4-plane <i>char</i> matrices, but that's OK because that's what we're scaling in this example. So, in this patch, since we're operating on 4-plane <i>char</i> matrices, and since we want you to have the ability to try scaling factors that exceed the 0 to 1 range, we have used <o>jit.scalebias</o>.</p>

<h2>Summary</h2>
<p>You can use the <o>jit.op</o> object to perform various math operations using the the values from two different matrices. <o>jit.op</o> performs the specified math operation on every value individually, pairing each value in the left matrix with the corresponding value in the right matrix. When the <m>dim</m>, <m>planecount</m>, and <m>type</m> attributes of the two matrices differ, <o>jit.op</o> uses the attributes of the matrix in the left inlet. Different math operators can create a variety of visual effects when used to combine two video images.</p>

	<seealsolist>
		<seealso display="Video and Graphics Tutorial 10: Composing the Screen" module="Video and Graphics" name="jitterchapter00l_Composing the screen" type="tutorial" />	
		<seealso name="jit.op">Apply binary or unary operators</seealso>
		<seealso name="jit.movie">Play or edit a movie</seealso>
		<seealso name="jit.scalebias">Multiply and add </seealso>
	</seealsolist>
	</chapter>
