//This function should be called every tick/frame. This used the previous functions to //Update time, get the current spectrum and the adaptive threshold and then does a check //to see if a beat has occured. It also allows for some post-detection ignore clause. //This function also does functions such as update the smoothing median list, create a //timestamp object, update the lastBeatRegistered and checks to see if the song //is still playing bool BeatDetectorLite::update(float* left, float* right, int length) { float* stereo = (float*) malloc(length * sizeof(float)); for(int i = 0; i < length; i++) { stereo[i] = left[i] + right[i]; } if(previousFFT == NULL) { previousFFT = stereo; return false; } beatThreshold = calculateFluxAndSmoothing(stereo); //Beat detected if (specFlux > beatThreshold && (clock() - timeBetween) > 350) { smootherValues.push_back(specFlux); if (smootherValues.size() >= 5) { smootherValues.erase(smootherValues.begin()); } timeBetween = clock(); TimeStamp* t = new TimeStamp(currentMinutes, currentSeconds, currentMillis, specFlux); std::cout << "BEAT AT: " << t->getMinutes() << ":" << t->getSeconds() << ":" << t->getMilliseconds() << " -- BEAT FREQ: " << t->getFrequency() << " -- THRESHOLD: " << beatThreshold << std::endl; lastBeatRegistered = t; return true; } else if ((clock() - timeBetween) > 5000) { if (thresholdSmoother>0.4f) thresholdSmoother -= 0.4f; timeBetween = clock(); return false; } return false; }
void testTimeStamp(FILE *fd,FILE *auxfd) { assert(nanoSecPerSec==1000000000); TimeStamp current; current.getCurrent(); fprintf(auxfd,"current %lli %i milliSec %lli\n", (long long)current.getSecondsPastEpoch(), current.getNanoSeconds(), (long long)current.getMilliseconds()); time_t tt; current.toTime_t(tt); struct tm ctm; memcpy(&ctm,localtime(&tt),sizeof(struct tm)); fprintf(auxfd, "%4.4d.%2.2d.%2.2d %2.2d:%2.2d:%2.2d %d isDst %s\n", ctm.tm_year+1900,ctm.tm_mon + 1,ctm.tm_mday, ctm.tm_hour,ctm.tm_min,ctm.tm_sec, current.getNanoSeconds(), (ctm.tm_isdst==0) ? "false" : "true"); tt = time(&tt); current.fromTime_t(tt); fprintf(auxfd,"fromTime_t\ncurrent %lli %i milliSec %lli\n", (long long)current.getSecondsPastEpoch(), current.getNanoSeconds(), (long long)current.getMilliseconds()); current.toTime_t(tt); memcpy(&ctm,localtime(&tt),sizeof(struct tm)); fprintf(auxfd, "%4.4d.%2.2d.%2.2d %2.2d:%2.2d:%2.2d %d isDst %s\n", ctm.tm_year+1900,ctm.tm_mon + 1,ctm.tm_mday, ctm.tm_hour,ctm.tm_min,ctm.tm_sec, current.getNanoSeconds(), (ctm.tm_isdst==0) ? "false" : "true"); TimeStamp right; TimeStamp left; right.put(current.getSecondsPastEpoch(),current.getNanoSeconds()); left.put(current.getSecondsPastEpoch(),current.getNanoSeconds()); double diff; diff = TimeStamp::diff(left,right); if(debug) fprintf(fd,"diff %e\n",diff); assert(diff==0.0); assert((left==right)); assert(!(left!=right)); assert((left<=right)); assert(!(left<right)); assert((left>=right)); assert(!(left>right)); left.put(current.getSecondsPastEpoch()+1,current.getNanoSeconds()); diff = TimeStamp::diff(left,right); if(debug) fprintf(fd,"diff %e\n",diff); assert(diff==1.0); assert(!(left==right)); assert((left!=right)); assert(!(left<=right)); assert(!(left<right)); assert((left>=right)); assert((left>right)); left.put(current.getSecondsPastEpoch()-1,current.getNanoSeconds()); diff = TimeStamp::diff(left,right); if(debug) fprintf(fd,"diff %e\n",diff); assert(diff==-1.0); assert(!(left==right)); assert((left!=right)); assert((left<=right)); assert((left<right)); assert(!(left>=right)); assert(!(left>right)); left.put(current.getSecondsPastEpoch(),current.getNanoSeconds()-nanoSecPerSec); diff = TimeStamp::diff(left,right); if(debug) fprintf(fd,"diff %e\n",diff); assert(diff==-1.0); assert(!(left==right)); assert((left!=right)); assert((left<=right)); assert((left<right)); assert(!(left>=right)); assert(!(left>right)); left.put(current.getSecondsPastEpoch(),current.getNanoSeconds()-1); diff = TimeStamp::diff(left,right); if(debug) fprintf(fd,"diff %e\n",diff); assert(diff<0.0); assert(!(left==right)); assert((left!=right)); assert((left<=right)); assert((left<right)); assert(!(left>=right)); assert(!(left>right)); left.put(current.getSecondsPastEpoch(),current.getNanoSeconds()); left += .1; diff = TimeStamp::diff(left,right); if(debug) fprintf(fd,"diff %e\n",diff); left.put(current.getSecondsPastEpoch(),current.getNanoSeconds()); int64 inc = -1; left += inc; diff = TimeStamp::diff(left,right); assert(diff==-1.0); fprintf(fd,"PASSED\n"); }