-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdisplayer.cpp
More file actions
89 lines (69 loc) · 2.4 KB
/
Copy pathdisplayer.cpp
File metadata and controls
89 lines (69 loc) · 2.4 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <QMainWindow>
#include <QGridLayout>
#include <QLabel>
#include <QWidget>
#include <QBoxLayout>
#include <iostream>
#include <memory>
#include "displayer.hpp"
#include "audio_input.hpp"
#include "circular_buffer.hpp"
#include "tone_utils.hpp"
#include "config.hpp"
Displayer::Displayer(std::shared_ptr<Config> conf, QWidget * parent) : QWidget(parent) {
setWindowTitle("Trubadur");
this->config = conf;
this->noteBuffer = CircularBuffer<std::string>(config->getNoteBufferSize());
auto vbox = new QVBoxLayout(this);
QFont frequencyFont("Arial", 20, QFont::Bold);
this->frequencyLabel = new QLabel("... / ...", this);
this->frequencyLabel->setMargin(20);
this->frequencyLabel->setAlignment(Qt::AlignCenter);
this->frequencyLabel->setFont(frequencyFont);
QFont toneFont("Arial", 50, QFont::Bold);
this->toneLabel = new QLabel("?", this);
this->toneLabel->setMargin(20);
this->toneLabel->setAlignment(Qt::AlignCenter);
this->toneLabel->setFont(toneFont);
vbox->setAlignment(Qt::AlignHCenter);
vbox->addWidget(this->toneLabel);
vbox->addWidget(this->frequencyLabel);
setLayout(vbox);
setMinimumSize(200, 200);
}
void Displayer::setFrequencies(double real, double desired) {
std::string displayedString = std::to_string(real) + " / " + std::to_string(desired);
this->frequencyLabel->setText(QString::fromStdString(displayedString));
}
void Displayer::setDefaultLabels() {
this->frequencyLabel->setText("... / ...");
this->toneLabel->setText("?");
}
bool Displayer::isBufferConsistent() {
try {
auto note = this->noteBuffer.getLast(1)[0];
auto buffer = this->noteBuffer.getLast(this->config->getNoteBufferSize());
for (auto n : buffer) {
if (note != n) {
return false;
}
}
return true;
} catch (std::invalid_argument& e) {
return false;
}
}
void Displayer::setNote(std::string& note) {
this->toneLabel->setText(QString::fromStdString(note));
}
void Displayer::showPitch(float number) {
std::pair<std::string, double> pitchNote = getClosestPitch(number, this->config->getConcertPitch());
std::string note = pitchNote.first;
this->noteBuffer.append(note);
if (isBufferConsistent()) {
this->setFrequencies(number, pitchNote.second);
this->setNote(pitchNote.first);
} else {
setDefaultLabels();
}
}