Click here to Skip to main content
6,596,602 members and growing! (19,535 online)
Email Password   helpLost your password?
Multimedia » Audio and Video » Audio     Intermediate

ShowWaveForm

By pj4533

A C# class for working with .WAV files
C#, VC7, VC7.1.NET 1.0, .NET 1.1, Win2K, WinXPVS.NET2003, Dev
Posted:21 Aug 2003
Views:79,610
Bookmarked:61 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
13 votes for this article.
Popularity: 4.40 Rating: 3.95 out of 5
2 votes, 15.4%
1
1 vote, 7.7%
2
3 votes, 23.1%
3
3 votes, 23.1%
4
4 votes, 30.8%
5

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

About the Author

pj4533


Member
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.
Occupation: Web Developer
Location: United States United States

Other popular Audio and Video articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 18 of 18 (Total in Forum: 18) (Refresh)FirstPrevNext
GeneralMy 5/5 ! ReadMe ! Pinmemberf_numb3r22:44 12 Jun '07  
GeneralShow waveform Result Vs Matlab Waveform Pinmembergsrivastav1:41 7 May '07  
GeneralCreate Image Pinsussjus_10117:58 1 May '05  
GeneralRe: Create Image PinmemberChristian Graus18:12 1 May '05  
GeneralRe: Create Image Pinsussjus_10118:14 1 May '05  
GeneralRe: Create Image PinmemberChristian Graus18:17 1 May '05  
GeneralRe: Create Image Pinsussjus_10114:14 3 May '05  
GeneralRe: Create Image PinmemberChristian Graus14:21 3 May '05  
GeneralRe: Create Image Pinsussjus_10114:47 3 May '05  
GeneralRe: Create Image PinmemberMember 416178013:16 1 May '08  
GeneralDataID invalid?? Pinmembercbranje2:55 11 Sep '04  
GeneralRe: DataID invalid?? Pinmembermauricerulez2:51 8 Nov '06  
GeneralRe: DataID invalid?? Pinmembersandu20040:34 2 Jul '07  
GeneralZoom Problem Pinmemberkrishnadevank21:15 20 Jun '04  
GeneralLink/Explanation? PinmemberJeremy Kimball20:41 26 Aug '03  
GeneralRe: Link/Explanation? Pinmemberpj45338:26 27 Aug '03  
GeneralRe: Link/Explanation? Pinmemberzgraf15:42 11 Dec '05  
QuestionRe: DOUBT PinmemberFlavioAR15:14 16 Aug '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 21 Aug 2003
Editor: Chris Maunder
Copyright 2003 by pj4533
Everything else Copyright © CodeProject, 1999-2009
Web22 | Advertise on the Code Project