<?xml version='1.0' encoding='UTF-8'?>

<?xml-stylesheet href="./_c74_tut.xsl" type="text/xsl"?>

<chapter name="Max MIDI Tutorial 4: MIDI Basic Sequencing">

<setdocpatch name="04iMIDIBasicSequencing" patch="04iMIDIBasicSequencing.maxpat"/>
<previous name="midichapter03">Parsing</previous>
<next name="midichapter05">Advanced Sequencing</next>
<parent name="00_maxindex">Max Tutorials</parent>

<indexinfo category="MIDI" title="Basic Sequencing">Playing back MIDI sequence data</indexinfo>

<h1>MIDI Tutorial 4: MIDI Basic Sequencing</h1>

<h2>Introduction</h2>

<p>This tutorial covers sequencing topics using the Max <o>seq</o> object. We will cover basic sequence setup, and the ability to start and stop playback.  We will also examine a few ways to control the playback speed of a loaded sequence.</p>

<p>MIDI event recording and playback (generally called <i>sequencing</i>) is an important task for many MIDI-based Max programs. There are several objects within Max that can accomplish MIDI sequencing tasks, but the <o>seq</o> object provides the easiest interface while still allowing for the recording and playback of notes, controllers and all other MIDI messages.</p>

<p>One of the things that makes MIDI sequencing so useful is that you can change playback speed with little effect on the output sound; the MIDI recipient is responsible for sound creation, and merely responds to the data stream provided by the sequencer.  Thus, playback rate change is an important part of MIDI sequencing, and is covered in depth with this tutorial.</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>Basic sequencing using <o>seq</o></h2>

<p>Our tutorial patcher contains three regions containing patcher logic routed through a single <o>seq</o> object.  The leftmost area (labeled <b>1</b>) shows the most basic of MIDI sequencing setups. The <o>seq</o> object is initialized with a MIDI sequence name as its argument.  This sequence is loaded when the patch is opened, and is available for playback. The left output of the <o>seq</o> object is the playback output – it sends raw MIDI data into a <o>midiflush</o> object, then to a <o>midiout</o> object.</p>

<p>The <o>midiflush</o> object is used to maintain the note-on/note-off pairings of a <i>raw</i> MIDI stream, and will send note-off messages when it receives a <m>bang</m>.  Using <o>midiflush</o>, we can stop all sounding notes even if the sequence is only half-way through its playback.  Clicking on the <m>stop</m> message at the top of the patch will not only stop the sequence, but will send a <m>bang</m> to <o>midiflush</o> to stop all sounding notes on your synthesizer.</p>

<p>Double-click on the <o>midiout</o> object and select a valid MIDI output device for your system.  Then click on either the <o>button</o> or the <m>start</m> message to begin sequence playback from the start of the sequence; the MIDI sequence loaded into the <o>seq</o> object will play back at its natural speed. When the sequence is done playing, a <m>bang</m> will be sent out the right outlet of the <o>seq</o> object. We have this connected to a <o>button</o> object, giving us a visual indication of sequence completion.</p>

<p>Try each of the messages attached to the <o>seq</o> object, and <m>stop</m> it during playback to hear the results of the <o>midiflush</o> object. Also notice that playback always starts at the beginning of the sequence.</p>

<h2>Changing playback speed</h2>

<p>The next area (labeled <b>2</b>) contains a bit of programming that changes the playback <i>speed</i> of the sequence.  It is based on the argument to the <m>start</m> message: an integer appended to the start message will change the playback rate.  A playback rate of <m>1024</m> represents normal speed; hence, <m>512</m> is half-speed, and <m>2048</m> is double-speed. While this may seem convoluted (in comparison to using a floating-point "rate" from the start), it is easy to calculate the proper value for playback.</p>

<p>In this case, we use a floating-point <o>number</o> box for playback speed, and multiply it by <m>1024</m> to get the number expected by <o>seq</o> object. The number is stored in an int object; then, hitting the <m>Go!</m> <o>message</o> box, we trigger a <o>button</o> that sends the number through a <m>start</m> <o>message</o>, with the properly formatted message sent to <o>seq</o>.  Remember, since the <o>button</o> object converts <i>any</i> message into a <m>bang</m>, the text in the <m>Go!</m> <o>message</o> is arbitrary.</p>

<p>To test this, change the floating-point <o>number</o> box to <m>0.5 </m>and hit <m>Go!</m>.  Change it to <m>2.0</m> and hit <m>Go!</m> again.  You will hear the sequence play at the suggested speed. Using an argument to the <m>start</m> message is an easy way to control playback, but this method can only be used to change playback speed at the start of playback.  To change the rate <i>during</i> playback, you need to work with the <m>tick</m> message.</p>

<h2>Using tick messages for playback</h2>

<p>The rightmost area (labeled <b>3</b>) shows how to change the speed of the sequence <i>during</i> playback.  Two messages are responsible for this: a variation on the <m>start</m> message (with the argument of <m>-1</m>) and the <m>tick</m> message. The <o>seq</o> object has an internal timer that is used by default for playback.  However, when the <m>start</m> message has the <m>-1</m> argument, the transport of the <o>seq</o> object is disengaged from an internal clock and depends on incoming <m>tick</m> messages to advance the sequence.</p>

<p>In our example, we are using a <o>tempo</o> object to transform a <i>bpm</i> (beats per minute) value into the appropriate ticks. The arguments to <o>tempo</o> set a default <m>rate</m> (in this case, <m>120</m> bpm), a default beat <m>multiplier</m> (<m>1</m>) and the number of pulses for each whole note (<m>96</m>, the equivalent of <m>24</m> pulses per quarter note). If you click on the <m>start -1</m> message and then turn on the <o>tempo</o> object with the <o>toggle</o> box, you will hear the sequence play back at <m>120</m> bpm.  Next, change the tempo to something faster (say <m>200</m> bpm) with the <o>number</o> box connected to its second inlet; if you restart the sequence you will hear it play much more quickly.  Turn the <o>tempo</o> object's speed down to <m>50</m> bpm and you will hear very slow playback.  You can also change the <o>tempo</o> object while the sequence is playing, and the playback will change to match your selected speed.</p>

<p>There are many creative options when using the <m>tick</m> message for playback clocking. Using a <o>line</o> object to alter the <o>tempo</o> object would allow you to ramp up playback speed for the duration of the sequence; alternatively, you could create a curve that would cause the playback speed to wobble while it plays. You can also <m>stop</m> the <o>tempo</o> object using the <o>toggle</o> to temporarily pause playback.  When you start it again, playback continues from the point at which you stopped.  Using your own clocking provides much more flexibility than simply changing the arguments to the <m>start</m> message.</p>

<h2>Summary</h2>

<p>The <o>seq</o> object is a robust sequencing tool, allowing you to play back MIDI sequences at a variety of speeds.  This is especially true when you override the internal clock and use the <m>tick</m> message to alter playback in real time. The output of the <o>seq</o> object should be routed through the <o>midiflush</o> object, so that stopping playback will also stop any currently sounding notes.  The <o>seq</o> object is perfect for playback of short loops, note/controller phrases or even complete sequences.</p>

<seealsolist>
<seealso name="seq">MIDI sequencer</seealso>
<seealso name="midiflush">Send note-offs for hanging note-ons in raw MIDI data</seealso>
<seealso name="tempo">Metronome controllable in beats per minute</seealso>
</seealsolist>

</chapter>
