<?xml version='1.0' encoding='UTF-8'?>

<?xml-stylesheet href="./_c74_tut.xsl" type="text/xsl"?>
<chapter name="Tutorial 38: Basic Performance Setup">
<setdocpatch name="38jPerformanceSetup" patch="38jPerformanceSetup.maxpat"/>

<previous name="jitterchapter37">Geometry Under the Hood</previous>
<next name="jitterchapter39">Spatial Mapping</next>
<parent name="jitindex">Jitter Tutorials</parent>



<h1>Tutorial 38: Basic Performance Setup</h1>
<p>In this chapter, we will walk through the various steps involved in setting up your Jitter patch for live performance. Along the way, we'll look at using a second video monitor for fullscreen output.</p>

<bullet>Open the tutorial patch.</bullet>
<p>Most of the patches we've looked at so far use the <o>jit.pwindow</o> object for displaying video data. While that object is extremely practical for looking at video inside of your patch, it's not so ideal for live performance (unless you want the audience to see your patch, too). In these situations, we use the <o>jit.window</o> object (which you may remember from way back in <link type="tutorial" module="jit" name="jitterchapter01">Tutorial 1</link> Play A Movie).</p>

<bullet>Click on the <o>message</o> box labeled <m>read dishes.mov</m> and turn on the <o>toggle</o> box labeled <i>Start Movie</i> to begin movie playback. You should see the movie playing in the <o>jit.window</o> object's window.</bullet>
<div>
<techdetail>What's that <m>@noaccel 1</m> attribute typed into the <o>jit.window</o> object? That tells the object that we want to disable OpenGL acceleration. For the purposes of this tutorial, we're going to ignore some of the more advanced features of <o>jit.window</o>&#x2014;in particular, the ability to use Jitter's OpenGL support to improve the appearance and overall speed of video data. Don't worry, though. Those features will be covered in later Tutorial chapters.</techdetail>
</div>
<p>Let's take a look at some of the capabilities of the <o>jit.window</o> object. In addition to moving and sizing the window with the mouse (clicking in the lower right hand corner lets us resize the window manually), we can send messages to the object and cause the window to move or resize, we can remove the window's border, make the window float above all other windows, and we can make the window fill the entire screen.</p>

<illustration><img src="images/jitterchapter38a.png"/>Resizing the <o>jit.window</o> object's window with the mouse.</illustration>
<bullet>Click on the <o>message</o> box labeled <m>size 160 120</m> and see what happens. Just like the <o>jit.pwindow</o> object, the <o>jit.window</o> object automatically scales incoming video to fit the size of the window when it receives the message <m>size 160 120</m>. Click on the <o>message</o> box labeled <m>size 320 240</m> to return to the original size.</bullet>
<bullet>Click on the <o>message</o> box labeled <m>pos 300 400</m> and see what happens. We can move the window to any valid screen position, just by sending it the <m>pos</m> (position) message followed by coordinates. We'll use this feature in a little bit to create an automatic window-placement patch. Click on the message box labeled <m>pos 800 100</m> to return to the original position.</bullet>
<bullet>Turn off the <o>toggle</o> labeled <i>Border On/Off</i> and see what happens. The window's title bar and (on Windows) border should have disappeared. Turn the <o>toggle</o> on to bring the border back.</bullet>
<bullet>Turn on the <o>toggle</o> labeled <i>Float On/Off</i> and see what happens. A floating window "floats" above all non-floating windows, and can never be sent to the back (although another floating window could cover it). Causing a window to float can help you to ensure that your video data is always visible if you have a complex patch with many windows. Turn the toggle off to turn off floating for this window.</bullet>
<bullet>Finally, press the escape key (esc) on your computer keyboard and watch how the window automatically fills the entire screen of your primary monitor. Pressing the escape key again will collapse the window back to its previous size.</bullet>
<p>If you look in the patch, we can see clearly how this last trick functions. We've simply used the Max <o>key</o> object in combination with a <o>select</o> object to listen for the escape key (ASCII key code 27). When that key is pressed, a <m>bang</m> is sent from the <o>select</o> object to the <o>toggle</o>, causing it's state to change from 1 to 0 or from 0 to 1 (depending on what it was before it received the <m>bang</m>). This value is sent to the <o>message</o> box labeled <m>fullscreen $1</m>, causing the messages <m>fullscreen 1</m> and <m>fullscreen 0</m> to be sent to the <o>jit.window</o> object. </p>

<p>Hooking this message up to a key is particularly clever, because once the window is fullscreen, it's sometimes a little tricky to get back to your patch! By the way, the <o>jit.window</o> object has a useful attribute&#x2014;<m>fsmenubar</m>&#x2014;which tells it whether or not to hide the system menu bar when the object is in fullscreen mode. By default, the <m>fsmenubar</m> attribute is set to 1, meaning that the menubar should stay where it is.</p>

<illustration><img src="images/jitterchapter38b.png"/>Toggling the fullscreen state of the <o>jit.window</o> object with the escape key.</illustration>

<h2>Got It Covered</h2>
<p>Most of the time, though, we want to be able to use our patch on one screen, while looking at the output on another. The <o>jit.window</o> object is capable of putting a window on any display attached to your computer (or display device, like a video projector).</p>

<p>As far as your computer is concerned, once you've attached additional displays in a spanning configuration (that means, the displays aren't mirroring one another), you simply have one large display area. Moving windows to a second screen is simply a matter of knowing the correct coordinates.</p>

<illustration><img src="images/jitterchapter38c.png"/>A view of a two-monitor system, with coordinates.</illustration>
<p>Of course, figuring out where your second monitor is located and doing the math is a little bit boring. Luckily, Jitter includes an object, <o>jit.displays</o>, which can figure it all out for you.</p>

<illustration><img src="images/jitterchapter38d.png"/>The <o>jit.displays</o> object in action. </illustration>
<p>Using the <o>jit.displays</o> object to enumerate the number and coordinates of attached monitors is simple. The <m>count</m> message causes the word <m>count</m>, followed by the number of attached displays to be sent from the object's left outlet. In our patch, we're testing to make sure that there are more than one attached display using the <o>&gt;</o> (greater-than) object. If there are, the message <m>coords 1</m> is sent to the <o>jit.displays</o> object, requesting the coordinates of monitor 1 (monitors are numbered starting at 0&#x2014;your primary display is typically monitor 0).</p>

<bullet>Click on the <o>message</o> box labeled <m>count</m>. If you have a second monitor attached to your computer, the <o>jit.window</o> object's window should have appeared on it. By simply using the top-left coordinates of the second display as the basis for a simple calculation (we added 50 pixels to each coordinate to make sure that we avoided any menubars), we were able to send a <m>pos</m> message to the <o>jit.window</o> object and position the window squarely on it.</bullet>
<bullet>Press the escape key again. The window should have gone into fullscreen mode, but this time, on the second display. The <o>jit.window</o> object figures out which display it's on before entering fullscreen mode, to ensure that the correct area of the screen gets covered.</bullet>
<bullet>Press the escape key again to disable fullscreen mode.</bullet>
<p>Of course, the <o>jit.displays</o> object will work with systems comprising more than two displays.</p>

<p>Typically, you'll want to keep a variation of this portion of the patch as part of your performance setup. We've noticed that the position settings for a second (or third) display don't always remain the same from use to use, when connecting different kinds of devices to our computers. Using the <o>jit.displays</o> object to automatically determine the exact position of your attached hardware and move your output window eliminates some of the hassle and worry of working in new venues or with unfamiliar equipment.</p>

<h2>A Little Performance Tip</h2>
<p>Now that we've mastered the <o>jit.window</o> object, let's turn our attention back to the <o>jit.pwindow</o> object. Although the purpose of this tutorial isn't to offer tips and tricks for improving Jitter's performance, the <o>jit.pwindow</o> object is so often used as a small monitor in live performance settings, that a quick look at its effect on playback speed is worth our time.</p>

<bullet>Click the cell in the <o>matrixctrl</o> object labeled <i>jit.pwindow</i> to route the video data to the <o>jit.pwindow</o> object. You should see video in both the <o>jit.window</o> object's window and the <o>jit.pwindow</o> object. Compare the framerates (as reported by the <o>jit.fpsgui</o> object) with the <o>jit.pwindow</o> object enabled and disabled. On our computer, we get around 300 frames per second with the <o>jit.pwindow</o> disable and about 110 frames per second with it enabled. With the <o>jit.pwindow</o> object enabled and the <o>jit.window</o> object turned off, we get around 260 frames per second. Your results will vary widely depending on your computer's operating system and hardware.</bullet>

<bullet>Unlock the patch and open the <o>jit.pwindow</o> object's Inspector.</bullet>
<illustration><img src="images/jitterchapter38e.png"/>The <o>jit.pwindow</o> object's Inspector</illustration>
<p>Do you see the checkbox labeled <i>Onscreen</i> in the <o>jit.pwindow</o> object's Inspector? Uncheck it, and compare framerates with both windows active. On our system, we get around 160 frames per second now&#x2014;nearly 50% faster! <i>Anytime you are using a jit.pwindow object which is smaller than the video being displayed</i> (here, our video is 320x240, and our jit.pwindow is 80x60)<i>, you should turn off onscreen mode</i>. It's a simple rule that will get the best possible speed from your performance patch.</p>

<div>
<techdetail>Onscreen and offscreen modes use different drawing algorithms to do their work&#x2014;data is drawn directly to the display in onscreen mode, while offscreen mode draws the data first to an offscreen buffer and then copies it to the display. The algorithm used in onscreen mode doesn't favor downsampling, so the performance is not as good.</techdetail>
<techdetail>The <i>Use Onscreen</i> setting in the <o>jit.pwindow</o> object's Inspector is saved with the patch, so it need only be set once. You can set the onscreen attribute directly from Max using messages, as well.</techdetail>
</div>
<p>Another strategy for reducing the impact of <o>jit.pwindow</o> objects in your performance patch is to reduce the amount of data being drawn. The Max <o>speedlim</o> and <o>qlim</o> objects are ideal for this purpose, although we're not going to demonstrate their use in this tutorial (the <o>speedlim</o> object is discussed in <link type="tutorial" module="max" name="midichapter03">MIDI tutorial 3</link>).</p>

<h2>Summary</h2>
<p>In this tutorial, we learned a few strategies for overcoming typical challenges in a performance environment: the appearance settings for the <o>jit.window</o> object; the fullscreen mode; the use of the <o>jit.displays</o> object to sense additional display hardware attached to your computer and autoconfigure the position of a <o>jit.window</o> object's window; and the management of the <o>jit.pwindow</o> object's onscreen drawing mode to improve the performance of your in-patch screens.</p>

	<seealsolist>
		<seealso name="jit.displays">Set/query monitor attributes</seealso>
		<seealso name="jit.pwindow">In-Patcher Window</seealso>
		<seealso name="jit.window">Display data in a Window</seealso>
	</seealsolist>
	</chapter>
