From 39ec820c931b07bc0cec98add36f106a5965e137 Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Sat, 4 Jan 2025 13:48:34 +0100 Subject: BPM detect --- BPMDetect.cpp | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 BPMDetect.cpp (limited to 'BPMDetect.cpp') 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(m_timestamps.back() - m_timestamps.front()).count(); + + signal_bpm(bpm); + } +} -- cgit v1.2.3