Click here to Skip to main content
Click here to Skip to main content

VB.NET Wav file graphical viewer control

By , 22 Mar 2005
 

Introduction

Firstly, I would like to acknowledge that the some of the methods for this control came from Steve McMahon's (http://www.vbaccelerator.com/) VB6 version, so, all the credit for the idea goes to him. The contribution I have made is to update the code for use in VB.NET 2003 and introduce a few additional elements. I should also mention that the WaveFile class used in the control is an extension of the one written by The KPD-Team.

The operation of the control is to provide a visual representation of both channels of a 16-bit PCM wav file, with variable zoom. (Future versions will hopefully support other bit sizes as well.) The full list of features is as follows:

  • Display 2-channel 16-bit wav file data.
  • Provide sample frequency data.
  • Provide bits per sample information.
  • Provide number of channels (see #).
  • Zoom control ranging from 24:1 to 1:16384.
  • Full control over the color used in the graphics.
  • Full control over the line forms used in the graphics.
  • Ability to run wav file data gathering function in a secondary thread for slow computers.
  • Events for all major operations available to host application.

# It will always have a result of 2 due to limitations of this version and will be more useful in later versions that will have support for mono wav files.

The project files provided along with this article include both the control code and a test application. It should be possible for most of you to follow how the code works, but here is a small guide:

  1. The control is set to default values when first inserted into the host application.
  2. The format for background and lines is determined by using the RenderWave sub.
  3. Main_Paint sub takes the information and displays the lines. Changes to any properties causes Renderwave to be called again and in turn Main_Paint.
  4. The file name is passed to the control from the host app using <name>.WaveFile=.
  5. This is passed to the WaveFile class where the file headers are stripped out and checked, make sure that the file is a valid wav file.
  6. The class sets up a data buffer, returns to the main control code.
  7. RenderWave is again called.
  8. If thread support has been enabled, the function WaveInfo is called via thread calls, otherwise it is called as a normal function. Either method triggers the RenderStart event.
  9. WaveInfo starts to read data from the wav file in blocks equal to the buffer length (0.1s).
  10. The number of samples plotted per block depends on the zoom factor.
  11. If you are using threading, RenderWave will finish. The callback function will cause Main_Paint to update once the data has been collected. Otherwise Renderwave will continue after the WaveInfo function has finished and in turn will call Main_Paint.
  12. Changing the zoom or moving the scroll bar will start the functions again.

The project, although useful in its own right, might also be useful for people wishing to learn more about:

  • GDI+
  • GraphicsPath
  • Marshal.Copy
  • Wave headers and Streaming
  • Control Properties and the "Visual Studio Property" descriptions and category options.
  • Unmanaged code
  • Structures

Please contact me if you have any questions or suggestions for improvement.

Updates to this article
  • Corrected spellings.
  • Inserted a new point "10" in the guide.

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

Quenchmaster
Web Developer
United Kingdom United Kingdom
Member
Having done a four year Electronic Engineering Undergraduate course at the University of Warwick, I have now moved on to doing a PhD at the same place.
 
I am currently envolved in the use of signal processing in watermarking for copyright control.
 
I have always had a knack of picking up different programming languages quite easily but have not done much serious programming untill now.

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.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionCan it do 8-bit?memberMember 402649816 Jul '09 - 6:09 
I think this code will be very useful to me, but I was wondering if it can easily be converted to handle 8-bit wav files. I tried using "short" instead of "Int16", but it didn't work.
 
Thanks!
 
Dan Portman
GeneralGenerating a tonememberjumpjack7 Jun '08 - 21:36 
I'd like to generate a fixed-frequency tone in correspondance with "pieces" of a waveform with amplitude greater than a given value; I can analyze amplitude using your source, but how can I generate tone?
QuestionAnalog to digital converter?memberjumpjack19 May '08 - 7:49 
Do you think to be able to analyze a signal like this one to figure out when and how long the signal is high and low?
<img src="http://www.planetmobile.it/jumpjack/remote-dvd-lg-rh255-PLAY-wav-76000Hz.JPG<br mode=" hold=" /">
It's the result of sampling my remote control using an IR led connected to audio card.
If I could send its digital version out to com port, I could use LIRC to decode the signal, without buying the TSOP 1738 component, usually required for LIRC sampling but which I am not able to find, unless sent for 8$ s&h!
 
Of course I should create a loopback among two serial ports: one for my output, one for LIRC input. </img>
GeneralC# usage and possible bugmemberdnjuls9 Apr '08 - 22:04 
I tried to use your class WaveFile in my C# project.
My wave file has only 1 channel.
 
Why do you divide the size by four when returning ReadSize, BufferSize, etc.?
 

Thanks
GeneralRe: C# usage and possible bugmemberQuenchmaster9 Apr '08 - 22:51 
Its been such a long time since I looked at the code and WAV files that I may be a bit rusty but after a quick glance:
 
1) This software was writen for 16-bit Two Channel WAV Files
2) Stereo WAV data is stored in pairs. Left, Right, Left, Right etc. So to get say 100 samples for both you have to read 200 samples and split the data. This gives a factor of 2
3) The file system read functions - something to do with 16-bit / 32-bit data changes also introduce a factor of 2.
 
Combining points 2 and 3 give you the factor of 4. The WAV reader code was writen taking all this into acount so as to not have fidaly changes for the API calls.
 
Now as I said, I am a bit rusty and my explination may not be easy to follow - I suggest you find some information of WAV file structures - there are plenty of websites and even Wikipedia can help on this.
 
EDIT: I may have more time in the next few months to improve on the software and even update to newer VS versions.
Generalmodify for only one chanelmemberamiashu25 Jul '07 - 9:10 
hey hi how can i modify your code to make it work for single chanel
i took out the statement that check whether or not if it had 2 signals or not
and it gave me 2 same views
i went thorugh your website but you havent added anything there yet too as you said you will
can you guide me for this buddy
 
thanks,
amit
QuestionFor a C++ Project?memberlaserbeak4320 Mar '07 - 14:14 
will i be able to use this with a C++ project?
if so, how?
 
thanks
GeneralDrawing problemsmemberEnriad27 Nov '06 - 14:08 
Hi,
 
thanks for a fantastic article! It helps a lot, though do you know of a way to reduce the flickering whilst scrolling through the wav file? I would love to use this control in a program I'm writing which will allow you to play, seek and edit wav files and I was thinking about a slider inside the control (which I've added), though due to the need of an almost constant invalidate(), the drawing is very flickery.
 
Also, please let me know if you do not wish to me use this in my program. I will of course, give you full credit, as you are the one who wrote it in the first place.
 
Kind regards,
 

Daniel

GeneralRe: Drawing problemsmemberQuenchmaster28 Nov '06 - 0:42 
Daniel,
 
Your more than welcome to use the control in your application as long is it remains free and credit is in the "About" box.
 
As for the flickering, im not sure there is much that can be done if you want it to constently refresh. I had this problem with the left to right scroll used in the display window and the only solution I found was to stop it updating untill after the scroll was finished.
 
The control does need a lot of work. It is inefficent for a a major application as it uses large amounts of processor every time it re-renders. There are ways around this, and I might be able to make it better, but I currently do not have time. If you keep an eye out at the software section www.quenchmaster.co.uk I will be adding this code there and will try to update it as well.
 
Cheers
 
Adrian
Questionmono only?membernicolas_gauthier8 Mar '06 - 5:43 
Hello,
 
I downloaded your waverender code. And i'm very interesting by this code and i have a lots of questions:
 
- My first question is about the restriction of 16 bits and stereo. Do you have a newer version who don't have this resctriction (I need to render MONO wav) ?
 
- Is it possible to help me make the link between time and the wav data ? For example, i would like to get a value for each 0.1 second of the wav for all kinds of wav.
 
- Is it possible to read a .wmv or others file type ?
 
- Can you help me understand the “level”, for exemple, how to know what is the “0” level. (a need to convert ‘your’ value to x/100(percent) value)
 
- Do you know a code who add / read / edit cue point in a wav file ?
 
- Do you have a newer version?
 
Thank you very much.
 
Nicolas G.

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130523.1 | Last Updated 22 Mar 2005
Article Copyright 2005 by Quenchmaster
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid