0

I am new on processing wav file and C#.My goal is to real time data plotting in waveform of wavfile.I mean while recording sound(wav) file,i want to plot its graph simultaneously.I searched some sound libiraries and decide to use NAudio.(Dont know it is the best choice for me.I am open to any suggestions about choosing audio libirary). However i have no idea about real time data plotting using sound. Some people suggest GDI but as i said i am new and i think it will take too much time to use GDI efficiently.If i must learn GDI,pls share any article that can help a beginner like me. Actually i look like dont know where should i start. Need to be guided :)) And i have a question.

One of the tutorial of NAudio,he works with byte array to plot the waveform in Chart.It is fine if you know the size of wav file.However it works too slow and gives Out of Memory Exception for bigger wav files than 10mb.The code below refers to what i mean.

    OpenFileDialog open = new OpenFileDialog();
    open.Filter = "Wave File (*.wav)|*.wav;";
    if (open.ShowDialog() != DialogResult.OK) return;
    chart1.Series.Add("wave");
    chart1.Series["wave"].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.FastLine;
    chart1.Series["wave"].ChartArea = "ChartArea1";
    NAudio.Wave.WaveChannel32 wave = new NAudio.Wave.WaveChannel32(new NAudio.Wave.WaveFileReader(open.FileName));

    byte[] buffer = new byte[426565];
    int read;
    while (wave.Position < wave.Length)
    {
        read = wave.Read(buffer, 0, 426565);
        for (int i = 0; i < read / 4; i++)
        {
            chart1.Series["wave"].Points.Add(BitConverter.ToSingle(buffer, i * 4));
        }
    }

Is there a way to perform this operation faster?

Paul R
  • 195,989
  • 32
  • 353
  • 519
Blast
  • 865
  • 1
  • 17
  • 39
  • As Gigasoft founder, see our [DirectX /Direct3D Charting wav data demo, example 123](http://www.gigasoft.com) The demo shows exes in WinForms, WPF, and C++/MFC pure native. Real-time updated with play position via vertical line annotation showing 12M points continuously updated with no lag while playing and dragging viewport, etc. Also shows an easy custom x axis of Minutes:Seconds. Zoom-able via mouse and mouse-wheel. Shows zoom-box of current zoomed area along with full waveform of channels. It's charting similar to Adobe Audition or Nero wave editor with just a few lines of code. – Robert Jun 13 '14 at 16:49

2 Answers2

5

If you plot every single sample, you will end up with a waveform that is unmanageably large since audio usually contains many thousands of samples per second. A common way waveforms are drawn is by selecting the maximum value over a period of time, and then drawing a vertical line to represent it. For example, if you had a three minute song, and wanted a waveform around 600 pixels wide, each pixel would represent about a third of a second. So you'd find the largest sample value in that third of a second and use that to plot your waveform.

Also, in your sample code you are reading an odd number of bytes. But since this is floating point audio, you should always read in multiples of four bytes.

Mark Heath
  • 45,254
  • 26
  • 128
  • 188
1

This worked for me

            WaveChannel32 wave = new WaveChannel32(new WaveFileReader(txtWave.Text));
            int sampleSize = 1024;
            var bufferSize = 16384 * sampleSize;
            var buffer = new byte[bufferSize];
            int read = 0;
            chart.Series.Add("wave");
            chart.Series["wave"].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.FastLine;
            chart.Series["wave"].ChartArea = "ChartArea1";
            while (wave.Position < wave.Length)
            {
                read = wave.Read(buffer, 0, bufferSize);
                for (int i = 0; i < read / sampleSize; i++)
                {
                    var point = BitConverter.ToSingle(buffer, i * sampleSize);

                    chart.Series["wave"].Points.Add(point);
                }
            }
Asif Ashraf
  • 545
  • 1
  • 6
  • 15