andrew@0: /* andrew@0: * AubioPitch.cpp andrew@0: * fileLoaderAndOnsetDetection andrew@0: * andrew@0: * Created by Andrew on 24/01/2012. andrew@0: * Copyright 2012 QMUL. All rights reserved. andrew@0: * andrew@0: */ andrew@0: andrew@0: #include "AubioPitch.h" andrew@0: andrew@0: andrew@0: andrew@0: //HERE I'm using doPitchDetection as it avoids the buffering problem andrew@0: //I just fill the buffer with what samples I want and get pitch returned. andrew@0: andrew@0: andrew@0: AubioPitch::AubioPitch(){ andrew@0: bufsize = 4096; andrew@0: hopsize = bufsize / 2; andrew@0: pitch = 0.0; andrew@0: andrew@0: aubio_pitchdetection_type type_pitch = aubio_pitch_yinfft;// now changed andrew@0: aubio_pitchdetection_mode mode_pitch = aubio_pitchm_freq; andrew@0: // bufsize*4 below andrew@0: pitchDetect = new_aubio_pitchdetection(bufsize, hopsize, 1, 44100., type_pitch, mode_pitch); andrew@0: andrew@0: aubio_pitchdetection_set_yinthresh(pitchDetect, 0.85); andrew@0: andrew@0: vec = (fvec_t *)new_fvec(hopsize,1);//anr changed from hopsize andrew@0: andrew@0: pos = 0; andrew@0: } andrew@0: andrew@0: AubioPitch::~AubioPitch(){ andrew@0: del_aubio_pitchdetection(pitchDetect); andrew@0: del_fvec(vec); andrew@0: aubio_cleanup(); andrew@0: //delk fvec andrew@0: andrew@0: } andrew@0: andrew@0: bool AubioPitch::processFrame(float* frame, int size){// andrew@0: //note this no longer called andrew@0: int j; andrew@0: bool pitchDetected = false; andrew@0: //smpl_t pitch; andrew@0: andrew@0: for (j=0;jpitchOutlet, pitch); andrew@0: andrew@0: // end of block loop andrew@0: pos = -1; // so it will be zero next j loop // andrew@0: } andrew@0: pos++; andrew@0: andrew@0: } andrew@0: return pitchDetected; andrew@0: andrew@0: } andrew@0: andrew@0: //anr idea of adding to buffer andrew@0: void AubioPitch::addToBuffer(float* tmpFrame, const int& n){ andrew@0: for (int j=0;j < n;j++) { andrew@0: fvec_write_sample(vec, tmpFrame[j], 0, pos); andrew@0: andrew@0: if (pos == hopsize - 1){ andrew@0: //pitch = aubio_pitchdetection(x->o,x->vec); andrew@0: //outlet_float(x->pitchOutlet, pitch); andrew@0: andrew@0: pitch = aubio_pitchdetection(pitchDetect, vec); andrew@0: pos = -1; andrew@0: } andrew@0: andrew@0: pos++; andrew@0: } andrew@0: } andrew@0: andrew@0: float AubioPitch::getPitch(){ andrew@0: //float newPitch = aubio_pitchdetection(pitchDetect, vec); andrew@0: // return newPitch; andrew@0: return pitch; andrew@0: } andrew@0: andrew@0: //this function is more useful here andrew@0: //can process a frame - by converting it into an fvec_t type used by aubio andrew@0: //but since only do this once, better for our purposes than the more iterative method above andrew@0: //we just store the float frames and when needed, call this for our pitch calculation on the whole andrew@0: //buffer - checking that length below is in fact bufsize andrew@0: andrew@0: float AubioPitch::doPitchDetection(float* frame, const int& length){ andrew@0: //fn to do pitch detection without all the buffering etc andrew@0: float newPitch = -1.0; andrew@0: if (length == bufsize){ andrew@0: fvec_t* tmpVec; andrew@0: tmpVec = new_fvec(bufsize,1); andrew@0: for (int j =0;j < length;j++){ andrew@0: fvec_write_sample(tmpVec, frame[j], 0, j); andrew@0: // printf("vec[%i] = %.3f\n", j, frame[j]); andrew@0: } andrew@0: newPitch = aubio_pitchdetection(pitchDetect, tmpVec); andrew@0: // printf("NEW PITCH FOUND %f\n", newPitch); andrew@0: } andrew@0: return newPitch; andrew@0: } andrew@0: andrew@0: andrew@0: //float AubioPitch::getPitchDetectedFromBuffer(float* frame, const int& length){ andrew@0: andrew@0: // smpl_t pitch = 0.; andrew@0: /* fvec_t * buf; andrew@0: for (int i = 0;i < length;i++){ andrew@0: buf[i] = frame[i]; andrew@0: } andrew@0: */ andrew@0: andrew@0: /* if (length == bufsize){ andrew@0: pitch = aubio_pitchyinfft_detect(pitchDetect->yinfft, buf, pitchDetect->yinthres); andrew@0: andrew@0: if (pitch > 0){ andrew@0: pitch = pitchDetect->srate/(pitch + 0.); andrew@0: } else { andrew@0: pitch = 0.; andrew@0: } andrew@0: andrew@0: }//end if right size andrew@0: */ andrew@0: // return pitch; andrew@0: andrew@0: //} andrew@0: andrew@0: andrew@0: andrew@0: andrew@0: andrew@0: andrew@0: /*This is the fvec_t defn from aubio: andrew@0: AUBIO f_vec andrew@0: struct _fvec_t { andrew@0: ba_uint_t length; /**< length of buffer andrew@0: ba_uint_t channels; /**< number of channels andrew@0: smpl_t **data; /**< data array of size [length] * [channels] andrew@0: */