Chris@885
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@885
|
2
|
Chris@885
|
3 /*
|
Chris@885
|
4 Sonic Visualiser
|
Chris@885
|
5 An audio file viewer and annotation editor.
|
Chris@885
|
6 Centre for Digital Music, Queen Mary, University of London.
|
Chris@885
|
7
|
Chris@885
|
8 This program is free software; you can redistribute it and/or
|
Chris@885
|
9 modify it under the terms of the GNU General Public License as
|
Chris@885
|
10 published by the Free Software Foundation; either version 2 of the
|
Chris@885
|
11 License, or (at your option) any later version. See the file
|
Chris@885
|
12 COPYING included with this distribution for more information.
|
Chris@885
|
13 */
|
Chris@885
|
14
|
Chris@885
|
15 #include "UnitConverter.h"
|
Chris@885
|
16
|
Chris@885
|
17 #include <QSpinBox>
|
Chris@885
|
18 #include <QComboBox>
|
Chris@885
|
19 #include <QDoubleSpinBox>
|
Chris@885
|
20 #include <QLabel>
|
Chris@885
|
21 #include <QDialogButtonBox>
|
Chris@885
|
22 #include <QGridLayout>
|
Chris@885
|
23
|
Chris@885
|
24 #include "base/Debug.h"
|
Chris@885
|
25 #include "base/Pitch.h"
|
Chris@885
|
26
|
Chris@885
|
27 using namespace std;
|
Chris@885
|
28
|
Chris@886
|
29 static QString pianoNotes[] = {
|
Chris@886
|
30 "C", "C# / Db", "D", "D# / Eb", "E",
|
Chris@886
|
31 "F", "F# / Gb", "G", "G# / Ab", "A", "A# / Bb", "B"
|
Chris@886
|
32 };
|
Chris@886
|
33
|
Chris@885
|
34 UnitConverter::UnitConverter(QWidget *parent) :
|
Chris@885
|
35 QDialog(parent)
|
Chris@885
|
36 {
|
Chris@885
|
37 QGridLayout *grid = new QGridLayout;
|
Chris@885
|
38 setLayout(grid);
|
Chris@885
|
39
|
Chris@885
|
40 m_hz = new QDoubleSpinBox;
|
Chris@885
|
41 m_hz->setSuffix(QString(" Hz"));
|
Chris@885
|
42 m_hz->setDecimals(6);
|
Chris@885
|
43 m_hz->setMinimum(1e-6);
|
Chris@885
|
44 m_hz->setMaximum(1e6);
|
Chris@885
|
45 m_hz->setValue(440);
|
Chris@885
|
46 connect(m_hz, SIGNAL(valueChanged(double)),
|
Chris@885
|
47 this, SLOT(hzChanged(double)));
|
Chris@885
|
48
|
Chris@885
|
49 m_midi = new QSpinBox;
|
Chris@885
|
50 m_midi->setMinimum(0);
|
Chris@885
|
51 m_midi->setMaximum(127);
|
Chris@885
|
52 connect(m_midi, SIGNAL(valueChanged(int)),
|
Chris@885
|
53 this, SLOT(midiChanged(int)));
|
Chris@885
|
54
|
Chris@885
|
55 m_note = new QComboBox;
|
Chris@886
|
56 for (int i = 0; i < 12; ++i) {
|
Chris@886
|
57 m_note->addItem(pianoNotes[i]);
|
Chris@886
|
58 }
|
Chris@886
|
59 connect(m_note, SIGNAL(currentIndexChanged(int)),
|
Chris@886
|
60 this, SLOT(noteChanged(int)));
|
Chris@885
|
61
|
Chris@885
|
62 m_octave = new QSpinBox;
|
Chris@886
|
63 m_octave->setMinimum(-4);
|
Chris@886
|
64 m_octave->setMaximum(12);
|
Chris@886
|
65 connect(m_octave, SIGNAL(valueChanged(int)),
|
Chris@886
|
66 this, SLOT(octaveChanged(int)));
|
Chris@885
|
67
|
Chris@885
|
68 m_cents = new QDoubleSpinBox;
|
Chris@885
|
69 m_cents->setSuffix(tr(" cents"));
|
Chris@885
|
70 m_cents->setDecimals(4);
|
Chris@885
|
71 m_cents->setMinimum(-50);
|
Chris@885
|
72 m_cents->setMaximum(50);
|
Chris@885
|
73 connect(m_cents, SIGNAL(valueChanged(double)),
|
Chris@885
|
74 this, SLOT(centsChanged(double)));
|
Chris@885
|
75
|
Chris@885
|
76 m_piano = new QSpinBox;
|
Chris@885
|
77 //!!!
|
Chris@885
|
78
|
Chris@886
|
79 int row = 1;
|
Chris@886
|
80
|
Chris@886
|
81 grid->addWidget(m_hz, row, 0);
|
Chris@886
|
82 grid->addWidget(new QLabel(tr("=")), row, 1);
|
Chris@885
|
83
|
Chris@886
|
84 grid->addWidget(new QLabel(tr("+")), row, 7);
|
Chris@886
|
85 grid->addWidget(m_cents, row, 8);
|
Chris@885
|
86
|
Chris@886
|
87 grid->addWidget(new QLabel(tr("Piano note")), row, 2, 1, 2);
|
Chris@886
|
88 grid->addWidget(m_note, row, 4);
|
Chris@886
|
89 grid->addWidget(new QLabel(tr("in octave")), row, 5);
|
Chris@886
|
90 grid->addWidget(m_octave, row, 6);
|
Chris@886
|
91
|
Chris@886
|
92 ++row;
|
Chris@886
|
93
|
Chris@886
|
94 grid->addWidget(new QLabel(tr("MIDI note")), row, 2, 1, 2);
|
Chris@886
|
95 grid->addWidget(m_midi, row, 4);
|
Chris@886
|
96
|
Chris@886
|
97 ++row;
|
Chris@885
|
98
|
Chris@885
|
99 QDialogButtonBox *bb = new QDialogButtonBox(QDialogButtonBox::Close);
|
Chris@886
|
100 grid->addWidget(bb, row, 0, 1, 9);
|
Chris@885
|
101 connect(bb, SIGNAL(rejected()), this, SLOT(close()));
|
Chris@885
|
102
|
Chris@885
|
103 updateAllFromHz();
|
Chris@885
|
104 }
|
Chris@885
|
105
|
Chris@885
|
106 UnitConverter::~UnitConverter()
|
Chris@885
|
107 {
|
Chris@885
|
108 }
|
Chris@885
|
109
|
Chris@885
|
110 void
|
Chris@885
|
111 UnitConverter::hzChanged(double hz)
|
Chris@885
|
112 {
|
Chris@885
|
113 cerr << "hzChanged: " << hz << endl;
|
Chris@885
|
114 updateAllFromHz();
|
Chris@885
|
115 }
|
Chris@885
|
116
|
Chris@885
|
117 void
|
Chris@885
|
118 UnitConverter::midiChanged(int midi)
|
Chris@885
|
119 {
|
Chris@885
|
120 cerr << "midiChanged: " << midi << endl;
|
Chris@885
|
121 m_hz->setValue(Pitch::getFrequencyForPitch(m_midi->value(), m_cents->value()));
|
Chris@885
|
122 updateAllFromHz();
|
Chris@885
|
123 }
|
Chris@885
|
124
|
Chris@885
|
125 void
|
Chris@885
|
126 UnitConverter::noteChanged(int note)
|
Chris@885
|
127 {
|
Chris@885
|
128 cerr << "noteChanged: " << note << endl;
|
Chris@887
|
129 int pitch = Pitch::getPitchForNoteAndOctave(m_note->currentIndex(),
|
Chris@887
|
130 m_octave->value());
|
Chris@887
|
131 m_hz->setValue(Pitch::getFrequencyForPitch(pitch, m_cents->value()));
|
Chris@885
|
132 }
|
Chris@885
|
133
|
Chris@885
|
134 void
|
Chris@885
|
135 UnitConverter::octaveChanged(int oct)
|
Chris@885
|
136 {
|
Chris@885
|
137 cerr << "octaveChanged: " << oct << endl;
|
Chris@885
|
138 }
|
Chris@885
|
139
|
Chris@885
|
140 void
|
Chris@885
|
141 UnitConverter::centsChanged(double cents)
|
Chris@885
|
142 {
|
Chris@885
|
143 cerr << "centsChanged: " << cents << endl;
|
Chris@885
|
144 m_hz->setValue(Pitch::getFrequencyForPitch(m_midi->value(), m_cents->value()));
|
Chris@885
|
145 updateAllFromHz();
|
Chris@885
|
146 }
|
Chris@885
|
147
|
Chris@885
|
148 void
|
Chris@885
|
149 UnitConverter::pianoChanged(int piano)
|
Chris@885
|
150 {
|
Chris@885
|
151 cerr << "pianoChanged: " << piano << endl;
|
Chris@885
|
152 }
|
Chris@885
|
153
|
Chris@885
|
154 void
|
Chris@885
|
155 UnitConverter::updateAllFromHz()
|
Chris@885
|
156 {
|
Chris@885
|
157 float cents = 0;
|
Chris@885
|
158 int pitch = Pitch::getPitchForFrequency(m_hz->value(), ¢s);
|
Chris@887
|
159 int note, octave;
|
Chris@887
|
160 Pitch::getNoteAndOctaveForPitch(pitch, note, octave);
|
Chris@885
|
161
|
Chris@885
|
162 m_midi->blockSignals(true);
|
Chris@885
|
163 m_cents->blockSignals(true);
|
Chris@887
|
164 m_note->blockSignals(true);
|
Chris@887
|
165 m_octave->blockSignals(true);
|
Chris@885
|
166
|
Chris@885
|
167 m_midi->setValue(pitch);
|
Chris@885
|
168 m_cents->setValue(cents);
|
Chris@887
|
169 m_note->setCurrentIndex(note);
|
Chris@887
|
170 m_octave->setValue(octave);
|
Chris@887
|
171
|
Chris@885
|
172 m_midi->blockSignals(false);
|
Chris@885
|
173 m_cents->blockSignals(false);
|
Chris@887
|
174 m_note->blockSignals(false);
|
Chris@887
|
175 m_octave->blockSignals(false);
|
Chris@885
|
176 }
|
Chris@885
|
177
|
Chris@885
|
178
|
Chris@885
|
179
|
Chris@885
|
180
|