1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
|
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
/*
Sonic Visualiser
An audio file viewer and annotation editor.
Centre for Digital Music, Queen Mary, University of London.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version. See the file
COPYING included with this distribution for more information.
*/
#include "UnitConverter.h"
#include <QSpinBox>
#include <QComboBox>
#include <QDoubleSpinBox>
#include <QLabel>
#include <QDialogButtonBox>
#include <QGridLayout>
#include <QTabWidget>
#include "base/Debug.h"
#include "base/Pitch.h"
#include "base/Preferences.h"
using namespace std;
namespace sv {
static QString pianoNotes[] = {
"C", "C# / Db", "D", "D# / Eb", "E",
"F", "F# / Gb", "G", "G# / Ab", "A", "A# / Bb", "B"
};
UnitConverter::UnitConverter(QWidget *parent) :
QDialog(parent)
{
QGridLayout *maingrid = new QGridLayout;
setLayout(maingrid);
QTabWidget *tabs = new QTabWidget;
maingrid->addWidget(tabs, 0, 0);
QDialogButtonBox *bb = new QDialogButtonBox(QDialogButtonBox::Close);
maingrid->addWidget(bb, 1, 0);
connect(bb, SIGNAL(rejected()), this, SLOT(close()));
QFrame *frame = new QFrame;
tabs->addTab(frame, tr("Pitch"));
QGridLayout *grid = new QGridLayout;
frame->setLayout(grid);
m_freq = new QDoubleSpinBox;
m_freq->setSuffix(QString(" Hz"));
m_freq->setDecimals(6);
m_freq->setMinimum(1e-3);
m_freq->setMaximum(1e6);
m_freq->setValue(440);
connect(m_freq, SIGNAL(valueChanged(double)),
this, SLOT(freqChanged()));
// The min and max range values for all the remaining controls are
// determined by the min and max Hz above
m_midi = new QSpinBox;
m_midi->setMinimum(-156);
m_midi->setMaximum(203);
connect(m_midi, SIGNAL(valueChanged(int)),
this, SLOT(midiChanged()));
m_note = new QComboBox;
for (int i = 0; i < 12; ++i) {
m_note->addItem(pianoNotes[i]);
}
connect(m_note, SIGNAL(currentIndexChanged(int)),
this, SLOT(noteChanged()));
m_octave = new QSpinBox;
m_octave->setMinimum(-14);
m_octave->setMaximum(15);
connect(m_octave, SIGNAL(valueChanged(int)),
this, SLOT(octaveChanged()));
m_cents = new QDoubleSpinBox;
m_cents->setSuffix(tr(" cents"));
m_cents->setDecimals(4);
m_cents->setMinimum(-50);
m_cents->setMaximum(50);
connect(m_cents, SIGNAL(valueChanged(double)),
this, SLOT(centsChanged()));
int row = 0;
grid->addWidget(new QLabel(tr("In 12-tone Equal Temperament:")), row, 0, 1, 9);
++row;
grid->setRowMinimumHeight(row, 8);
++row;
grid->addWidget(m_freq, row, 0, 2, 1, Qt::AlignRight | Qt::AlignVCenter);
grid->addWidget(new QLabel(tr("=")), row, 1, 2, 1, Qt::AlignHCenter | Qt::AlignVCenter);
grid->addWidget(new QLabel(tr("+")), row, 7, 2, 1, Qt::AlignHCenter | Qt::AlignVCenter);
grid->addWidget(m_cents, row, 8, 2, 1, Qt::AlignLeft | Qt::AlignVCenter);
grid->addWidget(new QLabel(tr("Piano note")), row, 2, 1, 2);
grid->addWidget(m_note, row, 4);
grid->addWidget(new QLabel(tr("in octave")), row, 5);
grid->addWidget(m_octave, row, 6);
++row;
grid->addWidget(new QLabel(tr("MIDI pitch")), row, 2, 1, 2);
grid->addWidget(m_midi, row, 4);
++row;
grid->setRowStretch(row, 20);
grid->setRowMinimumHeight(row, 8);
++row;
m_pitchPrefsLabel = new QLabel;
grid->addWidget(m_pitchPrefsLabel, row, 0, 1, 9);
++row;
grid->addWidget
(new QLabel(tr("Note that only pitches in the range 0 to 127 are valid "
"in the MIDI protocol.")),
row, 0, 1, 9);
++row;
frame = new QFrame;
tabs->addTab(frame, tr("Tempo"));
grid = new QGridLayout;
frame->setLayout(grid);
m_samples = new QDoubleSpinBox;
m_samples->setSuffix(QString(" samples"));
m_samples->setDecimals(2);
m_samples->setMinimum(1);
m_samples->setMaximum(1e8);
m_samples->setValue(22050);
connect(m_samples, SIGNAL(valueChanged(double)),
this, SLOT(samplesChanged()));
m_period = new QDoubleSpinBox;
m_period->setSuffix(QString(" ms"));
m_period->setDecimals(4);
m_period->setMinimum(1e-3);
m_period->setMaximum(100000);
m_period->setValue(500);
connect(m_period, SIGNAL(valueChanged(double)),
this, SLOT(periodChanged()));
m_bpm = new QDoubleSpinBox;
m_bpm->setSuffix(QString(" bpm"));
m_bpm->setDecimals(4);
m_bpm->setMinimum(0.1);
m_bpm->setMaximum(1e6);
m_bpm->setValue(120);
connect(m_bpm, SIGNAL(valueChanged(double)),
this, SLOT(bpmChanged()));
m_tempofreq = new QDoubleSpinBox;
m_tempofreq->setSuffix(QString(" beats/sec"));
m_tempofreq->setDecimals(4);
m_tempofreq->setMinimum(1e-3);
m_tempofreq->setMaximum(1e5);
m_tempofreq->setValue(0.5);
connect(m_tempofreq, SIGNAL(valueChanged(double)),
this, SLOT(tempofreqChanged()));
m_samplerate = new QComboBox;
QList<int> rates;
rates << 8000;
for (int i = 1; i <= 16; i *= 2) {
rates << 11025 * i << 12000 * i;
}
foreach (int r, rates) {
m_samplerate->addItem(QString("%1 Hz").arg(r));
}
connect(m_samplerate, SIGNAL(currentIndexChanged(int)),
this, SLOT(samplerateChanged()));
m_samplerate->setCurrentText("44100 Hz");
connect(Preferences::getInstance(),
SIGNAL(propertyChanged(PropertyContainer::PropertyName)),
this, SLOT(preferenceChanged(PropertyContainer::PropertyName)));
row = 0;
grid->setRowStretch(row, 20);
grid->setRowMinimumHeight(row, 8);
++row;
grid->addWidget(new QLabel(tr("Beat period")), row, 0, 2, 1, Qt::AlignVCenter);
grid->addWidget(m_period, row, 1);
grid->addWidget(new QLabel(tr("=")), row, 2, 2, 1, Qt::AlignVCenter);
grid->addWidget(m_tempofreq, row, 3);
grid->addWidget(new QLabel(tr("at")), row, 4, 2, 1, Qt::AlignVCenter);
grid->addWidget(m_samplerate, row, 5, 2, 1, Qt::AlignVCenter);
++row;
grid->addWidget(m_samples, row, 1);
grid->addWidget(m_bpm, row, 3);
++row;
grid->setRowStretch(row, 20);
grid->setRowMinimumHeight(row, 8);
updatePitchesFromFreq();
updatePitchPrefsLabel();
updateTempiFromSamples();
}
UnitConverter::~UnitConverter()
{
}
void
UnitConverter::setTo(QSpinBox *box, int value)
{
box->blockSignals(true);
if (value < box->minimum() || value > box->maximum()) {
QPalette p;
p.setColor(QPalette::Text, Qt::red);
box->setPalette(p);
} else {
box->setPalette(QPalette());
}
box->setValue(value);
box->blockSignals(false);
}
void
UnitConverter::setTo(QDoubleSpinBox *box, double value)
{
box->blockSignals(true);
if (value < box->minimum() || value > box->maximum()) {
QPalette p;
p.setColor(QPalette::Text, Qt::red);
box->setPalette(p);
} else {
box->setPalette(QPalette());
}
box->setValue(value);
box->blockSignals(false);
}
void
UnitConverter::preferenceChanged(PropertyContainer::PropertyName)
{
updatePitchesFromFreq();
updatePitchPrefsLabel();
}
void
UnitConverter::updatePitchPrefsLabel()
{
m_pitchPrefsLabel->setText
(tr("With concert-A tuning frequency at %1 Hz, and "
"middle C residing in octave %2.\n"
"(These can be changed in the application preferences.)")
.arg(Preferences::getInstance()->getTuningFrequency())
.arg(Preferences::getInstance()->getOctaveOfMiddleC()));
}
void
UnitConverter::freqChanged()
{
updatePitchesFromFreq();
}
void
UnitConverter::midiChanged()
{
double freq = Pitch::getFrequencyForPitch(m_midi->value(), m_cents->value());
m_freq->setValue(freq);
}
void
UnitConverter::noteChanged()
{
int pitch = Pitch::getPitchForNoteAndOctave(m_note->currentIndex(),
m_octave->value());
double freq = Pitch::getFrequencyForPitch(pitch, m_cents->value());
m_freq->setValue(freq);
}
void
UnitConverter::octaveChanged()
{
int pitch = Pitch::getPitchForNoteAndOctave(m_note->currentIndex(),
m_octave->value());
double freq = Pitch::getFrequencyForPitch(pitch, m_cents->value());
m_freq->setValue(freq);
}
void
UnitConverter::centsChanged()
{
double freq = Pitch::getFrequencyForPitch(m_midi->value(), m_cents->value());
m_freq->setValue(freq);
}
void
UnitConverter::updatePitchesFromFreq()
{
double cents = 0;
int pitch = Pitch::getPitchForFrequency(m_freq->value(), ¢s);
int note, octave;
Pitch::getNoteAndOctaveForPitch(pitch, note, octave);
// cerr << "pitch " << pitch << " note " << note << " octave " << octave << " cents " << cents << endl;
setTo(m_midi, pitch);
setTo(m_cents, cents);
setTo(m_octave, octave);
m_note->blockSignals(true);
m_note->setCurrentIndex(note);
m_note->blockSignals(false);
}
void
UnitConverter::samplesChanged()
{
updateTempiFromSamples();
}
void
UnitConverter::periodChanged()
{
double rate = getSampleRate();
double sec = m_period->value() / 1000.0;
double samples = rate * sec;
m_samples->setValue(samples);
}
void
UnitConverter::bpmChanged()
{
double rate = getSampleRate();
double sec = 60.0 / m_bpm->value();
double samples = rate * sec;
m_samples->setValue(samples);
}
void
UnitConverter::tempofreqChanged()
{
double rate = getSampleRate();
double samples = rate / m_tempofreq->value();
m_samples->setValue(samples);
}
void
UnitConverter::samplerateChanged()
{
// Preserve the beat period in seconds, here, not in samples
periodChanged();
}
double
UnitConverter::getSampleRate()
{
return double(atoi(m_samplerate->currentText().toLocal8Bit().data()));
}
void
UnitConverter::updateTempiFromSamples()
{
double samples = m_samples->value();
double rate = getSampleRate();
// cerr << samples << " samples at rate " << rate << endl;
double sec = samples / rate;
double hz = rate / samples;
double bpm = 60.0 / sec;
setTo(m_bpm, bpm);
setTo(m_period, sec * 1000.0);
setTo(m_tempofreq, hz);
}
} // end namespace sv
|