How to obtain the sound's waveform |
|
After loading a sound inside a player, through one of the many methods having the "Load" prefix like LoadSound or LoadSoundFromMemory or LoadTrackFromCd methods, the Waveform class allows generating a bitmap of the sound's waveform: this bitmap can represent the sound in its full length or, in alternative, one or more portions (also known as "views") of the same sound.
In order to generate one or more bitmaps of the loaded sound, the control needs to analyse the sound's contents: this task is performed through a call to the Waveform.AnalyzeFullSound method. The analysis can be more or less accurate depending upon the resolution passed to the nResolution parameter; higher resolutions will allow a better quality because more peaks will be detected: as a side effect, higher resolutions will require more memory and will generate larger bitmaps. The analysis can be aborted at any time through a call to the Waveform.AnalyzeAbort method.
After calling this method, the control will allow the container application to stay up-to-date about the analysis advancement through the following events:
• | WaveAnalysisStart: fired when the sound's analysis begins. |
• | WaveAnalysisPerc: fired several times during the analysis in order to inform the container application about the percentage of advancement. |
• | WaveAnalysisDone: fired when the sound's analysis is completed: after catching this event, it will be possible requesting to the control the creation of the waveform's bitmap through a call to the Waveform.BitmapViewSaveToFile or Waveform.BitmapViewSaveToMemory methods. It's important to note that this event will report the exact number of peaks detected inside the loaded sound and the exact duration in milliseconds of each peak. When generating the bitmap for the full sound's waveform, the reported number of peaks will be exactly equal to the width in pixels of the bitmap. In case you should create a view of a given sound's range, you could obtain the number of pixels needed to display the waveform's view through a call to the Waveform.BitmapViewGetWidth method. |
After having analyzed a certain song, in order to save CPU time when you will load and analyze again the same song, you have the possibility to store analyzed waveform's peaks into a file using the Waveform.PeaksSave method and to retrieve them at a later time using the Waveform.PeaksLoad method: WaveAnalysisStart and WaveAnalysisDone events will be generated in this case also.
The generated waveform's bitmap will be represented using the Waveform.ColorLine and Waveform.ColorBackground colors. You can also decide if the peaks of the waveform should be drawn with a color which is half the intensity of the color set through the Waveform.ColorLine property by setting the Waveform.UseHalfColorForPeaks property.
Below you can see five bitmaps generated using the same sound (whose duration is around 2.4 seconds length) but with five different resolutions (just for your information the song used for generating these bitmaps is available on this link); all these bitmaps have been created using the same height of 100 pixels:
Resolution passed to AnalyzeFullSound method |
Generated bitmap |
|
|
WAVEFORM_RES_MAXIMUM |
|
WAVEFORM_RES_VERY_HIGH |
|
WAVEFORM_RES_HIGH |
|
WAVEFORM_RES_MIDDLE |
|
WAVEFORM_RES_LOW |
As mentioned before, you can also generate a "view" of a defined portion of the loaded sound: on the bitmap below you can see the original song in its full length (with WAVEFORM_RES_MAXIMUM resolution)
and below you can see the view of the same song for the range between 500 and 1500 milliseconds
On the samples above we have used a very small song limited to 2.4 seconds: in case you should be in need of displaying the full waveform of a longer song on a space much smaller than the total number of required pixels, the Waveform.BitmapViewSaveToFile and Waveform.BitmapViewSaveToMemory methods will shrink the waveform in order to fit exactly inside the available space; in the example below you can see a 3 minutes song fitting inside a 455 pixels wide bitmap: the original full length bitmap, using the WAVEFORM_RES_MAXIMUM resolution, would require a width of 243,683 pixels.
VERY IMPORTANT: When using high resolutions and big sound files, keep count that the Windows operating system has limits on the size of bitmaps creation; this means that it would be better avoiding the creation of very large bitmaps: in this case it would be a better approach splitting the total song's bitmap into several smaller bitmaps, for example creating bitmaps whose width in pixels doesn't exceed the current screen width.
After catching the WaveAnalysisDone event, it will be also possible rendering the waveform directly inside a graphical device context (through its handle or HDC) using the Waveform.BitmapViewDrawToHdc method; this may be quite useful if you should need to perform real-time rendering of the waveform on the screen. Although this could be also used for creating a scrolling waveform during playback, we suggest checking the How to scroll the sound's waveform during playback tutorial for further details about managing a scrolling waveform.
In case you should need to perform your own graphic rendering of waveform's peaks, after the analysis you could obtain peak values for the full file or for a portion of the same through the Waveform.PeaksBufferGet method.
When dealing with video clips, after having loaded the video clip into a certain player, you can extract its audio track through the VideoPlayer.AudioTrackExtract method and, after completing the extraction, you can proceed to the sound waveform analysis as described for regular sound files.
Samples of waveform management in Visual C#.NET and Visual Basic.NET can be found inside the following samples installed with the product's setup package:
- BeatsDetection
- Waveform
- WaveformScroll
For further details about using the embedded Waveform refer to the Waveform class section.
For details about using Visual Feedbacks refer to the How to use the embedded Visual Feedbacks section.