annotate layer/SpectrumLayer.cpp @ 278:a078aa2932cc

* Fix piano keyboard in spectrum, add pitch labels to frequency displays in measurement rect (clunkily done) and harmonic cursor in spectrum
author Chris Cannam
date Tue, 03 Jul 2007 18:47:39 +0000
parents 8acd30ed735c
children 3c402c6052f6
rev   line source
Chris@133 1
Chris@133 2 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@133 3
Chris@133 4 /*
Chris@133 5 Sonic Visualiser
Chris@133 6 An audio file viewer and annotation editor.
Chris@133 7 Centre for Digital Music, Queen Mary, University of London.
Chris@195 8 This file copyright 2006-2007 QMUL.
Chris@133 9
Chris@133 10 This program is free software; you can redistribute it and/or
Chris@133 11 modify it under the terms of the GNU General Public License as
Chris@133 12 published by the Free Software Foundation; either version 2 of the
Chris@133 13 License, or (at your option) any later version. See the file
Chris@133 14 COPYING included with this distribution for more information.
Chris@133 15 */
Chris@133 16
Chris@133 17 #include "SpectrumLayer.h"
Chris@133 18
Chris@133 19 #include "data/model/FFTModel.h"
Chris@133 20 #include "view/View.h"
Chris@153 21 #include "base/AudioLevel.h"
Chris@153 22 #include "base/Preferences.h"
Chris@167 23 #include "base/RangeMapper.h"
Chris@277 24 #include "base/Pitch.h"
Chris@254 25 #include "ColourMapper.h"
Chris@254 26
Chris@254 27 #include <QPainter>
Chris@133 28
Chris@133 29 SpectrumLayer::SpectrumLayer() :
Chris@193 30 m_originModel(0),
Chris@153 31 m_channel(-1),
Chris@153 32 m_channelSet(false),
Chris@153 33 m_windowSize(1024),
Chris@153 34 m_windowType(HanningWindow),
Chris@275 35 m_windowHopLevel(2),
Chris@275 36 m_newFFTNeeded(true)
Chris@133 37 {
Chris@153 38 Preferences *prefs = Preferences::getInstance();
Chris@153 39 connect(prefs, SIGNAL(propertyChanged(PropertyContainer::PropertyName)),
Chris@153 40 this, SLOT(preferenceChanged(PropertyContainer::PropertyName)));
Chris@153 41 setWindowType(prefs->getWindowType());
Chris@195 42
Chris@195 43 setBinScale(LogBins);
Chris@133 44 }
Chris@133 45
Chris@133 46 SpectrumLayer::~SpectrumLayer()
Chris@133 47 {
Chris@193 48 //!!! delete parent's model
Chris@193 49 // for (size_t i = 0; i < m_fft.size(); ++i) delete m_fft[i];
Chris@133 50 }
Chris@133 51
Chris@133 52 void
Chris@133 53 SpectrumLayer::setModel(DenseTimeValueModel *model)
Chris@133 54 {
Chris@193 55 if (m_originModel == model) return;
Chris@193 56 m_originModel = model;
Chris@277 57
Chris@277 58 if (m_sliceableModel) {
Chris@277 59 const Model *oldModel = m_sliceableModel;
Chris@277 60 setSliceableModel(0);
Chris@277 61 // surprised I'm allowed to delete a const pointer -- may be a
Chris@277 62 // source of future compiler rejection?
Chris@277 63 delete oldModel;
Chris@277 64 }
Chris@275 65 //!!! setupFFT();
Chris@153 66 }
Chris@153 67
Chris@153 68 void
Chris@193 69 SpectrumLayer::setupFFT()
Chris@153 70 {
Chris@193 71 FFTModel *oldFFT = dynamic_cast<FFTModel *>
Chris@193 72 (const_cast<DenseThreeDimensionalModel *>(m_sliceableModel));
Chris@153 73
Chris@193 74 if (oldFFT) {
Chris@193 75 setSliceableModel(0);
Chris@193 76 delete oldFFT;
Chris@153 77 }
Chris@153 78
Chris@193 79 FFTModel *newFFT = new FFTModel(m_originModel,
Chris@193 80 m_channel,
Chris@193 81 m_windowType,
Chris@193 82 m_windowSize,
Chris@193 83 getWindowIncrement(),
Chris@193 84 m_windowSize,
Chris@193 85 true);
Chris@153 86
Chris@193 87 setSliceableModel(newFFT);
Chris@193 88
Chris@254 89 m_biasCurve.clear();
Chris@254 90 for (size_t i = 0; i < m_windowSize; ++i) {
Chris@254 91 m_biasCurve.push_back(1.f / (float(m_windowSize)/2.f));
Chris@254 92 }
Chris@254 93
Chris@193 94 newFFT->resume();
Chris@153 95 }
Chris@153 96
Chris@153 97 void
Chris@153 98 SpectrumLayer::setChannel(int channel)
Chris@153 99 {
Chris@153 100 m_channelSet = true;
Chris@153 101
Chris@193 102 FFTModel *fft = dynamic_cast<FFTModel *>
Chris@193 103 (const_cast<DenseThreeDimensionalModel *>(m_sliceableModel));
Chris@193 104
Chris@153 105 if (m_channel == channel) {
Chris@193 106 if (fft) fft->resume();
Chris@153 107 return;
Chris@153 108 }
Chris@153 109
Chris@153 110 m_channel = channel;
Chris@153 111
Chris@275 112 //!!! if (!fft) setupFFT();
Chris@153 113
Chris@153 114 emit layerParametersChanged();
Chris@133 115 }
Chris@133 116
Chris@153 117 Layer::PropertyList
Chris@153 118 SpectrumLayer::getProperties() const
Chris@153 119 {
Chris@193 120 PropertyList list = SliceLayer::getProperties();
Chris@153 121 list.push_back("Window Size");
Chris@153 122 list.push_back("Window Increment");
Chris@153 123 return list;
Chris@153 124 }
Chris@153 125
Chris@153 126 QString
Chris@153 127 SpectrumLayer::getPropertyLabel(const PropertyName &name) const
Chris@153 128 {
Chris@153 129 if (name == "Window Size") return tr("Window Size");
Chris@153 130 if (name == "Window Increment") return tr("Window Overlap");
Chris@193 131 return SliceLayer::getPropertyLabel(name);
Chris@153 132 }
Chris@153 133
Chris@153 134 Layer::PropertyType
Chris@153 135 SpectrumLayer::getPropertyType(const PropertyName &name) const
Chris@153 136 {
Chris@193 137 if (name == "Window Size") return ValueProperty;
Chris@193 138 if (name == "Window Increment") return ValueProperty;
Chris@193 139 return SliceLayer::getPropertyType(name);
Chris@153 140 }
Chris@153 141
Chris@153 142 QString
Chris@153 143 SpectrumLayer::getPropertyGroupName(const PropertyName &name) const
Chris@153 144 {
Chris@153 145 if (name == "Window Size" ||
Chris@153 146 name == "Window Increment") return tr("Window");
Chris@193 147 return SliceLayer::getPropertyGroupName(name);
Chris@153 148 }
Chris@153 149
Chris@153 150 int
Chris@153 151 SpectrumLayer::getPropertyRangeAndValue(const PropertyName &name,
Chris@216 152 int *min, int *max, int *deflt) const
Chris@153 153 {
Chris@216 154 int val = 0;
Chris@153 155
Chris@216 156 int garbage0, garbage1, garbage2;
Chris@153 157 if (!min) min = &garbage0;
Chris@153 158 if (!max) max = &garbage1;
Chris@216 159 if (!deflt) deflt = &garbage2;
Chris@153 160
Chris@193 161 if (name == "Window Size") {
Chris@153 162
Chris@153 163 *min = 0;
Chris@254 164 *max = 15;
Chris@216 165 *deflt = 5;
Chris@153 166
Chris@216 167 val = 0;
Chris@153 168 int ws = m_windowSize;
Chris@216 169 while (ws > 32) { ws >>= 1; val ++; }
Chris@153 170
Chris@153 171 } else if (name == "Window Increment") {
Chris@153 172
Chris@153 173 *min = 0;
Chris@153 174 *max = 5;
Chris@216 175 *deflt = 2;
Chris@153 176
Chris@216 177 val = m_windowHopLevel;
Chris@153 178
Chris@153 179 } else {
Chris@193 180
Chris@216 181 val = SliceLayer::getPropertyRangeAndValue(name, min, max, deflt);
Chris@153 182 }
Chris@153 183
Chris@216 184 return val;
Chris@153 185 }
Chris@153 186
Chris@153 187 QString
Chris@153 188 SpectrumLayer::getPropertyValueLabel(const PropertyName &name,
Chris@153 189 int value) const
Chris@153 190 {
Chris@153 191 if (name == "Window Size") {
Chris@153 192 return QString("%1").arg(32 << value);
Chris@153 193 }
Chris@153 194 if (name == "Window Increment") {
Chris@153 195 switch (value) {
Chris@153 196 default:
Chris@153 197 case 0: return tr("None");
Chris@153 198 case 1: return tr("25 %");
Chris@153 199 case 2: return tr("50 %");
Chris@153 200 case 3: return tr("75 %");
Chris@153 201 case 4: return tr("87.5 %");
Chris@153 202 case 5: return tr("93.75 %");
Chris@153 203 }
Chris@153 204 }
Chris@193 205 return SliceLayer::getPropertyValueLabel(name, value);
Chris@153 206 }
Chris@153 207
Chris@167 208 RangeMapper *
Chris@167 209 SpectrumLayer::getNewPropertyRangeMapper(const PropertyName &name) const
Chris@167 210 {
Chris@193 211 return SliceLayer::getNewPropertyRangeMapper(name);
Chris@167 212 }
Chris@167 213
Chris@133 214 void
Chris@153 215 SpectrumLayer::setProperty(const PropertyName &name, int value)
Chris@133 216 {
Chris@193 217 if (name == "Window Size") {
Chris@153 218 setWindowSize(32 << value);
Chris@153 219 } else if (name == "Window Increment") {
Chris@153 220 setWindowHopLevel(value);
Chris@193 221 } else {
Chris@193 222 SliceLayer::setProperty(name, value);
Chris@153 223 }
Chris@153 224 }
Chris@153 225
Chris@153 226 void
Chris@153 227 SpectrumLayer::setWindowSize(size_t ws)
Chris@153 228 {
Chris@153 229 if (m_windowSize == ws) return;
Chris@153 230 m_windowSize = ws;
Chris@275 231 //!!! setupFFT();
Chris@275 232 m_newFFTNeeded = true;
Chris@153 233 emit layerParametersChanged();
Chris@153 234 }
Chris@153 235
Chris@153 236 void
Chris@153 237 SpectrumLayer::setWindowHopLevel(size_t v)
Chris@153 238 {
Chris@153 239 if (m_windowHopLevel == v) return;
Chris@153 240 m_windowHopLevel = v;
Chris@275 241 //!!! setupFFT();
Chris@275 242 m_newFFTNeeded = true;
Chris@153 243 emit layerParametersChanged();
Chris@153 244 }
Chris@153 245
Chris@153 246 void
Chris@153 247 SpectrumLayer::setWindowType(WindowType w)
Chris@153 248 {
Chris@153 249 if (m_windowType == w) return;
Chris@153 250 m_windowType = w;
Chris@275 251 //!!! setupFFT();
Chris@275 252 m_newFFTNeeded = true;
Chris@153 253 emit layerParametersChanged();
Chris@153 254 }
Chris@153 255
Chris@153 256 void
Chris@153 257 SpectrumLayer::preferenceChanged(PropertyContainer::PropertyName name)
Chris@153 258 {
Chris@153 259 if (name == "Window Type") {
Chris@153 260 setWindowType(Preferences::getInstance()->getWindowType());
Chris@153 261 return;
Chris@153 262 }
Chris@153 263 }
Chris@153 264
Chris@133 265 bool
Chris@248 266 SpectrumLayer::getValueExtents(float &, float &, bool &, QString &) const
Chris@133 267 {
Chris@133 268 return false;
Chris@133 269 }
Chris@133 270
Chris@254 271 float
Chris@265 272 SpectrumLayer::getXForBin(int bin, int totalBins, float w) const
Chris@265 273 {
Chris@265 274 if (!m_sliceableModel) return SliceLayer::getXForBin(bin, totalBins, w);
Chris@265 275
Chris@265 276 float sampleRate = m_sliceableModel->getSampleRate();
Chris@265 277 float binfreq = (sampleRate * bin) / (totalBins * 2);
Chris@265 278
Chris@265 279 return getXForFrequency(binfreq, w);
Chris@265 280 }
Chris@265 281
Chris@265 282 int
Chris@265 283 SpectrumLayer::getBinForX(float x, int totalBins, float w) const
Chris@265 284 {
Chris@265 285 if (!m_sliceableModel) return SliceLayer::getBinForX(x, totalBins, w);
Chris@265 286
Chris@265 287 float sampleRate = m_sliceableModel->getSampleRate();
Chris@265 288 float binfreq = getFrequencyForX(x, w);
Chris@265 289
Chris@265 290 return int((binfreq * totalBins * 2) / sampleRate);
Chris@265 291 }
Chris@265 292
Chris@265 293 float
Chris@254 294 SpectrumLayer::getFrequencyForX(float x, float w) const
Chris@254 295 {
Chris@254 296 float freq = 0;
Chris@254 297
Chris@254 298 int sampleRate = m_sliceableModel->getSampleRate();
Chris@254 299
Chris@254 300 float maxfreq = float(sampleRate) / 2;
Chris@254 301
Chris@254 302 switch (m_binScale) {
Chris@254 303
Chris@254 304 case LinearBins:
Chris@254 305 freq = ((x * maxfreq) / w);
Chris@254 306 break;
Chris@254 307
Chris@254 308 case LogBins:
Chris@254 309 freq = powf(10.f, (x * log10f(maxfreq)) / w);
Chris@254 310 break;
Chris@254 311
Chris@254 312 case InvertedLogBins:
Chris@254 313 freq = maxfreq - powf(10.f, ((w - x) * log10f(maxfreq)) / w);
Chris@254 314 break;
Chris@254 315 }
Chris@254 316
Chris@254 317 return freq;
Chris@254 318 }
Chris@254 319
Chris@254 320 float
Chris@254 321 SpectrumLayer::getXForFrequency(float freq, float w) const
Chris@254 322 {
Chris@254 323 float x = 0;
Chris@254 324
Chris@254 325 int sampleRate = m_sliceableModel->getSampleRate();
Chris@254 326
Chris@254 327 float maxfreq = float(sampleRate) / 2;
Chris@254 328
Chris@254 329 switch (m_binScale) {
Chris@254 330
Chris@254 331 case LinearBins:
Chris@254 332 x = (freq * w) / maxfreq;
Chris@254 333 break;
Chris@254 334
Chris@254 335 case LogBins:
Chris@254 336 x = (log10f(freq) * w) / log10f(maxfreq);
Chris@254 337 break;
Chris@254 338
Chris@254 339 case InvertedLogBins:
Chris@274 340 if (maxfreq == freq) x = w;
Chris@274 341 else x = w - (log10f(maxfreq - freq) * w) / log10f(maxfreq);
Chris@254 342 break;
Chris@254 343 }
Chris@254 344
Chris@254 345 return x;
Chris@254 346 }
Chris@254 347
Chris@260 348 bool
Chris@267 349 SpectrumLayer::getXScaleValue(const View *v, int x,
Chris@260 350 float &value, QString &unit) const
Chris@260 351 {
Chris@267 352 if (m_xorigins.find(v) == m_xorigins.end()) return false;
Chris@267 353 int xorigin = m_xorigins.find(v)->second;
Chris@267 354 value = getFrequencyForX(x - xorigin, v->width() - xorigin - 1);
Chris@260 355 unit = "Hz";
Chris@260 356 return true;
Chris@260 357 }
Chris@260 358
Chris@264 359 bool
Chris@274 360 SpectrumLayer::getYScaleValue(const View *v, int y,
Chris@274 361 float &value, QString &unit) const
Chris@274 362 {
Chris@274 363 value = getValueForY(y, v);
Chris@274 364
Chris@274 365 if (m_energyScale == dBScale || m_energyScale == MeterScale) {
Chris@274 366
Chris@274 367 float thresh = -80.f;
Chris@274 368
Chris@274 369 if (value > 0.f) {
Chris@274 370 value = 10.f * log10f(value);
Chris@274 371 if (value < thresh) value = thresh;
Chris@274 372 } else value = thresh;
Chris@274 373
Chris@274 374 unit = "dBV";
Chris@274 375
Chris@274 376 } else {
Chris@274 377 unit = "V";
Chris@274 378 }
Chris@274 379
Chris@274 380 return true;
Chris@274 381 }
Chris@274 382
Chris@274 383 bool
Chris@274 384 SpectrumLayer::getYScaleDifference(const View *v, int y0, int y1,
Chris@274 385 float &diff, QString &unit) const
Chris@274 386 {
Chris@274 387 bool rv = SliceLayer::getYScaleDifference(v, y0, y1, diff, unit);
Chris@274 388 if (rv && (unit == "dBV")) unit = "dB";
Chris@274 389 return rv;
Chris@274 390 }
Chris@274 391
Chris@274 392
Chris@274 393 bool
Chris@264 394 SpectrumLayer::getCrosshairExtents(View *v, QPainter &paint,
Chris@264 395 QPoint cursorPos,
Chris@264 396 std::vector<QRect> &extents) const
Chris@264 397 {
Chris@264 398 QRect vertical(cursorPos.x(), cursorPos.y(), 1, v->height() - cursorPos.y());
Chris@264 399 extents.push_back(vertical);
Chris@264 400
Chris@264 401 QRect horizontal(0, cursorPos.y(), v->width(), 12);
Chris@264 402 extents.push_back(horizontal);
Chris@264 403
Chris@278 404 int hoffset = 1;
Chris@278 405 if (m_binScale == LogBins) hoffset = 12;
Chris@278 406
Chris@278 407 QRect label(cursorPos.x(),
Chris@278 408 v->height() - paint.fontMetrics().height() - hoffset,
Chris@264 409 paint.fontMetrics().width("123456 Hz") + 2,
Chris@264 410 paint.fontMetrics().height());
Chris@264 411 extents.push_back(label);
Chris@264 412
Chris@278 413 int w(paint.fontMetrics().width("C#10+50c") + 2);
Chris@278 414 QRect pitch(cursorPos.x() - w,
Chris@278 415 v->height() - paint.fontMetrics().height() - hoffset,
Chris@278 416 w,
Chris@278 417 paint.fontMetrics().height());
Chris@278 418 extents.push_back(pitch);
Chris@278 419
Chris@264 420 return true;
Chris@264 421 }
Chris@264 422
Chris@254 423 void
Chris@254 424 SpectrumLayer::paintCrosshairs(View *v, QPainter &paint,
Chris@254 425 QPoint cursorPos) const
Chris@254 426 {
Chris@254 427 paint.save();
Chris@254 428
Chris@254 429 ColourMapper mapper(m_colourMap, 0, 1);
Chris@254 430 paint.setPen(mapper.getContrastingColour());
Chris@254 431
Chris@254 432 int xorigin = m_xorigins[v];
Chris@254 433 int w = v->width() - xorigin - 1;
Chris@254 434
Chris@254 435 paint.drawLine(xorigin, cursorPos.y(), v->width(), cursorPos.y());
Chris@254 436 paint.drawLine(cursorPos.x(), cursorPos.y(), cursorPos.x(), v->height());
Chris@254 437
Chris@254 438 float fundamental = getFrequencyForX(cursorPos.x() - xorigin, w);
Chris@254 439
Chris@278 440 int hoffset = 1;
Chris@278 441 if (m_binScale == LogBins) hoffset = 12;
Chris@278 442
Chris@278 443 v->drawVisibleText(paint,
Chris@278 444 cursorPos.x() + 2,
Chris@278 445 v->height() - 2 - hoffset,
Chris@278 446 QString("%1 Hz").arg(fundamental),
Chris@278 447 View::OutlinedText);
Chris@278 448
Chris@278 449 if (Pitch::isFrequencyInMidiRange(fundamental)) {
Chris@278 450 QString pitchLabel = Pitch::getPitchLabelForFrequency(fundamental);
Chris@278 451 v->drawVisibleText(paint,
Chris@278 452 cursorPos.x() - paint.fontMetrics().width(pitchLabel) - 2,
Chris@278 453 v->height() - 2 - hoffset,
Chris@278 454 pitchLabel,
Chris@278 455 View::OutlinedText);
Chris@278 456 }
Chris@264 457
Chris@254 458 int harmonic = 2;
Chris@254 459
Chris@254 460 while (harmonic < 100) {
Chris@254 461
Chris@254 462 float hx = lrintf(getXForFrequency(fundamental * harmonic, w));
Chris@254 463 hx += xorigin;
Chris@254 464
Chris@254 465 if (hx < xorigin || hx > v->width()) break;
Chris@254 466
Chris@254 467 int len = 7;
Chris@254 468
Chris@254 469 if (harmonic % 2 == 0) {
Chris@254 470 if (harmonic % 4 == 0) {
Chris@254 471 len = 12;
Chris@254 472 } else {
Chris@254 473 len = 10;
Chris@254 474 }
Chris@254 475 }
Chris@254 476
Chris@254 477 paint.drawLine(int(hx),
Chris@254 478 cursorPos.y(),
Chris@254 479 int(hx),
Chris@254 480 cursorPos.y() + len);
Chris@254 481
Chris@254 482 ++harmonic;
Chris@254 483 }
Chris@254 484
Chris@254 485 paint.restore();
Chris@254 486 }
Chris@254 487
Chris@199 488 QString
Chris@199 489 SpectrumLayer::getFeatureDescription(View *v, QPoint &p) const
Chris@199 490 {
Chris@199 491 if (!m_sliceableModel) return "";
Chris@199 492
Chris@199 493 int minbin = 0, maxbin = 0, range = 0;
Chris@199 494 QString genericDesc = SliceLayer::getFeatureDescription
Chris@199 495 (v, p, false, minbin, maxbin, range);
Chris@199 496
Chris@199 497 if (genericDesc == "") return "";
Chris@199 498
Chris@199 499 float minvalue = 0.f;
Chris@248 500 if (minbin < int(m_values.size())) minvalue = m_values[minbin];
Chris@199 501
Chris@199 502 float maxvalue = minvalue;
Chris@248 503 if (maxbin < int(m_values.size())) maxvalue = m_values[maxbin];
Chris@199 504
Chris@199 505 if (minvalue > maxvalue) std::swap(minvalue, maxvalue);
Chris@199 506
Chris@199 507 QString binstr;
Chris@199 508 QString hzstr;
Chris@199 509 int minfreq = lrintf((minbin * m_sliceableModel->getSampleRate()) /
Chris@199 510 m_windowSize);
Chris@199 511 int maxfreq = lrintf((std::max(maxbin, minbin+1)
Chris@199 512 * m_sliceableModel->getSampleRate()) /
Chris@199 513 m_windowSize);
Chris@199 514
Chris@199 515 if (maxbin != minbin) {
Chris@199 516 binstr = tr("%1 - %2").arg(minbin+1).arg(maxbin+1);
Chris@199 517 } else {
Chris@199 518 binstr = QString("%1").arg(minbin+1);
Chris@199 519 }
Chris@199 520 if (minfreq != maxfreq) {
Chris@199 521 hzstr = tr("%1 - %2 Hz").arg(minfreq).arg(maxfreq);
Chris@199 522 } else {
Chris@199 523 hzstr = tr("%1 Hz").arg(minfreq);
Chris@199 524 }
Chris@199 525
Chris@199 526 QString valuestr;
Chris@199 527 if (maxvalue != minvalue) {
Chris@199 528 valuestr = tr("%1 - %2").arg(minvalue).arg(maxvalue);
Chris@199 529 } else {
Chris@199 530 valuestr = QString("%1").arg(minvalue);
Chris@199 531 }
Chris@199 532
Chris@199 533 QString dbstr;
Chris@199 534 float mindb = AudioLevel::multiplier_to_dB(minvalue);
Chris@199 535 float maxdb = AudioLevel::multiplier_to_dB(maxvalue);
Chris@199 536 QString mindbstr;
Chris@199 537 QString maxdbstr;
Chris@199 538 if (mindb == AudioLevel::DB_FLOOR) {
Chris@199 539 mindbstr = tr("-Inf");
Chris@199 540 } else {
Chris@199 541 mindbstr = QString("%1").arg(lrintf(mindb));
Chris@199 542 }
Chris@199 543 if (maxdb == AudioLevel::DB_FLOOR) {
Chris@199 544 maxdbstr = tr("-Inf");
Chris@199 545 } else {
Chris@199 546 maxdbstr = QString("%1").arg(lrintf(maxdb));
Chris@199 547 }
Chris@199 548 if (lrintf(mindb) != lrintf(maxdb)) {
Chris@199 549 dbstr = tr("%1 - %2").arg(mindbstr).arg(maxdbstr);
Chris@199 550 } else {
Chris@199 551 dbstr = tr("%1").arg(mindbstr);
Chris@199 552 }
Chris@199 553
Chris@199 554 QString description;
Chris@199 555
Chris@248 556 if (range > int(m_sliceableModel->getResolution())) {
Chris@199 557 description = tr("%1\nBin:\t%2 (%3)\n%4 value:\t%5\ndB:\t%6")
Chris@199 558 .arg(genericDesc)
Chris@199 559 .arg(binstr)
Chris@199 560 .arg(hzstr)
Chris@199 561 .arg(m_samplingMode == NearestSample ? tr("First") :
Chris@199 562 m_samplingMode == SampleMean ? tr("Mean") : tr("Peak"))
Chris@199 563 .arg(valuestr)
Chris@199 564 .arg(dbstr);
Chris@199 565 } else {
Chris@199 566 description = tr("%1\nBin:\t%2 (%3)\nValue:\t%4\ndB:\t%5")
Chris@199 567 .arg(genericDesc)
Chris@199 568 .arg(binstr)
Chris@199 569 .arg(hzstr)
Chris@199 570 .arg(valuestr)
Chris@199 571 .arg(dbstr);
Chris@199 572 }
Chris@199 573
Chris@199 574 return description;
Chris@199 575 }
Chris@199 576
Chris@254 577 void
Chris@275 578 SpectrumLayer::paint(View *v, QPainter &paint, QRect rect) const
Chris@275 579 {
Chris@275 580 if (!m_originModel || !m_originModel->isOK() ||
Chris@275 581 !m_originModel->isReady()) return;
Chris@275 582
Chris@275 583 if (m_newFFTNeeded) {
Chris@275 584 const_cast<SpectrumLayer *>(this)->setupFFT(); //ugh
Chris@275 585 m_newFFTNeeded = false;
Chris@275 586 }
Chris@277 587
Chris@277 588 FFTModel *fft = dynamic_cast<FFTModel *>
Chris@277 589 (const_cast<DenseThreeDimensionalModel *>(m_sliceableModel));
Chris@277 590
Chris@277 591 float thresh = powf(10, -8) / m_gain; // -80dB
Chris@277 592
Chris@277 593 int xorigin = getVerticalScaleWidth(v, paint) + 1;
Chris@277 594 int w = v->width() - xorigin - 1;
Chris@277 595
Chris@278 596 int pkh = 0;
Chris@278 597 if (m_binScale == LogBins) pkh = 10;
Chris@278 598
Chris@277 599 if (fft) {
Chris@277 600
Chris@277 601 // draw peak lines
Chris@277 602 //!!! should be optional
Chris@277 603
Chris@277 604 size_t col = v->getCentreFrame() / fft->getResolution();
Chris@277 605
Chris@277 606 paint.save();
Chris@277 607 paint.setRenderHint(QPainter::Antialiasing, false);
Chris@277 608 paint.setPen(QColor(160, 160, 160)); //!!!
Chris@277 609
Chris@277 610 ColourMapper mapper(m_colourMap, 0, 1);
Chris@277 611
Chris@277 612 BiasCurve curve;
Chris@277 613 getBiasCurve(curve);
Chris@277 614 size_t cs = curve.size();
Chris@277 615
Chris@277 616 for (size_t bin = 0; bin < fft->getHeight(); ++bin) {
Chris@277 617
Chris@277 618 if (!fft->isLocalPeak(col, bin)) continue;
Chris@277 619 if (!fft->isOverThreshold(col, bin, thresh)) continue;
Chris@277 620
Chris@277 621 float freq = 0;
Chris@277 622 bool haveFreq = fft->estimateStableFrequency(col, bin, freq);
Chris@277 623 if (!haveFreq) continue;
Chris@277 624
Chris@277 625 int x = lrintf(getXForFrequency(freq, w));
Chris@277 626
Chris@277 627 float value = m_sliceableModel->getValueAt(col, bin);
Chris@277 628 if (bin < cs) value *= curve[bin];
Chris@277 629 float norm = 0.f;
Chris@277 630 float y = getYForValue(value, v, norm); // don't need y, need norm
Chris@277 631
Chris@277 632 paint.setPen(mapper.map(norm));
Chris@278 633 paint.drawLine(xorigin + x, 0, xorigin + x, v->height() - pkh - 1);
Chris@277 634 }
Chris@277 635
Chris@277 636 paint.restore();
Chris@277 637 }
Chris@275 638
Chris@275 639 SliceLayer::paint(v, paint, rect);
Chris@277 640
Chris@278 641 //!!! All of this stuff relating to depicting frequencies
Chris@278 642 //(keyboard, crosshairs etc) should be applicable to any slice
Chris@278 643 //layer whose model has a vertical scale unit of Hz. However, the
Chris@278 644 //dense 3d model at the moment doesn't record its vertical scale
Chris@278 645 //unit -- we need to fix that and hoist this code as appropriate.
Chris@278 646 //Same really goes for any code in SpectrogramLayer that could be
Chris@278 647 //relevant to Colour3DPlotLayer with unit Hz, but that's a bigger
Chris@278 648 //proposition.
Chris@278 649
Chris@277 650 if (m_binScale == LogBins) {
Chris@277 651
Chris@277 652 int pkh = 10;
Chris@277 653 int h = v->height();
Chris@277 654
Chris@277 655 // piano keyboard
Chris@277 656 //!!! should be in a new paintHorizontalScale()?
Chris@278 657 // nice to have a piano keyboard class, of course
Chris@277 658
Chris@278 659 paint.drawLine(xorigin, h - pkh - 1, w + xorigin, h - pkh - 1);
Chris@277 660
Chris@277 661 int px = xorigin, ppx = xorigin;
Chris@278 662 paint.setBrush(paint.pen().color());
Chris@277 663
Chris@277 664 for (int i = 0; i < 128; ++i) {
Chris@277 665
Chris@277 666 float f = Pitch::getFrequencyForPitch(i);
Chris@277 667 int x = lrintf(getXForFrequency(f, w));
Chris@278 668
Chris@278 669 x += xorigin;
Chris@277 670
Chris@278 671 if (i == 0) {
Chris@278 672 px = ppx = x;
Chris@278 673 }
Chris@278 674 if (i == 1) {
Chris@278 675 ppx = px - (x - px);
Chris@278 676 }
Chris@278 677
Chris@278 678 if (x < xorigin) {
Chris@278 679 ppx = px;
Chris@278 680 px = x;
Chris@277 681 continue;
Chris@277 682 }
Chris@278 683
Chris@278 684 if (x > w) {
Chris@278 685 break;
Chris@278 686 }
Chris@277 687
Chris@277 688 int n = (i % 12);
Chris@277 689
Chris@277 690 if (n == 1) {
Chris@277 691 // C# -- fill the C from here
Chris@277 692 if (x - ppx > 2) {
Chris@278 693 paint.fillRect((px + ppx) / 2 + 1,
Chris@277 694 h - pkh,
Chris@278 695 x - (px + ppx) / 2 - 1,
Chris@277 696 pkh,
Chris@277 697 Qt::gray);
Chris@277 698 }
Chris@277 699 }
Chris@277 700
Chris@277 701 if (n == 1 || n == 3 || n == 6 || n == 8 || n == 10) {
Chris@277 702 // black notes
Chris@277 703 paint.drawLine(x, h - pkh, x, h);
Chris@278 704 int rw = lrintf(float(x - px) / 4) * 2;
Chris@277 705 if (rw < 2) rw = 2;
Chris@278 706 paint.drawRect(x - rw/2, h - pkh, rw, pkh/2);
Chris@277 707 } else if (n == 0 || n == 5) {
Chris@277 708 // C, F
Chris@277 709 if (px < w) {
Chris@277 710 paint.drawLine((x + px) / 2, h - pkh, (x + px) / 2, h);
Chris@277 711 }
Chris@277 712 }
Chris@277 713
Chris@277 714 ppx = px;
Chris@277 715 px = x;
Chris@277 716 }
Chris@277 717 }
Chris@275 718 }
Chris@275 719
Chris@275 720 void
Chris@254 721 SpectrumLayer::getBiasCurve(BiasCurve &curve) const
Chris@254 722 {
Chris@254 723 curve = m_biasCurve;
Chris@254 724 }
Chris@199 725
Chris@220 726 QString
Chris@220 727 SpectrumLayer::toXmlString(QString indent, QString extraAttributes) const
Chris@220 728 {
Chris@220 729 QString s;
Chris@199 730
Chris@220 731 s += QString("windowSize=\"%1\" "
Chris@220 732 "windowHopLevel=\"%2\"")
Chris@220 733 .arg(m_windowSize)
Chris@220 734 .arg(m_windowHopLevel);
Chris@220 735
Chris@220 736 return SliceLayer::toXmlString(indent, extraAttributes + " " + s);
Chris@220 737 }
Chris@220 738
Chris@220 739 void
Chris@220 740 SpectrumLayer::setProperties(const QXmlAttributes &attributes)
Chris@220 741 {
Chris@220 742 SliceLayer::setProperties(attributes);
Chris@220 743
Chris@220 744 bool ok = false;
Chris@220 745
Chris@220 746 size_t windowSize = attributes.value("windowSize").toUInt(&ok);
Chris@220 747 if (ok) setWindowSize(windowSize);
Chris@220 748
Chris@220 749 size_t windowHopLevel = attributes.value("windowHopLevel").toUInt(&ok);
Chris@220 750 if (ok) setWindowHopLevel(windowHopLevel);
Chris@220 751 }
Chris@220 752
Chris@220 753