Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

ShowWaveForm

0.00/5 (No votes)
21 Aug 2003 2  
A C# class for working with .WAV files

Sample Image - showwaveform.jpg

Introduction

ShowWaveForm is based off of a c++ class written by Alexander Beletsky called CWaveFile. This is a port to C#, with some added features. A small .wav file is included in the demo project .zip file for testing.

WaveFile.cs

WaveFile.cs is the main class. It reads & parses the .WAV file headers. When passed a PaintEventArgs (typically from an OnPaint event), it will draw the waveform. The class also allows for zooming in/out on the waveform.

Usage

Using the class is very simple. Here is the sample project file open menu handler:

private void fileOpen_Click(object sender, System.EventArgs e)
{
  OpenFileDialog fileDlg = new OpenFileDialog();

  if ( fileDlg.ShowDialog() == DialogResult.OK )
  {
     wave = new WaveFile( fileDlg.FileName );
     sbpMainPanel.Text = "Reading .WAV file...";
     wave.Read( );
     sbpMainPanel.Text = "Finished Reading .WAV file...";
     m_DrawWave = true;
     Refresh( );
  }
}

And here is the Paint handler of the demo project:

private void Form1_Paint( object sender, 
              System.Windows.Forms.PaintEventArgs e
            )
{
  Pen pen = new Pen( ForeColor );
  if ( m_DrawWave )
{ sbpMainPanel.Text = "Drawing .WAV file..."; wave.Draw( e, pen ); sbpMainPanel.Text = "Finished drawing .WAV file..."; } }

The last interesting thing in the demo project is the MouseWheel handler, which controls the zooming of the waveform:

protected override void OnMouseWheel( MouseEventArgs mea )
{
 if ( mea.Delta * SystemInformation.MouseWheelScrollLines / 120 > 0 )
  wave.ZoomIn( );
 else
  wave.ZoomOut( );

 Refresh( );

}

Questions/Discussion

This is my first attempt at programming in C#/.NET. So please comment on every thing in this code! Specifically I have questions about:

  • Drawing of waveform is very slow for large files, perhaps because of using PageScale?
  • How to catch some of the errors that can occur in zooming in/out too far
  • Drawing of stereo waveforms

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here