title: NAudio writing sample by sample to file
body: <p>I'm building an application which records audio from the microphone to a file (.mp3). I want my application to write data to the file only when a high enough amplitude is detected. I'm having trouble trying to save the selected data. All I get is a fast distortion, not even close from what was recorded.</p>
  
  <p>This is my code:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.Windows.Forms;
  using NAudio.Wave;
  
  namespace NAudio_Testing
  {
      public partial class Form1 : Form
      {
          int counter = 0;
          int passed = 0;
          private BufferedWaveProvider bufferedWaveProvider;
          WaveIn waveIn;
          WaveFileWriter writer;
          string output = "C:/Users/klein/AppData/Roaming/Microsoft/Windows/gravacao.mp3";
          public Form1()
          {
              InitializeComponent();
              waveIn = new WaveIn();
              writer = new WaveFileWriter(output, waveIn.WaveFormat);
  
              waveIn.DataAvailable += new EventHandler&lt;WaveInEventArgs&gt;(new_dataAvailable);
  
              bufferedWaveProvider = new BufferedWaveProvider(waveIn.WaveFormat);
              bufferedWaveProvider.DiscardOnBufferOverflow = true;
              waveIn.StartRecording();
          }
  
          private void Form1_Load(object sender, EventArgs e)
          {
              timer1.Interval = 1000;
              timer1.Start();
          }
  
          void new_dataAvailable(object sender, WaveInEventArgs e)
          {
              for (int i = 0; i &lt; e.BytesRecorded; i+=2)
              {
                  short sample = (short)((e.Buffer[i + 1] &lt;&lt; 8) | e.Buffer[i + 0]);
                  float sample32 = sample / 32768f;
                  if (sample32 &gt; 0.02) //0.02~0.03
                  {
                      writer.WriteSample(sample);
                  }
              }
          }
  
          private void button1_Click(object sender, EventArgs e)
          {
              waveIn.StopRecording();
              waveIn.Dispose();
              waveIn = null;
              writer.Close();
              writer = null;
          }
  
          private void timer1_Tick(object sender, EventArgs e)
          {
              textBox1.Text = counter++.ToString();
          }
  
      }
  }
  </code></pre>
  
  <p>EDIT: I am able to record the audio successfully if I remove the selective if and just add the lines, the problem, of course is that then I'm unable to record or not according to the amplitude of the audio:</p>
  
  <pre><code>writer.WriteByte(e.Buffer[i + 0]);
  writer.WriteByte(e.Buffer[i + 1]);
  </code></pre>
  
  <p>Like so:</p>
  
  <pre><code>void new_dataAvailable(object sender, WaveInEventArgs e)
      {
          for (int i = 0; i &lt; e.BytesRecorded; i+=2)
          {
              short sample = (short)((e.Buffer[i + 1] &lt;&lt; 8) | e.Buffer[i + 0]);
              float sample32 = sample / 32768f;
  
              writer.WriteByte(e.Buffer[i + 0]);
              writer.WriteByte(e.Buffer[i + 1]);
  
          }
      }
  </code></pre>
  
  <p>Thank you.</p>