Click here to Skip to main content
Licence 
First Posted 21 Aug 2003
Views 98,704
Bookmarked 77 times

ShowWaveForm

By | 21 Aug 2003 | Article
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

About the Author

pj4533

Web Developer

United States United States

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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy 5/5 ! ReadMe ! Pinmemberf_numb3r21:44 12 Jun '07  
GeneralShow waveform Result Vs Matlab Waveform Pinmembergsrivastav0:41 7 May '07  
GeneralCreate Image Pinsussjus_10116:58 1 May '05  
GeneralRe: Create Image PinmemberChristian Graus17:12 1 May '05  
GeneralRe: Create Image Pinsussjus_10117:14 1 May '05  
GeneralRe: Create Image PinmemberChristian Graus17:17 1 May '05  
GeneralRe: Create Image Pinsussjus_10113:14 3 May '05  
GeneralRe: Create Image PinmemberChristian Graus13:21 3 May '05  
GeneralRe: Create Image Pinsussjus_10113:47 3 May '05  
GeneralRe: Create Image PinmemberMember 416178012:16 1 May '08  
QuestionDataID invalid?? Pinmembercbranje1:55 11 Sep '04  
AnswerRe: DataID invalid?? Pinmembermauricerulez1:51 8 Nov '06  
GeneralRe: DataID invalid?? Pinmembersandu200423:34 1 Jul '07  
GeneralZoom Problem Pinmemberkrishnadevank20:15 20 Jun '04  
QuestionLink/Explanation? PinmemberJeremy Kimball19:41 26 Aug '03  
AnswerRe: Link/Explanation? Pinmemberpj45337:26 27 Aug '03  
GeneralRe: Link/Explanation? Pinmemberzgraf14:42 11 Dec '05  
Greetings PJ,
 
Nice article.
 
(1) One thing you didn't include that the original author included
was the "FIXUP" for files that don't have the 'd','a','t','a' bytes
located in the right place. Basically, you need to keep reading
bytes until you encounter that section. I had to adapt the code to do this,
or it would blow up on some of my WAV files.
 
(2) Having issues reading 24-bit files. Do you have logic for
24 bit WAV files yet?
 
(3) You asked why drawing was so slow. I think the answer is you just need to reduce the number of data points when graphing data for a large file. Yeah, it's going to be DOG SLOW graphing MILLIONS of data points.
I simplified and rewrote your "Draw()" function like this:
 

public void Draw(Graphics g, Pen pen )
{
// Graphs the Points
// Here the Range of the Value is [-32K, 32K] -- I think (JJ)
RectangleF visBounds = g.VisibleClipBounds;

// Reduce the Number of Points to Graph, If Huge!
int Increment = 1;
while (m_Data.NumSamples / Increment > 65536)
Increment *= 2;
 
// Do the Graph
for (int k = 1; k < m_Data.NumSamples; k += Increment)
{
float XVal = (float)((double)k * visBounds.Width / m_Data.NumSamples);
float YVal = (float)((((double)(m_Data[k] + 32768)) * visBounds.Height) / 65536.0);
 
g.DrawLine(pen, XVal, visBounds.Height/2, XVal, YVal);

// JJ: Not sure about this!
if (m_Fmt.Channels == 2)
k++;
}
}
 
Notice that if the number of data points is > 64K, I just iteratively cut the number of points in half, until it gets down within range. You really don't NEED to graph several million data points anyway--you won't be able to make out the detail!
 
I didn't look at your ZOOM logic yet...
 
If you have any improvements available, please keep me in the loop.
Love this stuff.
 
- john
zgraf@yahoo.com
www.zgrafsoftware.com
 

 

QuestionRe: DOUBT PinmemberFlavioAR14:14 16 Aug '07  

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

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

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