| 1 |
/* |
| 2 |
* CoachController.cpp |
| 3 |
* |
| 4 |
* Created on: 7 May 2017 |
| 5 |
* Author: jeremy |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "CoachController.h" |
| 9 |
|
| 10 |
#include <gtkmm/grid.h> |
| 11 |
#include <glibmm/main.h> |
| 12 |
#include <gtkmm/label.h> |
| 13 |
|
| 14 |
#include <chrono> |
| 15 |
#include <numeric> |
| 16 |
#include <algorithm> |
| 17 |
|
| 18 |
using namespace std::chrono; |
| 19 |
|
| 20 |
namespace Controllers |
| 21 |
{ |
| 22 |
|
| 23 |
CoachController::CoachController(Glib::RefPtr<Gtk::Builder> builder, std::shared_ptr<eXaDrumsApi::eXaDrums> drumKit) |
| 24 |
: builder(builder), drumKit(std::move(drumKit)) |
| 25 |
{ |
| 26 |
|
| 27 |
// Get all widgets |
| 28 |
{ |
| 29 |
this->builder->get_widget("CoachWindow", coachWindow); |
| 30 |
this->builder->get_widget("CoachButton", coachButton); |
| 31 |
this->builder->get_widget("CoachCloseButton", coachCloseButton); |
| 32 |
this->builder->get_widget("ClickMeter", clickMeter); |
| 33 |
this->builder->get_widget("CoachJitterMeter", jitterMeter); |
| 34 |
this->builder->get_widget("HitMeterBar", hitMeterBar); |
| 35 |
this->builder->get_widget("CoachScore", scoreLabel); |
| 36 |
} |
| 37 |
|
| 38 |
// Connect all signals |
| 39 |
{ |
| 40 |
coachButton->signal_clicked().connect([&]{ OpenRhythmCoach(); }); |
| 41 |
coachCloseButton->signal_clicked().connect([&]{ CloseRhythmCoach(); }); |
| 42 |
} |
| 43 |
|
| 44 |
scores.resize(4); |
| 45 |
std::fill(scores.begin(), scores.end(), 0); |
| 46 |
|
| 47 |
hitMeterBar->set_min_value(0.); |
| 48 |
hitMeterBar->set_max_value(1.); |
| 49 |
|
| 50 |
return; |
| 51 |
} |
| 52 |
|
| 53 |
CoachController::~CoachController() |
| 54 |
{ |
| 55 |
|
| 56 |
delete coachWindow; |
| 57 |
|
| 58 |
return; |
| 59 |
} |
| 60 |
|
| 61 |
bool CoachController::UpdateCoach() |
| 62 |
{ |
| 63 |
|
| 64 |
|
| 65 |
double clickPos = drumKit->GetClickPosition(); |
| 66 |
int bpm = drumKit->GetBpmeas(); |
| 67 |
double beatPos = clickPos * bpm; |
| 68 |
|
| 69 |
double beatFraction = beatPos - int(beatPos); |
| 70 |
beatFraction = std::min<double>(std::max<double>(0.0, beatFraction), 1.0); |
| 71 |
|
| 72 |
|
| 73 |
double clickFraction = clickPos - int(clickPos); |
| 74 |
|
| 75 |
// Update click section |
| 76 |
{ |
| 77 |
|
| 78 |
if(int(beatPos) % 2 != 0) |
| 79 |
{ |
| 80 |
this->clickMeter->set_inverted(false); |
| 81 |
} |
| 82 |
else |
| 83 |
{ |
| 84 |
this->clickMeter->set_inverted(true); |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
// Clip fraction's value |
| 89 |
|
| 90 |
this->clickMeter->set_value(100*beatFraction); |
| 91 |
} |
| 92 |
|
| 93 |
// Update LEDs states |
| 94 |
{ |
| 95 |
Gtk::Label* greenLed = nullptr; |
| 96 |
Gtk::Label* redLed = nullptr; |
| 97 |
|
| 98 |
this->builder->get_widget("CoachGreenLed", greenLed); |
| 99 |
this->builder->get_widget("CoachRedLed", redLed); |
| 100 |
|
| 101 |
double th = 0.2; |
| 102 |
|
| 103 |
if(beatFraction < th) |
| 104 |
{ |
| 105 |
greenLed->set_visible(true); |
| 106 |
} |
| 107 |
else |
| 108 |
{ |
| 109 |
greenLed->set_visible(false); |
| 110 |
} |
| 111 |
|
| 112 |
if(clickFraction < th / double(bpm)) |
| 113 |
{ |
| 114 |
redLed->set_visible(true); |
| 115 |
} |
| 116 |
else |
| 117 |
{ |
| 118 |
redLed->set_visible(false); |
| 119 |
} |
| 120 |
|
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
// Update hit meter section |
| 125 |
long long lastTrigTime = drumKit->GetLastTrigTime(); |
| 126 |
|
| 127 |
high_resolution_clock::time_point now = high_resolution_clock::now(); |
| 128 |
long long t = (long long) time_point_cast<microseconds>(now).time_since_epoch().count(); |
| 129 |
{ |
| 130 |
long long dt = (t - lastTrigTime); |
| 131 |
|
| 132 |
double fraction = std::exp(-double(dt) / 200000.0); |
| 133 |
double value = double(drumKit->GetLastTrigValue()) / 100.0; |
| 134 |
auto val = std::max(value * fraction, 0.); |
| 135 |
if(val < 0.01) |
| 136 |
val = 0.; |
| 137 |
hitMeterBar->set_value(val); |
| 138 |
} |
| 139 |
|
| 140 |
// Check accuracy |
| 141 |
{ |
| 142 |
long long lastClickTime = drumKit->GetLastClickTime(); |
| 143 |
int tempo = drumKit->GetTempo(); |
| 144 |
double measureDuration = bpm * 60e6 / double(tempo) / drumKit->GetRhythm(); // Measure duration in µs |
| 145 |
|
| 146 |
const auto dtTime = std::abs(lastTrigTime - prevLastTrigTime); |
| 147 |
const bool isComputeScore = dtTime <= measureDuration && lastTrigTime != prevLastTrigTime; |
| 148 |
|
| 149 |
// Create vector of the clicks times |
| 150 |
std::vector<long long> clicksTimes(bpm + 1); |
| 151 |
|
| 152 |
for(std::size_t i = 0; i < clicksTimes.size(); i++) |
| 153 |
{ |
| 154 |
clicksTimes[i] = lastClickTime + (long long)(i * (measureDuration / bpm)); |
| 155 |
} |
| 156 |
|
| 157 |
std::vector<int> offsets(clicksTimes.size()); |
| 158 |
std::transform(clicksTimes.begin(), clicksTimes.end(), offsets.begin(), [&](long long v) { return int(lastTrigTime - v); }); |
| 159 |
|
| 160 |
|
| 161 |
// Find the most accurate hit's error |
| 162 |
int bestHitError = *std::min_element(offsets.begin(), offsets.end(), [](int x, int y) { return std::abs(x) < std::abs(y); }); |
| 163 |
|
| 164 |
int maxError = int(measureDuration / bpm); |
| 165 |
int jitterValue = -(100 * bestHitError) / maxError + 50; |
| 166 |
jitterValue = std::clamp(jitterValue, 0, 100); |
| 167 |
|
| 168 |
int relScore = 2 * (50 - std::abs(jitterValue - 50.)); |
| 169 |
|
| 170 |
if(isComputeScore) |
| 171 |
{ |
| 172 |
scores[scoreIndex % scores.size()] = relScore; |
| 173 |
scoreIndex = (scoreIndex + 1) % scores.size(); |
| 174 |
} |
| 175 |
|
| 176 |
prevLastTrigTime = lastTrigTime; |
| 177 |
|
| 178 |
int avgScore = std::accumulate(scores.begin(), scores.end(), int{0}) / scores.size(); |
| 179 |
|
| 180 |
scoreLabel->set_text(std::to_string(avgScore)); |
| 181 |
|
| 182 |
jitterMeter->set_value(jitterValue); |
| 183 |
|
| 184 |
} |
| 185 |
|
| 186 |
return true; |
| 187 |
} |
| 188 |
|
| 189 |
void CoachController::OpenRhythmCoach() |
| 190 |
{ |
| 191 |
|
| 192 |
coachTimeout = Glib::signal_timeout().connect(sigc::mem_fun(this, &CoachController::UpdateCoach), 20); |
| 193 |
|
| 194 |
coachWindow->show(); |
| 195 |
|
| 196 |
return; |
| 197 |
} |
| 198 |
|
| 199 |
void CoachController::CloseRhythmCoach() |
| 200 |
{ |
| 201 |
|
| 202 |
coachWindow->hide(); |
| 203 |
|
| 204 |
coachTimeout.disconnect(); |
| 205 |
|
| 206 |
return; |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
} /* namespace Controllers */ |