<?xml version="1.0" encoding="utf-8" standalone="yes"?>

<?xml-stylesheet href="./_c74_tut.xsl" type="text/xsl"?>

<chapter name="MSP Sampling Tutorial 4: Variable-length Wavetable">

	<previous module="msp"  name="07_samplingchapter03"></previous>
	<next module="msp" name="07_samplingchapter05"></next>
	<parent name="00_mspindex">MSP Tutorials</parent>

<setdocpatch name="04sVariableLengthWavetable" patch="04sVariableLengthWavetable.maxpat"/>

<h1>
	Sampling Tutorial 4: Variable-length Wavetable
</h1>

	<p>
		In this tutorial, we look at the <o>wave~</o> object, which allows
		you to access <o>buffer~</o> data as a wavetable for an oscillator.
		This allows you to load samples and use them as waveforms which
		are accessed based on playback frequency. You can use these wavetables
		to build sample-based oscillators for more complex synthesis patches.
	</p>

	<h2>
		Use any part of a buffer~ as a wavetable: wave~
	</h2>

	<p>
		As was shown in an earlier tutorial, the <o>cycle~</o> object can
		samples of a <o>buffer~</o> as a wavetable through which
		it reads repeatedly to play a periodically repeating tone. The
		<o>wave~</o> object is an extension of that idea; it allows you to
		use <i>any</i> section of a <o>buffer~</o> as a wavetable with detailed control of the endpoints.
	</p>

	<p>
		The start and end points within the <o>buffer~</o> are determined
		by the number or signal received in the middle and right inlets of
		the <o>wave~</o> object. As a signal in the <o>wave~</o> object's
		left inlet goes from <m>0.</m> to <m>1.</m>, <o>wave~</o> sends
		out the contents of the <o>buffer~</o> from the specified start
		point to the end point. The <o>phasor~</o> object, ramping
		repeatedly from 0 to 1, is the obvious choice as an input
		signal for the left inlet of <o>wave~</o>.
	</p>

	<p>
		In a standard implementation of wavetable synthesis, the wavetable
		(16K samples in the case of <o>cycle~</o>, or a section of any
		length in the case of <o>wave~</o>) would be one single cycle of
		a waveform, and the frequency of the <o>cycle~</o> object
		(or the <o>phasor~</o> driving the <o>wave~</o>) would determine
		the fundamental frequency of the tone. In the case of <o>wave~</o>,
		however, the wavetable could contain virtually anything (an
		entire spoken sentence, for example).
	</p>

	<p>
		<o>wave~</o> yields rather unpredictable results compared to some of
		the more traditional sound generation ideas presented so far, but
		with some experimentation you can find a great variety of timbres
		using <o>wave~</o>. In this tutorial patch, you will see some ways
		of reading the contents of a <o>buffer~</o> with <o>wave~</o>.
	</p>

	<h2>
		Synthesis with a segment of sampled sound
	</h2>

	<p>
		The tutorial patch is designed to let you try three different
		ways of driving <o>wave~</o> (labeled <m>1</m>, <m>2</m>, and <m>3</m>)
		with two different audio samples (labeled <m>A</m> and <m>B</m>): with a
		repeating ramp signal (<o>phasor~</o>), a sinusoid (<o>cycle~</o>),
		or a single ramp (<o>line~</o>). The bottom part of the patcher is
		devoted to the basic implementation of <o>wave~</o>, and the upper
		part of the window contains the three methods of reading through the
		wavetable. First, let's look at the bottom half of the window.
	</p>

	<bullet>
		Turn audio on. Set the amplitude to a comfortable level. Click the <m>message</m> box labeled <m>A</m>. This will instruct the <o>wave~</o> object to use the <o>buffer~</o> named <m>words</m> and will set the length of the wavetable to the entire length of the sample. Now we are ready to read through the wavetable.
	</bullet><br/>

	<bullet>
		Choose ‘Read forward’ from the pop-up <o>umenu</o> in the middle
		of the window. This will open the first signal inlet of the <o>selector~</o>,
		allowing <o>wave~</o> to be controlled by the <o>phasor~</o> object in
		the circuit in the upper-left (labeled <m>1</m>).
	</bullet><br/>

	<bullet>
		Set the <o>number</o> box marked ‘Range’ to <m>1</m>. This sets
		the amplitude of the <o>phasor~</o>, so it effectively determines
		what fraction of the wavetable will be used. Set the <o>number</o> box
		marked ‘Frequency’ to <m>2</m>. The <o>phasor~</o> now goes from 0 to 1
		two times per second, so you should hear <o>wave~</o> reading through
		the <o>buffer~</o> every half second.
	</bullet><br/>

	<bullet>
		Try a few different sub-audio frequency values for the <o>phasor~</o>,
		to read through the <o>buffer~</o> at different speeds. You can change
		the portion of the <o>buffer~</o> being read, either by changing the ‘Range’
		value, or by changing the start and end times of the <o>wave~</o> at the
		bottom of the patcher. Try audio frequencies for the <o>phasor~</o> as well.
	</bullet>

	<p>
		Notice that the rate of the <o>phasor~</o> often has no obvious relationship
		to the perceived pitch, because the contents of the wavetable do not represent
		a single cycle of a waveform. Furthermore, such rapid repetition of an
		arbitrarily selected segment of a complex sample has a very high likelihood
		of producing frequencies well in excess of the Nyquist rate, which will be
		folded back into the audible range in unpredictable ways.
	</p>

	<bullet>
		Click on the <o>message</o> box labeled <m>B</m> to refer <o>wave~</o>
		to the <o>buffer~</o> object named <m>chords</m>.
	</bullet>

	<p>
		This changes the contents of the wavetable (because <o>wave~</o>
		now accesses a different <o>buffer~</o>), and sets the value of
		the ‘End time’ <o>number</o> box equal to the length of the
		file <i>sacre.aiff</i>. Notice an additional little programming trick -
		shown in the example below - employed to prevent the user from
		entering inappropriate start and end times for <o>wave~</o>.
	</p>

<illustration><img  src="images/samplingchapter04a.png"/></illustration>

	<caption>
		<i>Each time the start or end time is changed, it revises the
		limits of the other number box</i>
	</caption>

	<bullet>
		With this new <o>buffer~</o>, experiment further by reading different
		length segments of the <o>buffer~</o> at various rates.
	</bullet>

	<h2>
		Using wave~ as a transfer function
	</h2>

	<p>
		Take a look at the section of patcher logic labeled <m>2</m>. The <o>buffer~</o>
		object can also be used as a lookup table that can be accessed in
		different ways by other objects. In the next tutorial we'll look at this
		in a more traditional manner, but with the <o>wave~</o> object you can
		generate interesting timbres by feeding a simple waveform into the
		object as an index value. To use <o>wave~</o> in this way, we must
		scale and offset the incoming <o>cycle~</o> so that it sends signal
		values from 0 to 1.
	</p>

	<bullet>
		Set the start and end times of <o>wave~</o> close together, so that
		only a few milliseconds of sound are being used for the wavetable.
		Choose ‘Read back and forth’ from the pop-up <o>umenu</o> in the
		middle of the window. This opens the second signal inlet of the
		<o>selector~</o>, allowing <o>wave~</o> to be controlled by the
		<o>cycle~</o> object.
	</bullet><br/>

	<bullet>
		Set the ‘Range’ <o>number</o> box to a very small value such
		as <m>0.01</m> at first, to limit the <o>cycle~</o> object's
		amplitude. This way, <o>cycle~</o> will use a very small segment
		of the wavetable as the transfer function. Set the frequency of
		<o>cycle~</o> to <m>220</m> Hz. You will probably hear a rich
		tone with a fundamental frequency of 220 Hz. Drag on the ‘Range’
		<link type="refpage" name="number">number box</link> to change the
		amplitude of the cosine wave; the timbre will change accordingly.
		You can also experiment with different wavetable lengths by changing
		the start and end times of <o>wave~</o>. Sub-audio frequencies for the
		<o>cycle~</o> object will produce unusual vibrato-like effects as
		it scans back and forth through the wavetable.
	</bullet>

	<h2>
		Play the segment as a note
	</h2>

	<p>
		Look at the third section of patcher logic at the top,
		labeled <m>3</m>. Because <o>wave~</o> accepts any signal
		input in the rage 0 to 1, you can read through the wavetable
		just once by sending <o>wave~</o> a ramp signal from 0 to 1
		(or backward, from 1 to 0). Other objects such as <o>play~</o>
		and <o>groove~</o> are better suited for this purpose, but it
		is nevertheless possible with <o>wave~</o>.
	</p>

	<bullet>
		Choose ‘Read once’ from the pop-up <o>umenu</o> in the middle
		of the window. This opens the third signal inlet of the <o>selector~</o>,
		allowing <o>wave~</o> to be controlled by the <o>line~</o> object.
		Set start and end times for your wavetable, set the ‘Duration’
		<o>number</o> box to <m>1000</m>, and click on the <o>button</o>
		to traverse the wavetable in one second. Experiment with both
		<o>buffer~</o> objects, using various wavetable lengths and durations.
	</bullet>

	<h2>
		Changing the wavetable dynamically
	</h2>

	<p>
		The <o>cycle~</o> object in the right part of the Patcher window is
		used to add a sinusoidal position change to the wavetable. As the cosine
		wave rises and falls, the start and end times of the wavetable increase
		and decrease. As a result, the wavetable is constantly shifting its
		position in the <o>buffer~</o>, in a sinusoidally varying manner.
		Sonically this produces a unique sort of vibrato, not of fundamental
		frequency but of timbre. The wavetable length and the rate at which it is
		being read stay the same, but the wavetable's contents are continually changing.
	</p>

	<bullet>
		Set the ‘Shift amount’ to <m>0.35</m>, and set the ‘Shift rate’ to <m>6</m>.
		Set the start time of the wavetable to <m>102</m> and the end time to <m>109</m>.
		Click on the <o>message</o> box to refer <o>wave~</o> to the <o>buffer~</o>
		<m>chords</m> object. Choose ‘Read forward’ from the pop-up <o>umenu</o>.
		Set the frequency of the <o>phasor~</o> to an audio rate such as <m>110</m>,
		and set its range to <m>1</m>. You should hear a vibrato-like timbre change
		at the rate of 6 Hz. Experiment with varying the shift rate and the shift
		amount. When you are done, click on the <o>toggle</o> to turn audio off.
	</bullet>

	<h2>
		Summary
	</h2>

	<p>
		Any segment of the contents of a <o>buffer~</o> can be used as a
		wavetable for the <o>wave~</o> object. You can read through the
		wavetable by sending a signal to <o>wave~</o> that goes from 0 to 1.
		So, by connecting the output of a <o>phasor~</o> object to the input
		of <o>wave~</o>, you can read through the wavetable repeatedly at a
		sub-audio or audio rate. You can also scale and offset the output of
		a <o>cycle~</o> object so that it is in the range 0 to 1, and use that
		as input to <o>wave~</o>. This treats the wavetable as a transfer function,
		and results in waveshaping synthesis. The position of the wavetable in
		the <o>buffer~</o> can be varied dynamically - by adding a sinusoidal offset
		to the start and end times of <o>wave~</o>, for example - resulting
		in unique sorts of timbre modulation.
	</p>

<seealsolist>
	<seealso name="buffer~">Store audio samples</seealso>

	<seealso name="phasor~">Sawtooth wave generator</seealso>

	<seealso name="wave~">Variable-size wavetable</seealso>
</seealsolist>

</chapter>


