1. #include <aubio.h>
  2. #include <stdlib.h>
  3. #include "portaudio/include/portaudio.h"
  4. #include "portaudio/src/common/pa_trace.h"
  5. #include <windows.h>
  6.  
  7.  
  8. #define SAMPLE_RATE (44100)
  9. #define PA_SAMPLE_TYPE paFloat32
  10. #define FRAMES_PER_BUFFER (64)
  11.  
  12. typedef struct PaQaData
  13. {
  14. unsigned long framesLeft;
  15. int numChannels;
  16. int bytesPerSample;
  17. int mode;
  18. short sawPhase;
  19. PaSampleFormat format;
  20. }
  21. PaQaData;
  22.  
  23. static int gNumNoInputs = 0;
  24.  
  25. ////AUBIO///
  26. smpl_t threshold = (smpl_t)0.3;
  27. smpl_t silence = -90.;
  28. aubio_pvoc_t * pv;
  29. cvec_t * fftgrain;
  30. uint_t buffer_size = 128; //1024;
  31. uint_t overlap_size = 64; //512;
  32. uint_t channels = 1;
  33. fvec_t * ibuf;
  34. aubio_onsetdetection_type type_onset = aubio_onset_kl;
  35. aubio_onsetdetection_t *o;
  36. fvec_t *onset;
  37. aubio_pickpeak_t * parms;
  38. int isonset = 0;
  39. unsigned int pos = 0; /*frames%dspblocksize*/
  40.  
  41. int number = 0;
  42.  
  43.  
  44.  
  45. ///////////////////////////////////////////////////////////////////
  46. class Audio
  47. {
  48. public:
  49. Audio();
  50. ~Audio();
  51.  
  52. void StopCallback();
  53. void StartCallback();
  54.  
  55. PaStreamParameters *inDev, *outDev;
  56. PaStream *stream;
  57. PaError err;
  58. PaQaData myData;
  59.  
  60. static int CallBack( const void *inputBuffer, void *outputBuffer,
  61. unsigned long framesPerBuffer,
  62. const PaStreamCallbackTimeInfo* timeInfo,
  63. PaStreamCallbackFlags statusFlags,
  64. void *userData );
  65. };
  66.  
  67.  
  68. Audio::Audio()
  69. {
  70. inDev = new PaStreamParameters;
  71. outDev = new PaStreamParameters;
  72.  
  73. err = Pa_Initialize();
  74. if( err != paNoError ){ return; }
  75. }
  76.  
  77. Audio::~Audio()
  78. {
  79. //err = Pa_CloseStream( stream );
  80. if( err != paNoError ){
  81. }
  82.  
  83. Pa_Terminate();
  84. delete inDev;
  85. delete outDev;
  86. }
  87.  
  88. void Audio::StopCallback()
  89. {
  90. err = Pa_StopStream( stream );
  91. if( err != paNoError ){ return; }
  92. }
  93.  
  94. void Audio::StartCallback()
  95. {
  96. err = Pa_StartStream( stream );
  97. if( err != paNoError ){ return; }
  98. }
  99.  
  100. int Audio::CallBack( const void *inputBuffer, void *outputBuffer, unsigned long framesPerBuffer,
  101. const PaStreamCallbackTimeInfo* timeInfo, PaStreamCallbackFlags statusFlags, void *userData )
  102. {
  103. float *out = (float*)outputBuffer;
  104. smpl_t *in = (smpl_t*)inputBuffer;
  105.  
  106. unsigned int i;
  107. isonset=0;
  108.  
  109. /*
  110. // LOOP the signal to output to test our callback
  111. //////////////////////////////////////////
  112. if( inputBuffer == NULL ){
  113. for( i=0; i<framesPerBuffer; i++ ){
  114. *out++ = 0; // left - silent
  115.   *out++ = 0; // right - silent
  116.   }
  117.   gNumNoInputs += 1;
  118. }
  119. else {
  120. for( i=0; i<framesPerBuffer; i++ ){
  121. *out++ = *in++; //left
  122.   *out++ = *in++; // right
  123.   }
  124. }
  125. //////////////////////////////////////////
  126. */
  127.  
  128.  
  129. /////// AUBIO ONSET DETECTION ////////////
  130. unsigned int f; /*frames*/
  131.  
  132. for (f=0;f<(unsigned)framesPerBuffer;f++) {
  133.  
  134. //time for fft
  135. if (pos == overlap_size-1) {
  136. // block loop
  137.  
  138. ibuf->data = &in;
  139.  
  140. aubio_pvoc_do(pv,ibuf, fftgrain);
  141. aubio_onsetdetection(o,fftgrain, onset);
  142. isonset = aubio_peakpick_pimrt(onset,parms);
  143. if (isonset){
  144. // test for silence
  145. if (aubio_silence_detection(ibuf, silence)==1)
  146. isonset=0;
  147. else
  148. for (pos = 0; pos < overlap_size; pos++){
  149. printf("%d !ONSET DETECTED! \n", number++);
  150. }
  151. }
  152. //else{}
  153. // end of block loop
  154. pos = -1; // so it will be zero next j loop
  155. }
  156. pos++;
  157. }
  158.  
  159. return paContinue;
  160. }
  161.  
  162. ////////////////////////////////////////////////
  163. int main(){
  164.  
  165. ///// AUBIO /////
  166. pv = new_aubio_pvoc(buffer_size, overlap_size, channels);
  167. ibuf = new_fvec(overlap_size, channels);
  168. fftgrain = new_cvec(buffer_size, channels);
  169. parms = new_aubio_peakpicker(threshold);
  170. o = new_aubio_onsetdetection(type_onset,buffer_size,channels);
  171. onset = new_fvec(1, channels);
  172.  
  173.  
  174. ///// PORTAUDIO //////////////////
  175. Audio *audio = new Audio();
  176.  
  177. const PaDeviceInfo *pdi;
  178. const PaDeviceInfo *pdo; *pdo;
  179.  
  180. int temp = Pa_GetDeviceCount();
  181. printf("#################### DEVICES ###################################\n");
  182. for(int i = 0; i < temp; i++){
  183. pdi = Pa_GetDeviceInfo( i );
  184. printf("# %d: %s\n", i, pdi->name);
  185. }
  186. printf("################################################################\n");
  187.  
  188. //pdi = Pa_GetDeviceInfo( 1 );
  189. //audio->inDev->device = (PaDeviceIndex)1;
  190. audio->inDev->device = Pa_GetDefaultInputDevice();
  191. audio->inDev->channelCount = channels;
  192. audio->inDev->sampleFormat = PA_SAMPLE_TYPE;
  193. audio->inDev->suggestedLatency = Pa_GetDeviceInfo( audio->inDev->device )->defaultLowInputLatency;
  194. audio->inDev->hostApiSpecificStreamInfo = NULL;
  195. printf("Opening Input Device : %d\n", audio->inDev->device);
  196.  
  197. //pdo = Pa_GetDeviceInfo( 3 );
  198. //audio->outDev->device = (PaDeviceIndex)3;
  199. audio->outDev->device = Pa_GetDefaultOutputDevice();
  200. audio->outDev->channelCount = channels;
  201. audio->outDev->sampleFormat = PA_SAMPLE_TYPE;
  202. audio->outDev->suggestedLatency = Pa_GetDeviceInfo( audio->outDev->device )->defaultLowInputLatency;
  203. audio->outDev->hostApiSpecificStreamInfo = NULL;
  204. printf("Opening Output Device : %d\n", audio->outDev->device);
  205.  
  206. Pa_OpenStream ( &audio->stream, audio->inDev, audio->outDev, SAMPLE_RATE, FRAMES_PER_BUFFER, 0, audio->CallBack, NULL );
  207. audio->StartCallback();
  208.  
  209. printf("\n The program will end in 10 seconds");
  210. Sleep(10000);
  211.  
  212.  
  213. return 0;
  214. }