<?xml version='1.0' encoding='UTF-8'?>

<?xml-stylesheet href="./_c74_tut.xsl" type="text/xsl"?>

<chapter name="Max MIDI Tutorial 1: Basic MIDI">

<setdocpatch name="01iMIDIBasics" patch="01iMIDIBasics.maxpat"/>
<previous name="basicchapter22">Designing Equations</previous>
<next name="midichapter02">Note Management</next>
<parent name="00_maxindex">Max Tutorials</parent>

<indexinfo category="MIDI" title="Basics">Getting MIDI input and output</indexinfo>

<h1>MIDI Tutorial 1: Basic MIDI</h1>

<h2>Introduction</h2>

<p>This tutorial will cover some of the basics of using the MIDI (Musical Instrument Digital Interface) communications protocol.  We will see how to receive MIDI information from input devices, send it to other hardware or software and select the device that will get the sent information.</p>

<p>While the prevalence of software-only music systems have reduced the need for MIDI devices, the MIDI protocol is still necessary for working with both hardware and software synthesizers and samplers. MIDI is also becoming more widely used by artists working with physical interfaces, since it provides a compact and easy-to-use communications protocol for receiving sensor information and producing control messages for motors and other controllable devices.</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>The <o>midiin</o> and <o>midiout</o> objects</h2>

<p>The tutorial patcher contains a number of small MIDI-specific patches. The left-most patch changes a <o>number</o> box anytime information is received from a MIDI input device (via the <o>midiin</o> object), then sends that information out to another MIDI device (through the <o>midiout</o> object). However, you need to select which device will be used for MIDI input and output.  A simple way select a MIDI device is to double-click on the MIDI object you want to assign.  Double-clicking on the <o>midiin</o> object produces a menu of available MIDI <i>inputs</i>, while double-clicking on the <o>midiout</o> object will display a menu of MIDI <i>outputs</i>.</p>

<p>In addition to the physical MIDI devices that are connected to your computer, you will also see some <i>virtual</i> MIDI ports, depending on your operating system and how it's configured. One of the most useful virtual ports is the built-in synthesizer output – it allows you to generate sounds from MIDI messages without having to connect a physical synthesizer or sampler.</p>

<p>If you select a MIDI input keyboard for the <o>midiin</o> object, and the built-in synthesizer for output, you have a simple MIDI <i>thru</i> application with very little patching.  The <o>midiin</o> and <o>midiout</o> objects expect unformatted <i>raw</i> MIDI messages – if you route the output of the <o>midiin</o> object to a print object, you will see that you have a serial stream of numbers that can be difficult to interpret.  Max contains a number of objects that give us more control over how we use MIDI data inside our program by selecting the <i>types</i> of MIDI events (notes, continuous controllers, etc.) we want to work with.</p>

<h2>All about notes: the <o>notein</o> and <o>noteout</o> objects</h2>

<p>The <o>notein</o> and <o>noteout</o> objects are an example of <i>message-specific</i> MIDI objects. They accept the input of a MIDI stream, and ignore all message types other than <i>note</i> messages - those traditionally caused by playing keys on a MIDI keyboard or pads on a drum trigger.  As with the <o>midiin</o> and <o>midiout</o> objects, we can select the MIDI port by double-clicking on the object.</p>

<p>The second patch shows a basic note display; we can select a MIDI port, then view the notes that are received on that port. The <o>notein</o> object displays three pieces of information for each incoming note message: the note <i>number</i> (or <i>pitch</i>), the note <i>velocity</i> and the MIDI channel it was transmitted on. Play a MIDI keyboard or other controller that creates MIDI note messages, and see how the note information is displayed by the <o>number</o> boxes.</p>

<p>You will notice that there is no “on or off” display to differentiate keys being played (a "note-on") and keys being released (a "note-off") – rather, a note-off message is displayed as a note with a velocity of zero (<m>0</m>). This is a common MIDI convention, and it is used by many Max objects as the preferred way of displaying a note-off message.  We have also used a <o>number</o> box with the <b>Display Format</b> attribute set to <m>MIDI</m>. This provides us with an easily-read cue as to the pitch of the note that is being played.</p>

<p>The next patch is a simple example of the reverse process: <i>generating</i> note messages to be played by a MIDI device. The <o>noteout</o> object expects <m>pitch</m>, <m>velocity</m>, and <m>channel</m> numbers to be received in its left, center, and right inlets, respectively.  The left inlet (<m>pitch</m>) is <i>hot</i>, while the others are cold.  In this case, we use Max’s ability to decode a <i>list</i> of messages as the input to all of the inlets of an object.  Thus, the <o>message</o> box labeled <m>64 100 0</m> is treated like three separate messages to each inlet: they are “channel 0”, “velocity 100” and “note 64”.  This will send a note-on message for pitch <m>64</m> (<m>E3</m>) at a velocity of <m>100</m> to channel <m>0</m>.  The second <o>message</o> box is almost the same, but uses a velocity of <m>0</m> – which will generate a note-off message.  Double-click on the <o>noteout</o> object, and select a valid MIDI output device.  Then click on the first <o>message</o> box.  You should hear the device sound the note.  It should sustain until you hit the second <o>message</o> box, which will turn it off.  This is a basic MIDI note event structure, and can be used as the basis for generating MIDI messages that play music.</p>

<h2>Controllers and port selection: <o>ctlin</o> and <o>ctlout</o>, and <o>midiinfo</o></h2>

<p>The right-most patch is a simple version of an application that <i>remaps</i> MIDI controller data: it changes any modulation wheel control value (by convention, MIDI controller <m>1</m>) into a pan control value (controller <m>10</m>). In this case, the <o>ctlin</o> and <o>ctlout</o> objects are used for receiving and sending MIDI continuous controller messages.  These are message-specific MIDI objects dedicated to MIDI controller values which are often sent by MIDI fader boxes and knob/rotary controllers, as well as more music-centric interfaces such as keyboard sustain pedals. As with the other objects, you can double-click them to set a MIDI port for use.</p>

<p>The incoming values are used to set up the control system. The patch uses the incoming controller number (the middle outlet of <o>ctlin</o>) to determine the routing of a graphical gate (<o>gswitch2</o>) object. When controller <m>1</m> is seen, the <o>==</o> object outputs a <m>1</m>, which sets the the switch to allow the controller value (the <i>left</i> outlet) to go into the subsequent <o>message</o> box. This value is then assembled into a valid message for <o>ctlout</o>, which will send the value (assigned to MIDI controller <m>10</m> on channel <m>0</m>) to the selected MIDI output.</p>
<techdetail>
	This is admittedly the hard way to watch for control 1 messages. If we give an argument of 1 to a <o>ctlin</o> object, it will only respond to control 1 messages.
</techdetail>

<p>There is a little extra programming attached to the <o>ctlout</o> object: a <o>button</o> is connected to a <o>midiinfo</o> object, which loads a <o>umenu</o> object with some data when it receives a <m>bang</m> message.  This data is fed into the <o>ctlout</o> object. What is this about?</p>

<p>Sometimes, you don’t want the end user of your patch to have to double-click the MIDI objects to set a port, or you may want to set the port on many objects simultaneously. The <o>midiinfo</o> object provides a lot of information on your current MIDI setup, which you can use to set the port information on your MIDI objects. In this case, when the <o>midiinfo</o> object receives a <m>bang</m> message in its <i>left</i> inlet, it produces the names of the available MIDI <i>output</i> ports currently defined on your system. This is used to load the <o>umenu</o> object with your current MIDI setup.  When you select an item from the menu, it is sent to the <o>ctlout</o> object as an identifier of the port you want to use.</p>

<p>This is just one of the uses of the <o>midiinfo</o> object; we will see other uses in upcoming tutorials. If you want to query your computer for information about available MIDI ports and available controllers, review the <o>midiinfo</o> help files and reference pages. </p>

<h2>Creating a basic MIDI monitor</h2>

<p>In order to exercise the MIDI message tools, let’s create a basic MIDI monitoring application. We can start by creating a version of the <o>midiinfo</o>/<o>umenu</o> combination that will show available MIDI <i>inputs</i>.  In order to see the inputs, we need to send a <m>bang</m> message into the <i>right</i> inlet of the <o>midiinfo</o> object.  You can do this with a <o>button</o> object, or use a <o>loadbang</o> object so the menu will be loaded each time the patch is loaded.</p>

<p>In order to track both note and controller information, we can send the output of the <o>umenu</o> to both a <o>notein</o> and <o>ctlin</o> object.  This sets up the two objects to receive MIDI information from the same MIDI port. Now, connect <o>number</o> boxes to each of the outlets of the <o>notein</o> and <o>ctlin</o> objects. We can probably have a single <o>number</o> box share the “channel” messages, since we won’t have notes and controllers being generated at the same time. Finally, changing the note <o>number</o> box to use a <b>Display Format</b> of MIDI will allow us to easily see the key of the depressed note.</p>

<p>While this is a fairly simple MIDI monitor application, it is the sort of application that can help you debug a complex MIDI setup. You can expand this patch by using other MIDI message-specific objects, such as <o>pgmin</o> (MIDI program changes) and <o>bendin</o> (MIDI pitch bend messages), to see even more of the received MIDI data stream.</p>

<h2>Summary</h2>

<p>The basic contents of an incoming MIDI stream can be retrieved using the <o>midiin</o> object, and a MIDI stream can be sent using the <o>midiout</o> object. However, in most cases, it is easier to deal with individual message types using their message-specific objects. The <o>notein</o>/<o>noteout</o> and <o>ctlin</o>/<o>ctlout</o> objects provide an easy way to deal with messages without having to decode the raw MIDI input or create raw MIDI output.</p>

<p>The <o>midiinfo</o> object, is a powerful tool for examining the current state of your MIDI setup. Using it to load a menu of available MIDI ports can help make your patch easier to use and is also the best way to set a number of objects simultaneously.</p>

<seealsolist>
<seealso name="midiin">Output incoming MIDI bytes</seealso>
<seealso name="midiout">Sent integers as raw MIDI data</seealso>
<seealso name="notein">Output incoming MIDI note messages</seealso>
<seealso name="noteout">Send MIDI note-on and note-off messages</seealso>
<seealso name="ctlin">Output incoming MIDI control change messages</seealso>
<seealso name="ctlout">Send MIDI control change messages</seealso>
<seealso name="midiinfo">Set a pop-up menu with current names of MIDI devices</seealso>
<seealso name="gswitch2">Switches the right inlet between two outputs</seealso>
</seealsolist>

</chapter>
