<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" media="screen" href="/styles/xslt/rss.xslt"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:media="http://search.yahoo.com/mrss/" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:c9="http://channel9.msdn.com">
<channel>
	<title>Channel 9</title>
    <atom:link rel="self" type="application/rss+xml" href="http://channel9.msdn.com/Niners/c4f.Mark-Heath/Posts/RSS"></atom:link>
    <itunes:summary></itunes:summary>
    <itunes:author>Microsoft</itunes:author>
    <itunes:subtitle></itunes:subtitle>
    <image>
      <url>http://mschnlnine.vo.llnwd.net/d1/Dev/App_Themes/C9/images/feedimage.png</url>
      <title>Channel 9</title>
      <link>http://channel9.msdn.com/Niners/c4f.Mark-Heath/Posts</link>
    </image>
    <itunes:image href=""></itunes:image>
    <itunes:category text="Technology"></itunes:category>
    <description>Channel 9 keeps you up to date with the latest news and behind the scenes info from Microsoft that developers love to keep up with. From LINQ to SilverLight – Watch videos and hear about all the cool technologies coming and the people behind them.</description>
    <link>http://channel9.msdn.com/Niners/c4f.Mark-Heath/Posts</link>
    <language>en</language>
    <pubDate>Wed, 19 Jun 2013 04:53:13 GMT</pubDate>
    <lastBuildDate>Wed, 19 Jun 2013 04:53:13 GMT</lastBuildDate>
    <generator>Rev9</generator>
    <c9:totalResults>1</c9:totalResults>
    <c9:pageCount>1</c9:pageCount>
    <c9:pageSize>25</c9:pageSize>
  <item>
      <title>.NET Voice Recorder</title>
      <description><![CDATA[
<p>In this article I demonstrate how to record from the microphone in .NET, with support for setting the recording level, trimming noise from the start and end, visualizing the waveform in WPF and converting to MP3.</p>

<h2>Audio Recording in .NET</h2>
<h4></h4>
<p>The .NET framework does not provide any direct support for recording audio, so I will make use of the open source
<a href="http://naudio.codeplex.com">NAudio</a> project, which includes wrappers for a number of Windows audio recording APIs.
</p>
<blockquote>
<p><b>Note: </b>It is important to point out that .NET is not an appropriate choice for high sample rate and
<i>low latency</i> audio recording, such as that found in Digital Audio Workstation software used in recording studios. This is because the .NET garbage collector can interrupt the process at any point. However, for purposes of recording speech from the microphone,
 the .NET framework is more than capable. By default, NAudio asks the soundcard to give us data every 100ms, which gives plenty of time for the garbage collector to run as well as our own code.</p>
</blockquote>
<p>We will make use of the wrappers for the waveIn API's, as these are the most universally supported, and allow us freedom to choose the sample rate. We will record in mono, 16 bit at 8kHz, which is more than good enough audio quality for speech, and will
 not overly tax the processor, which is important as we want to visualize the waveform as well.</p>
<h4>Choosing a Capture Device</h4>
<p>Normally, you will be able to use the default audio capture device without any difficulties, but should you need to offer the user a choice, NAudio will allow you to do so. You can use the
<b>WaveIn.DeviceCount</b> and <b>WaveIn.GetDeviceCapabilities</b> to find out how many recording devices are present, and query for their name and number of supported channels.</p>
<p>On my computer, I have a single waveIn device (Microphone Array) until I plug my headset in, at which point, a new device appears and becomes the default (device 0 is always the default).</p>
<pre class="csharpcode"><span class="kwrd">int</span> waveInDevices = WaveIn.DeviceCount;
<span class="kwrd">for</span> (<span class="kwrd">int</span> waveInDevice = 0; waveInDevice &lt; waveInDevices; waveInDevice&#43;&#43;)
{
    WaveInCapabilities deviceInfo = WaveIn.GetCapabilities(waveInDevice);
    Console.WriteLine(<span class="str">&quot;Device {0}: {1}, {2} channels&quot;</span>, 
        waveInDevice, deviceInfo.ProductName, deviceInfo.Channels);
}</pre>
<style type="text/css">
<!--
.csharpcode, .csharpcode 
	{font-size:small;
	color:black;
	font-family:consolas,"Courier New",courier,monospace;
	background-color:#ffffff}
.csharpcode 
	{margin:0em}
.csharpcode .rem
	{color:#008000}
.csharpcode .kwrd
	{color:#0000ff}
.csharpcode .str
	{color:#006080}
.csharpcode .op
	{color:#0000c0}
.csharpcode .preproc
	{color:#cc6633}
.csharpcode .asp
	{background-color:#ffff00}
.csharpcode .html
	{color:#800000}
.csharpcode .attr
	{color:#ff0000}
.csharpcode .alt
	{background-color:#f4f4f4;
	width:100%;
	margin:0em}
.csharpcode .lnum
	{color:#606060}
-->
</style>
<p>This produces the following output on my computer:</p>
<blockquote>
<p>Device 0: Microphone / Line In (SigmaTel , 2 channels <br>
Device 1: Microphone Array (SigmaTel High, 2 channels</p>
</blockquote>
<p>Unfortunately these device names are truncated because the WAVEINCAPS structure only supports 31 characters. There is
<a href="http://stackoverflow.com/questions/1449162/get-the-full-name-of-a-wavein-device">
a way of getting the full device name</a>, but it is rather convoluted.</p>
<p>Normally, you will choose Device 0 (the default), but if you wish to select a different input device, simply set the
<b>DeviceNumber</b> property on your WaveIn object to the desired number.</p>
<h4>Checking the Recording Level</h4>
<p>The first step in recording is usually to help the user determine if their microphone is working or not. This is especially important if the user has more than one input on their soundcard. The way we achieve this is simply by starting recording and displaying
 the level of audio detected to the user with a volume meter. The waveIn APIs do not write anything to disk, so no audio is actually being ‘recorded' at this point, we are simply examining the input level and then throwing the captured audio samples away.</p>
<p>To begin capturing audio from the soundcard, we use the <b>WaveIn</b> class in NAudio. We configure it with the WaveFormat in which we would like to record (in our case 8kHz mono), before calling
<b>StartRecording</b>, to start capturing audio from the device.</p>
<pre class="csharpcode">waveIn = <span class="kwrd">new</span> WaveIn();
waveIn.DeviceNumber = selectedDevice;
waveIn.DataAvailable &#43;= waveIn_DataAvailable;
<span class="kwrd">int</span> sampleRate = 8000; <span class="rem">// 8 kHz</span>
<span class="kwrd">int</span> channels = 1; <span class="rem">// mono</span>
waveIn.WaveFormat = <span class="kwrd">new</span> WaveFormat(sampleRate, channels);
waveIn.StartRecording();</pre>
<p>The <b>DataAvailable</b> event handler will notify us whenever a buffer of audio has been returned to us from the sound card. The data comes back as an array of bytes, representing PCM sample data. This is fine if we are planning to write the audio directly
 to disk, but what if we wish to have a look at the audio data itself? Each audio sample is 16 bits, i.e. two bytes, meaning that we will need to convert pairs of bytes into shorts to be able to make sense of the data.</p>
<blockquote>
<p><b>Note:</b> if we were recording in stereo, the 16 bit samples would themselves come in pairs, first the left sample, then the right sample.</p>
</blockquote>
<p>The following code shows how we might process the raw bytes in the <b>DataAvailable</b> event, and read the individual audio samples out. Notice that we use the
<b>BytesRecorded</b> field, not the buffer's Length property. Also, I have chosen to convert the samples to 32 bit floating point format and scaled them so the maximum volume is 1.0f. This makes processing them through effects and visualizing them much easier.</p>
<pre class="csharpcode"><span class="kwrd">void</span> waveIn_DataAvailable(<span class="kwrd">object</span> sender, WaveInEventArgs e)
{
    <span class="kwrd">for</span> (<span class="kwrd">int</span> index = 0; index &lt; e.BytesRecorded; index &#43;= 2)
    {
        <span class="kwrd">short</span> sample = (<span class="kwrd">short</span>)((e.Buffer[index &#43; 1] &lt;&lt; 8) | 
                                e.Buffer[index &#43; 0]);
        <span class="kwrd">float</span> sample32 = sample / 32768f;
        ProcessSample(sample32);
    }
}</pre>
<p></p>
<blockquote>
<p><b>Note: </b>One complication of using the waveIn and waveOut APIs is deciding on a callback mechanism. NAudio offers three options. First is
<b>function</b> callbacks. This means that the waveIn API is given a (pinned) function pointer which it calls back onto. This means that your DataAvailable callback will come in on a background thread. In some ways this is the cleanest approach, but you need
 to beware of rogue soundcard drivers that can hang in calls to waveOutReset when using function callbacks (the SoundMAX chipset found on a lot of laptops is particularly prone to this problem).</p>
<p>The second is to supply a <b>window handle</b>. The waveIn APIs will post a message back to be handled on the message queue of that window handle. This method tends to be the most reliable and most commonly used. One gotcha to watch out for is that if you
 stop recording and <i>immediately</i> restart, a message from the old recording session could get handled in the new session resulting in a nasty exception.
</p>
<p>The third is to let NAudio create its own <b>new window</b> and post messages to that. This gets round any danger of messages from one recording session getting muddled up with another. This is the callback method that NAudio will use by default if you call
 the default <b>WaveIn</b> constructor. But don't use this from a background thread or from a console application, or the new window that NAudio creates won't actually get round to processing its message queue.</p>
</blockquote>
<h4>Visualizing the Recording Level</h4>
<p>We have seen how we can begin to capture audio from the soundcard for the purposes of checking the recording level. Now we need to give the user some visual feedback. We will use WPF for our sample recording application. The simplest control we have available
 to display a single numeric value graphically is the <b>ProgressBar</b>. And because it is WPF, we can fully customize the graphical appearance of the progress bar to look a little more like a volume meter. I have used a gradient going from green to red to
 show the current volume level. You can read more about how I created this ProgressBar template
<a href="http://mark-dot-net.blogspot.com/2009/09/styling-wpf-volume-meter.html">
here</a>. </p>
<p><a href="http://ecn.channel9.msdn.com/o9/c4fcontent/migration/9905168/image_12.png"><img title="image" border="0" alt="image" src="http://ecn.channel9.msdn.com/o9/c4fcontent/migration/9905168/image_thumb_5.png" width="296" height="37"></a>
<br>
<strong>Figure 1 - A Progress Bar Showing the Current Microphone Volume Level</strong></p>
<p>To help provide the volume level to display, I have created a <b>SampleAggregator</b> class. This is passed every audio sample value we receive and keeps track of the maximum and minimum values. Then, after a specified number of samples, it raises an event
 allowing the GUI components to respond. We need to be careful not to raise too many of these events or performance will be badly affected. I am raising one every 800 samples, meaning we get 10 updates per second to the screen.</p>
<p>Because I am using data binding, when one of these updates fires, I must raise a
<b>PropertyChangedEvent</b> on my DataContext object (also known as the “ViewModel” in the MVVM pattern). Here's the XAML syntax for binding to my
<b>CurrentInputLevel</b> property:</p>
<pre class="csharpcode"><span class="kwrd">&lt;</span><span class="html">ProgressBar</span> <span class="attr">Orientation</span><span class="kwrd">=&quot;Horizontal&quot;</span> 
    <span class="attr">Value</span><span class="kwrd">=&quot;{Binding CurrentInputLevel, Mode=OneWay}&quot;</span> 
    <span class="attr">Height</span><span class="kwrd">=&quot;20&quot;</span> <span class="kwrd">/&gt;</span></pre>
<p>And here's the code in the ViewModel that ensures that the GUI updates whenever we calculate a new maximum input level:</p>
<pre class="csharpcode"><span class="kwrd">private</span> <span class="kwrd">float</span> lastPeak;

<span class="kwrd">void</span> recorder_MaximumCalculated(<span class="kwrd">object</span> sender, MaxSampleEventArgs e) 
{
    lastPeak = Math.Max(e.MaxSample, Math.Abs(e.MinSample));
    RaisePropertyChangedEvent(<span class="str">&quot;CurrentInputLevel&quot;</span>);
}

<span class="rem">// multiply by 100 because the Progress bar's default maximum value is 100 </span>
<span class="kwrd">public</span> <span class="kwrd">float</span> CurrentInputLevel { get { <span class="kwrd">return</span> lastPeak * 100; } }</pre>
<p></p>
<blockquote>
<p><b>Note: </b>Model View ViewModel (MVVM) is a pattern that is growing in popularity amongst WPF and Silverlight developers. The basic idea is that you have no code behind whatsoever on your View (i.e. your xaml markup file), and simply specify all communications
 with your business logic by means of data binding. The ViewModel serves as an adapter to ease the process of data binding. This approach gives very good separation of appearance and behavior. For the most part, this pattern works very well, but there are a
 few tricky areas, for which you will need to either write a few lines of code behind, or make use of some cunning tricks such as attached dependency properties or custom triggers. There are several excellent open source helper libraries that can take some
 of the work out of getting an MVVM application up and running. Have a look <a href="http://stackoverflow.com/questions/1409553/what-framework-for-mvvm-should-i-use">
here</a> for a comprehensive list.</p>
</blockquote>
<h4>Adjusting the Recording Level</h4>
<p>Suppose the current input level is too high or too soft. We would like to be able to support modifying the recording level. Again, we would like to use data binding to do so, so we will add a volume slider to our XAML:</p>
<pre class="csharpcode"><span class="kwrd">&lt;</span><span class="html">Slider</span> <span class="attr">Orientation</span><span class="kwrd">=&quot;Horizontal&quot;</span> 
    <span class="attr">Value</span><span class="kwrd">=&quot;{Binding MicrophoneLevel, Mode=TwoWay}&quot;</span> 
    <span class="attr">Maximum</span><span class="kwrd">=&quot;100&quot;</span> 
    <span class="attr">Margin</span><span class="kwrd">=&quot;5&quot;</span> <span class="kwrd">/&gt;</span></pre>
<p>Now we have to get hold of the <b>MixerLine</b> that will allow us to access the input volume control for our waveIn device. This requires us to make use of the Windows mixer APIs, which also have wrappers in NAudio. Getting hold of this volume control is
 not always as straightforward as you might hope (and can require different approaches for XP and Vista), but the following is code that seems to work on most systems:</p>
<pre class="csharpcode"><span class="kwrd">private</span> <span class="kwrd">void</span> TryGetVolumeControl()
{
    <span class="kwrd">int</span> waveInDeviceNumber = 0;
    var mixerLine = <span class="kwrd">new</span> MixerLine((IntPtr)waveInDeviceNumber, 
                                   0, MixerFlags.WaveIn);
    <span class="kwrd">foreach</span> (var control <span class="kwrd">in</span> mixerLine.Controls)
    {
        <span class="kwrd">if</span> (control.ControlType == MixerControlType.Volume)
        {
            volumeControl = control <span class="kwrd">as</span> UnsignedMixerControl;        
            <span class="kwrd">break</span>;
        }
    }
}</pre>
<p></p>
<p>Now we can use the <b>Percent</b> property on the UnsignedMixerControl to set volume to a value anywhere between 0 and 100.</p>
<h4>Starting Recording</h4>
<p>Now we have got our recording levels set up correctly, we are ready to actually start recording. But since we have already opened our waveIn device, all we need to do is start writing the data we have received into a file.
</p>
<p>NAudio has a class called <b>WaveFileWriter </b>which will allow us to write our recorded data to a file. For now, we will write it to a temporary file in PCM format, and convert it later into a better compressed format such as MP3. The following code creates
 a new WAV file:</p>
<pre class="csharpcode">writer = <span class="kwrd">new</span> WaveFileWriter(waveFileName, recordingFormat);</pre>
<p>Now we can write to the file as we receive notifications from the waveIn device:</p>
<pre class="csharpcode"><span class="kwrd">void</span> waveIn_DataAvailable(<span class="kwrd">object</span> sender, WaveInEventArgs e)
{
    <span class="kwrd">if</span> (recordingState == RecordingState.Recording)
        writer.WriteData(e.Buffer, 0, e.BytesRecorded);            

   <span class="rem">// ...</span>
}</pre>
<style type="text/css">
<!--
.csharpcode, .csharpcode 
	{font-size:small;
	color:black;
	font-family:consolas,"Courier New",courier,monospace;
	background-color:#ffffff}
.csharpcode 
	{margin:0em}
.csharpcode .rem
	{color:#008000}
.csharpcode .kwrd
	{color:#0000ff}
.csharpcode .str
	{color:#006080}
.csharpcode .op
	{color:#0000c0}
.csharpcode .preproc
	{color:#cc6633}
.csharpcode .asp
	{background-color:#ffff00}
.csharpcode .html
	{color:#800000}
.csharpcode .attr
	{color:#ff0000}
.csharpcode .alt
	{background-color:#f4f4f4;
	width:100%;
	margin:0em}
.csharpcode .lnum
	{color:#606060}
-->
</style>
<blockquote>
<p><b>Note:</b> There are three main options for how to store audio while it is being recorded. First, you can write it to a
<b>MemoryStream</b>. This saves the inconvenience of dealing with a temporary file, but you need to be careful not to run out of memory. Also, if your recording program crashes half way through, you have lost everything. At the sample rate we are using for
 this demo, one minute of audio takes just under 1 MB of memory, but if you were recording at 44.1kHz stereo (the standard for music), you would need about 10 MB per minute.</p>
<p>Second, you can write to a temporary WAV file to be converted to another format later, as we are doing here. While this is not a disk space efficient format, it is very easy to work with, and particularly useful if you are planning to apply any effects or
 edit the audio in any way after recording. </p>
<p>Third, you can pass the audio directly to an encoder (such as WMA or MP3) as it is being recorded. This might be the best option if you are making a longer recording, and have no need to edit it after recording.</p>
</blockquote>
<h4>Stopping Recording</h4>
<p>Obviously we will stop when the user clicks the stop recording button, but we might also want to set a maximum recording duration to stop the user inadvertently filling up their hard disk. For this example, we will allow one minute of recording.</p>
<pre class="csharpcode"><span class="kwrd">long</span> maxFileLength = <span class="kwrd">this</span>.recordingFormat.AverageBytesPerSecond * 60;
 
<span class="kwrd">int</span> toWrite = (<span class="kwrd">int</span>)Math.Min(maxFileLength - writer.Length, bytesRecorded);
<span class="kwrd">if</span> (toWrite &gt; 0)
    writer.WriteData(buffer, 0, bytesRecorded);
<span class="kwrd">else</span>
    Stop();</pre>
<p><b></b></p>
<blockquote>
<p><b>Note: </b>Something that can be slightly confusing for users is that when using window callbacks with WaveIn, the last bit of audio you recorded comes in
<i>after</i> you have asked recording to stop, so make sure you don't close the file you are saving to until you have got all the audio back. The
<b>FinishedRecording</b> event on the WaveIn object will help you determine when it is safe to close the WaveFileWriter and clean up your resources.</p>
</blockquote>
<h4>Visualizing the Wave Form</h4>
<p>It is often desirable to display the audio waveform to the user. Displaying the waveform while you are recording is sometimes called “confidence recording”, because it allows you to see that audio is being recorded as expected and the levels are still right.</p>
<p>There are a variety of possible approaches for drawing audio waveforms. The simplest is to draw a vertical line showing the minimum and maximum values every time our sample aggregator fires:</p>
<p><a href="http://ecn.channel9.msdn.com/o9/c4fcontent/migration/9905168/image_10.png"><img title="image" border="0" alt="image" src="http://ecn.channel9.msdn.com/o9/c4fcontent/migration/9905168/image_thumb_4.png" width="190" height="76"></a>
<br>
<strong>Figure 2 - Audio Waveform using vertical lines </strong></p>
<p>At first glance it may seem that this would be trivial to implement in WPF, but there is a real danger of consuming too many resources. For example, simply adding a new line to a Canvas every time a new maximum sample is calculated performs very badly, so
 it is better to have a fixed number of vertical lines and resize them dynamically.</p>
<p>Another approach is to create a polygon. This requires us to add two points to a Polygon's Points collection every time we receive a new sample. The trick is to add these points in the middle of the Points collection, rather than at the end, so that the
 end result is a single shape. This means our waveform can have a different outline color and fill color. To stop the edges from appearing too jagged, we plot points two units apart along on the X axis.</p>
<p><a href="http://ecn.channel9.msdn.com/o9/c4fcontent/migration/9905168/image_8.png"><img title="image" border="0" alt="image" src="http://ecn.channel9.msdn.com/o9/c4fcontent/migration/9905168/image_thumb_3.png" width="383" height="105"></a><strong>
<br>
Figure 3 - Audio Waveform rendered using a Polygon</strong></p>
<p>Like the microphone volume meter, the waveform drawing control needs to receive several notifications a second of the maximum and minimum sample values received by the SampleAggregator. When each sample value is received, we either insert new points into
 our polygon, or, if the whole screen is full, we go back to the left-hand edge and continue drawing from there.</p>
<p>For the confidence recording display I have used the Polygon method, which is in a class called
<b>PolygonWaveFormControl</b>. Here's the code which calculates the new points or updated point locations as we receive a new maximum sample:</p>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">void</span> AddValue(<span class="kwrd">float</span> maxValue, <span class="kwrd">float</span> minValue)
{
    <span class="kwrd">int</span> visiblePixels = (<span class="kwrd">int</span>)(ActualWidth / xScale);
    <span class="kwrd">if</span> (visiblePixels &gt; 0)
    {
        CreatePoint(maxValue, minValue);

        <span class="kwrd">if</span> (renderPosition &gt; visiblePixels)
        {
            renderPosition = 0;
        }
        <span class="kwrd">int</span> erasePosition = (renderPosition &#43; blankZone) % visiblePixels;
        <span class="kwrd">if</span> (erasePosition &lt; Points)
        {
            <span class="kwrd">double</span> yPos = SampleToYPosition(0);
            waveForm.Points[erasePosition] = 
               <span class="kwrd">new</span> Point(erasePosition * xScale, yPos);
            waveForm.Points[BottomPointIndex(erasePosition)] = 
               <span class="kwrd">new</span> Point(erasePosition * xScale, yPos);
        }
    }
}

<span class="kwrd">private</span> <span class="kwrd">void</span> CreatePoint(<span class="kwrd">float</span> topValue, <span class="kwrd">float</span> bottomValue)
{
    <span class="kwrd">double</span> topYPos = SampleToYPosition(topValue);
    <span class="kwrd">double</span> bottomYPos = SampleToYPosition(bottomValue);
    <span class="kwrd">double</span> xPos = renderPosition * xScale;
    <span class="kwrd">if</span> (renderPosition &gt;= Points)
    {
        <span class="kwrd">int</span> insertPos = Points;
        waveForm.Points.Insert(insertPos, <span class="kwrd">new</span> Point(xPos, topYPos));
        waveForm.Points.Insert(insertPos &#43; 1, <span class="kwrd">new</span> Point(xPos, bottomYPos));
    }
    <span class="kwrd">else</span>
    {
        waveForm.Points[renderPosition] = <span class="kwrd">new</span> Point(xPos, topYPos);
        waveForm.Points[BottomPointIndex(renderPosition)] = 
              <span class="kwrd">new</span> Point(xPos, bottomYPos);
    }
    renderPosition&#43;&#43;;
}</pre>
<p>The erase position calculation is to blank out some previous sample values to make it obvious where the new data is appearing after we have wrapped around once:</p>
<p><a href="http://ecn.channel9.msdn.com/o9/c4fcontent/migration/9905168/image_14.png"><img title="image" border="0" alt="image" src="http://ecn.channel9.msdn.com/o9/c4fcontent/migration/9905168/image_thumb_6.png" width="291" height="53"></a>
<br>
<strong>Figure 4 PolygonWaveForm control's “blank zone”</strong></p>
<blockquote>
<p><b>Note:</b> There are faster ways to perform rendering in WPF. One option is to use the
<b>WriteableBitmap</b> class and draw directly onto it. This could be a good approach if you were using the vertical lines method of rendering. The second is to use
<b>DrawingVisual</b> objects, which are lightweight drawing objects offering better performance than using classes derived from Shape. The down-side is the loss of features such as DataBinding and the ability to fully describe the picture in XAML, but for WaveForm
 drawing this is not really a drawback. I use the DrawingVisual method in the Save Audio part of this application.</p>
</blockquote>
<p>Another challenge was how the waveform drawing control could receive notifications since I am using MVVM so I have no direct access to the SampleAggregator. A simple way around this was to create a Dependency Property on PolygonWaveFormControl:</p>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">readonly</span> DependencyProperty SampleAggregatorProperty = 
       DependencyProperty.Register(
          <span class="str">&quot;SampleAggregator&quot;</span>, 
          <span class="kwrd">typeof</span>(SampleAggregator), 
          <span class="kwrd">typeof</span>(PolygonWaveFormControl), 
          <span class="kwrd">new</span> PropertyMetadata(<span class="kwrd">null</span>, OnSampleAggregatorChanged));

<span class="kwrd">public</span> SampleAggregator SampleAggregator
{
    get { <span class="kwrd">return</span> (SampleAggregator)<span class="kwrd">this</span>.GetValue(SampleAggregatorProperty); }
    set { <span class="kwrd">this</span>.SetValue(SampleAggregatorProperty, <span class="kwrd">value</span>); }
}
        
<span class="kwrd">private</span> <span class="kwrd">static</span> <span class="kwrd">void</span> OnSampleAggregatorChanged(<span class="kwrd">object</span> sender, DependencyPropertyChangedEventArgs e)
{
    PolygonWaveFormControl control = (PolygonWaveFormControl)sender;
    control.Subscribe();
}</pre>
<p></p>
<p>This allows us to bind the PolygonWaveFormControl to the SampleAggregator made public on our DataContext:</p>
<pre class="csharpcode"><span class="kwrd">&lt;</span><span class="html">my:PolygonWaveFormControl</span> 
    <span class="attr">Height</span><span class="kwrd">=&quot;40&quot;</span> 
    <span class="attr">SampleAggregator</span><span class="kwrd">=&quot;{Binding SampleAggregator}&quot;</span> <span class="kwrd">/&gt;</span></pre>
<h4>Trimming the Audio</h4>
<p>We have created a temporary WAV file, but before the user saves it to a file of their choosing, we want to allow them to trim off any unwanted parts from the start and end of the recording. To do this I would like to display the entire recorded waveform,
 with a selection rectangle superimposed on top to allow a sub-range to be selected.</p>
<p><a href="http://ecn.channel9.msdn.com/o9/c4fcontent/migration/9905168/image_16.png"><img title="image" border="0" alt="image" src="http://ecn.channel9.msdn.com/o9/c4fcontent/migration/9905168/image_thumb_7.png" width="289" height="159"></a>
<br>
<strong>Figure 5 - GUI to allow selection of a portion of the recorded audio</strong></p>
<p>To accomplish this kind of interface we need three components. The first is a <b>
ScrollViewer</b>. The ScrollViewer allows us to scroll left and right through the WaveForm if it is too big to fit onto a screen, which is likely if you record more than a few seconds of audio.
</p>
<p>The second is a new type of WaveForm renderer that will render an entire file, rather than my PolygonWaveFormControl which started again at the left when the screen filled up. For this I created
<b>WaveFormVisual</b> which uses DrawingVisual objects to draw the entire WaveForm. Obviously if we wanted to record for a long period, this approach would need to be optimised as the polygon it creates would have thousands of points, but for short recordings,
 it works fine.</p>
<p>The third piece was the hardest to get right – the selection rectangle to support mouse dragging selection of the waveform. For this I created the
<b>RangeSelectionControl</b>.</p>
<p>The <b>RangeSelectionControl</b> is simply a blue rectangle with a solid outline and semi-transparent fill sitting on a Canvas. The magic occurs in the mouse handler. We need to detect when the user hovers over the left or right edge of the rectangle, and
 set the cursor to show a horizontal resizing icon. This can be done in the MouseMove event, checking the X coordinate and then setting the Cursor property:</p>
<pre class="csharpcode">Cursor = Cursors.SizeWE;</pre>
<p>When the user clicks the left-button while over the edge, we begin to drag. Key to this is calling
<b>Canvas.CaptureMouse</b>. If we don't do this, as soon as you try to drag the rectangle bigger, the mouse move events are lost to other controls underneath.</p>
<pre class="csharpcode"><span class="kwrd">void</span> RangeSelectionControl_MouseDown(<span class="kwrd">object</span> sender, MouseButtonEventArgs e)
{
    <span class="kwrd">if</span> (e.LeftButton == MouseButtonState.Pressed)
    {
        Point position = e.GetPosition(<span class="kwrd">this</span>);
        Edge edge = EdgeAtPosition(position.X);
        DragEdge = edge;
        <span class="kwrd">if</span> (DragEdge != Edge.None)
        {
            mainCanvas.CaptureMouse();
        }
    }
}</pre>
<p></p>
<p>Now in the MouseMove methods, we can change the Canvas.Left and Width properties of the rectangle to resize it.</p>
<p>The <b>ScrollViewer</b> is quite straightforward to use, but you must remember to set CanContentScroll property to true, and also to set the size of the items within the ScrollViewer correctly.</p>
<pre class="csharpcode"><span class="kwrd">&lt;</span><span class="html">ScrollViewer</span> <span class="attr">CanContentScroll</span><span class="kwrd">=&quot;True&quot;</span> 
         <span class="attr">HorizontalScrollBarVisibility</span><span class="kwrd">=&quot;Visible&quot;</span> 
         <span class="attr">VerticalScrollBarVisibility</span><span class="kwrd">=&quot;Hidden&quot;</span><span class="kwrd">&gt;</span>
   <span class="kwrd">&lt;</span><span class="html">Grid</span><span class="kwrd">&gt;</span>
       <span class="kwrd">&lt;</span><span class="html">my:WaveFormVisual</span> <span class="attr">Height</span><span class="kwrd">=&quot;100&quot;</span> 
           <span class="attr">HorizontalAlignment</span><span class="kwrd">=&quot;Left&quot;</span> 
           <span class="attr">x:Name</span><span class="kwrd">=&quot;waveFormRenderer&quot;</span><span class="kwrd">/&gt;</span>
       <span class="kwrd">&lt;</span><span class="html">my:RangeSelectionControl</span> 
           <span class="attr">HorizontalAlignment</span><span class="kwrd">=&quot;Left&quot;</span> 
           <span class="attr">x:Name</span><span class="kwrd">=&quot;rangeSelection&quot;</span> <span class="kwrd">/&gt;</span>
   <span class="kwrd">&lt;/</span><span class="html">Grid</span><span class="kwrd">&gt;</span>
<span class="kwrd">&lt;/</span><span class="html">ScrollViewer</span><span class="kwrd">&gt;</span></pre>
<p>We set the appropriate Width of the WaveFormVisual and RangeSelectionControl based on the total number of points we have drawn in the waveform.
</p>
<h4>Saving the Audio</h4>
<p>So we are finally ready to save the audio. We will offer the user two choices of format to save in. The first is simply to save as a WAV file. If the user has selected the entire recording, we only need to copy the audio across to their desired location.
 If, however, the user has selected a sub-range, then we need to trim the WAV file. This can be quickly accomplished using a
<b>TrimWavFile</b> utility function that copies from a WAV file reader to a WAV file writer, skipping over a certain number of bytes from the beginning and end.</p>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">void</span> TrimWavFile(<span class="kwrd">string</span> inPath, <span class="kwrd">string</span> outPath, 
                TimeSpan cutFromStart, TimeSpan cutFromEnd)
{
    <span class="kwrd">using</span> (WaveFileReader reader = <span class="kwrd">new</span> WaveFileReader(inPath))
    {
        <span class="kwrd">using</span> (WaveFileWriter writer = 
               <span class="kwrd">new</span> WaveFileWriter(outPath, reader.WaveFormat))
        {
            <span class="kwrd">int</span> bytesPerMillisecond = 
                reader.WaveFormat.AverageBytesPerSecond / 1000;

            <span class="kwrd">int</span> startPos = (<span class="kwrd">int</span>)cutFromStart.TotalMilliseconds * 
                           bytesPerMillisecond;
            startPos = startPos - startPos % reader.WaveFormat.BlockAlign;

            <span class="kwrd">int</span> endBytes = (<span class="kwrd">int</span>)cutFromEnd.TotalMilliseconds * 
                           bytesPerMillisecond;
            endBytes = endBytes - endBytes % reader.WaveFormat.BlockAlign;
            <span class="kwrd">int</span> endPos = (<span class="kwrd">int</span>)reader.Length - endBytes; 

            TrimWavFile(reader, writer, startPos, endPos);
        }
    }
}

<span class="kwrd">private</span> <span class="kwrd">static</span> <span class="kwrd">void</span> TrimWavFile(WaveFileReader reader, 
                    WaveFileWriter writer, <span class="kwrd">int</span> startPos, <span class="kwrd">int</span> endPos)
{
    reader.Position = startPos;
    <span class="kwrd">byte</span>[] buffer = <span class="kwrd">new</span> <span class="kwrd">byte</span>[1024];
    <span class="kwrd">while</span> (reader.Position &lt; endPos)
    {
        <span class="kwrd">int</span> bytesRequired = (<span class="kwrd">int</span>)(endPos - reader.Position);
        <span class="kwrd">if</span> (bytesRequired &gt; 0)
        {
            <span class="kwrd">int</span> bytesToRead = Math.Min(bytesRequired, buffer.Length);
            <span class="kwrd">int</span> bytesRead = reader.Read(buffer, 0, bytesToRead);
            <span class="kwrd">if</span> (bytesRead &gt; 0)
            {
                writer.WriteData(buffer, 0, bytesRead);
            }
        }
    }
}</pre>
<p>We also want to offer the ability to save as MP3. The easiest way to create MP3 files is to use the open source LAME MP3 encoder (do a web search for lame.exe to get hold of this application if you haven't already got it). Our application will look in the
 current directory, and prompt the user to find lame.exe if it is not present, as we do not include it in the application download. Assuming you do provide a valid path, we can then convert our (trimmed) WAV file to MP3 by simply calling lame.exe with the appropriate
 parameters.</p>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">void</span> ConvertToMp3(<span class="kwrd">string</span> lameExePath, 
     <span class="kwrd">string</span> waveFile, <span class="kwrd">string</span> mp3File)
{
   Process converter = Process.Start(lameExePath, <span class="str">&quot;-V2 \&quot;&quot;</span> &#43; waveFile 
                            &#43; <span class="str">&quot;\&quot; \&quot;&quot;</span> &#43; mp3File &#43; <span class="str">&quot;\&quot;&quot;</span>);
   converter.WaitForExit();
}</pre>
<p></p>
<p>We end up with a nice compact MP3 file containing the selected portion of our microphone recording.</p>
<h4>Exploring the Sample Code Solution</h4>
<p>The main WPF sample application is found in the <b>VoiceRecorder</b> project. This contains the main window along with the three views and their associated ViewModels.
<b>VoiceRecorder.Core</b> contains some WPF helper classes and user controls to help with the plumbing and GUI of the application, while
<b>VoiceRecorder.Audio</b> contains the classes that actually perform the recording, editing and converting of audio.</p>
<h4>About the Author</h4>
<p>Mark Heath is a software developer currently working for NICE CTI Systems in Southampton, UK. He specializes in .NET development with a particular focus on client side technologies and audio playback. He blogs about audio, WPF, Silverlight and software engineering
 best practices at <a href="http://mark-dot-net.blogspot.com">http://mark-dot-net.blogspot.com</a>. He is the author of several open source projects hosted at CodePlex, including NAudio, a low-level .NET audio toolkit (<a href="http://www.codeplex.com/naudio">http://www.codeplex.com/naudio</a>).</p>
 <img src="http://m.webtrends.com/dcs1wotjh10000w0irc493s0e_6x1g/njs.gif?dcssip=channel9.msdn.com&dcsuri=http://channel9.msdn.com/Niners/c4f.Mark-Heath/Posts/RSS&WT.dl=0&WT.entryid=Entry:RSSView:aa6148d11dc848f181989e7600cb3bf6">]]></description>
      <comments>http://channel9.msdn.com/coding4fun/articles/NET-Voice-Recorder</comments>
      <itunes:summary>
In this article I demonstrate how to record from the microphone in .NET, with support for setting the recording level, trimming noise from the start and end, visualizing the waveform in WPF and converting to MP3. 

Audio Recording in .NET

The .NET framework does not provide any direct support for recording audio, so I will make use of the open source
NAudio project, which includes wrappers for a number of Windows audio recording APIs.
 

Note: It is important to point out that .NET is not an appropriate choice for high sample rate and
low latency audio recording, such as that found in Digital Audio Workstation software used in recording studios. This is because the .NET garbage collector can interrupt the process at any point. However, for purposes of recording speech from the microphone,
 the .NET framework is more than capable. By default, NAudio asks the soundcard to give us data every 100ms, which gives plenty of time for the garbage collector to run as well as our own code. 

We will make use of the wrappers for the waveIn API&#39;s, as these are the most universally supported, and allow us freedom to choose the sample rate. We will record in mono, 16 bit at 8kHz, which is more than good enough audio quality for speech, and will
 not overly tax the processor, which is important as we want to visualize the waveform as well. 
Choosing a Capture Device
Normally, you will be able to use the default audio capture device without any difficulties, but should you need to offer the user a choice, NAudio will allow you to do so. You can use the
WaveIn.DeviceCount and WaveIn.GetDeviceCapabilities to find out how many recording devices are present, and query for their name and number of supported channels. 
On my computer, I have a single waveIn device (Microphone Array) until I plug my headset in, at which point, a new device appears and becomes the default (device 0 is always the default). 
int waveInDevices = WaveIn.DeviceCount;
for (int waveInDevice = 0; waveInDevice &amp;lt</itunes:summary>
      <link>http://channel9.msdn.com/coding4fun/articles/NET-Voice-Recorder</link>
      <pubDate>Thu, 08 Oct 2009 18:29:39 GMT</pubDate>
      <guid isPermaLink="false">http://channel9.msdn.com/coding4fun/articles/NET-Voice-Recorder</guid>
      <media:thumbnail url="http://ecn.channel9.msdn.com/o9/c4f/images/9905168_100.jpg" height="75" width="100"></media:thumbnail>
      <media:thumbnail url="http://ecn.channel9.msdn.com/o9/c4f/images/9905168_220.jpg" height="165" width="220"></media:thumbnail>      
      <dc:creator>Mark Heath </dc:creator>
      <itunes:author>Mark Heath </itunes:author>
      <slash:comments>18</slash:comments>
      <wfw:commentRss>http://channel9.msdn.com/coding4fun/articles/NET-Voice-Recorder/RSS</wfw:commentRss>
      <category>Audio</category>
      <category>Model-View-ViewModel</category>
      <category>MVVM</category>
      <category>WPF</category>
    </item>    
</channel>
</rss>