#include <aubio.h>
#include <stdlib.h>
#include "portaudio/include/portaudio.h"
#include "portaudio/src/common/pa_trace.h"
#include <windows.h>
#define SAMPLE_RATE (44100)
#define PA_SAMPLE_TYPE paFloat32
#define FRAMES_PER_BUFFER (64)
typedef struct PaQaData
{
unsigned long framesLeft;
int numChannels;
int bytesPerSample;
int mode;
short sawPhase;
PaSampleFormat format;
}
PaQaData;
static int gNumNoInputs = 0;
////AUBIO///
smpl_t threshold = (smpl_t)0.3;
smpl_t silence = -90.;
aubio_pvoc_t * pv;
cvec_t * fftgrain;
uint_t buffer_size = 128; //1024;
uint_t overlap_size = 64; //512;
uint_t channels = 1;
fvec_t * ibuf;
aubio_onsetdetection_type type_onset = aubio_onset_kl;
aubio_onsetdetection_t *o;
fvec_t *onset;
aubio_pickpeak_t * parms;
int isonset = 0;
unsigned int pos = 0; /*frames%dspblocksize*/
int number = 0;
///////////////////////////////////////////////////////////////////
class Audio
{
public:
Audio();
~Audio();
void StopCallback();
void StartCallback();
PaStreamParameters *inDev, *outDev;
PaStream *stream;
PaError err;
PaQaData myData;
static int CallBack( const void *inputBuffer, void *outputBuffer,
unsigned long framesPerBuffer,
const PaStreamCallbackTimeInfo* timeInfo,
PaStreamCallbackFlags statusFlags,
void *userData );
};
Audio::Audio()
{
inDev = new PaStreamParameters;
outDev = new PaStreamParameters;
err = Pa_Initialize();
if( err != paNoError ){ return; }
}
Audio::~Audio()
{
//err = Pa_CloseStream( stream );
if( err != paNoError ){
}
Pa_Terminate();
delete inDev;
delete outDev;
}
void Audio::StopCallback()
{
err = Pa_StopStream( stream );
if( err != paNoError ){ return; }
}
void Audio::StartCallback()
{
err = Pa_StartStream( stream );
if( err != paNoError ){ return; }
}
int Audio::CallBack( const void *inputBuffer, void *outputBuffer, unsigned long framesPerBuffer,
const PaStreamCallbackTimeInfo* timeInfo, PaStreamCallbackFlags statusFlags, void *userData )
{
float *out = (float*)outputBuffer;
smpl_t *in = (smpl_t*)inputBuffer;
unsigned int i;
isonset=0;
/*
// LOOP the signal to output to test our callback
//////////////////////////////////////////
if( inputBuffer == NULL ){
for( i=0; i<framesPerBuffer; i++ ){
*out++ = 0; // left - silent
*out++ = 0; // right - silent
}
gNumNoInputs += 1;
}
else {
for( i=0; i<framesPerBuffer; i++ ){
*out++ = *in++; //left
*out++ = *in++; // right
}
}
//////////////////////////////////////////
*/
/////// AUBIO ONSET DETECTION ////////////
unsigned int f; /*frames*/
for (f=0;f<(unsigned)framesPerBuffer;f++) {
//time for fft
if (pos == overlap_size-1) {
// block loop
ibuf->data = ∈
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
for (pos = 0; pos < overlap_size; pos++){
printf("%d !ONSET DETECTED! \n", number++);
}
}
//else{}
// end of block loop
pos = -1; // so it will be zero next j loop
}
pos++;
}
return paContinue;
}
////////////////////////////////////////////////
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);
onset = new_fvec(1, channels);
///// PORTAUDIO //////////////////
Audio *audio = new Audio();
const PaDeviceInfo *pdi;
const PaDeviceInfo *pdo; *pdo;
int temp = Pa_GetDeviceCount();
printf("#################### DEVICES ###################################\n");
for(int i = 0; i < temp; i++){
pdi = Pa_GetDeviceInfo( i );
printf("# %d: %s\n", i, pdi->name);
}
printf("################################################################\n");
//pdi = Pa_GetDeviceInfo( 1 );
//audio->inDev->device = (PaDeviceIndex)1;
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;
printf("Opening Input Device : %d\n", audio->inDev->device);
//pdo = Pa_GetDeviceInfo( 3 );
//audio->outDev->device = (PaDeviceIndex)3;
audio->outDev->device = Pa_GetDefaultOutputDevice();
audio->outDev->channelCount = channels;
audio->outDev->sampleFormat = PA_SAMPLE_TYPE;
audio->outDev->suggestedLatency = Pa_GetDeviceInfo( audio->outDev->device )->defaultLowInputLatency;
audio->outDev->hostApiSpecificStreamInfo = NULL;
printf("Opening Output Device : %d\n", audio->outDev->device);
Pa_OpenStream ( &audio->stream, audio->inDev, audio->outDev, SAMPLE_RATE, FRAMES_PER_BUFFER, 0, audio->CallBack, NULL );
audio->StartCallback();
printf("\n The program will end in 10 seconds");
Sleep(10000);
return 0;
}