<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<?xml-stylesheet href="./_c74_tut.xsl" type="text/xsl"?>
<chapter name="MSP Sequencing Tutorial 1: Audio-Rate Sequencing">
	<previous module="msp"  name="11_polychapter03"></previous>
	<next module="msp" name="13_panningchapter01"></next>
	<parent name="00_mspindex">MSP Tutorials</parent>

<setdocpatch name="01qAudioRateSequencing" patch="01qAudioRateSequencing.maxpat"/>
	<h1>
		Sequencing Tutorial 1: Audio-Rate Sequencing
	</h1>

	<h2>
		Introduction
	</h2>

	<p>
		This tutorial looks at how to generate timed events in Max using
		MSP audio signals as the synchronization source. When designing timing-based
		systems in Max, we're used to using <o>metro</o>, <o>clocker</o>, <o>delay</o>,
		and other Max objects that run in the Max scheduler to generate events automatically.
		However, when working with MSP, it's often beneficial to be able to synchronize
		events directly off of an MSP signal. This technique not only allows
		for more precise control of event timing (so-called <i>sample accurate</i>
		sequencing), but also allows us to use a number of MSP objects that allow
		for the synchronization of multiple event streams based on a single clock
		signal. Our first tutorial looks at a few of these objects and how they work.
	</p>

	<h2>
		Providing a synchronization clock signal
	</h2>

	<bullet>
		Take a look at the tutorial patcher. Turn on audio in the patcher by clicking
		on the <o>ezdac~</o> object at the top of the patch. The <o>scope~</o> objects,
		as well as the <o>multislider</o> object in the middle of the patcher, should
		all spring to life. In the area of the patcher labeled <m>1</m>, click
		the <o>button</o> object a few times at a steady speed. Notice how the
		signal scopes change, and how the <o>number</o> box labeled <m>BPM</m>
		adjusts itself.
	</bullet>

	<p>
		The MSP object driving the behavior in our tutorial patcher is called <o>sync~</o>,
		and outputs the same kind of signal as a <o>phasor~</o> object, i.e. a linear
		signal ramp going from <m>0.</m> to <m>1.</m> at a steady rate. Unlike
		the <o>phasor~</o> object, however, the object understands its speed not in
		Hertz, but in <i>beats-per-minute</i> (BPM), a standard definition of
		musical <i>tempo</i> used in most audio sequencing applications. Clicking
		the <o>button</o> object at the top of the patcher sends <m>bang</m> messages
		to the <o>sync~</o> object, which simulates a <i>tap tempo</i> system used in
		many sequencers. By clicking the <o>button</o> at a steady rate,
		the <o>sync~</o> object adjusts its speed to send out a single ramp for every
		beat (interval) between the <m>bang</m> messages. The BPM of our <m>bang</m> messages
		is calculated and sent out the second outlet of the <o>sync~</o> object, which we
		then use to set a <o>number</o> box that we can change to set the BPM manually.
	</p>

	<bullet>
		In the <o>number</o> box at the top of the patcher, type <m>60.</m> and press return.
		The <o>sync~</o> object now runs at 60 BPM, outputting a signal ramp once per second.
		In the patcher area labeled <m>2</m> (colored green), turn up the
		left <o>gain~</o> slider and listen to the result.
	</bullet>

	<p>
		The output of the <o>sync~</o> object is a signal ramp, just like the output
		of the <o>phasor~</o> object. As such, we can use it as an envelope generator
		directly, to control the volume of an audio signal going to the <o>dac~</o> (in
		this case, a <o>rand~</o> noise generator). While this is certainly useful in
		certain applications, the main advantage of using a signal to provide event
		synchronization is in our ability to time-scale the signal and then generate
		events from it.
	</p>

	<h2>
		Many rates at once
	</h2>

	<bullet>
		Notice that, throughout the tutorial patcher, each of the <o>scope~</o> objects
		seem to be drawing ramps at different speeds:
	</bullet>

	<illustration>
		<img src="images/sequencingchapter01a.png"/>
	</illustration>
	<caption>
		<i>Different ramp time-scales generated by use of the <o>sync~</o>
		and <o>rate~</o> objects.</i>
	</caption>

	<p>
		The <o>rate~</o> object allows us to <i>time-scale</i> the ramp signals
		generated by <o>sync~</o> and <o>phasor~</o> objects. The argument to
		the object (or a new value provided in the right inlet) sets the scaling
		factor, e.g. a <o>rate~</o> object with a factor of <m>2.0</m> will
		generate a ramp that takes <i>twice as long</i> to repeat as the input signal.
		A <o>rate~</o> of <m>0.5</m>, by comparison, will go <i>two times as fast</i> as
		the input. Using this system, we can have multiple areas of signal logic in
		our MSP patcher running at different multiples of the original synchronization
		signal yet staying perfectly in time.
	</p>

	<h2>
		Generating Max events from a synchronization signal
	</h2>

	<bullet>
		In patcher area <m>2</m>, turn down the left <o>gain~</o> slider and
		turn up the right one. You should hear a regular metronomic 'click',
		generated by the <o>noise~</o> object with an envelope triggered by
		the <o>button</o> object in that part of the patcher.
	</bullet>

	<p>
		Two things are going on here of note. The first is that the 'click'
		is happening not at the speed we've set in the <o>sync~</o> object,
		but at four times that speed. This is the doing of the <o>rate~</o> object
		in patcher area <m>2</m>, which is set to a value of <m>0.25</m>. This
		means that our effective musical timing for one 'beat' in our patcher is
		subdivided into four in this part of the MSP signal chain, allowing us
		to trigger faster events.
	</p>

	<p>
		The 'click' we hear is generated by sending a <m>bang</m> message (from
		the <o>button</o> object to generate an envelope from the <o>line~</o> object,
		fading in and out the <o>noise~</o> quickly to make a sharp ticking sound.
		This logic is familiar to use from previous tutorials; the big question
		here is, how is the <o>button</o> object being triggered?
	</p>

	<p>
		The MSP <o>delta~</o> object takes a signal input and outputs a signal,
		the numbers of which report how much the incoming signal has <i>changed</i>
		from sample to sample:
	</p>

	<illustration>
		<img src="images/sequencingchapter01b.png"/>
	</illustration>
	<caption>
		<i>A <o>phasor~</o> ramp (top) and its <o>delta~</o> signal (bottom).</i>
	</caption>

	<p>
		In the case of a ramp signal, the <o>delta~</o> object reports a
		more-or-less constant positive value as the signal rises; when the
		ramp resets to <m>0.</m>, however, the <o>delta~</o> object reports
		a sudden, high, <i>negative</i> value that represents the sudden
		drop in the ramp. This momentary negative signal value can be detected
		by using a logical operator on the signal to test whether it goes negative.
		The <o>&lt;~</o> object in the patcher does this, by outputting a signal
		value of <m>1</m> when the input signal matches the condition specified
		by the object's argument, and a <m>0</m> otherwise. In the present case,
		the output of <o>&lt;~</o> will be a <m>1</m> whenever the input signal
		is <i>less than</i> <m>0</m>, i.e. only when the ramp coming out of
		the <o>rate~</o> object resets, causing the <o>delta~</o> object to
		output a negative value.
	</p>

	<p>
		The <o>edge~</o> object is an MSP object that takes input signals and
		uses them to trigger Max <m>bang</m> messages. It does this by looking
		at transition states from <m>0</m> to <m>1</m> (left outlet) or <m>1</m>
		to <m>0</m> (right outlet). When the <o>rate~</o> object resets its ramp,
		the left outlet of the <o>edge~</o> object generates a <m>bang</m> in
		response to the logical <m>0</m> to <m>1</m> (false to true) transition
		coming out of the <o>&lt;~</o> object. This <m>bang</m> can trigger any
		Max event, just as if it were fired from a <o>metro</o> object or
		triggered by clicking a <o>button</o> manually.
	</p>

	<bullet>
		Turn down the <o>gain~</o> slider at the bottom of patcher
		area <m>2</m> and take a look at patcher area <m>3</m>.
	</bullet>

	<h2>
		Audio-rate sequencing using fixed timing intervals
	</h2>

	<bullet>
		In patcher area <m>3</m>, click and draw in the two <o>multislider</o> objects
		labeled 'Pitch' and 'Amplitude'. Turn up the <o>gain~</o> slider
		at the bottom of patcher area <m>3</m>'s logic. You should hear
		a square wave repeating a melodic pattern that matches the shape
		you drew in the <o>multislider</o> objects. Try drawing different
		patterns for the sequence and listen to the results.
	</bullet>

	<p>
		The repeating melody specified by our <o>multislider</o> objects
		is being triggered by a sample-accurate sequencing object
		called <o>techno~</o>. The <o>techno~</o> object maintains
		a certain number of <i>steps</i> of data points based on
		its <m>length</m> parameter (set in our tutorial patcher with
		the message <m>length 16</m>). Each step can have a pitch
		and amplitude associated with it, which it then puts out
		as <i>signals</i> from its outlets. These signals contain
		all the information necessary to directly drive MSP oscillators
		and amplifiers (in this case the <o>rect~</o> and <o>*~</o> objects).
	</p>

	<p>
		Sequences are loaded into the <o>techno~</o> object by
		sending <m>pitch</m> and <m>amplitude</m> messages with the
		step number and value as their arguments. The <o>listfunnel</o> objects
		in our tutorial patcher create the appropriate messages for
		this purpose, by taking the sixteen-member lists from
		the <o>multislider</o> objects and translating them into sixteen
		individual, numbered messages. By a similar token, other attributes
		of the sequence process can be set, either for each step individually
		or for the entire sequence.
	</p>

	<bullet>
		At the bottom of patcher area <m>3</m>, set the <o>number</o> box
		labeled 'Attack' to <m>1.0</m>. Notice how the notes change from
		being short to long (or <i>staccato</i> to <i>legato</i>). Set
		the <o>number</o> box labeled 'Decay' to <m>0.</m>. Notice how the
		envelope of the notes change to produce a different kind of note separation where the square wave fades in and ends abruptly. Set both the 'Attack' and 'Decay' values to <m>1.</m> and change the <o>number</o> box labeled 'Curve' to <m>0.1</m>. Hear how the pitches now slide into on another.
	</bullet>

	<p>
		The <m>attack</m>, <m>decay</m>, and <m>curve</m> messages, like
		the <m>pitch</m> and <m>amplitude</m> messages, allow us to fine-tune
		the behavior of the internal sequencer of the <o>techno~</o> object.
		The <m>attack</m> values control the trajectory for the envelope coming
		out of the middle outlet of the <o>techno~</o> object as it rises at the
		beginning of a note. A value of <m>0</m> causes the notes to snap on
		instantaneously; a value of <m>1.0</m> causes the tones in the sequence
		to take the entire beat to fade in from their previous value.
		The <m>decay</m> values work on a similar principle, only with regards
		to the end (or 'release' stage) of the note's envelope. A <m>decay</m> of
		1.0 allows the notes to blend together; a value of <m>0</m> ensures that
		each note falls to silence between steps of the sequencer. These two
		values interact to create dynamic envelope shapes. the <m>curve</m> messages
		control the amount of <i>portamento</i> (or gliding) applied to the signal
		controlling the sequence's <i>pitch</i>. Curve values of <m>0</m> will
		cause the pitches to 'snap' from note to note in the sequence. As we
		raise the <m>curve</m> value, more and more of the beat will be taken
		up by an interpolating pitch ramp, causing the sound to slide around
		in frequency.
	</p>

	<p>
		As with the <m>pitch</m> and <m>amplitude</m>, the <m>attack</m>,
		<m>decay</m>, and <m>curve</m> parameters of the <o>techno~</o> object
		are provided on a <i>step-by-step</i> basis. Notice how we use the <o>uzi</o> object
		to transmit the same value sixteen times to set every step of our sequence
		to the same values.
	</p>

	<bullet>
		Play around with the possibilities of sequencing with <o>techno~</o>. Once
		you've gotten a grasp of how the controls work, turn down the <o>gain~</o> slider
		for patcher area <m>3</m> and take a look to the right, at patcher area <m>4</m>.
	</bullet>

	<h2>
		Audio-rate sequencing of arbitrary data
	</h2>

	<bullet>
		Turn up the <o>gain~</o> slider for patcher area <m>4</m>. Using your
		computer keyboard, type on the middle row of letter
		keys (A, S, D, F, G, H, J, K, L, ;). You should hear different
		notes being played from a simple sawtooth oscillator. If you like,
		double-click the <o>coll</o> object named <m>keys</m> to see the
		mapping from ASCII values to MIDI note numbers.
	</bullet>

	<bullet>
		Click the 'r' key on your keyboard and play some notes on the middle row.
		Notice that the <o>toggle</o> object attached to the <m>record $1</m> message
		becomes enabled. Click the 'r' key a second time, and click the 'p' key. Once
		the sequence repeats, you should hear your notes played back.
	</bullet>

	<p>
		The <o>seq~</o> object works as an audio-rate sequencer of arbitrary numeric events.
		The signal it receives in its input serves as a synchronization clock for the
		object, allowing us to feed it with a signal ramp from a <o>rate~</o> object
		set to eight measures. When <o>seq~</o> receives a <m>record 1</m> message,
		it listens to Max messages coming into the object and <i>timestamps</i> them
		based on the input signal. When <o>seq~</o> is in 'play' mode (through
		a <m>play 1</m> message), it uses the input signal to look up events based
		on their timestamp, and outputs them in the correct order. Note that,
		unlike the <o>techno~</o> object, the <o>seq~</o> object outputs Max events,
		not signal values, and so it can be used to store arbitrary data.
	</p>

	<techdetail>
		Though beyond the scope of this tutorial, the <o>seq~</o> object can store
		multiple sequences simultaneously, can overdub multiple passes of data, and
		can read and write files of data in a manner similar to the <o>coll</o> object.
		Also, because it stores arbitrary, timestamped messages, it can be used to
		sequence other Max commands besides MIDI with sample accuracy, e.g. lists of
		drawing commands to an <o>lcd</o> object, or messages to a series of Jitter
		objects, etc. Finally, the signal providing the timestamping for the <o>seq~</o> object
		can be at an arbitrary scale; it doesn't have to be in the range
		of <m>0</m> to <m>1</m>, nor do you have to send it a linear ramp. This
		further expands the flexibility of the object.
	</techdetail>

	<bullet>
		Turn up the lower <o>gain~</o> slider in patcher area <m>1</m> to give yourself a
		click to work against. Hit the 'c' key to <m>clear</m> the sequence stored
		in the <o>seq~</o> object. Hit the 'r' key to record a new sequence. When the
		sequence loop has wrapped around, disable recording and play your sequence.
		Notice that the <o>seq~</o> object can handle data with any timing interval,
		unlike the <o>techno~</o> object, which works in discrete steps.
	</bullet>


	<h2>
		Summary
	</h2>
	<p>

		MSP has a variety of tools for audio-rate sequencing. The <o>sync~</o> and <o>rate~</o> objects can be used to create simple timing ramps and regular intervals using beats-per-minute values to determine tempo. The <o>delta~</o> and <o>edge~</o> objects are useful in generating Max events from an MSP ramp signal. The <o>techno~</o> object allows for sample-accurate sequencing of a finite number of steps for a single audio oscillator/amplifier pair; for more flexible sequencing, the <o>seq~</o> object allows for audio-rate timestamping of arbitrary Max events which can be saved, edited, and replayed as sequences of instructions.

	</p>
	<seealsolist>
		<seealso name="sync~">Synchronize MSP with an external source</seealso>
		<seealso name="rate~">Time-scale the output of a phasor~</seealso>
		<seealso name="delta~">Signal of sample differences</seealso>
		<seealso name="edge~">Detect logical signal transitions</seealso>
		<seealso name="techno~">Signal-driven step sequencer</seealso>
		<seealso name="seq~">Signal-driven event sequencer</seealso>
	</seealsolist>
</chapter>

