title: NAudio - Get Wav Peak Data as Int16 Values
body: <p>I want to use NAudio to extract the peak values of wav audio files like the <em>audiowaveform</em> library provides. Which is an array of short values like:</p>
  <pre><code>{
    &quot;version&quot;: 2,
    &quot;channels&quot;: 2,
    &quot;sample_rate&quot;: 48000,
    &quot;samples_per_pixel&quot;: 512,
    &quot;bits&quot;: 8,
    &quot;length&quot;: 3,
    &quot;data&quot;: [-65,63,-66,64,-40,41,-39,45,-55,43,-55,44]
  }
  </code></pre>
  <p><a href="https://github.com/bbc/audiowaveform/blob/master/doc/DataFormat.md" rel="nofollow noreferrer">https://github.com/bbc/audiowaveform/blob/master/doc/DataFormat.md</a></p>
  <p>I saw that NAudio has a built-in WaveformRenderer that outputs png images, but I just need the raw peak data. Built-in NAudio classes like MaxPeakProvider (which is initialized with waveReader.ToSampleProvider()) etc works with floats, I need them as short values. Is it possible?</p>
  <p><strong>Edit after @Blindy's response</strong></p>
  <p>I have this piece of code, converting peak data to shorts, but it produces slightly wrong values when comparing with the output of audiowaveform. What might be the problem? I guess there is a data type conversion or rounding error?</p>
  <pre><code>        var waveReader = new WaveFileReader(openFileDialog1.FileName);
          var wave = new WaveChannel32(waveReader);
  
          var peakProvider = new MaxPeakProvider();
  
          int bytesPerSample = (waveReader.WaveFormat.BitsPerSample / 8);
          var samples = waveReader.Length / bytesPerSample;
  
          int pixelsPerPeak = 1;
          int spacerPixels = 0;
  
          var samplesPerPixel = 32; 
          var stepSize = pixelsPerPeak + spacerPixels;
          peakProvider.Init(waveReader.ToSampleProvider(), samplesPerPixel * stepSize);
          List&lt;short&gt; resultValuesAsShort = new List&lt;short&gt;();
          List&lt;float&gt; resultValuesAsFloat= new List&lt;float&gt;();
  
          PeakInfo currentPeak = null;
          for (int i = 0; i &lt; samples / samplesPerPixel; i++)
          {
              currentPeak = peakProvider.GetNextPeak();
  
              resultValuesAsShort.Add((short)(currentPeak.Min * short.MaxValue));
              resultValuesAsShort.Add((short)(currentPeak.Max * short.MaxValue));
  
              //resultValuesAsFloat.Add(currentPeak.Min);
              //resultValuesAsFloat.Add(currentPeak.Max);
          }
  </code></pre>
  <p>Here are the results comparison:</p>
  <p><a href="https://i.stack.imgur.com/0PSgU.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/0PSgU.png" alt="enter image description here" /></a></p>
  <p><strong>Edit2:</strong> Examining further, I noticed that it is generating quite different results for the latter values than <em>audiowaveform</em> library and I don't have any idea for the reason:</p>
  <p><a href="https://i.stack.imgur.com/3KK2b.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/3KK2b.png" alt="enter image description here" /></a></p>