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