aubio


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Real-Time Onset Detection



Something is up with this and I can’t find it.  I’m an AUBIO noob still.. lol

I’m trying to get onsets and their peak value in real-time.  It’s actually working, but not 100%.

 

ISSUES:

*It seems if to many onsets happen at once, the program gets stuck and it no longer detects them. Buffer issues?

*The “threshold” variable seems to be ignored. It has no effect.

*When I test for silence I get nothing. If I change silence to about -10.0, I get ALL silence. Is this working correctly?

 

Forgive me if there are obvious errors in this, I am new to AUBIO and DSP in general. This code was made in reference to aubioonset.c and utils.c.    I believe the issues are in my callback function.   Thanks for any help in advance.

 

 

 

 

 

//######################################################

//#  Real-Time Onset Detector

//# 

//#  Note: "currently NO error protection"

//#

//######################################################

 

#include <aubio.h>

#include "portaudio/include/portaudio.h"

#include "portaudio/src/common/pa_trace.h"

#include <windows.h>  //for Sleep() function

 

 

///////// PORTAUDIO ///////

#define SAMPLE_RATE          (44100)

#define PA_SAMPLE_TYPE       paFloat32

#define FRAMES_PER_BUFFER    (256)

 

typedef struct PaQaData

{

    unsigned long  framesLeft;

    int            numChannels;

    int            bytesPerSample;

    int            mode;

    short          sawPhase;

    PaSampleFormat format;

}PaQaData;

 

 

//////// AUBIO /////////

smpl_t threshold                      = (smpl_t)0.3;

smpl_t silence                        = -90.;

smpl_t peak;

fvec_t * ibuf;

aubio_pvoc_t * pv;

cvec_t * fftgrain;

uint_t buffer_size                    = (FRAMES_PER_BUFFER * 2);

uint_t overlap_size                   = FRAMES_PER_BUFFER;

uint_t channels                       = 1;

aubio_onsetdetection_type type_onset  = aubio_onset_kl;

aubio_onsetdetection_t *o;

fvec_t *onset;

aubio_pickpeak_t * parms;

 

uint_t isonset = 0;

uint_t pos = 0; /*frames%dspblocksize*/

uint_t number = 0;

static int gNumNoInputs = 0;

 

 

///////////////////////////////////////////////////////////////////

class Audio

{

public:

      Audio() { inDev = new PaStreamParameters; Pa_Initialize(); }

      ~Audio() { Pa_Terminate(); delete inDev; }

 

      void StartCallback() { Pa_StartStream( stream ); }

      void StopCallback() { Pa_StopStream( stream ); }

     

      static int CallBack( const void *inputBuffer, void *outputBuffer,unsigned long framesPerBuffer,

                           const PaStreamCallbackTimeInfo* timeInfo, PaStreamCallbackFlags statusFlags,

                           void *userData );

 

      PaStreamParameters *inDev;

      PaStream *stream;

      PaQaData myData;

};

 

int Audio::CallBack( const void *inputBuffer, void *outputBuffer, unsigned long framesPerBuffer,

                              const PaStreamCallbackTimeInfo* timeInfo, PaStreamCallbackFlags statusFlags, void *userData )

{

      smpl_t *in = (smpl_t*)inputBuffer;

      isonset=0;

 

      /////// AUBIO ONSET DETECTION ////////////

    unsigned int f;       /*frames*/

    ibuf->data = ""

 

    for (f=0;f<(unsigned)framesPerBuffer;f++) {

 

            //time for fft

            if (pos == overlap_size-1){   

 

                  aubio_pvoc_do(pv,ibuf, fftgrain);

                  aubio_onsetdetection(o,fftgrain, onset);

                  isonset = aubio_peakpick_pimrt(onset,parms);

 

                  if (isonset){

 

                        // test for silence

                        if (aubio_silence_detection(ibuf, silence)==1){

 

                              isonset=0;

                        }

                        else{

 

                              peak = aubio_peakpick_pimrt_getval(parms);

                              printf("%d !ONSET DETECTED! %f \n", number++, peak);

                        }

                  }

                  pos = -1; // so it will be zero next f loop

            }

            pos++;

      }

 

      return 0;

}

 

////////////////////////////////////////////////

int main(){

 

      ////////// AUBIO ////////////////////

      pv = new_aubio_pvoc(buffer_size, overlap_size, channels);

      ibuf      = new_fvec(overlap_size, channels);

    fftgrain  = new_cvec(buffer_size, channels);

      parms = new_aubio_peakpicker(threshold);

      o = new_aubio_onsetdetection(type_onset,buffer_size,channels);

      channels);

 

      //////// PORTAUDIO //////////////////

      Audio *audio = new Audio();

        

      audio->inDev->device = Pa_GetDefaultInputDevice();

      audio->inDev->channelCount = channels;

      audio->inDev->sampleFormat = PA_SAMPLE_TYPE;

      audio->inDev->suggestedLatency = Pa_GetDeviceInfo( audio->inDev->device )->defaultLowInputLatency;

      audio->inDev->hostApiSpecificStreamInfo = NULL;

 

      Pa_OpenStream  ( &audio->stream, audio->inDev, NULL, SAMPLE_RATE, FRAMES_PER_BUFFER, 0, audio->CallBack, NULL );

      audio->StartCallback();

 

      printf("\n The program will run for 30 seconds \n");

      Sleep(30000);

 

      return 0;

}