Files
lockstep/app/src/main/cpp/TempoController.cpp

64 lines
1.8 KiB
C++
Raw Normal View History

2026-06-14 16:26:27 +02:00
//
// Created by david on 14.06.2026.
//
#include "TempoController.h"
TempoController::TempoController(std::atomic<bool> *have_stretch_factor, std::atomic<double> *stretch_factor) :
have_stretch_factor(have_stretch_factor),
stretch_factor(stretch_factor),
lastBeatT(0.0),
lastStepT(0.0)
{
this->have_stretch_factor->store(false);
this->stretch_factor->store(1.0);
}
void TempoController::reset(std::vector<double> beatTimes) {
std::lock_guard<std::mutex> lock(mMutex);
beatTimesSec.assign(beatTimes.begin(), beatTimes.end());
lastBeatT = (0.0);
lastStepT = (0.0);
}
void TempoController::resume() {}
void TempoController::pause() {}
void TempoController::seekTo(double oldMarkerPosSec, double newMarkerPosSec) {
std::lock_guard<std::mutex> lock(mMutex);
double dt = newMarkerPosSec - oldMarkerPosSec;
lastBeatT += dt;
lastStepT += dt;
}
/**
* must be thread-safe since these are called from different threads
* @param t marker position in sec of original music time
*/
void TempoController::onBeat(double t) {
std::lock_guard<std::mutex> lock(mMutex);
2026-06-14 17:46:32 +02:00
if(lastBeatT != 0.0) dtBeat = t - lastBeatT;
2026-06-14 16:26:27 +02:00
lastBeatT = t;
2026-06-14 17:46:32 +02:00
setStretchFactor();
2026-06-14 16:26:27 +02:00
}
/**
* must be thread-safe since these are called from different threads
* @param t time in sec of accelerometer timebase
*/
void TempoController::onStep(double t) {
std::lock_guard<std::mutex> lock(mMutex);
2026-06-14 17:46:32 +02:00
if(lastStepT != 0.0) dtStep = t - lastStepT;
2026-06-14 16:26:27 +02:00
lastStepT = t;
2026-06-14 17:46:32 +02:00
setStretchFactor();
}
void TempoController::setStretchFactor() {
if(dtBeat == 0.0 || dtStep == 0.0) return;
double ratio = dtStep / dtBeat;
ratio = std::min(3.3, ratio);
ratio = std::max(0.3, ratio);
this->have_stretch_factor->store(true);
this->stretch_factor->store(ratio);
2026-06-14 16:26:27 +02:00
}