<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Anthony Mattox &#187; tutorial</title>
	<atom:link href="http://anthonymattox.com/category/tutorial/feed" rel="self" type="application/rss+xml" />
	<link>http://anthonymattox.com</link>
	<description>Interaction Design and Digital Art</description>
	<lastBuildDate>Fri, 07 Oct 2011 13:30:07 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Full Browser Processing.js</title>
		<link>http://anthonymattox.com/full-browser-processing-js</link>
		<comments>http://anthonymattox.com/full-browser-processing-js#comments</comments>
		<pubDate>Sat, 16 Jul 2011 14:42:21 +0000</pubDate>
		<dc:creator>Anthony</dc:creator>
				<category><![CDATA[article]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[browser]]></category>
		<category><![CDATA[processing]]></category>
		<category><![CDATA[processsing.js]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://anthonymattox.com/?p=2980</guid>
		<description><![CDATA[I played around a bit with processing.js, an awesome bit of software that runs processing scripts in a browser using the HTML canvas element. I wanted to use it to create an animated full browser background for a web page. I couldn&#8217;t find any info on how to do this online, but came up with [...]]]></description>
			<content:encoded><![CDATA[<p>I played around a bit with <a href="http://processingjs.org/">processing.js</a>, an awesome bit of software that runs processing scripts in a browser using the HTML canvas element. I wanted to use it to create an animated full browser background for a web page. I couldn&#8217;t find any info on how to do this online, but came up with a solution. It may not be the most elegant, and performance starts taking a big hit at large sizes, but I wanted to share it in case anyone wanted to build on it or already has a better answer. I&#8217;ll also note that I haven&#8217;t tested this thoroughly.</p>
<p>Simply resizing the canvas element just stretches the rendered image so it needs to be changed within processing.</p>
<p>Since the <a href="http://processing.org">processing</a> script is essentially converted into javascript it&#8217;s possible to communicate between the <a href="http://processing.org">processing</a> script and other javascripts. So, I added an listener for the window resize event (using <a href="http://jquery.com">jQuery</a>), which calls a function in the <a href="http://processing.org">processing</a> function to resize the canvas element.</p>
<p>The javascript looks like this:</p>
<pre>
// javascript (requires jQuery http://jquery.com)

var ProcessingInit = function() {
  function resizeWindow() {
    var pCanvas = Processing.getInstanceById('pCanvas');
    pCanvas.resize($(window).width(),$(window).height());
  }

  $(window).resize(resizeWindow);
  resizeWindow();
}
</pre>
<p>It&#8217;s wrapped in a function so that it can be called from <a href="http://processing.org">processing</a> when the application is started. <code>ProcessingInit();</code> in the setup function. This makes sure the canvas element is fully instantiated before it tries to manipulate it.</p>
<p>It&#8217;s pretty straightforward on the <a href="http://processing.org">processing</a> side as well. That function simply calls the size function again. I&#8217;m not sure if that&#8217;s good practice, but it seems to work fine.</p>
<pre>
// processing

void setup() {
  size(800,600);
  ProcessingInit();
}

void resize(float X, float  Y) {
  size(X,Y);
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://anthonymattox.com/full-browser-processing-js/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Creating a Polyphonic Synthesizer in Pure Data</title>
		<link>http://anthonymattox.com/creating-a-polyphonic-synthesizer-in-pure-data</link>
		<comments>http://anthonymattox.com/creating-a-polyphonic-synthesizer-in-pure-data#comments</comments>
		<pubDate>Sat, 13 Nov 2010 16:49:11 +0000</pubDate>
		<dc:creator>Anthony</dc:creator>
				<category><![CDATA[article]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[polyphonic]]></category>
		<category><![CDATA[pure data]]></category>
		<category><![CDATA[sound]]></category>
		<category><![CDATA[synthesizer]]></category>

		<guid isPermaLink="false">http://anthonymattox.com/?p=2631</guid>
		<description><![CDATA[I&#8217;ve been playing around with Pure Data for a while now, along with a few other pieces of audio software. The results of this can be heard in the audio for my recent flash game, Pulsus. Pure Data is a great application which allows you to control every detail building audio applications from very basic [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been playing around with <a href="http://puredata.info/">Pure Data</a> for a while now, along with a few other pieces of audio software. The results of this can be heard in the audio for my recent <a href="http://pulsusgame.com">flash game, Pulsus</a>.</p>
<p><a href="http://puredata.info/">Pure Data</a> is a great application which allows you to control every detail building audio applications from very basic components. Unfortunately, much of the program is very poorly documented, so I&#8217;d thought I&#8217;d share a few things I&#8217;ve managed to figure out.</p>
<p>Here&#8217;s a quick walkthrough to create a very basic midi controlled, polyphonic synthesizer in pure data. It doesn&#8217;t sound like much, but it&#8217;ll get things working.</p>
<h3>Generating a Tone</h3>
<p>This basic synthesizer will consist of two patches, one patch to generate a tone, and another which will read a midi signal and control a few instances of the tone generator.</p>
<div><img src="http://anthonymattox.com/wp-content/uploads/2010/11/Pure-Data-oscillator.png" alt="Oscillator for a Synthesizer in Pure Data" title="Pure Data Oscillator" width="402" height="387" class="alignnone size-full wp-image-2636" style="margin:0;" /><small>This is the oscillator patch which generates a tone from a midi message. The tone is created on the left side and the envelope (the change in volume over the course of a note) on the right.</small></div>
<p>The tone patch has an inlet at the top, which accepts a message containing a pitch/velocity pair, and an outlet at the bottom which sends out the audio signal generated. The <code>[unpack]</code> object splits the pitch and velocity. The pitch is sent to a series of objects on the left side which generate the tone. The velocity is sent to the right where the envelope will be generated.</p>
<p>The pitch, in the form of a midi note number, is converted to a frequency in Hertz which is then sent to an oscillator, generating an sine wave.</p>
<p>Two messages will be sent for each midi note, the first when the note begins and the second when the note ends. The &#8216;note off&#8217; message will be the same but the velocity will be 0. The velocity, how hard the note was hit, is usually used to set the volume of the note. The velocity is sent to a <code>[select]</code> object. If it is zero, the message is sent to release the note, otherwise to attack. Each of these send a message to a <code>[vline~]</code> object which sends a signal for the amplitude which either ramps up or down.</p>
<p>Finally, the tone is multiplied by the envelope and sent out the parent patch. When the note is hit, a message is also sent to the oscillator which resets the phase.</p>
<h3>MIDI and Polyphony</h3>
<p>Now that we have our oscillator we need to create a patch which will control a few instances of it. The patch will receive midi data from a hardware controller or from another piece of software. The midi settings will need to be configured for <a href="http://puredata.info">PD</a> to receive the data.</p>
<div><img src="http://anthonymattox.com/wp-content/uploads/2010/11/Pure-Data-polyphonic-synthesizer2.png.png" alt="A Simple Polyphonic Synthesizer Created in Pure Data" title="Pure Data Polyphonic Synthesizer 2" width="434" height="396" class="alignnone size-full wp-image-2649" style="margin:0" /><small>The PD patch which takes midi input and handles the voice allocation, controlling a number of oscillators.</small></div>
<p>At the top of our patch we have a <code>[notein]</code> object which reads midi from channel 1. <code>[notein]</code> will output pitch/velocity pairs. The <code>[poly]</code> object handles the allocation of different voices. When pitch/velocity pairs are sent to it it sends on the data with the addition of a voice number.</p>
<p>In order to play multiple notes at once, the messages will be routed to a number of different oscillators, each a different &#8216;voice&#8217;. When <code>[poly]</code> receives a note, it will give the number of the first available voice. When it gets a &#8216;note off&#8217; message (velocity of 0) it makes that voice available again so that that oscillator can play another note when it needs to.</p>
<p>Now we can route the processed messages to a number of instances of the &#8216;tone&#8217; patch. We do this by packing the messages into lists and using the <code>[route]</code> object which sorts them based on the first element in the message. Awesome. Finally we just add all the signals from the oscillators together and send them to the audio output. It first also multiplies the signal by a amplitude set by the slider so that we can adjust the volume of the synth.</p>
<h3>Ok, So What Now?</h3>
<p>Now that we have a simple little synth we should be able to get some sounds. A midi signal can come from a physical midi controller or from software. You can use a program, like <a href="http://www.ableton.com/">Ableton Live</a>, to create a composition and then send out midi. You can also create other pure data patches to generate midi. A patch could create generative melodies, act as a sequencer, or create midi from another input such as the keyboard. </p>
<p>The synth at this point just plays a sine wave for each note. It&#8217;s a start, but not thrilling. To create different timbres, the <code>[osc~]</code> object can be replaced with something to play <a href="http://en.flossmanuals.net/PureData/GeneratingWaveforms">different waveforms</a> and we could add modulators or a more complex envelope generator. On top of that we can run the audio signal through filters either in pure data or in other software. A bit of reverb will make it sound a little less flat.</p>
<p>If you have any suggestions post them in the comments. I&#8217;m certainly no expert, and I&#8217;d love to hear some other methods.</p>
]]></content:encoded>
			<wfw:commentRss>http://anthonymattox.com/creating-a-polyphonic-synthesizer-in-pure-data/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Controlling Quartz Composer with Pure Data</title>
		<link>http://anthonymattox.com/controlling-quartz-composer-with-pure-data</link>
		<comments>http://anthonymattox.com/controlling-quartz-composer-with-pure-data#comments</comments>
		<pubDate>Tue, 08 Jun 2010 12:48:49 +0000</pubDate>
		<dc:creator>Anthony</dc:creator>
				<category><![CDATA[article]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[midi]]></category>
		<category><![CDATA[pure data]]></category>
		<category><![CDATA[quartz]]></category>
		<category><![CDATA[quartz composer]]></category>

		<guid isPermaLink="false">http://www.anthonymattox.com/?p=2209</guid>
		<description><![CDATA[Recently I performed with a musical group, creating generative imagery as a backdrop. I decided try using quartz composer again, primarily because it renders graphics so quickly and smoothly, but also because it&#8217;s fairly easy to put something together. Unfortunately I ran into a significant roadblock. Quartz composer has no way to control elements while [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I performed with a musical group, creating generative imagery as a backdrop. I decided try using quartz composer again, primarily because it renders graphics so quickly and smoothly, but also because it&#8217;s fairly easy to put something together. Unfortunately I ran into a significant roadblock. Quartz composer has no way to control elements while they are running. Although you can completely restructure and modify your script while it is running, there are no interface elements to control simple changes in values. Obviously, this makes QC so much less valuable as a performance tool.</p>
<p>Having already set up most of my script in Quartz Composer I decided to find a way to control it some other way. <a href="http://puredata.info/">Pure Data</a>, an application similar to Quartz but for the production of sound, allows you to easily build interfaces. The data is then sent out of <a href="http://puredata.info/">pure data</a> as midi controller values, sent through a virtual midi channel, and grabbed by Quartz Composer. From there the values can be used for whatever is necessary.</p>
<p>Figuring out the details took a little while and some help from my friend <a href="http://pcm.peabody.jhu.edu/~mscottnelson/">Michael Scoot-Nelson</a>, but the setup is actually quite simple. In <a href="http://puredata.info/">Pure Data</a>, just set up as many controller as you need in the form of sliders or other UI elements.</p>
<p><img src="/wp-content/uploads/2010/06/pure-data-controls.png" alt="Pure Data Controls" title="Pure Data Controls" width="400" height="364" class="alignnone size-medium wp-image-2214" /></p>
<p>In this case, I have a number of vertical sliders labelled for a particular application. Above each controller I have an object which receives a bang [r b]. In Pure Data the value from each slider is only sent when the slider is changed. Sending a bang to the value &#8216;b&#8217; pushes all the values down and sends them out to the midi channel. The Sliders should be set to a range from 0-127.</p>
<p>The values from the sliders are plugged into the first inlet of control out objects (ctlout). The ctlout objects also need a controller number and a channel number. These are used to get the value on the other end. The channels can all be set to one, and each slider should have it&#8217;s own controller number.</p>
<p><img src="/wp-content/uploads/2010/06/quartz-composer-midi-input.png" alt="Quartz Composer - Midi Input from Pure Data" title="quartz-composer-midi-input" width="450" height="300" class="alignnone size-full wp-image-2408" /></p>
<p>In Quartz composer, a &#8216;Midi Controller Receiver&#8217; object will grab the values sent from Pure Data. Viewing it&#8217;s settings in the inspector, the number of inputs can be changed. Each input is listed with an integer, these are the controller numbers which must be matched to the ones set in pure data. The channels being received can also be changed.</p>
<p>There is one final step to getting everything hooked up. In <a href="http://puredata.info/">Pure Data</a>, under Preferences &gt; Midi Settings must be set to an Midi Channel that Quartz Composer is listening for. On a Mac it&#8217;s easy to set up this channel with the IAC Driver in Audio Midi Setup.</p>
<p><img src="http://www.anthonymattox.com/wp-content/uploads/2010/06/pure-data-pulse-width-modulation.png" alt="Pure Data - Pulse Width Modulation Midi Controller" title="Pure Data Pulse Width Modulation" width="169" height="387" class="alignleft wp-image-2215" /></p>
<p>I also created this little guy here. One element I was controlling in Quartz Composer (a particle generator) could only be set on or off and I needed it to vary smoothly. This creates what is essentially a pulse width modulation simulated analog output. Pressing the &#8216;on&#8217; and &#8216;off&#8217; buttons do just that, and pressing the pwm toggle lets you use the slider to set it more or less on.</p>
<p>Metro&#8217;s turn the output to on every tenth of a second. It is then turned off (ten times a second) after a short delay (less than a tenth of a second). If the slider is at the bottom it turns back off almost immediately, so for the greater part of a second it is off. If the slider is at the top, it takes nearly a tenth of a second to turn back off, so it is mostly on. In the middle it will be on half the time. The result, at least for my particle generator, was the appearance of it&#8217;s rate changing while really it was just turning on and off very quickly. I&#8217;m not sure where else this would be useful, but here it is.</p>
<p>It might be a little hard to read, but if it would be useful to anyone I can upload the pd file somewhere.</p>
]]></content:encoded>
			<wfw:commentRss>http://anthonymattox.com/controlling-quartz-composer-with-pure-data/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Processing: Saving Sequential Images</title>
		<link>http://anthonymattox.com/processing-saving-sequential-images</link>
		<comments>http://anthonymattox.com/processing-saving-sequential-images#comments</comments>
		<pubDate>Mon, 24 Aug 2009 15:33:41 +0000</pubDate>
		<dc:creator>Anthony</dc:creator>
				<category><![CDATA[article]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[generative]]></category>
		<category><![CDATA[image]]></category>
		<category><![CDATA[noise]]></category>
		<category><![CDATA[processing]]></category>
		<category><![CDATA[rendering]]></category>

		<guid isPermaLink="false">http://www.anthonymattox.com/?p=1726</guid>
		<description><![CDATA[Until recently I&#8217;ve just been saving all of my images out of Processing using a simple saveFrame command and throwing all of my images into one giant folder. Looking into this folder I see a few key problems. I&#8217;ve come up with a little system to help me keep better track of renderings of my [...]]]></description>
			<content:encoded><![CDATA[<p>Until recently I&#8217;ve just been saving all of my images out of <a href="http://www.processing.org">Processing</a> using a simple <a href="http://www.processing.org/reference/saveFrame_.html">saveFrame</a> command and throwing all of my images into one giant folder. Looking into this folder I see a few key problems. I&#8217;ve come up with a little system to help me keep better track of renderings of my generative works. Unfortunately it won&#8217;t work retroactively, but it&#8217;ll be useful for future work, and perhaps it can be helpful to some other Processors.</p>
<p>This very adding this very simple snipped to a <a href="http://www.processing.org">Processing</a> sketch will save out an png of the render window whenever the &#8216;s&#8217; key is pressed. #### will be replaced by the current frame number.</p>
<div class="code processing">
<pre><span Class="KEY">void</span> <span Class="KEY">keyPressed</span>() {
  <span Class="KEY">if</span> (<span Class="KEY">key</span>==<span Class="STR">'s'</span>) {
    <span Class="KEY">saveFrame</span>(<span Class="STR">&quot;name_of_sketch-####.png&quot;</span>);
  }
}</pre>
</div>
<p>This works out pretty nicely, but if you run the sketch twice and just happen to press &#8216;s&#8217; on the same frame the first image will be overwritten. Another issue, which might not seem to bad until you have a huge library of images built up, is the order of the files. If the sketch is run many times, all the saved images will be mixed together. Ideally images saved from one run of the sketch should be named sequentially so all the images from one run can be seen together.</p>
<p>Adding an arbitrary value to the particular run and sticking it into the file name fixes these two issues. Many of my scripts include a function to reset the program so it can be run a few times without being completely restarted. In this case, incrementing this arbitrary global value maintains the order of a set of runs.</p>
<div class="code processing">
<pre><span Class="KEY">int</span> G;

<span Class="KEY">void</span> <span Class="KEY">setup</span>() {
  G=<span Class="KEY">floor</span>(<span Class="KEY">random</span>(10000,90000));
  <span Class="COM">//noiseSeed(G);
</span>
}

<span Class="KEY">void</span> <span Class="KEY">draw</span>() {
}

<span Class="KEY">void</span> <span Class="KEY">keyPressed</span>() {
  <span Class="KEY">if</span> (<span Class="KEY">key</span>==<span Class="STR">'s'</span>) {
    <span Class="KEY">saveFrame</span>(<span Class="STR">&quot;name_of_sketch-&quot;</span>+G+<span Class="STR">&quot;-####.png&quot;</span>);
  } <span Class="KEY">else</span>
  <span Class="KEY">if</span> (<span Class="KEY">keyCode</span>==BACKSPACE || <span Class="KEY">keyCode</span>==DELETE) {
    <span Class="COM">// code to reset script
</span>    G++;
  }
}</pre>
</div>
<p>This system isn&#8217;t perfect, but is a vast organizational improvement for just a few extra lines of code</p>
<p>Another useful trick, if you have a script which utilizes a lot of <a href="http://www.processing.org/reference/noise_.html">Perlin Noise</a>, is the set the <a href="http://www.processing.org/reference/noiseSeed_.html">noiseSeed</a> to a random integer (like G in the script above), and include that in the file name. This way, if you want to rerun the script with the same parameters later, you can always set G to the integer in the file name of a particular saved image.</p>
]]></content:encoded>
			<wfw:commentRss>http://anthonymattox.com/processing-saving-sequential-images/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Rendering Processing for Print</title>
		<link>http://anthonymattox.com/rendering-processing-for-print</link>
		<comments>http://anthonymattox.com/rendering-processing-for-print#comments</comments>
		<pubDate>Wed, 25 Mar 2009 18:37:50 +0000</pubDate>
		<dc:creator>Anthony</dc:creator>
				<category><![CDATA[tutorial]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[processing]]></category>
		<category><![CDATA[rendering]]></category>

		<guid isPermaLink="false">http://www.anthonymattox.com/?p=1113</guid>
		<description><![CDATA[I&#8217;ve gotten a few questions recently about how to render Processing scripts for printing. There are a few obstacles, but there are two simple methods which work pretty well. Between the two I&#8217;ve been able to get high resolution renderings of different types of scripts without having to do anything terribly complicated. The image will [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve gotten a few questions recently about how to render <a href="http://www.processing.org" target="_blank">Processing</a> scripts for printing. There are a few obstacles, but there are two simple methods which work pretty well. Between the two I&#8217;ve been able to get high resolution renderings of different types of scripts without having to do anything terribly complicated. The image will be exported as either a vector (shapes) or a raster (pixels) image. Depending on the sketch one method might be much more useful.</p>
<h3>Exporting a Scalable Vector Images</h3>
<p>The best way to get a high resolution image from <a href="http://www.processing.org" target="_blank">Processing</a> is to export the sketch as a vector image. With respect to graphics and images a Vector is just a fancy word for a shape. Unlike a Raster image which records visual information in an array of pixels, vector images are composed of shapes. For some applications this can much more useful. Typefaces, many logos, and other graphics composed of flat shapes are created with vectors. The shapes in a vector image can the be colored, outlined, and styled in many other ways. Vector images can be much smaller than a rasterized version, and most importantly, they are scalable. Since the image is made of shapes it can be stretched as large as you need it and will lose no quality.</p>
<p>Shapes drawn with processing commands are vector shapes, and <a href="http://www.processing.org" target="_blank">Processing</a> includes a library to export this data as a PDF document, which can then be opened and used by any vector editing program.</p>
<p>To capture the vector data import the pdf library, call beginRecord(), draw your shapes, and then endRecord(). A simple code to capture a frame on a mouse press could look like this.</p>
<div class="code processing">
<pre><span Class="KEY">import</span> processing.pdf.*;

<span Class="KEY">boolean</span> capture=<span Class="KEY">false</span>;

<span Class="KEY">void</span> <span Class="KEY">setup</span>() {
}

<span Class="KEY">void</span> <span Class="KEY">draw</span>() {
  <span Class="KEY">background</span>(255);
  <span Class="KEY">if</span> (capture==<span Class="KEY">true</span>){
    <span Class="KEY">beginRecord</span>(PDF,<span Class="STR">&quot;vector_file.pdf&quot;</span>);
    render();
    <span Class="KEY">endRecord</span>();
    capture=<span Class="KEY">false</span>;
  }
}

<span Class="KEY">void</span> <span Class="KEY">mousePressed</span>() {
  capture=<span Class="KEY">true</span>;
}</pre>
</div>
<p>The script above would have all the drawing functions within the render() function. To be less obtrusive the beginRecord() and endRecord() functions can be within separate conditionals. The code between them will be run all the time and the data will be recorded only when the mouse is pressed. The new draw() function would look like this:</p>
<div class="code processing">
<pre><span Class="KEY">void</span> <span Class="KEY">draw</span>() {
  <span Class="KEY">background</span>(255);
  <span Class="KEY">if</span> (capture==<span Class="KEY">true</span>){
    <span Class="KEY">beginRecord</span>(PDF,<span Class="STR">&quot;vector_file.pdf&quot;</span>);
  }

  <span Class="COM">//
</span>  <span Class="COM">// drawing code
</span>  <span Class="COM">//
</span>
  <span Class="KEY">if</span> (capture==<span Class="KEY">true</span>) {
    <span Class="KEY">endRecord</span>();
    capture=<span Class="KEY">false</span>;
  }
}</pre>
</div>
<h3>Notes about exporting PDFs.</h3>
<ul>
<li>The exported PDF will be in the sketches folder and titled with the second parameter of beginRecord().</li>
<li>You can open the PDF with a vector program like Adobe Illustrator and edit the shapes.</li>
<li>Any shapes drawn, even if they are outside the rendering window will be in the PDF. Remove the Clipping mask to reveal all the shapes.</li>
<li>For fills and strokes to work they must be called after beginRecord().</li>
<li>The size of the vector file is dependent on the number of vertices in your image.
<li>
<li>For cumulative rendering place beginRecord() in the setup and endRecord() with a key or mouse press before closing the sketch.</li>
</ul>
<p><span class="hidden">&nbsp;</span>
</p>
<h3>Exporting Large Raster Images</h3>
<p>Using a raster image can be a better option if you have a program with a lot of complex shapes. Saving pixel images is also easier, faster, and less processor intensive, but you lose the scalability. To save an image use the saveFrame() function.</p>
<div class="code processing">
<pre><span Class="KEY">void</span> <span Class="KEY">keyPressed</span>() {
  <span Class="KEY">if</span> (<span Class="KEY">key</span>==<span Class="STR">'s'</span>) {
    <span Class="KEY">saveFrame</span>(<span Class="STR">&quot;<span Class="KEY">image</span>-####.png&quot;</span>);
  }
}</pre>
</div>
<p>The #### will be replaced with the frame number. TIFF, TARGA, PNG, and JPEG files can be saved (default is TIFF), and again the image will be in the sketch&#8217;s folder. To create very large renderings you can simply increase the window size and scale up the sketches parameters as necessary. I&#8217;ve rendered sketches much larger than my screen-size with good results, but can&#8217;t promise it will work on all platforms. On a mac you can enter exposee and an extra large window will shrink to fill the screen, although you can&#8217;t interact with it.</p>
]]></content:encoded>
			<wfw:commentRss>http://anthonymattox.com/rendering-processing-for-print/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Cellular Automata 2</title>
		<link>http://anthonymattox.com/cellular-automata-2</link>
		<comments>http://anthonymattox.com/cellular-automata-2#comments</comments>
		<pubDate>Fri, 27 Feb 2009 04:16:22 +0000</pubDate>
		<dc:creator>Anthony</dc:creator>
				<category><![CDATA[art]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[cellular automata]]></category>
		<category><![CDATA[generative]]></category>
		<category><![CDATA[processing]]></category>

		<guid isPermaLink="false">http://www.anthonymattox.com/?p=1043</guid>
		<description><![CDATA[After playing with binary cellular automata I thought I&#8217;d expand the script a little to accommodate more than two states of a pixel. Three loops are nested and each is iterated once for each state. If the index of each loop equals the three values above the current point the function returns the current state [...]]]></description>
			<content:encoded><![CDATA[<p>After playing with binary cellular automata I thought I&#8217;d expand the script a little to accommodate more than two states of a pixel. Three loops are nested and each is iterated once for each state. If the index of each loop equals the three values above the current point the function returns the current state of a counter which is incremented in the innermost loop. In a binary system this is a little more complex than just testing each of the eight possible combinations, but is almost necessary when dealing with more states. I&#8217;ll post my code at the bottom of this post. But first, some pretty pictures.</p>
<p><img src="http://www.anthonymattox.com/wp-content/uploads/2009/02/cellular_automata-4.jpg" alt="processing cellular automata 4" title="processing cellular automata 4" class="alignnone size-medium" /></p>
<p><img src="http://www.anthonymattox.com/wp-content/uploads/2009/02/cellular_automata-3.jpg" alt="processing cellular automata 3" title="processing cellular automata 3" class="alignnone size-medium" /></p>
<p><img src="http://www.anthonymattox.com/wp-content/uploads/2009/02/cellular_automata-2.jpg" alt="processing cellular automata 2" title="processing cellular automata 2" class="alignnone size-medium" /></p>
<p><span id="more-1043"></span></p>
<p><img src="http://www.anthonymattox.com/wp-content/uploads/2009/02/cellular_automata-5.jpg" alt="processing cellular automata 5" title="processing cellular automata 5" class="alignnone size-medium" /></p>
<p><img src="http://www.anthonymattox.com/wp-content/uploads/2009/02/cellular_automata-1.jpg" alt="processing cellular automata 1" title="processing cellular automata 1" class="alignnone size-medium" /></p>
<p>My code. Straight up. I used <a href="http://www.shiffman.net" taget="_blank">Dan Shiffman&#8217;s</a> example as a model and then went from there to add a few more features. Sorry the code isn&#8217;t annotated much but feel free to ask if you have any questions, and let me know if you make something cool with it.</p>
<div class="code processing">
<h4 class="cHead"><span><a href="http://www.processing.org">processing</a> code<br /><a href="http://www.anthonymattox.com/code_formatter">code formatter</a></span>Cellular Automata</h4>
<pre><span Class="COM">// Cellular Automata Script
</span><span Class="COM">// www.anthonymattox.com
</span><span Class="COM">// based on Dan Shiffman's example at
</span><span Class="COM">//      http://www.shiffman.net/teaching/nature/week7/
</span><span Class="COM">// added unlimited states of cells, colors, and keyboard control
</span>
cellsystem CS;

<span Class="KEY">void</span> <span Class="KEY">setup</span>() {
  <span Class="KEY">size</span>(1920,1200);
  <span Class="KEY">background</span>(0);
  CS=<span Class="KEY">new</span> cellsystem();
  <span Class="KEY">noFill</span>();
  <span Class="KEY">noStroke</span>();
  <span Class="KEY">rectMode</span>(<span Class="STR">CENTER</span>);
  <span Class="KEY">frameRate</span>(100);
  <span Class="KEY">smooth</span>();
}

<span Class="KEY">void</span> <span Class="KEY">draw</span>() {
  CS.run();
}

<span Class="COM">//---------- Cellular Automata Class
</span>
<span Class="KEY">class</span> cellsystem{
  <span Class="KEY">int</span> States;
  <span Class="KEY">int</span>[] cells;
  <span Class="KEY">int</span>[] newGen;
  <span Class="KEY">int</span>[] rules;
  <span Class="KEY">int</span> gen;
  <span Class="KEY">float</span> Size;
  <span Class="KEY">float</span> RSize;

  cellsystem() {
    States=4;
    Size=10;
    RSize=8;
    cells=<span Class="KEY">new</span> <span Class="KEY">int</span>[<span Class="KEY">int</span>((<span Class="STR">width</span>+Size)/Size)];
    newGen=<span Class="KEY">new</span> <span Class="KEY">int</span>[<span Class="KEY">int</span>((<span Class="STR">width</span>+Size)/Size)];
    rules=<span Class="KEY">new</span> <span Class="KEY">int</span>[States*States*States];
    restart();
  }

  <span Class="KEY">void</span> restart() {
    gen=0;
    <span Class="KEY">noiseSeed</span>(<span Class="KEY">int</span>(<span Class="KEY">random</span>(500)));
    <span Class="KEY">background</span>(255);
    <span Class="KEY">for</span> (<span Class="KEY">int</span> i=0; i&lt;rules.length; i++) {
      rules[i]=<span Class="KEY">int</span>(<span Class="KEY">random</span>(States));
    }
    <span Class="KEY">for</span> (<span Class="KEY">int</span> i=0; i&lt;cells.length; i++) {
      cells[i]=<span Class="KEY">constrain</span>(<span Class="KEY">int</span>(<span Class="KEY">noise</span>(i/20)*States),0,States-1);
    }
  }

  <span Class="KEY">void</span> Continue() {
    gen=0;
    <span Class="KEY">background</span>(255);
  }

  <span Class="KEY">void</span> run() {
    <span Class="KEY">if</span> (finished()==<span Class="KEY">false</span>) {
      gen++;
      update();
      render();
    }

  }

  <span Class="KEY">void</span> render() {
    <span Class="KEY">for</span> (<span Class="KEY">int</span> i=0; i&lt;cells.length; i++) {

      <span Class="KEY">if</span> (cells[i]==0) {
        <span Class="KEY">fill</span>(255);
        <span Class="COM">//<span Class="KEY">stroke</span>(255);
</span>      } <span Class="KEY">else</span> <span Class="KEY">if</span> (cells[i]==1) {
        <span Class="KEY">fill</span>(255,10,0,180);
        <span Class="COM">//<span Class="KEY">stroke</span>(255,10,0,180);
</span>      } <span Class="KEY">else</span> <span Class="KEY">if</span> (cells[i]==2) {
        <span Class="KEY">fill</span>(0,200,215,180);
        <span Class="COM">//<span Class="KEY">stroke</span>(0,200,215,180);
</span>      } <span Class="KEY">else</span>{
        <span Class="KEY">fill</span>(0,220,160,180);
        <span Class="COM">//<span Class="KEY">stroke</span>(0,220,160,180);
</span>      }

      <span Class="KEY">ellipse</span>(i*Size,gen*Size,RSize,RSize);
      <span Class="COM">//<span Class="KEY">rect</span>(i*Size,gen*Size,RSize,RSize);
</span>    }
  }

  <span Class="KEY">void</span> update() {
    <span Class="KEY">for</span> (<span Class="KEY">int</span> i=1; i&lt;newGen.length-1; i++) {
      <span Class="KEY">int</span> l=cells[i-1];
      <span Class="KEY">int</span> m=cells[i];
      <span Class="KEY">int</span> r=cells[i+1];
      newGen[i]=next(l,m,r);
    }
    cells=(<span Class="KEY">int</span>[]) newGen.clone();
  }

  <span Class="KEY">int</span> next(<span Class="KEY">int</span> l, <span Class="KEY">int</span> m, <span Class="KEY">int</span> r) {
    <span Class="KEY">int</span> n=0;
    <span Class="KEY">for</span> (<span Class="KEY">int</span> iL=0; iL&lt;States; iL++) {
      <span Class="KEY">for</span> (<span Class="KEY">int</span> iM=0; iM&lt;States; iM++) {
        <span Class="KEY">for</span> (<span Class="KEY">int</span> iR=0; iR&lt;States; iR++) {
          <span Class="KEY">if</span> (l==iL &#038;&#038; m==iM &#038;&#038; r==iR) {
            <span Class="KEY">return</span> rules[n];
          } <span Class="KEY">else</span> {
            n++;
          }
        }
      }
    }
    <span Class="KEY">return</span> 0;
  }

  <span Class="KEY">boolean</span> finished() {
    <span Class="KEY">if</span> (gen &gt; ((<span Class="STR">height</span>-Size*2)/Size)) {
      <span Class="KEY">return</span> <span Class="KEY">true</span>;
    } <span Class="KEY">else</span> {
      <span Class="KEY">return</span> <span Class="KEY">false</span>;
    }
  }
}

<span Class="COM">//---------- Input Scripts
</span>
<span Class="KEY">void</span> <span Class="KEY">keyPressed</span>() {
  <span Class="KEY">if</span> (<span Class="KEY">key</span>==<span Class="STR">'s'</span>) {     <span Class="COM">// <span Class="KEY">save</span> <span Class="KEY">image</span>
</span>    <span Class="KEY">saveFrame</span>(<span Class="STR">&quot;cellular_automata-####.png&quot;</span>);
  } <span Class="KEY">else</span>
  <span Class="KEY">if</span> (<span Class="KEY">key</span>==<span Class="STR">' '</span>) {
    CS.restart();     <span Class="COM">// restart from top, <span Class="KEY">new</span> rules+initial values
</span>  } <span Class="KEY">else</span>
  <span Class="KEY">if</span> (<span Class="KEY">key</span>==<span Class="STR">'r'</span>) {     <span Class="COM">// start back at top with current values
</span>    CS.Continue();
  }
}

<span Class="KEY">void</span> <span Class="KEY">mousePressed</span>() {
  <span Class="KEY">if</span> (<span Class="KEY">mouseButton</span>==<span Class="STR">LEFT</span>) {
    CS.cells[<span Class="KEY">int</span>(<span Class="KEY">mouseX</span>/CS.Size)]=<span Class="KEY">int</span>(<span Class="KEY">random</span>(CS.States));
  }
}</pre>
</div>
]]></content:encoded>
			<wfw:commentRss>http://anthonymattox.com/cellular-automata-2/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>LED Audio Visualizer</title>
		<link>http://anthonymattox.com/led-audio-visualizer</link>
		<comments>http://anthonymattox.com/led-audio-visualizer#comments</comments>
		<pubDate>Wed, 25 Feb 2009 21:26:49 +0000</pubDate>
		<dc:creator>Anthony</dc:creator>
				<category><![CDATA[tutorial]]></category>
		<category><![CDATA[arduino]]></category>
		<category><![CDATA[audio]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[led]]></category>
		<category><![CDATA[processing]]></category>
		<category><![CDATA[sound]]></category>
		<category><![CDATA[spectrum]]></category>
		<category><![CDATA[visualizer]]></category>

		<guid isPermaLink="false">http://www.anthonymattox.com/?p=1018</guid>
		<description><![CDATA[In my last post I explained how to control the brightness of multiple light emitting diodes connected to an Arduino with an interface scripted in Processing. The script which I created was great because it just took a series of values sent via USB and lit the LED&#8217;s appropriately. This is convenient because it is [...]]]></description>
			<content:encoded><![CDATA[<p>In my <a href="http://www.anthonymattox.com/serial-communication-sending-variables-to-the-arduino">last post</a> I explained how to control the brightness of multiple light emitting diodes connected to an <a href="http://www.arduino.cc" target="_blank">Arduino</a> with an interface scripted in <a href="http://www.processing.org" target="_blank">Processing</a>. The script which I created was great because it just took a series of values sent via USB and lit the LED&#8217;s appropriately. This is convenient because it is not specific to any input which might be needed to control the lights. The script to send a value serially along with an indicator character can be added to any <a href="http://www.processing.org" target="_blank">Processing</a> script. Naturally one of the first things I had to do with it was create an audio visualizer. With the <a href="http://www.arduino.cc" target="_blank">Arduino</a> programmed as it was all I had to do was use a sound library to break an audio input into frequency bands and send the values down.</p>
<p><img src="http://www.anthonymattox.com/wp-content/uploads/2009/02/arduino_led_audio_visualizer.jpg" alt="arduino led audio visualizer" title="arduino led audio visualizer" class="alignnone size-medium wp-image-1019" /></p>
<p>The circuit is pretty straight forward. Six LED&#8217;s are connected through resistors to the six pins which support PWM (3, 6, 5, 9, 10, 11) and to a ground pin. I have everything crammed onto a tiny breadboard on my proto-sheild cause it&#8217;s cute and self contained. That&#8217;s just my style. PWM stands for Pulse Width Modulation and is the a way to control the brightness of LED&#8217;s as well as some other components. It is a digital output and produces the effect by switching on and off very quickly. The result can be visualized as a square wave. When you send a higher value to a PWM pin it will spend more time on than off. This blinking is faster than we can see so the LED appears to be changing brightness according to the amount of time it spends in the on position. </p>
<p><img src="http://www.anthonymattox.com/wp-content/uploads/2009/02/pwm-graphs.gif" alt="pulse width modulation graph" title="pulse width modulation graph" class="alignnone size-medium wp-image-1022" /></p>
<p><span id="more-1018"></span></p>
<p>The <a href="http://www.arduino.cc" target="_blank">Arduino</a> is programmed with a script that interprets the serially transmitted data. It uses an array to store all the data and checks for &#8216;indicator&#8217; characters. This lets the program know how to interpret the data. Alternatively there could only be one indicator and then the script could take the next six characters and use them as necessary; however, this allows more flexibility. If only a few pins are needed the same script will continue working. The <a href="http://www.processing.org" target="_blank">Processing</a> script can just send the needed values with appropriate indicators and all unused pins will remain at zero.</p>
<div class="code arduino">
<h4 class="cHead"><span><a href="http://www.arduino.cc">arduino</a> code<br /><a href="http://www.anthonymattox.com/code_formatter">code formatter</a></span>Arduino Script</h4>
<pre><span Class="KEY">int</span> led0=3; <span Class="COM">// variables to store the pin numbers
</span><span Class="KEY">int</span> led1=5;
<span Class="KEY">int</span> led2=6;
<span Class="KEY">int</span> led3=9;
<span Class="KEY">int</span> led4=10;
<span Class="KEY">int</span> led5=11;

<span Class="KEY">int</span> lVal0=0; <span Class="COM">// variables to store the brightness values
</span><span Class="KEY">int</span> lVal1=0;
<span Class="KEY">int</span> lVal2=0;
<span Class="KEY">int</span> lVal3=0;
<span Class="KEY">int</span> lVal4=0;
<span Class="KEY">int</span> lVal5=0;

<span Class="KEY">char</span> buff[]= <span Class="STR">&quot;000000000000&quot;</span>; <span Class="COM">// <span Class="KEY">array</span> that stores incoming serial data
</span>
<span Class="KEY">void</span> <span Class="KEY">setup</span>() {
  <span Class="KEY">Serial</span>.begin(9600);

  <span Class="KEY">pinMode</span>(led0,<span Class="STR">OUTPUT</span>); <span Class="COM">// set up pins
</span>  <span Class="KEY">pinMode</span>(led1,<span Class="STR">OUTPUT</span>);
  <span Class="KEY">pinMode</span>(led2,<span Class="STR">OUTPUT</span>);
  <span Class="KEY">pinMode</span>(led3,<span Class="STR">OUTPUT</span>);
  <span Class="KEY">pinMode</span>(led4,<span Class="STR">OUTPUT</span>);
  <span Class="KEY">pinMode</span>(led5,<span Class="STR">OUTPUT</span>);
}

<span Class="KEY">void</span> <span Class="KEY">loop</span>() {
  <span Class="KEY">analogWrite</span>(led0,lVal0); <span Class="COM">// write the brightness values to the pins
</span>  <span Class="KEY">analogWrite</span>(led1,lVal1);
  <span Class="KEY">analogWrite</span>(led2,lVal2);
  <span Class="KEY">analogWrite</span>(led3,lVal3);
  <span Class="KEY">analogWrite</span>(led4,lVal4);
  <span Class="KEY">analogWrite</span>(led5,lVal5);

  <span Class="KEY">while</span> (<span Class="KEY">Serial</span>.available()&gt;0) { <span Class="COM">// <span Class="KEY">if</span> there is incoming data
</span>    <span Class="KEY">for</span> (<span Class="KEY">int</span> i=0; i&lt;12; i++) {   <span Class="COM">// <span Class="KEY">for</span> each element in the buffer <span Class="KEY">array</span>
</span>      buff[i]=buff[i+1];         <span Class="COM">// take the value of the next element
</span>    }
    buff[12]=<span Class="KEY">Serial</span>.read();      <span Class="COM">// place incoming data in last element
</span>    <span Class="KEY">if</span> (buff[12]==<span Class="STR">'A'</span>) {         <span Class="COM">// <span Class="KEY">if</span> this indicator value is received
</span>      lVal0=<span Class="KEY">int</span>(buff[11]);       <span Class="COM">// place the previous element in lVal0
</span>    }
    <span Class="KEY">if</span> (buff[12]==<span Class="STR">'B'</span>) {         <span Class="COM">// each value has a different indicator
</span>      lVal1=<span Class="KEY">int</span>(buff[11]);
    }
    <span Class="KEY">if</span> (buff[12]==<span Class="STR">'C'</span>) {
      lVal2=<span Class="KEY">int</span>(buff[11]);
    }
    <span Class="KEY">if</span> (buff[12]==<span Class="STR">'D'</span>) {
      lVal3=<span Class="KEY">int</span>(buff[11]);
    }
    <span Class="KEY">if</span> (buff[12]==<span Class="STR">'E'</span>) {
      lVal4=<span Class="KEY">int</span>(buff[11]);
    }
    <span Class="KEY">if</span> (buff[12]==<span Class="STR">'F'</span>) {
      lVal5=<span Class="KEY">int</span>(buff[11]);
    }

  }
  <span Class="KEY">delay</span>(10);
}</pre>
</div>
<p>The <a href="http://www.processing.org" target="_blank">Processing</a> script is a little bit more involved. To get the audio input I use the <a href="http://www.tree-axis.com/Ess/" target="_blank">ESS library</a>, but there are a few others which also work. <a href="http://www.tree-axis.com/Ess/" target="_blank">ESS</a> works great for this purpose. The code is very similar to an audio visualization script explained in an <a href="http://www.anthonymattox.com/visualizing-sound-with-processing">older post</a> but with the addition of frequency band averages to control each LED as well as some sliders to control scaling and damping and of course the serial writing functions.</p>
<p><img src="http://www.anthonymattox.com/wp-content/uploads/2009/02/audio_spectrum-1020.gif" alt="audio spectrum interface" title="audio spectrum interface" class="alignnone size-medium wp-image-1028" /></p>
<div class="code processing">
<h4 class="cHead"><span><a href="http://www.processing.org">processing</a> code<br /><a href="http://www.anthonymattox.com/code_formatter">code formatter</a></span>Processing Script</h4>
<pre><span Class="KEY">import</span> krister.Ess.*;        <span Class="COM">// <span Class="KEY">import</span> audio library
</span><span Class="KEY">import</span> processing.serial.*;  <span Class="COM">// serial communication library
</span>
FFT myfft;           <span Class="COM">// create <span Class="KEY">new</span> FFT object (audio spectrum)
</span>AudioInput myinput;  <span Class="COM">// create input object
</span><span Class="KEY">int</span> bufferSize=256;  <span Class="COM">// variable <span Class="KEY">for</span> number of frequency bands
</span><span Class="KEY">int</span> audioScale;      <span Class="COM">// variable to control scaing
</span>
slider s1;   <span Class="COM">// create two slider objects
</span>slider s2;

Serial port;

<span Class="KEY">void</span> <span Class="KEY">setup</span>() {
  <span Class="KEY">size</span>(510,380);
  <span Class="KEY">frameRate</span>(30);
  <span Class="KEY">background</span>(255);
  <span Class="KEY">noStroke</span>();
  <span Class="KEY">fill</span>(0);
  <span Class="KEY">smooth</span>();

  Ess.start(<span Class="KEY">this</span>);  <span Class="COM">// start audio
</span>  myinput=<span Class="KEY">new</span> AudioInput(bufferSize); <span Class="COM">// define input
</span>  myfft=<span Class="KEY">new</span> FFT(bufferSize*2);        <span Class="COM">// define fft
</span>  myinput.start();

  myfft.damp(.01);        <span Class="COM">// damping creates smoother motion
</span>  myfft.equalizer(<span Class="KEY">true</span>);
  myfft.limits(.005,.01);
  myfft.averages(6);      <span Class="COM">// controls number of averages
</span>
  <span Class="KEY">println</span>(<span Class="STR">&quot;Available serial ports:&quot;</span>);  <span Class="COM">// define <span Class="STR">'port'</span> as first
</span>  <span Class="KEY">println</span>(Serial.list());              <span Class="COM">// ...available serial port
</span>  port = <span Class="KEY">new</span> Serial(<span Class="KEY">this</span>, Serial.list()[0], 9600);

  s1=<span Class="KEY">new</span> slider(400,70,255, <span Class="KEY"><span Class="KEY">color</span></span>(200,150,80)); <span Class="COM">// define slider objects
</span>  s2=<span Class="KEY">new</span> slider(450,70,255, <span Class="KEY"><span Class="KEY">color</span></span>(200,150,80));
  s1.p=100;   <span Class="COM">// <span Class="KEY">default</span> position of sliders
</span>  s2.p=200;
}

<span Class="KEY">void</span> <span Class="KEY">draw</span>() {
  <span Class="KEY">background</span>(255);
  <span Class="KEY">pushStyle</span>();
  s1.render();  <span Class="COM">// render sliders
</span>  s2.render();
  <span Class="KEY">popStyle</span>();

  audioScale=s1.p*20; <span Class="COM">// adjust audio <span Class="KEY">scale</span> according to slider
</span>  myfft.damp(<span Class="KEY">map</span>(s2.p,0,255,.01,.1)); <span Class="COM">// adjust daming
</span>
  <span Class="KEY">for</span> (<span Class="KEY">int</span> i=0; i&lt;bufferSize;i++) {  <span Class="COM">// <span Class="KEY">draw</span> frequency spectrum
</span>    <span Class="KEY">rect</span>((i*1)+50,330,1,myfft.spectrum[i]*(-audioScale));
  }
  <span Class="KEY">pushStyle</span>();
  <span Class="KEY">for</span> (<span Class="KEY">int</span> i=0; i&lt;6; i++) { <span Class="COM">// <span Class="KEY">draw</span> averages
</span>    <span Class="KEY">int</span> a=<span Class="KEY">int</span>(myfft.averages[i]*(-audioScale));
    <span Class="KEY">fill</span>(200,150,80,100);
    <span Class="KEY">rect</span>((i*43)+50,330,43,a);
    <span Class="KEY">fill</span>(200,150,0);
    <span Class="KEY">rect</span>((i*43)+50,330+a,43,3);
  }
  <span Class="KEY">popStyle</span>();
  <span Class="COM">// write each average to the serial port followed by indicator character
</span>  <span Class="COM">// the values are constrained from 0 to 255 so the arduino can handle them
</span>  <span Class="COM">// values above 255 would start back at zero
</span>  port.write(<span Class="KEY">int</span>(<span Class="KEY">constrain</span>(myfft.averages[0]*audioScale,0,255)));
  port.write(<span Class="STR">'A'</span>);
  port.write(<span Class="KEY">int</span>(<span Class="KEY">constrain</span>(myfft.averages[1]*audioScale,0,255)));
  port.write(<span Class="STR">'B'</span>);
  port.write(<span Class="KEY">int</span>(<span Class="KEY">constrain</span>(myfft.averages[2]*audioScale,0,255)));
  port.write(<span Class="STR">'C'</span>);
  port.write(<span Class="KEY">int</span>(<span Class="KEY">constrain</span>(myfft.averages[3]*audioScale,0,255)));
  port.write(<span Class="STR">'D'</span>);
  port.write(<span Class="KEY">int</span>(<span Class="KEY">constrain</span>(myfft.averages[4]*audioScale,0,255)));
  port.write(<span Class="STR">'E'</span>);
  port.write(<span Class="KEY">int</span>(<span Class="KEY">constrain</span>(myfft.averages[5]*audioScale,0,255)));
  port.write(<span Class="STR">'F'</span>);
}

<span Class="COM">// sets up audio input
</span><span Class="KEY">public</span> <span Class="KEY">void</span> audioInputData(AudioInput theInput) {
  myfft.getSpectrum(myinput);
}</pre>
</div>
<p>You&#8217;ll need to grab the slider class from my <a href="http://www.anthonymattox.com/serial-communication-sending-variables-to-the-arduino">last post</a> (at the bottom) for this to work or you can just define the audio damping and scaling within the setup function and take them out all together. You will also need to download the <a href="http://www.tree-axis.com/Ess/" target="_blank">ESS sound library</a> and have it placed in the proper folder. The serial library is included in <a href="http://www.processing.org" target="_blank">Processing</a> by default but must be called in order to function.</p>
<p>The result is the brightness if each LED will change according to the volume of the respective frequency in the audio spectrum. As a certain pitch increases that LED will become brighter. Using the averages rather than just specific frequency bands gives more accurate results, allows for any number of outputs, and also allows for a nice visual. The full spectrum with the overlaid averages shows what&#8217;s gong on better than just a few chunks.</p>
]]></content:encoded>
			<wfw:commentRss>http://anthonymattox.com/led-audio-visualizer/feed</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Serial Communication: Sending Variables to the Arduino</title>
		<link>http://anthonymattox.com/serial-communication-sending-variables-to-the-arduino</link>
		<comments>http://anthonymattox.com/serial-communication-sending-variables-to-the-arduino#comments</comments>
		<pubDate>Tue, 24 Feb 2009 03:01:30 +0000</pubDate>
		<dc:creator>Anthony</dc:creator>
				<category><![CDATA[tutorial]]></category>
		<category><![CDATA[arduino]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[led]]></category>
		<category><![CDATA[processing]]></category>
		<category><![CDATA[serial communication]]></category>

		<guid isPermaLink="false">http://www.anthonymattox.com/?p=1005</guid>
		<description><![CDATA[As I started working with my shiny new Arduino board I quickly learned how to build basic circuits and program the micro controller to respond to various stimuli, but what really interested me in the technology was the ability to send information between the board and a computer. I&#8217;ve done a fair amount of programming [...]]]></description>
			<content:encoded><![CDATA[<p>As I started working with my shiny new <a href="http://www.arduino.cc" target="_blank">Arduino</a> board I quickly learned how to build basic circuits and program the micro controller to respond to various stimuli, but what really interested me in the technology was the ability to send information between the board and a computer. I&#8217;ve done a fair amount of programming in <a href="http://www.processing.org" target="_blank">Processing</a> and was excited to bring some of my work off screen. There is a nice script on the <a href="http://www.arduino.cc" target="_blank">Arduino</a> site on sending data from the board to a computer to control elements within a processing sketch. Unfortunately, the reverse is not as well documented.</p>
<p>I needed a script to send multiple variables from <a href="http://www.processing.org" target="_blank">Processing</a> to an <a href="http://www.arduino.cc" target="_blank">Arduino</a> to control a few components. After a bit of research, and more trial and error, I put together a script which controls the brightness of three LED&#8217;s through a virtual interface on a monitor. Here are my scripts and the circuit I came up with. It seems simple enough to me but if anyone has suggestions I&#8217;d love to hear them.</p>
<p><img src="http://www.anthonymattox.com/wp-content/uploads/2009/02/arduino_led_serial_pixel.jpg" alt="physical LED pixel on arduio board controlled with processing" title="physical LED pixel on arduio board controlled with processing" class="alignnone size-medium wp-image-1006" /></p>
<p>Here three color LED&#8217;s are connected to pins 9, 10, and 11 which can use Pulse Width Modulation (PWM) to control the brightness of each LED. They are connected to their respective pins through appropriate resistors and also connected to a grounded row in the bread board. In the image I am using the prototyping shield from <a href="http://www.adafruit.com/" target="_blank">Adafruit</a> which makes it easy to build small circuits.</p>
<p><span id="more-1005"></span></p>
<div class="code arduino">
<h4 class="cHead"><span><a href="http://www.arduino.cc">arduino</a> code<br /><a href="http://www.anthonymattox.com/code_formatter">code formatter</a></span>LED Pixel</h4>
<pre><span Class="COM">/* Led Pixel script - www.anthonymattox.com
 * recieves input from processing script  */</span>

<span Class="KEY">int</span> led0=9; <span Class="KEY">int</span> led1=10; <span Class="KEY">int</span> led2=11;
<span Class="KEY">int</span> rval=0; <span Class="KEY">int</span> gval=0; <span Class="KEY">int</span> bval=0;

<span Class="KEY">char</span> buff[]= <span Class="STR">&quot;0000000000&quot;</span>;

<span Class="KEY">void</span> <span Class="KEY">setup</span>() {
  <span Class="KEY">Serial</span>.begin(9600);
  <span Class="KEY">pinMode</span>(led0,<span Class="STR">OUTPUT</span>);
  <span Class="KEY">pinMode</span>(led1,<span Class="STR">OUTPUT</span>);
  <span Class="KEY">pinMode</span>(led2,<span Class="STR">OUTPUT</span>);
}

<span Class="KEY">void</span> <span Class="KEY">loop</span>() {
  <span Class="KEY">analogWrite</span>(led0,bval);
  <span Class="KEY">analogWrite</span>(led1,gval);
  <span Class="KEY">analogWrite</span>(led2,rval);

  <span Class="KEY">while</span> (<span Class="KEY">Serial</span>.available()&gt;0) {
    <span Class="KEY">for</span> (<span Class="KEY">int</span> i=0; i&lt;10; i++) {
      buff[i]=buff[i+1];
    }
    buff[10]=<span Class="KEY">Serial</span>.read();
    <span Class="KEY">if</span> (buff[10]==<span Class="STR">'R'</span>) {
      rval=<span Class="KEY">int</span>(buff[9]);
    }
    <span Class="KEY">if</span> (buff[10]==<span Class="STR">'G'</span>) {
      gval=<span Class="KEY">int</span>(buff[9]);
    }
    <span Class="KEY">if</span> (buff[10]==<span Class="STR">'B'</span>) {
      bval=<span Class="KEY">int</span>(buff[9]);
    }

  }
  <span Class="KEY">delay</span>(10);
}</pre>
</div>
<p>The script uses an array called buff to store all the incoming serial data. The While loop takes each incoming character and adds it to the end of the buff array after shifting all the values in the array down one. If it receives an indicator character, something created arbitrarily, it takes the previous character and puts it into another variable to be used later. Processing is sending a continues stream of data and this system of indicators allows the <a href="http://www.arduino.cc" target="_blank">Arduino</a> to take out the necessary information and use it to control the LED&#8217;s. <a href="http://www.processing.org" target="_blank">Processing</a> sends the indicator after the information so if a value is ever multiple characters when it gets the indicator the values are already in the buff array and can be used.</p>
<p>The <a href="http://www.processing.org" target="_blank">Processing</a> script creates three instances of a slider object and in every loop of draw sends the value of each slider, serially through a USB cable, followed by the appropriate indicator character.</p>
<div class="code processing">
<h4 class="cHead"><span><a href="http://www.processing.org">processing</a> code<br /><a href="http://www.anthonymattox.com/code_formatter">code formatter</a></span>LED Pixel Processing Script</h4>
<pre><span Class="KEY">import</span> processing.serial.*;

Serial port;
slider s1;
slider s2;
slider s3;

<span Class="KEY">void</span> <span Class="KEY">setup</span>() {
  <span Class="KEY">size</span>(200, 296);
  <span Class="KEY">smooth</span>();

  <span Class="KEY">println</span>(<span Class="STR">&quot;Available serial ports:&quot;</span>);
  <span Class="KEY">println</span>(Serial.list());
  port = <span Class="KEY">new</span> Serial(<span Class="KEY">this</span>, Serial.list()[0], 9600);

  s1=<span Class="KEY">new</span> slider(70,20,255, <span Class="KEY"><span Class="KEY">color</span></span>(255,0,0));
  s2=<span Class="KEY">new</span> slider(100,20,255, <span Class="KEY"><span Class="KEY">color</span></span>(0,255,0));
  s3=<span Class="KEY">new</span> slider(130,20,255, <span Class="KEY"><span Class="KEY">color</span></span>(0,0,255));
}

<span Class="KEY">void</span> <span Class="KEY">draw</span>() {
  <span Class="KEY">background</span>(0);
  s1.render();
  s2.render();
  s3.render();
  port.write(s1.p);
  port.write(<span Class="STR">'R'</span>);
  port.write(s2.p);
  port.write(<span Class="STR">'G'</span>);
  port.write(s3.p);
  port.write(<span Class="STR">'B'</span>);
}</pre>
</div>
<p>And the slider class which builds the interface looks like this</p>
<p><img src="http://www.anthonymattox.com/wp-content/uploads/2009/02/processing_led_pixel_control.jpg" alt="processing led pixel control" title="processing led pixel control" class="alignnone size-medium wp-image-1007" /></p>
<div class="code processing">
<h4 class="cHead"><span><a href="http://www.processing.org">processing</a> code<br /><a href="http://www.anthonymattox.com/code_formatter">code formatter</a></span>Slider UI Element Class</h4>
<pre><span Class="COM">/* Slider Class - www.anthonymattox.com */</span>

<span Class="KEY">class</span> slider {
  <span Class="KEY">int</span> xpos, ypos, thesize, p;
  <span Class="KEY">boolean</span> slide;
  <span Class="KEY"><span Class="KEY">color</span></span> c, cb;
  slider (<span Class="KEY">int</span> x, <span Class="KEY">int</span> y, <span Class="KEY">int</span> s, <span Class="KEY"><span Class="KEY">color</span></span> col) {
    xpos=x;
    ypos=y;
    thesize=s;
    p=0;
    slide=<span Class="KEY">true</span>;
    c=col;
    cb=<span Class="KEY"><span Class="KEY">color</span></span>(<span Class="KEY">red</span>(c),<span Class="KEY">green</span>(c),<span Class="KEY">blue</span>(c),150);
  }

  <span Class="KEY">void</span> render() {
    <span Class="KEY">stroke</span>(40);
    <span Class="KEY">strokeWeight</span>(10);
    <span Class="KEY">noFill</span>();
    <span Class="KEY">line</span>(xpos,ypos,xpos,ypos+thesize);

    <span Class="KEY">stroke</span>(80);
    <span Class="KEY">strokeWeight</span>(2);
    <span Class="KEY">noFill</span>();
    <span Class="KEY">line</span>(xpos,ypos,xpos,ypos+thesize);

    <span Class="KEY">noStroke</span>();
    <span Class="KEY">fill</span>(cb);
    <span Class="KEY">ellipse</span>(xpos, thesize-p+ypos, 17, 17);
    <span Class="KEY">fill</span>(c);
    <span Class="KEY">ellipse</span>(xpos, thesize-p+ypos, 13, 13);

    <span Class="COM">//<span Class="KEY">text</span>(thesize-dialy,xpos+10,dialy+ypos+5);
</span>
    <span Class="COM">// replace the +'s with <span Class="KEY">double</span> ampersands (web display issues)
</span>    <span Class="KEY">if</span> (slide=<span Class="KEY">true</span> + <span Class="KEY">mousePressed</span>==<span Class="KEY">true</span> + <span Class="KEY">mouseX</span>&lt;xpos+15 + <span Class="KEY">mouseX</span>&gt;xpos-15){
      <span Class="KEY">if</span> ((<span Class="KEY">mouseY</span>&lt;=ypos+thesize+10) &#038;&#038; (<span Class="KEY">mouseY</span>&gt;=ypos-10)) {
        p=(3*p+(thesize-(<span Class="KEY">mouseY</span>-ypos)))/4;
        <span Class="KEY">if</span> (p&lt;0) {
          p=0;
        } <span Class="KEY">else</span> <span Class="KEY">if</span> (p&gt;thesize) {
          p=thesize;
        }
      }
    }
  }
}</pre>
</div>
<p>Let me know is this is helpful or if you have any questions or problems.</p>
]]></content:encoded>
			<wfw:commentRss>http://anthonymattox.com/serial-communication-sending-variables-to-the-arduino/feed</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Arduino LED Display</title>
		<link>http://anthonymattox.com/arduino-led-display</link>
		<comments>http://anthonymattox.com/arduino-led-display#comments</comments>
		<pubDate>Wed, 11 Feb 2009 05:11:11 +0000</pubDate>
		<dc:creator>Anthony</dc:creator>
				<category><![CDATA[article]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[arduino]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[electronics]]></category>
		<category><![CDATA[led]]></category>
		<category><![CDATA[script]]></category>

		<guid isPermaLink="false">http://www.anthonymattox.com/?p=977</guid>
		<description><![CDATA[My processing work in my blog as slowed to a trickle recently. This is due to a couple of large projects which I can&#8217;t wait to reveal. One is a fun web project and the other a flash application. I&#8217;ve also been playing with my fancy new toy here and am beginning to make some [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.anthonymattox.com/wp-content/uploads/2009/02/arduino_led_display.jpg" alt="arduino led display" title="arduino led display" class="alignnone size-medium wp-image-978" /></p>
<p>My processing work in my blog as slowed to a trickle recently. This is due to a couple of large projects which I can&#8217;t wait to reveal. One is a fun web project and the other a flash application. I&#8217;ve also been playing with my fancy new toy here and am beginning to make some progress. Along with the <a href="http://www.arduino.cc" target="_blank">Arduino</a> board I&#8217;m also using a prototyping shield from <a href="http://www.adafruit.com/" target="_blank">Adafruit</a>. It creates a nice little workspace with a breadboard and some extra power sources and grounds. It also contains two led&#8217;s, one of which I wired to digital pin 0 on the underside of the board. This just keeps the light on (as long as I&#8217;m not using the pin for something else) when the board has power, and makes it blink when data is being transmitted. The shield covers up the power and transmission led&#8217;s on the <a href="http://www.arduino.cc" target="_blank">Arduino</a> itself.</p>
<p>In the photo above I have an led display I took out of something else and, for the sake of learning, programmed. Turning the potentiometer controls the display. Nothing thrilling, but it&#8217;s good practice. I&#8217;ve also been trying to understand serial communication so I can build devices which send and receive complex data.</p>
<p>Anyway, there&#8217;s a quick update, and here is my <a href="http://www.arduino.cc" target="_blank">Arduino</a> code in case it might be useful to someone. It&#8217;s not put together very well. In particular there should be a 2d array containing all the numerals and their corresponding led&#8217;s. But nonetheless.</p>
<p><span id="more-977"></span></p>
<div class="code arduino">
<h4 class="cHead"><span><a href="http://www.arduino.cc">arduino</a> code<br /><a href="http://www.anthonymattox.com/code_formatter">code formatter</a></span>Arduino LED Display</h4>
<p><!-- formatted code --></p>
<pre><span Class="KEY">int</span> v=6;
<span Class="KEY">int</span> d[8];
<span Class="KEY">int</span> t=0;
<span Class="KEY">int</span> b=12;
<span Class="KEY">int</span> p=14; <span Class="COM">// potentiometer input
</span><span Class="KEY">int</span> pval; <span Class="COM">// pot value
</span><span Class="KEY">int</span> out;

<span Class="KEY">void</span> <span Class="KEY">setup</span>() {
  <span Class="KEY">for</span> (<span Class="KEY">int</span> i=4; i&lt;=11; i++) {
    <span Class="KEY">pinMode</span>(i, <span Class="STR">OUTPUT</span>);
  }
  <span Class="KEY">pinMode</span>(b, <span Class="STR">INPUT</span>);
  <span Class="KEY">pinMode</span>(p, <span Class="STR">INPUT</span>);
  <span Class="KEY">Serial</span>.begin(9600);
}

<span Class="KEY">void</span> <span Class="KEY">loop</span>() {
  out=<span Class="KEY">analogRead</span>(p);
  <span Class="KEY">Serial</span>.println(<span Class="KEY">analogRead</span>(0));
  <span Class="COM">//<span Class="KEY">for</span> button
</span>  <span Class="COM">/*
  <span Class="KEY">if</span> (<span Class="KEY">digitalRead</span>(b)==<span Class="STR">LOW</span>) {
    v++;
    <span Class="KEY">if</span> (v&gt;9) {
      v=0;
    }
  }
  */</span>
  v=<span Class="KEY">int</span>(<span Class="KEY">analogRead</span>(0))/100; <span Class="COM">// <span Class="KEY">for</span> potentiometer
</span>  d[0]=0;
  <span Class="KEY">if</span> (v==0 || v==1 || v==3 || v==4 || v==5 || v==6 || v==7 || v==8 || v==9) {
    d[1]=1;
  } <span Class="KEY">else</span> {
    d[1]=0;
  }
  <span Class="KEY">if</span> (v==2 || v==3 || v==4 || v==5 || v==6 || v==8 || v==9) {
    d[2]=1;
  } <span Class="KEY">else</span> {
    d[2]=0;
  }
  <span Class="KEY">if</span> (v==0 || v==1 || v==2 || v==3 || v==4 || v==7 || v==8 || v==9) {
    d[3]=1;
  } <span Class="KEY">else</span> {
    d[3]=0;
  }
  <span Class="KEY">if</span> (v==0 || v==2 || v==3 || v==5 || v==6 || v==7 || v==8 || v==9) {
    d[4]=1;
  } <span Class="KEY">else</span> {
    d[4]=0;
  }
  <span Class="KEY">if</span> (v==0 || v==4 || v==5 || v==6 || v==8 || v==8 || v==9) {
    d[5]=1;
  } <span Class="KEY">else</span> {
    d[5]=0;
  }
  <span Class="KEY">if</span> (v==0 || v==2 || v==6 || v==8) {
    d[6]=1;
  } <span Class="KEY">else</span> {
    d[6]=0;
  }
  <span Class="KEY">if</span> (v==0 || v==2 || v==3 || v==5 || v==6 || v==8) {
    d[7]=1;
  } <span Class="KEY">else</span> {
    d[7]=0;
  }
  <span Class="KEY">for</span> (<span Class="KEY">int</span> i=4; i&lt;=11; i++) {
    <span Class="KEY">if</span> (d[i-4]==1) {
      <span Class="KEY">digitalWrite</span>(i, <span Class="STR">HIGH</span>);
    } <span Class="KEY">else</span> {
      <span Class="KEY">digitalWrite</span>(i, <span Class="STR">LOW</span>);
    }
  }
  <span Class="KEY">delay</span>(20);
}</pre>
</div>
]]></content:encoded>
			<wfw:commentRss>http://anthonymattox.com/arduino-led-display/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Perlin Noise</title>
		<link>http://anthonymattox.com/perlin-noise</link>
		<comments>http://anthonymattox.com/perlin-noise#comments</comments>
		<pubDate>Mon, 26 Jan 2009 04:39:29 +0000</pubDate>
		<dc:creator>Anthony</dc:creator>
				<category><![CDATA[tutorial]]></category>
		<category><![CDATA[3d]]></category>
		<category><![CDATA[noise]]></category>
		<category><![CDATA[opengl]]></category>
		<category><![CDATA[perlin noise]]></category>
		<category><![CDATA[processing]]></category>
		<category><![CDATA[source code]]></category>
		<category><![CDATA[texture]]></category>

		<guid isPermaLink="false">http://www.anthonymattox.com/?p=919</guid>
		<description><![CDATA[Perlin noise is a pseudo-random gradient texture, developed by Ken Perlin beginning with his work on the 1982 movie Tron. It continues to be a great tool to create textures and dynamic elements. The function generates a continuous string of values in any number of dimensions. Although it was initially developed to build textures it [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.anthonymattox.com/wp-content/uploads/2009/01/particles_perlin_flow-3151.jpg" alt="perlin noise flow field particle system 3151" title="perlin noise flow field particle system 3151"  class="alignnone size-medium wp-image-923" /></p>
<p>Perlin noise is a pseudo-random gradient texture, developed by Ken Perlin beginning with his work on the 1982 movie Tron. It continues to be a great tool to create textures and dynamic elements. The function generates a continuous string of values in any number of dimensions. Although it was initially developed to build textures it can be very useful for many other things such as particle motion. Noise is generated by a series oscillations over a variety of frequencies, similar to an audio signal.</p>
<p>Processing supports Perlin noise in up to three dimensions and can be implemented by calling the noise function with the parameters for the coordinate. I&#8217;ve been playing with using Three dimensional noise to create an animated force field or flow field in which particles move and thought I&#8217;d build a little tutorial to demonstrate some useful applications.</p>
<p><span id="more-919"></span></p>
<p><img src="http://www.anthonymattox.com/wp-content/uploads/2009/01/2d_and_3d_perlin_noise.jpg" alt="2d &#038; 3d Perlin Noise" title="2d &#038; 3d Perlin Noise" class="alignnone size-medium wp-image-921" /></p>
<p>Above we have examples of two and three dimensional noise. The two dimensional is actually a 2d slice of 3d noise. The third is projected across time to create an animated texture. This is done simply by having a variable incrementing each frame which is passed in as the Z values.</p>
<div class="code processing">
<h4 class="cHead"><span><a href="http://www.processing.org">processing</a> code<br /><a href="http://www.anthonymattox.com/code_formatter">code formatter</a></span>Simple Animated Noise</h4>
<pre><span Class="KEY">float</span> age;

<span Class="KEY">void</span> <span Class="KEY">setup</span>() {
  <span Class="KEY">size</span>(350,350);
  age=0;
  <span Class="KEY">noiseDetail</span>(6,.4);
}

<span Class="KEY">void</span> <span Class="KEY">draw</span>() {
  <span Class="KEY">background</span>(255);
  age+=.02;
  <span Class="KEY">for</span> (<span Class="KEY">float</span> x=0; x&lt;=<span Class="STR">width</span>; x++) {
    <span Class="KEY">for</span> (<span Class="KEY">float</span> y=0; y&lt;=<span Class="STR">height</span>; y++) {
      <span Class="KEY">stroke</span>(<span Class="KEY">noise</span>(x/200,y/200,age)*255);
      <span Class="KEY">point</span>(x,y);
    }
  }
}</pre>
</div>
<p>Two nested loops create a virtual grid of data. For each x and y coordinate representing a pixel the noise is calculated and drawn. This requires two scalings. First the coordinates are scaled down when the values are calculated to get a smoother result. The more zoomed into the noise grid the smoother the output will be. The function then returns a value between 0 and 1 which must be scaled back up to a full color range.</p>
<p>This is a basic animated noise structure which could be applied to any more complicated system. In the particle systems I&#8217;ve been working with two overlapping noise grids (really just two sections of the same) represent the x and y forces across the screen or the magnitude and direction of the force. The latter is better, but takes a little trigonometry and more processor power. In addition to interparticle forces, each particle is pushed based on it&#8217;s position within this field. This creates very interesting textures, especially if particles have varied weights and are effected differently.</p>
<div class="code processing">
<h4 class="cHead"><span><a href="http://www.processing.org">processing</a> code<br /><a href="http://www.anthonymattox.com/code_formatter">code formatter</a></span>Simple 3d Noise</h4>
<pre><span Class="KEY">import</span> processing.opengl.*;

<span Class="KEY">void</span> <span Class="KEY">setup</span>() {
  <span Class="KEY">size</span>(350,350,OPENGL);
  <span Class="KEY">noiseDetail</span>(6,.7);
  <span Class="KEY">noStroke</span>();
}

<span Class="KEY">void</span> <span Class="KEY">draw</span>() {
  <span Class="KEY">translate</span>(<span Class="STR">width</span>/2,<span Class="STR">height</span>/2);
  <span Class="KEY">background</span>(255);
  <span Class="KEY">for</span> (<span Class="KEY">float</span> x=0; x&lt;=10; x++) {
    <span Class="KEY">for</span> (<span Class="KEY">float</span> y=0; y&lt;=10; y++) {
      <span Class="KEY">for</span> (<span Class="KEY">float</span> z=0; z&lt;=10; z++) {
        <span Class="KEY">float</span> n=<span Class="KEY">noise</span>(x/3,y/3,z/3)-.65;
        <span Class="KEY">if</span> (n&gt;0) {
          <span Class="KEY">float</span> r=n*25;
          <span Class="KEY">fill</span>(0,n*255);
          <span Class="KEY"><span Class="KEY">pushMatrix</span></span>();
          <span Class="KEY">translate</span>((x-5)*20,(y-5)*20,(z-5)*20);
          <span Class="KEY">box</span>(r,r,r);
          <span Class="KEY">popMatrix</span>();
        }
      }
    }
  }
}</pre>
</div>
<p>3d Noise is more or less the same. Adding another for loop allows us to plot three dimensions. Here each point is represented by a cube scaled and filled based on the three dimensional noise, which is tweaked to get the most useful results. The loops may not be necessary in many applications where noise(x,y,z); can be called whenever at necessary coordinates, but they provide a visual of the full grid.</p>
<p>It is important to understand that the Perlin Noise field will remain the same as the program is run unless noiseSeed(int); is called, and although these examples show a finite grid, the values extend infinitely and remain continuous at any scale. The Seed is randomized when the program initializes but to work with the same noise it can be called with a specific integer, or a function like the one below can be used to generate a new set of values. </p>
<div class="code processing">
<pre><span Class="KEY">void</span> <span Class="KEY">mousePressed</span>() {
  <span Class="KEY">noiseSeed</span>(<span Class="KEY">int</span>(<span Class="KEY">random</span>(1000)));
}</pre>
</div>
<p>And that&#8217;s all I know about noise. Hooray and good luck.</p>
<p>Ken Perlin has some <a href="http://www.noisemachine.com/talk1/" target="_blank">nice information</a> put together if you&#8217;d like some more information on the development and the inner workings if his program.</p>
]]></content:encoded>
			<wfw:commentRss>http://anthonymattox.com/perlin-noise/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

