annotate widgets/UnitConverter.cpp @ 889:cd59f6e1aa26

hz -> freq, and some layout rearrangement
author Chris Cannam
date Tue, 09 Dec 2014 11:45:34 +0000
parents cb3a3e20634d
children 1f2dc7fcad92
rev   line source
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@889 23 #include <QTabWidget>
Chris@885 24
Chris@885 25 #include "base/Debug.h"
Chris@885 26 #include "base/Pitch.h"
Chris@888 27 #include "base/Preferences.h"
Chris@885 28
Chris@885 29 using namespace std;
Chris@885 30
Chris@886 31 static QString pianoNotes[] = {
Chris@886 32 "C", "C# / Db", "D", "D# / Eb", "E",
Chris@886 33 "F", "F# / Gb", "G", "G# / Ab", "A", "A# / Bb", "B"
Chris@886 34 };
Chris@886 35
Chris@885 36 UnitConverter::UnitConverter(QWidget *parent) :
Chris@885 37 QDialog(parent)
Chris@885 38 {
Chris@889 39 QGridLayout *maingrid = new QGridLayout;
Chris@889 40 setLayout(maingrid);
Chris@889 41
Chris@889 42 QTabWidget *tabs = new QTabWidget;
Chris@889 43 maingrid->addWidget(tabs, 0, 0);
Chris@889 44
Chris@889 45 QDialogButtonBox *bb = new QDialogButtonBox(QDialogButtonBox::Close);
Chris@889 46 maingrid->addWidget(bb, 1, 0);
Chris@889 47 connect(bb, SIGNAL(rejected()), this, SLOT(close()));
Chris@889 48
Chris@889 49 QFrame *frame = new QFrame;
Chris@889 50 tabs->addTab(frame, tr("Pitch"));
Chris@889 51
Chris@885 52 QGridLayout *grid = new QGridLayout;
Chris@889 53 frame->setLayout(grid);
Chris@885 54
Chris@889 55 m_freq = new QDoubleSpinBox;
Chris@889 56 m_freq->setSuffix(QString(" Hz"));
Chris@889 57 m_freq->setDecimals(6);
Chris@889 58 m_freq->setMinimum(1e-3);
Chris@889 59 m_freq->setMaximum(1e6);
Chris@889 60 m_freq->setValue(440);
Chris@889 61 connect(m_freq, SIGNAL(valueChanged(double)),
Chris@889 62 this, SLOT(freqChanged(double)));
Chris@885 63
Chris@888 64 // The min and max range values for all the remaining controls are
Chris@888 65 // determined by the min and max Hz above
Chris@888 66
Chris@885 67 m_midi = new QSpinBox;
Chris@888 68 m_midi->setMinimum(-156);
Chris@888 69 m_midi->setMaximum(203);
Chris@885 70 connect(m_midi, SIGNAL(valueChanged(int)),
Chris@885 71 this, SLOT(midiChanged(int)));
Chris@885 72
Chris@885 73 m_note = new QComboBox;
Chris@886 74 for (int i = 0; i < 12; ++i) {
Chris@886 75 m_note->addItem(pianoNotes[i]);
Chris@886 76 }
Chris@886 77 connect(m_note, SIGNAL(currentIndexChanged(int)),
Chris@886 78 this, SLOT(noteChanged(int)));
Chris@885 79
Chris@885 80 m_octave = new QSpinBox;
Chris@888 81 m_octave->setMinimum(-14);
Chris@888 82 m_octave->setMaximum(15);
Chris@886 83 connect(m_octave, SIGNAL(valueChanged(int)),
Chris@886 84 this, SLOT(octaveChanged(int)));
Chris@885 85
Chris@885 86 m_cents = new QDoubleSpinBox;
Chris@885 87 m_cents->setSuffix(tr(" cents"));
Chris@885 88 m_cents->setDecimals(4);
Chris@885 89 m_cents->setMinimum(-50);
Chris@885 90 m_cents->setMaximum(50);
Chris@885 91 connect(m_cents, SIGNAL(valueChanged(double)),
Chris@885 92 this, SLOT(centsChanged(double)));
Chris@885 93
Chris@885 94 m_piano = new QSpinBox;
Chris@885 95 //!!!
Chris@885 96
Chris@886 97 int row = 1;
Chris@886 98
Chris@889 99 grid->addWidget(m_freq, row, 0, 2, 1);
Chris@889 100 grid->addWidget(new QLabel(tr("=")), row, 1, 2, 1);
Chris@885 101
Chris@889 102 grid->addWidget(new QLabel(tr("+")), row, 7, 2, 1);
Chris@889 103 grid->addWidget(m_cents, row, 8, 2, 1);
Chris@885 104
Chris@886 105 grid->addWidget(new QLabel(tr("Piano note")), row, 2, 1, 2);
Chris@886 106 grid->addWidget(m_note, row, 4);
Chris@886 107 grid->addWidget(new QLabel(tr("in octave")), row, 5);
Chris@886 108 grid->addWidget(m_octave, row, 6);
Chris@886 109
Chris@886 110 ++row;
Chris@886 111
Chris@888 112 grid->addWidget(new QLabel(tr("MIDI pitch")), row, 2, 1, 2);
Chris@886 113 grid->addWidget(m_midi, row, 4);
Chris@886 114
Chris@886 115 ++row;
Chris@885 116
Chris@888 117 grid->addWidget
Chris@888 118 (new QLabel(tr("With concert A tuning frequency at %1 Hz, and "
Chris@888 119 "middle C residing in octave %2.\n"
Chris@888 120 "(These can be changed in the application preferences.)")
Chris@888 121 .arg(Preferences::getInstance()->getTuningFrequency())
Chris@888 122 .arg(Preferences::getInstance()->getOctaveOfMiddleC())),
Chris@888 123 row, 0, 1, 9);
Chris@888 124
Chris@888 125 ++row;
Chris@888 126
Chris@888 127 grid->addWidget
Chris@888 128 (new QLabel(tr("Note that only pitches in the range 0 to 127 are valid "
Chris@888 129 "in the MIDI protocol.")),
Chris@888 130 row, 0, 1, 9);
Chris@888 131
Chris@888 132 ++row;
Chris@888 133
Chris@889 134 QFrame *frame = new QFrame;
Chris@889 135 tabs->addTab(frame, tr("Pitch"));
Chris@889 136
Chris@889 137 QGridLayout *grid = new QGridLayout;
Chris@889 138 frame->setLayout(grid);
Chris@885 139
Chris@889 140 updatePitchesFromFreq();
Chris@885 141 }
Chris@885 142
Chris@885 143 UnitConverter::~UnitConverter()
Chris@885 144 {
Chris@885 145 }
Chris@885 146
Chris@885 147 void
Chris@889 148 UnitConverter::freqChanged(double freq)
Chris@885 149 {
Chris@889 150 cerr << "freqChanged: " << freq << endl;
Chris@889 151 updatePitchesFromFreq();
Chris@885 152 }
Chris@885 153
Chris@885 154 void
Chris@885 155 UnitConverter::midiChanged(int midi)
Chris@885 156 {
Chris@885 157 cerr << "midiChanged: " << midi << endl;
Chris@889 158 double freq = Pitch::getFrequencyForPitch(m_midi->value(), m_cents->value());
Chris@889 159 cerr << "freq -> " << freq << endl;
Chris@889 160 m_freq->setValue(freq);
Chris@885 161 }
Chris@885 162
Chris@885 163 void
Chris@885 164 UnitConverter::noteChanged(int note)
Chris@885 165 {
Chris@885 166 cerr << "noteChanged: " << note << endl;
Chris@887 167 int pitch = Pitch::getPitchForNoteAndOctave(m_note->currentIndex(),
Chris@887 168 m_octave->value());
Chris@889 169 double freq = Pitch::getFrequencyForPitch(pitch, m_cents->value());
Chris@889 170 cerr << "freq -> " << freq << endl;
Chris@889 171 m_freq->setValue(freq);
Chris@885 172 }
Chris@885 173
Chris@885 174 void
Chris@885 175 UnitConverter::octaveChanged(int oct)
Chris@885 176 {
Chris@885 177 cerr << "octaveChanged: " << oct << endl;
Chris@888 178 int pitch = Pitch::getPitchForNoteAndOctave(m_note->currentIndex(),
Chris@888 179 m_octave->value());
Chris@889 180 double freq = Pitch::getFrequencyForPitch(pitch, m_cents->value());
Chris@889 181 cerr << "freq -> " << freq << endl;
Chris@889 182 m_freq->setValue(freq);
Chris@885 183 }
Chris@885 184
Chris@885 185 void
Chris@885 186 UnitConverter::centsChanged(double cents)
Chris@885 187 {
Chris@885 188 cerr << "centsChanged: " << cents << endl;
Chris@889 189 double freq = Pitch::getFrequencyForPitch(m_midi->value(), m_cents->value());
Chris@889 190 cerr << "freq -> " << freq << endl;
Chris@889 191 m_freq->setValue(freq);
Chris@885 192 }
Chris@885 193
Chris@885 194 void
Chris@885 195 UnitConverter::pianoChanged(int piano)
Chris@885 196 {
Chris@885 197 cerr << "pianoChanged: " << piano << endl;
Chris@885 198 }
Chris@885 199
Chris@885 200 void
Chris@889 201 UnitConverter::updatePitchesFromFreq()
Chris@885 202 {
Chris@885 203 float cents = 0;
Chris@889 204 int pitch = Pitch::getPitchForFrequency(m_freq->value(), &cents);
Chris@887 205 int note, octave;
Chris@887 206 Pitch::getNoteAndOctaveForPitch(pitch, note, octave);
Chris@885 207
Chris@888 208 cerr << "pitch " << pitch << " note " << note << " octave " << octave << " cents " << cents << endl;
Chris@888 209
Chris@885 210 m_midi->blockSignals(true);
Chris@885 211 m_cents->blockSignals(true);
Chris@887 212 m_note->blockSignals(true);
Chris@887 213 m_octave->blockSignals(true);
Chris@885 214
Chris@885 215 m_midi->setValue(pitch);
Chris@885 216 m_cents->setValue(cents);
Chris@887 217 m_note->setCurrentIndex(note);
Chris@887 218 m_octave->setValue(octave);
Chris@887 219
Chris@885 220 m_midi->blockSignals(false);
Chris@885 221 m_cents->blockSignals(false);
Chris@887 222 m_note->blockSignals(false);
Chris@887 223 m_octave->blockSignals(false);
Chris@885 224 }
Chris@885 225
Chris@885 226
Chris@885 227
Chris@885 228