-2

I have Byte[] array data from (16bit, 44100hz, 2 channels) wav file,

string WavFile = @"C:\Users\test.wav";
            WaveReader wr = new WaveReader(File.OpenRead(WavFile));
            IntPtr format = wr.ReadFormat();
            byte[] input = wr.ReadData();

i want to generate float peaks like below using

-0.00445556640625,0.010162353515625,-0.0069580078125,0.00408935546875,-0.00604248046875,0.003143310546875,-0.001953125,0.00140380859375,-0.00189208984375

here is peak generator example in linux.

https://github.com/benallfree/wavesurfer-peakbuilder

please suggest how to calculate peaks

Thank you

Nick
  • 13,238
  • 17
  • 64
  • 100

1 Answers1

2

For each bar you want to generate, the height of the bar should be proportional to the RMS (root-mean-square) value of the bytes in the time interval for that bar.

So if the track is 4:00 at 44.1kHz, that's 4*60*44.1*1e3 = 10584000 samples on each channel. If you want 100 bars, that's 10584000 / 100 = 105840 samples per channel, per bar.

You need to know the byte order in the wave file. Typically that's channel interlaved little endian. So take the 2 bytes that correspond to a sample and assemble them. (Something like (input[1] << 8) | input[0].) You should end up with two integer arrays, 10584000 samples per array. Then take the RMS formula from Wikipedia and apply it to chunks of 105840 samples to generate 100 numbers representing the heights of the bars (on each channel).

Packing format for the PCM samples is (see also http://www.neurophys.wisc.edu/auditory/riff-format.txt)

                                Sample 1

             Channel 0    Channel 0   Channel 1    Channel 1
              (left)       (left)      (right)      (right)
             low-order   high-order   low-order   high-order
               byte         byte         byte        byte


                    Data Packing for 16-Bit Stereo PCM
Nick
  • 13,238
  • 17
  • 64
  • 100
  • What will be the **time interval**? the duration of the track? could you please add any example calculation. – Mohammed Yasin Shaik Aug 03 '17 at 10:20
  • Great Explanation, in mean time i have found another link that can create group/batch of every 44100 pixels and get the max value from the batch. https://stackoverflow.com/questions/13629277/analyzing-wav-and-drawing-a-graph – Mohammed Yasin Shaik Aug 08 '17 at 11:57