comparison widgets/UnitConverter.cpp @ 888:cb3a3e20634d

Fixes to limits etc in unit converter
author Chris Cannam
date Mon, 08 Dec 2014 15:37:49 +0000
parents 139aad8bb445
children cd59f6e1aa26
comparison
equal deleted inserted replaced
887:139aad8bb445 888:cb3a3e20634d
21 #include <QDialogButtonBox> 21 #include <QDialogButtonBox>
22 #include <QGridLayout> 22 #include <QGridLayout>
23 23
24 #include "base/Debug.h" 24 #include "base/Debug.h"
25 #include "base/Pitch.h" 25 #include "base/Pitch.h"
26 #include "base/Preferences.h"
26 27
27 using namespace std; 28 using namespace std;
28 29
29 static QString pianoNotes[] = { 30 static QString pianoNotes[] = {
30 "C", "C# / Db", "D", "D# / Eb", "E", 31 "C", "C# / Db", "D", "D# / Eb", "E",
38 setLayout(grid); 39 setLayout(grid);
39 40
40 m_hz = new QDoubleSpinBox; 41 m_hz = new QDoubleSpinBox;
41 m_hz->setSuffix(QString(" Hz")); 42 m_hz->setSuffix(QString(" Hz"));
42 m_hz->setDecimals(6); 43 m_hz->setDecimals(6);
43 m_hz->setMinimum(1e-6); 44 m_hz->setMinimum(1e-3);
44 m_hz->setMaximum(1e6); 45 m_hz->setMaximum(1e6);
45 m_hz->setValue(440); 46 m_hz->setValue(440);
46 connect(m_hz, SIGNAL(valueChanged(double)), 47 connect(m_hz, SIGNAL(valueChanged(double)),
47 this, SLOT(hzChanged(double))); 48 this, SLOT(hzChanged(double)));
48 49
50 // The min and max range values for all the remaining controls are
51 // determined by the min and max Hz above
52
49 m_midi = new QSpinBox; 53 m_midi = new QSpinBox;
50 m_midi->setMinimum(0); 54 m_midi->setMinimum(-156);
51 m_midi->setMaximum(127); 55 m_midi->setMaximum(203);
52 connect(m_midi, SIGNAL(valueChanged(int)), 56 connect(m_midi, SIGNAL(valueChanged(int)),
53 this, SLOT(midiChanged(int))); 57 this, SLOT(midiChanged(int)));
54 58
55 m_note = new QComboBox; 59 m_note = new QComboBox;
56 for (int i = 0; i < 12; ++i) { 60 for (int i = 0; i < 12; ++i) {
58 } 62 }
59 connect(m_note, SIGNAL(currentIndexChanged(int)), 63 connect(m_note, SIGNAL(currentIndexChanged(int)),
60 this, SLOT(noteChanged(int))); 64 this, SLOT(noteChanged(int)));
61 65
62 m_octave = new QSpinBox; 66 m_octave = new QSpinBox;
63 m_octave->setMinimum(-4); 67 m_octave->setMinimum(-14);
64 m_octave->setMaximum(12); 68 m_octave->setMaximum(15);
65 connect(m_octave, SIGNAL(valueChanged(int)), 69 connect(m_octave, SIGNAL(valueChanged(int)),
66 this, SLOT(octaveChanged(int))); 70 this, SLOT(octaveChanged(int)));
67 71
68 m_cents = new QDoubleSpinBox; 72 m_cents = new QDoubleSpinBox;
69 m_cents->setSuffix(tr(" cents")); 73 m_cents->setSuffix(tr(" cents"));
89 grid->addWidget(new QLabel(tr("in octave")), row, 5); 93 grid->addWidget(new QLabel(tr("in octave")), row, 5);
90 grid->addWidget(m_octave, row, 6); 94 grid->addWidget(m_octave, row, 6);
91 95
92 ++row; 96 ++row;
93 97
94 grid->addWidget(new QLabel(tr("MIDI note")), row, 2, 1, 2); 98 grid->addWidget(new QLabel(tr("MIDI pitch")), row, 2, 1, 2);
95 grid->addWidget(m_midi, row, 4); 99 grid->addWidget(m_midi, row, 4);
96 100
97 ++row; 101 ++row;
98 102
103 grid->addWidget
104 (new QLabel(tr("With concert A tuning frequency at %1 Hz, and "
105 "middle C residing in octave %2.\n"
106 "(These can be changed in the application preferences.)")
107 .arg(Preferences::getInstance()->getTuningFrequency())
108 .arg(Preferences::getInstance()->getOctaveOfMiddleC())),
109 row, 0, 1, 9);
110
111 ++row;
112
113 grid->addWidget
114 (new QLabel(tr("Note that only pitches in the range 0 to 127 are valid "
115 "in the MIDI protocol.")),
116 row, 0, 1, 9);
117
118 ++row;
119
99 QDialogButtonBox *bb = new QDialogButtonBox(QDialogButtonBox::Close); 120 QDialogButtonBox *bb = new QDialogButtonBox(QDialogButtonBox::Close);
100 grid->addWidget(bb, row, 0, 1, 9); 121 grid->addWidget(bb, row, 0, 1, 9);
101 connect(bb, SIGNAL(rejected()), this, SLOT(close())); 122 connect(bb, SIGNAL(rejected()), this, SLOT(close()));
102 123
103 updateAllFromHz(); 124 updateAllFromHz();
116 137
117 void 138 void
118 UnitConverter::midiChanged(int midi) 139 UnitConverter::midiChanged(int midi)
119 { 140 {
120 cerr << "midiChanged: " << midi << endl; 141 cerr << "midiChanged: " << midi << endl;
121 m_hz->setValue(Pitch::getFrequencyForPitch(m_midi->value(), m_cents->value())); 142 double hz = Pitch::getFrequencyForPitch(m_midi->value(), m_cents->value());
122 updateAllFromHz(); 143 cerr << "hz -> " << hz << endl;
144 m_hz->setValue(hz);
123 } 145 }
124 146
125 void 147 void
126 UnitConverter::noteChanged(int note) 148 UnitConverter::noteChanged(int note)
127 { 149 {
128 cerr << "noteChanged: " << note << endl; 150 cerr << "noteChanged: " << note << endl;
129 int pitch = Pitch::getPitchForNoteAndOctave(m_note->currentIndex(), 151 int pitch = Pitch::getPitchForNoteAndOctave(m_note->currentIndex(),
130 m_octave->value()); 152 m_octave->value());
131 m_hz->setValue(Pitch::getFrequencyForPitch(pitch, m_cents->value())); 153 double hz = Pitch::getFrequencyForPitch(pitch, m_cents->value());
154 cerr << "hz -> " << hz << endl;
155 m_hz->setValue(hz);
132 } 156 }
133 157
134 void 158 void
135 UnitConverter::octaveChanged(int oct) 159 UnitConverter::octaveChanged(int oct)
136 { 160 {
137 cerr << "octaveChanged: " << oct << endl; 161 cerr << "octaveChanged: " << oct << endl;
162 int pitch = Pitch::getPitchForNoteAndOctave(m_note->currentIndex(),
163 m_octave->value());
164 double hz = Pitch::getFrequencyForPitch(pitch, m_cents->value());
165 cerr << "hz -> " << hz << endl;
166 m_hz->setValue(hz);
138 } 167 }
139 168
140 void 169 void
141 UnitConverter::centsChanged(double cents) 170 UnitConverter::centsChanged(double cents)
142 { 171 {
143 cerr << "centsChanged: " << cents << endl; 172 cerr << "centsChanged: " << cents << endl;
144 m_hz->setValue(Pitch::getFrequencyForPitch(m_midi->value(), m_cents->value())); 173 double hz = Pitch::getFrequencyForPitch(m_midi->value(), m_cents->value());
145 updateAllFromHz(); 174 cerr << "hz -> " << hz << endl;
175 m_hz->setValue(hz);
146 } 176 }
147 177
148 void 178 void
149 UnitConverter::pianoChanged(int piano) 179 UnitConverter::pianoChanged(int piano)
150 { 180 {
157 float cents = 0; 187 float cents = 0;
158 int pitch = Pitch::getPitchForFrequency(m_hz->value(), &cents); 188 int pitch = Pitch::getPitchForFrequency(m_hz->value(), &cents);
159 int note, octave; 189 int note, octave;
160 Pitch::getNoteAndOctaveForPitch(pitch, note, octave); 190 Pitch::getNoteAndOctaveForPitch(pitch, note, octave);
161 191
192 cerr << "pitch " << pitch << " note " << note << " octave " << octave << " cents " << cents << endl;
193
162 m_midi->blockSignals(true); 194 m_midi->blockSignals(true);
163 m_cents->blockSignals(true); 195 m_cents->blockSignals(true);
164 m_note->blockSignals(true); 196 m_note->blockSignals(true);
165 m_octave->blockSignals(true); 197 m_octave->blockSignals(true);
166 198