How to draw an audio file waveform?

Hello,

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.

Regards

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 https://github.com/roymacdonald/ofxSoundObjects
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!

@roymacdonald
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”

https://github.com/kritzikratzi/ofxAvCodec 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 : https://trac.ffmpeg.org/wiki/Waveform#UsingGnuplot

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];
 
if(!(audioFile=popen(command.c_str(),"r"))){
   //return 1;
   ofLog()<<"It doesn't work"<<endl;
}
while(fgets(buff,sizeof(buff),audioFile)!=NULL){
  for(int i=0;i<sizeof(buff);i+=sizeof(char)*2){
    short p =(buff[i]<<8)|buff[i+1];
    ofLog()<<p<<endl;
    valeurs.push_back(p);
    if(maxValue<abs(p))maxValue=abs(p);
 }
}

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

Hey!
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?
best

Hello

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

I just tried the audioOutput example, and I copied you the errors there http://pastebin.com/9KN1TJBs

On the rpi, it seems it use libmpg123 :

HOST_OS=Linux
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 install_codecs.sh 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 : http://pastebin.com/qWiK1z2N ?

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"
#endif

should be:

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

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?
best!

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 ?

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

all the best!

@roymacdonald Done : https://github.com/roymacdonald/ofxSoundObjects/pull/12