Click here to Skip to main content
12,633,519 members (31,387 online)
Click here to Skip to main content
Add your own
alternative version

Stats

126.6K views
8.7K downloads
76 bookmarked
Posted

WaveControl

, 22 Aug 2003
Rate this:
Please Sign up or sign in to vote.
A user control for displaying .WAV files using C#

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

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

 
QuestionCan it used with video ?? Pin
Member 456277620-Jan-15 17:13
memberMember 456277620-Jan-15 17:13 
GeneralMy vote of 5 Pin
alaro4-Jun-11 10:13
memberalaro4-Jun-11 10:13 
General24bit Wav file support Pin
weedweaver1-Dec-08 2:56
memberweedweaver1-Dec-08 2:56 
QuestionHow I can add this project to other? Pin
Yulay18-Nov-08 6:49
memberYulay18-Nov-08 6:49 
QuestionChoose region Pin
captainhan29-Aug-07 0:03
membercaptainhan29-Aug-07 0:03 
QuestionHow to copy a part of sound and plaster it to other place Pin
captainhan28-Aug-07 23:44
membercaptainhan28-Aug-07 23:44 
QuestionRe: How to copy a part of sound and plaster it to other place Pin
Ibrahim Dwaikat18-Nov-07 7:12
memberIbrahim Dwaikat18-Nov-07 7:12 
GeneralFile access Pin
amiashu30-Jul-07 7:04
memberamiashu30-Jul-07 7:04 
GeneralC# 2005 Pin
jvp2929-Jun-07 17:40
memberjvp2929-Jun-07 17:40 
Generaladding real time "bouncing ball" cursor Pin
jbarbermd5-Apr-06 16:38
memberjbarbermd5-Apr-06 16:38 
GeneralRe: adding real time "bouncing ball" cursor Pin
jbarbermd20-Apr-06 17:36
memberjbarbermd20-Apr-06 17:36 
GeneralWAV Control Pin
DNA: Code this!6-Nov-04 10:52
memberDNA: Code this!6-Nov-04 10:52 
GeneralLeft &amp; Right Pin
Kenneta24-Sep-04 2:57
memberKenneta24-Sep-04 2:57 
GeneralRe: Left &amp; Right Pin
L Macia2-Nov-08 11:09
memberL Macia2-Nov-08 11:09 
GeneraluLaw format Pin
Anonymous17-May-04 18:12
sussAnonymous17-May-04 18:12 
QuestionHow to draw a waveform while recording? Pin
croch17-May-04 3:24
membercroch17-May-04 3:24 
AnswerRe: How to draw a waveform while recording? Pin
Ibrahim Dwaikat18-Nov-07 7:13
memberIbrahim Dwaikat18-Nov-07 7:13 

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 23 Aug 2003
Article Copyright 2003 by pj4533
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid