<?xml version='1.0' encoding='UTF-8'?>
<?xml-stylesheet href="./_c74_tut.xsl" type="text/xsl"?>

<!-- Tutorial -->
<chapter name="MSP Plugin Tutorial 2: Instruments">

	<!-- Tutorial Patcher -->
	<setdocpatch name="02vpluginInstruments" patch="02vpluginInstruments.maxpat"/>

	<!-- Define Prev/Next/Parent Tutorial -->
	<previous name="16_pluginchapter01"></previous>
	<next name="16_pluginchapter03">Chorus</next>
	<parent name="00_mspindex">MSP Tutorials</parent>

	<!-- Heading -->
	<h1>MSP Plugin Tutorial 2: Instruments</h1>

	<!-- First section -->
	<h2>Instrument hosting with vst~</h2>
	<p>
		The <o>vst~</o> object can also host your favorite plug-in synthesizers. In the
		previous <link type="tutorial" module="msp" name="16_pluginchapter01">tutorial</link> we
		saw how to load effects plugs and access their various features. To summarize, there are
		four ways to load a plug-in:
		<ul>
			<li>
				Click the plug icon on the left hand toolbar to open the plug browser&#x2014;this
				presents a list of all plug-ins with options to refine the list by type and a full
				search feature. Note that this can be done with the patcher locked or unlocked.
			</li>
			<li>
				In path edit mode, click on the object action menu at the left of the <o>vst~</o>
				object, hold the mouse pointer over the files option and select the desiged plug
				from the list that appears.
			</li>
			<li>
				Send the <o>vst~</o> object a <m>plug</m> message. This may take the name of
				a plug-in as an argument. If none is supplied, a file open dialogue will appear.
			</li>
			<li>
				Add the plug-in name as an argument to the <o>vst~</o> object.
			</li>
		</ul>
	</p>
	<illustration><img src="images/pluginchapter02a.png" /></illustration>

	<caption><i>A typical soft-synth plug</i></caption>
	<p>
		Once loaded, the features of a instrument plug-in are accessible in the same manner as effects
		plugs. The <m>open</m> message opens the instrument interface (the generic option is available),
		and parameter lists and program lists are available as before. The only differenece is
		instruments do not normally process audio. What is required is commands to play notes.
		Since plug-in instruments were first conceived for use in MIDI sequencers, it is not
		surprising that MIDI messages are used to play them.
	</p>
	<p>
		To send a MIDI message to an instrument in <o>vst~</o>, we include it as arguments to a
		<m>midievent</m> message (e.g. if we send the message <m>midievent 144 60 127</m>,
		the instrument should begin playing note 60 (middle C) loudly. This note should
		continue to play until we send the message <m>midievent 128 60 64</m> or
		<m>midievent 144 60 0</m>). Max simplifies the task of sending properly formatted <m>midievent</m>
		messages to an instrument in <o>vst~</o> by using the <o>midiparse</o> and <o>midiformat</o> objects.
	</p>
	<h2>MIDI input and VST instruments</h2>
	<p>
		Raw MIDI input can be automatically formatted as <m>midievent</m> messages using the
		<o>midiparse</o> object. The object receives raw MIDI input from a <o>midiin</o>
		object and outputs the proper <m>midievent</m> messages as a list out of its rightmost
		outlet. Connect that outlet to the <o>vst~</o> object's left inlet, and you're ready to go.
	</p>
	<illustration><img src="images/pluginchapter02f.png" /></illustration>
	<caption><i>External MIDI input</i></caption>
	<h2>MIDI messages and VST instruments</h2>
	<p>
		You may not always be using MIDI input from external sources to send messages to to an instrument
		in a <o>vst~</o> object. The <o>midiformat</o> object also has the ability to format and output
		<m>midievent</m> messages that can be sent to the <o>vst~</o> object's left inlet.
	</p>
	<bullet>
		Open the tutorial patch to see some methods of generating midievents.
	</bullet>

	<h3>Playing MIDI notes</h3>
	<illustration><img src="images/pluginchapter02b.png" /></illustration>
	<caption><i>Formatting midievents to play notes</i></caption>
	<p>
		The blue section of the tutorial patch shows one strategy for generating notes.
		Basically, anything created by <o>makenote</o> is easily handled by a <o>pack</o> and a
		<o>prepend</o> object. The <o>pack</o> object creates a list composed of the MIDI note
		number and a velocity value, which is sent to the leftmost inlet of the <o>midiformat</o>
		object. This method will work with the <o>makenote</o> behavior of sending a 0 velocity for
		note off. Any other note generation system we care to devise will also work, as long as the
		result is a list of note number and velocity.
	</p>
	<h3>MIDI Controllers</h3>
	<p>
		The green section of the tutorial patch shows an example of control change messages. Control change
		messages take the form of a list whose first value identifies the controller number, and whose
		second value represents the controller value. Some MIDI controller numbers are standard for
		all VST instruments (such as controller 64, which is a sustain pedal, or controller 7, which is
		the volume control), while others will vary from instrument to instrument.
	</p>
	<illustration><img src="images/pluginchapter02c.png" /></illustration>
	<caption><i>Formatting control change messages</i></caption>
	<p>
		The MIDI spec states
		that switch-type controls, like pedals, should be turned off with a value of 0 and on with 127.
		Some plug-in instruments will interpret <i>any</i> non-zero value as on, but you can't count on it,
		so we multiply the toggle output by 127 to send the specified message. In addition, one set of
		control numbers that is rigidly defined is the last 8, from 120 to 127. These are
		the <i>channel mode</i> messages, which affect the instrument as a whole. The useful ones
		are <m>120: all sound off</m>, <m>121: reset controllers</m> and <m>123: all notes off</m>.
		The others, which set features like polyphony and local control, don't really apply to plug-ins.
		All three of these messages are sent with a value of 0.
	</p>
	<h3>MIDI Pitch Bend</h3>
	<p>
		The pink section of the tutorial patch shows an example of sending high-resolution (14-bit) pitchbend
		messages. The technique involves adding the <m>hires</m> attribute as an argument to the
		<o>midiformat</o> (i.e. <m>@hires 2</m>), and using the <b>p bender</b> subpatcher to send
		a value to the <o>midiformat</o> object's pitch bend inlet.
	</p>
	<p>
		In this example we're setting the value of the <m>@hires</m> attribute to a value of <m>2</m>,
		which corresponds to traditional 14-bit MIDI pitch bend resolution. The pink section of the
		tutorial patch shows an example of this. In addition to setting the attribute value as an
		argument to the <o>midiformat</o> object, we are using the <b>p bender</b> subpatcher to send
		a value in the range of -8192 to 8191 (i.e. 16384 possible values) to the <o>midiformat</o>
		object's pitch bend inlet.
	</p>
	<illustration><img src="images/pluginchapter02_pbnew.png" /></illustration>
	<caption><i>Using midiformat to send pitch bends</i></caption>
	<p>
		The <b>p bender</b> patcher's contents show one way this can be done.
		The <o>slider</o> object in the tutorial patch has been given a size of 16384 steps
		(this is 14-bit resolution, and is typical of most synthesizers). The range we send
		includes the notion of a "center" position for no pitch bend (0). The <b>p bender</b>
		subpatch contains an additional bit of patching that will return the pitchbend control
		to 0 when it are released (otherwise, any bend is left on forever). The subpatcher takes
		care of this for us by detecting a mouse-up and triggering a <o>line</o> object to
		smoothly return the control to its center. The arrangement of <o>trigger</o>, <o>gate</o>
		and <m>set</m> message is necessary to keep <o>line</o> informed of the  current position
		of the <o>slider</o> so the ramp will start there.
	</p>
	<illustration><img src="images/pluginchapter02_pbnew2.png" /></illustration>
	<caption><i>Pitchbend control logic</i></caption>
	<p>
		<i>Note: An alternate method of sending pitchbend information would be to set
		the <m>@hires</m> attribute to a value of <m>1</m>. When set, the <o>midiformat</o>
		object will expect input as a floating point value in the range of -1.0 to 1.0. 
		You may find this to be a more intuitive way of working (and you'll have to modify the
		<b>p bender</b> patcher accordingly).</i>
	</p>
	<!-- Summary -->
	<summary>Summary</summary>
	<p>
		Max can host plug-in software synthesizers in either VST or AudioUnit formats with
		the <o>vst~</o> object. All features of either format are available to the Max programmer.
	</p>

	<!-- See Alsos -->
	<seealsolist>
		<seealso name="vst~">Load VST and AU plug-ins</seealso>
	</seealsolist>

</chapter>
