summaryrefslogtreecommitdiffhomepage
path: root/BPMDetect.cpp
diff options
context:
space:
mode:
authorRoland Reichwein <mail@reichwein.it>2025-01-04 13:48:34 +0100
committerRoland Reichwein <mail@reichwein.it>2025-01-04 13:48:34 +0100
commit39ec820c931b07bc0cec98add36f106a5965e137 (patch)
treed1f2f0feb07ff325f363e767181ffaeeab94e20b /BPMDetect.cpp
parentb2c35cdf69a9084806ac7930cf4475980d596cf6 (diff)
BPM detect
Diffstat (limited to 'BPMDetect.cpp')
-rw-r--r--BPMDetect.cpp37
1 files changed, 37 insertions, 0 deletions
diff --git a/BPMDetect.cpp b/BPMDetect.cpp
new file mode 100644
index 0000000..756747d
--- /dev/null
+++ b/BPMDetect.cpp
@@ -0,0 +1,37 @@
+#include "BPMDetect.h"
+
+#include "log.h"
+
+BPMDetect::BPMDetect(int divider):
+ m_divider{divider},
+ m_count{}
+{
+}
+
+// Strategy: evaluate last 3 valid beats
+// always skip m_divider-1 beats
+void BPMDetect::receive_event()
+{
+ // guard by divider
+ ++m_count;
+ if (m_count < m_divider) {
+ return;
+ } else {
+ m_count = 0;
+ }
+
+ // calculate bpm
+ time_point now = clock_type::now();
+
+ m_timestamps.push_back(now);
+
+ while (m_timestamps.size() > 3) {
+ m_timestamps.pop_front();
+ }
+
+ if (m_timestamps.size() == 3) {
+ int bpm = 60 * 2 / std::chrono::duration<double>(m_timestamps.back() - m_timestamps.front()).count();
+
+ signal_bpm(bpm);
+ }
+}