annotate layer/SpectrumLayer.cpp @ 1471:f2525e6cbdf1 by-id

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