5,662,937 members and growing! (21,001 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, C++Windows, .NET, .NET 1.0, .NET 1.1, Win2K, WinXPVS.NET2003, Visual Studio, Dev

Posted: 21 Aug 2003
Updated: 21 Aug 2003
Views: 69,228
Bookmarked: 47 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
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


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
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 18 of 18 (Total in Forum: 18) (Refresh)FirstPrevNext
GeneralMy 5/5 ! ReadMe !memberf_numb3r22:44 12 Jun '07  
GeneralShow waveform Result Vs Matlab Waveformmembergsrivastav1:41 7 May '07  
GeneralCreate Imagesussjus_10117:58 1 May '05  
GeneralRe: Create ImagememberChristian Graus18:12 1 May '05  
GeneralRe: Create Imagesussjus_10118:14 1 May '05  
GeneralRe: Create ImagememberChristian Graus18:17 1 May '05  
GeneralRe: Create Imagesussjus_10114:14 3 May '05  
GeneralRe: Create ImagememberChristian Graus14:21 3 May '05  
GeneralRe: Create Imagesussjus_10114:47 3 May '05  
GeneralRe: Create ImagememberMember 416178013:16 1 May '08  
GeneralDataID invalid??membercbranje2:55 11 Sep '04  
GeneralRe: DataID invalid??membermauricerulez2:51 8 Nov '06  
GeneralRe: DataID invalid??membersandu20040:34 2 Jul '07  
GeneralZoom Problemmemberkrishnadevank21:15 20 Jun '04  
GeneralLink/Explanation?memberJeremy Kimball20:41 26 Aug '03  
GeneralRe: Link/Explanation?memberpj45338:26 27 Aug '03  
GeneralRe: Link/Explanation?memberzgraf15:42 11 Dec '05  
QuestionRe: DOUBTmemberFlavioAR15: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-2008
Web07 | Advertise on the Code Project