1. //######################################################
  2. //# Real-Time Onset Detector
  3. //#
  4. //# Note: "currently NO error protection"
  5. //#
  6. //######################################################
  7.  
  8. #include <aubio.h>
  9. #include "portaudio/include/portaudio.h"
  10. #include "portaudio/src/common/pa_trace.h"
  11. #include <windows.h> //for Sleep() function
  12.  
  13.  
  14. ///////// PORTAUDIO ///////
  15. #define SAMPLE_RATE (44100)
  16. #define PA_SAMPLE_TYPE paFloat32
  17. #define FRAMES_PER_BUFFER (256)
  18.  
  19. typedef struct PaQaData
  20. {
  21. unsigned long framesLeft;
  22. int numChannels;
  23. int bytesPerSample;
  24. int mode;
  25. short sawPhase;
  26. PaSampleFormat format;
  27. }PaQaData;
  28.  
  29.  
  30. //////// AUBIO /////////
  31. smpl_t threshold = (smpl_t)0.3;
  32. smpl_t silence = -90.;
  33. smpl_t peak;
  34. fvec_t * ibuf;
  35. aubio_pvoc_t * pv;
  36. cvec_t * fftgrain;
  37. uint_t buffer_size = (FRAMES_PER_BUFFER * 2);
  38. uint_t overlap_size = FRAMES_PER_BUFFER;
  39. uint_t channels = 1;
  40. aubio_onsetdetection_type type_onset = aubio_onset_kl;
  41. aubio_onsetdetection_t *o;
  42. fvec_t *onset;
  43. aubio_pickpeak_t * parms;
  44.  
  45. uint_t isonset = 0;
  46. uint_t pos = 0; /*frames%dspblocksize*/
  47. uint_t number = 0;
  48. static int gNumNoInputs = 0;
  49.  
  50.  
  51. ///////////////////////////////////////////////////////////////////
  52. class Audio
  53. {
  54. public:
  55. Audio() { inDev = new PaStreamParameters; Pa_Initialize(); }
  56. ~Audio() { Pa_Terminate(); delete inDev; }
  57.  
  58. void StartCallback() { Pa_StartStream( stream ); }
  59. void StopCallback() { Pa_StopStream( stream ); }
  60.  
  61. static int CallBack( const void *inputBuffer, void *outputBuffer,unsigned long framesPerBuffer,
  62. const PaStreamCallbackTimeInfo* timeInfo, PaStreamCallbackFlags statusFlags,
  63. void *userData );
  64.  
  65. PaStreamParameters *inDev;
  66. PaStream *stream;
  67. PaQaData myData;
  68. };
  69.  
  70. int Audio::CallBack( const void *inputBuffer, void *outputBuffer, unsigned long framesPerBuffer,
  71. const PaStreamCallbackTimeInfo* timeInfo, PaStreamCallbackFlags statusFlags, void *userData )
  72. {
  73. smpl_t *in = (smpl_t*)inputBuffer;
  74. isonset=0;
  75.  
  76. /////// AUBIO ONSET DETECTION ////////////
  77. unsigned int f; /*frames*/
  78. ibuf->data = &in;
  79.  
  80. for (f=0;f<(unsigned)framesPerBuffer;f++) {
  81.  
  82. //time for fft
  83. if (pos == overlap_size-1){
  84.  
  85. aubio_pvoc_do(pv,ibuf, fftgrain);
  86. aubio_onsetdetection(o,fftgrain, onset);
  87. isonset = aubio_peakpick_pimrt(onset,parms);
  88.  
  89. if (isonset){
  90.  
  91. // test for silence
  92. if (aubio_silence_detection(ibuf, silence)==1){
  93.  
  94. isonset=0;
  95. }
  96. else{
  97.  
  98. peak = aubio_peakpick_pimrt_getval(parms);
  99. printf("%d !ONSET DETECTED! %f \n", number++, peak);
  100. }
  101. }
  102. pos = -1; // so it will be zero next f loop
  103. }
  104. pos++;
  105. }
  106.  
  107. return 0;
  108. }
  109.  
  110. ////////////////////////////////////////////////
  111. int main(){
  112.  
  113. ////////// AUBIO ////////////////////
  114. pv = new_aubio_pvoc(buffer_size, overlap_size, channels);
  115. ibuf = new_fvec(overlap_size, channels);
  116. fftgrain = new_cvec(buffer_size, channels);
  117. parms = new_aubio_peakpicker(threshold);
  118. o = new_aubio_onsetdetection(type_onset,buffer_size,channels);
  119. onset = new_fvec(1, channels);
  120.  
  121. //////// PORTAUDIO //////////////////
  122. Audio *audio = new Audio();
  123.  
  124. audio->inDev->device = Pa_GetDefaultInputDevice();
  125. audio->inDev->channelCount = channels;
  126. audio->inDev->sampleFormat = PA_SAMPLE_TYPE;
  127. audio->inDev->suggestedLatency = Pa_GetDeviceInfo( audio->inDev->device )->defaultLowInputLatency;
  128. audio->inDev->hostApiSpecificStreamInfo = NULL;
  129.  
  130. Pa_OpenStream ( &audio->stream, audio->inDev, NULL, SAMPLE_RATE, FRAMES_PER_BUFFER, 0, audio->CallBack, NULL );
  131. audio->StartCallback();
  132.  
  133. printf("\n The program will run for 30 seconds \n");
  134. Sleep(30000);
  135.  
  136. return 0;
  137. }