annotate widgets/UnitConverter.cpp @ 890:1f2dc7fcad92

Oops, included some unintended cruft
author Chris Cannam
date Tue, 09 Dec 2014 11:49:27 +0000
parents cd59f6e1aa26
children 09b2940f483a
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@885 133
Chris@889 134 updatePitchesFromFreq();
Chris@885 135 }
Chris@885 136
Chris@885 137 UnitConverter::~UnitConverter()
Chris@885 138 {
Chris@885 139 }
Chris@885 140
Chris@885 141 void
Chris@889 142 UnitConverter::freqChanged(double freq)
Chris@885 143 {
Chris@889 144 cerr << "freqChanged: " << freq << endl;
Chris@889 145 updatePitchesFromFreq();
Chris@885 146 }
Chris@885 147
Chris@885 148 void
Chris@885 149 UnitConverter::midiChanged(int midi)
Chris@885 150 {
Chris@885 151 cerr << "midiChanged: " << midi << endl;
Chris@889 152 double freq = Pitch::getFrequencyForPitch(m_midi->value(), m_cents->value());
Chris@889 153 cerr << "freq -> " << freq << endl;
Chris@889 154 m_freq->setValue(freq);
Chris@885 155 }
Chris@885 156
Chris@885 157 void
Chris@885 158 UnitConverter::noteChanged(int note)
Chris@885 159 {
Chris@885 160 cerr << "noteChanged: " << note << endl;
Chris@887 161 int pitch = Pitch::getPitchForNoteAndOctave(m_note->currentIndex(),
Chris@887 162 m_octave->value());
Chris@889 163 double freq = Pitch::getFrequencyForPitch(pitch, m_cents->value());
Chris@889 164 cerr << "freq -> " << freq << endl;
Chris@889 165 m_freq->setValue(freq);
Chris@885 166 }
Chris@885 167
Chris@885 168 void
Chris@885 169 UnitConverter::octaveChanged(int oct)
Chris@885 170 {
Chris@885 171 cerr << "octaveChanged: " << oct << endl;
Chris@888 172 int pitch = Pitch::getPitchForNoteAndOctave(m_note->currentIndex(),
Chris@888 173 m_octave->value());
Chris@889 174 double freq = Pitch::getFrequencyForPitch(pitch, m_cents->value());
Chris@889 175 cerr << "freq -> " << freq << endl;
Chris@889 176 m_freq->setValue(freq);
Chris@885 177 }
Chris@885 178
Chris@885 179 void
Chris@885 180 UnitConverter::centsChanged(double cents)
Chris@885 181 {
Chris@885 182 cerr << "centsChanged: " << cents << endl;
Chris@889 183 double freq = Pitch::getFrequencyForPitch(m_midi->value(), m_cents->value());
Chris@889 184 cerr << "freq -> " << freq << endl;
Chris@889 185 m_freq->setValue(freq);
Chris@885 186 }
Chris@885 187
Chris@885 188 void
Chris@885 189 UnitConverter::pianoChanged(int piano)
Chris@885 190 {
Chris@885 191 cerr << "pianoChanged: " << piano << endl;
Chris@885 192 }
Chris@885 193
Chris@885 194 void
Chris@889 195 UnitConverter::updatePitchesFromFreq()
Chris@885 196 {
Chris@885 197 float cents = 0;
Chris@889 198 int pitch = Pitch::getPitchForFrequency(m_freq->value(), &cents);
Chris@887 199 int note, octave;
Chris@887 200 Pitch::getNoteAndOctaveForPitch(pitch, note, octave);
Chris@885 201
Chris@888 202 cerr << "pitch " << pitch << " note " << note << " octave " << octave << " cents " << cents << endl;
Chris@888 203
Chris@885 204 m_midi->blockSignals(true);
Chris@885 205 m_cents->blockSignals(true);
Chris@887 206 m_note->blockSignals(true);
Chris@887 207 m_octave->blockSignals(true);
Chris@885 208
Chris@885 209 m_midi->setValue(pitch);
Chris@885 210 m_cents->setValue(cents);
Chris@887 211 m_note->setCurrentIndex(note);
Chris@887 212 m_octave->setValue(octave);
Chris@887 213
Chris@885 214 m_midi->blockSignals(false);
Chris@885 215 m_cents->blockSignals(false);
Chris@887 216 m_note->blockSignals(false);
Chris@887 217 m_octave->blockSignals(false);
Chris@885 218 }
Chris@885 219
Chris@885 220
Chris@885 221
Chris@885 222