Audio meters

How does Audacity access the data to make the audio meters say for a named ogg file?

Can anyone point me to code for this?

Thx

I haven’t looked at the source code for Audacity, but you can! :stuck_out_tongue:

The “hard part” is decoding the OGG file. (uncompressed WAV files are much simpler.) Once the OGG file is decoded, you get a series of samples (values or numbers) and each value represents the “height” of the wave at one moment in time. i.e. CDs are sampled at 44.1kHz, so there are 44,100 samples for every second of audio (for each channel). If you plot those values and “connect the dots”, you’ll see the waveform. If you zoom-in with Audacity, you can see the individual samples. Normally, when you are looking at a few minutes (or several seconds) of audio, there are not enough pixels to show all of the samples.

You can take the peak values (or calculate a moving average) to get the data for your meters.
With audacity (and most audio editors) the sample values are stored in floating-point with a value of 1.0 (or -1.0) representing 0dB. So, the decoded file is an array of floating-point numbers, usually between +1.0 and -1.0. (Silence is a series of zeros.) In a stereo WAV file, the left & right samples alternate, but the audio editor might use separate arrays for left & right.

Note that the audio file may hold samples in a different format. i.e. 16-bit WAV files holds integer values between
-32,768 and +32,767. But, the data is usually normalized to +/-1 (floating point) for editing, analysis, or DSP. The data has to be converted back to integers before being fed into the digital-to-analog converter for playback. A compressed file (OGG, MP3, etc.) doesn’t hold the individual sample values, so it has to be decoded before it can be analyzed or played-back.

Audacity does not operate directly on compressed audio files. The audio data is decoded to uncompressed PCM data when the file is imported.
The C++ Source code for the VU meters is in /src/widgets/Meter.cpp

Thx for the posts and the code, guess I need to be able to get the ogg to uncompress into memory to be able to access its bytes sequentially. Am working in darkBasic which can call and use dlls and indeed play ogg files(created with Audacity). Was thinking of how to make a dll to read sound values of my ogg as I play it. Perhaps I could invoke the lame enc to uncompress them to PCM wav data somehow.

Perhaps I could invoke the lame enc to uncompress them to PCM wav data somehow.

LAME does not encode/decode OGG files. It’s only for MP3s. (I don’t know how Audacity encodes/decodes OGG, but it’s easy to find a CODEC.)

Have a look at libsndfile and libvorbis.