<?xml version='1.0' encoding='UTF-8'?>

<?xml-stylesheet href="./_c74_tut.xsl" type="text/xsl"?>

<chapter name="Max Basic Tutorial 13: Movie Sequencing">

<setdocpatch name="13mMovieSequencing" patch="13mMovieSequencing.maxpat"/>
<previous name="basicchapter12">Movie Playback</previous>
<next name="basicchapter14">Encapsulation</next>
<parent name="00_maxindex">Max Tutorials</parent>

<indexinfo category="Basics" title="Movie Sequencing">Working with multiple media files</indexinfo>

<h1>Tutorial 13: Movie Sequencing</h1>

<h2>Introduction</h2>

<p>In this tutorial, we will work with a few new objects: the <o>rslider</o>, <o>umenu</o> and <o>loadbang</o> objects. The <o>rslider</o> object allows us to select a range of numbers using a single UI element.  The <o>umenu</o> object allows us to pick an item from a pop-up menu within a patcher.  The <o>loadbang</o> object is a setup object – it allows us to trigger messages when a patcher is first loaded.  All of these are important items for more complex patches – and we will use them in the context of making some interesting video-driven programs.</p>

<p>Providing user selections is important once your patch gets complicated – or if you want to share your patch with others.  The <o>rslider</o> object is great for selecting loop points (since it gives you the ability to select start and end points with a single mouse swipe), and the <o>umenu</o> object is perfect for selecting among a list of files or display options. Finally, <o>loadbang</o> gives us the ability to set object parameters when a patch is loaded, allowing us to create patchers that require a minimum of setup to get started with.</p>

<p>To open the tutorial patch, click on the <b>Open Tutorial</b> button in the upper right-hand corner of the documentation window.</p>

<h2>A few new objects: <o>rslider</o>, <o>loadbang</o>, <o>umenu</o></h2>

<p>Take a look at the tutorial patcher.  This patch contains a few simple patches at the left, and two larger patches to the right. Let’s start by looking at the left-hand patches, where we are introduced to our three new objects.</p>

<p>The right-most patch is the example patch for the <o>rslider</o> object. Click on the surface, and you will see that the wiper (if any) will will vanish. However, if you click the mouse then drag up or down on the surface, you will see that you can actually select a <i>range</i> of values – reflected in the two outputs of the object. If you shift-click, you can adjust the end closest to the mouse. The name <o>rslider</o> refers to the fact that this is a “range slider”, capable of selecting a range of values with a single mouse movement.  The actual range that the <o>rslider</o> covers can be set with a simple message (it defaults to the range <m>0</m> to <m>127</m>), as we'll see later in the tutorial.</p>

<p>The next object is pretty simple, but very important.  It is the <o>loadbang</o> object, and it sends out a <m>bang</m> whenever the patcher file is loaded (hence the name). This is most often used for setting up values in a user interface – in this case, we are loading the <o>number</o> box with <m>55</m> (it would normally default to <m>0</m>) each time we open the patch. We can also force the <o>loadbang</o> to fire by double-clicking on it. If we change the connected <m>number</m> box to another number, double-clicking on the <o>loadbang</o> will cause a <m>bang</m> message to hit the <m>message</m> box (containing the message <m>55</m>), and the <m>number</m> box will again update to its previous value.</p>
<techdetail>
	<o>Loadbang</o> will not produce a bang when it is created or copied, so it is easy to get new work out of sync with what will happen when a patcher is loaded. Be sure to give them a double-click before testing a patch. Better yet, save, close, and re-open the patch to comnfirm that <o>loadbang</o> is doing its job. If you need to duplicate the action of a <o>loadbang</o> automatically, it will respond to a bang in its inlet by sending a bang.
</techdetail>

<p>Finally, the <o>umenu</o> is shown in the bottom of the three small patches. If you click on the <o>umenu</o>, it displays a set of options as a pop-up menu. Selecting one will produce two forms of output: the <i>left</i> outlet will send the index number of the selection, starting at <m>0</m>; and the <i>middle</i> outlet will output a symbol containing the <i>text</i> of the menu option.</p>

<p>The items available in a <o>umenu</o> object can be set in a number of ways; in the next section we'll look at how to populate it automatically.  First, however, <i>unlock</i> the patcher and open the <b>Inspector</b> for the <o>umenu</o> object on the right-hand toolbar.  In the Inspector you'll see a section labeled "Items"; within this section we can view the list of items contained in the <o>umenu</o> (<m>small</m>, <m>medium</m>, etc.).  Click on the "Edit" button next to the list and a window should open that will allow us to type in anything we want.  Items in the <o>umenu</o> need to be separated by commas.  Add to the list (or create an entirely new one), click <b>OK</b>, and close the Inspector.  You should see the <o>umenu</o> change to reflect the new items we've just added.</p>

<h2>Loading <o>umenu</o> with file info</h2>

<p>The first large patch is labeled <b>1</b>, and has two interesting sections: the top area is used to select movie files from a folder of movies that ship with the Max distribution, while the bottom area is used to loop the playback of these movies within an <o>imovie</o> object.  Let’s start by looking at the top section.</p>

<p>What we have here is a rather complete file selection system. A <o>loadbang</o> object is used to trigger a <o>message</o> box that sends the message <m>prefix ./patches/media</m> to the <o>umenu</o> object. If we look at the reference material for the <o>umenu</o> object, we see that <m>prefix</m> is an attribute that sets a menu-wide prefix that is added to the beginning of any selected item.  It has a second use as well – it tells the <o>umenu</o> where to load files when it is told to populate (or, in our case, to <m>autopopulate</m>). The <o>umenu</o> object has its autopopulate attribute set in the inspector, so when the <m>prefix</m> message is received, the <o>umenu</o> is loaded with the names of every file in the folder pointed to by the <m>prefix</m> message.</p>

<p>As a result, we have a <o>umenu</o> loaded with file names from a folder called "media" inside of a folder called "patches" (The "patches" folder is actually found within the Max application bundle. Otherwise this form would refer to folders relative to the folder where Max is installed on your system. Most of the time your paths will be relative to ~users.)  When we select one, three things happen, thanks to the <o>trigger</o> object connected to the middle outlet of the <o>umenu</o>.  First, the message <m>dispose</m> is sent to the <o>imovie</o>; this clears the <o>imovie</o> object of any video file that is currently loaded into it.  Next, the symbol (the <m>s</m> in <o>trigger</o>) containing the name of the file with the folder prefix <m>./patches/media</m> prepended to it is sent out.  The <m>read</m> message is placed in front (via the <o>prepend</o> object) and then sent to the <o>imovie</o>.  Third, the <o>trigger</o> object sends a <m>bang</m> through a <o>delay</o> object, which delays the message by <m>100</m> milliseconds; this gives the <o>imovie</o> object time to <m>read</m> in the movie.  After the delay has expired, the <o>message</o> box below is triggered.  It contains a few initialization messages to <o>imovie</o>: <m>length</m> (to query how long the movie is), <m>loop 1</m> (to tell the movie to loop), and <m>start</m> (to begin playback).  In this case, although we have a <o>number</o> box connected to the left outlet, we aren’t doing anything with the <o>umenu</o> object's index – everything is triggered by the middle (text-based) outlet.  Note how the <o>trigger</o> object is extremely useful in this case, allowing us to set up a complex series of steps in the correct sequence: <m>dispose</m> the previous movie, <m>read</m> the new movie, find out its <m>length</m>, tell it to <m>loop</m>, and <m>start</m> it.</p>

<p>Select a movie from the <o>umenu</o>, and notice that it will load and quickly begin playing, looping at the end of the clip.  If you select a different clip, the previous one with be disposed and the new one will start looping.</p>

<h2>Using the <o>rslider</o> to set loop points</h2>

<p>In addition to loading and starting the movie, that <m>length</m>, <m>loop 1</m>, <m>start</m> <o>message</o> had another consequence – it forced the movie <m>length</m> out the left outlet of the <o>imovie</o> object, and set a new chain of events in motion.  This <m>length</m> message is used to set up the <m>size</m> (i.e. the <i>range</i>) of an <o>rslider</o>, and set the object with its complete range selected (this is what the <m>0 $1</m> message does).  We can easily change the contents of the <o>rslider</o> by clicking and dragging within it, therefore changing the loop points that the <o>imovie</o> will use.  To best see this in action, select the movie “countdown.mov” from the <o>umenu</o> at the top – this is the familiar movie that counts down from 10 to 1.</p>

<p>Now, if we click and drag in the <o>rslider</o> at the bottom of the patch, you can see that we change the start and end points of the loop. The output of the <o>rslider</o> object is sent to a <o>pack</o> object and then used to create a <m>loopset</m> message, which changes the looping "in and out points" honored by <o>imovie</o>. In addition, it rewinds the film to the start of the loop (the <m>$1</m>) message.  This section of the patch shows a very common use for the <o>rslider</o> object; it is initialized with the length of a media file, then is used for playback start and end positions.</p>

<h2>Setting things up: <o>loadbang</o> and <o>screensize</o></h2>

<p>Finally, we are going to see how <o>loadbang</o> (along with a few other messages) can be used to completely set up a patch for the user when it is first opened.  This patch (labeled <b>2</b>, and on the right), takes the current mouse location and uses it to decide where to place a movie within a 320 x 240 pixel display area. The <o>mousestate</o> is polled, and a <m>rect</m> message puts the currently playing movie, in shrunken form, at the specified location within the <o>imovie</o> object.</p>

<p>To begin, one <o>loadbang</o> message is used to load the “bball.mov” movie file when the patch is first loaded.  This way, you (or your user) does not have to find the file themselves. Secondly, another <o>loadbang</o> is used at the top-right of the patch; it sends a <m>bang</m> message to the <o>screensize</o> object.  The <o>screensize</o> object outputs a list of four coordinates representing the <i>coordinates</i> of your primary display.  The last two elements in this list are, for all intents and purposes, going to give us the <i>width</i> and <i>height</i>, in pixels, of our screen (e.g. <m>1024</m> and <m>768</m>).  The <o>mousestate</o> object, as we remember, puts out the position of the mouse within that range starting at <m>0</m>, so if we subtract <m>1</m> from the screensize we can properly <o>scale</o> the output of <o>mousestate</o> into the range <m>0.</m> to <m>1.</m> - this is a common trick when working with mouse coordinates, as we seldom use their <i>actual</i> pixel values; more often than not, we simply want to use them to control an entirely different range of numbers.</p>

<p>Once the <o>scale</o> object has its maximum input set by the <o>screensize</o> object, the current mouse position will always yield values between 0.0 and 1.0.  These are then multiplied by <m>240</m> and <m>180</m> to get the upper-left-hand screen location for the video placement within the <o>imovie</o> object.  The <m>80 60</m> portion of the <m>rect</m> message sets the <i>width</i> and <i>height</i> of the rectangle within the <o>imovie</o> - as a result, for a <m>320</m> by <m>240</m> window, our maximum input coordinates should be <m>240</m> and <m>180</m>.</p>

<p>Turn on the <o>metro</o> object with the <o>toggle</o> to begin polling the mouse, and click on the <m>start</m> <o>message</o>.  Move your mouse around the display.  You should see a small version of the basketball movie track your current cursor’s location, and it will always be scaled correctly – no matter what display you run it on.</p>

<h2>Summary</h2>

<p>In this tutorial, we’ve learned about three very important new objects: <o>rslider</o> for range selection, <o>umenu</o> for the creation and selection of a list of items (or file names), and <o>loadbang</o> for initial state setup. In addition, we saw how the <o>screensize</o> object can be used to find out information about the size of your display.  We demonstrated them by creating some complex video playback systems that controlled loop length and display positioning. These objects will be some of the most important tools you will use on future patches.</p>

<seealsolist>
<seealso name="rslider">Select and display a range of two values</seealso>
<seealso name="umenu">Pop-up menu</seealso>
<seealso name="loadbang">Generate a bang when a Patcher is loaded</seealso>
<seealso name="screensize">Provides the size of the current screen</seealso>
</seealsolist>

</chapter>
