<?xml version='1.0' encoding='UTF-8'?>

<?xml-stylesheet href="./_c74_tut.xsl" type="text/xsl"?>

<chapter name="Max Basic Tutorial 19: Timing">

<setdocpatch name="19mTiming" patch="19mTiming.maxpat"/>
  <previous name="basicchapter18">Data Collections</previous>
<next name="basicchapter20">Presentation Mode</next>
<parent name="00_maxindex">Max Tutorials</parent>

<indexinfo category="Basics" title="Timing">Scheduling events</indexinfo>

<h1>Tutorial 19: Timing</h1>

<h2>Introduction</h2>

<p>In this tutorial, we will be working with <i>time</i> – objects that deal with time, and a set of objects that work with a metrical timing system built into Max. Additionally, we will look at some decision-making objects that use logic operations to determine program flow.</p>

<p>As we’ve seen in earlier tutorials, the use of time is an important part of working with Max programs; time-based objects like <o>metro</o> can help create generative music and drawing actions, and can be used in conjunction with user actions to create playable programs. The <o>transport</o> objects in Max provide a centralized musical timing system that can provide play/stop controls, bar/beat feedback and can even cause events to fire at specific points in time. All of this is built on the ability to define specific times and time durations in metrically-significant ways.</p>

<p>In addition to the time functions, we also cover some of the Boolean comparison objects – objects that create logical results based on comparing and testing input values. These are especially useful when working with time-based objects, since you will often want to make operational decisions based on comparisons between the current time and some desired time or sub-time function.
</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>Some new objects...</h2>

<p>The left-hand side of the tutorial has five small patches that show the operation of a handful of new objects that deal with time in Max. Patch <b>1</b> provides an example of the <o>pipe</o> object, which can be used to delay the passage of any message or list for a certain amount of time.  The argument provides the default delay time – in this case, <m>1000</m> milliseconds (1 second). If you change the value of the <i>left</i> number box, you will see your changes reflected in the output 1 second later.  The delay time can be changed with a value sent to the <i>right</i> inlet – an integer value is used to set the number of milliseconds of delay time.  Change the right-hand number box to <m>2000</m>, then change the left input – you will now see the changes occur after a 2 second delay.</p>

<p>The next small patch, labeled <b>2</b>, is an example of the <o>delay</o> object.  This is very similar to the <o>pipe</o> object, except that it is specifically tailored to delay <m>bang</m> messages.  Like <o>pipe</o>, the <o>delay</o> time can be changed by sending an integer value into the right inlet. Both the <o>delay</o> and <o>pipe</o> objects will respond to a <m>stop</m> message to  abort pending output.</p>

<p>Patch <b>3</b> shows the <o>clocker</o> object, which is closely related to the <o>metro</o> object.  The primary difference between <o>clocker</o> and <o>metro</o> is the output: while the <o>metro</o> object produces <m>bang</m> messages at regular intervals,  <o>clocker</o> outputs the overall <i>elapsed time</i> (in milliseconds) since it was started with a <m>bang</m>. Clicking on the <m>stop</m> message will stop the <o>clocker</o>, while sending a new <m>bang</m> message to clocker will restart the timer and the elapsed time measurement.</p>

<p>The patch labeled <b>4</b> demonstrates another object that counts elapsed time: the <o>timer</o> object.  This object takes <m>bang</m> messages in both inlets; the <i>left</i> inlet starts the interval to be timed, and the <i>right</i> inlet stops the interval.  The <o>timer</o> object is therefore equivalent to a <i>stopwatch</i>; click the left-hand <o>button</o> to start the clock, wait a few seconds, and then click the right-hand <o>button</o> to see how much time has elapsed.  Note that <o>timer</o> is a slightly unusual Max object in that its <i>right</i> inlet is <i>hot</i> (produces output), not its <i>left</i> inlet.</p>

<p>The fifth patch shows some of the most common <i>comparison</i> objects in action. The <o>&gt;</o> (greater-than) object outputs a <m>1</m> if the incoming number is greater than the number used as an argument, or a <m>0</m> if it is not; the object therefore performs a test, reporting TRUE (<m>1</m>) or FALSE (<m>0</m>). The <o>&lt;</o> (less-than) object is the inverse, reporting true or false (<m>1</m> or <m>0</m>) depending on whether the input is lower than the object's argument.  The use of <m>1</m> or <m>0</m> to indicate true or false are in common use across programming languages, providing the foundation of <i>Boolean</i> operations. The remaining logical objects will probably be more familiar to programmers.  The <o>==</o> (equals) object reports a <m>1</m> only when the incoming number is <i>equal</i> to the argument, while the <o>!=</o> (not equals) is its inverse – it reports a <m>1</m> when the incoming number is not equal to the argument.  As with many Max objects, a new comparison operator can be supplied by a number sent into the <i>right</i> inlet of the object.</p>

<p>The final two logical operators are <o>&amp;&amp;</o> (logical AND) and <o>||</o> (logical OR).  In the first case, the <o>&amp;&amp;</o> object will output a <m>1</m> only if <i>both</i> numbers sent into the left and right inlets are <i>non-zero</i>; otherwise, it reports a <m>0</m>.  In the second case, the <o>||</o> object will output a <m>1</m> when <i>either</i> the left or the right inlets are sent non-zero values. If both are <m>0</m>, this object will output a <m>0</m>.</p>

<p>Change the <o>number</o> box at the top of the patch containing these objects and you'll see how they behave.  Notice how the only time all six operators yield a <m>1</m> is when the input value is <m>10</m>.</p>

<h2>Metrical Timing, Part 1</h2>

<p>The large patch on the right (labeled <b>6</b>) is a performance patch that uses all of these new objects (and some old ones) within the context of the <i>metrical timing</i> system. Start the patch (using the large yellow <o>toggle</o> at the top); you will hear a steady beat out of your default MIDI synthesizer, with variations that occur at different times throughout the performance.  The performance is looped, rerunning itself every 16 measures.  In order to understand this rather complex patch, we first need to understand the metrical time system.</p>

<p>Metrical timing is based on a central time-keeping object – the <o>transport</o> – and its ability to broadcast time information to other objects that are expecting timing information. Many of these “listening” objects are already familiar to us (<o>metro</o>) or have been introduced in this tutorial (<o>clocker</o>).  We have used these objects in self-clocking mode, where they use a timebase expressed in milliseconds to produce output at regular times. When used in conjunction with the <o>transport</o> object, however, time is expressed using <i>metrical notation</i>: values such as <m>4n</m> (for a quarter-note) can be used as an alternative to millisecond timing notation.</p>

<p>The <o>transport</o> object is the central time keeper, and provides time in <i>bar/beat/tick</i> format – all driven by a <i>tempo</i> and a <i>time signature</i>.  Hence, if a transport is set to run with a time signature of <m>4/4</m> and at a tempo of <m>120</m> beats-per-minute, each beat will take 500 milliseconds, and a measure will take 2 seconds (2000 milliseconds).  In order to see all the ways the <o>transport</o> controls this patch, we need to break it down into sections.</p>

<p>Our “big checkbox” starts the process – mainly because it starts the <o>transport</o>.  The <o>transport</o> accepts a <m>1</m>/<m>0</m> message for start and stop, which is perfect for control by a <o>toggle</o>.  The <o>toggle</o> also starts the <o>metro</o> to the left (part of the “red” section of the patch), which uses <m>4n</m> as the timing argument.  In the past, we’ve used milliseconds for the <o>metro</o> object's timing, but in this case, the <m>4n</m> notation means that it will send out a <m>bang</m> every <i>quarter note</i>, based on the timing broadcast by the transport.  The output of this <o>metro</o> goes to two places: to a <o>message</o> box that creates a MIDI note <m>70</m> (via <o>makenote</o>), and back to the <o>transport</o>.  Why does it get routed to the <o>transport</o>?  Because a <m>bang</m> message received by the <o>transport</o> will cause it to output the current time, which we display in three <o>number</o> boxes connected to the first three outlets of the object.  This time is displayed in bars, beats and ticks, respectively.  There are <m>480</m> ticks per beat (480 pulses-per-quarter-note, or <i>ppq</i>, by default).	</p>

<p>Next, let’s look at the “green” section of the patch.  This uses the bar count (as output by the <o>transport</o>), and uses the <o>&gt;</o> and <o>&lt;</o> objects to determine if we are between bars <m>4</m> and <m>8</m>.  If we are in that range, it turns on another <o>metro</o> that is clocked at <m>16n</m> (16th notes). This generates a random number used to create one of four values (using the <o>random</o> and <o>select</o> objects); these numbers are sent to the <o>makenote</o> object to generate MIDI notes.  That value is also sent to a <o>pipe</o> object with an argument of <m>4nd</m>, which delays the values by a dotted-quarter note.  This output has <m>7</m> added to it, causing output that, when sent to the MIDI synthesizer, sounds a perfect fifth above the original.  The result is a rapid-fire set of notes for a four-bar segment of the loop.</p>

<p>Let’s now dig into the “blue” section, which is the part of the patch that forces the sequence to <i>loop</i>.  The head of this section of the patch is a new object: <o>timepoint</o>.  The <o>timepoint</o> object takes a metrical time as an argument; its sole purpose is to generate a <m>bang</m> message when that metrical time is reached by the <o>transport</o>. In this case, when we reach the beginning of the measure <m>17</m> (i.e. after sixteen measures), a four-note chord of numbers is sent to the <o>makenote</o>, then a <m>bang</m> message is sent all the way back to a <o>message</o> connected to the right inlet of the transport.  This message (<m>0.</m>) tells the transport to immediately jump to tick <m>0.</m> of the sequence, i.e. bar 1, beat 1 and tick 0. This is how we force the sequence to loop: every time we hit the 17th bar, we are immediately sent back to the beginning.</p>

<h2>Metrical Timing, Part 2</h2>

<p>Next, we should decode the “brown” section of the patch. It uses <o>key</o> and <o>select</o> objects to watch for the space bar (ASCII <m>32</m>) being depressed. When this happens, a <o>timer</o> object receives a <m>bang</m> on <i>both</i> inlets.  Because Max objects transmit messages in right-to-left order, hitting the space bar simultaneously stops the <o>timer</o> with a <m>bang</m> to the <i>right</i> (and outputs the elapsed time) and restarts it for another measurement with a <m>bang</m> to the <i>left</i>. The object outputs the elapsed time in milliseconds; we divide <m>60000</m> (the number of milliseconds in a minute) by the time to get a bpm (beats-per-minute) value.</p>

<p>However, we want this bpm value to fit in a sensible range, so we use the <o>split</o> object, and only use values that are between <m>30.</m> And <m>200.</m>. If we are within this range, <o>split</o> will output the number from its <i>left</i> outlet, and the value gets used to create a <m>tempo</m> message that is sent to the <o>transport</o>.  This will immediately change the playback tempo of the sequence, and will change the relative speeds of many (but not all) of the objects used to create our sequence.  If the <o>timer</o> outputs a value outside of the <o>split</o> object's range (for example, if we haven't hit the space bar in a while), it will be sent out the right outlet of the object and discarded.</p>

<p>One of the reasons that not all of the sequence is changed is because some of the patch functions use standard (millisecond) timing rather than metrical time.  The right-most (“blue-and-purple”) section of the patch is a good example.  A <o>timepoint</o> object is fired when the transport time is at bar <m>6</m>, beat <m>3</m>.  This starts a <o>clocker</o> that runs for <m>10</m> seconds (10,000 milliseconds), producing a two-note chord every quarter-note (<m>4n</m>).  The <o>timepoint</o> object also sends a <m>bang</m> to a <o>delay</o> <m>3000</m> object, which produces a two-note chord <m>3000</m> milliseconds after it receives the message.  At a very slow tempo, this may occur only a few beats after the <o>timepoint</o> is fired, while at rapid tempo, if may take many bars.  This is a good example of how metrical and standard time can be mixed and matched for interesting generative results.  Note that the <o>clocker</o> object's running time is compared against a <o>&gt;</o> object to switch itself off after 10 seconds... a <o>timer</o> object tracking the interval between the start and stop of the <o>clocker</o> object confirms this.</p>

<h2>The Importance of Overdrive</h2>

<p>One factor that could cause variant behavior in this patch is the <b>Overdrive</b> setting, found in the <b>Options</b> menu.  If Overdrive is checked (on), event processing and calculations are given priority over screen-drawing and graphical processes.  When Overdrive is unchecked (off), drawing processes get the same priority as event handling.  Depending on the nature of your patch, Overdrive may provide better performance for time-critical event handling tasks.</p>

<p>Since our tutorial patch is, in fact, time-critical, it will perform best with Overdrive turned on.  If Overdrive is off, you may see that the transport output display doesn’t always occur on the <m>0</m> tick, and you may find that playback gets jerky when the density of notes is high or you perform other tasks on your computer (such as opening another patcher in Max).  Modern computers are incredibly fast, but our ears are very sensitive to timing discontinuities.  Therefore, when working on patches that sequence events in a time-critical way, it is normally advisable to turn Overdrive on.</p>

<h2>Summary</h2>

<p>In this tutorial, we covered an extensive new system within Max: the <i>metrical timing</i> system.  This provides a centralized timing control for your Max patch, coordinating the generation of events and easing the creation of musical sequences. We also learned about several new time-based object, including <o>pipe</o>, <o>delay</o>, <o>clocker</o>, and <o>timer</o>, which can be used (either with or without metrical timing) to create sequenced actions.  Finally, we saw the use of boolean comparison and logical operator objects to aid in decision making within a complex patch.</p>

<seealsolist>
<seealso name="pipe">Delay line for numerical values</seealso>
<seealso name="delay">Delay a bang</seealso>
<seealso name="clocker">Metronome that reports elapsed time</seealso>
<seealso name="timer">Report elapsed time between two events</seealso>
<seealso name="&gt;">Comparison operator (greater than)</seealso>
<seealso name="&lt;">Comparison operator (less than)</seealso>
<seealso name="==">Comparison operator (equals)</seealso>
<seealso name="!=">Comparison operator (not equals)</seealso>
<seealso name="&amp;&amp;">Logical AND</seealso>
<seealso name="||">Logical OR</seealso>
<seealso name="transport">Control a master clock and report time values</seealso>
<seealso name="timepoint">Output a bang when transport reaches a specific time</seealso>
<seealso name="split">Look for a range of numbers</seealso>
</seealsolist>

</chapter>
