File indexing completed on 2024-04-28 16:08:28

0001 // SPDX-FileCopyrightText: 2023 Mathis BrĂ¼chert <mbb@kaidan.im>
0002 //
0003 // SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0004 
0005 #include "tapin.h"
0006 
0007 #include <QDebug>
0008 
0009 #include <algorithm>
0010 
0011 TapIn::TapIn(QObject *parent) : QObject(parent)
0012 {
0013 }
0014 
0015 void TapIn::tap()
0016 {
0017     qDebug() << "List" << m_times;
0018     qDebug() << "Counter" << m_tapCounter;
0019 
0020     if (m_tapCounter == 3) {
0021         m_tapCounter = 0;
0022         Q_EMIT tapCounterChanged();
0023 
0024         qDebug() << "recording data";
0025         m_times.push_back(m_tapTimer.elapsed());
0026 
0027         Q_ASSERT(m_times.size() == 3);
0028 
0029         int average = std::accumulate(m_times.begin(), m_times.end(), 0) / m_times.size();
0030         m_bpm = 60000 / average;
0031         Q_EMIT bpmChanged();
0032 
0033         m_times.clear();
0034 
0035         Q_EMIT tapStopped();
0036         return;
0037     }
0038 
0039     if (m_tapCounter == 0) {
0040         qDebug() << "Restart";
0041         m_tapTimer.restart();
0042         m_tapCounter++;
0043         Q_EMIT tapCounterChanged();
0044 
0045         return;
0046     }
0047 
0048     if (m_tapTimer.elapsed() != 0 && m_tapCounter < 3) {
0049         qDebug() << "recording data";
0050         m_times.push_back(m_tapTimer.elapsed());
0051         m_tapCounter++;
0052         Q_EMIT tapCounterChanged();
0053     }
0054 
0055     m_tapTimer.restart();
0056 }
0057 
0058 int TapIn::bpm() const
0059 {
0060     return m_bpm;
0061 }
0062 
0063 int TapIn::tapCounter() const
0064 {
0065     return m_tapCounter;
0066 }