<?xml version='1.0' encoding='UTF-8'?>

<?xml-stylesheet href="./_c74_tut.xsl" type="text/xsl"?>
<chapter name="Tutorial 27: Using MSP Audio in a Jitter Matrix">
<setdocpatch name="27jAudioIntoMatrix" patch="27jAudioIntoMatrix.maxpat"/>

<previous name="jitterchapter26">MIDI Control of Video</previous>
<next name="jitterchapter28">Audio Control of Video</next>
<parent name="jitindex">Jitter Tutorials</parent>



<h1>Tutorial 27: Using MSP Audio in a Jitter Matrix</h1>
<techdetail>
	This article is obsolete. With Apple's withdrawal of support for quicktime <o>spigot~</o> is no longer available. However the <o>jit.movie~</o> object duplicates its functionality, and the tutorial patch has been updated to demonstrate.
</techdetail>
<p>This tutorial shows how to copy an MSP audio signal into a Jitter matrix using an object called <o>jit.poke~</o>. Along the way, we'll investigate how to use the soundtrack from a movie in the MSP signal network using the sound output component attribute of the <o>jit.movie</o> object and a new MSP object called <o>spigot~</o>. </p>

<p>This tutorial assumes familiarity with routing MSP signals using <o>send~</o> and <o>receive~</o>. It also uses a simple delay network using <o>tapin~</o>/<o>tapout~</o> objects. <i>Tutorial 4</i> and <i>Tutorial 27</i> in the <i>MSP</i> manual cover these topics.</p>


<p>The <o>jit.movie</o> object at the top left of the tutorial patch reads a movie called <i>rca.mov</i> upon opening. </p>

<illustration><img src="images/jitterchapter27a.png"/>Our <o>jit.movie</o> object</illustration>
<bullet>Start the <o>metro</o> object at the top of the patch by clicking the <o>toggle</o> box. You will see an image in the lefthand <o>jit.pwindow</o> object below the <o>jit.movie</o> object. You won't see anything in the other <o>jit.pwindow</o> objects yet, nor will you hear any sound.</bullet>
<p>Our <o>jit.movie</o> object has two attributes set in its object box in addition to its <m>dim</m> attribute (<m>320</m> by <m>240</m> cells). The <m>loop</m> attribute with a value of <m>2</m> tells the <o>jit.movie</o> object to loop the movie as a <i>palindrome</i>. Once the playback of the movie reaches the end, it will play backwards to the beginning of the file, rather than looping around to the beginning and playing forward (the default behavior, when the <m>loop</m> attribute is set to <m>1</m>). If you watch the movie, you'll see that the arm manipulating the oscillator control moves up and then down again in an endless loop. The movie actually only contains footage of the arm moving upward, but the <m>loop</m> attribute we've used reverses the playback in the second half of the loop.</p>

<h2>The Sound Output Component</h2>
<p>The second attribute we've set in our <o>jit.movie</o> object sets the <i>Sound Output Component</i> (<m>soc</m>) for that instance of the <o>jit.movie</o> object. The name specified as an argument to the <m>soc</m> attribute (in this case <m>gliss</m>) specifies a new sound output component that MSP can use to acquire the soundtrack of the movie loaded into the <o>jit.movie</o> object. By default, the <m>soc</m> attribute is set to <m>none</m>, which routes the movie's audio output directly to the Sound Manager. A named <m>soc</m> attribute routes the audio to a <o>spigot~</o> object with the same name as the component, allowing you to access the audio signal in MSP:</p>

<illustration><img src="images/jitterchapter27b.png"/>The <o>spigot~</o> object</illustration>
<p>The <o>spigot~</o> object in the upper-right hand corner of the tutorial patch has an argument (<m>gliss</m>) which matches the <m>soc</m> attribute of our <o>jit.movie</o> object. If a movie file loaded into that <o>jit.movie</o> object has a soundtrack (which the <i>rca.mov</i> file conveniently does), the audio from the movie is sent out as MSP signals from the <o>spigot~</o>. Note that the <o>spigot~</o> object has two outlets, which correspond to the left and right audio channels of the movie soundtrack. Our <i>rca.mov</i> file has a monaural soundtrack, so we only need to use one of the outlets in our patch.</p>

<div>
<techdetail>Important: The <m>soc</m> attribute of the <o>jit.movie</o> object allows you to create a separate sound output component for each <o>jit.movie</o> object in your patch. You can use as many <o>spigot~</o> objects as you like, each with a unique name, to grab the audio from multiple movies. It's important to note, however, that you can only have one <o>spigot~</o> object per sound output component, and each <o>jit.movie</o> object must have a unique <m>soc</m> attribute (unless, of course, the <m>soc</m> is set to <m>none</m>&#x2014;System Sound can take the sound from as many movies as you wish). Once you have multiple movie audio tracks as MSP signals you can mix them as you please.</techdetail>
</div>
<bullet>Start the <o>dac~</o> object at the bottom of the patch by clicking the <o>toggle</o> box attached to it. You will see images appear in the remaining <o>jit.pwindow</o> objects and will see a signal level appear in the <o>meter~</o> object attached to the <o>spigot~</o>. If you turn up the <o>gain~</o> slider attached to the <o>dac~</o>, you should begin to hear sound out of whatever device you currently have selected as your MSP audio driver. For more information on how to set up your computer's audio system with MSP, consult the MSP tutorial <link type="tutorial" module="msp" name="04_mspaudioio">Audio I/O - Audio input and output with MSP</link>.</bullet>
<illustration><img src="images/jitterchapter27c.png"/>Receiving the audio signal from the <o>spigot~</o></illustration>
<p>The soundtrack from the <i>rca.mov</i> file is sent as an MSP signal from the <o>spigot~</o> object into a two-tap delay line (generated by the <o>tapin~</o> and <o>tapout~</o> objects in the patch). The dry audio signal is sent to a <o>send~</o> object with the name <m>red</m> attached to it; the two delay taps are sent to <o>send~</o> objects named <m>green</m> and <m>blue</m>, respectively. The three audio signals are output by named <o>receive~</o> objects and summed into the <o>gain~</o> object at the bottom of the patch, allowing you to hear all of them at once.</p>

<bullet>Adjust the delay times using the <o>number</o> box objects labeled <i>Delay times (green/blue)</i> attached to the <o>tapout~</o> objects. You can adjust the delays up to a maximum length of <m>1000</m> milliseconds (the maximum delay time allocated by our <o>tapin~</o> object).</bullet>

<h2>Poke~ing Around</h2>
<p>The righthand <o>jit.pwindow</o> object at the top of the tutorial patch shows the output of a <o>jit.matrix</o> named <m>scope</m>, which also gets <m>bang</m> messages from the <o>metro</o> object at the top of the patch:</p>

<illustration><img src="images/jitterchapter27d.png"/>The output of the <m>scope</m> <o>jit.matrix</o></illustration>
<p>The <m>scope</m> Jitter matrix is generated by three <o>jit.poke~</o> objects at the right of the tutorial patch, which write MSP audio signals into cells in the matrix. These cells, when displayed in the <o>jit.pwindow</o> object, portray an oscilloscope view of the movie soundtrack, with the dry and two delayed signals appearing as the colors <m>red</m>, <m>green</m>, and <m>blue</m>, respectively.</p>

<illustration>
<img src="images/jitterchapter27e.png" />The three <o>jit.poke~</o> objects, writing into the <m>scope</m> matrix</illustration>
<p>The three similar regions at the right of the screen use the <o>jit.poke~</o> object to write MSP signal data into our <m>scope</m> matrix. The <o>jit.poke~</o> object takes three arguments: the <m>name</m> of the Jitter matrix to write into, the number of <m>dim</m> inlets to use, and the <m>plane</m> of the destination matrix to write numbers to. All three <o>jit.poke~</o> objects in our patch write into the matrix <m>scope</m>. Since <m>scope</m> is a 2-dimensional matrix, we need <m>2</m> inlets to specify where to write the data (one inlet for the column and one inlet for the row). The three objects differ in that they each write to a different <m>plane</m> of the <m>scope</m> matrix.</p>

<p>The first inlet of the <o>jit.poke~</o> object provides the value to write into the matrix cell specified by the other two inlets, which take signals to specify the cell location. We use a <o>sig~</o> object with a value of <m>1</m> to write a constant value into our current position in the scope matrix. The value of <m>1</m> gets interpreted as <m>255</m> when writing into a matrix containing <i>char</i> data (which is what we're doing in this case).</p>

<p>The other two inlets in our <o>jit.poke~</o> objects determine where in the output matrix they should write data (this set of coordinates defines the <i>write pointer</i> for the object&#x2014;you could think of this as the location of the record head, only with two dimensions instead of one). The rightmost inlet receives the audio signal from our named <o>receive~</o> objects and sets the vertical (<m>dim 1</m>) coordinate of the write pointer to correspond to the amplitude of the signal. The <o>*~</o> and <o>+~</o> objects in the patch scale the output of the audio signal from between <m>-1</m> and <m>1</m> (the typical range for an audio signal) to between <m>0</m> and <m>239</m> (the range of the vertical dimension of our output matrix).</p>

<h2>Sync or Swim</h2>
<p>The middle inlet to our <o>jit.poke~</o> object receives a <i>sync</i> signal that specifies where along the horizontal axis of the matrix we write the current amplitude from the audio signal. This signal is unrelated to the audio data coming from the movie&#x2014;you could think of it as the horizontal refresh rate of the virtual oscilloscope we've made in this patch. The sync signal is generated by a <o>phasor~</o> object in the <m>Scope_setup</m> subpatch:</p>

<illustration><img src="images/jitterchapter27f.png"/>Generating the horizontal sync signal for our <o>jit.poke~</o> objects</illustration>
<p>Our <o>phasor~</o> object generates a repeating ramp signal from <m>0</m> to (nearly) <m>1</m>. The <o>*~</o> below it rescales this signal to generate values appropriate to the width of our matrix (<m>0</m> to <m>319</m>). This signal is then passed to a <o>send~</o> object with the name <m>h_sync</m>, which forwards the signal to <o>receive~</o> objects connected to the middle inlets of our <o>jit.poke~</o> objects. The frequency of the <o>phasor~</o> (specified by the number box connected to its first inlet) determines the rate at which our <o>jit.poke~</o> objects scan from the left to right through the matrix.</p>

<bullet>Try changing the frequency of the <o>phasor~</o> by changing the number box labeled <i>Horizontal scan rate (Hz)</i>. Notice how at higher frequencies  you can see the waveform generated by the movie audio in more detail. If you set the rate to a negative value, the matrix will be written backwards (i.e. from right to left).</bullet>
<p>The dry audio signal and the two delayed outputs are visualized as the three visible planes of our <m>scope</m> matrix (<m>1</m>, <m>2</m>, and <m>3</m>, or <m>red</m>, <m>green</m>, and <m>blue</m>). When the cells written by the <o>jit.poke~</o> objects overlap, different color combinations will appear in the output matrix.</p>

<p>Now that we understand how the matrix is being written, we need to look into how the matrix clears itself every time a horizontal scan is completed. The relevant parts of the patch are shown below:</p>

<illustration><img src="images/jitterchapter27g.png"/>Detect when the horizontal sync resets and clear the matrix</illustration>
<p>The <o>change~</o> object outputs a value of <m>1</m> when the ramp generated by the <o>phasor~</o> object is on the increase. When the <o>phasor~</o> snaps back to <m>0</m> at the end of the ramp, <o>change~</o> will briefly output a value of <m>-1</m>. The <o>==~</o> operator, which outputs a <m>1</m> when the <o>change~</o> object
does, will output a <m>0</m> at that point. When the <o>phasor~</o> begins to ramp again, the <o>==~</o> object
will output a <m>1</m>, triggering a <m>bang</m> from the <o>edge~</o> object (which detects a zero to non-zero transition in the last signal vector). The <m>bang</m> is then sent to a <o>receive</o> object named <m>reset</m>, which triggers a <m>clear</m> message to the <o>jit.matrix</o> object. As a result, our <m>scope</m> matrix is cleared every time the <o>phasor~</o> restarts its ramp.</p>

<h2>Putting it all Together</h2>
<p>Our two Jitter matrices (the image from the <o>jit.movie</o> object and the oscilloscope drawn by our <o>jit.poke~</o> objects) are composited into a final matrix by the <o>jit.op</o> object:</p>

<illustration><img src="images/jitterchapter27h.png"/>Compositing the two matrices using <o>jit.op</o></illustration>
<p>The <m>op</m> attribute we've specified initially for our <o>jit.op</o> object is <m>*</m>. As a result, our composite is made from the multiplication of the two matrices. Since most of the cells in our <m>scope</m> matrix are <m>0</m> (black), you only see the movie image appear in those cells and planes where the <o>jit.poke~</o> objects have traced the waveform. </p>

<bullet>Change the <m>op</m> attribute of the <o>jit.op</o> object by clicking on some of the <o>message</o> boxes attached to the <o>prepend</o> object to the right of the <o>jit.pwindow</o> showing the <m>scope</m> matrix. Notice how the different arithmetic operators change the compositing operation of the two matrices.</bullet>

<h2>Summary</h2>
<p>The <m>soc</m> attribute of the <o>jit.movie</o> object lets you define a named <i>Sound Output Component</i>. The <o>spigot~</o> object lets you access the soundtrack of a movie as an MSP signal by giving it an argument that matches the <m>soc</m> attribute of the <o>jit.movie</o> object playing the movie.</p>

<p>You can use the <o>jit.poke~</o> object to write data from MSP signals into a named Jitter matrix. The <o>jit.poke~</o> object takes arguments in the form of the <m>name</m> of the matrix to write to, the number of inlets with which to specify cell coordinates, and the <m>plane</m> to write to in the matrix. The first inlet of <o>jit.poke~</o> takes the value to be written into the matrix. Subsequent inlets take MSP signals that specify the cell location in the matrix in which data should be written.</p>

	<seealsolist>
		<seealso display="Video and Graphics Tutorial 8: Audio into a matrix" module="Video and Graphics" name="jitterchapter00j_Audio into a matrix" type="tutorial" />
		<seealso name="change~">Report signal direction</seealso>
		<seealso name="dac~">Audio output and on/off</seealso>
		<seealso name="edge~">Detect logical signal transitions</seealso>
		<seealso name="jit.matrix">The Jitter Matrix!</seealso>
		<seealso name="jit.op">Apply binary or unary operators</seealso>
		<seealso name="jit.poke~">Write an audio signal into a matrix</seealso>
		<seealso name="jit.pwindow">In-Patcher Window</seealso>
		<seealso name="jit.movie">Play or edit a movie</seealso>
		<seealso name="phasor~">Sawtooth wave generator</seealso>
		<seealso name="spigot~">Route QT audio into MSP</seealso>
		<seealso name="tapin~">Input to a delay filter</seealso>
		<seealso name="tapout~">Output from a delay line</seealso>
	</seealsolist>
	</chapter>
