[linux-audio-dev] Algorithm for displaying waveforms

vanDongen/Gilcher gml at xs4all.nl
Mon Jun 6 09:17:54 UTC 2005


On Monday 06 June 2005 01:05, Them wrote:
> For a small application I am developing, I need to display waveforms
> (read-only) from audio data loaded into my application using libsndfile.
>


As I recently found out, this can be very messy :)
The basic algorithm is this:
Each horizontal pixel represents n samples. Usually n is pretty big.
Of those n samples you take the min and the max, and then you draw a vertical 
line between them.
Obviously this has to be scaled to your vertical resolution and vertically 
translated to the right position  on your canvas. 
If n<=1 you can choose to draw lines between the points, or just show the 
points.

What is often done is to generate a peakfile, containing the min and max for a 
set chunksize.
This is usefull if you have to redraw the waveform often. 

Where it gets messy is if you want to zoom, display parts of the file, rapidly 
scroll etc.etc. Then you need to set up clever caching and deal with rounding 
problems. 
In ardour the is ardour/gtk_ardour/canvas-waveview.c that does the clever 
caching and libs/ardour/source.cc read_peaks() that generates the peaks.

The code in ardour may be to complex for your application because it is made 
to show any part of an audiofile at any position, where you may move an 
audioregion to a position "in between" screen pixels, where the region starts 
at any frame and the frames_per_pixel of the display is not an integer.

Gerard



More information about the Linux-audio-dev mailing list