<?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>Hear A Blog &#187; wav</title>
	<atom:link href="http://blog.hearablog.com/tag/wav/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.hearablog.com</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Wed, 26 May 2010 19:15:16 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=6037</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Mixing audio in C# using NAudio</title>
		<link>http://blog.hearablog.com/2010/01/mixing-audio-in-c-using-naudio/</link>
		<comments>http://blog.hearablog.com/2010/01/mixing-audio-in-c-using-naudio/#comments</comments>
		<pubDate>Thu, 07 Jan 2010 16:30:04 +0000</pubDate>
		<dc:creator>Daniel</dc:creator>
				<category><![CDATA[Other]]></category>
		<category><![CDATA[.Net]]></category>
		<category><![CDATA[audio]]></category>
		<category><![CDATA[audio-processing]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[mp3]]></category>
		<category><![CDATA[NAudio]]></category>
		<category><![CDATA[wav]]></category>
		<category><![CDATA[wave]]></category>

		<guid isPermaLink="false">http://blog.hearablog.com/?p=57</guid>
		<description><![CDATA[
			
				
			
		
Processing audio is one of our core tasks. When you create a startup there are a thousand things that you could do manually or automate. Which ones do you automate? We decided to automate as little as possible. Yes, you&#8217;ve read it correctly; as little as possible.
Automating is great because you solve a problem once [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fblog.hearablog.com%2F2010%2F01%2Fmixing-audio-in-c-using-naudio%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fblog.hearablog.com%2F2010%2F01%2Fmixing-audio-in-c-using-naudio%2F&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>Processing audio is one of our core tasks. When you create a startup there are a thousand things that you could do manually or automate. Which ones do you automate? We decided to automate as little as possible. Yes, you&#8217;ve read it correctly; as <em>little</em> as possible.</p>
<p>Automating is great because you solve a problem once and you don&#8217;t have to do it ever again. If you know that you&#8217;ll have to do it again. In a startup, you don&#8217;t know what you&#8217;ll be doing tomorrow. They are higly experimetal endevours. We decided to do all the audio processing by hand; and we did it until <a href="http://audacity.sourceforge.net/">Audacity, an excellent audio processing tool</a>, became a recurring character in our nightmares.</p>
<p>We decided to automate. We picked the library <a href="http://naudio.codeplex.com/">NAudio</a> because it&#8217;s free and apparently capable of doing what we need. We struggle with it at first, but it turned out there was a bug, which was solved quite quickly by the developers. Surely they deserve all the donations they can get, as soon as we are profitable, we&#8217;ll make one.</p>
<p>Our process is simple:</p>
<ol>
<li>The intro starts</li>
<li>6 seconds after that the main audio starts</li>
<li>30 seconds after the main audio finishes, the outro finishes.</li>
</ol>
<p>That ensures the proper synchronization of our amazing intro and outro. And the code is quite simple.</p>
<p>First, we load the intro, the outro and the main audio:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">var intro <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> WaveFileReader<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Intro.wav&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
var outro <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> WaveFileReader<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Outro.wav&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
var audio <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> WaveFileReader<span style="color: #000000;">&#40;</span>OriginalAudioFileName<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>Then we create the mixer stream:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">var mixer <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> WaveMixerStream32<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
mixer.<span style="color: #0000FF;">AutoStop</span><span style="color: #008000;">;</span></pre></div></div>

<p>Out of the loaded audios we create new ones with the proper time offsets (note: the intro doesn&#8217;t need an offset):</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">var audioOffsetted <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> WaveOffsetStream<span style="color: #000000;">&#40;</span>
   audio, TimeSpan.<span style="color: #0000FF;">FromSeconds</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">6</span><span style="color: #000000;">&#41;</span>, TimeSpan.<span style="color: #0000FF;">Zero</span>, audio.<span style="color: #0000FF;">TotalTime</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
var outroOffset <span style="color: #008000;">=</span> TimeSpan.<span style="color: #0000FF;">FromSeconds</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">6</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> audio.<span style="color: #0000FF;">TotalTime</span> <span style="color: #008000;">+</span>
   TimeSpan.<span style="color: #0000FF;">FromSeconds</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">30</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">-</span> outro.<span style="color: #0000FF;">TotalTime</span><span style="color: #008000;">;</span>
var outroOffsetted <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> WaveOffsetStream<span style="color: #000000;">&#40;</span>
   outro, outroOffset, TimeSpan.<span style="color: #0000FF;">Zero</span>, outro.<span style="color: #0000FF;">TotalTime</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>All our wavs are stored as 16bit, but the mixer expects 32bit wavs, so we convert them before adding them to the mixer:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">var intro32 <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> WaveChannel32<span style="color: #000000;">&#40;</span>intro<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
intro32.<span style="color: #0000FF;">PadWithZeroes</span> <span style="color: #008000;">=</span> false<span style="color: #008000;">;</span>
mixer.<span style="color: #0000FF;">AddInputStream</span><span style="color: #000000;">&#40;</span>intro32<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
var outro32 <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> WaveChannel32<span style="color: #000000;">&#40;</span>outroOffsetted<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
outro32.<span style="color: #0000FF;">PadWithZeroes</span> <span style="color: #008000;">=</span> false<span style="color: #008000;">;</span>
mixer.<span style="color: #0000FF;">AddInputStream</span><span style="color: #000000;">&#40;</span>outro32<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
var audio32 <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> WaveChannel32<span style="color: #000000;">&#40;</span>audioOffsetted<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
audio32.<span style="color: #0000FF;">PadWithZeroes</span> <span style="color: #008000;">=</span> false<span style="color: #008000;">;</span>
mixer.<span style="color: #0000FF;">AddInputStream</span><span style="color: #000000;">&#40;</span>audio32<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>Pad with zeros is set to false because otherwise you end up with pretty big files. When we forgot that we ended up with a 10GB wav, not sure if we stopped it or it crashed.</p>
<p>Once our mixer is set up we save that wav to a temporary location</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">WaveFileWriter.<span style="color: #0000FF;">CreateWaveFile</span><span style="color: #000000;">&#40;</span>tempwav, <span style="color: #008000;">new</span> Wave32To16Stream<span style="color: #000000;">&#40;</span>mixer<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>tempwav is the full file name of the temporary wav. Note that we go back to 16bit wavs. After that comes the ugly part of converting to MP3 which is left as an exercise for the reader.</p>
<p>We hope you find this information useful.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.hearablog.com/2010/01/mixing-audio-in-c-using-naudio/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
