comparison widgets/UnitConverter.cpp @ 892:af63372e9002

Start connecting up tempo bits
author Chris Cannam
date Tue, 09 Dec 2014 13:53:44 +0000
parents 09b2940f483a
children 78ae34f388f6
comparison
equal deleted inserted replaced
891:09b2940f483a 892:af63372e9002
88 m_cents->setDecimals(4); 88 m_cents->setDecimals(4);
89 m_cents->setMinimum(-50); 89 m_cents->setMinimum(-50);
90 m_cents->setMaximum(50); 90 m_cents->setMaximum(50);
91 connect(m_cents, SIGNAL(valueChanged(double)), 91 connect(m_cents, SIGNAL(valueChanged(double)),
92 this, SLOT(centsChanged(double))); 92 this, SLOT(centsChanged(double)));
93
94 m_piano = new QSpinBox;
95 //!!!
96 93
97 int row = 1; 94 int row = 1;
98 95
99 grid->addWidget(m_freq, row, 0, 2, 1); 96 grid->addWidget(m_freq, row, 0, 2, 1, Qt::AlignRight | Qt::AlignVCenter);
100 grid->addWidget(new QLabel(tr("=")), row, 1, 2, 1); 97 grid->addWidget(new QLabel(tr("=")), row, 1, 2, 1, Qt::AlignHCenter | Qt::AlignVCenter);
101 98
102 grid->addWidget(new QLabel(tr("+")), row, 7, 2, 1); 99 grid->addWidget(new QLabel(tr("+")), row, 7, 2, 1, Qt::AlignHCenter | Qt::AlignVCenter);
103 grid->addWidget(m_cents, row, 8, 2, 1); 100 grid->addWidget(m_cents, row, 8, 2, 1, Qt::AlignLeft | Qt::AlignVCenter);
104 101
105 grid->addWidget(new QLabel(tr("Piano note")), row, 2, 1, 2); 102 grid->addWidget(new QLabel(tr("Piano note")), row, 2, 1, 2);
106 grid->addWidget(m_note, row, 4); 103 grid->addWidget(m_note, row, 4);
107 grid->addWidget(new QLabel(tr("in octave")), row, 5); 104 grid->addWidget(new QLabel(tr("in octave")), row, 5);
108 grid->addWidget(m_octave, row, 6); 105 grid->addWidget(m_octave, row, 6);
130 tabs->addTab(frame, tr("Tempo")); 127 tabs->addTab(frame, tr("Tempo"));
131 128
132 grid = new QGridLayout; 129 grid = new QGridLayout;
133 frame->setLayout(grid); 130 frame->setLayout(grid);
134 131
132 m_samples = new QDoubleSpinBox;
133 m_samples->setSuffix(QString(" samples"));
134 m_samples->setDecimals(3);
135 m_samples->setMinimum(2);
136 m_samples->setMaximum(1e10);
137 m_samples->setValue(22050);
138 connect(m_samples, SIGNAL(valueChanged(double)),
139 this, SLOT(samplesChanged(double)));
140
141 m_period = new QDoubleSpinBox;
142 m_period->setSuffix(QString(" ms"));
143 m_period->setDecimals(4);
144 m_period->setMinimum(1e-10);
145 m_period->setMaximum(1000);
146 m_period->setValue(500);
147 connect(m_period, SIGNAL(valueChanged(double)),
148 this, SLOT(periodChanged(double)));
149
150 m_bpm = new QDoubleSpinBox;
151 m_bpm->setSuffix(QString(" bpm"));
152 m_bpm->setDecimals(4);
153 m_bpm->setMinimum(0.1);
154 m_bpm->setMaximum(1e6);
155 m_bpm->setValue(120);
156 connect(m_bpm, SIGNAL(valueChanged(double)),
157 this, SLOT(bpmChanged(double)));
158
159 m_tempofreq = new QDoubleSpinBox;
160 m_tempofreq->setSuffix(QString(" beats/sec"));
161 m_tempofreq->setDecimals(4);
162 m_tempofreq->setMinimum(1e-4);
163 m_tempofreq->setMaximum(1e6);
164 m_tempofreq->setValue(0.5);
165 connect(m_tempofreq, SIGNAL(valueChanged(double)),
166 this, SLOT(tempofreqChanged(double)));
167
168 m_samplerate = new QComboBox;
169 QList<int> rates;
170 rates << 8000;
171 for (int i = 1; i <= 16; i *= 2) {
172 rates << 11025 * i << 12000 * i;
173 }
174 foreach (int r, rates) {
175 m_samplerate->addItem(QString("%1 Hz").arg(r));
176 }
177 connect(m_samplerate, SIGNAL(currentItemChanged(QString)),
178 this, SLOT(samplerateChanged(QString)));
179 m_samplerate->setCurrentText("44100 Hz");
180
135 connect(Preferences::getInstance(), 181 connect(Preferences::getInstance(),
136 SIGNAL(propertyChanged(PropertyContainer::PropertyName)), 182 SIGNAL(propertyChanged(PropertyContainer::PropertyName)),
137 this, SLOT(preferenceChanged(PropertyContainer::PropertyName))); 183 this, SLOT(preferenceChanged(PropertyContainer::PropertyName)));
138 184
185 row = 1;
186
187 grid->addWidget(new QLabel(tr("Beat period")), row, 0);
188 grid->addWidget(m_samples, row, 1);
189 grid->addWidget(new QLabel(tr("=")), row, 2);
190
191 grid->addWidget(new QLabel(tr("at sample rate")), row, 4);
192 grid->addWidget(m_samplerate, row, 5);
193
194 grid->addWidget(m_bpm, row, 3);
195
196 ++row;
197
198 grid->addWidget(m_period, row, 3);
199
200 ++row;
201
202 grid->addWidget(m_tempofreq, row, 3);
203
139 updatePitchesFromFreq(); 204 updatePitchesFromFreq();
140 updatePitchPrefsLabel(); 205 updatePitchPrefsLabel();
141 updateTempiFromSamples(); 206 updateTempiFromSamples();
142 } 207 }
143 208
209 cerr << "freq -> " << freq << endl; 274 cerr << "freq -> " << freq << endl;
210 m_freq->setValue(freq); 275 m_freq->setValue(freq);
211 } 276 }
212 277
213 void 278 void
214 UnitConverter::pianoChanged(int piano)
215 {
216 cerr << "pianoChanged: " << piano << endl;
217 }
218
219 void
220 UnitConverter::updatePitchesFromFreq() 279 UnitConverter::updatePitchesFromFreq()
221 { 280 {
222 float cents = 0; 281 double cents = 0;
223 int pitch = Pitch::getPitchForFrequency(m_freq->value(), &cents); 282 int pitch = Pitch::getPitchForFrequency(m_freq->value(), &cents);
224 int note, octave; 283 int note, octave;
225 Pitch::getNoteAndOctaveForPitch(pitch, note, octave); 284 Pitch::getNoteAndOctaveForPitch(pitch, note, octave);
226 285
227 cerr << "pitch " << pitch << " note " << note << " octave " << octave << " cents " << cents << endl; 286 cerr << "pitch " << pitch << " note " << note << " octave " << octave << " cents " << cents << endl;
241 m_note->blockSignals(false); 300 m_note->blockSignals(false);
242 m_octave->blockSignals(false); 301 m_octave->blockSignals(false);
243 } 302 }
244 303
245 void 304 void
305 UnitConverter::samplesChanged(double)
306 {
307 updateTempiFromSamples();
308 }
309
310 void
311 UnitConverter::periodChanged(double)
312 {
313 double rate = getSampleRate();
314 double sec = m_period->value() / 1000.0;
315 double samples = rate * sec;
316 m_samples->setValue(samples);
317 }
318
319 void
320 UnitConverter::bpmChanged(double)
321 {
322 double rate = getSampleRate();
323 double sec = 60.0 / m_bpm->value();
324 double samples = rate * sec;
325 m_samples->setValue(samples);
326 }
327
328 void
329 UnitConverter::tempofreqChanged(double)
330 {
331 double rate = getSampleRate();
332 double samples = rate / m_tempofreq->value();
333 m_samples->setValue(samples);
334 }
335
336 void
337 UnitConverter::samplerateChanged(QString)
338 {
339 updateTempiFromSamples();
340 }
341
342 double
343 UnitConverter::getSampleRate()
344 {
345 return double(atoi(m_samplerate->currentText().toLocal8Bit().data()));
346 }
347
348 void
246 UnitConverter::updateTempiFromSamples() 349 UnitConverter::updateTempiFromSamples()
247 { 350 {
351 double samples = m_samples->value();
352 double rate = getSampleRate();
353
354 cerr << samples << " samples at rate " << rate << endl;
355
356 double sec = samples / rate;
357 double hz = rate / samples;
358 double bpm = 60.0 / sec;
359
360 m_bpm->blockSignals(true);
361 m_period->blockSignals(true);
362 m_tempofreq->blockSignals(true);
363
364 m_bpm->setValue(bpm);
365 m_period->setValue(sec * 1000.0);
366 m_tempofreq->setValue(hz);
367
368 m_bpm->blockSignals(false);
369 m_period->blockSignals(false);
370 m_tempofreq->blockSignals(false);
248 } 371 }
249 372
250 373