<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<?xml-stylesheet href="./_c74_tut.xsl" type="text/xsl"?>
<chapter name="MSP Polyphony Tutorial 1: Using the poly~ Object">
	<previous module="msp"  name="10_midichapter03"></previous>
	<next module="msp" name="11_polychapter02"></next>
	<parent name="00_mspindex">MSP Tutorials</parent>

<setdocpatch name="01hUsingpoly~" patch="01hUsingpoly~.maxpat"/>
	<h1>
		Polyphony Tutorial 1: Using the <o>poly~</o> Object
	</h1>

	<h2>
		A different approach to polyphony
	</h2>
	<p>
		In an earlier MSP tutorial on using
		<link type="tutorial" module="msp" name="10_midichapter02">MIDI with MSP</link> ,
		we demonstrated how to use the <o>poly</o> object to make polyphonic
		voice assignments in a simple case. This chapter will describe a
		more elegant and efficient way to handle polyphonic voice
		allocation - the <o>poly~</o> object.
	</p>
	<p>
		In the example in the previous chapter, we created multiple
		copies of our synthesizer subpatch and used the poly object's
		voice numbering to route messages to different copies of the
		subpatch. Our example could just as easily have used any kind
		of sound-producing subpatch.
	</p>
	<bullet>
		Take a look at the tutorial patcher. Open the <o>patcher</o> object
		labeled 'thehardway'. The example inside uses the
		subpatch <m>littlesynth~</m> to implement a simple four-voice
		polyphonic synthesizer:
	</bullet>
	<illustration><img src="images/polychapter01c.png"/></illustration>
	<p>
		While this method works, it has two disadvantages. First, there's
		a lot of housekeeping necessary to duplicate and patch the multiple
		copies of <m>littlesynth~</m> together. But there is also a
		problem in terms of CPU usage. All four copies of
		the <m>littlesynth~</m> subpatcher are always on, processing their
		audio even when there is no sound being produced.
	</p>
	<p>
		There is a way to solve this problem - the <o>poly~</o> object
		allows you to create and manage multiple copies of the same MSP
		subpatch all within one object. You can also control the signal
		processing activity within each copy of the subpatch to conserve
		CPU resources.
	</p>
	<h2>
		The <o>poly~</o> object
	</h2>
	<bullet>
		Close the 'thehardway' subpatch and open the <o>patcher</o> object
		labeled 'simple_poly'. Turn on the audio in the subpatch by
		clicking the <m>startwindow</m> message to the <o>dac~</o>. Click
		the <o>toggle</o> object at the top of the patcher and turn up
		the volume on the <o>gain~</o> slider.
	</bullet>
	<p>
		The <o>poly~</o> object takes as its argument the name of a patcher
		file, followed by a number that specifies the number of copies
		(or <i>instances</i>) of the patch to be created. You'll want to
		specify the same number of copies as you would have had to duplicate
		manually when implementing polyphony the old-fashioned way.
	</p>
	<bullet>
		Double-click on the <o>poly~</o> object. This opens up the
		subpatcher to show you the inside of the <m>littlebeep~</m> object.
	</bullet>
	<p>
		Let's look at the <m>littlebeep~</m> patch for a minute. While you
		haven't seen the <o>in</o>, <o>out~</o>, or <o>thispoly~</o> objects
		before, the rest of the patcher is pretty straightforward; it takes
		an incoming MIDI note number, converts it to a frequency value using
		the <o>mtof</o> object, and outputs a sine wave at that frequency with
		a duration of 140 milliseconds and an amplitude envelope supplied by
		the <o>line~</o> object for <m>140</m> ms with an envelope on it.
	</p>
	<p>
		But what about the <o>in</o> and <o>out~</o> objects? Subpatches
		created for use in the <o>poly~</o> object use special objects for inlets
		and outlets. The objects <o>in</o> and <o>out</o> create control inlets
		and outlets, and the <o>in~</o> and <o>out~</o> objects create signal
		inlets and outlets. You specify which inlet is assigned to which object
		by adding a number argument to the object - the <o>in</o> <m>1</m> object
		corresponds to the leftmost inlet on the <o>poly~</o> object, and so on.
		The <o>poly~</o> object keeps track of the number of inlets and outlets it
		needs to create when you tell it which subpatch to load.
	</p>
	<p>
		Messages sent to a <o>poly~</o> object are directed to different
		instances of the subpatch dynamically using the <m>note</m>
		and <m>midinote</m> messages, and manually using the <m>target</m> message.
	</p>
	<p>
		When <o>poly~</o> receives a <m>note</m> message in its left inlet,
		it scans through the copies of the subpatch it has in memory until it
		finds one that is currently not busy, and then passes the message to it.
		A subpatch instance can tell its parent <o>poly~</o> object that it is
		busy using the <o>thispoly~</o> object. The <o>thispoly~</o> object accepts
		either a signal or number in its inlet to set its busy state. A zero signal
		or a value of <m>0</m> sent to its inlet tells the parent <o>poly~</o> that
		this instance is available for <m>note</m> or <m>midinote</m> messages. A
		non-zero signal or value sent to its inlet tells the parent <o>poly~</o> that
		the instance is busy; no <m>note</m> or <m>midinote</m> messages will be sent
		to the object until it is no longer busy. The busy state was intended to
		correspond to the duration of a note being played by the subpatcher instance,
		but it could be used to mean anything. In the example above, when the audio
		level out of the <o>*~</o> is nonzero -- that iteration of the subpatch is
		currently busy. Once the amplitude envelope out of <o>line~</o> reaches zero
		and the sound stops, that subpatch's copy of <o>thispoly~</o> tells <o>poly~</o> that
		it is ready for more input.
	</p>
	<h2>
		Muting voices
	</h2>
	<bullet>
		Close the 'simple_poly' subpatch and open the <o>patcher</o> object
		named 'poly_using_mute'. Start it running as you did the last
		patcher, and double-click the <o>poly~</o> object.
	</bullet>
	<p>
		The <o>thispoly~</o> object can also control the activity of
		signal processing within each copy of the subpatch. When
		the <m>mute</m> message is sent to <o>thispoly~</o> followed by
		a <m>1</m>, all signal processing in that subpatch stops. When
		a <m>mute 0 </m>message is received, signal processing starts again.
	</p>
	<p>
		In this patcher, we've rewriten the <m>littlebeep~</m> subpatcher
		to take advantage of this by turning off signal processing when a
		note is finished and turning it on again when a new event is
		received. While this doesn't change the function of the patch,
		it would be more efficient, since the amount of CPU allocated is
		always based on the number of notes currently sounding.
	</p>
	<techdetail>
		Why do we care about efficiency? Well, even though modern computers perform at a level not imaginable fifteen years ago, they still have limits. Audio processing constantly pushes those limits in a way little else does. (After all, the internet does not mind a short pause during an upload.) When the limit is reached, it is audible as clicks and distortion. That is why there is a non-realtime output option. (See <i><link type="tutorial" module="msp" name="04_mspaudioio">Audio I/O - Audio input and output with MSP</link></i> ) Now consider this patch:

	<illustration><img src="images/polychapter01a.png"/></illustration>

	<caption><i>A silent MSP patcher</i></caption>

		As shown, the patch is making no noise. It will, as soon as it receives a note number, but for now it is on standby.
		However, it still requires computation. The signal from the <o>cycle~</o> object is multiplied by 0, so nothing is
		heard, but multiplies are happening 44,100 times a second, not to mention whatever else <o>cycle~</o> does to produce
		a signal. Once enough patches, plug-ins and so forth are open, something like this can be the "straw to break the
		camels back", so  disabling audio that is not currently needed is a good idea. The <m>littlebeep2~</m> subpatcher
		demonstrates the use of the <m>mute</m> message to <o>thispoly~</o>.
		We can disable individual voices in <o>poly~</o> with a mute message to the parent <o>poly~</o> object. This
		takes the form of <m>mute n x</m> where <m>n</m> is the voice number and <m>x</m> is 1 for mute and 0 for not muted.
	</techdetail>

	<techdetail>
		If we send a <o>poly~</o> the message <m>mute 0 1</m> all audio processing in the <o>poly~</o> stops. This is a  powerful tool for managing DSP resources, even if you only need one copy of some process. There are certain objects, such as <o>pfft~</o>, (discussed in <i><link type="tutorial" module="msp" name="14_analysischapter04">Signal Processing with pfft~</link></i>) that can be quite computationally expensive. Yet the uses of <o>pfft~</o>  are varied enough (pitch changing, precise filtering, and vocoding, among others) that a patch could easily wind up with a dozen or so. This is enough to account for 30% of the CPU on a medium powered machine. Encasing each <o>pfft~</o> routine in its own <o>poly~</o> will allow you to disable all that are not currently in use. (Note that once you have muted the entire <o>poly~</o> with <m>mute 0 1</m> you cannot start a single voice with something like <m>mute 1 0</m> until you have sent a <m>mute 0 0</m>.)
	</techdetail>

	<h2>
		Targeting individual voices
	</h2>
	<bullet>
		Close the 'poly_using_mute' subpatch and open the one
		named 'poly_using_target'. Start the program and examine the
		patcher logic both inside and outside the <o>poly~</o> object.
	</bullet><br/>
	<p>
		Another way to allocate events using <o>poly~</o> is through
		the <m>target</m> message. Sending a <m>target</m> message followed
		by an integer in the left inlet of a <o>poly~</o> subpatch
		tells <o>poly~</o> to send all subsequent messages to that instance
		of the subpatch. You can then use <o>poly~</o> in conjunction with
		the <o>poly</o> object to create a MIDI synthesizer.
	</p>
	<p>
		In this example <o>patcher</o>, pairs of incoming MIDI pitches
		and velocities are used to synthesize a sine tone. When a list
		is received, the subpatcher sends a <m>bang</m> to <o>thispoly~</o>,
		causing it to output the instance or voice number. In our tutorial
		patcher, the voice number is sent out an outlet so you can watch it
		from the parent patch.
	</p>
	<p>
		In the parent patch the <o>poly</o> object assigns voice
		numbers to MIDI pitch/velocity pairs output by <o>makenote</o>.
		The voice number from the <o>poly</o> object is sent to <o>poly~</o>
		with the <m>target</m> message prepended to it, telling <o>poly~</o>
		to send subsequent data to the instance of the <m>targetbeep~</m>
		subpatcher specified by <o>poly~</o>. When a new note is generated,
		the target will change. Since <o>poly</o> keeps track of note-offs,
		it should recycle voices properly. The second outlet of <o>poly~</o> reports
		the voice that last received a message -- it should be the same as
		the voice number output by <o>poly</o>, since we're using <o>poly</o> to
		specify a specific target.
	</p>

	<h2>
		Using <o>poly~</o> for audio processing.
	</h2>
	<bullet>
		Close the 'poly_using_target' patcher and open the one
		named 'poly_using_signal_input'. Turn on the audio, raise
		the <o>gain~</o> slider, and start the <o>metro</o> by clicking
		the <o>toggle</o> object. Change the value in the rightmost inlet
		from <m>100.</m> to <m>50.</m> and listen to the result.
	</bullet>
	<p>
		The floating-point <o>number</o> box can be used to specify
		parameters to specific instances of a <o>poly~</o> subpatcher.
		By connecting a <o>loadbang</o> object to <o>thispoly~</o>, we
		can use the voice number to control the center frequency of a filter.
	</p>
	<bullet>
		Open the <m>littlefilter~</m> subpatch by double-clicking
		the <o>poly~</o> object.
	</bullet>
	<p>
		The <m>littlefilter~</m> abstraction uses the voice number
		from <o>thispoly~</o> and multiplies it by the base frequency
		received in the second inlet. The incoming signal is filtered
		by all sixteen instances simultaneously, with the output amplitude
		of each instance being controlled by an integer coming into the
		first inlet.
	</p>
	<p>
		The <o>metro</o> object in the main patcher is hooked up to both
		a <o>counter</o> and a <o>random</o>. The <o>counter</o>, which
		feeds the <m>target</m> message, cycles through the 16 voices
		of <m>littlefilter~</m> loaded into the <o>poly~</o> object,
		supplying each with a random number which is used to control the
		amplitude of that voice.
	</p>
	<p>
		A signal connected to an inlet of <o>poly~</o> will be sent
		to the corresponding <o>in~</o> objects of all subpatcher
		instances, so the <o>noise~</o> object in the example
		above feeds noise to all the subpatchers inside the <o>poly~</o>.
		The second inlet (which corresponds to the <o>in</o> <m>2</m> box
		in the subpatcher) controls the base frequency of the filters.
		Note that for the frequency to get sent to all <o>poly~</o>
		iterations it is preceded by a <m>target 0</m> message. You
		can open a specific instance of a <o>poly~</o> subpatch by
		giving the object the <m>open</m> message, followed by the
		voice you want to look at.
	</p>
	<bullet>
		Open voice <m>15</m> of the <m>littlefilter~</m> abstraction
		by typing that number in to the <o>number</o> box attached
		to the <m>open</m> message. The patcher assigned to voice
		number 15 should look like this:
	</bullet>
	<illustration><img src="images/polychapter01b.png"/></illustration>
	<p>
		As you can see, the base frequency of this particular iteration
		of <m>littlefilter~</m> is 1500. Hz, which is the multiple of
		the voice number (15) with the most recently entered base frequency
		into the second inlet (100. Hz).
	</p>
	<h2>
		Summary
	</h2>
	<p>
		<o>poly~</o> is a powerful way to manage multiple copies of the same
		subpatch for polyphonic voice allocation. The <o>thispoly~</o> object works
		inside a subpatch to control its busy state and turn signal processing on
		and off. The objects <o>in</o>, <o>in~</o>, <o>out</o>, and <o>out~</o> create
		special control and signal inputs and outputs that work with the inlets and
		outlets of the <o>poly~</o> object. The ability to disable audio processing with <m>mute</m> messages makes <o>poly~</o> avaluable tool for managing the CPU load caused by standby audio processes.
	</p>
	<seealsolist>
		<seealso name="poly~">Polyphony/DSP manager for patchers</seealso>
		<seealso name="thispoly~">Control <o>poly~</o> voice allocation and muting</seealso>
		<seealso name="in">Message input for a patcher loaded by <o>poly~</o></seealso>
		<seealso name="in~">Signal input for a patcher loaded by <o>poly~</o></seealso>
		<seealso name="out">Message output for a patcher loaded by <o>poly~</o></seealso>
		<seealso name="out~">Signal output for a patcher loaded by <o>poly~</o></seealso>
		</seealsolist>
</chapter>
