<?xml version="1.0" encoding="utf-8" standalone="yes"?>

<?xml-stylesheet href="./_c74_tut.xsl" type="text/xsl"?>

<chapter name="MSP MIDI Tutorial 1: Mapping MIDI to MSP">
	<previous module="msp"  name="09_dynamicschapter03"></previous>
	<next module="msp" name="10_midichapter02"></next>
	<parent name="00_mspindex">MSP Tutorials</parent>

<setdocpatch name="01iMappingMIDItoMSP" patch="01iMappingMIDItoMSP.maxpat"/>

<h1>
	MIDI Tutorial 1: Mapping MIDI to MSP
</h1>

	<p>
		In this group of tutorials, we'll look at different strategies for integrating
		MIDI control into MSP patchers. Commercial hardware synthesizers and samplers
		typically implement MIDI in some form or another in fairly standardized ways;
		software systems that mimic these devices (such as Max patches) can use all
		the same routing and mapping systems to successfully control our sound-producing
		systems with MIDI controllers.
	</p>

	<p>
		A review of the tutorials that cover
		<link type="tutorial" module="max" name="10_midichapter01">MIDI objects</link>
		in Max may be useful before reading these tutorials.
	</p>

	<bullet>
		To use the tutorial patchers in this section of the tutorial, make sure you
		have a correctly configured MIDI controller connected to your computer. The
		tutorials in this section use a variety of MIDI messages as example input; if
		your controller lacks any of these features, you can simulate their input with
		user interface objects in the tutorial.
	</bullet>

	<h2>
		MIDI, Redux
	</h2>

	<p>
		The MIDI standard was developed to enable the control of electronic music devices
		through the use of a simple serial communications standard that would be easy to
		implement on any platform and could be used by anyone. As music performance
		equipment has evolved in the 25 years since, MIDI has remained a standard-issue
		interface protocol for nearly all music-making equipment, whether we're talking
		about hardware or software. Whether you're interested in routing a keyboard,
		slider box, or software sequencer in Max, you need to deal with translating
		MIDI messages into numbers that make sense for MSP.
	</p>

	<bullet>
		Take a look at the tutorial patcher. The patcher logic consists of an FM synthesis
		abstraction called <m>simpleFM~</m> controlled by a number of signal objects, the
		values of which are set by floating-point <o>number</o> boxes. Turn on the audio
		in the patcher and turn up the <o>gain~</o> slider. Familiarize yourself with the
		way the audio portion of the patcher works by trying different values is in
		the <o>number</o> boxes labeled <m>1</m> through <m>5</m>.
	</bullet><br/>

	<p>
		Our patcher uses frequency modulation synthesis as we looked at earlier in the
		tutorials, with the addition of a circuit that provides <i>vibrato</i> to the
		main frequency of the synthesizer. The <o>cycle~</o> object labeled <m>Vibrato rate</m>
		is scaled by the <m>Vibrato depth</m> control to oscillate the main frequency (the
		output of the <o>line~</o> object on the right of the patcher. Unlike a vibrato circuit
		which <i>adds</i> the output of a modulating oscillator to a constant frequency, this
		patcher logic <i>multiplies</i> the constant frequency by the output of the oscillator
		treated as an exponent by the <o>pow~</o> object. A vibrato depth of <m>1</m>,
		therefore, has the equivalent of turning off the vibrato (raising <m>1</m> to
		any power yields <m>1</m>, which leaves our main frequency untouched). A depth
		of <m>2</m> scales the <o>cycle~</o> object to the range of <m>0.5</m> to <m>2.0</m>,
		scaling the main frequency up and down by an octave.
	</p>

	<p>
		The <m>Octave bend</m> value on the left allows us to use a value as a
		frequency multiplier in a range where each unit of value transposes the
		pitch by an octave. Like the vibrato circuit, it uses an exponential
		scaling logic; a value of <m>0.</m> as the bend amount raises <m>2</m> to
		the power of <m>0.</m>, yielding a multiplier of <m>1.</m> for the frequency.
		An octave bend of <m>1.</m> yields a multiple of <m>2.</m>; a bend of <m>-1.</m>
		yields a multiple of <m>0.5</m>.
	</p>

	<h2>
		Scaling MIDI for signal control
	</h2>

	<bullet>
		Configure a MIDI continuous controller connected to your computer so that
		it sends CC#1 on any MIDI channel (the modulation wheel on a keyboard
		controller is a good candidate for this mapping). From the <o>umenu</o> object
		labeled <m>A</m>, select option <m>1</m> ('Octave Pitch Bend'). Move your
		controller and listen to and watch the result.
	</bullet>

	<p>
		The Max <o>ctlin</o> object transmits our controller through the <o>slider</o> object
		in the patcher and from there through a <o>scale</o> object. The <o>gate</o> object
		allows us to route the controller to different destination <o>number</o> boxes
		in the patcher. Notice that, with the default scaling, our MIDI number
		range (<m>0</m> to <m>127</m>) maps to a floating-point value between <m>0.</m>
		and <m>1.</m>
	</p>

	<p>
		When working with MIDI messages, the general rule of thumb is that they
		transmit integers which are typically in the range of <m>0</m> to <m>127</m>.
		While these values make sense within the MIDI domain, when applied to signal
		values these ranges are usually wrong. When we want to control an oscillator,
		we need to think in terms of <i>frequency</i> (cycles per second or Hz), not
		the <i>pitch</i> that MIDI note numbers refer to. When thinking about <i>amplitude</i>,
		we might want these numbers scaled between <m>0.</m> and <m>1.</m> to control the output
		of a <o>*~</o> object. The <o>scale</o> object is an important utility object in Max
		for performing these calculations.
	</p>

	<bullet>
		In the <o>preset</o> object at the right of the patcher, click the first circle.
		The values for the <o>scale</o> object should change so that the <m>xmin</m>
		and <m>xmax</m> values read <m>0</m> and <m>127</m>; the <m>ymin</m> and <m>ymax</m>
		values should read <m>0.</m> and <m>2.</m> Move the MIDI controller and notice the
		difference.
	</bullet>

	<p>
		Using the settings controlled by this preset, we can take a MIDI controller
		and use it to give us an octave bend range of two octaves. Setting the
		controller to its lowest value will leave our FM synthesizer untransposed.
		Moving it all the way up will cause the frequency to output two octaves
		higher, at <m>880</m> Hz.
	</p>

	<bullet>
		Click in the <o>number</o> box labeled <m>ymin</m> and enter the value of <m>-2.</m>
		Move your MIDI controller and notice the difference.
	</bullet>

	<p>
		The <o>scale</o> object can map any range to any other range, including
		negative values. Now our controller leaves the synthesizer untransposed in
		the <i>middle</i> of its range; moving it downwards and upwards gives us
		two octaves of shift in either direction.
	</p>

	<bullet>
		Click the other <o>preset</o> states available and try them out; each one
		contains a different mapping and routing:
	</bullet>

	<ol>
		<li>Octave Pitch Bend. The controller outputs values from <m>0.</m> to <m>2.</m></li>
		<li>Vibrato Rate. The controller outputs values from <m>2.</m> to <m>0.</m>, inverting
		the scaling so that it runs from high to low.</li>
		<li>Vibrato Depth. The controller scales its input to run from <m>1.</m> to <m>100.</m></li>
		<li>Modulation Index. The MIDI gets scaled from <m>0.</m> to <m>20.</m></li>
		<li>Amplitude. The MIDI goes from <m>0.</m> to <m>1.</m> with an <i>exponential scaling</i> factor of <m>1.06</m>. Using this value means that upper values of the MIDI range control a wider part of the mapping than lower values.</li>
	</ol>

	<p>
		Notice that different MSP parameters in our patch require different scalings,
		none of which directly correspond to the default values of MIDI controllers.
	</p>

	<bullet>
		In patcher area <m>B</m>, look at the <o>number</o> boxes below the
		<o>notein</o> object. Play some notes on a MIDI keyboard connected to
		your computer.
	</bullet>

	<p>
		When dealing with pitch in MIDI values, a utility Max object called <o>mtof</o> allows
		us to convert directly to frequency. The <o>stripnote</o> object in this patch is
		necessary to prevent double-triggering of values due to the note-off events
		generated by the keyboard. Notice that because our MSP synth in this patch is
		monophonic, only one note can be sounded at a time.
	</p>

	<h2>
		Summary
	</h2>

	<p>
		Working with MIDI controllers within the Max environment is extremely simple.
		Using these values with MSP patches requires an understanding of how to <i>map</i> the
		MIDI values appropriately for the different parameters of a digital signal
		network. The Max <o>scale</o> and <o>mtof</o> objects are incredibly useful
		for performing this function, and allow you to map MIDI input into
		appropriate ranges for any number of applications.
	</p>

	<seealsolist>
		<seealso name="scale">Map an input range of values to an output range</seealso>

		<seealso name="mtof">Convert a MIDI note number to frequency</seealso>

	</seealsolist>

</chapter>


