<?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>computermusicblog.com &#187; wav</title>
	<atom:link href="http://computermusicblog.com/blog/index.php/tag/wav/feed/" rel="self" type="application/rss+xml" />
	<link>http://computermusicblog.com/blog</link>
	<description>electronic and computer music as it happens</description>
	<lastBuildDate>Tue, 31 Jan 2012 07:37:02 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Reading and Writing WAV Files in Java</title>
		<link>http://computermusicblog.com/blog/2008/08/29/reading-and-writing-wav-files-in-java/</link>
		<comments>http://computermusicblog.com/blog/2008/08/29/reading-and-writing-wav-files-in-java/#comments</comments>
		<pubDate>Fri, 29 Aug 2008 16:58:32 +0000</pubDate>
		<dc:creator>evan</dc:creator>
				<category><![CDATA[Featured]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[wav]]></category>

		<guid isPermaLink="false">http://computermusicblog.com/blog/?p=64</guid>
		<description><![CDATA[<p>Earlier this year, we looked at how to read and write wav files in C++ and VB.Net, and C/C++ is really the natural choice for this kind of file IO (for a number of reasons that we won&#8217;t go into here).  Nonetheless, not everyone uses C or C++ for computer music software.  Increasingly, [...]]]></description>
			<content:encoded><![CDATA[<p>Earlier this year, we looked at how to read and write wav files in C++ and VB.Net, and C/C++ is really the natural choice for this kind of file IO (for a number of reasons that we won&#8217;t go into here).  Nonetheless, not everyone uses C or C++ for computer music software.  Increasingly, programmers are turning to Java.
</p>
<p>
There are a number of libraries available for reading and writing wav files, however, to really understand the format, it&#8217;s necessary to get your hands dirty.  So in this article, we are looking at reading and writing wav files in Java, which is not as easy as it may seem.
</p>
<p>
<span class="subtitle">Java Numbers</a>
</p>
<p>
The problem is that Java has no unsigned types.  In other words, all numerical types in java can hold either positive or negative numbers.  There are no types that are designed for only positive numbers.
</p>
<p>
This doesn&#8217;t sound like a big deal unless you know a little bit about how negative numbers are represented in most programming languages.  The most important thing to know is that most languages represent negative numbers, in a way that is incompatible with the way they represent only positive numbers.  More specifically, wav file headers represent numbers as <a href="http://en.wikipedia.org/wiki/Integer_(computer_science)">unsigned bytes</a>, whereas java represents numbers using a format called <a href="http://en.wikipedia.org/wiki/Two%27s_complement">Two&#8217;s Complement</a>.
</p>
<p>
The practical implications of this are simple: you can&#8217;t use java&#8217;s built-in IO functions for reading wav headers.  You have to read the data as bytes, then do the conversion in a subroutine.
</p>
<p>
Understanding this problem is hard, but luckily, working around it is relatively easy.  In our solution, we made 2 changes.  First, we stored all of our numbers with more bytes than are necessary in other languages (to ensure that we are storing the correct numbers).  So for each 2-byte number, which would usually be stored by a short, we used an int, and for each 4-byte number, we used a long.  Then we added 4 extra subroutines to do the conversions for us.  These routines are at the bottom of <a href="http://www.computermusicblog.com/sourcecode/javaWavIO/wavIO.java">wavIO.java</a>.
</p>
<blockquote>
<p>
<span class="comment">// these two routines convert a byte array to a unsigned short</span><br />
public static int byteArrayToInt(byte[] b)<br />
{</p>
<p>&nbsp;&nbsp;int start = 0;<br />
&nbsp;&nbsp;int low = b[start] &#038; 0xff;<br />
&nbsp;&nbsp;int high = b[start+1] &#038; 0xff;<br />
&nbsp;&nbsp;return (int)( high &lt;&lt; 8 | low );<br />
}
</p>
<p>
<span class="comment">// these two routines convert a byte array to an unsigned integer</span></p>
<p>public static long byteArrayToLong(byte[] b)<br />
{<br />
&nbsp;&nbsp;int start = 0;<br />
&nbsp;&nbsp;int i = 0;<br />
&nbsp;&nbsp;int len = 4;<br />
&nbsp;&nbsp;int cnt = 0;<br />
&nbsp;&nbsp;byte[] tmp = new byte[len];<br />
&nbsp;&nbsp;for (i = start; i &lt; (start + len); i++)</p>
<p>&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;tmp[cnt] = b[i];<br />
&nbsp;&nbsp;&nbsp;&nbsp;cnt++;<br />
&nbsp;&nbsp;}<br />
&nbsp;&nbsp;long accum = 0;<br />
&nbsp;&nbsp;i = 0;<br />
&nbsp;&nbsp;for ( int shiftBy = 0; shiftBy &lt; 32; shiftBy += 8 )<br />
&nbsp;&nbsp;{</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;accum |= ( (long)( tmp[i] &#038; 0xff ) ) &lt;&lt; shiftBy;<br />
&nbsp;&nbsp;&nbsp;&nbsp;i++;<br />
&nbsp;&nbsp;}<br />
&nbsp;&nbsp;return accum;<br />
}
</p>
<p>
<span class="comment">// returns a byte array of length 4</span></p>
<p>private static byte[] intToByteArray(int i)<br />
{<br />
&nbsp;&nbsp;byte[] b = new byte[4];<br />
&nbsp;&nbsp;b[0] = (byte) (i &#038; 0x00FF);<br />
&nbsp;&nbsp;b[1] = (byte) ((i &gt;&gt; 8) &#038; 0x000000FF);<br />
&nbsp;&nbsp;b[2] = (byte) ((i &gt;&gt; 16) &#038; 0x000000FF);</p>
<p>&nbsp;&nbsp;b[3] = (byte) ((i &gt;&gt; 24) &#038; 0x000000FF);<br />
&nbsp;&nbsp;return b;<br />
}
</p>
<p>
<span class="comment">// convert a short to a byte array</span><br />
public static byte[] shortToByteArray(short data)<br />
{</p>
<p>&nbsp;&nbsp;return new byte[]{(byte)(data &#038; 0xff),(byte)((data &gt;&gt;&gt; 8) &#038; 0xff)};<br />
}
</p>
</blockquote>
<p>
<span class="subtitle">WAV File Specification</a>
</p>
<p>
Again, we are using a VERY basic version of the wav file format for this example.  This won&#8217;t read many wav files, it will only read <a href="http://ccrma.stanford.edu/courses/422/projects/WaveFormat/">what are referred to here as canonical wave files</a>.  Basically, old fashioned wav files (before the chaos introduced by 24 and 32 bit recording).
</p>
<p>
If you want to read more complicated wav files, you may want to check out what&#8217;s written <a href="http://www-mmsp.ece.mcgill.ca/Documents/AudioFormats/WAVE/WAVE.html">here</a> and <a href="http://www.sonicspot.com/guide/wavefiles.html#wavefilechunks">here</a>, although you really need to look at the actual specification (and you had better have some free time).
</p>
<p>
<span class="subtitle">The Source Code</a>
</p>
<p>
To use the source code, just unzip all the files into the same directory, and then use the batch files I created called <i>compile.bat</i> and <i>run.bat</i>.  Of course, I am assuming that you have <a href="http://java.sun.com/javase/downloads/index.jsp">the latest version of java installed</a>.
</p>
<p>
<a href="http://www.computermusicblog.com/sourcecode/javaWavIO/wavIO.zip">Download the source code for reading and writing wav files in java here</a>.</p>
<div class="tweetthis" style="text-align:left;"><p> <a class="tt" href="http://twitter.com/intent/tweet?text=Reading+and+Writing+WAV+Files+in+Java+http%3A%2F%2Fcomputermusicblog.com%2Fblog%2F%3Fp%3D64" title="Post to Twitter"><img class="nothumb" src="http://computermusicblog.com/blog/wp-content/plugins/tweet-this/icons/en/twitter/tt-twitter3.png" alt="Post to Twitter" /></a> <a class="tt" href="http://twitter.com/intent/tweet?text=Reading+and+Writing+WAV+Files+in+Java+http%3A%2F%2Fcomputermusicblog.com%2Fblog%2F%3Fp%3D64" title="Post to Twitter"> </a> <a class="tt" href="http://buzz.yahoo.com/buzz?targetUrl=http://computermusicblog.com/blog/2008/08/29/reading-and-writing-wav-files-in-java/&amp;headline=Reading+and+Writing+WAV+Files+in+Java" title="Post to Yahoo Buzz"><img class="nothumb" src="http://computermusicblog.com/blog/wp-content/plugins/tweet-this/icons/en/buzz/tt-buzz.png" alt="Post to Yahoo Buzz" /></a> <a class="tt" href="http://buzz.yahoo.com/buzz?targetUrl=http://computermusicblog.com/blog/2008/08/29/reading-and-writing-wav-files-in-java/&amp;headline=Reading+and+Writing+WAV+Files+in+Java" title="Post to Yahoo Buzz"> </a> <a class="tt" href="http://digg.com/submit?url=http://computermusicblog.com/blog/2008/08/29/reading-and-writing-wav-files-in-java/&amp;title=Reading+and+Writing+WAV+Files+in+Java" title="Post to Digg"><img class="nothumb" src="http://computermusicblog.com/blog/wp-content/plugins/tweet-this/icons/en/digg/tt-digg.png" alt="Post to Digg" /></a> <a class="tt" href="http://digg.com/submit?url=http://computermusicblog.com/blog/2008/08/29/reading-and-writing-wav-files-in-java/&amp;title=Reading+and+Writing+WAV+Files+in+Java" title="Post to Digg"> </a> <a class="tt" href="http://www.facebook.com/share.php?u=http://computermusicblog.com/blog/2008/08/29/reading-and-writing-wav-files-in-java/&amp;t=Reading+and+Writing+WAV+Files+in+Java" title="Post to Facebook"><img class="nothumb" src="http://computermusicblog.com/blog/wp-content/plugins/tweet-this/icons/en/facebook/tt-facebook.png" alt="Post to Facebook" /></a> <a class="tt" href="http://www.facebook.com/share.php?u=http://computermusicblog.com/blog/2008/08/29/reading-and-writing-wav-files-in-java/&amp;t=Reading+and+Writing+WAV+Files+in+Java" title="Post to Facebook"> </a> <a class="tt" href="http://www.myspace.com/Modules/PostTo/Pages/?l=3&amp;u=http://computermusicblog.com/blog/2008/08/29/reading-and-writing-wav-files-in-java/&amp;t=Reading+and+Writing+WAV+Files+in+Java" title="Post to MySpace"><img class="nothumb" src="http://computermusicblog.com/blog/wp-content/plugins/tweet-this/icons/en/myspace/tt-myspace.png" alt="Post to MySpace" /></a> <a class="tt" href="http://www.myspace.com/Modules/PostTo/Pages/?l=3&amp;u=http://computermusicblog.com/blog/2008/08/29/reading-and-writing-wav-files-in-java/&amp;t=Reading+and+Writing+WAV+Files+in+Java" title="Post to MySpace"> </a> <a class="tt" href="http://stumbleupon.com/submit?url=http://computermusicblog.com/blog/2008/08/29/reading-and-writing-wav-files-in-java/&amp;title=Reading+and+Writing+WAV+Files+in+Java" title="Post to StumbleUpon"><img class="nothumb" src="http://computermusicblog.com/blog/wp-content/plugins/tweet-this/icons/en/su/tt-su.png" alt="Post to StumbleUpon" /></a> <a class="tt" href="http://stumbleupon.com/submit?url=http://computermusicblog.com/blog/2008/08/29/reading-and-writing-wav-files-in-java/&amp;title=Reading+and+Writing+WAV+Files+in+Java" title="Post to StumbleUpon"> </a></p></div>]]></content:encoded>
			<wfw:commentRss>http://computermusicblog.com/blog/2008/08/29/reading-and-writing-wav-files-in-java/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
	</channel>
</rss>

