Click here to Skip to main content
6,636,867 members and growing! (20,771 online)
Email Password   helpLost your password?
Multimedia » Audio and Video » Audio     Intermediate

WaveControl

By pj4533

A user control for displaying .WAV files using C#
C#.NET 1.1, Win2K, WinXPVS.NET2003, Dev
Posted:22 Aug 2003
Views:70,943
Bookmarked:55 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
14 votes for this article.
Popularity: 5.04 Rating: 4.40 out of 5
1 vote, 7.1%
1
1 vote, 7.1%
2
1 vote, 7.1%
3

4
11 votes, 78.6%
5

Sample Image - wavecontrol.jpg

Introduction

WaveControl is based on the WaveFile class that I wrote in another article. This user control uses that class for parsing of the wave file, and the control itself handles all the drawing/zooming of the waveform.

Features of control

  • Zoom to region - Left click & drag to highlight a region, then right click to zoom
  • Zoom In/Out - Mouse wheel
  • Scroll waveform - Alt-Left click & drag
  • Zoom out full - Double right click

Usage

Using the control in your own applications is very simple.

  1. Copy WaveControl.cs, WaveControl.resx, WaveFile.cs to your projects directory
  2. Add the 2 .cs files to the project. I normally do this by displaying the source file and rt-clicking, choosing 'Move <filename> to project'
  3. Save All & Build (VS.NET to get confused if you don't do this)
  4. Now WaveControl should be available for addition to your forms from the 'My User Controls' toolbox.

I am in the process of learning VS.NET & C#, so this might not have been the easiest way to create a reusable control, but it does work. I look forward to other suggestions readers might have.

Performance

When I wrote the WaveFile article and first tried to display a waveform, I was getting very poor performance when drawing larger wave files. It was pointed out that this was because I was using PageScale to scale the drawing to something usable. However, this caused much more drawing than was actually necessary. So, in this version I find the max/min peaks for each X value that is being drawn, and draw a line between them, as shown here:

for ( int x = 0; x < m_SamplesPerPixel; x++ )

{

maxVal = Math.Max( maxVal, m_Wavefile.Data[ x + index ] );

minVal = Math.Min( minVal, m_Wavefile.Data[ x + index ] );

}

int scaledMinVal = (int) (( (minVal + 32768) * visBounds.Height ) / 65536 );

int scaledMaxVal = (int) (( (maxVal + 32768) * visBounds.Height ) / 65536 );

Conclusion

Things that need fixing/changing:

  • Stereo waveforms still don't work right
  • Only support 16bit waveforms
  • Loading a wave and then loading another wave sometimes causes errors.

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 15 of 15 (Total in Forum: 15) (Refresh)FirstPrevNext
General24bit Wav file support Pinmemberweedweaver2:56 1 Dec '08  
QuestionHow I can add this project to other? PinmemberYulay6:49 18 Nov '08  
QuestionChoose region Pinmembercaptainhan0:03 29 Aug '07  
QuestionHow to copy a part of sound and plaster it to other place Pinmembercaptainhan23:44 28 Aug '07  
QuestionRe: How to copy a part of sound and plaster it to other place PinmemberIbrahim Dwaikat7:12 18 Nov '07  
GeneralFile access Pinmemberamiashu7:04 30 Jul '07  
GeneralC# 2005 Pinmemberjvp2917:40 29 Jun '07  
Generaladding real time "bouncing ball" cursor Pinmemberjbarbermd16:38 5 Apr '06  
GeneralRe: adding real time "bouncing ball" cursor Pinmemberjbarbermd17:36 20 Apr '06  
GeneralWAV Control PinmemberDNA: Code this!10:52 6 Nov '04  
GeneralLeft & Right PinmemberKenneta2:57 24 Sep '04  
GeneralRe: Left & Right PinmemberL Macia11:09 2 Nov '08  
GeneraluLaw format PinsussAnonymous18:12 17 May '04  
GeneralHow to draw a waveform while recording? Pinmembercroch3:24 17 May '04  
QuestionRe: How to draw a waveform while recording? PinmemberIbrahim Dwaikat7:13 18 Nov '07  

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

PermaLink | Privacy | Terms of Use
Last Updated: 22 Aug 2003
Editor: Smitha Vijayan
Copyright 2003 by pj4533
Everything else Copyright © CodeProject, 1999-2009
Web09 | Advertise on the Code Project