Skip to content

Commit

Permalink
MidiBeatClockReceiver: Use the average interval over the last 24 ticks
Browse files Browse the repository at this point in the history
  • Loading branch information
Holzhaus committed Mar 14, 2021
1 parent 44b5711 commit 70d1a4f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/controllers/midi/midibeatclockreceiver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@

#include "controllers/midi/midimessage.h"

namespace {
constexpr int kPulsesPerQuarterNote = 24;
}

namespace mixxx {

MidiBeatClockReceiver::MidiBeatClockReceiver()
Expand Down Expand Up @@ -37,8 +33,22 @@ void MidiBeatClockReceiver::receive(unsigned char status, Duration timestamp) {
break;
case MidiOpCode::MIDI_TIMING_CLK:
if (m_lastTimestamp != Duration::empty() && timestamp != Duration::empty()) {
Duration interval = timestamp - m_lastTimestamp;
m_bpm = Bpm(1000000000.0 / interval.toDoubleNanos() / kPulsesPerQuarterNote * 60.0);
m_intervalRingBuffer[m_clockTickIndex] = timestamp - m_lastTimestamp;

int numValues = 0;
Duration sumIntervals;
for (int i = 0; i < kPulsesPerQuarterNote; i++) {
if (m_intervalRingBuffer[i] != Duration::empty()) {
sumIntervals += m_intervalRingBuffer[i];
numValues++;
}
}
DEBUG_ASSERT(numValues >= 1);

m_bpm = Bpm(1000000000.0 /
(sumIntervals.toDoubleNanos() / numValues) /
kPulsesPerQuarterNote * 60.0);
qWarning() << m_bpm;
}
m_clockTickIndex = (m_clockTickIndex + 1) % kPulsesPerQuarterNote;
m_lastTimestamp = timestamp;
Expand Down
3 changes: 3 additions & 0 deletions src/controllers/midi/midibeatclockreceiver.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ namespace mixxx {

class MidiBeatClockReceiver {
public:
static constexpr int kPulsesPerQuarterNote = 24;

MidiBeatClockReceiver();
static bool canReceiveMidiStatus(unsigned char status);
void receive(unsigned char status, Duration timestamp);
Expand All @@ -24,6 +26,7 @@ class MidiBeatClockReceiver {
bool m_isPlaying;
Duration m_lastTimestamp;
int m_clockTickIndex;
Duration m_intervalRingBuffer[kPulsesPerQuarterNote];
};

} // namespace mixxx

0 comments on commit 70d1a4f

Please sign in to comment.