annotate layer/SpectrumLayer.cpp @ 1383:86f319dd6ab9 spectrogramparam

Switch spectrum to antialiased drawing with considerable speedup for the dense areas; take horizontal scale height into account when rendering slice layer height
author Chris Cannam
date Wed, 07 Nov 2018 15:59:10 +0000
parents 2df1af7ac752
children 37e9d6a1e00c
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@153 290 m_windowSize = ws;
Chris@275 291 m_newFFTNeeded = true;
Chris@153 292 emit layerParametersChanged();
Chris@153 293 }
Chris@153 294
Chris@153 295 void
Chris@805 296 SpectrumLayer::setWindowHopLevel(int v)
Chris@153 297 {
Chris@153 298 if (m_windowHopLevel == v) return;
Chris@153 299 m_windowHopLevel = v;
Chris@275 300 m_newFFTNeeded = true;
Chris@153 301 emit layerParametersChanged();
Chris@153 302 }
Chris@153 303
Chris@153 304 void
Chris@153 305 SpectrumLayer::setWindowType(WindowType w)
Chris@153 306 {
Chris@153 307 if (m_windowType == w) return;
Chris@153 308 m_windowType = w;
Chris@275 309 m_newFFTNeeded = true;
Chris@153 310 emit layerParametersChanged();
Chris@153 311 }
Chris@153 312
Chris@153 313 void
Chris@1382 314 SpectrumLayer::setOversampling(int oversampling)
Chris@1382 315 {
Chris@1382 316 if (m_oversampling == oversampling) return;
Chris@1382 317 m_oversampling = oversampling;
Chris@1382 318 m_newFFTNeeded = true;
Chris@1382 319 emit layerParametersChanged();
Chris@1382 320 }
Chris@1382 321
Chris@1382 322 int
Chris@1382 323 SpectrumLayer::getOversampling() const
Chris@1382 324 {
Chris@1382 325 return m_oversampling;
Chris@1382 326 }
Chris@1382 327
Chris@1382 328 void
Chris@284 329 SpectrumLayer::setShowPeaks(bool show)
Chris@284 330 {
Chris@284 331 if (m_showPeaks == show) return;
Chris@284 332 m_showPeaks = show;
Chris@284 333 emit layerParametersChanged();
Chris@284 334 }
Chris@284 335
Chris@284 336 void
Chris@153 337 SpectrumLayer::preferenceChanged(PropertyContainer::PropertyName name)
Chris@153 338 {
Chris@153 339 if (name == "Window Type") {
Chris@1382 340 auto type = Preferences::getInstance()->getWindowType();
Chris@1382 341 SVDEBUG << "SpectrumLayer::preferenceChanged: Window type changed to "
Chris@1382 342 << type << endl;
Chris@1382 343 setWindowType(type);
Chris@153 344 return;
Chris@153 345 }
Chris@153 346 }
Chris@153 347
Chris@1238 348 double
Chris@1238 349 SpectrumLayer::getFrequencyForX(const LayerGeometryProvider *v, double x) const
Chris@133 350 {
Chris@1238 351 if (!m_sliceableModel) return 0;
Chris@1238 352 double bin = getBinForX(v, x);
Chris@1281 353 // we assume the frequency of a bin corresponds to the centre of
Chris@1281 354 // its visual range
Chris@1281 355 bin -= 0.5;
Chris@1238 356 return (m_sliceableModel->getSampleRate() * bin) /
Chris@1238 357 (m_sliceableModel->getHeight() * 2);
Chris@133 358 }
Chris@133 359
Chris@908 360 double
Chris@1238 361 SpectrumLayer::getXForFrequency(const LayerGeometryProvider *v, double freq) const
Chris@265 362 {
Chris@280 363 if (!m_sliceableModel) return 0;
Chris@1238 364 double bin = (freq * m_sliceableModel->getHeight() * 2) /
Chris@1238 365 m_sliceableModel->getSampleRate();
Chris@1281 366 // we want the centre of the bin range
Chris@1281 367 bin += 0.5;
Chris@1238 368 return getXForBin(v, bin);
Chris@254 369 }
Chris@254 370
Chris@260 371 bool
Chris@918 372 SpectrumLayer::getXScaleValue(const LayerGeometryProvider *v, int x,
Chris@908 373 double &value, QString &unit) const
Chris@260 374 {
Chris@1238 375 value = getFrequencyForX(v, x);
Chris@260 376 unit = "Hz";
Chris@260 377 return true;
Chris@260 378 }
Chris@260 379
Chris@264 380 bool
Chris@918 381 SpectrumLayer::getYScaleValue(const LayerGeometryProvider *v, int y,
Chris@908 382 double &value, QString &unit) const
Chris@274 383 {
Chris@1238 384 value = getValueForY(v, y);
Chris@274 385
Chris@274 386 if (m_energyScale == dBScale || m_energyScale == MeterScale) {
Chris@274 387
Chris@908 388 if (value > 0.0) {
Chris@908 389 value = 10.0 * log10(value);
Chris@284 390 if (value < m_threshold) value = m_threshold;
Chris@284 391 } else value = m_threshold;
Chris@274 392
Chris@274 393 unit = "dBV";
Chris@274 394
Chris@274 395 } else {
Chris@274 396 unit = "V";
Chris@274 397 }
Chris@274 398
Chris@274 399 return true;
Chris@274 400 }
Chris@274 401
Chris@274 402 bool
Chris@918 403 SpectrumLayer::getYScaleDifference(const LayerGeometryProvider *v, int y0, int y1,
Chris@908 404 double &diff, QString &unit) const
Chris@274 405 {
Chris@274 406 bool rv = SliceLayer::getYScaleDifference(v, y0, y1, diff, unit);
Chris@274 407 if (rv && (unit == "dBV")) unit = "dB";
Chris@274 408 return rv;
Chris@274 409 }
Chris@274 410
Chris@274 411
Chris@274 412 bool
Chris@918 413 SpectrumLayer::getCrosshairExtents(LayerGeometryProvider *v, QPainter &paint,
Chris@264 414 QPoint cursorPos,
Chris@264 415 std::vector<QRect> &extents) const
Chris@264 416 {
Chris@918 417 QRect vertical(cursorPos.x(), cursorPos.y(), 1, v->getPaintHeight() - cursorPos.y());
Chris@264 418 extents.push_back(vertical);
Chris@264 419
Chris@918 420 QRect horizontal(0, cursorPos.y(), v->getPaintWidth(), 12);
Chris@264 421 extents.push_back(horizontal);
Chris@264 422
Chris@280 423 int hoffset = 2;
Chris@280 424 if (m_binScale == LogBins) hoffset = 13;
Chris@278 425
Chris@607 426 int sw = getVerticalScaleWidth(v, false, paint);
Chris@280 427
Chris@280 428 QRect value(sw, cursorPos.y() - paint.fontMetrics().ascent() - 2,
Chris@280 429 paint.fontMetrics().width("0.0000001 V") + 2,
Chris@264 430 paint.fontMetrics().height());
Chris@280 431 extents.push_back(value);
Chris@280 432
Chris@280 433 QRect log(sw, cursorPos.y() + 2,
Chris@280 434 paint.fontMetrics().width("-80.000 dBV") + 2,
Chris@280 435 paint.fontMetrics().height());
Chris@280 436 extents.push_back(log);
Chris@280 437
Chris@280 438 QRect freq(cursorPos.x(),
Chris@918 439 v->getPaintHeight() - paint.fontMetrics().height() - hoffset,
Chris@280 440 paint.fontMetrics().width("123456 Hz") + 2,
Chris@280 441 paint.fontMetrics().height());
Chris@280 442 extents.push_back(freq);
Chris@264 443
Chris@278 444 int w(paint.fontMetrics().width("C#10+50c") + 2);
Chris@278 445 QRect pitch(cursorPos.x() - w,
Chris@918 446 v->getPaintHeight() - paint.fontMetrics().height() - hoffset,
Chris@278 447 w,
Chris@278 448 paint.fontMetrics().height());
Chris@278 449 extents.push_back(pitch);
Chris@278 450
Chris@264 451 return true;
Chris@264 452 }
Chris@264 453
Chris@254 454 void
Chris@918 455 SpectrumLayer::paintCrosshairs(LayerGeometryProvider *v, QPainter &paint,
Chris@254 456 QPoint cursorPos) const
Chris@254 457 {
Chris@280 458 if (!m_sliceableModel) return;
Chris@280 459
Chris@254 460 paint.save();
Chris@282 461 QFont fn = paint.font();
Chris@282 462 if (fn.pointSize() > 8) {
Chris@282 463 fn.setPointSize(fn.pointSize() - 1);
Chris@282 464 paint.setFont(fn);
Chris@282 465 }
Chris@254 466
Chris@1362 467 ColourMapper mapper(m_colourMap, m_colourInverted, 0, 1);
Chris@254 468 paint.setPen(mapper.getContrastingColour());
Chris@254 469
Chris@1238 470 int xorigin = m_xorigins[v->getId()];
Chris@918 471 paint.drawLine(xorigin, cursorPos.y(), v->getPaintWidth(), cursorPos.y());
Chris@918 472 paint.drawLine(cursorPos.x(), cursorPos.y(), cursorPos.x(), v->getPaintHeight());
Chris@254 473
Chris@1238 474 double fundamental = getFrequencyForX(v, cursorPos.x());
Chris@254 475
Chris@280 476 int hoffset = 2;
Chris@280 477 if (m_binScale == LogBins) hoffset = 13;
Chris@278 478
Chris@1078 479 PaintAssistant::drawVisibleText(v, paint,
Chris@1238 480 cursorPos.x() + 2,
Chris@1238 481 v->getPaintHeight() - 2 - hoffset,
Chris@1238 482 QString("%1 Hz").arg(fundamental),
Chris@1238 483 PaintAssistant::OutlinedText);
Chris@278 484
Chris@278 485 if (Pitch::isFrequencyInMidiRange(fundamental)) {
Chris@278 486 QString pitchLabel = Pitch::getPitchLabelForFrequency(fundamental);
Chris@1078 487 PaintAssistant::drawVisibleText(v, paint,
Chris@1238 488 cursorPos.x() -
Chris@1238 489 paint.fontMetrics().width(pitchLabel) - 2,
Chris@1238 490 v->getPaintHeight() - 2 - hoffset,
Chris@1238 491 pitchLabel,
Chris@1238 492 PaintAssistant::OutlinedText);
Chris@278 493 }
Chris@264 494
Chris@1238 495 double value = getValueForY(v, cursorPos.y());
Chris@908 496 double thresh = m_threshold;
Chris@908 497 double db = thresh;
Chris@908 498 if (value > 0.0) db = 10.0 * log10(value);
Chris@280 499 if (db < thresh) db = thresh;
Chris@280 500
Chris@1078 501 PaintAssistant::drawVisibleText(v, paint,
Chris@280 502 xorigin + 2,
Chris@280 503 cursorPos.y() - 2,
Chris@280 504 QString("%1 V").arg(value),
Chris@1078 505 PaintAssistant::OutlinedText);
Chris@280 506
Chris@1078 507 PaintAssistant::drawVisibleText(v, paint,
Chris@280 508 xorigin + 2,
Chris@280 509 cursorPos.y() + 2 + paint.fontMetrics().ascent(),
Chris@280 510 QString("%1 dBV").arg(db),
Chris@1078 511 PaintAssistant::OutlinedText);
Chris@280 512
Chris@254 513 int harmonic = 2;
Chris@254 514
Chris@254 515 while (harmonic < 100) {
Chris@254 516
Chris@1238 517 int hx = int(lrint(getXForFrequency(v, fundamental * harmonic)));
Chris@254 518
Chris@918 519 if (hx < xorigin || hx > v->getPaintWidth()) break;
Chris@254 520
Chris@254 521 int len = 7;
Chris@254 522
Chris@254 523 if (harmonic % 2 == 0) {
Chris@254 524 if (harmonic % 4 == 0) {
Chris@254 525 len = 12;
Chris@254 526 } else {
Chris@254 527 len = 10;
Chris@254 528 }
Chris@254 529 }
Chris@254 530
Chris@908 531 paint.drawLine(hx,
Chris@254 532 cursorPos.y(),
Chris@908 533 hx,
Chris@254 534 cursorPos.y() + len);
Chris@254 535
Chris@254 536 ++harmonic;
Chris@254 537 }
Chris@254 538
Chris@254 539 paint.restore();
Chris@254 540 }
Chris@254 541
Chris@199 542 QString
Chris@918 543 SpectrumLayer::getFeatureDescription(LayerGeometryProvider *v, QPoint &p) const
Chris@199 544 {
Chris@199 545 if (!m_sliceableModel) return "";
Chris@199 546
Chris@199 547 int minbin = 0, maxbin = 0, range = 0;
Chris@805 548 QString genericDesc = SliceLayer::getFeatureDescriptionAux
Chris@199 549 (v, p, false, minbin, maxbin, range);
Chris@199 550
Chris@199 551 if (genericDesc == "") return "";
Chris@199 552
Chris@1238 553 int i0 = minbin - m_minbin;
Chris@1238 554 int i1 = maxbin - m_minbin;
Chris@1238 555
Chris@1238 556 float minvalue = 0.0;
Chris@1238 557 if (in_range_for(m_values, i0)) minvalue = m_values[i0];
Chris@199 558
Chris@1238 559 float maxvalue = minvalue;
Chris@1238 560 if (in_range_for(m_values, i1)) maxvalue = m_values[i1];
Chris@1238 561
Chris@199 562 if (minvalue > maxvalue) std::swap(minvalue, maxvalue);
Chris@199 563
Chris@199 564 QString binstr;
Chris@199 565 QString hzstr;
Chris@908 566 int minfreq = int(lrint((minbin * m_sliceableModel->getSampleRate()) /
Chris@1382 567 getFFTSize()));
Chris@1256 568 int maxfreq = int(lrint((std::max(maxbin, minbin)
Chris@908 569 * m_sliceableModel->getSampleRate()) /
Chris@1382 570 getFFTSize()));
Chris@199 571
Chris@199 572 if (maxbin != minbin) {
Chris@199 573 binstr = tr("%1 - %2").arg(minbin+1).arg(maxbin+1);
Chris@199 574 } else {
Chris@199 575 binstr = QString("%1").arg(minbin+1);
Chris@199 576 }
Chris@199 577 if (minfreq != maxfreq) {
Chris@199 578 hzstr = tr("%1 - %2 Hz").arg(minfreq).arg(maxfreq);
Chris@199 579 } else {
Chris@199 580 hzstr = tr("%1 Hz").arg(minfreq);
Chris@199 581 }
Chris@199 582
Chris@199 583 QString valuestr;
Chris@199 584 if (maxvalue != minvalue) {
Chris@199 585 valuestr = tr("%1 - %2").arg(minvalue).arg(maxvalue);
Chris@199 586 } else {
Chris@199 587 valuestr = QString("%1").arg(minvalue);
Chris@199 588 }
Chris@199 589
Chris@199 590 QString dbstr;
Chris@908 591 double mindb = AudioLevel::multiplier_to_dB(minvalue);
Chris@908 592 double maxdb = AudioLevel::multiplier_to_dB(maxvalue);
Chris@199 593 QString mindbstr;
Chris@199 594 QString maxdbstr;
Chris@199 595 if (mindb == AudioLevel::DB_FLOOR) {
Chris@1147 596 mindbstr = Strings::minus_infinity;
Chris@199 597 } else {
Chris@908 598 mindbstr = QString("%1").arg(lrint(mindb));
Chris@199 599 }
Chris@199 600 if (maxdb == AudioLevel::DB_FLOOR) {
Chris@1147 601 maxdbstr = Strings::minus_infinity;
Chris@199 602 } else {
Chris@908 603 maxdbstr = QString("%1").arg(lrint(maxdb));
Chris@199 604 }
Chris@908 605 if (lrint(mindb) != lrint(maxdb)) {
Chris@199 606 dbstr = tr("%1 - %2").arg(mindbstr).arg(maxdbstr);
Chris@199 607 } else {
Chris@199 608 dbstr = tr("%1").arg(mindbstr);
Chris@199 609 }
Chris@199 610
Chris@199 611 QString description;
Chris@199 612
Chris@248 613 if (range > int(m_sliceableModel->getResolution())) {
Chris@199 614 description = tr("%1\nBin:\t%2 (%3)\n%4 value:\t%5\ndB:\t%6")
Chris@199 615 .arg(genericDesc)
Chris@199 616 .arg(binstr)
Chris@199 617 .arg(hzstr)
Chris@199 618 .arg(m_samplingMode == NearestSample ? tr("First") :
Chris@199 619 m_samplingMode == SampleMean ? tr("Mean") : tr("Peak"))
Chris@199 620 .arg(valuestr)
Chris@199 621 .arg(dbstr);
Chris@199 622 } else {
Chris@199 623 description = tr("%1\nBin:\t%2 (%3)\nValue:\t%4\ndB:\t%5")
Chris@199 624 .arg(genericDesc)
Chris@199 625 .arg(binstr)
Chris@199 626 .arg(hzstr)
Chris@199 627 .arg(valuestr)
Chris@199 628 .arg(dbstr);
Chris@199 629 }
Chris@199 630
Chris@199 631 return description;
Chris@199 632 }
Chris@199 633
Chris@254 634 void
Chris@916 635 SpectrumLayer::paint(LayerGeometryProvider *v, QPainter &paint, QRect rect) const
Chris@275 636 {
Chris@275 637 if (!m_originModel || !m_originModel->isOK() ||
Chris@349 638 !m_originModel->isReady()) {
Chris@587 639 SVDEBUG << "SpectrumLayer::paint: no origin model, or origin model not OK or not ready" << endl;
Chris@349 640 return;
Chris@349 641 }
Chris@275 642
Chris@275 643 if (m_newFFTNeeded) {
Chris@587 644 SVDEBUG << "SpectrumLayer::paint: new FFT needed, calling setupFFT" << endl;
Chris@275 645 const_cast<SpectrumLayer *>(this)->setupFFT(); //ugh
Chris@275 646 }
Chris@277 647
Chris@277 648 FFTModel *fft = dynamic_cast<FFTModel *>
Chris@277 649 (const_cast<DenseThreeDimensionalModel *>(m_sliceableModel));
Chris@277 650
Chris@1382 651 double thresh = (pow(10, -6) / m_gain) * (getFFTSize() / 2.0); // -60dB adj
Chris@277 652
Chris@607 653 int xorigin = getVerticalScaleWidth(v, false, paint) + 1;
Chris@1281 654 int scaleHeight = getHorizontalScaleHeight(v, paint);
Chris@345 655
Chris@284 656 if (fft && m_showPeaks) {
Chris@277 657
Chris@277 658 // draw peak lines
Chris@277 659
Chris@908 660 int col = int(v->getCentreFrame() / fft->getResolution());
Chris@277 661
Chris@277 662 paint.save();
Chris@277 663 paint.setRenderHint(QPainter::Antialiasing, false);
Chris@277 664
Chris@1281 665 ColourMapper mapper =
Chris@1281 666 hasLightBackground() ?
Chris@1362 667 ColourMapper(ColourMapper::BlackOnWhite, m_colourInverted, 0, 1) :
Chris@1362 668 ColourMapper(ColourMapper::WhiteOnBlack, m_colourInverted, 0, 1);
Chris@1281 669
Chris@290 670 int peakminbin = 0;
Chris@290 671 int peakmaxbin = fft->getHeight() - 1;
Chris@908 672 double peakmaxfreq = Pitch::getFrequencyForPitch(128);
Chris@908 673 peakmaxbin = int(((peakmaxfreq * fft->getHeight() * 2) / fft->getSampleRate()));
Chris@290 674
Chris@280 675 FFTModel::PeakSet peaks = fft->getPeakFrequencies
Chris@290 676 (FFTModel::MajorPitchAdaptivePeaks, col, peakminbin, peakmaxbin);
Chris@280 677
Chris@277 678 BiasCurve curve;
Chris@277 679 getBiasCurve(curve);
Chris@908 680 int cs = int(curve.size());
Chris@280 681
Chris@908 682 std::vector<double> values;
Chris@277 683
Chris@805 684 for (int bin = 0; bin < fft->getHeight(); ++bin) {
Chris@908 685 double value = m_sliceableModel->getValueAt(col, bin);
Chris@280 686 if (bin < cs) value *= curve[bin];
Chris@280 687 values.push_back(value);
Chris@280 688 }
Chris@280 689
Chris@280 690 for (FFTModel::PeakSet::iterator i = peaks.begin();
Chris@280 691 i != peaks.end(); ++i) {
Chris@280 692
Chris@805 693 int bin = i->first;
Chris@277 694
Chris@682 695 // cerr << "bin = " << bin << ", thresh = " << thresh << ", value = " << fft->getMagnitudeAt(col, bin) << endl;
Chris@280 696
Chris@908 697 if (!fft->isOverThreshold(col, bin, float(thresh))) continue;
Chris@277 698
Chris@908 699 double freq = i->second;
Chris@280 700
Chris@1238 701 int x = int(lrint(getXForFrequency(v, freq)));
Chris@277 702
Chris@908 703 double norm = 0.f;
Chris@1238 704 (void)getYForValue(v, values[bin], norm); // don't need return value, need norm
Chris@277 705
Chris@277 706 paint.setPen(mapper.map(norm));
Chris@1281 707 paint.drawLine(x, 0, x, v->getPaintHeight() - scaleHeight - 1);
Chris@277 708 }
Chris@277 709
Chris@277 710 paint.restore();
Chris@277 711 }
Chris@275 712
Chris@1281 713 paint.save();
Chris@1281 714
Chris@275 715 SliceLayer::paint(v, paint, rect);
Chris@1281 716
Chris@1281 717 paintHorizontalScale(v, paint, xorigin);
Chris@277 718
Chris@1281 719 paint.restore();
Chris@1281 720 }
Chris@1281 721
Chris@1281 722 int
Chris@1281 723 SpectrumLayer::getHorizontalScaleHeight(LayerGeometryProvider *v,
Chris@1281 724 QPainter &paint) const
Chris@1281 725 {
Chris@1281 726 int pkh = int(paint.fontMetrics().height() * 0.7 + 0.5);
Chris@1281 727 if (pkh < 10) pkh = 10;
Chris@1281 728
Chris@1281 729 int scaleh = HorizontalFrequencyScale().getHeight(v, paint);
Chris@1281 730
Chris@1281 731 return pkh + scaleh;
Chris@1281 732 }
Chris@1281 733
Chris@1281 734 void
Chris@1281 735 SpectrumLayer::paintHorizontalScale(LayerGeometryProvider *v,
Chris@1281 736 QPainter &paint,
Chris@1281 737 int xorigin) const
Chris@1281 738 {
Chris@278 739 //!!! All of this stuff relating to depicting frequencies
Chris@1238 740 // (keyboard, crosshairs etc) should be applicable to any slice
Chris@1238 741 // layer whose model has a vertical scale unit of Hz. However,
Chris@1238 742 // the dense 3d model at the moment doesn't record its vertical
Chris@1238 743 // scale unit -- we need to fix that and hoist this code as
Chris@1238 744 // appropriate. Same really goes for any code in SpectrogramLayer
Chris@1238 745 // that could be relevant to Colour3DPlotLayer with unit Hz, but
Chris@1238 746 // that's a bigger proposition.
Chris@278 747
Chris@1281 748 if (!v->getViewManager()->shouldShowHorizontalValueScale()) {
Chris@1281 749 return;
Chris@1281 750 }
Chris@1281 751
Chris@1281 752 int totalScaleHeight = getHorizontalScaleHeight(v, paint); // inc piano
Chris@1281 753 int freqScaleHeight = HorizontalFrequencyScale().getHeight(v, paint);
Chris@1281 754 int paintHeight = v->getPaintHeight();
Chris@1281 755 int paintWidth = v->getPaintWidth();
Chris@277 756
Chris@1238 757 PianoScale().paintPianoHorizontal
Chris@1276 758 (v, this, paint,
Chris@1281 759 QRect(xorigin, paintHeight - totalScaleHeight - 1,
Chris@1281 760 paintWidth - 1, totalScaleHeight - freqScaleHeight));
Chris@345 761
Chris@1281 762 int scaleLeft = int(getXForBin(v, 1));
Chris@1281 763
Chris@1281 764 paint.drawLine(int(getXForBin(v, 0)), paintHeight - freqScaleHeight,
Chris@1281 765 scaleLeft, paintHeight - freqScaleHeight);
Chris@1281 766
Chris@1281 767 QString hz = tr("Hz");
Chris@1281 768 int hzw = paint.fontMetrics().width(hz);
Chris@1281 769 if (scaleLeft > hzw + 5) {
Chris@1281 770 paint.drawText
Chris@1281 771 (scaleLeft - hzw - 5,
Chris@1281 772 paintHeight - freqScaleHeight + paint.fontMetrics().ascent() + 5,
Chris@1281 773 hz);
Chris@1281 774 }
Chris@1281 775
Chris@1281 776 HorizontalFrequencyScale().paintScale
Chris@1276 777 (v, this, paint,
Chris@1281 778 QRect(scaleLeft, paintHeight - freqScaleHeight,
Chris@1281 779 paintWidth, totalScaleHeight),
Chris@1281 780 m_binScale == LogBins);
Chris@275 781 }
Chris@275 782
Chris@275 783 void
Chris@254 784 SpectrumLayer::getBiasCurve(BiasCurve &curve) const
Chris@254 785 {
Chris@254 786 curve = m_biasCurve;
Chris@254 787 }
Chris@199 788
Chris@316 789 void
Chris@316 790 SpectrumLayer::toXml(QTextStream &stream,
Chris@316 791 QString indent, QString extraAttributes) const
Chris@220 792 {
Chris@316 793 QString s = QString("windowSize=\"%1\" "
Chris@456 794 "windowHopLevel=\"%2\" "
Chris@1382 795 "oversampling=\"%3\" "
Chris@1382 796 "showPeaks=\"%4\" ")
Chris@220 797 .arg(m_windowSize)
Chris@456 798 .arg(m_windowHopLevel)
Chris@1382 799 .arg(m_oversampling)
Chris@456 800 .arg(m_showPeaks ? "true" : "false");
Chris@220 801
Chris@316 802 SliceLayer::toXml(stream, indent, extraAttributes + " " + s);
Chris@220 803 }
Chris@220 804
Chris@220 805 void
Chris@220 806 SpectrumLayer::setProperties(const QXmlAttributes &attributes)
Chris@220 807 {
Chris@220 808 SliceLayer::setProperties(attributes);
Chris@220 809
Chris@220 810 bool ok = false;
Chris@220 811
Chris@805 812 int windowSize = attributes.value("windowSize").toUInt(&ok);
Chris@220 813 if (ok) setWindowSize(windowSize);
Chris@220 814
Chris@805 815 int windowHopLevel = attributes.value("windowHopLevel").toUInt(&ok);
Chris@220 816 if (ok) setWindowHopLevel(windowHopLevel);
Chris@456 817
Chris@1382 818 int oversampling = attributes.value("oversampling").toUInt(&ok);
Chris@1382 819 if (ok) setOversampling(oversampling);
Chris@1382 820
Chris@456 821 bool showPeaks = (attributes.value("showPeaks").trimmed() == "true");
Chris@456 822 setShowPeaks(showPeaks);
Chris@220 823 }
Chris@220 824
Chris@220 825