<?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>Wouter Hisschemöller &#187; Wouter</title>
	<atom:link href="http://www.hisschemoller.com/author/wouter/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.hisschemoller.com</link>
	<description>Flash actionscript and audio programming. And Java.</description>
	<lastBuildDate>Sun, 25 Sep 2011 19:53:29 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>Euclidean Pattern Generator 1.1 &#8211; OSC support</title>
		<link>http://www.hisschemoller.com/2011/euclidean-pattern-generator-1-1-osc-support/</link>
		<comments>http://www.hisschemoller.com/2011/euclidean-pattern-generator-1-1-osc-support/#comments</comments>
		<pubDate>Sun, 25 Sep 2011 19:53:29 +0000</pubDate>
		<dc:creator>Wouter</dc:creator>
				<category><![CDATA[Euclidean rhythms]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[MIDI]]></category>
		<category><![CDATA[OSC]]></category>

		<guid isPermaLink="false">http://www.hisschemoller.com/?p=770</guid>
		<description><![CDATA[Summer is at it’s end and it’s time to do some programming again. The Euclidean rhythms application I made earlier this year still has a lot of potential for new functionality, so here is the new version 1.1. The main new feature in this version is support for Open Sound Control (OSC). After the last [...]]]></description>
			<content:encoded><![CDATA[<div id="attachment_771" class="wp-caption alignright" style="width: 268px"><img class="size-full wp-image-771" title="Pure Data OSC patch" src="http://www.hisschemoller.com/wp-content/uploads/2011/09/puredatapatch.gif" alt="Pure Data OSC patch" width="268" height="214" /><p class="wp-caption-text">Pure Data OSC test patch</p></div>Summer is at it’s end and it’s time to do some programming again. The Euclidean rhythms application I made earlier this year still has a lot of potential for new functionality, so here is the new version 1.1.</p>
<p>The main new feature in this version is support for <a href="http://opensoundcontrol.org">Open Sound Control</a> (OSC). After the last version a great offer came from programmer Michael Heuer to collaborate and add OSC support to the application. So this part of the program is very much his work. You can view more of his <a href="http://github.com/heuermh">projects on GitHub</a>.</p>
<p><span id="more-770"></span></p>
<h2>The application</h2>
<p><div id="attachment_772" class="wp-caption aligncenter" style="width: 540px"><img class="size-full wp-image-772" title="Euclidean Pattern Generator v1.1" src="http://www.hisschemoller.com/wp-content/uploads/2011/09/app_whole.jpg" alt="Euclidean Pattern Generator v1.1" width="540" height="371" /><p class="wp-caption-text">Euclidean Pattern Generator v1.1</p></div>
<p>As usual here are some download links for the app and the source files. Note that the application is now released under the GPL v3 license. If you’re a developer you might find it easiest to visit the GitHub repository.</p>
<p><a href="http://www.hisschemoller.com/projects/v022_epg_1_1/epg_1_1_jar.zip">Download Java JAR file.</a><br />
<a href="http://www.hisschemoller.com/projects/v022_epg_1_1/epg_1_1_app.zip">Download Mac OS X APP file.</a><br />
<a href="http://www.hisschemoller.com/projects/v022_epg_1_1/epg_1_1_project.zip">Download Java source files (Eclipse project), JAR and APP, all in one ZIP file.</a><br />
<a href="https://github.com/WouterHisschemoller/Euclidean-Pattern-Generator/tree/v1.1">GitHub repository for the Euclidean Pattern Generator.</a></p>
<h2>The OSC implementation</h2>
<p>All OSC messages are sent to ‘localhost’ on a port number you can specify yourself. In the top part of the application I added a text input labelled ‘OSC Port’. Here you can enter the port number of the receiving OSC server you want to send the OSC messages to.</p>
<p>Because there is a choice now between MIDI and OSC I added checkboxes ‘Send OSC’ and ‘Send MIDI’. Only when these are checked OSC and / or MIDI messages will actually be sent.</p>
<p>The receiving OSC application is able to identify which pattern sent the message if an address for the pattern is entered.<br />
In the pattern’s MIDI / OSC Settings panel there is a new text input field labelled ‘OSC Address’. This field is automatically filled with a default address ‘/eu/patternX’ where ‘X’ is a number that is auto-increased with each new created pattern. Of course you can use your own preferred address naming scheme as well.</p>
<p>Here is a video that demonstrates sending OSC messages from the application to a Pure Data patch.</p>
<p><iframe class="youtube-player" type="text/html" width="540" height="335" src="http://www.youtube.com/embed/5x_FaPc8grU" frameborder="0"><br />
</iframe></p>
<h2>Other new features</h2>
<p>Next to the Open Sound Control support there are a number of other new features, some of which were requested by users in comments on the previous version.</p>
<ul>
<li>Step duration (quantization) from 4 beats (one 4/4 measure) to 1/8 beat (1/32 note).</li>
<li>Tempo maximum increased to 300 BPM.</li>
<li>Pattern length up to 64 steps. The pattern grows in size to accommodate the number of step circles.</li>
<li>Patterns can be named. New text input <em>Pattern name</em> in the pattern’s <em>Other Settings</em> panel. Whatever is typed here appears under the pattern graphic. When there are a lot of patterns in a project it’s easy to lose track of which pattern does what, so now you can name them ‘kick’, ‘snare’ etc.</li>
<li>‘All Notes Off’ button to kill hanging notes. This sends an ‘All Notes Off’ MIDI Channel Mode Message  to each channel. All Notes Off is actually MIDI CC #123 with value 0 (see the <a href="http://www.midi.org/techspecs/midimessages.php">MIDI specification</a>).</li>
<li>New patterns start with velocity 10 (handy in live situations where you don’t want a new pattern to come in at full volume).</li>
<li>Preferences: Several settings are automatically stored and recalled the next time you open the application.
<ul>
<li>Window size and position.</li>
<li>MIDI enabled and MIDI Out port.</li>
<li>OSC enabled and OSC port number.</li>
<li>Last used directory to open or save a project.</li>
</ul>
</li>
</ul>
<p>And lastly, not really a new feature, but ‘behind the scenes’ the user interface now uses <a href="http://www.swixml.org">SwiXML</a>, a layout engine that uses XML files to define an interface built with Java Swing components. This makes the interface much more flexible and easier to maintain for future changes.</p>
<h2>Next version</h2>
<p>In the next version I’ll add MIDI input and I hope it will be possible to get the app to synchronize to external MIDI clock. That would be cool.</p>
<p>And I’d like to add triplets to the quantization options. For even more unusual polyrhythms.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hisschemoller.com/2011/euclidean-pattern-generator-1-1-osc-support/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Euclidean MIDI Patterns</title>
		<link>http://www.hisschemoller.com/2011/euclidean-midi-patterns/</link>
		<comments>http://www.hisschemoller.com/2011/euclidean-midi-patterns/#comments</comments>
		<pubDate>Sun, 22 May 2011 13:48:55 +0000</pubDate>
		<dc:creator>Wouter</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[MIDI]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[euclidean]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[midi]]></category>
		<category><![CDATA[pattern]]></category>
		<category><![CDATA[polyrhythms]]></category>
		<category><![CDATA[rhythm]]></category>

		<guid isPermaLink="false">http://www.hisschemoller.com/?p=645</guid>
		<description><![CDATA[In January I posted a Euclidean rhythm generator in Flash. Polyrhythms generated by a mathematical algorithm. I wanted the next version to be able to send MIDI, so I could use it with software like Ableton Live and hardware music machines like my Elektron Machinedrum. Since Flash doesn’t do MIDI I spent the last few [...]]]></description>
			<content:encoded><![CDATA[<div id="attachment_646" class="wp-caption alignright" style="width: 270px"><a href="http://www.hisschemoller.com/2011/euclidean-midi-patterns/java_euclid_1/" rel="attachment wp-att-646"><img src="http://www.hisschemoller.com/wp-content/uploads/2011/05/java_euclid_1.jpg" alt="Euclidean MIDI patterns screenshot" title="Screenshot" width="270" height="200" class="size-full wp-image-646" /></a><p class="wp-caption-text">Screenshot</p></div>In January I <a href="http://www.hisschemoller.com/2011/euclidean-rhythms/">posted</a> a Euclidean rhythm generator in Flash. Polyrhythms generated by a mathematical algorithm. I wanted the next version to be able to send MIDI, so I could use it with software like <a href="http://www.ableton.com/live">Ableton Live</a> and hardware music machines like my <a href="http://www.vintagesynth.com/misc/machinedrum.php">Elektron Machinedrum</a>.</p>
<p>Since Flash doesn’t do MIDI I spent the last few months learning Java and rebuilt the pattern generator to add MIDI capabilities. </p>
<p><span id="more-645"></span></p>
<h1>Running the application</h1>
<p>The Java app doesn’t run in the browser, so I can’t show it embedded on the web page like I’m used to with Flash projects. Instead you’ll have to download the Java .jar file and run it on your computer like you would any regular desktop application. For Mac users there’s a OS X .app file as well.<br />
I haven’t looked into creating an .exe file for Windows users yet, but double clicking the .jar file should start up the program just like an .exe.</p>
<p><a href="http://www.hisschemoller.com/projects/v021_euclid_midi_java/euclidean_midi_patterns_jar.zip">Download Java JAR file.</a> *<br />
<a href="http://www.hisschemoller.com/projects/v021_euclid_midi_java/euclidean_midi_patterns_app.zip">Download Mac OS X APP file.</a><br />
<a href="http://www.hisschemoller.com/projects/v021_euclid_midi_java/euclidean_midi_patterns_project.zip">Download Java source files (Eclipse project), JAR and APP, all in one ZIP file.</a></p>
<p>* The JAR file needs the runtime for Java v1.6 (also known as Java SE 6) to be installed. On Mac this version is automatically installed with OS X 10.6 (Snow Leopard), but OS X 10.5 (Leopard) has the older Java v1.5 (or J2SE 5.0). I tested on Windows XP with Java v1.6 and that runs fine. You can test your Java version by opening Command Prompt (Windows) or Terminal (OS X) and typing &#8220;java -version&#8221; (without the quatation marks and press return after typing).</p>
<p>To compensate for the lack of an embedded app here’s a video with a short overview of what the Euclidean Pattern Sequencer does:</p>
<p><iframe class="youtube-player" type="text/html" width="540" height="335" src="http://www.youtube.com/embed/KKbYdExy3jQ" frameborder="0"><br />
</iframe><br />
</p>
<h1>Features</h1>
<p>The features are basic in this first version:</p>
<ul>
<li>Double click anywhere in the lower panel to generate a new pattern. A new pattern is 16 steps with 4 notes by default.</li>
<li>Click a pattern’s center circle to select the pattern. A selected pattern shows a double center ring and it’s settings are displayed on the panel at the right.</li>
<li>Drag a pattern’s center circle to move the pattern. So you can visually reorganize your screen when there’s a lot of patterns.</li>
<li>All patterns with their settings and location can be saved in a file. This is a regular XML text file. The File menu has the familiar New, Open, Save and Save As options. The project tempo is saved in the file as well.</li>
</ul>
<p><div id="attachment_706" class="wp-caption aligncenter" style="width: 512px"><img src="http://www.hisschemoller.com/wp-content/uploads/2011/05/euclidean_java_controls2.jpg" alt="Pattern generator control bar" title="euclidean_java_controls2" width="512" height="111" class="size-full wp-image-706" /><p class="wp-caption-text">Pattern generator control bar</p></div>
<p><div id="attachment_709" class="wp-caption alignright" style="width: 282px"><img src="http://www.hisschemoller.com/wp-content/uploads/2011/05/euclidean_java_settings.jpg" alt="Pattern and MIDI Settings" title="euclidean_java_settings" width="282" height="402" class="size-full wp-image-709" /><p class="wp-caption-text">Pattern Settings Panel</p></div>On top are the main controls: Start / stop playback, a slider and input field to set the tempo in BPM and a combobox to choose a MIDI out port. The MIDI In port is not used at the moment.</p>
<p>Once a pattern is selected it’s settings can be edited:</p>
<ul>
<li>The <strong>Steps</strong> and <strong>Fills</strong> sliders determine the pattern. Each time one of them changes the algorithm is recalculated.</li>
<li><strong>Rotate</strong> rotates the pattern. As you’ll note when you play with the <strong>Steps</strong> and <strong>Fills</strong> sliders, a pattern always starts with a note on the first step. To overcome this limitation you can rotate the pattern with this slider.</li>
<li>MIDI settings <strong>Channel</strong>, <strong>Pitch</strong> and <strong>Velocity</strong> are parts of the MIDI Note On and Note Off messages that the pattern sends to the MIDI port.</li>
<li><strong>Length</strong> is the duration of the note. Once the end of a note is reached a MIDI Note Off message is sent to the MIDI port. The length of a note can be 16 steps maximum. At the moment length is measured in the internal sequencer resolution, which is 24 PPQN. Because one step is seen as a 16th note, the maximum slider value is ( 16 steps / 4 ) * 24 PPQN = 96. Not very intuitive, I know. I’ll think of something better in an update.</li>
<li><strong>Mute</strong>, <strong>Solo</strong> and <strong>Delete</strong> do what you’d expect them to do.</li>
</ul>
<h1>Examples</h1>
<p>Here are two pieces of music I’ve already made with the app. I tried to get them to be a bit more like finished track than just demos of layered polyrhythms.</p>
<p>The first one is the same setup as in the video above but with more patterns. About sixteen of them sending MIDI to sixteen tracks in Ableton Live with mainly samples being played. I recorded the MIDI patterns in Live, did some arranging and automation and exported the result as audio.</p>
<p><object height="81" width="100%"><param name="movie" value="http://player.soundcloud.com/player.swf?url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F15718546"></param><param name="allowscriptaccess" value="always"></param> <embed allowscriptaccess="always" height="81" src="http://player.soundcloud.com/player.swf?url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F15718546" type="application/x-shockwave-flash" width="100%"></embed></object>  <span><a href="http://soundcloud.com/hisschemoller/euclidean-patterns-demo-1">Euclidean Patterns Demo 1</a> by <a href="http://soundcloud.com/hisschemoller">Wouter Hisschemöller</a></span></p>
<p>The second one are patterns sending MIDI notes to a hardware drum machine via the soundcard’s MIDI output. This is my new Machinedrum UW that plays samples as well as synthesizing drum sounds. All sounds are the Machinedrum’s sixteen tracks playing and recorded on the computer.</p>
<p><object height="81" width="100%"><param name="movie" value="http://player.soundcloud.com/player.swf?url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F15718843"></param><param name="allowscriptaccess" value="always"></param> <embed allowscriptaccess="always" height="81" src="http://player.soundcloud.com/player.swf?url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F15718843" type="application/x-shockwave-flash" width="100%"></embed></object>  <span><a href="http://soundcloud.com/hisschemoller/euclidean-patterns-demo-2">Euclidean Patterns Demo 2</a> by <a href="http://soundcloud.com/hisschemoller">Wouter Hisschemöller</a></span> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.hisschemoller.com/2011/euclidean-midi-patterns/feed/</wfw:commentRss>
		<slash:comments>114</slash:comments>
		</item>
		<item>
		<title>Euclidean rhythms update</title>
		<link>http://www.hisschemoller.com/2011/euclidean-rhythms-update/</link>
		<comments>http://www.hisschemoller.com/2011/euclidean-rhythms-update/#comments</comments>
		<pubDate>Fri, 20 May 2011 17:57:20 +0000</pubDate>
		<dc:creator>Wouter</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[audio]]></category>
		<category><![CDATA[bug]]></category>
		<category><![CDATA[euclidean]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[pattern]]></category>
		<category><![CDATA[rhythm]]></category>
		<category><![CDATA[sequencer]]></category>

		<guid isPermaLink="false">http://www.hisschemoller.com/?p=654</guid>
		<description><![CDATA[There’s an error in the Euclidean rhythms generator I posted in January. A few days ago I got a comment by Thomas pointing out the app generates wrong patterns. And he’s absolutely right. Here’s an updated version in which I rewrote the algorithm code. I checked it with several patterns and they all came out [...]]]></description>
			<content:encoded><![CDATA[<div id="attachment_655" class="wp-caption alignright" style="width: 222px"><a href="http://www.hisschemoller.com/2011/euclidean-rhythms-update/euclidean_rhythms_fixed/" rel="attachment wp-att-655"><img src="http://www.hisschemoller.com/wp-content/uploads/2011/05/euclidean_rhythms_fixed.gif" alt="E(5, 13) pattern" title="euclidean_rhythms_fixed" width="222" height="230" class="size-full wp-image-655" /></a><p class="wp-caption-text">E(5, 13) pattern</p></div>There’s an error in the <a href="http://www.hisschemoller.com/2011/euclidean-rhythms/">Euclidean rhythms generator</a> I posted in January.  A few days ago I got a comment by Thomas pointing out the app generates wrong patterns. And he’s absolutely right.</p>
<p>Here’s an updated version in which I rewrote the algorithm code. I checked it with several patterns and they all came out correct, so hopefully I have it right this time.</p>
<p><span id="more-654"></span></p>
<h1>The new Flash app</h1>
<div style="position: relative; width: 540px; height: 540px;">
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="fm_euclid_761331204"
			align="left"
			class="flashmovie"
			width="540"
			height="540">
	<param name="movie" value="http://www.hisschemoller.com/flash/v019_euclid_fixed/static/swf/euclid.swf" />
	<param name="menu" value="false" />
	<param name="quality" value="best" />
	<param name="scale" value="noscale" />
	<param name="bgcolor" value="#FFFFFF" />
	<param name="base" value="http://www.hisschemoller.com/flash/v019_euclid_fixed/static/swf/" />
	<!--[if !IE]>-->
	<object	type="application/x-shockwave-flash"
			data="http://www.hisschemoller.com/flash/v019_euclid_fixed/static/swf/euclid.swf"
			name="fm_euclid_761331204"
			align="left"
			width="540"
			height="540">
		<param name="menu" value="false" />
		<param name="quality" value="best" />
		<param name="scale" value="noscale" />
		<param name="bgcolor" value="#FFFFFF" />
		<param name="base" value="http://www.hisschemoller.com/flash/v019_euclid_fixed/static/swf/" />
	<!--<![endif]-->
		
<p><a href="http://adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a></p>

	<!--[if !IE]>-->
	</object>
	<!--<![endif]-->
</object>
</div>
<p><a href="http://www.hisschemoller.com/flash/v019_euclid_fixed/euclidean_rhythms_fixed.zip">Download the source files here.</a></p>
<p>I didn’t update the download ZIP file in the original post. So if you tire of the regular Euclidean patterns you can still download the old version and use those quirky ‘not quite right’ patterns. &#58;&#45;&#41;</p>
<p></p>
<h1>The difference</h1>
<p>The difference between the old and new version can be seen in the outcome of the algorithm for a pattern of thirteen steps with five notes. The old version generates as outcome:</p>
<p><span style="font-family:monospace;">E(5, 13) = [x..x..x..x.x.]</span></p>
<p>while it should be:</p>
<p><span style="font-family:monospace;">E(5, 13) = [x..x.x..x.x..]</span></p>
<p>and the correct calculation broken down in iterations:</p>
<p><span style="font-family:monospace;">[x][x][x][x][x][.][.][.][.][.][.][.][.]<br />
[x.][x.][x.][x.][x.][.][.][.]<br />
[x..][x..][x..][x.][x.]<br />
[x..x.][x..x.][x..]</span></p>
<p>It appears the old code stops one iteration too early: The wrong pattern is the same as the one before last iteration. Here’s the ActionScript code of the new version. It might be more compact and optimized, but I find it clearer to follow this way.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p654code2'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p6542"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
</pre></td><td class="code" id="p654code2"><pre class="actionscript" style="font-family:monospace;">package com.<span style="color: #006600;">hisschemoller</span>.<span style="color: #006600;">utils</span>
<span style="color: #66cc66;">&#123;</span>
   <span style="color: #808080; font-style: italic;">/**
    * @author Wouter Hisschemoller
    * (c) May 18, 2011
    */</span>
   <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> EuclidGenerator
   <span style="color: #66cc66;">&#123;</span>
       <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> _pattern : Vector.<span style="color: #66cc66;">&lt;</span>Boolean<span style="color: #66cc66;">&gt;</span> = <span style="color: #000000; font-weight: bold;">new</span> Vector.<span style="color: #66cc66;">&lt;</span>Boolean<span style="color: #66cc66;">&gt;</span><span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">&#41;</span>;
&nbsp;
       <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> EuclidGenerator <span style="color: #66cc66;">&#40;</span> total : <span style="color: #0066CC;">int</span>, hits : <span style="color: #0066CC;">int</span> <span style="color: #66cc66;">&#41;</span>
       <span style="color: #66cc66;">&#123;</span>
           <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> hits <span style="color: #66cc66;">&gt;</span>= total <span style="color: #66cc66;">||</span> hits == <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">||</span> hits == <span style="color: #cc66cc;">0</span> <span style="color: #66cc66;">&#41;</span>
           <span style="color: #66cc66;">&#123;</span>
               _pattern = <span style="color: #000000; font-weight: bold;">new</span> Vector.<span style="color: #66cc66;">&lt;</span>Boolean<span style="color: #66cc66;">&gt;</span><span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">&#41;</span>;
               <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> hits <span style="color: #66cc66;">&gt;</span>= total <span style="color: #66cc66;">&#41;</span>
               <span style="color: #66cc66;">&#123;</span>
                   <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #000000; font-weight: bold;">var</span> i : <span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">0</span>; i <span style="color: #66cc66;">&lt;</span> total; i ++ <span style="color: #66cc66;">&#41;</span>
                   <span style="color: #66cc66;">&#123;</span>
                       _pattern.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span> <span style="color: #000000; font-weight: bold;">true</span> <span style="color: #66cc66;">&#41;</span>;
                   <span style="color: #66cc66;">&#125;</span>
               <span style="color: #66cc66;">&#125;</span>
               <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> total == <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">&#41;</span>
               <span style="color: #66cc66;">&#123;</span>
                   _pattern.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span> hits == <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">&#41;</span>;
               <span style="color: #66cc66;">&#125;</span>
               <span style="color: #b1b100;">else</span>
               <span style="color: #66cc66;">&#123;</span>
                   <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span> i = <span style="color: #cc66cc;">0</span>; i <span style="color: #66cc66;">&lt;</span> total; i ++ <span style="color: #66cc66;">&#41;</span>
                   <span style="color: #66cc66;">&#123;</span>
                       _pattern.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span> <span style="color: #000000; font-weight: bold;">false</span> <span style="color: #66cc66;">&#41;</span>;
                   <span style="color: #66cc66;">&#125;</span>
               <span style="color: #66cc66;">&#125;</span>
&nbsp;
               <span style="color: #b1b100;">return</span>;
           <span style="color: #66cc66;">&#125;</span>
&nbsp;
           <span style="color: #000000; font-weight: bold;">var</span> bigArray : Vector.<span style="color: #66cc66;">&lt;</span>Vector.<span style="color: #66cc66;">&lt;</span>Boolean<span style="color: #66cc66;">&gt;&gt;</span> = <span style="color: #000000; font-weight: bold;">new</span> Vector.<span style="color: #66cc66;">&lt;</span>Vector.<span style="color: #66cc66;">&lt;</span>Boolean<span style="color: #66cc66;">&gt;&gt;</span><span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">&#41;</span>;
           <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span> i = <span style="color: #cc66cc;">0</span>; i <span style="color: #66cc66;">&lt;</span> hits; i ++ <span style="color: #66cc66;">&#41;</span>
           <span style="color: #66cc66;">&#123;</span>
               <span style="color: #000000; font-weight: bold;">var</span> boolArray : Vector.<span style="color: #66cc66;">&lt;</span>Boolean<span style="color: #66cc66;">&gt;</span> = <span style="color: #000000; font-weight: bold;">new</span> Vector.<span style="color: #66cc66;">&lt;</span>Boolean<span style="color: #66cc66;">&gt;</span><span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">&#41;</span>;
               boolArray.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span> <span style="color: #000000; font-weight: bold;">true</span> <span style="color: #66cc66;">&#41;</span>;
               bigArray.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span> boolArray <span style="color: #66cc66;">&#41;</span>;
           <span style="color: #66cc66;">&#125;</span>
&nbsp;
           <span style="color: #000000; font-weight: bold;">var</span> smallArray : Vector.<span style="color: #66cc66;">&lt;</span>Vector.<span style="color: #66cc66;">&lt;</span>Boolean<span style="color: #66cc66;">&gt;&gt;</span> = <span style="color: #000000; font-weight: bold;">new</span> Vector.<span style="color: #66cc66;">&lt;</span>Vector.<span style="color: #66cc66;">&lt;</span>Boolean<span style="color: #66cc66;">&gt;&gt;</span><span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">&#41;</span>;
           <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span> i = <span style="color: #cc66cc;">0</span>; i <span style="color: #66cc66;">&lt;</span> <span style="color: #66cc66;">&#40;</span> total - hits <span style="color: #66cc66;">&#41;</span>; i ++ <span style="color: #66cc66;">&#41;</span>
           <span style="color: #66cc66;">&#123;</span>
               boolArray = <span style="color: #000000; font-weight: bold;">new</span> Vector.<span style="color: #66cc66;">&lt;</span>Boolean<span style="color: #66cc66;">&gt;</span><span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">&#41;</span>;
               boolArray.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span> <span style="color: #000000; font-weight: bold;">false</span> <span style="color: #66cc66;">&#41;</span>;
               smallArray.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span> boolArray <span style="color: #66cc66;">&#41;</span>;
           <span style="color: #66cc66;">&#125;</span>
&nbsp;
           _pattern = bjorklund<span style="color: #66cc66;">&#40;</span> bigArray, smallArray <span style="color: #66cc66;">&#41;</span>;
       <span style="color: #66cc66;">&#125;</span>
&nbsp;
       <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">get</span> pattern <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> : Vector.<span style="color: #66cc66;">&lt;</span>Boolean<span style="color: #66cc66;">&gt;</span>
       <span style="color: #66cc66;">&#123;</span>
           <span style="color: #b1b100;">return</span> _pattern;
       <span style="color: #66cc66;">&#125;</span>
&nbsp;
       <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> bjorklund <span style="color: #66cc66;">&#40;</span> bigArray : Vector.<span style="color: #66cc66;">&lt;</span>Vector.<span style="color: #66cc66;">&lt;</span>Boolean<span style="color: #66cc66;">&gt;&gt;</span>, smallArray : Vector.<span style="color: #66cc66;">&lt;</span>Vector.<span style="color: #66cc66;">&lt;</span>Boolean<span style="color: #66cc66;">&gt;&gt;</span> <span style="color: #66cc66;">&#41;</span> : Vector.<span style="color: #66cc66;">&lt;</span>Boolean<span style="color: #66cc66;">&gt;</span>
       <span style="color: #66cc66;">&#123;</span>
           <span style="color: #808080; font-style: italic;">/** Done, flatten arrays. */</span>
           <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> smallArray.<span style="color: #0066CC;">length</span> <span style="color: #66cc66;">&lt;</span>= <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">&#41;</span>
           <span style="color: #66cc66;">&#123;</span>
               <span style="color: #000000; font-weight: bold;">var</span> resultList : Vector.<span style="color: #66cc66;">&lt;</span>Boolean<span style="color: #66cc66;">&gt;</span> = <span style="color: #000000; font-weight: bold;">new</span> Vector.<span style="color: #66cc66;">&lt;</span>Boolean<span style="color: #66cc66;">&gt;</span><span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">&#41;</span>;
               <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #000000; font-weight: bold;">var</span> i : <span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">0</span>; i <span style="color: #66cc66;">&lt;</span> bigArray.<span style="color: #0066CC;">length</span>; i ++ <span style="color: #66cc66;">&#41;</span>
               <span style="color: #66cc66;">&#123;</span>
                   <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #000000; font-weight: bold;">var</span> j : <span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">0</span>; j <span style="color: #66cc66;">&lt;</span> bigArray<span style="color: #66cc66;">&#91;</span> i <span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">length</span>; j ++ <span style="color: #66cc66;">&#41;</span>
                   <span style="color: #66cc66;">&#123;</span>
                       resultList.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span> bigArray<span style="color: #66cc66;">&#91;</span> i <span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span> j <span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#41;</span>;
                   <span style="color: #66cc66;">&#125;</span>
               <span style="color: #66cc66;">&#125;</span>
               <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span> i = <span style="color: #cc66cc;">0</span>; i <span style="color: #66cc66;">&lt;</span> smallArray.<span style="color: #0066CC;">length</span>; i ++ <span style="color: #66cc66;">&#41;</span>
               <span style="color: #66cc66;">&#123;</span>
                   <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span> j = <span style="color: #cc66cc;">0</span>; j <span style="color: #66cc66;">&lt;</span> smallArray<span style="color: #66cc66;">&#91;</span> i <span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">length</span>; j ++ <span style="color: #66cc66;">&#41;</span>
                   <span style="color: #66cc66;">&#123;</span>
                       resultList.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span> smallArray<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span>j<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#41;</span>;
                   <span style="color: #66cc66;">&#125;</span>
               <span style="color: #66cc66;">&#125;</span>
               <span style="color: #b1b100;">return</span> resultList;
           <span style="color: #66cc66;">&#125;</span>
&nbsp;
           <span style="color: #000000; font-weight: bold;">var</span> fullRounds : <span style="color: #0066CC;">int</span> = <span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">floor</span><span style="color: #66cc66;">&#40;</span> smallArray.<span style="color: #0066CC;">length</span> <span style="color: #66cc66;">/</span> bigArray.<span style="color: #0066CC;">length</span> <span style="color: #66cc66;">&#41;</span>;
           <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span> i = <span style="color: #cc66cc;">0</span>; i <span style="color: #66cc66;">&lt;</span> fullRounds; i ++ <span style="color: #66cc66;">&#41;</span>
           <span style="color: #66cc66;">&#123;</span>
               <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span> j = <span style="color: #cc66cc;">0</span>; j <span style="color: #66cc66;">&lt;</span> bigArray.<span style="color: #0066CC;">length</span>; j ++ <span style="color: #66cc66;">&#41;</span>
               <span style="color: #66cc66;">&#123;</span>
                   <span style="color: #000000; font-weight: bold;">var</span> sourceBoolArray : Vector.<span style="color: #66cc66;">&lt;</span>Boolean<span style="color: #66cc66;">&gt;</span> = smallArray<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>;
                   <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #000000; font-weight: bold;">var</span> k : <span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">0</span>; k <span style="color: #66cc66;">&lt;</span> sourceBoolArray.<span style="color: #0066CC;">length</span>; k ++ <span style="color: #66cc66;">&#41;</span>
                   <span style="color: #66cc66;">&#123;</span>
                       bigArray<span style="color: #66cc66;">&#91;</span>j<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span> sourceBoolArray<span style="color: #66cc66;">&#91;</span>k<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#41;</span>;
                   <span style="color: #66cc66;">&#125;</span>
                   smallArray.<span style="color: #0066CC;">splice</span><span style="color: #66cc66;">&#40;</span> <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">&#41;</span>;
               <span style="color: #66cc66;">&#125;</span>
           <span style="color: #66cc66;">&#125;</span>
&nbsp;
           <span style="color: #000000; font-weight: bold;">var</span> remainder : <span style="color: #0066CC;">int</span> = smallArray.<span style="color: #0066CC;">length</span> <span style="color: #66cc66;">%</span> bigArray.<span style="color: #0066CC;">length</span>;
           <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span> i = <span style="color: #cc66cc;">0</span>; i <span style="color: #66cc66;">&lt;</span> remainder; i ++ <span style="color: #66cc66;">&#41;</span>
           <span style="color: #66cc66;">&#123;</span>
               sourceBoolArray = smallArray<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>;
               <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span> k = <span style="color: #cc66cc;">0</span>; k <span style="color: #66cc66;">&lt;</span> sourceBoolArray.<span style="color: #0066CC;">length</span>; k ++ <span style="color: #66cc66;">&#41;</span>
               <span style="color: #66cc66;">&#123;</span>
                   bigArray<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span> sourceBoolArray<span style="color: #66cc66;">&#91;</span>k<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#41;</span>;
               <span style="color: #66cc66;">&#125;</span>
               smallArray.<span style="color: #0066CC;">splice</span><span style="color: #66cc66;">&#40;</span> <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">&#41;</span>;
           <span style="color: #66cc66;">&#125;</span>
&nbsp;
           <span style="color: #808080; font-style: italic;">/** Split result in two new arrays. */</span>
           <span style="color: #000000; font-weight: bold;">var</span> newBigArray : Vector.<span style="color: #66cc66;">&lt;</span>Vector.<span style="color: #66cc66;">&lt;</span>Boolean<span style="color: #66cc66;">&gt;&gt;</span> = <span style="color: #000000; font-weight: bold;">new</span> Vector.<span style="color: #66cc66;">&lt;</span>Vector.<span style="color: #66cc66;">&lt;</span>Boolean<span style="color: #66cc66;">&gt;&gt;</span><span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">&#41;</span>;
           <span style="color: #000000; font-weight: bold;">var</span> newSmallArray : Vector.<span style="color: #66cc66;">&lt;</span>Vector.<span style="color: #66cc66;">&lt;</span>Boolean<span style="color: #66cc66;">&gt;&gt;</span> = <span style="color: #000000; font-weight: bold;">new</span> Vector.<span style="color: #66cc66;">&lt;</span>Vector.<span style="color: #66cc66;">&lt;</span>Boolean<span style="color: #66cc66;">&gt;&gt;</span><span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">&#41;</span>;
           <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span> i = <span style="color: #cc66cc;">0</span>; i <span style="color: #66cc66;">&lt;</span> bigArray.<span style="color: #0066CC;">length</span>; i ++ <span style="color: #66cc66;">&#41;</span>
           <span style="color: #66cc66;">&#123;</span>
               <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> remainder == <span style="color: #cc66cc;">0</span> <span style="color: #66cc66;">&#41;</span>
               <span style="color: #66cc66;">&#123;</span>
                   newBigArray.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span> bigArray<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#41;</span>;
               <span style="color: #66cc66;">&#125;</span>
               <span style="color: #b1b100;">else</span>
               <span style="color: #66cc66;">&#123;</span>
                   <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> i <span style="color: #66cc66;">&lt;</span> remainder <span style="color: #66cc66;">&#41;</span>
                   <span style="color: #66cc66;">&#123;</span>
                       newBigArray.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span> bigArray<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#41;</span>;
                   <span style="color: #66cc66;">&#125;</span>
                   <span style="color: #b1b100;">else</span>
                   <span style="color: #66cc66;">&#123;</span>
                       newSmallArray.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span> bigArray<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#41;</span>;
                   <span style="color: #66cc66;">&#125;</span>
               <span style="color: #66cc66;">&#125;</span>
           <span style="color: #66cc66;">&#125;</span>
&nbsp;
           <span style="color: #b1b100;">return</span> bjorklund<span style="color: #66cc66;">&#40;</span> newBigArray, newSmallArray <span style="color: #66cc66;">&#41;</span>;
       <span style="color: #66cc66;">&#125;</span>
   <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.hisschemoller.com/2011/euclidean-rhythms-update/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Audio Editor 1.1</title>
		<link>http://www.hisschemoller.com/2011/audio-editor-1-1/</link>
		<comments>http://www.hisschemoller.com/2011/audio-editor-1-1/#comments</comments>
		<pubDate>Sun, 20 Feb 2011 18:34:29 +0000</pubDate>
		<dc:creator>Wouter</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[audio]]></category>
		<category><![CDATA[editor]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[loop]]></category>
		<category><![CDATA[snd]]></category>
		<category><![CDATA[update]]></category>
		<category><![CDATA[waveform]]></category>

		<guid isPermaLink="false">http://www.hisschemoller.com/?p=601</guid>
		<description><![CDATA[While cleaning up my laptop’s hard drive I found an update to the audio editor I posted here last year. Not radically different, but maybe useful to someone. Its new feature is the loop function. Toggle the loop button and playback loops between the start marker (S) and the end marker (E). Just like you’d [...]]]></description>
			<content:encoded><![CDATA[<div id="attachment_603" class="wp-caption alignright" style="width: 270px"><a href="http://www.hisschemoller.com/2011/audio-editor-1-1/audioeditor1_1-2/" rel="attachment wp-att-603"><img src="http://www.hisschemoller.com/wp-content/uploads/2011/02/audioeditor1_11.gif" alt="Audio Editor 1.1" title="Audio Editor 1.1" width="270" height="231" class="size-full wp-image-603" /></a><p class="wp-caption-text">Audio Editor 1.1</p></div>While cleaning up my laptop’s hard drive I found an update to the <a href="http://www.hisschemoller.com/2010/audio-editor-1-0/">audio editor</a> I posted here last year. Not radically different, but maybe useful to someone.</p>
<p>Its new feature is the loop function. Toggle the loop button and playback loops between the start marker (S) and the end marker (E). Just like you’d expect.</p>
<p>Next to this single new feature there’s a new waveform display that I really like.<br />
In the old version the waveform shape was rendered by drawing vertical lines next to each other for each pixel from left to right. The new version draws a much nicer vector shape that is less processor intensive as well.</p>
<p><span id="more-601"></span></p>
<h1>The Flash app</h1>
<div style="position: relative; width: 540px; height: 540px;">
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="fm_app_1095741802"
			align="left"
			class="flashmovie"
			width="540"
			height="540">
	<param name="movie" value="http://www.hisschemoller.com/flash/v016_audio_editor_1_1/static/swf/app.swf" />
	<param name="menu" value="false" />
	<param name="quality" value="best" />
	<param name="scale" value="noscale" />
	<param name="bgcolor" value="#FFFFFF" />
	<param name="base" value="http://www.hisschemoller.com/flash/v016_audio_editor_1_1/static/swf/" />
	<!--[if !IE]>-->
	<object	type="application/x-shockwave-flash"
			data="http://www.hisschemoller.com/flash/v016_audio_editor_1_1/static/swf/app.swf"
			name="fm_app_1095741802"
			align="left"
			width="540"
			height="540">
		<param name="menu" value="false" />
		<param name="quality" value="best" />
		<param name="scale" value="noscale" />
		<param name="bgcolor" value="#FFFFFF" />
		<param name="base" value="http://www.hisschemoller.com/flash/v016_audio_editor_1_1/static/swf/" />
	<!--<![endif]-->
		
<p><a href="http://adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a></p>

	<!--[if !IE]>-->
	</object>
	<!--<![endif]-->
</object>
</div>
<p><a href="http://www.hisschemoller.com/flash/v016_audio_editor_1_1/audio_editor_v1_1.zip">Download the ActionScript source code and files for this project here.</a></p>
<p>What happens is that for each pixel the maximum and minimum sample value is polled. Those values are stored in two arrays. Then the array of maximums is used to draw the top half of the waveform and the array of minimums for the bottom half. Together the two halves form a filled shape.</p>
<p>Like in the earlier waveform display the render method changes when zoomed in. If the zoom level is high enough (when one pixel represents less than forty samples) the filled shape is replaced by a single line. I find a single line easier for close inspection of an audio signal and most audio editors work like this.</p>
<p>The mouse interaction remains the same as well: Click on the waveform and drag up or down to zoom and left or right to move, and even both at the same time! I still like this behaviour a lot. First saw it in Steinberg WaveLab.</p>
<p>A note: This waveform display works well for audio files of a minute or less in length. The longer the file the less precise the waveform gets. This is because a fixed number of samples is polled for each pixel. If the file is long one pixel represents a lot of audio, and if it’s polled at too large intervals the polling results get less and less precise. I didn’t increase the number of polls because I found that slows down the screen redraws too much.</p>
<p><div id="attachment_608" class="wp-caption alignleft" style="width: 150px"><a href="http://www.hisschemoller.com/2011/audio-editor-1-1/snd-systems-medley/" rel="attachment wp-att-608"><img src="http://www.hisschemoller.com/wp-content/uploads/2011/02/snd-systems-medley.jpg" alt="Snd - Systems Medley" title="Snd - Systems Medley" width="150" height="150" class="size-full wp-image-608" /></a><p class="wp-caption-text">Snd - Systems Medley</p></div>The audio in this example is <strong>Untitled Locked Groove #3</strong> by <a href="http://www.makesnd.com/01/">Snd</a> which appeared on their <a href="http://www.city-centre-offices.de/drupal/index.php/">Systems Medley / Planets</a> 7” from 2000 on <a href="http://www.city-centre-offices.de/drupal/index.php/">City Centre Offices</a>. I listen to their records a lot. Good programming music as well. :-)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hisschemoller.com/2011/audio-editor-1-1/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Euclidean rhythms</title>
		<link>http://www.hisschemoller.com/2011/euclidean-rhythms/</link>
		<comments>http://www.hisschemoller.com/2011/euclidean-rhythms/#comments</comments>
		<pubDate>Thu, 27 Jan 2011 22:56:57 +0000</pubDate>
		<dc:creator>Wouter</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[euclidean]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[Godfried Toussaint]]></category>
		<category><![CDATA[pattern]]></category>
		<category><![CDATA[rhythm]]></category>
		<category><![CDATA[sequencer]]></category>

		<guid isPermaLink="false">http://www.hisschemoller.com/?p=565</guid>
		<description><![CDATA[Last week I read a really interesting article about computer generated rhythms: “Generating African rhythms using the euclidean algorithm” on Ruin &#038; Wesen’s website. Usually I’m not so impressed by generative and algorithmic music, but this formula gives some very cool results. The formula is simple really. It’s an algorithm to distribute an amount of [...]]]></description>
			<content:encoded><![CDATA[<div id="attachment_566" class="wp-caption alignright" style="width: 270px"><a href="http://www.hisschemoller.com/wp-content/uploads/2011/01/euclidean_patterns.jpg"><img src="http://www.hisschemoller.com/wp-content/uploads/2011/01/euclidean_patterns.jpg" alt="Euclidean patterns app screenshot" title="Euclidean patterns" width="270" height="200" class="size-full wp-image-566" /></a><p class="wp-caption-text">Euclidean patterns</p></div>Last week I read a really interesting article about computer generated rhythms: “<a href="http://ruinwesen.com/blog?id=216" target="_blank">Generating African rhythms using the euclidean algorithm</a>” on <a href="http://ruinwesen.com" target="_blank">Ruin &#038; Wesen</a>’s website. Usually I’m not so impressed by generative and algorithmic music, but this formula gives some very cool results.</p>
<p><span id="more-565"></span><br />
The formula is simple really. It’s an algorithm to distribute an amount of notes as evenly as possible over a period of time, where time is divided in equal parts. A very basic example: Say you have one measure of sixteen sixteenth notes and there are four notes to be played, then this is how those four notes would be equally distributed by the algorithm:</p>
<p><span style="font-family:monospace;">x . . . x . . . x . . . x . . .</span></p>
<p>A basic house or techno kick drum pattern. There’s four notes on sixteen steps, so the division is easy, 16 / 4 = 4. It gives you one note every four steps. But it gets more interesting when the numbers don’t divide so easily. Take five hits on sixteen steps. Then the pattern becomes more irregular:</p>
<p><span style="font-family:monospace;">x . . . x . . x . . x . . x . .</span></p>
<p>It’s these irregular patterns that create the best rhythms. Especially when several patterns are combined. In Ruin &#038; Wesen’s article there’s a few audio examples and here’s a video on blip.tv of a <a href="http://ruinwesen.blip.tv/file/3166970/" target="_blank">MiniCommand MIDI controller generating Euclidean rhythms on a Machinedrum</a>.</p>
<h1>The algorithm</h1>
<p>The algorithm is very well explained in the <a href="http://ruinwesen.com/blog?id=216" target="_blank">article</a> I mentioned, so I won’t repeat it here. There’s another article on the weblog of Robin Price which includes a <a href="http://registeringdomainnamesismorefunthandoingrealwork.com/blogs/?p=236" target="_blank">Max/MSP example</a>, there&#8217;s a <a href="http://doc.gold.ac.uk/~ma801dp/blog/?p=40" target="_blank">Pure Data example by Dave Poulter</a> as well as one in <a href="http://kreese.net/index.php/2010/03/generating-musical-rhythms/" target="_blank">Java by Kristopher Reese</a>.</p>
<p>Absolutely worth reading is the paper by <a href="http://cgm.cs.mcgill.ca/~godfried/" target="_blank">Godfried Toussaint</a> who as far as I know first recognized the connection between the Euclidean algorithm and musical rhythms. It’s downlaodable as a PDF: “<a href="http://cgm.cs.mcgill.ca/~godfried/publications/banff.pdf" target="_blank">The Euclidean Algorithm Generates Traditional Musical Rhythms</a>”.</p>
<h1>The Flash app</h1>
<div style="position: relative; width: 540px; height: 540px;">
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="fm_euclid_1375380344"
			align="left"
			class="flashmovie"
			width="540"
			height="540">
	<param name="movie" value="http://www.hisschemoller.com/flash/v019_euclid/static/swf/euclid.swf" />
	<param name="menu" value="false" />
	<param name="quality" value="best" />
	<param name="scale" value="noscale" />
	<param name="bgcolor" value="#FFFFFF" />
	<param name="base" value="http://www.hisschemoller.com/flash/v019_euclid/static/swf/" />
	<!--[if !IE]>-->
	<object	type="application/x-shockwave-flash"
			data="http://www.hisschemoller.com/flash/v019_euclid/static/swf/euclid.swf"
			name="fm_euclid_1375380344"
			align="left"
			width="540"
			height="540">
		<param name="menu" value="false" />
		<param name="quality" value="best" />
		<param name="scale" value="noscale" />
		<param name="bgcolor" value="#FFFFFF" />
		<param name="base" value="http://www.hisschemoller.com/flash/v019_euclid/static/swf/" />
	<!--<![endif]-->
		
<p><a href="http://adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a></p>

	<!--[if !IE]>-->
	</object>
	<!--<![endif]-->
</object>
</div>
<p><a href="http://www.hisschemoller.com/flash/v019_euclid/euclidean_rhythms.zip">Download the full source code here.</a></p>
<p>This is a Flash app I made over the weekend to quickly try out combining rhythms. It’s by no means complete, but it does work:</p>
<ul>
<li>To create a new pattern doubleclick somewhere in the lower panel. A basic sixteen step quarter note pattern appears where you clicked.</li>
<li>To edit the pattern doubleclick it’s center circle. A settings popup appears with sliders for pattern length and number of hits. Change these to quickly get a feel for how these Euclidean patterns work. The ‘Sound’ dropdown menu lets you select a different sample for the pattern to play.</li>
<li>Click and drag the center circle to move a pattern.</li>
<li>The controls at the top are obvious: Start / stop playback, adjust volume and change tempo (in beats per minute).</li>
</ul>
<p>
You will notice that when you create a new pattern while patterns are already playing, the new pattern is not synchronized. To synchronize all patterns stop and restart playback.</p>
<p><div id="attachment_578" class="wp-caption alignright" style="width: 249px"><a href="http://www.hisschemoller.com/2011/euclidean-rhythms/euclides/" rel="attachment wp-att-578"><img src="http://www.hisschemoller.com/wp-content/uploads/2011/01/euclides.jpg" alt="Euclid of Alexandria" title="Euclid of Alexandria" width="249" height="300" class="size-full wp-image-578" /></a><p class="wp-caption-text">Euclid of Alexandria</p></div>There’s a lot I should add to make this a complete app: An open and save option for pattern combinations and their settings, an option to load and use your own samples, basic sample editing like volume, speed and sample start adjustment and more pattern options.<br />
But the first thing I’d like to do is to add MIDI output. So I can use these patterns to drive music software like <a href="http://www.ableton.com" target="_blank">Ableton Live</a> or hardware like my <a href="http://www.elektron.se/products/machinedrum" target="_blank">Machinedrum</a>. It seems Java is the easiest way to write software that can use MIDI, so next I will be learning to program Java.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hisschemoller.com/2011/euclidean-rhythms/feed/</wfw:commentRss>
		<slash:comments>24</slash:comments>
		</item>
		<item>
		<title>Audio Editor 1.0</title>
		<link>http://www.hisschemoller.com/2010/audio-editor-1-0/</link>
		<comments>http://www.hisschemoller.com/2010/audio-editor-1-0/#comments</comments>
		<pubDate>Fri, 09 Apr 2010 20:30:05 +0000</pubDate>
		<dc:creator>Wouter</dc:creator>
				<category><![CDATA[ActionScript]]></category>

		<guid isPermaLink="false">http://www.hisschemoller.com/?p=527</guid>
		<description><![CDATA[A useful application! I posted a lot of experimental stuff on this weblog over the past year. Or components that might have their use when built into proper applications. But this is my first web app that&#8217;s useful on it&#8217;s own. This is a simple Flash audio editor that works in the browser. Everything it [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.hisschemoller.com/2010/audio-editor-1-0/audioeditor1/" rel="attachment wp-att-528"><img src="http://www.hisschemoller.com/wp-content/uploads/2010/04/audioeditor1.gif" alt="Audio Editor 1.0" title="AudioEditor10" width="285" height="171" class="alignright size-full wp-image-528" /></a>A useful application!</p>
<p>I posted a lot of experimental stuff on this weblog over the past year. Or components that might have their use when built into proper applications. But this is my first web app that&#8217;s useful on it&#8217;s own.</p>
<p>This is a simple Flash audio editor that works in the browser. Everything it does is done by ActionScript code inside the application. There&#8217;s no backend that does the difficult work. So it would function just as well in the standalone Flash Player or exported as an Air application as it does in the browser.</p>
<p><span id="more-527"></span><br />
This audio editor has only a very basic set of features. Here&#8217;s the things it can do:</p>
<ul>
<li>Open mono and stereo WAV and MP3 files.</li>
<li>Select part of the audio file by graphically adjusting the start and end point.</li>
<li>Change the pitch by adjusting the playback speed.</li>
<li>Reverse the file so it plays backwards.</li>
<li>The result of these adjustments (start and end, pitch, reverse) can be saved as a new stereo WAV file.</li>
</ul>
<h1>The Flash file</h1>
<div style="position:relative; width:540px; height:540px;">

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="fm_app_1705448109"
			align="left"
			class="flashmovie"
			width="540"
			height="540">
	<param name="movie" value="http://www.hisschemoller.com/flash/v013_audioeditor/static/swf/app.swf" />
	<param name="menu" value="false" />
	<param name="quality" value="best" />
	<param name="scale" value="noscale" />
	<param name="bgcolor" value="#FFFFFF" />
	<param name="base" value="http://www.hisschemoller.com/flash/v013_audioeditor/static/swf/" />
	<!--[if !IE]>-->
	<object	type="application/x-shockwave-flash"
			data="http://www.hisschemoller.com/flash/v013_audioeditor/static/swf/app.swf"
			name="fm_app_1705448109"
			align="left"
			width="540"
			height="540">
		<param name="menu" value="false" />
		<param name="quality" value="best" />
		<param name="scale" value="noscale" />
		<param name="bgcolor" value="#FFFFFF" />
		<param name="base" value="http://www.hisschemoller.com/flash/v013_audioeditor/static/swf/" />
	<!--<![endif]-->
		
<p><a href="http://adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a></p>

	<!--[if !IE]>-->
	</object>
	<!--<![endif]-->
</object></div>
<p><a href="http://www.hisschemoller.com/flash/v013_audioeditor/audioeditor_1_0.zip">Download the source files here.</a></p>
<h1>Code</h1>
<p>This project is built on the PureMVC framework again. It&#8217;s what I&#8217;ve been using for all my projects the last half year and I&#8217;m still really happy with it. The code gets more complicated with each project but I still have a clear overview of where everything is and I can even find my way quickly around older projects.</p>
<p>Here&#8217;s a short overview of the main building blocks of the audio engine:</p>
<div id="attachment_548" class="wp-caption alignleft" style="width: 540px"><img src="http://www.hisschemoller.com/wp-content/uploads/2010/04/audioeditor_engineoverview.jpg" alt="Audio engine overview" title="Audio engine overview" width="540" height="420" class="size-full wp-image-548" /><p class="wp-caption-text">Audio engine overview</p></div>
<ul>
<li><strong>AudioDriverProxy</strong> listens to the SampleDataEvent. On each event it sends a request to the audio engine (AudioEngineProxy) to fill the buffer with samples.</li>
<li><strong>AudioEngineProxy</strong> holds all the instruments (of type IAudioInstrument) that produce sound. When it receives the request for samples from the audio driver it passes this request on to all the instruments that are registered with the engine.</li>
<li><strong>IAudioInstrument</strong> is (the interface for) a general instrument. An instrument doesn&#8217;t generate sound itself, but it maintains a list of IAudioProcessor objects that produce the actual audio. Each processor is a note that the instrument currently plays. So if an instrument was to play a three note chord three processors would be added to the list. And when a note is finished the processor is removed from the list. Besides that IAudioInstrument has an API for the instrument to be played. These are MIDI inspired functions like noteOn(), noteOff() and programChange().</li>
<li><strong>IAudioProcessor</strong> is the object that produces sound. When an instrument receives a buffer from the audio engine it passes the buffer on to each of its processors. The processor loops through all the samples in the buffer and adds its own values to it. An IAudioProcessor can generate sound like a synthesizer or play back existing sound like a sampler. It doesn&#8217;t matter as long as it implements the IAudioProcessor interface. </li>
<li><strong>AudioOutputProxy</strong> is the last stage in the audio generation process. Once all instruments have contributed their sounds to the buffer, the audio output is the last one that gets the buffer with sample data from the audio engine. Here final adjustments to the sound can be made. In the case of the audio editor the only adjustment that can be made is to set the overall output volume.</li>
</ul>
<p>The audio editor is a really simple implementation of this audio engine: There is only one instrument &#8211; the editor itself (WaveEditorProxy) &#8211; and it can play only one sound at a time: the audio being edited (PitchedSampleProcessor as IAudioProcessor).</p>
<div style="position:relative; width:540px; height:200px;">
<div id="attachment_537" class="wp-caption alignleft" style="width: 300px"><img src="http://www.hisschemoller.com/wp-content/uploads/2010/04/harrington1200.jpg" alt="The Harrington 1200" title="harrington1200" width="300" height="168" class="size-full wp-image-537" /><p class="wp-caption-text">The Harrington 1200, a state of the art songwriting computer.</p></div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.hisschemoller.com/2010/audio-editor-1-0/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Animation &amp; audio synchronization</title>
		<link>http://www.hisschemoller.com/2010/animation-audio-synchronization/</link>
		<comments>http://www.hisschemoller.com/2010/animation-audio-synchronization/#comments</comments>
		<pubDate>Mon, 08 Mar 2010 22:32:03 +0000</pubDate>
		<dc:creator>Wouter</dc:creator>
				<category><![CDATA[ActionScript]]></category>

		<guid isPermaLink="false">http://www.hisschemoller.com/?p=488</guid>
		<description><![CDATA[It&#8217;s not that easy to synchronize programmed animation and dynamic sound in Flash. To have something happen on screen at exactly the same time a sound starts to play. I thought about it a bit and came up with two different scenarios where audio and visuals need to be synchronized: The first is where a [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.hisschemoller.com/2010/animation-audio-synchronization"><img src="http://www.hisschemoller.com/wp-content/uploads/2010/03/latency_screenshot.gif" alt="" title="Sync test screenshot" width="275" height="241" class="alignright size-full wp-image-489" /></a></a>It&#8217;s not that easy to synchronize programmed animation and dynamic sound in Flash. To have something happen on screen at exactly the same time a sound starts to play.</p>
<p>I thought about it a bit and came up with two different scenarios where audio and visuals need to be synchronized:</p>
<ol>
<li>The first is where a sound plays and something visual on screen happens to accompany that audio event.</li>
<li>The second is the other way around. Something happens on screen that should be accompanied by a sound.</li>
</ol>
<p>In the first case the animation is the initiator, in the second case the sound. For the second case I included a Flash example here with the source code available.</p>
<p><span id="more-488"></span></p>
<h1>1. Animation follows audio</h1>
<p>This is the simpler scenario. For example: There&#8217;s an online drum machine built in Flash that plays a drum sound on every beat. A kick drum in a techno track for example. On the machine&#8217;s interface there&#8217;s a small red LED light (a MovieClip) that should blink each time the drum hits.</p>
<p>The drum machine has a steady rhythm set in BPM (Beats Per Minute). So once the machine has started it&#8217;s always known when the next drum hit will happen. Here&#8217;s a graph of the events:</p>
<p><img class="alignleft size-full wp-image-490" title="Animation follows audio events." src="http://www.hisschemoller.com/wp-content/uploads/2010/03/latency_graph1.jpg" alt="" width="540" height="137" /></p>
<ul>
<li>(a), (b), (c) &#8230; are the moments in time when a SampleDataEvent is received and the buffer is filled with samples.</li>
<li>(p) is where the drum hit should happen. Because the drum BPM and buffer refresh rate are completely independent the drum hit will almost always start somewhere between two buffer refreshes.</li>
<li>(q) is where the drum sound is actually heard through the speakers. There is a delay between when the samples are sent out from Flash to when they are played through the speakers. This is the latency; the time needed for the soundcard to process the samples and send them as analog audio to the soundcard&#8217;s outputs. Latency is the duration from (p) to (q).</li>
</ul>
<p>To synchronize the two it&#8217;s a simple addition. Duration (a)(p) plus duration (p)(q).</p>
<ul>
<li>(a)(p) is the time from the first sample in the buffer to the first sample of the drum hit (and to convert this duration in samples to a duration in milliseconds it must be divided by 44.1 (sample rate / 1000)).</li>
<li>(p)(q) is the latency. That&#8217;s <font face="monospace">((SampleDataEvent.position / 44.1) &#8211; SoundChannelObject.position)</font>. (See the <a href="http://help.adobe.com/en_US/AS3LCR/Flash_10.0/flash/events/SampleDataEvent.html">AS3.0 Language reference on SampleDataEvent</a>.)</li>
<li>The result is the total delay in milliseconds. A Timer could be started when the buffer is filled at (a) so that the LED blinks at the frame nearest (q).</li>
</ul>
<h1>2. Audio follows animation</h1>
<div style="position:relative; width:540px; height:400px;">

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="fm_app_304411141"
			align="left"
			class="flashmovie"
			width="400"
			height="400">
	<param name="movie" value="http://www.hisschemoller.com/flash/v015_latency/static/swf/app.swf" />
	<param name="menu" value="false" />
	<param name="quality" value="best" />
	<param name="scale" value="noscale" />
	<param name="bgcolor" value="#FFFFFF" />
	<param name="base" value="http://www.hisschemoller.com/flash/v015_latency/static/swf/" />
	<!--[if !IE]>-->
	<object	type="application/x-shockwave-flash"
			data="http://www.hisschemoller.com/flash/v015_latency/static/swf/app.swf"
			name="fm_app_304411141"
			align="left"
			width="400"
			height="400">
		<param name="menu" value="false" />
		<param name="quality" value="best" />
		<param name="scale" value="noscale" />
		<param name="bgcolor" value="#FFFFFF" />
		<param name="base" value="http://www.hisschemoller.com/flash/v015_latency/static/swf/" />
	<!--<![endif]-->
		
<p><a href="http://adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a></p>

	<!--[if !IE]>-->
	</object>
	<!--<![endif]-->
</object></div>
<p><a href="http://www.hisschemoller.com/flash/v015_latency/animation_sound_sync.zip">Download the source code of this example</a>.</p>
<p>This second scenario is a bit more complicated. Hopefully someone else has a simpler solution. But until then this is the solution I use.</p>
<p>Click the &#8216;Start&#8217; button in the example above: Balls bounce on the floor and each time one of them hits the floor a &#8216;boing&#8217; sound plays. A simple implementation of <a href="http://en.wikipedia.org/wiki/Newton%27s_laws_of_motion">Newton&#8217;s laws of motion</a>.</p>
<p>A graph of the events:<br />
<img src="http://www.hisschemoller.com/wp-content/uploads/2010/03/latency_graph2.jpg" alt="" title="Audio follows animation." width="540" height="225" class="alignleft size-full wp-image-497" /></p>
<ul>
<li>(a), (b), (c) &#8230; are the times when a buffer is requested and filled. Same as in the previous example.</li>
<li>(1), (2), (3) &#8230; are frames where Flash updates the screen (Event.ENTER_FRAME).</li>
<li>(p) is where the ball hitting the floor is calculated to happen.</li>
<li>(q) is where the sound is heard.</li>
<li>(r) is where the ball hitting the floor is drawn on screen.</li>
</ul>
<h1>What&#8217;s going on?</h1>
<p>The trick is to delay drawing the animation for a &#8216;latency&#8217; amount of time. </p>
<ol>
<li>First calculate where the ball WOULD be (but NOT draw it).</li>
<li>Fire a sound if the calculation shows the ball would hit the floor.</li>
<li>Draw the animation with a delay determined by the latency.</li>
</ol>
<p>That is the principle of how it works. The actual implementation is a bit more involved:</p>
<ul>
<li>At (a), (b), (c) &#8230; (each SampleDataEvent.SAMPLE_DATA) the position of the ball is calculated. The current time and position of the ball are stored in a Vector called _history.</li>
<li>If the calculation shows the ball will hit the floor during the current buffer (at  (p)):
<ul>
<li>The time from the start of the buffer till the hit is calculated (a)(p).</li>
<li>The sound is started at time offset (a)(p) in the buffer.</li>
</ul>
</li>
<li>Meanwhile at (1), (2), (3) &#8230; (at each Event.ENTER_FRAME) the screen is updated.</li>
<li>To know where to draw the ball the _history of the ball is used.</li>
<li>First get the current time and subtract the latency. This is the moment in the past that should be drawn on screen.</li>
<li>Search in the balls _history for that moment in the past. </li>
<li>It&#8217;s very unlikely however that the position at exactly the required millisecond is stored. More likely somewhere in between two stored moments in time. So get the closest positions before and after the requested time and use linear interpolation to draw the ball at the right position between the &#8216;before&#8217; and &#8216;after&#8217; records.</li>
</ul>
<h1>So what&#8217;s going on?</h1>
<p>I&#8217;m afraid this explanation might be more complex than the actual code. So I&#8217;ll add some of the relevant ActionScript. </p>
<p>To understand it you should know that there is an AudioDriver class that listens to the SampleDataEvent. Each SampleDataEvent it sends an ENTER_BUFFER notification (this is PureMVC) to notify all Ball instances to store their position. To do this the function enterBuffer is called on each Ball instance:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p488code5'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p4885"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
</pre></td><td class="code" id="p488code5"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> enterBuffer<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> : <span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">var</span> hit : <span style="color: #0066CC;">Boolean</span>;
&nbsp;
    <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>_isDragging<span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
        _vy = <span style="color: #cc66cc;">0</span>;
    <span style="color: #66cc66;">&#125;</span>
    <span style="color: #b1b100;">else</span>
    <span style="color: #66cc66;">&#123;</span>
        _vy += _gravity;
        <span style="color: #0066CC;">_y</span> += _vy;
&nbsp;
        <span style="color: #808080; font-style: italic;">/** If the ball hits the floor. */</span>
        <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">_y</span> <span style="color: #66cc66;">&gt;</span> _floorY - _radius<span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">var</span> distance : <span style="color: #0066CC;">Number</span> = _vy;
            <span style="color: #000000; font-weight: bold;">var</span> overlap : <span style="color: #0066CC;">Number</span> = <span style="color: #0066CC;">_y</span> + _radius - _floorY;
            _hitAlpha = <span style="color: #66cc66;">&#40;</span>distance - overlap<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">/</span> distance;
&nbsp;
            <span style="color: #0066CC;">_y</span> = _floorY - _radius - overlap;
            _vy <span style="color: #66cc66;">*</span>= -<span style="color: #cc66cc;">1</span>;
            hit = <span style="color: #000000; font-weight: bold;">true</span>;
&nbsp;
            dispatchEvent<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Event<span style="color: #66cc66;">&#40;</span>Ball.<span style="color: #006600;">BOUNCE</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/** Store as data for later use. */</span>
    _history.<span style="color: #0066CC;">splice</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #000000; font-weight: bold;">new</span> BallVO<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">_x</span>, <span style="color: #0066CC;">_y</span>, _vx, _vy, hit<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    <span style="color: #808080; font-style: italic;">/** Keep maximum 100 history items so the maximum latency is buffer * 100. */</span>
    <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>_history.<span style="color: #0066CC;">length</span> <span style="color: #66cc66;">&gt;</span> <span style="color: #cc66cc;">100</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">var</span> n : <span style="color: #0066CC;">int</span> = _history.<span style="color: #0066CC;">length</span>;
        <span style="color: #b1b100;">while</span><span style="color: #66cc66;">&#40;</span>--n <span style="color: #66cc66;">&gt;</span> <span style="color: #cc66cc;">99</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#123;</span>
            _history.<span style="color: #0066CC;">pop</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>This function does what was expected: A new position is calculated and stored in a BallVO value object, the BallVO is added to the _history and a bit of maintenance is done: Keep _history&#8217;s length within bounds.</p>
<p>The only other thing is the _isDragging Boolean. Balls can be dragged to a new position.</p>
<p>The AudioDriver also sends an ENTER_FRAME notification. This is just like a regular Event.ENTER_FRAME from a DisplayObject except that it has a AudioStreamInfoVO value object as an argument. The AudioStreamInfoVO has a &#8216;latencyInMS&#8217; property that holds (as you&#8217;ll have guessed) the latency in milliseconds that the animation should be delayed.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p488code6'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p4886"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
</pre></td><td class="code" id="p488code6"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> enterFrame<span style="color: #66cc66;">&#40;</span>infoVO : AudioStreamInfoVO<span style="color: #66cc66;">&#41;</span> : <span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
    graphics.<span style="color: #0066CC;">clear</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
    graphics.<span style="color: #0066CC;">lineStyle</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span>, 0xFFFFFF<span style="color: #66cc66;">&#41;</span>;
    graphics.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span>0xBBBBBB, <span style="color: #cc66cc;">0.1</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>_isDragging<span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
        graphics.<span style="color: #006600;">drawCircle</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">_x</span>, <span style="color: #0066CC;">_y</span>, _radius<span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
    <span style="color: #b1b100;">else</span>
    <span style="color: #66cc66;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">time</span> : <span style="color: #0066CC;">Number</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Date</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #0066CC;">getTime</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> - infoVO.<span style="color: #006600;">latencyInMS</span>;
&nbsp;
        <span style="color: #000000; font-weight: bold;">var</span> i : <span style="color: #0066CC;">int</span> = -<span style="color: #cc66cc;">1</span>;
        <span style="color: #000000; font-weight: bold;">var</span> n : <span style="color: #0066CC;">int</span> = _history.<span style="color: #0066CC;">length</span>;
        <span style="color: #b1b100;">while</span><span style="color: #66cc66;">&#40;</span>++i <span style="color: #66cc66;">&lt;</span> n<span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#123;</span>
            <span style="color: #808080; font-style: italic;">/** Get the data from immediately before and after 'time'. */</span>
            <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>_history<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">time</span> <span style="color: #66cc66;">&lt;</span> <span style="color: #0066CC;">time</span> <span style="color: #66cc66;">&amp;&amp;</span> i <span style="color: #66cc66;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>
            <span style="color: #66cc66;">&#123;</span>
                <span style="color: #000000; font-weight: bold;">var</span> before : BallVO = _history<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>;
                <span style="color: #000000; font-weight: bold;">var</span> after : BallVO = _history<span style="color: #66cc66;">&#91;</span>i - <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span>;
                <span style="color: #000000; font-weight: bold;">var</span> alpha : <span style="color: #0066CC;">Number</span> = <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">time</span> - before.<span style="color: #0066CC;">time</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">/</span> <span style="color: #66cc66;">&#40;</span>after.<span style="color: #0066CC;">time</span> - before.<span style="color: #0066CC;">time</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
                <span style="color: #808080; font-style: italic;">/** If a bounce on the floor happened between before and after. */</span>
                <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>before.<span style="color: #006600;">vy</span> <span style="color: #66cc66;">&gt;</span> <span style="color: #cc66cc;">0</span> <span style="color: #66cc66;">&amp;&amp;</span> after.<span style="color: #006600;">vy</span> <span style="color: #66cc66;">&lt;</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>
                <span style="color: #66cc66;">&#123;</span>
                    <span style="color: #808080; font-style: italic;">/** Linear interpolation to correct the vertical position. */</span>
                    <span style="color: #000000; font-weight: bold;">var</span> maxY : <span style="color: #0066CC;">Number</span> = _floorY - _radius;
                    <span style="color: #000000; font-weight: bold;">var</span> partBeforeBounce : <span style="color: #0066CC;">Number</span> = maxY - before.<span style="color: #006600;">y</span>;
                    <span style="color: #000000; font-weight: bold;">var</span> partAfterBounce : <span style="color: #0066CC;">Number</span> = maxY - after.<span style="color: #006600;">y</span>;
                    <span style="color: #000000; font-weight: bold;">var</span> bounceAlpha : <span style="color: #0066CC;">Number</span> = partBeforeBounce <span style="color: #66cc66;">/</span> <span style="color: #66cc66;">&#40;</span>partBeforeBounce + partAfterBounce<span style="color: #66cc66;">&#41;</span>;
&nbsp;
                    <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>alpha <span style="color: #66cc66;">&lt;</span> bounceAlpha<span style="color: #66cc66;">&#41;</span>
                    <span style="color: #66cc66;">&#123;</span>
                        <span style="color: #000000; font-weight: bold;">var</span> y : <span style="color: #0066CC;">Number</span> = before.<span style="color: #006600;">y</span> + <span style="color: #66cc66;">&#40;</span>before.<span style="color: #006600;">vy</span> <span style="color: #66cc66;">*</span> bounceAlpha<span style="color: #66cc66;">&#41;</span>;
                    <span style="color: #66cc66;">&#125;</span>
                    <span style="color: #b1b100;">else</span>
                    <span style="color: #66cc66;">&#123;</span>
                        y = after.<span style="color: #006600;">y</span> + <span style="color: #66cc66;">&#40;</span>before.<span style="color: #006600;">vy</span> <span style="color: #66cc66;">*</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span> - bounceAlpha<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
                    <span style="color: #66cc66;">&#125;</span>
                <span style="color: #66cc66;">&#125;</span>
                <span style="color: #b1b100;">else</span>
                <span style="color: #66cc66;">&#123;</span>
                    <span style="color: #808080; font-style: italic;">/** Regular linear interpolation for a ball in mid flight. */</span>
                    y = before.<span style="color: #006600;">y</span> + <span style="color: #66cc66;">&#40;</span>alpha <span style="color: #66cc66;">*</span> <span style="color: #66cc66;">&#40;</span>after.<span style="color: #006600;">y</span> - before.<span style="color: #006600;">y</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
                <span style="color: #66cc66;">&#125;</span>
&nbsp;
                <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>before.<span style="color: #006600;">hit</span><span style="color: #66cc66;">&#41;</span> graphics.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span>0xFFFFFF<span style="color: #66cc66;">&#41;</span>;
                graphics.<span style="color: #006600;">drawCircle</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">_x</span>, y, _radius<span style="color: #66cc66;">&#41;</span>;
&nbsp;
                <span style="color: #b1b100;">break</span>;
            <span style="color: #66cc66;">&#125;</span>
        <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>So in this function there&#8217;s a variable &#8216;time&#8217; which holds the moment in time in the past that must be drawn, &#8216;before.time&#8217; and &#8216;after.time&#8217; and a resulting &#8216;alpha&#8217; which is the interpolation value between before and after. The resulting ball position calculation is this:</p>
<p><font face="monospace">y = before.y + (alpha * (after.y &#8211; before.y));</font></p>
<p><div id="attachment_510" class="wp-caption alignright" style="width: 150px"><img src="http://www.hisschemoller.com/wp-content/uploads/2010/03/isaac_newton_small.jpg" alt="Isaac Newton" title="Isaac Newton" width="150" height="159" class="size-full wp-image-510" /><p class="wp-caption-text">Isaac Newton</p></div>The new thing here is that if the ball hits the floor somewhere between before and after  (if(before.vy > 0 &#038;&#038; after.vy < 0)), then the regular linear interpolation doesn&#8217;t give the right visual result and some extra code is necessary. But that&#8217;s another story.</p>
<p>All this code is not optimized at all. In this example I wanted to keep it all as clear as possible to read and understand. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.hisschemoller.com/2010/animation-audio-synchronization/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Augmented reality and sound</title>
		<link>http://www.hisschemoller.com/2010/augmented-reality-and-sound/</link>
		<comments>http://www.hisschemoller.com/2010/augmented-reality-and-sound/#comments</comments>
		<pubDate>Fri, 19 Feb 2010 13:01:00 +0000</pubDate>
		<dc:creator>Wouter</dc:creator>
				<category><![CDATA[ActionScript]]></category>

		<guid isPermaLink="false">http://www.hisschemoller.com/?p=343</guid>
		<description><![CDATA[Yes I know, it&#8217;s the hype of 2009 and we&#8217;re in 2010 now, but I haven&#8217;t seen anything yet where augmented reality is combined with sound. If you do know examples of augmented reality and audio, please leave a comment. I&#8217;d love to see what other people do with it. This is a small experiment [...]]]></description>
			<content:encoded><![CDATA[<p><a rel="attachment wp-att-344" href="http://www.hisschemoller.com/2010/augmented-reality-and-sound/flartoolkit/"><img class="alignright size-full wp-image-344" title="flartoolkit" src="http://www.hisschemoller.com/wp-content/uploads/2010/02/flartoolkit.jpg" alt="" width="263" height="186" /></a>Yes I know, it&#8217;s the hype of 2009 and we&#8217;re in 2010 now, but I haven&#8217;t seen anything yet where augmented reality is combined with sound. If you do know examples of augmented reality and audio, please leave a comment. I&#8217;d love to see what other people do with it.</p>
<p>This is a small experiment I started half a year ago but never finished. I looked it up again last week because I needed a Flash AR example for a project at work. Here I improved it a bit to make it more presentable.</p>
<p><span id="more-343"></span></p>
<h1>What it is</h1>
<p>It&#8217;s basically a sort of <a href="http://en.wikipedia.org/wiki/Theremin">theremin</a>. Hold the marker in front of your computer&#8217;s webcam and a sound starts to play. Move the marker and the sound changes.</p>
<h1>The Flash file</h1>
<div style="position:relative; width:540px; height:280px;">

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="fm_v010_flartoolkit_148763401"
			align="left"
			class="flashmovie"
			width="460"
			height="280">
	<param name="movie" value="http://www.hisschemoller.com/flash/v010_flartoolkit/static/swf/v010_flartoolkit.swf" />
	<param name="menu" value="false" />
	<param name="quality" value="best" />
	<param name="scale" value="noscale" />
	<param name="bgcolor" value="#112233" />
	<param name="base" value="http://www.hisschemoller.com/flash/v010_flartoolkit/static/swf/" />
	<!--[if !IE]>-->
	<object	type="application/x-shockwave-flash"
			data="http://www.hisschemoller.com/flash/v010_flartoolkit/static/swf/v010_flartoolkit.swf"
			name="fm_v010_flartoolkit_148763401"
			align="left"
			width="460"
			height="280">
		<param name="menu" value="false" />
		<param name="quality" value="best" />
		<param name="scale" value="noscale" />
		<param name="bgcolor" value="#112233" />
		<param name="base" value="http://www.hisschemoller.com/flash/v010_flartoolkit/static/swf/" />
	<!--<![endif]-->
		
<p><a href="http://adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a></p>

	<!--[if !IE]>-->
	</object>
	<!--<![endif]-->
</object></div>
<p><a href="http://www.hisschemoller.com/flash/v010_flartoolkit/augmented_reality_and_sound.zip">All source files can be downloaded here.</a> They&#8217;re a bit messy however. This was just a quick update to an abandoned older experiment, I didn&#8217;t take care to write clean code.</p>
<p>To try this you&#8217;ll need the marker to move in front of the webcam. <a href="http://www.hisschemoller.com/flash/v010_flartoolkit/static/bin/flarlogo-marker.pdf">Download it here as a PDF file</a>. Print the file and &#8211; for the best results &#8211; glue it on a piece of cardboard.</p>
<h1>The video</h1>
<p><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="400" height="238" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="src" value="http://vimeo.com/moogaloop.swf?clip_id=9495963&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=00ADEF&amp;fullscreen=1" /><embed type="application/x-shockwave-flash" width="400" height="238" src="http://vimeo.com/moogaloop.swf?clip_id=9495963&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=00ADEF&amp;fullscreen=1" allowscriptaccess="always" allowfullscreen="true"></embed></object><br />
Because I know it&#8217;s a lot of hassle to download and print the marker I recorded a short demonstration screencap video to show how it all works.</p>
<h1>How to use it</h1>
<p>Click &#8220;Allow&#8221; on the Flash player settings screen. This allows Flash to switch on the webcam and use the images it films. Then hold the printed marker in front of the webcam to generate some sounds:</p>
<ul>
<li>With the marker at the center of the video a sine wave plays a C note.</li>
<li>Move the marker slowly up and the pitch rises in steps, move down and the pitch goes down. It plays a pentatonic scale in the key of C minor. Two octaves up and two down. With added flattened fifth to make it sound more bluesy.</li>
<li>Move the marker to the right to add vibrato. The more to the right the faster and the more intense the vibrato gets.</li>
<li>Move left from the center and a &#8216;note repeat&#8217; effect starts. At the outer left edge of the webcam the repeat effect doubles in speed. When the marker is moved vertically at the left new notes start and combine with the previous notes that still play and produce a harp-like sound. Very nice.</li>
</ul>
<p>This idea could be taken a lot further. There&#8217;s more information to be had from the marker:</p>
<ul>
<li>The distance from the webcam can be measured. To lower the volume the farther the marker is away from the webcam for instance.</li>
<li>The three rotations in 3D are available: Rotation around the X, Y and Z axis.</li>
</ul>
<p>The only obstacle for direct musical use is the latency. The delay between a movement and hearing the resulting sound is too big to be able to play along with a record for example.</p>
<p>For the augmented reality code I used FLARToolkit. More information can be found at the <a href="http://www.libspark.org/wiki/saqoosha/FLARToolKit/en">English FLARToolKit page</a> and the <a href="http://saqoosha.net/en/flartoolkit/start-up-guide">Start Up Guide</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hisschemoller.com/2010/augmented-reality-and-sound/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>MP3 Wave Display 2</title>
		<link>http://www.hisschemoller.com/2010/mp3-wave-display-2/</link>
		<comments>http://www.hisschemoller.com/2010/mp3-wave-display-2/#comments</comments>
		<pubDate>Sun, 17 Jan 2010 19:18:24 +0000</pubDate>
		<dc:creator>Wouter</dc:creator>
				<category><![CDATA[ActionScript]]></category>

		<guid isPermaLink="false">http://www.hisschemoller.com/?p=290</guid>
		<description><![CDATA[Wave Display 2 is an improved version of the last one (in the previous post). A few things had to be rewritten in the code for waveform rendering, and there were some extra features I wanted to include to make the application a bit more interesting to use. The new features in this version: Audio [...]]]></description>
			<content:encoded><![CDATA[<div id="attachment_291" class="wp-caption alignright" style="width: 265px"><a href="http://www.hisschemoller.com/2010/mp3-wave-display-2/"><img src="http://www.hisschemoller.com/wp-content/uploads/2010/01/wavedisplay2.gif" alt="This version is orange." title="wavedisplay2" width="265" height="267" class="size-full wp-image-291" /></a><p class="wp-caption-text">MP3 wave display is orange.</p></div>
<p>Wave Display 2 is an improved version of the last one (in the <a href="http://www.hisschemoller.com/2010/mp3-wave-display/">previous post</a>). A few things had to be rewritten in the code for waveform rendering, and there were some extra features I wanted to include to make the application a bit more interesting to use.</p>
<p>The new features in this version:
<ul>
<li>Audio playback. You can now listen to the loaded MP3 file. Click &#8216;play mp3&#8242; or press the SPACE bar to listen.</li>
<li>Draggable start and end locators to select only part of the sound. Drag the &#8216;S&#8217; and &#8216;E&#8217; locators to set the start and end point.</li>
<li>Reverse playback. Click the &#8216;reverse&#8217; button to toggle reverse playback.</li>
<li>Adjustable playback speed. Drag the &#8216;transpose&#8217; slider to change the playback speed. The speed is adjusted so that the pitch changes from one octave down to one octave up in semitone steps.</li>
<li>Drag to zoom. Press and drag the mouse on the display area for finer control: Drag vertical to zoom and horizontal to move the waveform. I always really liked this behaviour in <a href="http://www.steinberg.net">Steinberg</a> <a href="http://www.steinberg.net/en/products/audioediting_product/audioediting_wavelab6.html">WaveLab</a> where I first saw it, and where I now copied it from.</li>
</ul>
<p><span id="more-290"></span><br />

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="fm_app_569725531"
			class="flashmovie"
			width="450"
			height="450">
	<param name="movie" value="http://www.hisschemoller.com/flash/v012_wavedisplay2/static/swf/app.swf" />
	<param name="menu" value="false" />
	<param name="quality" value="best" />
	<param name="scale" value="noscale" />
	<param name="base" value="http://www.hisschemoller.com/flash/v012_wavedisplay2/static/swf/" />
	<!--[if !IE]>-->
	<object	type="application/x-shockwave-flash"
			data="http://www.hisschemoller.com/flash/v012_wavedisplay2/static/swf/app.swf"
			name="fm_app_569725531"
			width="450"
			height="450">
		<param name="menu" value="false" />
		<param name="quality" value="best" />
		<param name="scale" value="noscale" />
		<param name="base" value="http://www.hisschemoller.com/flash/v012_wavedisplay2/static/swf/" />
	<!--<![endif]-->
		
<p><a href="http://adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a></p>

	<!--[if !IE]>-->
	</object>
	<!--<![endif]-->
</object>
<p><a href="http://www.hisschemoller.com/flash/v012_wavedisplay2/mp3_wavedisplay2.zip">Click here to download all source files in a ZIP.</a></p>
<h1>Transpose by playback speed</h1>
<p>Transpose works fairly straightforward. The transpose value is set with the slider. And every time the slider is moved the new transpose value is stored.</p>
<p>Each time playback is started, the transpose value is passed to the class PitchedSamplePlayer that processes the sound. However,  PitchedSamplePlayer uses a multiplier to set the playback speed instead of a semitone value. For example 0.5 is playback at half speed. So the transpose value in semitones must be converted to playback speed. That&#8217;s done with this conversion:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p290code9'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p2909"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code" id="p290code9"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/**
 * Covert semitone shift to playback speed multiplier.
 * For example: -12 returns 0.5, 12 returns 2, 0 returns 1.
 */</span>
<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">function</span> semitoneToRate<span style="color: #66cc66;">&#40;</span>semitone : <span style="color: #0066CC;">Number</span><span style="color: #66cc66;">&#41;</span> : <span style="color: #0066CC;">Number</span>
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">pow</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span>, semitone <span style="color: #66cc66;">/</span> <span style="color: #cc66cc;">12</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Then inside PitchedSamplePlayer linear interpolation is used to change the playback speed and maintain acceptable sound quality. I took the code for pitching a sample from this <a href="http://blog.andre-michelle.com/2009/pitch-mp3/">MP3 Pitch example</a> by <a href="http://www.andre-michelle.com">Andre Michelle</a>, which builds on <a href="http://www.leebrimelow.com/">Lee Brimelow</a>&#8216;s post <a href="http://theflashblog.com/?p=1129">Adjusting audio pitch in Flash Player 10</a>.</p>
<h1>Reverse playback</h1>
<p>The display and the sample player both read samples from a ByteArray. I had the choice of reversing the whole ByteArray or leaving the ByteArray as it is and read from it backwards. From end to start.</p>
<p>It seemed best to reverse the order of the ByteArray because the audio processing and display rendering code can stay the same and as short and fast as possible:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p290code10'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p29010"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code" id="p290code10"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/** Reverse the order of samples in samplerPresetVO.soundData.byteArray. */</span>
<span style="color: #000000; font-weight: bold;">var</span> bytesPerSample : <span style="color: #0066CC;">int</span> = AudioDriverProxy.<span style="color: #006600;">BYTES_PER_SAMPLE</span>;
<span style="color: #000000; font-weight: bold;">var</span> numBytes : <span style="color: #0066CC;">Number</span> = samplerPresetVO.<span style="color: #006600;">soundData</span>.<span style="color: #006600;">numSamplesTotal</span> <span style="color: #66cc66;">*</span> bytesPerSample;
<span style="color: #000000; font-weight: bold;">var</span> tempByteArray : ByteArray = <span style="color: #000000; font-weight: bold;">new</span> ByteArray<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
samplerPresetVO.<span style="color: #006600;">soundData</span>.<span style="color: #006600;">byteArray</span>.<span style="color: #0066CC;">position</span> = <span style="color: #cc66cc;">0</span>;
tempByteArray.<span style="color: #006600;">writeBytes</span><span style="color: #66cc66;">&#40;</span>samplerPresetVO.<span style="color: #006600;">soundData</span>.<span style="color: #006600;">byteArray</span><span style="color: #66cc66;">&#41;</span>;
samplerPresetVO.<span style="color: #006600;">soundData</span>.<span style="color: #006600;">byteArray</span>.<span style="color: #0066CC;">position</span> = <span style="color: #cc66cc;">0</span>;
&nbsp;
<span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> offset : <span style="color: #0066CC;">Number</span> = <span style="color: #66cc66;">&#40;</span>numBytes - bytesPerSample<span style="color: #66cc66;">&#41;</span>; offset <span style="color: #66cc66;">&gt;</span>= <span style="color: #cc66cc;">0</span>; offset -= bytesPerSample<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
   samplerPresetVO.<span style="color: #006600;">soundData</span>.<span style="color: #006600;">byteArray</span>.<span style="color: #006600;">writeBytes</span><span style="color: #66cc66;">&#40;</span>tempByteArray, offset, bytesPerSample<span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>What happens here is:
<ul>
<li>Copy all sample data to a temporary ByteArray.</li>
<li>Read two samples (or one stereo sample) at a time from the temporary ByteArray, reading backwards from end to start.</li>
<li>And write each stereo sample to the original ByteArray, writing normally from start to end.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.hisschemoller.com/2010/mp3-wave-display-2/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>MP3 Wave Display</title>
		<link>http://www.hisschemoller.com/2010/mp3-wave-display/</link>
		<comments>http://www.hisschemoller.com/2010/mp3-wave-display/#comments</comments>
		<pubDate>Sat, 09 Jan 2010 18:02:09 +0000</pubDate>
		<dc:creator>Wouter</dc:creator>
				<category><![CDATA[ActionScript]]></category>

		<guid isPermaLink="false">http://www.hisschemoller.com/?p=250</guid>
		<description><![CDATA[2010! A new year, a new decennium even! This year I want to be more consistent in publishing articles on this weblog. That&#8217;s my new year&#8217;s resolution. The first one of the new year is more of a utility than a complete application. It&#8217;s a wave display; It loads an MP3 file and displays the [...]]]></description>
			<content:encoded><![CDATA[<div id="attachment_251" class="wp-caption alignright" style="width: 280px"><a href="http://www.hisschemoller.com/2010/mp3-wave-display/"><img class="size-full wp-image-251" title="wavedisplay" src="http://www.hisschemoller.com/wp-content/uploads/2010/01/wavedisplay.gif" alt="The waveform display panel" width="280" height="260" /></a><p class="wp-caption-text">The waveform display</p></div>
<p>2010! A new year, a new decennium even! This year I want to be more consistent in publishing articles on this weblog. That&#8217;s my new year&#8217;s resolution.</p>
<p>The first one of the new year is more of a utility than a complete application. It&#8217;s a wave display; It loads an MP3 file and displays the sound as a graph of the waveform. It makes no sound. A simple waveform display like this might be a useful component in a larger application where audio needs to be edited. Here it&#8217;s on its own because I was figuring out how to best program such a component. So this application is more experimental than useful.</p>
<p><span id="more-250"></span><br />

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="fm_app_2012275971"
			class="flashmovie"
			width="450"
			height="450">
	<param name="movie" value="http://www.hisschemoller.com/flash/v011_wavedisplay/static/swf/app.swf" />
	<param name="menu" value="false" />
	<param name="quality" value="best" />
	<param name="scale" value="noscale" />
	<param name="base" value="http://www.hisschemoller.com/flash/v011_wavedisplay/static/swf/" />
	<!--[if !IE]>-->
	<object	type="application/x-shockwave-flash"
			data="http://www.hisschemoller.com/flash/v011_wavedisplay/static/swf/app.swf"
			name="fm_app_2012275971"
			width="450"
			height="450">
		<param name="menu" value="false" />
		<param name="quality" value="best" />
		<param name="scale" value="noscale" />
		<param name="base" value="http://www.hisschemoller.com/flash/v011_wavedisplay/static/swf/" />
	<!--<![endif]-->
		
<p><a href="http://adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a></p>

	<!--[if !IE]>-->
	</object>
	<!--<![endif]-->
</object>
<p><a href="http://www.hisschemoller.com/flash/v011_wavedisplay/wavedisplay.zip">Click here to download all source files in a ZIP.</a></p>
<h1>How to use it</h1>
<p>A default MP3 file is loaded and displayed at startup. But you can load your own MP3. Click the &#8216;load mp3&#8242; button and browse to an MP3 file on your computer. Opening a large MP3 file might take a long time however. Best to try with short files. Five minutes or less for example.</p>
<p>Once the MP3 is loaded and displayed there&#8217;s several controls to zoom in closer on details of the waveform:</p>
<ul>
<li>Zoom in to a detail of the waveform with the &#8216;zoom horizontal&#8217; slider.</li>
<li>When zoomed in you can move through the waveform with the scrollbar directly under the waveform.</li>
<li>For more precise movement once zoomed in drag the mouse left or right on the waveform.</li>
<li>Magnify the waveform vertically with the &#8216;zoom vertical&#8217; slider. This is useful to inspect very quiet parts of the sound.</li>
</ul>
<p>Some information is shown above the display</p>
<ul>
<li>In Flash the sample rate is 44100 samples per second. When the waveform is at the highest zoom level, it shows one sample per pixel. (So one second of audio would take up 44100 pixels!)</li>
<li>At lower zoom levels each pixel displays the average amplitude of several pixels. The amount is displayed  at &#8216;samples per pixel&#8217;.</li>
<li>The first and last sample in view are shown at &#8216;first sample&#8217; and &#8216;last sample&#8217;.</li>
</ul>
<h1>Some notes on the ActionScript</h1>
<p>The biggest problem with the display was to get it to respond quick to zoom and position changes. It should use as little calculations as possible to refresh the display. Three things reduce the number of calculations (and so the processor load) here:</p>
<p>Each change in zoom level or position results in a changed portion of the waveform that is actually shown in the display. When the display needs updating, it&#8217;s first determined what the first and last sample are that will be displayed. Then only that portion of the file is processed. That means that especially at high zoom levels only a tiny fraction of the file needs to be calculated.</p>
<p>But at low zoom levels still a lot of samples need to be averaged: To show one minute of waveform 44100 x 60 = 2,646,000 samples must be processed. More than two and a half million! To reduce this number samples are skipped. The more samples there are, the more are skipped.</p>
<p>This bit of ActionScript shows how these two reductions are done:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p250code14'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p25014"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
</pre></td><td class="code" id="p250code14"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/**
 * Sets the zoom level of the display. Measured in how many samples one horizontal pixel of the display represents.
 * @param inSamplesPerPixel : How many samples one horizontal pixel of the display represents.
 * @param inCenterSamplePosition : The center or focus of the zoom. 0.5 is the horizontal center. 
 */</span>
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> setCurrentSamplesPerPixel<span style="color: #66cc66;">&#40;</span>inSamplesPerPixel : <span style="color: #0066CC;">Number</span>, inCenterSamplePosition : <span style="color: #0066CC;">Number</span> = <span style="color: #cc66cc;">0.5</span><span style="color: #66cc66;">&#41;</span> : <span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
  <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>inSamplesPerPixel == mCurrentSamplesPerPixel<span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">return</span>;
&nbsp;
  <span style="color: #808080; font-style: italic;">// get the sample that is the center of the zoom</span>
  <span style="color: #000000; font-weight: bold;">var</span> centerSample : <span style="color: #0066CC;">int</span> = mFirstSampleInView + <span style="color: #66cc66;">&#40;</span>mNumSamplesInView <span style="color: #66cc66;">*</span> inCenterSamplePosition<span style="color: #66cc66;">&#41;</span>;
&nbsp;
  mCurrentSamplesPerPixel = <span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">max</span><span style="color: #66cc66;">&#40;</span>mMinSamplesPerPixel, <span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">min</span><span style="color: #66cc66;">&#40;</span>inSamplesPerPixel, mMaxSamplesPerPixel<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
  mNumSamplesInView = mCurrentSamplesPerPixel <span style="color: #66cc66;">*</span> mDisplayWidth;
  mFirstSampleInView = centerSample - <span style="color: #66cc66;">&#40;</span>mNumSamplesInView <span style="color: #66cc66;">*</span> inCenterSamplePosition<span style="color: #66cc66;">&#41;</span>;
&nbsp;
  <span style="color: #808080; font-style: italic;">// polling stuff</span>
  mPollingFrequency = <span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">ceil</span><span style="color: #66cc66;">&#40;</span>mCurrentSamplesPerPixel <span style="color: #66cc66;">/</span> <span style="color: #cc66cc;">20</span><span style="color: #66cc66;">&#41;</span>;
  mNumPolls = <span style="color: #0066CC;">int</span><span style="color: #66cc66;">&#40;</span>mCurrentSamplesPerPixel <span style="color: #66cc66;">/</span> mPollingFrequency<span style="color: #66cc66;">&#41;</span>;
  mBytesToSkip = <span style="color: #66cc66;">&#40;</span>mPollingFrequency - <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">*</span> SoundProxy.<span style="color: #006600;">BYTES_PER_SAMPLE</span>;
&nbsp;
  <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>mFirstSampleInView <span style="color: #66cc66;">&lt;</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> mFirstSampleInView = <span style="color: #cc66cc;">0</span>;
  <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>mFirstSampleInView + mNumSamplesInView<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&gt;</span> mNumSamplesTotal<span style="color: #66cc66;">&#41;</span> mFirstSampleInView = mNumSamplesTotal - mNumSamplesInView;
&nbsp;
  updateGraphic<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>And the third thing to reduce the processor load is a simple timer. There&#8217;s a maximum of how often the display can update. In this case a maximum of once every 50 milliSeconds was fine. The timer script was added at the start of the update function:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p250code15'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p25015"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p250code15"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> mUpdateDelayTimer : Timer = <span style="color: #000000; font-weight: bold;">new</span> Timer<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">50</span>, <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;</pre></td></tr></table></div>


<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p250code16'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p25016"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code" id="p250code16"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/**
 * Redraws the display.
 */</span>
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> updateGraphic<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> : <span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
  <span style="color: #808080; font-style: italic;">// delay between updates</span>
  <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>mUpdateDelayTimer.<span style="color: #006600;">running</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">return</span>;
  mUpdateDelayTimer.<span style="color: #0066CC;">start</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
  <span style="color: #808080; font-style: italic;">// etcetera ...</span></pre></td></tr></table></div>

<p>That&#8217;s it for now. Download the <a href="flash/v011_wavedisplay/wavedisplay.zip">source files</a> to have a closer look at the code or to use it in your own projects.</p>
<p>This project is build with <a href="http://www.puremvc.org">PureMVC</a> as framework again. I like it more and more.<br />
The UI components I used here are very much based on <a href="http://www.bit-101.com">Keith Peters</a>&#8216; <a href="http://code.google.com/p/minimalcomps/">Minimal Comps</a>.<br />
To load an MP3 file and use it in Flash as a Sound object I used <a href="http://www.flexiblefactory.co.uk">FlexibleFactory</a>&#8216;s <a href="http://www.flexiblefactory.co.uk/flexible/?p=46">MP3FileReferenceLoader</a> library.<br />
For debugging I used the <a href="http://code.google.com/p/yalog/">Yalog</a> logging tool by <a href="http://stephan.acidcats.nl/blog/">Stephan Bezoen</a>. Very nice Air application.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hisschemoller.com/2010/mp3-wave-display/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>

