Click here to Skip to main content
12,635,398 members (23,904 online)
Click here to Skip to main content
Add your own
alternative version

Stats

119.5K views
4.1K downloads
81 bookmarked
Posted

ShowWaveForm

, 21 Aug 2003
Rate this:
Please Sign up or sign in to vote.
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 )<BR>  {
     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

Share

About the Author

pj4533
Web Developer
United States United States
PJ currently works for Avid Technology, and lives in Cambridge, MA. When he isn't coding, you can find him collecting old dusty funk & soul records.

You may also be interested in...

Comments and Discussions

 
GeneralMy 5/5 ! ReadMe ! Pin
f_numb3r12-Jun-07 22:44
memberf_numb3r12-Jun-07 22:44 
GeneralShow waveform Result Vs Matlab Waveform Pin
gsrivastav7-May-07 1:41
membergsrivastav7-May-07 1:41 
GeneralCreate Image Pin
jus_1011-May-05 17:58
sussjus_1011-May-05 17:58 
GeneralRe: Create Image Pin
Christian Graus1-May-05 18:12
memberChristian Graus1-May-05 18:12 
GeneralRe: Create Image Pin
jus_1011-May-05 18:14
sussjus_1011-May-05 18:14 
GeneralRe: Create Image Pin
Christian Graus1-May-05 18:17
memberChristian Graus1-May-05 18:17 
GeneralRe: Create Image Pin
jus_1013-May-05 14:14
sussjus_1013-May-05 14:14 
GeneralRe: Create Image Pin
Christian Graus3-May-05 14:21
memberChristian Graus3-May-05 14:21 
GeneralRe: Create Image Pin
jus_1013-May-05 14:47
sussjus_1013-May-05 14:47 
GeneralRe: Create Image Pin
Member 41617801-May-08 13:16
memberMember 41617801-May-08 13:16 
QuestionDataID invalid?? Pin
cbranje11-Sep-04 2:55
membercbranje11-Sep-04 2:55 
AnswerRe: DataID invalid?? Pin
mauricerulez8-Nov-06 2:51
membermauricerulez8-Nov-06 2:51 
GeneralRe: DataID invalid?? Pin
sandu20042-Jul-07 0:34
membersandu20042-Jul-07 0:34 
GeneralZoom Problem Pin
krishnadevank20-Jun-04 21:15
memberkrishnadevank20-Jun-04 21:15 
QuestionLink/Explanation? Pin
Jeremy Kimball26-Aug-03 20:41
memberJeremy Kimball26-Aug-03 20:41 
AnswerRe: Link/Explanation? Pin
pj453327-Aug-03 8:26
memberpj453327-Aug-03 8:26 
GeneralRe: Link/Explanation? Pin
zgraf11-Dec-05 15:42
memberzgraf11-Dec-05 15:42 
QuestionRe: DOUBT Pin
FlavioAR16-Aug-07 15:14
memberFlavioAR16-Aug-07 15:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161208.2 | Last Updated 22 Aug 2003
Article Copyright 2003 by pj4533
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid