-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTuner.cpp
More file actions
182 lines (147 loc) · 6.17 KB
/
Copy pathTuner.cpp
File metadata and controls
182 lines (147 loc) · 6.17 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
* File: Tuner.cpp
* Author: astral
*
* Created on 11 Июль 2012 г., 17:41
*/
#include "Tuner.h"
#include "Note.h"
Tuner::Tuner() {
audio = new AudioIO();
sAna = new SingleToneAnalyzer(BUF_SIZE, SAMPLE_RATE, audio);
TunerInit();
}
Tuner::Tuner(AudioIO* audio, SingleToneAnalyzer* analyzer) {
this->audio = audio;
this->sAna = analyzer;
TunerInit();
}
Tuner::~Tuner() {
sAna->stopProcess();
}
void Tuner::closeEvent(QCloseEvent *event) {
emit TunerClosed();
event->accept();
}
void Tuner::TunerInit(){
unsigned int i;
widget.setupUi(this);
for (i = 0; i < (sizeof(InstrumentTypes)/sizeof(InstrumentType_t)); i++) {
widget.TunerGuitarMenu->addItem(QString(InstrumentTypes[i].name));
}
SetStrings();
assert (connect(widget.TunerGuitarMenu, SIGNAL(currentIndexChanged(int)), this, SLOT(SetStrings())) );
QFont font;
font.setPointSize(10);
font.setWeight(75);
pointer = new QGraphicsLineItem(0, 0, 0, 0);
name = new QGraphicsTextItem("");
name->setFont(font);
scene = new QGraphicsScene(this);
widget.TunerVisual->setScene(scene);
widget.TunerVisual->setRenderHint(QPainter::Antialiasing);
widget.TunerVisual->setAlignment(Qt::AlignLeft);
scene->setSceneRect(0, 0, 235, 190); // TODO remove hardcoding
QGraphicsEllipseItem *sector = new QGraphicsEllipseItem();//scene);
scene->addItem(sector);
//sector->setPos(scene->width()/2, scene->height());
sector->setRect(-45, 20, scene->width() + 90, 2 * scene->height() - 20);
sector->setStartAngle(45 * 16);
sector->setSpanAngle(90 * 16);
scene->addItem(name);
name->setPos(scene->width()/2 - 10, 0);
QGraphicsTextItem *b = new QGraphicsTextItem("b");
QGraphicsTextItem *sh = new QGraphicsTextItem("#");
scene->addItem(b);
scene->addItem(sh);
b->setPos(0, 40); // TODO formula based pos calculations
sh->setPos(scene->width() - 10, 40);
scene->addItem(pointer);
pointer->setLine(scene->width()/2, scene->height()+10, scene->width()/2, 30);
pointer->setTransformOriginPoint(scene->width()/2, scene->height()+10);
QPushButton *button;
if (widget.gridLayout->count() > 2 &&
( button = dynamic_cast<QPushButton*>(widget.gridLayout->itemAt(1)->widget()) )) {
button->setDefault(true);
SetCurrString();
}
connect(sAna, SIGNAL(sendAnalyzerData(bool, double, int, const char*, double)), this, SLOT(Tune(bool, double, int, const char*, double)));
connect(widget.TunerDone, SIGNAL(clicked()), sAna, SLOT(stopProcess()));
sAna->startProcess();
}
void Tuner::SetStrings() {
int item = widget.TunerGuitarMenu->currentIndex();
int count = 0;
while (count < MAX_TUNE_FREQ && InstrumentTypes[item].frequences[count++]);
count--;
for (int i = 0; i < widget.gridLayout->count(); i++) {
QPushButton *button;
if (( button = dynamic_cast<QPushButton*>(widget.gridLayout->itemAt(i)->widget()) )) {
disconnect(button, SIGNAL(clicked()), this, SLOT(SetCurrString()));
}
QWidget *w = widget.gridLayout->itemAt(i)->widget();
widget.gridLayout->removeItem(widget.gridLayout->itemAt(i));
delete w;
}
for (int i = 0; i < count; i++) {
widget.gridLayout->addWidget(new QLabel(QString("String # %1:").arg(i+1)), i, 0);
QPushButton *button = new QPushButton( tr(SingleToneAnalyzer::noteName(round(SingleToneAnalyzer::freq2pitch(InstrumentTypes[item].frequences[i])))) );
widget.gridLayout->addWidget(button, i, 1);
QLabel *value = new QLabel(QString::number(InstrumentTypes[item].frequences[i]));
value->hide();
widget.gridLayout->addWidget(value, i, 2);
// Test
//if (button->text().toAscii()[0] == 'D')button->hide();
connect(button, SIGNAL(clicked()), this, SLOT(SetCurrString()));
}
}
void Tuner::SetCurrString() {
QPushButton *button = NULL;
QLabel *value = NULL;
for (int i = 0; i < widget.gridLayout->count(); i++) {
if (( button = dynamic_cast<QPushButton*>(widget.gridLayout->itemAt(i)->widget()) )) {
if (button->isDefault()) {
value = dynamic_cast<QLabel*>(widget.gridLayout->itemAt(i+1)->widget());
break;
}
}
}
if (value) {
ChangeTone(
tr(SingleToneAnalyzer::noteName(round(SingleToneAnalyzer::freq2pitch(value->text().toDouble())))),
value->text().toDouble());
}
}
void Tuner::ChangeTone(QString text, double tone) {
int pos;
name->setPlainText(text);
natural = tone;
pos = Note::FreqToOctavePos(natural);
flat = (pos > 0 ? octave_notes[pos - 1] : natural);
sharp = (pos < TOTAL_TONES - 1 ? octave_notes[pos + 1] : natural);
pointer->hide();
}
void Tuner::Tune(bool result, double freq, int pitch, const char* note, double logPower){
if (result) {
//widget.TunerFreq->setText(QString::number(freq));
//widget.TunerNote->setText(QString(note));
double delta, harmonics = freq; // Due to algorithm (probably mine) bug, detected F0 is usually in lower freq. harmonics to actual input tone
if (harmonics < natural) {
harmonics *= floor((natural/harmonics) + 0.5);
}
if ( (delta = harmonics - natural) > 0) {
delta = delta/(sharp - natural); // % deviation
delta = (delta > 1 ? 1 : delta);
} else {
delta = delta/(natural - flat); // % deviation
delta = (delta < -1 ? -1 : delta);
}
pointer->show();
QPen pen;
pen.setBrush(QColor((int)(fabs(delta)*200.), (int)((1. - fabs(delta))*200.), 0));
pointer->setPen(pen);
pointer->setRotation(delta * 45);
}
//std::cout << "Got signal. Lol! \n";
//std::cout << ", frq = " << freq << ", pitch = " << pitch << ", note = " << note << ", log = " << logPower << "\n";
}