TimeStamp TimeStamp::operator+(const TimeStamp & t) const { int32_t sumSeconds = m_seconds + t.getSeconds(); int32_t sumMicroseconds = m_microseconds + t.getFractionalMicroseconds(); while (sumMicroseconds > 1000000L) { sumSeconds++; sumMicroseconds -= 1000000L; } return TimeStamp(sumSeconds, sumMicroseconds); }
TimeStamp TimeStamp::operator-(const TimeStamp & t) const { int32_t deltaSeconds = m_seconds - t.getSeconds(); int32_t deltaMicroseconds = m_microseconds - t.getFractionalMicroseconds(); while (deltaMicroseconds < 0) { deltaSeconds--; deltaMicroseconds += 1000000L; } return TimeStamp(deltaSeconds, deltaMicroseconds); }
//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; }