annotate widgets/UnitConverter.cpp @ 885:6778a82d6b76

Adding the unit converter code might have enhanced the previous commit a little
author Chris Cannam
date Tue, 02 Dec 2014 13:30:24 +0000
parents
children 01cceacee3da
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@885 26
Chris@885 27 using namespace std;
Chris@885 28
Chris@885 29 UnitConverter::UnitConverter(QWidget *parent) :
Chris@885 30 QDialog(parent)
Chris@885 31 {
Chris@885 32 QGridLayout *grid = new QGridLayout;
Chris@885 33 setLayout(grid);
Chris@885 34
Chris@885 35 m_hz = new QDoubleSpinBox;
Chris@885 36 m_hz->setSuffix(QString(" Hz"));
Chris@885 37 m_hz->setDecimals(6);
Chris@885 38 m_hz->setMinimum(1e-6);
Chris@885 39 m_hz->setMaximum(1e6);
Chris@885 40 m_hz->setValue(440);
Chris@885 41 connect(m_hz, SIGNAL(valueChanged(double)),
Chris@885 42 this, SLOT(hzChanged(double)));
Chris@885 43
Chris@885 44 m_midi = new QSpinBox;
Chris@885 45 m_midi->setMinimum(0);
Chris@885 46 m_midi->setMaximum(127);
Chris@885 47 connect(m_midi, SIGNAL(valueChanged(int)),
Chris@885 48 this, SLOT(midiChanged(int)));
Chris@885 49
Chris@885 50 m_note = new QComboBox;
Chris@885 51 //!!!
Chris@885 52
Chris@885 53 m_octave = new QSpinBox;
Chris@885 54 //!!!
Chris@885 55
Chris@885 56 m_cents = new QDoubleSpinBox;
Chris@885 57 m_cents->setSuffix(tr(" cents"));
Chris@885 58 m_cents->setDecimals(4);
Chris@885 59 m_cents->setMinimum(-50);
Chris@885 60 m_cents->setMaximum(50);
Chris@885 61 connect(m_cents, SIGNAL(valueChanged(double)),
Chris@885 62 this, SLOT(centsChanged(double)));
Chris@885 63
Chris@885 64 m_piano = new QSpinBox;
Chris@885 65 //!!!
Chris@885 66
Chris@885 67 grid->addWidget(m_hz, 1, 0);
Chris@885 68 grid->addWidget(new QLabel(tr("=")), 1, 1);
Chris@885 69
Chris@885 70 grid->addWidget(new QLabel(tr("MIDI note")), 1, 2, 1, 2);
Chris@885 71 grid->addWidget(m_midi, 1, 4);
Chris@885 72
Chris@885 73 grid->addWidget(new QLabel(tr("+")), 1, 5);
Chris@885 74 grid->addWidget(m_cents, 1, 6);
Chris@885 75
Chris@885 76 QDialogButtonBox *bb = new QDialogButtonBox(QDialogButtonBox::Close);
Chris@885 77 grid->addWidget(bb, 2, 0, 1, 7);
Chris@885 78 connect(bb, SIGNAL(rejected()), this, SLOT(close()));
Chris@885 79
Chris@885 80 updateAllFromHz();
Chris@885 81 }
Chris@885 82
Chris@885 83 UnitConverter::~UnitConverter()
Chris@885 84 {
Chris@885 85 }
Chris@885 86
Chris@885 87 void
Chris@885 88 UnitConverter::hzChanged(double hz)
Chris@885 89 {
Chris@885 90 cerr << "hzChanged: " << hz << endl;
Chris@885 91 updateAllFromHz();
Chris@885 92 }
Chris@885 93
Chris@885 94 void
Chris@885 95 UnitConverter::midiChanged(int midi)
Chris@885 96 {
Chris@885 97 cerr << "midiChanged: " << midi << endl;
Chris@885 98 m_hz->setValue(Pitch::getFrequencyForPitch(m_midi->value(), m_cents->value()));
Chris@885 99 updateAllFromHz();
Chris@885 100 }
Chris@885 101
Chris@885 102 void
Chris@885 103 UnitConverter::noteChanged(int note)
Chris@885 104 {
Chris@885 105 cerr << "noteChanged: " << note << endl;
Chris@885 106 }
Chris@885 107
Chris@885 108 void
Chris@885 109 UnitConverter::octaveChanged(int oct)
Chris@885 110 {
Chris@885 111 cerr << "octaveChanged: " << oct << endl;
Chris@885 112 }
Chris@885 113
Chris@885 114 void
Chris@885 115 UnitConverter::centsChanged(double cents)
Chris@885 116 {
Chris@885 117 cerr << "centsChanged: " << cents << endl;
Chris@885 118 m_hz->setValue(Pitch::getFrequencyForPitch(m_midi->value(), m_cents->value()));
Chris@885 119 updateAllFromHz();
Chris@885 120 }
Chris@885 121
Chris@885 122 void
Chris@885 123 UnitConverter::pianoChanged(int piano)
Chris@885 124 {
Chris@885 125 cerr << "pianoChanged: " << piano << endl;
Chris@885 126 }
Chris@885 127
Chris@885 128 void
Chris@885 129 UnitConverter::updateAllFromHz()
Chris@885 130 {
Chris@885 131 float cents = 0;
Chris@885 132 int pitch = Pitch::getPitchForFrequency(m_hz->value(), &cents);
Chris@885 133
Chris@885 134 m_midi->blockSignals(true);
Chris@885 135 m_cents->blockSignals(true);
Chris@885 136
Chris@885 137 m_midi->setValue(pitch);
Chris@885 138 m_cents->setValue(cents);
Chris@885 139
Chris@885 140 m_midi->blockSignals(false);
Chris@885 141 m_cents->blockSignals(false);
Chris@885 142 }
Chris@885 143
Chris@885 144
Chris@885 145
Chris@885 146