Skip to content

Commit

Permalink
Merge pull request #1 from allejok96/tap-tempo
Browse files Browse the repository at this point in the history
High precision display
  • Loading branch information
sakertooth authored Apr 18, 2022
2 parents 6bda6b2 + 964b903 commit 786c336
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 26 deletions.
70 changes: 49 additions & 21 deletions plugins/TapTempo/TapTempo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,19 @@
#include <QLabel>
#include <QKeyEvent>
#include <QVBoxLayout>
#include <QLabel>
#include <QPushButton>

#include "embed.h"
#include "LedCheckBox.h"
#include "plugin_export.h"


double asSeconds(std::chrono::duration<double> duration)
{
return duration.count();
}


extern "C"
{
Plugin::Descriptor PLUGIN_EXPORT taptempo_plugin_descriptor =
Expand Down Expand Up @@ -73,24 +81,34 @@ QString TapTempo::nodeName() const
}

TapTempoView::TapTempoView(ToolPlugin * _tool) :
ToolPluginView(_tool), m_firstTime(), m_previousTime(), m_numTaps(0)
ToolPluginView(_tool),
m_numTaps(0)
{
setFixedSize(200, 200);
m_bpmButton = new QPushButton;
m_bpmButton->setText("0");
m_bpmButton->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

m_bpmInformation = new QLabel;
m_bpmInformation->setText(tr("Beat length:\nHz:"));

QFont font = m_bpmButton->font();
font.setPointSize(25);
m_bpmButton->setFont(font);

QWidget* labelArea = new QWidget(this);
m_msLabel = new QLabel;
m_hzLabel = new QLabel;

LedCheckBox* checkBox = new LedCheckBox(labelArea, tr("High precision"));
checkBox->setToolTip(tr("High precision"));
connect(checkBox, &LedCheckBox::toggled, [this](bool checked){ m_showDecimal = checked; updateLabels(); });

QVBoxLayout * layout = new QVBoxLayout(this);
layout->setAlignment(Qt::AlignCenter);
layout->addWidget(m_bpmButton, Qt::AlignCenter);
layout->addWidget(m_bpmInformation);
layout->addWidget(labelArea);
QHBoxLayout* labelLayout = new QHBoxLayout(labelArea);
labelLayout->addWidget(m_msLabel, 1);
labelLayout->addWidget(m_hzLabel, 1);
labelLayout->addWidget(checkBox, 0);

connect(m_bpmButton, &QPushButton::pressed, this, &TapTempoView::onBpmClick);

Expand All @@ -105,35 +123,47 @@ TapTempoView::TapTempoView(ToolPlugin * _tool) :
flags &= ~Qt::WindowMaximizeButtonHint;
parentWidget()->setWindowFlags(flags);
}

updateLabels();
}


void TapTempoView::onBpmClick()
{
auto currentTime = std::chrono::steady_clock::now();
auto distanceFromPreviousTime = std::chrono::duration_cast<std::chrono::duration<double>>(currentTime - m_previousTime).count();

if (distanceFromPreviousTime > 2.0)
if (asSeconds(currentTime - m_previousTime) > 2.0)
{
m_numTaps = 1;
m_firstTime = currentTime;
m_previousTime = currentTime;
m_bpmButton->setText("0");
m_bpmInformation->setText("Beat length: 0ms\nHz: 0");
m_bpm = 0;
updateLabels();
return;
}

auto distanceFromCurrentTime = std::chrono::duration_cast<std::chrono::duration<double>>(currentTime - m_firstTime).count();
++m_numTaps;

double hz = (m_numTaps - 1) / std::max(DBL_MIN, distanceFromCurrentTime);
double bpm = 60 * hz;
m_bpm = (m_numTaps - 1) / std::max(DBL_MIN, asSeconds(currentTime - m_firstTime)) * 60;
updateLabels();

m_bpmButton->setText(QString::number(std::round(bpm)));
m_bpmInformation->setText(tr("Beat length: ") + QString::number(distanceFromPreviousTime * 1000) + "ms"
+ "\nHz: " + QString::number(hz));
m_previousTime = currentTime;
}


void TapTempoView::updateLabels()
{
// Round the BPM before calculating Hz and ms
double bpm = m_showDecimal ? m_bpm : std::round(m_bpm);
double hz = bpm / 60;
double ms = bpm > 0 ? 1 / hz * 1000 : 0;

m_bpmButton->setText(QString::number(bpm, 'f', m_showDecimal ? 1 : 0));
m_msLabel->setText(tr("%1 ms").arg(ms, 0, 'f', m_showDecimal ? 1 : 0));
m_hzLabel->setText(tr("%1 Hz").arg(hz, 0, 'f', 4));
}


void TapTempoView::keyPressEvent(QKeyEvent* event)
{
QWidget::keyPressEvent(event);
Expand All @@ -146,8 +176,6 @@ void TapTempoView::keyPressEvent(QKeyEvent* event)
void TapTempoView::closeEvent(QCloseEvent* event)
{
m_numTaps = 0;
m_bpmButton->setText("0");
m_firstTime = std::chrono::time_point<std::chrono::steady_clock>();
m_previousTime = std::chrono::time_point<std::chrono::steady_clock>();
m_bpmInformation->setText(tr("Beat length:\nHz:"));
}
m_bpm = 0;
updateLabels();
}
15 changes: 10 additions & 5 deletions plugins/TapTempo/TapTempo.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@

#include <chrono>

#include <QLabel>
#include <QPushButton>

#include "ToolPlugin.h"
#include "ToolPluginView.h"

class QLabel;
class QPushButton;
class LedCheckBox;

class TapTempoView : public ToolPluginView
{
Q_OBJECT
Expand All @@ -48,13 +49,17 @@ class TapTempoView : public ToolPluginView

private:
void reset();
void updateLabels();

private:
std::chrono::time_point<std::chrono::steady_clock> m_firstTime;
std::chrono::time_point<std::chrono::steady_clock> m_previousTime;
int m_numTaps;
QPushButton* m_bpmButton;
QLabel* m_bpmInformation;
QLabel* m_msLabel;
QLabel* m_hzLabel;
double m_bpm = 0;
bool m_showDecimal = false;
};

class TapTempo : public ToolPlugin
Expand Down Expand Up @@ -82,4 +87,4 @@ class TapTempo : public ToolPlugin
}
};

#endif
#endif

0 comments on commit 786c336

Please sign in to comment.