<?xml version='1.0' encoding='UTF-8'?>

<?xml-stylesheet href="./_c74_tut.xsl" type="text/xsl"?>

<chapter name="Max MIDI Tutorial 5: MIDI Advanced Sequencing">

<setdocpatch name="05iMIDIAdvancedSequencing" patch="05iMIDIAdvancedSequencing.maxpat"/>
<previous name="midichapter04">Basic Sequencing</previous>
<next name="datachapter01">Data Viewing</next>
<parent name="00_maxindex">Max Tutorials</parent>

<indexinfo category="MIDI" title="Advanced Sequencing">Recording and manipulating MIDI sequences</indexinfo>

<h1>MIDI Tutorial 5: MIDI Advanced Sequencing</h1>

<h2>Introduction</h2>

<p>In this tutorial, we will expand on our use of the <o>seq</o> object by recording new MIDI data, loading existing files and writing sequences to disk.  We will also use the second outlet of the <o>seq</o> object to provide looped playback of the current sequence.</p>

<p>Working with existing sequences is useful, but being able to record new sequences within Max allows us to create and store sequences and phrases for later use. This sequencing tutorial is focused on recording, the use of the file input/output functions, and the ability to use the <o>seq</o> object output to loop playback.</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>Recording a sequence</h2>

<p>Our tutorial has a number of regions with different patcher logic in each. The leftmost region (labeled <b>Recording</b>) shows the basic setup for selecting MIDI input that we saw in earlier MIDI tutorials. The <o>seq</o> object expects a raw MIDI stream, so our recording input comes directly from a <o>midiin</o> object.</p>

<p>Select a valid MIDI input device from the <o>umenu</o> so we can record MIDI data into our own sequence.  In addition, double-click the <o>midiout</o> object and select a valid MIDI output device.  In addition to feeding the <o>seq</o> object, the <o>midiin</o> object is also sending data to the <o>midiout</o> (via <o>midiflush</o>), so you should be able to test your connections by playing on your MIDI controller.</p>

<p>Clicking on the red <m>record</m> message will start the recording of MIDI data.  Click on this <o>message</o> box, then play some MIDI data in from a connected controller.  Clicking the <m>stop</m> message will stop recording; we can then hit <m>start</m> to hear the playback of the MIDI we just recorded.  Since the <o>seq</o> object is recording the complete MIDI stream, we can also send continuous controllers, pitchbend messages, program changes or any other MIDI data – it will be recorded and played back just like note messages.  As in the last tutorial, we use the <o>midiflush</o> object in between <o>seq</o> and <o>midiout</o> to silence any remaining note-on messages when we <m>stop</m> our sequence.</p>

<h2>Loading and saving a sequence</h2>

<p>Once we’ve recorded a sequence, we may want to save the results.  This is where the file-handling messages come into play. The top-right area (labeled <b>File Handling</b>) shows three messages that <o>seq</o> understands for saving and loading files; in this case, the <m>write</m> message is our friend. Clicking on the <m>write</m> message will open a <b>Save</b> dialog that allows us to store the sequence to disk. Click on the <m>write</m> message, and save your newly recorded sequence to disk.</p>

<p>Reading the file from disk can take two forms: we can explicitly choose a file to read, or open a dialog to select from any MIDI file stored on disk.  Explicit selection is done using a file name argument to the <m>read</m> message.  Sending the <m>read</m> message without an argument will provide an <b>Open</b> dialog for selecting any available MIDI file.  Click on the <m>read seq_sc.midi</m> message to load our test sequence file; to prove that we have changed the current sequence, play it using the <m>start</m> message. Click on the no-argument <m>read</m> message and select your saved sequence.  Clicking the <m>start</m> message should play back our sequence as we performed it earlier.</p>

<h2>Looping playback</h2>

<p>In the previous tutorial, we saw that the right outlet of <o>seq</o> produces a <m>bang</m> message when a sequence is done with playback. Using this information, we can perform a common sequencing task: looping playback of our sequence.  The bottom-right area (labeled <b>Loop Playback</b>) shows a <o>gswitch2</o> (Graphical Gate) that will route the <m>bang</m> message back to the <m>start</m> message whenever the <o>toggle</o> object is checked. This is a simple use of the right-hand output for sequencer control; it could also be used for loading another sequence, starting a second <o>seq</o> object or even starting another process in Max entirely.</p>

<h2>Summary</h2>

<p>Recording a raw MIDI stream is one of the strengths of the <o>seq</o> object; it can grab and store almost any type of MIDI message. Using the read and write commands, we can save and then restore any recorded MIDI data, whether received from a MIDI controller or generated from Max. We can also use the right outlet of <o>seq</o> to control playback parameters or even start completely new Max processes.  The <o>seq</o> object should be seen as a robust, useful tool for many sequencing tasks.</p>

<seealsolist>
<seealso name="seq">MIDI sequencer</seealso>
<seealso name="midiflush">Send note-offs for hanging note-ons in raw MIDI data</seealso>
</seealsolist>

</chapter>
