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:
- The control is set to default values when first inserted into the host application.
- The format for background and lines is determined by using the
RenderWave
sub.
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
.
- The file name is passed to the control from the host app using
<name>.WaveFile=
.
- 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.
- The class sets up a data buffer, returns to the main control code.
RenderWave
is again called.
- 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.
WaveInfo
starts to read data from the wav file in blocks equal to the buffer length (0.1s).
- The number of samples plotted per block depends on the zoom factor.
- 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
.
- 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.