<?xml version='1.0' encoding='UTF-8'?>

<?xml-stylesheet href="./_c74_tut.xsl" type="text/xsl"?>
<chapter name="Tutorial 4: Controlling Movie Playback">
<setdocpatch name="04jControllingMoviePlayback" patch="04jControllingMoviePlayback.maxpat"/>

<previous name="jitterchapter03">Math Operations on a Matrix</previous>
<next name="jitterchapter05">ARGB Color</next>
<parent name="jitindex">Jitter Tutorials</parent>



<h1>Tutorial 4: Controlling Movie Playback</h1>
<p>This tutorial expands on what we've learned thus far about playing back movies in Jitter. We'll learn how to get some useful information about the movie that you're playing, as well as how to manipulate the playback of the movie by changing its speed, volume, and loop points.</p>

<p>The two Jitter objects in this patch should already be familiar to you: <o>jit.movie</o> and <o>jit.pwindow</o>. The rest of the patch will let you experiment with changing the playback behavior of the movie you have loaded into the <o>jit.movie</o> object.</p>


<p>The left side of the patch should seem very familiar to you from the very first tutorial: </p>


<illustration><img src="images/jitterchapter04a.png"/>Open and play the movie.</illustration>
<bullet>Open the file <i>crashtest.mov</i> by clicking the <o>message</o> box that says <m>read crashtest.mov</m>.</bullet>
<p>The movie clip should begin playing as soon as it is read into the <o>jit.movie</o> object. Since this movie has a soundtrack, you should begin to hear some music as soon as the movie is loaded. The movie soundtrack will come out of computer's built-in sound system, not MSP. If you do not wish to hear the soundtrack, you can change the number above the message box that says <b>vol $1</b> to <m>0</m>.</p>


<p>You won't see anything in the <o>jit.pwindow</o> because, even though the movie is playing, the <o>jit.movie</o> object needs a <m>bang</m> message to send a matrix out to the <o>jit.pwindow</o>. Start the <o>metro</o> object by clicking on the <o>toggle</o> box connected to its inlet. You will see the movie's image appear in the <o>jit.pwindow</o> object. Don't worry about the <m>gettime</m> message yet; we'll get to that below.</p>


<h2>Obtaining Some Information About the Movie</h2>
<p>When a <o>jit.movie</o> object opens a new movie, it reads in a lot of information (contained in the movie's <i>header</i>) about the movie, including how long it is, how many frames of video are in the movie, and how fast it is meant to be played. We will use this <i>metadata</i> to control the movie's playback.</p>

<div>
<techdetail><i>Important note</i>: unlike many Jitter attributes, which are set either by you or the object itself, many attributes used by <o>jit.movie</o> are dependent on the current movie file. Different movie files will generate different settings for many of the attributes discussed in this tutorial.</techdetail>

<p>The first thing we want to do with this movie is read the metadata attributes.  It can be retrieved by sending <m>get</m> messages to <o>jit.movie</o>. We then parse the messages the object sends out its rightmost outlet in response. The information we need is the frame rate, the time scale (how many "QuickTime time units" in a second), the duration (in QuickTime units), the playback rate, and the volume. This is all gathered by a message box with <m>getfps, gettimescale, getduration, getrate, getvol</m>.</p>

<p>When you tell a <o>jit.movie</o> to open a movie for playback (by sending it the <m>read</m> message), the object sends a message out its right outlet to tell you that it has found your movie and understood how to play it. If you were to connect a <o>print</o> object to the right outlet of the <o>jit.movie</o> in the patch and reload the movie in the patch, you would see the following printed in the Max Console: <m>read crashtest.mov 1</m>. In this patch, there is a route object connected to the right outlet of <o>jit.movie</o>. That route will catch a read message and pass it to an <o>unpack</o> and <o>select</o> that will trigger the <m>get</m> message after a successful load. If for some reason the object can't locate the <i>crashtest.mov</i> file, you will see a number other than 1 after the name of the file, and no request will be made. </p>


<illustration><img src="images/jitterchapter04b.png"/>Automatically querying the <o>jit.movie</o> object.</illustration>

<p>In addition to catching the <m>read</m> message the <o>route</o> object distributes the movie information as it arrives. Jitter attributes are always output by objects in the same format that you would set them with in your patch: the <i>name</i> of the attribute followed by whatever information the object needs to set that attribute.There are <o>number</o> boxes attached to the route outlets to display the requested values. We don't know what those mean yet, but this mechanism will display the attributes every time we successfully load in a new movie into the <o>jit.movie</o> object.</p>


<h2>Starting, Stopping, and Slowing Down</h2>

<illustration><img src="images/jitterchapter04c.png"/>Some simple movie playback controls </illustration>

<p>The top of the tutorial patch contains some controls to change the playback behavior of the <o>jit.movie</o> object. Sending a <m>stop</m> message to <o>jit.movie</o> will freeze the movie's playback at the current point in the movie. Sending a <m>start</m> message will resume playback where you last left off. Any soundtrack that exists in the movie file will stop playing when the movie's playback is halted. Stopping and starting the movie has no effect on the <o>jit.movie</o> object's matrix output, which is still controlled by the <o>metro</o> object. If you <m>stop</m> the movie with the <b>metro</b> on, you will still receive a new matrix at the rate of the <o>metro</o> object (in this case, 25 times per second), even though all the matrices will be the same.</p>


<p>Changing the <m>rate</m> of the movie will change the speed at which it plays back its video and audio content. Positive <m>rate</m> values will make the movie go forward, with a value of <m>1</m> signifying normal playback speed. Negative values will make the movie go backwards. A rate of <m>0</m> will stop the movie. The <o>jit.movie</o> object takes a floating-point number as the argument to its <m>rate</m> attribute, so a value of <m>0.5</m> will make the movie play at half speed, and a value of <m>-2.3</m> will make the movie play backwards at a bit more than double speed. If you play around with this value, you will note that the soundtrack will speed up, slow down, and play backwards to remain in sync with the video. Once the movie reaches its last frame (or first frame, if you're playing it backwards), it will <m>loop</m> to the opposite end of the file. This behavior can be changed by setting the <m>loop</m> attribute of the <o>jit.movie</o> object with a value of <m>0</m> (no looping), <m>1</m> (regular looping), or <m>2</m> (palindrome looping).</p>


<p>The <m>vol</m> attribute controls the volume (loudness) of any soundtrack component the movie has. A value of <m>1</m> equals full volume, and a value of <m>0</m> will turn the sound off.</p>


<p>In this patch, both the <m>rate</m> and the <m>vol</m> attributes are initialized by the <o>message</o> box in the middle of the patch when the film is loaded. This way they will reflect the values stored in each new movie.</p>


<h2>Time is on My Side</h2>

</div>
<techdetail><i>Note</i>: While the information in the section below is still relevant, updates to the jit.movie object provide simpler alternatives for querying the time and duration state of a loaded movie file. To get the movie length in seconds use the <m>seconds</m> or <m>milliseconds</m> attributes. To get or set the current playback time in seconds use the <m>time_secs</m> attribute, or in milliseconds use the <m>time_ms</m> attribute and for a normalized position value between 0 and 1 use the <m>position</m> attribute. To set the loop points in seconds or milliseconds use <m>looppoints_secs</m> or <m>looppoints_ms</m>.</techdetail>

<p>The first three attributes we queried, <m>duration</m>, <m>timescale</m>, and <m>fps</m>, tell us about how the movie file deals with timing. The <m>duration</m> attribute tells us the total length of the movie. This value is not expressed in milliseconds or frames, but in QuickTime time units. The actual length of each time unit depends on the <m>timescale</m> of the movie. The movie's <m>timescale</m> is the timing resolution of the movie per second. Dividing the <m>duration</m> of a movie by its <m>timescale</m> will tell you the approximate length of the movie, in seconds. Our <i>crashtest.mov</i> file, for example, has a <m>duration</m> of 2836 time units and a <m>timescale</m> of 600. The movie should run for about 4.73 seconds. If we want to move two seconds into the movie, we could set the <o>jit.movie</o> object the <m>message</m><m> time 1200</m> (1200 time units divided by a <m>timescale</m> of 600 units/second gives us 2 seconds).</p>


<p>The <m>fps</m>, or frames per second, of a movie tells us how many individual video images are shown every second. The higher the <m>fps</m> of a movie, the smoother the apparent motion of the movie will be (assuming, that is, that the individual frames are all in some way unique). Some common <m>fps</m> rates are 15, 24, 29.97, and 30. Our movie file in this example runs at 15 frames per second, which works out to a new frame of video every 40 time units, or about every 66.7 milliseconds. If we divide the duration of <i>crashtest.mov</i> by the number of time units per frame in the movie, we can determine that the movie file has 70 frames. If we wanted to, we could get the total number of frames in the movie by querying the <o>jit.movie</o> object with the <m>getframecount</m> message, but then we wouldn't get to do the fun math!</p>


<h2>Scrubbing and Looping</h2>
<illustration><img src="images/jitterchapter04d.png"/>Displaying and setting the current playback frame</illustration>
<p>The area at the bottom of the patch contains two controls for further manipulating the movie's playback. The <o>number</o> box on the left displays the frame that the movie is currently playing. This value is being updated by the <m>gettime</m> message sent into the <o>jit.movie</o> object by the <o>metro</o> object at the top of the patch; each time a new frame is output the time is updated. If you stop the movie's transport (by sending <o>jit.movie</o> a <m>stop</m> message), you can "scrub" through the movie by dragging on the <o>number</o> box. The movie will jump to the frame specified as an argument to the <m>frame</m> message.</p>


<illustration><img src="images/jitterchapter04e.png"/>Setting loop points in a movie</illustration>

<p>Loop points (pairs of time values which specify the beginning and end of a loop) can be sent to a <o>jit.movie</o> object by setting the <m>looppoints</m> attribute with two integer arguments. The <o>rslider</o> in the tutorial patch lets you select regions of the movie that the <o>jit.movie</o> object will loop between. The size of the <o>rslider</o> has been set to the <m>duration</m> of the movie through the attribute query we performed when the movie was loaded. You can reset loop points by sending <o>jit.movie</o> a <m>looppoints</m> message with no arguments (an example of this is at the top of the patch, along with a query message that highlights the entire <o>rslider</o>).</p>


<h2>Summary</h2>
<p>The <o>jit.movie</o> object offers a number of simple attributes that allow you to change the way QuickTime content is played. You can <m>stop</m> and <m>start</m> movie playback with those messages. The <m>rate</m> attribute lets you change the speed and direction of movie playback. You can control the volume of a movie's soundtrack with the <m>vol</m> attribute.</p>


<p>You can get important information about the current movie loaded into the <o>jit.movie</o> object by querying attributes such as <m>duration</m>, <m>timescale</m>, and <m>fps</m>. You can go to specific frames in a movie with the <m>frame</m> message, and you can set and retrieve <m>looppoints</m> for the movie. You can query the current time position of a movie by sending <o>jit.movie</o> a <m>gettime</m> message. </p>


<p>More powerful functions, such as editing and saving movies, can be accomplished and will be discussed in later tutorials. </p>


	<seealsolist>
		<seealso display="Video and Graphics 01: Display a Video" module="Video and Graphics" name="jitterchapter00c_Display a Video" type="tutorial" />	
		<seealso name="jit.movie">Play or edit a movie</seealso>
		<seealso name="metro">send bangs at regular intervals</seealso>
		<seealso name="pack">Combine numbers and symbols into a list</seealso>
		<seealso name="route">Selectively pass the input out a specific outlet</seealso>
		<seealso name="unpack">Break a list up into individual messages</seealso>
	</seealsolist>
	</chapter>
