<?xml version='1.0' encoding='UTF-8'?>

<?xml-stylesheet href="./_c74_tut.xsl" type="text/xsl"?>
<chapter name="Tutorial 1: Playing a Movie">
<setdocpatch name="01jPlayAMovie" patch="01jPlayAMovie.maxpat"/>

<previous name="jitterchapter00l_Composing the screen">Composing the Screen</previous>
<next name="jitterchapter02">Create a Matrix</next>
<parent name="jitindex">Jitter Tutorials</parent>



<h1>Tutorial 1: Playing a Movie</h1>
<p>This first tutorial demonstrates one of the simplest yet most useful tasks you can perform with Jitter: playing a movie in a window.</p>

<p>There are two Jitter objects in this patch: <o>jit.movie</o> and <o>jit.window</o>. The <o>jit.window</o> object automatically opens a window on your computer screen. The <o>jit.movie</o> object allows you to open an existing movie, and begins to play it.</p>

<bullet>Click on the <o>message</o> box containing the message <m>read countdown.mov</m>. This causes the <o>jit.movie</o> object to open the movie file <i>countdown.mov</i> and begin reading from it.</bullet>
<illustration><img src="images/jitterchapter01a.png"/>The <m>read</m> message opens a movie file.</illustration>
<p>By default a <o>jit.movie</o> will begin playing a movie as soon as it opens it. (Alternatively, you can alter that behavior by sending a <o>jit.movie</o> object an <m>autostart 0</m> message before opening the file, but for now the default behavior is fine.) Notice, however, that even though we've said that the <o>jit.movie</o> object is playing the movie, the movie is not being shown in the Movie window. Here's why:</p>

<p>Each object in Jitter does a particular task. The task might be very simple or might be rather complicated. What we casually think of as "playing a movie" is actually broken down by Jitter into two tasks: </p>

<p>1.   Reading each frame of movie data into RAM from the file on the hard disk</p>
<p>2.   Getting data that's in RAM and showing it as colored pixels on the screen.</p>
<p>The first task is performed by the <o>jit.movie</o> object, and the second by the <o>jit.window</o> object. But in order for the <o>jit.window</o> object to know what to display, these two objects need to communicate.</p>

<h2>How Jitter Objects Communicate</h2>
<div>
<techdetail><i>Important Concept:</i> The most important thing that Jitter objects communicate to each other is a <i>name</i>, referring to a <i>matrix</i>&#x2014;a place in memory where data is stored. (We'll explain the meaning of the word "matrix" in more detail in the next tutorial chapter.) Jitter objects output a message that only other Jitter objects understand. That message is the word <m>jit_matrix</m> followed by a space and the name of a matrix where data is stored. This message is communicated from one Jitter object to another through a patch cord in the normal Max manner. (But, just as MSP objects' patch cords look different from other Max patch cords, the patch cords from Jitter obejcts' outlets that send the <m>jit_matrix</m> message have their own unique look.) The receiving Jitter object receives the message in its inlet (most commonly the left inlet), gets the data from the specified place in memory, modifies the data in some way, and sends the name of the modified data out its left outlet to all connected Jitter objects. In this way, tasks are performed by each object without necessarily knowing what the other objects are doing, and each object gets the data it needs by looking at the appropriate place in memory. Most Jitter objects don't really <i>do</i> anything until they get a <m>jit_matrix</m> message from another Jitter object, telling them to look at that matrix and do something with the data there.</techdetail>
<techdetail>In many cases a Jitter object will generate a unique name for its matrix on its own. In other cases, it is possible (and even desirable) to tell an object what name to use for a matrix. By explicitly naming a matrix, we can cause objects to use that same memory space. You will see examples of this in future tutorial chapters. </techdetail>
</div>
<h2>Causing Action by Jitter Objects</h2>
<p>What causes one Jitter object to send a <m>jit_matrix</m> message to another object? Most Jitter objects send out a <m>jit_matrix</m> message when they receive the message <m>outputmatrix</m> or <m>bang</m>. (These two messages have the same effect in most Jitter objects.) The other time that an object sends out a <m>jit_matrix</m> message is when it has received such a message itself, and has modified the data in some way; it then automatically sends out a <m>jit_matrix</m> message to inform other objects of the name of the matrix containing the new data.</p>

<p>To restate the previous paragraph, when an object receives a <m>jit_matrix</m> message, it does something and sends out a <m>jit_matrix</m> message of its own. When an object receives <m>outputmatrix</m> or <m>bang</m>, it sends out a <m>jit_matrix</m> message without doing anything else.</p>

<p>So, in our example patch, the <o>jit.movie</o> object is "playing" the movie, constantly storing the current frame of video, but the <o>jit.window</o> object will only display something when it receives a <m>jit_matrix</m> message from the <o>jit.movie</o> object. And that will only happen when <o>jit.movie</o> receives the message <m>bang</m> (or <m>outputmatrix</m>). At that time, <o>jit.window</o> will display whatever frame of video happens to be currently playing in the movie (that is, the frame that's currently stored by <o>jit.movie</o>).</p>

<p>In order to make <o>jit.window</o> update its display at the desired rate to show a continuously progressing video, we need to send the message <m>bang</m> to <o>jit.movie</o> at that rate.</p>

<illustration><img src="images/jitterchapter01b.png"/>The movie is playing in jit.movie,<br/> but we need to send it a <m>bang</m> each time we want to display a frame.</illustration>
<bullet>Click on the <o>toggle</o> object marked "Play" to start the <o>metro</o> object. This will send out <m>bang</m> at the rate of 25 times per second (every 40 milliseconds). That should be fast enough to display every frame of this video. As long as the <m>bang</m> messages continue, you will see the movie displayed in the Movie window.</bullet>

<illustration><img src="images/jitterchapter01c.png"/><o>jit.window</o> displays the contents of a matrix: in this case a frame of a movie.</illustration>

<bullet>Click on the <o>toggle</o> to stop the <o>metro</o>. The <o>jit.window</o> object stops updating the Movie window, so you will now just see a still image of whatever frame was last displayed. The movie is still playing&#x2014;and <o>jit.movie</o> is still updating its memory frame-by-frame&#x2014;but <o>jit.window</o> is now oblivious because <o>jit.movie</o> is no longer sending messages.</bullet>
<bullet>You can verify that the movie is still progressing by clicking on the <o>button</o> object just below the <o>metro</o>. This will cause <o>jit.movie</o> to send a <m>jit_matrix</m> message to <o>jit.window</o>, which will update the Movie window with the current frame. If you do this a few times, you will see that the movie has progressed in between clicks of the mouse. (The movie is a ten-second countdown, playing in a continuous loop.)</bullet>
<p>To summarize, <o>jit.movie</o> is continually reading in one video frame of the movie, frame by frame at the movie's normal rate. When <o>jit.movie</o> receives a <m>bang</m>, it communicates the location of that data (that single frame of video) to <o>jit.window</o>, so whatever frame <o>jit.movie</o> contains when it receives a <m>bang</m> is the data that will be displayed by <o>jit.window</o>.</p>

<h2>Arguments in the Objects</h2>
<p>The <o>jit.movie</o> object in this tutorial patch has two typed-in arguments: <m>320 240</m>. These numbers specify the horizontal and vertical (width and height) dimensions the object will use in order to keep a single frame of video in memory. It will claim enough RAM to store a frame with those dimensions. So, in the simplest case, it makes sense to type in the dimensions of the movie you expect to read in with the <m>read</m> message. In this case (since we made the movie in question ourselves) we happen to know that the dimensions of the movie <i>countdown.mov</i> are 320x240.</p>

<p>If we type in dimension arguments smaller than the dimensions of the movie we read in, <o>jit.movie</o> will not have claimed enough memory space and will be obliged to ignore some of the pixels of each frame of the movie. Conversely, if we type in dimension arguments larger than the dimensions of the movie we read in, there will not be enough pixels in each frame of the movie to fill all the memory space that's been allocated, so <o>jit.movie</o> will distribute the data it does get evenly and will fill its additional memory with duplicate data.</p>

<p>The <o>jit.window</o> object has five typed-in arguments:
<m>Movie 5 41 325 281</m>. The first argument is a name that will be given to the matrix of data that <o>jit.window</o> displays. That name will also appear in the title bar of the movie window. It can be any single word, or it can be more than one word if the full name is enclosed between quote characters. The next two arguments indicate the <i>x,y</i> screen coordinates of the upper-left corner of the display region of the movie window, and the last two arguments provide the <i>x,y</i> coordinates of the lower-right corner of the display region. (Another way to think of these four numbers is to remember them as the coordinates meaning "left", "top", "right", and "bottom".) We have chosen these particular numbers because <i>a</i>) they describe a display region that is 320x240 pixels, the same size as the movie we intend to display, and <i>b</i>) when we take into account the dimensions of the window borders, title bar, and menu bar that the OS imposes, the entire window will be neatly tucked in the upper-left corner of our useable desktop. (It's possible to make the window borders and title bar disappear with a <m>border 0</m> message to <o>jit.window</o>, but the default borders are OK for now.)</p>

<p>We have typed the value of <m>40</m> in as an argument to <o>metro</o> to cause it to send out 25 <m>bang</m> messages per second. The movie actually has a frame rate of exactly 24 frames per second, so this <o>metro</o> will trigger the <o>jit.movie</o> object frequently enough to ensure that every frame is made available to <o>jit.window</o> and we'll get to see every frame.</p>

<p>The <o>jit.movie</o> object actually understands many more messages besides just <m>bang</m> (way too many to try to explain here). In the upper-right corner of the Patcher window, we've included an example of one more message, simply to demonstrate that the progress of the movie can be controlled in <o>jit.movie</o> independently of the rate at which the <o>metro</o> is sending it <m>bang</m> messages. The message <m>time</m>, followed by a number, causes <o>jit.movie</o> to jump immediately to a specific time location in the movie.</p>

<bullet>Click on the <o>button</o> object labeled "Restart". This sends a message of <m>time 0</m> to <o>jit.movie</o>. causing it to jump to the beginning of the movie, and then sends a <m>1</m> message to the <o>toggle</o> to start the <o>metro</o> and begin displaying the movie.</bullet>
<p>To play a movie, use the <o>jit.movie</o> object to open the file and read successive frames of the video into RAM, and use the <o>jit.window</o> object to display the movie in a separate window. Use typed-in arguments to specify the dimensions of the movie, and the precise coordinates of the display area on your screen.</p>

<h2>Summary</h2>
<p>Jitter objects communicate the information about a particular frame of video by sending each other the name of a <i>matrix</i>&#x2014;a place in memory where that information is located. When a Jitter object gets a matrix name, it performs its designated task using the data at that location, then sends out the name of the modified data to other Jitter objects. Almost all Jitter objects send out a name (in a <m>jit_matrix</m> message) when they receive the message <m>bang</m> (or <m>outputmatrix</m>). Thus, to show successive frames of a video, send <m>bang</m> messages at the desired rate to a<b> </b><o>jit.movie</o> object connected to a <o>jit.window</o> object.</p>
<illustration><img src="images/jitterchapter01d.png"/>Tracing the messages and roles of each object</illustration>
	<seealsolist>
		<seealso name="jit.movie">Play or edit a movie</seealso>
		<seealso name="jit.window">Display data in a Window</seealso>
		<seealso name="metro">send bangs at regular intervals</seealso>
	</seealsolist>
	</chapter>
