Permalink
Browse files

Add primitive support for guessing BPM.

  • Loading branch information...
1 parent d35834f commit 6bc936358b25f11cf340df27191c1e9046d3fe25 @ddunbar committed Oct 27, 2011
Showing with 28 additions and 1 deletion.
  1. +19 −1 LightManager.cpp
  2. +2 −0 LightManager.h
  3. +7 −0 SimLightController.cpp
View
@@ -21,6 +21,9 @@ namespace {
LightProgram *ActiveProgram;
bool ChangeProgramRequested;
+ double RecentBeatTimes[128];
+ unsigned RecentBeatPosition, NumRecentBeatTimes;
+
protected:
virtual LightController &GetController() const {
return *Controller;
@@ -35,7 +38,10 @@ namespace {
: Controller(Controller_),
LightSetup(LightSetup_),
ActiveProgram(0),
- ChangeProgramRequested(false)
+ ChangeProgramRequested(false),
+ RecentBeatTimes(),
+ RecentBeatPosition(0),
+ NumRecentBeatTimes(sizeof(RecentBeatTimes) / sizeof(RecentBeatTimes[0]))
{
// Load the list of all programs.
std::vector<LightProgram *> AllPrograms;
@@ -68,6 +74,10 @@ namespace {
}
virtual void HandleBeat(MusicMonitorHandler::BeatKind Kind, double Time) {
+ RecentBeatTimes[RecentBeatPosition % NumRecentBeatTimes] =
+ get_elapsed_time_in_seconds();
+ ++RecentBeatPosition;
+
Controller->BeatNotification(Kind, Time);
MaybeSwitchPrograms();
@@ -115,6 +125,14 @@ namespace {
ActiveProgram->GetName().c_str());
}
}
+
+ virtual double GetRecentBPM() const {
+ unsigned NumBeats = std::min(RecentBeatPosition, NumRecentBeatTimes);
+ double OldestTime = RecentBeatTimes[
+ (RecentBeatPosition + 1) % NumRecentBeatTimes];
+
+ return 60 * NumBeats / (get_elapsed_time_in_seconds() - OldestTime);
+ }
};
}
View
@@ -32,6 +32,8 @@ class LightManager : public MusicMonitorHandler {
virtual void SetLight(unsigned Index, bool Enable) = 0;
virtual const LightState &GetLightState(unsigned Index) const = 0;
+
+ virtual double GetRecentBPM() const = 0;
};
LightManager *CreateLightManager(LightController *Controller,
View
@@ -197,6 +197,13 @@ void GLUTSimLightController::draw() {
glutDrawString(10, 10 + textHeight*y++, buffer);
}
+ if (light_manager) {
+ double bpm = light_manager->GetRecentBPM();
+ sprintf(buffer, "BPM: %.4fs\n", bpm);
+ glColor3f(1, 1, 1);
+ glutDrawString(10, 10 + textHeight*y++, buffer);
+ }
+
glFlush();
glutSwapBuffers();

0 comments on commit 6bc9363

Please sign in to comment.