Example of playing back a sample in ofSoundStream

Cheers Manish,

Was more the examples of sample data saving and waveform generation I was after. Straight FFT I’ve got down :smile:

I think the files got lost during one of the forum upgrades. Here’s the sampleWithWaveform code…it’s old and pretty ugly, but might be useful.
src.zip (6.0 KB)

@grimus thank you so much!

hmm I compiled uploaded source. but it seems not work.
I tried with 44100, stereo wav file.
check the screenshot below.

app played only few milliseconds,

I actually revisited that sampler code recently for a demo, and under OSX there were issues loading the .wav file. Can you try loading and then re-exporting the .wav using the Audacity editor? That fixed things for me.

wow! it works!
I already tried wav file that exported with “garageband”.
I don’t know what difference between them. but anyway. now it works.

super thanks!!

Basically this .wav loader is super raw, so it’s probably only successfully loading really clean vanilla files, such as those output by audacity. Garageband may well have some extra junk in the .wav header

I see. maybe that junks are some metadata. right ? :slight_smile:

Hello. Here is code for drawing WAV files using ofSoundPlayer, with FMOD.
(Tested in oF 0.7.4/Windows, oF 0.8.4/OSX, currently it supports 8,16 and 32 bit files, and not support 24-bit files)

#include "testApp.h"

ofSoundPlayer sound;          

float vrate_ = 120.0; //rate for sampling for visualization
vector<float> data_; //values for drawing waveform, with rate vrate_, from 0 (silence) to 1 (max loudness)

float duration;    //duration of sound in seconds

void testApp::setup(){

    ofPtr<ofBaseSoundPlayer> player  = sound.getPlayer();
    ofFmodSoundPlayer *fmod = (ofFmodSoundPlayer *)player.get();

    int length = fmod->length;
    //FMOD_Sound_GetLength(sound, &length, FMOD_TIMEUNIT_PCM);

    void *ptr1, *ptr2;
    unsigned int len1=0, len2=0;
    FMOD_RESULT result;

    //get sound samples
    result = FMOD_Sound_Lock(fmod->sound, 0, length, &ptr1, &ptr2, &len1, &len2);
    if ( result == FMOD_OK ) {
        FMOD_SOUND_TYPE type;
        FMOD_SOUND_FORMAT format;
        int channels;
        int bits;
        result = FMOD_Sound_GetFormat(fmod->sound, &type, &format, &channels, &bits);

        unsigned int length_ms;
        result = FMOD_Sound_GetLength(fmod->sound, &length_ms, FMOD_TIMEUNIT_MS);
        duration = length_ms/1000.0;
        int n = duration * vrate_;
        long long int N = (long long int (length))*channels;
        //cout << "n " << n << endl;
        data_.resize( n );
        fill( data_.begin(), data_.end(), 0 );
        for (int i=0; i<N; i++) {    //check samples in all channels, and project them to data_
            float v = 0;
            switch ( format ) {
            case FMOD_SOUND_FORMAT_PCM8:    v = ((char *)ptr1)[i]; break;           /* 8bit integer PCM data. */
            case FMOD_SOUND_FORMAT_PCM16:    v = ((short *)ptr1)[i]; break;         /* 16bit integer PCM data.  */
            //case FMOD_SOUND_FORMAT_PCM24: v = ((??? *)ptr1)[i];  break;        /* 24bit integer PCM data.  */
            case FMOD_SOUND_FORMAT_PCM32:    v = ((int *)ptr1)[i]; break;          /* 32bit integer PCM data.  */
            case FMOD_SOUND_FORMAT_PCMFLOAT: v = ((float *)ptr1)[i]; break;        /* 32bit floating point PCM data.  */
            v = fabs(v);

            int j = (long long int)(i) * n / N;
            data_[j] = max( data_[j], v );
      //normalize values
        float maxVal = 0;
        for (int i=0; i<n; i++) {
            maxVal = max( maxVal, data_[i] );
        if ( maxVal > 0 ) {
            for (int i=0; i<n; i++) {
                data_[i] /= maxVal;

         //unlock pointer to sound samples
        result = FMOD_Sound_Unlock(fmod->sound, &ptr1, &ptr2, len1, len2);


void testApp::update(){


void testApp::draw(){

    //draw waveform
    int w = 1000;
    int n = data_.size();
    ofSetColor( 255 );
    for (int i=0; i<w; i++) {
        float v = data_[i*n/w];
        ofLine( i, 300-v*50, i, 300+v*50 );  
1 Like

Lots of thanks for this example, @grimus. I think this is a really important topic, and I found it hard to find the solution. Hence i created this repository:

I would feel better if I could transfer the ownership to you, as you created the code. But anyway it is already there fo future improvements.

No problem!

If I were to put it up on my github, I would have to clean the code up…which I don’t have the time to do unless I use that code again some time, which unfortunately I haven’t :smile:

Hey guys,
@autotel good to see you around here! :slight_smile:

You should try this addon http://github.com/roymacdonald/ofxSoundObjects/
It allows a lot more stuff than just loading and plotting samples, it doesn’t rely on fmod and it is super flexible, extensible and modular.