-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tapin.cpp
62 lines (46 loc) · 1.16 KB
/
tapin.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include "tapin.h"
#include <QDebug>
#include <algorithm>
TapIn::TapIn(QObject *parent) : QObject(parent)
{
}
void TapIn::tap()
{
qDebug() << "List" << m_times;
qDebug() << "Counter" << m_tapCounter;
if (m_tapCounter == 3) {
m_tapCounter = 0;
Q_EMIT tapCounterChanged();
qDebug() << "recording data";
m_times.push_back(m_tapTimer.elapsed());
Q_ASSERT(m_times.size() == 3);
int average = std::accumulate(m_times.begin(), m_times.end(), 0) / m_times.size();
m_bpm = 60000 / average;
Q_EMIT bpmChanged();
m_times.clear();
Q_EMIT tapStopped();
return;
}
if (m_tapCounter == 0) {
qDebug() << "Restart";
m_tapTimer.restart();
m_tapCounter++;
Q_EMIT tapCounterChanged();
return;
}
if (m_tapTimer.elapsed() != 0 && m_tapCounter < 3) {
qDebug() << "recording data";
m_times.push_back(m_tapTimer.elapsed());
m_tapCounter++;
Q_EMIT tapCounterChanged();
}
m_tapTimer.restart();
}
int TapIn::bpm() const
{
return m_bpm;
}
int TapIn::tapCounter() const
{
return m_tapCounter;
}