<?xml version='1.0' encoding='UTF-8'?>

<?xml-stylesheet href="./_c74_tut.xsl" type="text/xsl"?>
<chapter name="Tutorial 48: Frames of MSP signals">
<setdocpatch name="48jsignals" patch="48jsignals.maxpat"/>

<previous name="jitterchapter47">Using Jitter Object Callbacks in JavaScript</previous>
<next name="jitterchapter49">Colorspaces</next>
<parent name="jitindex">Jitter Tutorials</parent>



<h1>Tutorial 48: Frames of MSP signals</h1>
<p>In <i><link type="tutorial" module="jit" name="jitterchapter27">Tutorial 27:</link> Using MSP Audio in a Jitter Matrix</i> we learned how to use the <o>jit.poke~</o> object to copy an MSP audio signal sample-by-sample into a Jitter matrix. This tutorial introduces <o>jit.catch~</o>, another object that moves data from the signal to the matrix domain. We'll see how to use the <o>jit.graph</o> objects to visualize audio and other one-dimensional data, and how <o>jit.catch~</o> can be used in a frame-based analysis context. We'll also meet the <o> jit.release~</o> object, which moves data from matrices to the signal domain, and see how we can use Jitter objects to process and synthesize sound.</p>


<p>The patch is divided into several smaller subpatches. In the upper left the <m>audio</m> <o>patcher</o> contains a network labeled <i>Sine wave madness</i> where some <o>cycle~</o> objects modulate each other. Playback of an audio file is also possible through the <o>sfplay~</o> object in the upper-right hand corner of the subpatch. </p>

<illustration><img src="images/jitterchapter48a.png"/>Inside the Audio <o>patcher</o>.</illustration>

<h2>Basic Viz</h2>
<bullet>Click the <o>message</o> box labeled <m>dsp start</m> to begin processing audio vectors.</bullet>
<bullet>Open the <o>patcher</o> named <m>basic visualization</m>. Click on the <o>toggle</o> box connected to the <o>qmetro</o> object.</bullet>
<illustration><img src="images/jitterchapter48b.png"/>Capturing and outputting the audio signals as Jitter matrices.</illustration>

<p>In the <m>basic visualization</m> <o>patcher</o> the two signals from the <m>audio</m> <o>patcher</o> are input into a <o>jit.catch~</o> object. The basic function of the <o>jit.catch~</o> object is to move signal data into the domain of Jitter matrices. The object supports the synchronous capture of multiple signals; the object's first argument sets the number of signal inputs; a separate inlet is created for each. The data from different signals is multiplexed into different planes of a <m>float32</m> Jitter matrix. In the example in this subpatch, two signals are being captured. As a result, our output matrices will have two planes. </p>

<p>The <o>jit.catch~</o> object can operate in several different ways, according to the <m>mode</m> attribute. When the <m>mode</m> attribute is set to <m>0</m>, the object simply outputs all the signal data that has been collected since the last <m>bang</m> was received. For example, if 1024 samples have been received since the last time the object received a <m>bang</m>, a one-dimensional <m>1024</m> cell <m>float32</m> matrix would be output.</p>

<p>The object's <m>mode 1</m> causes <o>jit.catch~</o> to output whatever fits in a multiple of a fixed frame size, the length of which can be set with the <m>framesize</m> attribute. The data is arranged in a two dimensional matrix with a width equal to the <m>framesize</m>. For instance, if the <m>framesize</m> were <m>100</m> and the same 1024 samples were internally cached waiting to be output, in <m>mode 1</m> a <m>bang</m> would cause a <m>100x10</m> matrix to be output, and the 24 remaining samples would stay in the cache until the next <m>bang</m>. These 24 samples would be at the beginning of the next output frame as more signal data is received.</p>

<p>When working in <m>mode 2</m>, the object outputs the most recent data of <m>framesize</m> length. Using the example above (with a <m>framesize</m> of <m>100</m> and 1024 samples captured since our last output), in <m>mode 2</m> our <o>jit.catch~</o> object would output the last <m>100</m> samples received. </p>

<p>In our <m>basic visualization</m> subpatch, our <o>jit.catch~</o> object is set to use <m>mode 3</m>, which causes it to act similarly to an oscilloscope in trigger mode. The object monitors the input data for values that cross a threshold value set by the <m>trigthresh</m> attribute. This <m>mode</m> is most useful when looking at periodic waveforms, as we are in this example.</p>

<div>
<techdetail>Note that <m>mode 0</m> and <m>mode 1</m> of the <o>jit.catch~</o> object attempt to output every signal value received exactly once, whereas <m>mode 2</m> and <m>mode 3</m> do not. Furthermore, the matrices output in <m>mode 0</m> and <m>mode 1</m> will be variable in size, whereas those output in <m>mode 2</m> and <m>mode 3</m> will always have the same dimensionality. For all modes, the <o>jit.catch~</o> object must maintain a pool of memory to hold the received signal values while they are waiting to be output. By default the object will cache 100 milliseconds of data, but this size can be increased by sending the object a <m>bufsize</m> message with an argument of the desired number of milliseconds. In <m>mode 0</m> and <m>mode 1</m>, when more data is input than can be internally stored, the next <m>bang</m> will not output a matrix but instead will cause a buffer overrun message to be output from the dump (right-hand) outlet of the object.</techdetail>
</div>
<br/>
<illustration><img src="images/jitterchapter48c.png"/>Our basic visualization of the audio data.</illustration>
<p>Since the two signals are multiplexed across different planes of the output matrices, we use the <o>jit.unpack</o> object to access the signals independently in the Jitter domain. In this example, each of the resulting single-plane matrices is sent to a <o>jit.graph</o> object. The <o>jit.graph</o> object takes one-dimensional data and expands it into a two-dimensional plot suitable for visualization. The low and high range of the plot can be set with the <m>rangehi</m> and <m>rangelo</m> attributes, respectively. By default the graph range is from <m>-1.0</m> to <m>1.0</m>. </p>

<p>These two <o>jit.graph</o> objects have been instantiated with a number of attribute arguments. First, the <m>out_name</m> attribute has been specified so that both objects render into a matrix named <m>jane</m>. Second, the right-hand <o>jit.graph</o> object (which will execute first) has its <m>clearit</m> attribute set to <m>1</m>, whereas the left-hand <o>jit.graph</o> object has its <m>clearit</m> attribute set to <m>0</m>. As you might expect, if the <m>clearit</m> attribute is set to <m>1</m> the matrix will be cleared before rendering the graph into it; otherwise the <o>jit.graph</o> object simply renders its graph on top of whatever is already in the matrix. To visualize two or more channels in a single matrix, then, we need to have the first <o>jit.graph</o> object clear the matrix (<m>clearit 1</m>); the rest of the <o>jit.graph</o> objects should have their <m>clearit</m> attribute set to <m>0</m>.</p>

<p>The <o>jit.graph</o> object's <m>height</m> attribute specifies how many pixels high the rendered matrix should be. A <m>width</m> attribute is not necessary because the output matrix has the same width as the input matrix. The <m>frgb</m> attribute controls the color of the rendered line as four integer values representing the desired alpha, red, green, and blue values of the line. Finally, the <m>mode</m> attribute of <o>jit.graph</o> allows four different rendering systems: <m>mode 0</m> renders each cell as a point; <m>mode 1</m> connects the points into a line; <m>mode 2</m> shades the area between each point and the zero-axis; and the bipolar <m>mode 3</m> mirrors the shaded area about the zero-axis.</p>

<h2>Frames</h2>

<bullet>Turn off the <o>qmetro</o> (by un-checking the <o>toggle</o> box) and close the <m>basic visualization</m> subpatch. Open the <o>patcher</o> named <m>frame-based analysis</m> and click the <o>toggle</o> box inside to activate the <o>qmetro</o> object.</bullet>
<illustration><img src="images/jitterchapter48d.png"/>Performing a frame-based analysis of our audio.</illustration>
<p>The <m>frame-based analysis</m> subpatch shows an example of how one might use the <o>jit.catch~</o>
object's ability to throw out all but the most recent data. The single-plane output of
<o>jit.catch~</o> is being sent to a <o>jit.op</o> object which is multiplying it by itself, squaring each
element of the input matrix. This is being sent to a <o>jit.3m</o> object, and the mean value of the
matrix is then sent out the object's middle outlet to a <o>sqrt</o> object. The result is that we're
calculating the <i>root mean square</i> (RMS) value of the signal &#x2013; a standard way to measure
signal strength. Using this value as the argument to the <m>brightness</m> attribute of a <o>jit.brcosa</o> object, our subpatch maps the amplitude of the audio signal to brightness of a video image.</p>

<p>Our frame-based analysis technique gives a good estimate of the average amplitude of the
audio signal over the period of time immediately before the <o>jit.catch~</o> object received its
most recent <m>bang</m>. The <o>peakamp~</o> object, which upon receiving a <m>bang</m> outputs the highest
signal value reached since the last <m>bang</m>, can also be used to estimate the amplitude of an
audio signal, but it has a couple of disadvantages compared to the <o>jit.catch~</o> technique, which examines only the final 512 samples, thereby striking a balance between accuracy and efficiency. The effect that this savings makes is amplified in situations where the analysis itself is more expensive, for example when performing an FFT analysis on the frame.</p>

<bullet>Turn off the <o>qmetro</o> and close the <m>frame-based analysis</m> subpatch. Open the <o>patcher</o> named <m>audio</m> again and enter a <m>0</m> into the <o>number</o> box connected to the <o>*~</o> objects at the bottom of the patch; the generated signals will no longer be sent to your speakers. Close the <m>audio</m> subpatch and open the patcher named <m>processing audio with jitter objects</m>.</bullet>
<illustration><img src="images/jitterchapter48e.png"/>Using Jitter objects to process audio data, transforming the results back into signals.</illustration>

<p>The <o>jit.release~</o> object is the reverse of <o>jit.catch~</o>: multi-plane matrices are input into the
object which then outputs multiple signals, one for each plane of the incoming matrix.
The <o>jit.release~</o> object's <m>latency</m> attribute controls how much matrix data should be buffered
before the data is output as signals. Given a Jitter network that is supplying a <o>jit.release~</o>
object with data, the longer the <m>latency</m> the lower the probability that the buffer will
underflow &#x2013; that is, the <i>event-driven</i> Jitter network will not be able to supply enough data
for the signal vectors that the <o>jit.release~</o> object must create at a constant (<i>signal-driven</i>) rate.</p>

<bullet>Click on the <o>toggle</o> box into the subpatch to start the <o>qmetro</o> object. You should begin to hear our sine waves mixed with white noise (supplied by the <o>noise~</o> object). Change the value in the <o>number</o> box attached to the <o>message</o> box labeled <m>val $1</m>. Note the effect on the amplitude of the sounds coming from the patch.</bullet>
<p>The combination of <o>jit.catch~</o> and <o>jit.release~</o> allows processing of audio to be
accomplished using Jitter objects. In this simple example a <o>jit.op</o> object sits between the
<o>jit.catch~</o> and <o>jit.release~</o> objects, effectively multiplying all three channels of audio by the
same gain. For a more involved example of using <o>jit.catch~</o> and <o>jit.release~</o> for the
processing of audio, look at the <openpatchlink>jit.forbidden-planet</openpatchlink> example, which does frame-based FFT processing in Jitter.</p>

<h2>Varispeed </h2>

<bullet>Stop the <o>qmetro</o> and close the subpatch. Open the <o>patcher</o> named <m>interpolating </m>.</bullet>
<p>The <o>jit.release~</o> object can operate in one of two modes: in the standard <m>mode 0</m> it expects
that it will be supplied directly with samples at the audio rate &#x2013; that is, for CD-quality
audio the object will on average receive 44,100 <m>float32</m> elements every second and it will
place those samples directly into the signals. In <m>mode 1</m>, however, the object will interpolate
in its internal buffer based on how many samples it has stored. If the playback time of
samples stored is less than the length of the latency attribute, <o>jit.release~</o> will play through
the samples more slowly. If the object stops receiving data altogether, playback will slide
to a stop in a manner analogous to hitting a turntable platter's stop button. On the other
hand, if there are more samples than needed in the internal buffer, <o>jit.release~</o> will play through the samples more quickly.</p>

<p>One can use this feature to generate sound directly from an event-driven network. The
<m>interpolating release~</m> subpatch shows a way for us to do this. The data that drives the
<o>jit.release~</o> object in this subpatcher receives data from a <o>jit.buffer~</o> object, which allows us
to extract data from audio samples in matrix form using messages to the MSP <o>buffer~</o>
object. The <o>jit.buffer~</o> object is essentially a Jitter wrapper around a regular <o>buffer~</o> object;
<o>jit.buffer~</o> will accept every message that <o>buffer~</o> accepts, allows the getting and setting
of <o>buffer~</o> data in matrix form, and also provides some efficient functions for visualizing
waveform data in two dimensions, which we use in this patch in the <o>jit.pwindow</o> object at
right. At <o>loadbang</o> time this <o>jit.buffer~</o> object loaded the data in the sound file <m>talk.aiff</m>.</p>

<bullet>Turn on the <o>toggle</o> box connected to the <o>qmetro</o> object in the subpatch. Play with the <o>number</o> boxes labeled <i>period</i> and <i>length ratio</i> and the <o>slider</o> object labeled <i>start</i>.</bullet>
<illustration><img src="images/jitterchapter48f.png"/>Playing back interpolated audio data from a sound file in Jitter.</illustration>
<p>The rate of the <o>qmetro</o> determines how often data from the <o>jit.buffer~</o> object is sent down towards the <o>jit.release~</o> object. The construction at the top maintains a ratio between the period and the number of samples that are output from <o>jit.buffer~</o>. Experimenting with the start point, the length ratio, and the output period will give you a sense of the types of sounds that are possible in this <m>mode</m> of <o>jit.release~</o>.</p>

<h2>Summary</h2>
<p>In this Tutorial we were introduced to <o>jit.catch~</o>, <o>jit.graph</o>, <o>jit.release~</o>, and <o>jit.buffer~</o> as objects for storing, visualizing, outputting, and reading MSP signal data as Jitter matrices. The <o>jit.catch~</o> and <o>jit.release~</o> objects allow us to transform MSP signals into Jitter matrices at an event rate, and vice versa. The <o>jit.graph</o> object provides a number of ways to visualize single-dimension matrix data in a two-dimensional plot, making it ideal for the visualization of audio data. The <o>jit.buffer~</o> object acts in a similar manner to the MSP <o>buffer~</o> object, allowing us to load audio data directly into a Jitter matrix from a sound file.</p>

	<seealsolist>
		<seealso display="Video and Graphics Tutorial 8: Audio into a matrix" module="Video and Graphics" name="jitterchapter00j_Audio into a matrix" type="tutorial" />
		<seealso name="jit.3m">Report min/mean/max values</seealso>
		<seealso name="jit.brcosa">Adjust image brightness/contrast/saturation</seealso>
		<seealso name="jit.buffer~">A matrix-friendly MSP buffer~</seealso>
		<seealso name="jit.catch~">Transforms signal data into matrices</seealso>
		<seealso name="jit.graph">Floating-point data visualization</seealso>
		<seealso name="jit.op">Apply binary or unary operators</seealso>
		<seealso name="jit.movie">Play or edit a movie</seealso>
		<seealso name="jit.release~">Transforms matrix data into signals</seealso>
	</seealsolist>
	</chapter>
