annotate layer/SpectrumLayer.cpp @ 1395:32bbb86094c3

Merge from branch spectrogramparam
author Chris Cannam
date Wed, 14 Nov 2018 14:23:17 +0000
parents 4a36f6130056
children 2e316a724336
rev   line source
Chris@133 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@133 2
Chris@133 3 /*
Chris@133 4 Sonic Visualiser
Chris@133 5 An audio file viewer and annotation editor.
Chris@133 6 Centre for Digital Music, Queen Mary, University of London.
Chris@195 7 This file copyright 2006-2007 QMUL.
Chris@133 8
Chris@133 9 This program is free software; you can redistribute it and/or
Chris@133 10 modify it under the terms of the GNU General Public License as
Chris@133 11 published by the Free Software Foundation; either version 2 of the
Chris@133 12 License, or (at your option) any later version. See the file
Chris@133 13 COPYING included with this distribution for more information.
Chris@133 14 */
Chris@133 15
Chris@133 16 #include "SpectrumLayer.h"
Chris@133 17
Chris@133 18 #include "data/model/FFTModel.h"
Chris@133 19 #include "view/View.h"
Chris@153 20 #include "base/AudioLevel.h"
Chris@153 21 #include "base/Preferences.h"
Chris@167 22 #include "base/RangeMapper.h"
Chris@277 23 #include "base/Pitch.h"
Chris@1147 24 #include "base/Strings.h"
Chris@1078 25
Chris@376 26 #include "ColourMapper.h"
Chris@1078 27 #include "PaintAssistant.h"
Chris@1276 28 #include "PianoScale.h"
Chris@1281 29 #include "HorizontalFrequencyScale.h"
Chris@254 30
Chris@254 31 #include <QPainter>
Chris@316 32 #include <QTextStream>
Chris@316 33
Chris@133 34
Chris@133 35 SpectrumLayer::SpectrumLayer() :
Chris@193 36 m_originModel(0),
Chris@153 37 m_channel(-1),
Chris@153 38 m_channelSet(false),
Chris@290 39 m_windowSize(4096),
Chris@153 40 m_windowType(HanningWindow),
Chris@290 41 m_windowHopLevel(3),
Chris@1382 42 m_oversampling(1),
Chris@284 43 m_showPeaks(false),
Chris@275 44 m_newFFTNeeded(true)
Chris@133 45 {
Chris@153 46 Preferences *prefs = Preferences::getInstance();
Chris@153 47 connect(prefs, SIGNAL(propertyChanged(PropertyContainer::PropertyName)),
Chris@153 48 this, SLOT(preferenceChanged(PropertyContainer::PropertyName)));
Chris@153 49 setWindowType(prefs->getWindowType());
Chris@195 50
Chris@195 51 setBinScale(LogBins);
Chris@133 52 }
Chris@133 53
Chris@133 54 SpectrumLayer::~SpectrumLayer()
Chris@133 55 {
Chris@349 56 Model *m = const_cast<Model *>
Chris@349 57 (static_cast<const Model *>(m_sliceableModel));
Chris@458 58 if (m) m->aboutToDelete();
Chris@349 59 m_sliceableModel = 0;
Chris@349 60 delete m;
Chris@133 61 }
Chris@133 62
Chris@133 63 void
Chris@133 64 SpectrumLayer::setModel(DenseTimeValueModel *model)
Chris@133 65 {
Chris@587 66 SVDEBUG << "SpectrumLayer::setModel(" << model << ") from " << m_originModel << endl;
Chris@345 67
Chris@193 68 if (m_originModel == model) return;
Chris@349 69
Chris@193 70 m_originModel = model;
Chris@277 71
Chris@350 72 if (m_sliceableModel) {
Chris@350 73 Model *m = const_cast<Model *>
Chris@350 74 (static_cast<const Model *>(m_sliceableModel));
Chris@350 75 m->aboutToDelete();
Chris@350 76 setSliceableModel(0);
Chris@350 77 delete m;
Chris@350 78 }
Chris@350 79
Chris@349 80 m_newFFTNeeded = true;
Chris@349 81
Chris@349 82 emit layerParametersChanged();
Chris@349 83 }
Chris@349 84
Chris@349 85 void
Chris@349 86 SpectrumLayer::setChannel(int channel)
Chris@349 87 {
Chris@587 88 SVDEBUG << "SpectrumLayer::setChannel(" << channel << ") from " << m_channel << endl;
Chris@349 89
Chris@349 90 m_channelSet = true;
Chris@349 91
Chris@349 92 if (m_channel == channel) return;
Chris@349 93
Chris@349 94 m_channel = channel;
Chris@349 95
Chris@349 96 m_newFFTNeeded = true;
Chris@349 97
Chris@349 98 emit layerParametersChanged();
Chris@153 99 }
Chris@153 100
Chris@153 101 void
Chris@193 102 SpectrumLayer::setupFFT()
Chris@153 103 {
Chris@349 104 if (m_sliceableModel) {
Chris@349 105 Model *m = const_cast<Model *>
Chris@349 106 (static_cast<const Model *>(m_sliceableModel));
Chris@349 107 m->aboutToDelete();
Chris@193 108 setSliceableModel(0);
Chris@349 109 delete m;
Chris@349 110 }
Chris@349 111
Chris@349 112 if (!m_originModel) {
Chris@349 113 return;
Chris@153 114 }
Chris@153 115
Chris@1382 116 int fftSize = getFFTSize();
Chris@1382 117
Chris@193 118 FFTModel *newFFT = new FFTModel(m_originModel,
Chris@193 119 m_channel,
Chris@193 120 m_windowType,
Chris@193 121 m_windowSize,
Chris@193 122 getWindowIncrement(),
Chris@1382 123 fftSize);
Chris@153 124
Chris@193 125 setSliceableModel(newFFT);
Chris@193 126
Chris@254 127 m_biasCurve.clear();
Chris@1382 128 for (int i = 0; i < fftSize; ++i) {
Chris@1382 129 m_biasCurve.push_back(1.f / (float(fftSize)/2.f));
Chris@254 130 }
Chris@254 131
Chris@349 132 m_newFFTNeeded = false;
Chris@133 133 }
Chris@133 134
Chris@153 135 Layer::PropertyList
Chris@153 136 SpectrumLayer::getProperties() const
Chris@153 137 {
Chris@193 138 PropertyList list = SliceLayer::getProperties();
Chris@153 139 list.push_back("Window Size");
Chris@153 140 list.push_back("Window Increment");
Chris@1382 141 list.push_back("Oversampling");
Chris@284 142 list.push_back("Show Peak Frequencies");
Chris@153 143 return list;
Chris@153 144 }
Chris@153 145
Chris@153 146 QString
Chris@153 147 SpectrumLayer::getPropertyLabel(const PropertyName &name) const
Chris@153 148 {
Chris@153 149 if (name == "Window Size") return tr("Window Size");
Chris@153 150 if (name == "Window Increment") return tr("Window Overlap");
Chris@1382 151 if (name == "Oversampling") return tr("Oversampling");
Chris@284 152 if (name == "Show Peak Frequencies") return tr("Show Peak Frequencies");
Chris@193 153 return SliceLayer::getPropertyLabel(name);
Chris@153 154 }
Chris@153 155
Chris@335 156 QString
Chris@335 157 SpectrumLayer::getPropertyIconName(const PropertyName &name) const
Chris@335 158 {
Chris@335 159 if (name == "Show Peak Frequencies") return "show-peaks";
Chris@335 160 return SliceLayer::getPropertyIconName(name);
Chris@335 161 }
Chris@335 162
Chris@153 163 Layer::PropertyType
Chris@153 164 SpectrumLayer::getPropertyType(const PropertyName &name) const
Chris@153 165 {
Chris@193 166 if (name == "Window Size") return ValueProperty;
Chris@193 167 if (name == "Window Increment") return ValueProperty;
Chris@1382 168 if (name == "Oversampling") return ValueProperty;
Chris@284 169 if (name == "Show Peak Frequencies") return ToggleProperty;
Chris@193 170 return SliceLayer::getPropertyType(name);
Chris@153 171 }
Chris@153 172
Chris@153 173 QString
Chris@153 174 SpectrumLayer::getPropertyGroupName(const PropertyName &name) const
Chris@153 175 {
Chris@153 176 if (name == "Window Size" ||
Chris@1382 177 name == "Window Increment" ||
Chris@1382 178 name == "Oversampling") return tr("Window");
Chris@557 179 if (name == "Show Peak Frequencies") return tr("Bins");
Chris@193 180 return SliceLayer::getPropertyGroupName(name);
Chris@153 181 }
Chris@153 182
Chris@153 183 int
Chris@153 184 SpectrumLayer::getPropertyRangeAndValue(const PropertyName &name,
Chris@216 185 int *min, int *max, int *deflt) const
Chris@153 186 {
Chris@216 187 int val = 0;
Chris@153 188
Chris@216 189 int garbage0, garbage1, garbage2;
Chris@153 190 if (!min) min = &garbage0;
Chris@153 191 if (!max) max = &garbage1;
Chris@216 192 if (!deflt) deflt = &garbage2;
Chris@153 193
Chris@193 194 if (name == "Window Size") {
Chris@153 195
Chris@1266 196 *min = 0;
Chris@1266 197 *max = 15;
Chris@216 198 *deflt = 5;
Chris@1266 199
Chris@1266 200 val = 0;
Chris@1266 201 int ws = m_windowSize;
Chris@1266 202 while (ws > 32) { ws >>= 1; val ++; }
Chris@153 203
Chris@153 204 } else if (name == "Window Increment") {
Chris@1266 205
Chris@1266 206 *min = 0;
Chris@1266 207 *max = 5;
Chris@216 208 *deflt = 2;
Chris@1266 209
Chris@216 210 val = m_windowHopLevel;
Chris@153 211
Chris@1382 212 } else if (name == "Oversampling") {
Chris@1382 213
Chris@1382 214 *min = 0;
Chris@1382 215 *max = 3;
Chris@1382 216 *deflt = 0;
Chris@1382 217
Chris@1382 218 val = 0;
Chris@1382 219 int ov = m_oversampling;
Chris@1382 220 while (ov > 1) { ov >>= 1; val ++; }
Chris@1382 221
Chris@284 222 } else if (name == "Show Peak Frequencies") {
Chris@284 223
Chris@284 224 return m_showPeaks ? 1 : 0;
Chris@284 225
Chris@153 226 } else {
Chris@193 227
Chris@216 228 val = SliceLayer::getPropertyRangeAndValue(name, min, max, deflt);
Chris@153 229 }
Chris@153 230
Chris@216 231 return val;
Chris@153 232 }
Chris@153 233
Chris@153 234 QString
Chris@153 235 SpectrumLayer::getPropertyValueLabel(const PropertyName &name,
Chris@1266 236 int value) const
Chris@153 237 {
Chris@153 238 if (name == "Window Size") {
Chris@1266 239 return QString("%1").arg(32 << value);
Chris@153 240 }
Chris@153 241 if (name == "Window Increment") {
Chris@1266 242 switch (value) {
Chris@1266 243 default:
Chris@1266 244 case 0: return tr("None");
Chris@1266 245 case 1: return tr("25 %");
Chris@1266 246 case 2: return tr("50 %");
Chris@1266 247 case 3: return tr("75 %");
Chris@1266 248 case 4: return tr("87.5 %");
Chris@1266 249 case 5: return tr("93.75 %");
Chris@1266 250 }
Chris@153 251 }
Chris@1382 252 if (name == "Oversampling") {
Chris@1382 253 switch (value) {
Chris@1382 254 default:
Chris@1382 255 case 0: return tr("1x");
Chris@1382 256 case 1: return tr("2x");
Chris@1382 257 case 2: return tr("4x");
Chris@1382 258 case 3: return tr("8x");
Chris@1382 259 }
Chris@1382 260 }
Chris@193 261 return SliceLayer::getPropertyValueLabel(name, value);
Chris@153 262 }
Chris@153 263
Chris@167 264 RangeMapper *
Chris@167 265 SpectrumLayer::getNewPropertyRangeMapper(const PropertyName &name) const
Chris@167 266 {
Chris@193 267 return SliceLayer::getNewPropertyRangeMapper(name);
Chris@167 268 }
Chris@167 269
Chris@133 270 void
Chris@153 271 SpectrumLayer::setProperty(const PropertyName &name, int value)
Chris@133 272 {
Chris@193 273 if (name == "Window Size") {
Chris@1266 274 setWindowSize(32 << value);
Chris@153 275 } else if (name == "Window Increment") {
Chris@153 276 setWindowHopLevel(value);
Chris@1382 277 } else if (name == "Oversampling") {
Chris@1382 278 setOversampling(1 << value);
Chris@284 279 } else if (name == "Show Peak Frequencies") {
Chris@284 280 setShowPeaks(value ? true : false);
Chris@193 281 } else {
Chris@193 282 SliceLayer::setProperty(name, value);
Chris@153 283 }
Chris@153 284 }
Chris@153 285
Chris@153 286 void
Chris@805 287 SpectrumLayer::setWindowSize(int ws)
Chris@153 288 {
Chris@153 289 if (m_windowSize == ws) return;
Chris@1389 290
Chris@1389 291 SVDEBUG << "setWindowSize: from " << m_windowSize
Chris@1389 292 << " to " << ws << ": updating min and max bins from "
Chris@1389 293 << m_minbin << " and " << m_maxbin << " to ";
Chris@1389 294
Chris@1389 295 m_minbin = int(round((double(m_minbin) / m_windowSize) * ws));
Chris@1389 296 m_maxbin = int(round((double(m_maxbin) / m_windowSize) * ws));
Chris@1389 297
Chris@1389 298 SVDEBUG << m_minbin << " and " << m_maxbin << endl;
Chris@1389 299
Chris@153 300 m_windowSize = ws;
Chris@275 301 m_newFFTNeeded = true;
Chris@153 302 emit layerParametersChanged();
Chris@153 303 }
Chris@153 304
Chris@153 305 void
Chris@805 306 SpectrumLayer::setWindowHopLevel(int v)
Chris@153 307 {
Chris@153 308 if (m_windowHopLevel == v) return;
Chris@153 309 m_windowHopLevel = v;
Chris@275 310 m_newFFTNeeded = true;
Chris@153 311 emit layerParametersChanged();
Chris@153 312 }
Chris@153 313
Chris@153 314 void
Chris@153 315 SpectrumLayer::setWindowType(WindowType w)
Chris@153 316 {
Chris@153 317 if (m_windowType == w) return;
Chris@153 318 m_windowType = w;
Chris@275 319 m_newFFTNeeded = true;
Chris@153 320 emit layerParametersChanged();
Chris@153 321 }
Chris@153 322
Chris@153 323 void
Chris@1382 324 SpectrumLayer::setOversampling(int oversampling)
Chris@1382 325 {
Chris@1382 326 if (m_oversampling == oversampling) return;
Chris@1389 327
Chris@1389 328 SVDEBUG << "setOversampling: from " << m_oversampling
Chris@1389 329 << " to " << oversampling << ": updating min and max bins from "
Chris@1389 330 << m_minbin << " and " << m_maxbin << " to ";
Chris@1389 331
Chris@1389 332 m_minbin = int(round((double(m_minbin) / m_oversampling) * oversampling));
Chris@1389 333 m_maxbin = int(round((double(m_maxbin) / m_oversampling) * oversampling));
Chris@1389 334
Chris@1389 335 SVDEBUG << m_minbin << " and " << m_maxbin << endl;
Chris@1389 336
Chris@1382 337 m_oversampling = oversampling;
Chris@1382 338 m_newFFTNeeded = true;
Chris@1389 339
Chris@1382 340 emit layerParametersChanged();
Chris@1382 341 }
Chris@1382 342
Chris@1382 343 int
Chris@1382 344 SpectrumLayer::getOversampling() const
Chris@1382 345 {
Chris@1382 346 return m_oversampling;
Chris@1382 347 }
Chris@1382 348
Chris@1382 349 void
Chris@284 350 SpectrumLayer::setShowPeaks(bool show)
Chris@284 351 {
Chris@284 352 if (m_showPeaks == show) return;
Chris@284 353 m_showPeaks = show;
Chris@284 354 emit layerParametersChanged();
Chris@284 355 }
Chris@284 356
Chris@284 357 void
Chris@153 358 SpectrumLayer::preferenceChanged(PropertyContainer::PropertyName name)
Chris@153 359 {
Chris@153 360 if (name == "Window Type") {
Chris@1382 361 auto type = Preferences::getInstance()->getWindowType();
Chris@1382 362 SVDEBUG << "SpectrumLayer::preferenceChanged: Window type changed to "
Chris@1382 363 << type << endl;
Chris@1382 364 setWindowType(type);
Chris@153 365 return;
Chris@153 366 }
Chris@153 367 }
Chris@153 368
Chris@1238 369 double
Chris@1386 370 SpectrumLayer::getBinForFrequency(double freq) const
Chris@1386 371 {
Chris@1386 372 if (!m_sliceableModel) return 0;
Chris@1386 373 double bin = (freq * getFFTSize()) / m_sliceableModel->getSampleRate();
Chris@1386 374 // we assume the frequency of a bin corresponds to the centre of
Chris@1386 375 // its visual range
Chris@1386 376 bin += 0.5;
Chris@1386 377 return bin;
Chris@1386 378 }
Chris@1386 379
Chris@1386 380 double
Chris@1386 381 SpectrumLayer::getBinForX(const LayerGeometryProvider *v, double x) const
Chris@1386 382 {
Chris@1386 383 if (!m_sliceableModel) return 0;
Chris@1386 384 double bin = getBinForFrequency(getFrequencyForX(v, x));
Chris@1386 385 return bin;
Chris@1386 386 }
Chris@1386 387
Chris@1386 388 double
Chris@1238 389 SpectrumLayer::getFrequencyForX(const LayerGeometryProvider *v, double x) const
Chris@133 390 {
Chris@1238 391 if (!m_sliceableModel) return 0;
Chris@1394 392
Chris@1394 393 double fmin = getFrequencyForBin(m_minbin);
Chris@1394 394
Chris@1394 395 if (m_binScale == LogBins && m_minbin == 0) {
Chris@1394 396 // Avoid too much space going to the first bin, but do so in a
Chris@1394 397 // way that usually avoids us shifting left/right as the
Chris@1394 398 // window size or oversampling ratio change - i.e. base this
Chris@1394 399 // on frequency rather than bin number unless we have a lot of
Chris@1394 400 // very low-resolution content
Chris@1394 401 fmin = getFrequencyForBin(0.8);
Chris@1394 402 if (fmin > 6.0) fmin = 6.0;
Chris@1394 403 }
Chris@1394 404
Chris@1394 405 double fmax = getFrequencyForBin(m_maxbin);
Chris@1394 406
Chris@1394 407 double freq = getScalePointForX(v, x, fmin, fmax);
Chris@1386 408 return freq;
Chris@1386 409 }
Chris@1386 410
Chris@1386 411 double
Chris@1386 412 SpectrumLayer::getFrequencyForBin(double bin) const
Chris@1386 413 {
Chris@1386 414 if (!m_sliceableModel) return 0;
Chris@1281 415 // we assume the frequency of a bin corresponds to the centre of
Chris@1281 416 // its visual range
Chris@1281 417 bin -= 0.5;
Chris@1386 418 double freq = (bin * m_sliceableModel->getSampleRate()) / getFFTSize();
Chris@1386 419 return freq;
Chris@1386 420 }
Chris@1386 421
Chris@1386 422 double
Chris@1386 423 SpectrumLayer::getXForBin(const LayerGeometryProvider *v, double bin) const
Chris@1386 424 {
Chris@1386 425 if (!m_sliceableModel) return 0;
Chris@1386 426 double x = getXForFrequency(v, getFrequencyForBin(bin));
Chris@1386 427 return x;
Chris@133 428 }
Chris@133 429
Chris@908 430 double
Chris@1238 431 SpectrumLayer::getXForFrequency(const LayerGeometryProvider *v, double freq) const
Chris@265 432 {
Chris@280 433 if (!m_sliceableModel) return 0;
Chris@1394 434
Chris@1394 435 double fmin = getFrequencyForBin(m_minbin);
Chris@1394 436 if (m_binScale == LogBins && m_minbin == 0) {
Chris@1394 437 // See comment in getFrequencyForX above
Chris@1394 438 fmin = getFrequencyForBin(0.8);
Chris@1394 439 if (fmin > 6.0) fmin = 6.0;
Chris@1394 440 }
Chris@1394 441
Chris@1394 442 double fmax = getFrequencyForBin(m_maxbin);
Chris@1394 443
Chris@1394 444 double x = getXForScalePoint(v, freq, fmin, fmax);
Chris@1386 445 return x;
Chris@254 446 }
Chris@254 447
Chris@260 448 bool
Chris@918 449 SpectrumLayer::getXScaleValue(const LayerGeometryProvider *v, int x,
Chris@908 450 double &value, QString &unit) const
Chris@260 451 {
Chris@1238 452 value = getFrequencyForX(v, x);
Chris@260 453 unit = "Hz";
Chris@260 454 return true;
Chris@260 455 }
Chris@260 456
Chris@264 457 bool
Chris@918 458 SpectrumLayer::getYScaleValue(const LayerGeometryProvider *v, int y,
Chris@908 459 double &value, QString &unit) const
Chris@274 460 {
Chris@1238 461 value = getValueForY(v, y);
Chris@274 462
Chris@274 463 if (m_energyScale == dBScale || m_energyScale == MeterScale) {
Chris@274 464
Chris@908 465 if (value > 0.0) {
Chris@908 466 value = 10.0 * log10(value);
Chris@284 467 if (value < m_threshold) value = m_threshold;
Chris@284 468 } else value = m_threshold;
Chris@274 469
Chris@274 470 unit = "dBV";
Chris@274 471
Chris@274 472 } else {
Chris@274 473 unit = "V";
Chris@274 474 }
Chris@274 475
Chris@274 476 return true;
Chris@274 477 }
Chris@274 478
Chris@274 479 bool
Chris@918 480 SpectrumLayer::getYScaleDifference(const LayerGeometryProvider *v, int y0, int y1,
Chris@908 481 double &diff, QString &unit) const
Chris@274 482 {
Chris@274 483 bool rv = SliceLayer::getYScaleDifference(v, y0, y1, diff, unit);
Chris@274 484 if (rv && (unit == "dBV")) unit = "dB";
Chris@274 485 return rv;
Chris@274 486 }
Chris@274 487
Chris@274 488
Chris@274 489 bool
Chris@918 490 SpectrumLayer::getCrosshairExtents(LayerGeometryProvider *v, QPainter &paint,
Chris@264 491 QPoint cursorPos,
Chris@264 492 std::vector<QRect> &extents) const
Chris@264 493 {
Chris@918 494 QRect vertical(cursorPos.x(), cursorPos.y(), 1, v->getPaintHeight() - cursorPos.y());
Chris@264 495 extents.push_back(vertical);
Chris@264 496
Chris@918 497 QRect horizontal(0, cursorPos.y(), v->getPaintWidth(), 12);
Chris@264 498 extents.push_back(horizontal);
Chris@264 499
Chris@280 500 int hoffset = 2;
Chris@280 501 if (m_binScale == LogBins) hoffset = 13;
Chris@278 502
Chris@607 503 int sw = getVerticalScaleWidth(v, false, paint);
Chris@280 504
Chris@280 505 QRect value(sw, cursorPos.y() - paint.fontMetrics().ascent() - 2,
Chris@280 506 paint.fontMetrics().width("0.0000001 V") + 2,
Chris@264 507 paint.fontMetrics().height());
Chris@280 508 extents.push_back(value);
Chris@280 509
Chris@280 510 QRect log(sw, cursorPos.y() + 2,
Chris@280 511 paint.fontMetrics().width("-80.000 dBV") + 2,
Chris@280 512 paint.fontMetrics().height());
Chris@280 513 extents.push_back(log);
Chris@280 514
Chris@280 515 QRect freq(cursorPos.x(),
Chris@918 516 v->getPaintHeight() - paint.fontMetrics().height() - hoffset,
Chris@280 517 paint.fontMetrics().width("123456 Hz") + 2,
Chris@280 518 paint.fontMetrics().height());
Chris@280 519 extents.push_back(freq);
Chris@264 520
Chris@278 521 int w(paint.fontMetrics().width("C#10+50c") + 2);
Chris@278 522 QRect pitch(cursorPos.x() - w,
Chris@918 523 v->getPaintHeight() - paint.fontMetrics().height() - hoffset,
Chris@278 524 w,
Chris@278 525 paint.fontMetrics().height());
Chris@278 526 extents.push_back(pitch);
Chris@278 527
Chris@264 528 return true;
Chris@264 529 }
Chris@264 530
Chris@254 531 void
Chris@918 532 SpectrumLayer::paintCrosshairs(LayerGeometryProvider *v, QPainter &paint,
Chris@254 533 QPoint cursorPos) const
Chris@254 534 {
Chris@280 535 if (!m_sliceableModel) return;
Chris@280 536
Chris@254 537 paint.save();
Chris@282 538 QFont fn = paint.font();
Chris@282 539 if (fn.pointSize() > 8) {
Chris@282 540 fn.setPointSize(fn.pointSize() - 1);
Chris@282 541 paint.setFont(fn);
Chris@282 542 }
Chris@254 543
Chris@1362 544 ColourMapper mapper(m_colourMap, m_colourInverted, 0, 1);
Chris@254 545 paint.setPen(mapper.getContrastingColour());
Chris@254 546
Chris@1238 547 int xorigin = m_xorigins[v->getId()];
Chris@918 548 paint.drawLine(xorigin, cursorPos.y(), v->getPaintWidth(), cursorPos.y());
Chris@918 549 paint.drawLine(cursorPos.x(), cursorPos.y(), cursorPos.x(), v->getPaintHeight());
Chris@254 550
Chris@1238 551 double fundamental = getFrequencyForX(v, cursorPos.x());
Chris@254 552
Chris@1392 553 int hoffset = getHorizontalScaleHeight(v, paint) +
Chris@1392 554 2 * paint.fontMetrics().height();
Chris@278 555
Chris@1078 556 PaintAssistant::drawVisibleText(v, paint,
Chris@1238 557 cursorPos.x() + 2,
Chris@1238 558 v->getPaintHeight() - 2 - hoffset,
Chris@1392 559 tr("%1 Hz").arg(fundamental),
Chris@1238 560 PaintAssistant::OutlinedText);
Chris@278 561
Chris@278 562 if (Pitch::isFrequencyInMidiRange(fundamental)) {
Chris@278 563 QString pitchLabel = Pitch::getPitchLabelForFrequency(fundamental);
Chris@1078 564 PaintAssistant::drawVisibleText(v, paint,
Chris@1238 565 cursorPos.x() -
Chris@1238 566 paint.fontMetrics().width(pitchLabel) - 2,
Chris@1238 567 v->getPaintHeight() - 2 - hoffset,
Chris@1238 568 pitchLabel,
Chris@1238 569 PaintAssistant::OutlinedText);
Chris@278 570 }
Chris@264 571
Chris@1238 572 double value = getValueForY(v, cursorPos.y());
Chris@280 573
Chris@1078 574 PaintAssistant::drawVisibleText(v, paint,
Chris@280 575 xorigin + 2,
Chris@280 576 cursorPos.y() - 2,
Chris@280 577 QString("%1 V").arg(value),
Chris@1078 578 PaintAssistant::OutlinedText);
Chris@280 579
Chris@1392 580 if (value > m_threshold) {
Chris@1392 581 double db = 10.0 * log10(value);
Chris@1392 582 PaintAssistant::drawVisibleText(v, paint,
Chris@1392 583 xorigin + 2,
Chris@1392 584 cursorPos.y() + 2 +
Chris@1392 585 paint.fontMetrics().ascent(),
Chris@1392 586 QString("%1 dBV").arg(db),
Chris@1392 587 PaintAssistant::OutlinedText);
Chris@1392 588 }
Chris@280 589
Chris@254 590 int harmonic = 2;
Chris@254 591
Chris@254 592 while (harmonic < 100) {
Chris@254 593
Chris@1238 594 int hx = int(lrint(getXForFrequency(v, fundamental * harmonic)));
Chris@254 595
Chris@918 596 if (hx < xorigin || hx > v->getPaintWidth()) break;
Chris@254 597
Chris@254 598 int len = 7;
Chris@254 599
Chris@254 600 if (harmonic % 2 == 0) {
Chris@254 601 if (harmonic % 4 == 0) {
Chris@254 602 len = 12;
Chris@254 603 } else {
Chris@254 604 len = 10;
Chris@254 605 }
Chris@254 606 }
Chris@254 607
Chris@908 608 paint.drawLine(hx,
Chris@254 609 cursorPos.y(),
Chris@908 610 hx,
Chris@254 611 cursorPos.y() + len);
Chris@254 612
Chris@254 613 ++harmonic;
Chris@254 614 }
Chris@254 615
Chris@254 616 paint.restore();
Chris@254 617 }
Chris@254 618
Chris@199 619 QString
Chris@918 620 SpectrumLayer::getFeatureDescription(LayerGeometryProvider *v, QPoint &p) const
Chris@199 621 {
Chris@199 622 if (!m_sliceableModel) return "";
Chris@199 623
Chris@199 624 int minbin = 0, maxbin = 0, range = 0;
Chris@805 625 QString genericDesc = SliceLayer::getFeatureDescriptionAux
Chris@199 626 (v, p, false, minbin, maxbin, range);
Chris@199 627
Chris@199 628 if (genericDesc == "") return "";
Chris@199 629
Chris@1238 630 int i0 = minbin - m_minbin;
Chris@1238 631 int i1 = maxbin - m_minbin;
Chris@1238 632
Chris@1238 633 float minvalue = 0.0;
Chris@1238 634 if (in_range_for(m_values, i0)) minvalue = m_values[i0];
Chris@199 635
Chris@1238 636 float maxvalue = minvalue;
Chris@1238 637 if (in_range_for(m_values, i1)) maxvalue = m_values[i1];
Chris@1238 638
Chris@199 639 if (minvalue > maxvalue) std::swap(minvalue, maxvalue);
Chris@199 640
Chris@199 641 QString binstr;
Chris@199 642 QString hzstr;
Chris@908 643 int minfreq = int(lrint((minbin * m_sliceableModel->getSampleRate()) /
Chris@1382 644 getFFTSize()));
Chris@1256 645 int maxfreq = int(lrint((std::max(maxbin, minbin)
Chris@908 646 * m_sliceableModel->getSampleRate()) /
Chris@1382 647 getFFTSize()));
Chris@199 648
Chris@199 649 if (maxbin != minbin) {
Chris@199 650 binstr = tr("%1 - %2").arg(minbin+1).arg(maxbin+1);
Chris@199 651 } else {
Chris@199 652 binstr = QString("%1").arg(minbin+1);
Chris@199 653 }
Chris@199 654 if (minfreq != maxfreq) {
Chris@199 655 hzstr = tr("%1 - %2 Hz").arg(minfreq).arg(maxfreq);
Chris@199 656 } else {
Chris@199 657 hzstr = tr("%1 Hz").arg(minfreq);
Chris@199 658 }
Chris@199 659
Chris@199 660 QString valuestr;
Chris@199 661 if (maxvalue != minvalue) {
Chris@199 662 valuestr = tr("%1 - %2").arg(minvalue).arg(maxvalue);
Chris@199 663 } else {
Chris@199 664 valuestr = QString("%1").arg(minvalue);
Chris@199 665 }
Chris@199 666
Chris@199 667 QString dbstr;
Chris@908 668 double mindb = AudioLevel::multiplier_to_dB(minvalue);
Chris@908 669 double maxdb = AudioLevel::multiplier_to_dB(maxvalue);
Chris@199 670 QString mindbstr;
Chris@199 671 QString maxdbstr;
Chris@199 672 if (mindb == AudioLevel::DB_FLOOR) {
Chris@1147 673 mindbstr = Strings::minus_infinity;
Chris@199 674 } else {
Chris@908 675 mindbstr = QString("%1").arg(lrint(mindb));
Chris@199 676 }
Chris@199 677 if (maxdb == AudioLevel::DB_FLOOR) {
Chris@1147 678 maxdbstr = Strings::minus_infinity;
Chris@199 679 } else {
Chris@908 680 maxdbstr = QString("%1").arg(lrint(maxdb));
Chris@199 681 }
Chris@908 682 if (lrint(mindb) != lrint(maxdb)) {
Chris@199 683 dbstr = tr("%1 - %2").arg(mindbstr).arg(maxdbstr);
Chris@199 684 } else {
Chris@199 685 dbstr = tr("%1").arg(mindbstr);
Chris@199 686 }
Chris@199 687
Chris@199 688 QString description;
Chris@199 689
Chris@248 690 if (range > int(m_sliceableModel->getResolution())) {
Chris@199 691 description = tr("%1\nBin:\t%2 (%3)\n%4 value:\t%5\ndB:\t%6")
Chris@199 692 .arg(genericDesc)
Chris@199 693 .arg(binstr)
Chris@199 694 .arg(hzstr)
Chris@199 695 .arg(m_samplingMode == NearestSample ? tr("First") :
Chris@199 696 m_samplingMode == SampleMean ? tr("Mean") : tr("Peak"))
Chris@199 697 .arg(valuestr)
Chris@199 698 .arg(dbstr);
Chris@199 699 } else {
Chris@199 700 description = tr("%1\nBin:\t%2 (%3)\nValue:\t%4\ndB:\t%5")
Chris@199 701 .arg(genericDesc)
Chris@199 702 .arg(binstr)
Chris@199 703 .arg(hzstr)
Chris@199 704 .arg(valuestr)
Chris@199 705 .arg(dbstr);
Chris@199 706 }
Chris@199 707
Chris@199 708 return description;
Chris@199 709 }
Chris@199 710
Chris@254 711 void
Chris@916 712 SpectrumLayer::paint(LayerGeometryProvider *v, QPainter &paint, QRect rect) const
Chris@275 713 {
Chris@275 714 if (!m_originModel || !m_originModel->isOK() ||
Chris@349 715 !m_originModel->isReady()) {
Chris@587 716 SVDEBUG << "SpectrumLayer::paint: no origin model, or origin model not OK or not ready" << endl;
Chris@349 717 return;
Chris@349 718 }
Chris@275 719
Chris@275 720 if (m_newFFTNeeded) {
Chris@587 721 SVDEBUG << "SpectrumLayer::paint: new FFT needed, calling setupFFT" << endl;
Chris@275 722 const_cast<SpectrumLayer *>(this)->setupFFT(); //ugh
Chris@275 723 }
Chris@277 724
Chris@277 725 FFTModel *fft = dynamic_cast<FFTModel *>
Chris@277 726 (const_cast<DenseThreeDimensionalModel *>(m_sliceableModel));
Chris@277 727
Chris@1382 728 double thresh = (pow(10, -6) / m_gain) * (getFFTSize() / 2.0); // -60dB adj
Chris@277 729
Chris@607 730 int xorigin = getVerticalScaleWidth(v, false, paint) + 1;
Chris@1281 731 int scaleHeight = getHorizontalScaleHeight(v, paint);
Chris@345 732
Chris@1391 733 QPoint localPos;
Chris@1391 734 bool shouldIlluminate = v->shouldIlluminateLocalFeatures(this, localPos);
Chris@1391 735
Chris@1392 736 // cerr << "shouldIlluminate = " << shouldIlluminate << ", localPos = " << localPos.x() << "," << localPos.y() << endl;
Chris@1392 737
Chris@284 738 if (fft && m_showPeaks) {
Chris@277 739
Chris@277 740 // draw peak lines
Chris@277 741
Chris@908 742 int col = int(v->getCentreFrame() / fft->getResolution());
Chris@277 743
Chris@277 744 paint.save();
Chris@277 745 paint.setRenderHint(QPainter::Antialiasing, false);
Chris@277 746
Chris@1281 747 ColourMapper mapper =
Chris@1281 748 hasLightBackground() ?
Chris@1362 749 ColourMapper(ColourMapper::BlackOnWhite, m_colourInverted, 0, 1) :
Chris@1362 750 ColourMapper(ColourMapper::WhiteOnBlack, m_colourInverted, 0, 1);
Chris@1281 751
Chris@290 752 int peakminbin = 0;
Chris@290 753 int peakmaxbin = fft->getHeight() - 1;
Chris@908 754 double peakmaxfreq = Pitch::getFrequencyForPitch(128);
Chris@1387 755 peakmaxbin = int(((peakmaxfreq * fft->getHeight() * 2) /
Chris@1387 756 fft->getSampleRate()));
Chris@290 757
Chris@280 758 FFTModel::PeakSet peaks = fft->getPeakFrequencies
Chris@290 759 (FFTModel::MajorPitchAdaptivePeaks, col, peakminbin, peakmaxbin);
Chris@280 760
Chris@277 761 BiasCurve curve;
Chris@277 762 getBiasCurve(curve);
Chris@908 763 int cs = int(curve.size());
Chris@280 764
Chris@1387 765 int px = -1;
Chris@1387 766
Chris@1392 767 int fuzz = ViewManager::scalePixelSize(3);
Chris@1392 768 bool illuminatedSomething = false;
Chris@1391 769
Chris@280 770 for (FFTModel::PeakSet::iterator i = peaks.begin();
Chris@280 771 i != peaks.end(); ++i) {
Chris@280 772
Chris@1387 773 double freq = i->second;
Chris@1387 774 int x = int(lrint(getXForFrequency(v, freq)));
Chris@1387 775 if (x == px) {
Chris@1387 776 continue;
Chris@1387 777 }
Chris@1391 778
Chris@805 779 int bin = i->first;
Chris@277 780
Chris@682 781 // cerr << "bin = " << bin << ", thresh = " << thresh << ", value = " << fft->getMagnitudeAt(col, bin) << endl;
Chris@280 782
Chris@1385 783 double value = fft->getValueAt(col, bin);
Chris@1385 784 if (value < thresh) continue;
Chris@1385 785 if (bin < cs) value *= curve[bin];
Chris@1392 786
Chris@1392 787 double norm = 0.f;
Chris@1392 788 // we need the norm here for colour map; the y coord is
Chris@1392 789 // only used to pick a label height if illuminating the
Chris@1392 790 // local point
Chris@1392 791 double y = getYForValue(v, value, norm);
Chris@1391 792
Chris@1392 793 QColor colour = mapper.map(norm);
Chris@1392 794
Chris@1392 795 paint.setPen(QPen(colour, 1));
Chris@1392 796 paint.drawLine(x, 0, x, v->getPaintHeight() - scaleHeight - 1);
Chris@1392 797
Chris@1392 798 bool illuminateThis = false;
Chris@1392 799 if (shouldIlluminate && !illuminatedSomething &&
Chris@1392 800 std::abs(localPos.x() - x) <= fuzz) {
Chris@1392 801 illuminateThis = true;
Chris@1392 802 }
Chris@1392 803
Chris@1392 804 if (illuminateThis) {
Chris@1392 805 int labelY = v->getPaintHeight() -
Chris@1392 806 getHorizontalScaleHeight(v, paint) -
Chris@1392 807 paint.fontMetrics().height() * 3;
Chris@1392 808 QString text = tr("%1 Hz").arg(freq);
Chris@1392 809 int lw = paint.fontMetrics().width(text);
Chris@1392 810 int gap = ViewManager::scalePixelSize(3);
Chris@1392 811 double half = double(gap)/2.0;
Chris@1392 812 int labelX = x - lw - gap;
Chris@1392 813 if (labelX < getVerticalScaleWidth(v, false, paint)) {
Chris@1392 814 labelX = x + gap;
Chris@1392 815 }
Chris@1392 816 PaintAssistant::drawVisibleText
Chris@1392 817 (v, paint, labelX, labelY,
Chris@1392 818 text, PaintAssistant::OutlinedText);
Chris@1392 819 if (Pitch::isFrequencyInMidiRange(freq)) {
Chris@1392 820 QString pitchLabel = Pitch::getPitchLabelForFrequency(freq);
Chris@1392 821 PaintAssistant::drawVisibleText
Chris@1392 822 (v, paint,
Chris@1392 823 labelX, labelY + paint.fontMetrics().ascent() + gap,
Chris@1392 824 pitchLabel, PaintAssistant::OutlinedText);
Chris@1392 825 }
Chris@1392 826 paint.fillRect(QRectF(x - half, labelY + gap, gap, gap),
Chris@1392 827 colour);
Chris@1392 828 illuminatedSomething = true;
Chris@1391 829 }
Chris@277 830
Chris@1387 831 px = x;
Chris@277 832 }
Chris@277 833
Chris@277 834 paint.restore();
Chris@277 835 }
Chris@275 836
Chris@1281 837 paint.save();
Chris@1281 838
Chris@275 839 SliceLayer::paint(v, paint, rect);
Chris@1281 840
Chris@1281 841 paintHorizontalScale(v, paint, xorigin);
Chris@277 842
Chris@1281 843 paint.restore();
Chris@1281 844 }
Chris@1281 845
Chris@1281 846 int
Chris@1281 847 SpectrumLayer::getHorizontalScaleHeight(LayerGeometryProvider *v,
Chris@1281 848 QPainter &paint) const
Chris@1281 849 {
Chris@1281 850 int pkh = int(paint.fontMetrics().height() * 0.7 + 0.5);
Chris@1281 851 if (pkh < 10) pkh = 10;
Chris@1281 852
Chris@1281 853 int scaleh = HorizontalFrequencyScale().getHeight(v, paint);
Chris@1281 854
Chris@1281 855 return pkh + scaleh;
Chris@1281 856 }
Chris@1281 857
Chris@1281 858 void
Chris@1281 859 SpectrumLayer::paintHorizontalScale(LayerGeometryProvider *v,
Chris@1281 860 QPainter &paint,
Chris@1281 861 int xorigin) const
Chris@1281 862 {
Chris@278 863 //!!! All of this stuff relating to depicting frequencies
Chris@1238 864 // (keyboard, crosshairs etc) should be applicable to any slice
Chris@1238 865 // layer whose model has a vertical scale unit of Hz. However,
Chris@1238 866 // the dense 3d model at the moment doesn't record its vertical
Chris@1238 867 // scale unit -- we need to fix that and hoist this code as
Chris@1238 868 // appropriate. Same really goes for any code in SpectrogramLayer
Chris@1238 869 // that could be relevant to Colour3DPlotLayer with unit Hz, but
Chris@1238 870 // that's a bigger proposition.
Chris@278 871
Chris@1281 872 if (!v->getViewManager()->shouldShowHorizontalValueScale()) {
Chris@1281 873 return;
Chris@1281 874 }
Chris@1281 875
Chris@1281 876 int totalScaleHeight = getHorizontalScaleHeight(v, paint); // inc piano
Chris@1281 877 int freqScaleHeight = HorizontalFrequencyScale().getHeight(v, paint);
Chris@1281 878 int paintHeight = v->getPaintHeight();
Chris@1281 879 int paintWidth = v->getPaintWidth();
Chris@277 880
Chris@1238 881 PianoScale().paintPianoHorizontal
Chris@1276 882 (v, this, paint,
Chris@1281 883 QRect(xorigin, paintHeight - totalScaleHeight - 1,
Chris@1281 884 paintWidth - 1, totalScaleHeight - freqScaleHeight));
Chris@345 885
Chris@1281 886 int scaleLeft = int(getXForBin(v, 1));
Chris@1281 887
Chris@1281 888 paint.drawLine(int(getXForBin(v, 0)), paintHeight - freqScaleHeight,
Chris@1281 889 scaleLeft, paintHeight - freqScaleHeight);
Chris@1281 890
Chris@1281 891 QString hz = tr("Hz");
Chris@1281 892 int hzw = paint.fontMetrics().width(hz);
Chris@1281 893 if (scaleLeft > hzw + 5) {
Chris@1281 894 paint.drawText
Chris@1281 895 (scaleLeft - hzw - 5,
Chris@1281 896 paintHeight - freqScaleHeight + paint.fontMetrics().ascent() + 5,
Chris@1281 897 hz);
Chris@1281 898 }
Chris@1281 899
Chris@1281 900 HorizontalFrequencyScale().paintScale
Chris@1276 901 (v, this, paint,
Chris@1281 902 QRect(scaleLeft, paintHeight - freqScaleHeight,
Chris@1281 903 paintWidth, totalScaleHeight),
Chris@1281 904 m_binScale == LogBins);
Chris@275 905 }
Chris@275 906
Chris@275 907 void
Chris@254 908 SpectrumLayer::getBiasCurve(BiasCurve &curve) const
Chris@254 909 {
Chris@254 910 curve = m_biasCurve;
Chris@254 911 }
Chris@199 912
Chris@316 913 void
Chris@316 914 SpectrumLayer::toXml(QTextStream &stream,
Chris@316 915 QString indent, QString extraAttributes) const
Chris@220 916 {
Chris@316 917 QString s = QString("windowSize=\"%1\" "
Chris@456 918 "windowHopLevel=\"%2\" "
Chris@1382 919 "oversampling=\"%3\" "
Chris@1382 920 "showPeaks=\"%4\" ")
Chris@220 921 .arg(m_windowSize)
Chris@456 922 .arg(m_windowHopLevel)
Chris@1382 923 .arg(m_oversampling)
Chris@456 924 .arg(m_showPeaks ? "true" : "false");
Chris@220 925
Chris@316 926 SliceLayer::toXml(stream, indent, extraAttributes + " " + s);
Chris@220 927 }
Chris@220 928
Chris@220 929 void
Chris@220 930 SpectrumLayer::setProperties(const QXmlAttributes &attributes)
Chris@220 931 {
Chris@220 932 SliceLayer::setProperties(attributes);
Chris@220 933
Chris@220 934 bool ok = false;
Chris@220 935
Chris@805 936 int windowSize = attributes.value("windowSize").toUInt(&ok);
Chris@220 937 if (ok) setWindowSize(windowSize);
Chris@220 938
Chris@805 939 int windowHopLevel = attributes.value("windowHopLevel").toUInt(&ok);
Chris@220 940 if (ok) setWindowHopLevel(windowHopLevel);
Chris@456 941
Chris@1382 942 int oversampling = attributes.value("oversampling").toUInt(&ok);
Chris@1382 943 if (ok) setOversampling(oversampling);
Chris@1382 944
Chris@456 945 bool showPeaks = (attributes.value("showPeaks").trimmed() == "true");
Chris@456 946 setShowPeaks(showPeaks);
Chris@220 947 }
Chris@220 948
Chris@220 949