How to draw an audio file waveform?


For my project, I need to draw the waveform of an audio file.

Ofgetspectrum is unavailable on embedded platform, and I thought to use ofxfft, but it seems to take an audio stream/buffer at source, and for what I seen we can’t pass from an audio player to an audio buffer/stream ( if I’m wrong, thanks to correct me).

So, how can I draw an audio file waveform, without playing it ?

I thought to use a third party software, like PureData, but I would like to keep it in of, as I will already use another software in background.


ofSoundGetSpectrum should work on linux arm, do you get any error when trying o use it?

Yes, it works great !

But now I have another question, except by playing with the speed parameters, how can I use it for draw the waveform in a second thread, at an accelerated speed, in fact the fatest possible as the rpi can ? :slight_smile:

void DrawWav(ofPoint start, float* wavData, int wavDataSize, int direction/*+1 or -1*/, float xFactor)
    for (int i = 0; i < wavDataSize; i++)
        if(i % 2 == 0)
            float firstx = start.x + direction*(i / 2);
            float firsty = start.y - (wavData[i] * xFactor);
            float secondy = start.y - (wavData[i+1] * xFactor);
            ofDrawLine( firstx, firsty, firstx, secondy);

I think you mean spectrum, but this will draw the wav image. I needed something simple and reusable. xFactor is just the height (should really be yFactor).

Hi @cgiles
try out this addon
Check the examples.
I haven´t tested it on embedded platforms, although it seems to work on Linux. (almost sure about this. ping @thomasgeissl)
Let me know it it works.
all the best!

I was going to contact you, because I tried it, and it doesn’t work with the raspberry pi.

First I got a lot of warning about the files in your libs not being a directory.

Then I got an error : mpg123_handle * mp3File; was not declared in this scope

Of course everything which depend of this make an error then.

And every mpg123 things generate an error as “was not declared in this scope” doesn’t work too.

One solution I found is to do a popen to ffmpeg, and get back the data in oF, based on this :

But I fail to convert from my char buff to short little endian.

my code :

 string fileName=ofToDataPath("bohemian2.mp3");
string command="ffmpeg  -i "+fileName+"  -ac 1 -filter:a aresample=100 -map 0:a -c:a pcm_s16le -f data -";//   pipe:1";
FILE  *audioFile;
char buff[512];
   //return 1;
   ofLog()<<"It doesn't work"<<endl;
  for(int i=0;i<sizeof(buff);i+=sizeof(char)*2){
    short p =(buff[i]<<8)|buff[i+1];

Thanks for the snippet, but I already have this part, my issue is how to get the float* wavData.

I haven´t tried ofxSoundObjects on the rpi.
As far as I can remember, OF on linux uses soundfile lib and not mpeg123. Not really sure.
You can safely remove the libs directory of ofxSoundObjects, as it only contains libaudiodecoder which is for win and mac.
The mpg123 error is because you are not adding the mpg123 lib. Take a look at ofxSoundFile. you could maybe force to undefine OF_USING_MPG123 so to avoid using it or add this lib correctly. Are the regular sound examples working on the raspi?


I tried to compile the sound player example, and it failed.

I just tried the audioOutput example, and I copied you the errors there

On the rpi, it seems it use libmpg123 :

checking pkg-config libraries: cairo zlib gstreamer-app-1.0 gstreamer-1.0 gstreamer-video-1.0 gstreamer-base-1.0 libud ev freetype2 fontconfig sndfile openal openssl gtk±3.0 libmpg123

But I’m not an expert, better to ask to @arturo

you need to run the script

I already did it when I installed oF the first time, tried again, and it installed nothing, everything is up to date.

can you run:

pkg-config libmpg123 --exists; echo $?

and post what it outputs?

It outputs just “0”

that’s correct, can you post the error with a little bit more context, like the compiler call that make outputs, the line of the error…

Does this is more complete : ?

Or should I add something to my make ?

haven’t tested it but in ofSoundFile.h line 19:

#if defined (OF_USING_SNDFILE)
	#include <sndfile.h>
#elif defined (OF_USING_MPG123)
	#include <mpg123.h>
#elif defined (OF_USING_LAD)
	#include "audiodecoder.h"

should be:

#if defined (OF_USING_SNDFILE)
	#include <sndfile.h>
#elif defined (OF_USING_LAD)
    #include "audiodecoder.h"
#if defined (OF_USING_MPG123)
	#include <mpg123.h>

since linux uses libsndfile to open most file formats except for mp3 for which it optionally uses libmpg123 if it’s installed

@arturo is this modification to ofxSoundFile.h something that I should apply and commit to the ofxSoundObjects repo or is it just a fix for this particular issue?

yes that’s an issue that would happen in linux in general without that fix

@arturo @roymacdonald

It works, I compiled the soundPlayerExample of ofxSoundObject

I needed to declare an “int bitDepth;” and do some tweaking.

But it only support wavs, mp3 just give noises.

One issue I faced is the non support of the dialog window. Is there a tweak somewhere for get it works ?

@arturo thanks! i’ll fix it.
@cgiles can you post here those fixes you made please? or even better make a pull request to

all the best!

@roymacdonald Done :