annotate layer/SpectrogramLayer.cpp @ 1090:c8c747783110 spectrogram-minor-refactor

Cut over to using the renderer (though it's very incomplete) and fix some cache problems and pointer lifetime misunderstandings
author Chris Cannam
date Tue, 05 Jul 2016 17:48:26 +0100
parents c8683d94442a
children ac10a087e045
rev   line source
Chris@58 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@0 2
Chris@0 3 /*
Chris@59 4 Sonic Visualiser
Chris@59 5 An audio file viewer and annotation editor.
Chris@59 6 Centre for Digital Music, Queen Mary, University of London.
Chris@484 7 This file copyright 2006-2009 Chris Cannam and QMUL.
Chris@0 8
Chris@59 9 This program is free software; you can redistribute it and/or
Chris@59 10 modify it under the terms of the GNU General Public License as
Chris@59 11 published by the Free Software Foundation; either version 2 of the
Chris@59 12 License, or (at your option) any later version. See the file
Chris@59 13 COPYING included with this distribution for more information.
Chris@0 14 */
Chris@0 15
Chris@0 16 #include "SpectrogramLayer.h"
Chris@0 17
Chris@128 18 #include "view/View.h"
Chris@0 19 #include "base/Profiler.h"
Chris@0 20 #include "base/AudioLevel.h"
Chris@0 21 #include "base/Window.h"
Chris@24 22 #include "base/Pitch.h"
Chris@118 23 #include "base/Preferences.h"
Chris@167 24 #include "base/RangeMapper.h"
Chris@253 25 #include "base/LogRange.h"
Chris@1063 26 #include "base/ColumnOp.h"
Chris@376 27 #include "widgets/CommandHistory.h"
Chris@1078 28 #include "data/model/Dense3DModelPeakCache.h"
Chris@1078 29
Chris@376 30 #include "ColourMapper.h"
Chris@283 31 #include "ImageRegionFinder.h"
Chris@690 32 #include "PianoScale.h"
Chris@1078 33 #include "PaintAssistant.h"
Chris@1089 34 #include "Colour3DPlotRenderer.h"
Chris@0 35
Chris@0 36 #include <QPainter>
Chris@0 37 #include <QImage>
Chris@0 38 #include <QPixmap>
Chris@0 39 #include <QRect>
Chris@92 40 #include <QApplication>
Chris@178 41 #include <QMessageBox>
Chris@283 42 #include <QMouseEvent>
Chris@316 43 #include <QTextStream>
Chris@1017 44 #include <QSettings>
Chris@0 45
Chris@0 46 #include <iostream>
Chris@0 47
Chris@0 48 #include <cassert>
Chris@0 49 #include <cmath>
Chris@0 50
Chris@545 51 #ifndef __GNUC__
Chris@545 52 #include <alloca.h>
Chris@545 53 #endif
Chris@545 54
Chris@1090 55 #define DEBUG_SPECTROGRAM 1
Chris@1044 56 #define DEBUG_SPECTROGRAM_REPAINT 1
Chris@1025 57
Chris@1025 58 using namespace std;
Chris@907 59
Chris@44 60 SpectrogramLayer::SpectrogramLayer(Configuration config) :
Chris@0 61 m_model(0),
Chris@0 62 m_channel(0),
Chris@0 63 m_windowSize(1024),
Chris@0 64 m_windowType(HanningWindow),
Chris@97 65 m_windowHopLevel(2),
Chris@0 66 m_gain(1.0),
Chris@215 67 m_initialGain(1.0),
Chris@37 68 m_threshold(0.0),
Chris@215 69 m_initialThreshold(0.0),
Chris@9 70 m_colourRotation(0),
Chris@215 71 m_initialRotation(0),
Chris@119 72 m_minFrequency(10),
Chris@0 73 m_maxFrequency(8000),
Chris@135 74 m_initialMaxFrequency(8000),
Chris@0 75 m_colourScale(dBColourScale),
Chris@197 76 m_colourMap(0),
Chris@0 77 m_frequencyScale(LinearFrequencyScale),
Chris@37 78 m_binDisplay(AllBins),
Chris@1063 79 m_normalization(ColumnOp::NoNormalization),
Chris@133 80 m_lastEmittedZoomStep(-1),
Chris@390 81 m_synchronous(false),
Chris@608 82 m_haveDetailedScale(false),
Chris@193 83 m_exiting(false),
Chris@1088 84 m_fftModel(0),
Chris@1088 85 m_peakCache(0),
Chris@1088 86 m_peakCacheDivisor(8)
Chris@0 87 {
Chris@1017 88 QString colourConfigName = "spectrogram-colour";
Chris@1017 89 int colourConfigDefault = int(ColourMapper::Green);
Chris@1017 90
Chris@215 91 if (config == FullRangeDb) {
Chris@215 92 m_initialMaxFrequency = 0;
Chris@215 93 setMaxFrequency(0);
Chris@215 94 } else if (config == MelodicRange) {
Chris@0 95 setWindowSize(8192);
Chris@97 96 setWindowHopLevel(4);
Chris@215 97 m_initialMaxFrequency = 1500;
Chris@215 98 setMaxFrequency(1500);
Chris@215 99 setMinFrequency(40);
Chris@0 100 setColourScale(LinearColourScale);
Chris@215 101 setColourMap(ColourMapper::Sunset);
Chris@215 102 setFrequencyScale(LogFrequencyScale);
Chris@1017 103 colourConfigName = "spectrogram-melodic-colour";
Chris@1017 104 colourConfigDefault = int(ColourMapper::Sunset);
Chris@224 105 // setGain(20);
Chris@37 106 } else if (config == MelodicPeaks) {
Chris@37 107 setWindowSize(4096);
Chris@97 108 setWindowHopLevel(5);
Chris@135 109 m_initialMaxFrequency = 2000;
Chris@40 110 setMaxFrequency(2000);
Chris@37 111 setMinFrequency(40);
Chris@37 112 setFrequencyScale(LogFrequencyScale);
Chris@215 113 setColourScale(LinearColourScale);
Chris@37 114 setBinDisplay(PeakFrequencies);
Chris@1063 115 setNormalization(ColumnOp::NormalizeColumns);
Chris@1017 116 colourConfigName = "spectrogram-melodic-colour";
Chris@1017 117 colourConfigDefault = int(ColourMapper::Sunset);
Chris@0 118 }
Chris@110 119
Chris@1017 120 QSettings settings;
Chris@1017 121 settings.beginGroup("Preferences");
Chris@1017 122 setColourMap(settings.value(colourConfigName, colourConfigDefault).toInt());
Chris@1017 123 settings.endGroup();
Chris@1017 124
Chris@122 125 Preferences *prefs = Preferences::getInstance();
Chris@122 126 connect(prefs, SIGNAL(propertyChanged(PropertyContainer::PropertyName)),
Chris@122 127 this, SLOT(preferenceChanged(PropertyContainer::PropertyName)));
Chris@122 128 setWindowType(prefs->getWindowType());
Chris@122 129
Chris@197 130 initialisePalette();
Chris@0 131 }
Chris@0 132
Chris@0 133 SpectrogramLayer::~SpectrogramLayer()
Chris@0 134 {
Chris@1088 135 invalidateFFTModel();
Chris@0 136 }
Chris@0 137
Chris@0 138 void
Chris@0 139 SpectrogramLayer::setModel(const DenseTimeValueModel *model)
Chris@0 140 {
Chris@682 141 // cerr << "SpectrogramLayer(" << this << "): setModel(" << model << ")" << endl;
Chris@34 142
Chris@110 143 if (model == m_model) return;
Chris@110 144
Chris@0 145 m_model = model;
Chris@1088 146 invalidateFFTModel();
Chris@0 147
Chris@0 148 if (!m_model || !m_model->isOK()) return;
Chris@0 149
Chris@320 150 connectSignals(m_model);
Chris@0 151
Chris@0 152 connect(m_model, SIGNAL(modelChanged()), this, SLOT(cacheInvalid()));
Chris@906 153 connect(m_model, SIGNAL(modelChangedWithin(sv_frame_t, sv_frame_t)),
Chris@906 154 this, SLOT(cacheInvalid(sv_frame_t, sv_frame_t)));
Chris@0 155
Chris@0 156 emit modelReplaced();
Chris@110 157 }
Chris@115 158
Chris@0 159 Layer::PropertyList
Chris@0 160 SpectrogramLayer::getProperties() const
Chris@0 161 {
Chris@0 162 PropertyList list;
Chris@87 163 list.push_back("Colour");
Chris@87 164 list.push_back("Colour Scale");
Chris@87 165 list.push_back("Window Size");
Chris@97 166 list.push_back("Window Increment");
Chris@862 167 list.push_back("Normalization");
Chris@87 168 list.push_back("Bin Display");
Chris@87 169 list.push_back("Threshold");
Chris@87 170 list.push_back("Gain");
Chris@87 171 list.push_back("Colour Rotation");
Chris@153 172 // list.push_back("Min Frequency");
Chris@153 173 // list.push_back("Max Frequency");
Chris@87 174 list.push_back("Frequency Scale");
Chris@0 175 return list;
Chris@0 176 }
Chris@0 177
Chris@87 178 QString
Chris@87 179 SpectrogramLayer::getPropertyLabel(const PropertyName &name) const
Chris@87 180 {
Chris@87 181 if (name == "Colour") return tr("Colour");
Chris@87 182 if (name == "Colour Scale") return tr("Colour Scale");
Chris@87 183 if (name == "Window Size") return tr("Window Size");
Chris@112 184 if (name == "Window Increment") return tr("Window Overlap");
Chris@862 185 if (name == "Normalization") return tr("Normalization");
Chris@87 186 if (name == "Bin Display") return tr("Bin Display");
Chris@87 187 if (name == "Threshold") return tr("Threshold");
Chris@87 188 if (name == "Gain") return tr("Gain");
Chris@87 189 if (name == "Colour Rotation") return tr("Colour Rotation");
Chris@87 190 if (name == "Min Frequency") return tr("Min Frequency");
Chris@87 191 if (name == "Max Frequency") return tr("Max Frequency");
Chris@87 192 if (name == "Frequency Scale") return tr("Frequency Scale");
Chris@87 193 return "";
Chris@87 194 }
Chris@87 195
Chris@335 196 QString
Chris@862 197 SpectrogramLayer::getPropertyIconName(const PropertyName &) const
Chris@335 198 {
Chris@335 199 return "";
Chris@335 200 }
Chris@335 201
Chris@0 202 Layer::PropertyType
Chris@0 203 SpectrogramLayer::getPropertyType(const PropertyName &name) const
Chris@0 204 {
Chris@87 205 if (name == "Gain") return RangeProperty;
Chris@87 206 if (name == "Colour Rotation") return RangeProperty;
Chris@87 207 if (name == "Threshold") return RangeProperty;
Chris@0 208 return ValueProperty;
Chris@0 209 }
Chris@0 210
Chris@0 211 QString
Chris@0 212 SpectrogramLayer::getPropertyGroupName(const PropertyName &name) const
Chris@0 213 {
Chris@153 214 if (name == "Bin Display" ||
Chris@153 215 name == "Frequency Scale") return tr("Bins");
Chris@87 216 if (name == "Window Size" ||
Chris@1086 217 name == "Window Increment") return tr("Window");
Chris@87 218 if (name == "Colour" ||
Chris@87 219 name == "Threshold" ||
Chris@87 220 name == "Colour Rotation") return tr("Colour");
Chris@862 221 if (name == "Normalization" ||
Chris@153 222 name == "Gain" ||
Chris@87 223 name == "Colour Scale") return tr("Scale");
Chris@0 224 return QString();
Chris@0 225 }
Chris@0 226
Chris@0 227 int
Chris@0 228 SpectrogramLayer::getPropertyRangeAndValue(const PropertyName &name,
Chris@216 229 int *min, int *max, int *deflt) const
Chris@0 230 {
Chris@216 231 int val = 0;
Chris@216 232
Chris@216 233 int garbage0, garbage1, garbage2;
Chris@55 234 if (!min) min = &garbage0;
Chris@55 235 if (!max) max = &garbage1;
Chris@216 236 if (!deflt) deflt = &garbage2;
Chris@10 237
Chris@87 238 if (name == "Gain") {
Chris@0 239
Chris@0 240 *min = -50;
Chris@0 241 *max = 50;
Chris@0 242
Chris@906 243 *deflt = int(lrint(log10(m_initialGain) * 20.0));
Chris@216 244 if (*deflt < *min) *deflt = *min;
Chris@216 245 if (*deflt > *max) *deflt = *max;
Chris@216 246
Chris@906 247 val = int(lrint(log10(m_gain) * 20.0));
Chris@216 248 if (val < *min) val = *min;
Chris@216 249 if (val > *max) val = *max;
Chris@0 250
Chris@87 251 } else if (name == "Threshold") {
Chris@37 252
Chris@37 253 *min = -50;
Chris@37 254 *max = 0;
Chris@37 255
Chris@906 256 *deflt = int(lrint(AudioLevel::multiplier_to_dB(m_initialThreshold)));
Chris@216 257 if (*deflt < *min) *deflt = *min;
Chris@216 258 if (*deflt > *max) *deflt = *max;
Chris@216 259
Chris@906 260 val = int(lrint(AudioLevel::multiplier_to_dB(m_threshold)));
Chris@216 261 if (val < *min) val = *min;
Chris@216 262 if (val > *max) val = *max;
Chris@37 263
Chris@87 264 } else if (name == "Colour Rotation") {
Chris@9 265
Chris@9 266 *min = 0;
Chris@9 267 *max = 256;
Chris@216 268 *deflt = m_initialRotation;
Chris@216 269
Chris@216 270 val = m_colourRotation;
Chris@9 271
Chris@87 272 } else if (name == "Colour Scale") {
Chris@0 273
Chris@0 274 *min = 0;
Chris@176 275 *max = 4;
Chris@216 276 *deflt = int(dBColourScale);
Chris@216 277
Chris@216 278 val = (int)m_colourScale;
Chris@0 279
Chris@87 280 } else if (name == "Colour") {
Chris@0 281
Chris@0 282 *min = 0;
Chris@196 283 *max = ColourMapper::getColourMapCount() - 1;
Chris@216 284 *deflt = 0;
Chris@216 285
Chris@216 286 val = m_colourMap;
Chris@0 287
Chris@87 288 } else if (name == "Window Size") {
Chris@0 289
Chris@0 290 *min = 0;
Chris@0 291 *max = 10;
Chris@216 292 *deflt = 5;
Chris@0 293
Chris@216 294 val = 0;
Chris@0 295 int ws = m_windowSize;
Chris@216 296 while (ws > 32) { ws >>= 1; val ++; }
Chris@0 297
Chris@97 298 } else if (name == "Window Increment") {
Chris@0 299
Chris@0 300 *min = 0;
Chris@97 301 *max = 5;
Chris@216 302 *deflt = 2;
Chris@216 303
Chris@216 304 val = m_windowHopLevel;
Chris@0 305
Chris@87 306 } else if (name == "Min Frequency") {
Chris@37 307
Chris@37 308 *min = 0;
Chris@37 309 *max = 9;
Chris@216 310 *deflt = 1;
Chris@37 311
Chris@37 312 switch (m_minFrequency) {
Chris@216 313 case 0: default: val = 0; break;
Chris@216 314 case 10: val = 1; break;
Chris@216 315 case 20: val = 2; break;
Chris@216 316 case 40: val = 3; break;
Chris@216 317 case 100: val = 4; break;
Chris@216 318 case 250: val = 5; break;
Chris@216 319 case 500: val = 6; break;
Chris@216 320 case 1000: val = 7; break;
Chris@216 321 case 4000: val = 8; break;
Chris@216 322 case 10000: val = 9; break;
Chris@37 323 }
Chris@37 324
Chris@87 325 } else if (name == "Max Frequency") {
Chris@0 326
Chris@0 327 *min = 0;
Chris@0 328 *max = 9;
Chris@216 329 *deflt = 6;
Chris@0 330
Chris@0 331 switch (m_maxFrequency) {
Chris@216 332 case 500: val = 0; break;
Chris@216 333 case 1000: val = 1; break;
Chris@216 334 case 1500: val = 2; break;
Chris@216 335 case 2000: val = 3; break;
Chris@216 336 case 4000: val = 4; break;
Chris@216 337 case 6000: val = 5; break;
Chris@216 338 case 8000: val = 6; break;
Chris@216 339 case 12000: val = 7; break;
Chris@216 340 case 16000: val = 8; break;
Chris@216 341 default: val = 9; break;
Chris@0 342 }
Chris@0 343
Chris@87 344 } else if (name == "Frequency Scale") {
Chris@0 345
Chris@0 346 *min = 0;
Chris@0 347 *max = 1;
Chris@216 348 *deflt = int(LinearFrequencyScale);
Chris@216 349 val = (int)m_frequencyScale;
Chris@0 350
Chris@87 351 } else if (name == "Bin Display") {
Chris@35 352
Chris@35 353 *min = 0;
Chris@35 354 *max = 2;
Chris@216 355 *deflt = int(AllBins);
Chris@216 356 val = (int)m_binDisplay;
Chris@35 357
Chris@862 358 } else if (name == "Normalization") {
Chris@36 359
Chris@862 360 *min = 0;
Chris@862 361 *max = 3;
Chris@1063 362 *deflt = int(ColumnOp::NoNormalization);
Chris@862 363 val = (int)m_normalization;
Chris@120 364
Chris@0 365 } else {
Chris@216 366 val = Layer::getPropertyRangeAndValue(name, min, max, deflt);
Chris@0 367 }
Chris@0 368
Chris@216 369 return val;
Chris@0 370 }
Chris@0 371
Chris@0 372 QString
Chris@0 373 SpectrogramLayer::getPropertyValueLabel(const PropertyName &name,
Chris@9 374 int value) const
Chris@0 375 {
Chris@87 376 if (name == "Colour") {
Chris@196 377 return ColourMapper::getColourMapName(value);
Chris@0 378 }
Chris@87 379 if (name == "Colour Scale") {
Chris@0 380 switch (value) {
Chris@0 381 default:
Chris@37 382 case 0: return tr("Linear");
Chris@37 383 case 1: return tr("Meter");
Chris@215 384 case 2: return tr("dBV^2");
Chris@215 385 case 3: return tr("dBV");
Chris@119 386 case 4: return tr("Phase");
Chris@0 387 }
Chris@0 388 }
Chris@862 389 if (name == "Normalization") {
Chris@862 390 return ""; // icon only
Chris@862 391 }
Chris@87 392 if (name == "Window Size") {
Chris@0 393 return QString("%1").arg(32 << value);
Chris@0 394 }
Chris@97 395 if (name == "Window Increment") {
Chris@0 396 switch (value) {
Chris@0 397 default:
Chris@112 398 case 0: return tr("None");
Chris@112 399 case 1: return tr("25 %");
Chris@112 400 case 2: return tr("50 %");
Chris@112 401 case 3: return tr("75 %");
Chris@112 402 case 4: return tr("87.5 %");
Chris@112 403 case 5: return tr("93.75 %");
Chris@0 404 }
Chris@0 405 }
Chris@87 406 if (name == "Min Frequency") {
Chris@37 407 switch (value) {
Chris@37 408 default:
Chris@38 409 case 0: return tr("No min");
Chris@37 410 case 1: return tr("10 Hz");
Chris@37 411 case 2: return tr("20 Hz");
Chris@37 412 case 3: return tr("40 Hz");
Chris@37 413 case 4: return tr("100 Hz");
Chris@37 414 case 5: return tr("250 Hz");
Chris@37 415 case 6: return tr("500 Hz");
Chris@37 416 case 7: return tr("1 KHz");
Chris@37 417 case 8: return tr("4 KHz");
Chris@37 418 case 9: return tr("10 KHz");
Chris@37 419 }
Chris@37 420 }
Chris@87 421 if (name == "Max Frequency") {
Chris@0 422 switch (value) {
Chris@0 423 default:
Chris@0 424 case 0: return tr("500 Hz");
Chris@0 425 case 1: return tr("1 KHz");
Chris@0 426 case 2: return tr("1.5 KHz");
Chris@0 427 case 3: return tr("2 KHz");
Chris@0 428 case 4: return tr("4 KHz");
Chris@0 429 case 5: return tr("6 KHz");
Chris@0 430 case 6: return tr("8 KHz");
Chris@0 431 case 7: return tr("12 KHz");
Chris@0 432 case 8: return tr("16 KHz");
Chris@38 433 case 9: return tr("No max");
Chris@0 434 }
Chris@0 435 }
Chris@87 436 if (name == "Frequency Scale") {
Chris@0 437 switch (value) {
Chris@0 438 default:
Chris@0 439 case 0: return tr("Linear");
Chris@0 440 case 1: return tr("Log");
Chris@0 441 }
Chris@0 442 }
Chris@87 443 if (name == "Bin Display") {
Chris@35 444 switch (value) {
Chris@35 445 default:
Chris@37 446 case 0: return tr("All Bins");
Chris@37 447 case 1: return tr("Peak Bins");
Chris@37 448 case 2: return tr("Frequencies");
Chris@35 449 }
Chris@35 450 }
Chris@0 451 return tr("<unknown>");
Chris@0 452 }
Chris@0 453
Chris@862 454 QString
Chris@862 455 SpectrogramLayer::getPropertyValueIconName(const PropertyName &name,
Chris@862 456 int value) const
Chris@862 457 {
Chris@862 458 if (name == "Normalization") {
Chris@862 459 switch(value) {
Chris@862 460 default:
Chris@862 461 case 0: return "normalise-none";
Chris@862 462 case 1: return "normalise-columns";
Chris@862 463 case 2: return "normalise";
Chris@862 464 case 3: return "normalise-hybrid";
Chris@862 465 }
Chris@862 466 }
Chris@862 467 return "";
Chris@862 468 }
Chris@862 469
Chris@167 470 RangeMapper *
Chris@167 471 SpectrogramLayer::getNewPropertyRangeMapper(const PropertyName &name) const
Chris@167 472 {
Chris@167 473 if (name == "Gain") {
Chris@167 474 return new LinearRangeMapper(-50, 50, -25, 25, tr("dB"));
Chris@167 475 }
Chris@167 476 if (name == "Threshold") {
Chris@167 477 return new LinearRangeMapper(-50, 0, -50, 0, tr("dB"));
Chris@167 478 }
Chris@167 479 return 0;
Chris@167 480 }
Chris@167 481
Chris@0 482 void
Chris@0 483 SpectrogramLayer::setProperty(const PropertyName &name, int value)
Chris@0 484 {
Chris@87 485 if (name == "Gain") {
Chris@906 486 setGain(float(pow(10, float(value)/20.0)));
Chris@87 487 } else if (name == "Threshold") {
Chris@37 488 if (value == -50) setThreshold(0.0);
Chris@906 489 else setThreshold(float(AudioLevel::dB_to_multiplier(value)));
Chris@87 490 } else if (name == "Colour Rotation") {
Chris@9 491 setColourRotation(value);
Chris@87 492 } else if (name == "Colour") {
Chris@197 493 setColourMap(value);
Chris@87 494 } else if (name == "Window Size") {
Chris@0 495 setWindowSize(32 << value);
Chris@97 496 } else if (name == "Window Increment") {
Chris@97 497 setWindowHopLevel(value);
Chris@87 498 } else if (name == "Min Frequency") {
Chris@37 499 switch (value) {
Chris@37 500 default:
Chris@37 501 case 0: setMinFrequency(0); break;
Chris@37 502 case 1: setMinFrequency(10); break;
Chris@37 503 case 2: setMinFrequency(20); break;
Chris@37 504 case 3: setMinFrequency(40); break;
Chris@37 505 case 4: setMinFrequency(100); break;
Chris@37 506 case 5: setMinFrequency(250); break;
Chris@37 507 case 6: setMinFrequency(500); break;
Chris@37 508 case 7: setMinFrequency(1000); break;
Chris@37 509 case 8: setMinFrequency(4000); break;
Chris@37 510 case 9: setMinFrequency(10000); break;
Chris@37 511 }
Chris@133 512 int vs = getCurrentVerticalZoomStep();
Chris@133 513 if (vs != m_lastEmittedZoomStep) {
Chris@133 514 emit verticalZoomChanged();
Chris@133 515 m_lastEmittedZoomStep = vs;
Chris@133 516 }
Chris@87 517 } else if (name == "Max Frequency") {
Chris@0 518 switch (value) {
Chris@0 519 case 0: setMaxFrequency(500); break;
Chris@0 520 case 1: setMaxFrequency(1000); break;
Chris@0 521 case 2: setMaxFrequency(1500); break;
Chris@0 522 case 3: setMaxFrequency(2000); break;
Chris@0 523 case 4: setMaxFrequency(4000); break;
Chris@0 524 case 5: setMaxFrequency(6000); break;
Chris@0 525 case 6: setMaxFrequency(8000); break;
Chris@0 526 case 7: setMaxFrequency(12000); break;
Chris@0 527 case 8: setMaxFrequency(16000); break;
Chris@0 528 default:
Chris@0 529 case 9: setMaxFrequency(0); break;
Chris@0 530 }
Chris@133 531 int vs = getCurrentVerticalZoomStep();
Chris@133 532 if (vs != m_lastEmittedZoomStep) {
Chris@133 533 emit verticalZoomChanged();
Chris@133 534 m_lastEmittedZoomStep = vs;
Chris@133 535 }
Chris@87 536 } else if (name == "Colour Scale") {
Chris@0 537 switch (value) {
Chris@0 538 default:
Chris@0 539 case 0: setColourScale(LinearColourScale); break;
Chris@0 540 case 1: setColourScale(MeterColourScale); break;
Chris@215 541 case 2: setColourScale(dBSquaredColourScale); break;
Chris@215 542 case 3: setColourScale(dBColourScale); break;
Chris@119 543 case 4: setColourScale(PhaseColourScale); break;
Chris@0 544 }
Chris@87 545 } else if (name == "Frequency Scale") {
Chris@0 546 switch (value) {
Chris@0 547 default:
Chris@0 548 case 0: setFrequencyScale(LinearFrequencyScale); break;
Chris@0 549 case 1: setFrequencyScale(LogFrequencyScale); break;
Chris@0 550 }
Chris@87 551 } else if (name == "Bin Display") {
Chris@35 552 switch (value) {
Chris@35 553 default:
Chris@37 554 case 0: setBinDisplay(AllBins); break;
Chris@37 555 case 1: setBinDisplay(PeakBins); break;
Chris@37 556 case 2: setBinDisplay(PeakFrequencies); break;
Chris@35 557 }
Chris@862 558 } else if (name == "Normalization") {
Chris@862 559 switch (value) {
Chris@862 560 default:
Chris@1063 561 case 0: setNormalization(ColumnOp::NoNormalization); break;
Chris@1063 562 case 1: setNormalization(ColumnOp::NormalizeColumns); break;
Chris@1063 563 case 2: setNormalization(ColumnOp::NormalizeVisibleArea); break;
Chris@1063 564 case 3: setNormalization(ColumnOp::NormalizeHybrid); break;
Chris@862 565 }
Chris@0 566 }
Chris@0 567 }
Chris@0 568
Chris@0 569 void
Chris@478 570 SpectrogramLayer::invalidateImageCaches()
Chris@95 571 {
Chris@1044 572 #ifdef DEBUG_SPECTROGRAM
Chris@1044 573 cerr << "SpectrogramLayer::invalidateImageCaches called" << endl;
Chris@1044 574 #endif
Chris@478 575 for (ViewImageCache::iterator i = m_imageCaches.begin();
Chris@478 576 i != m_imageCaches.end(); ++i) {
Chris@1030 577 i->second.invalidate();
Chris@95 578 }
Chris@1089 579
Chris@1089 580 //!!!
Chris@1089 581 for (ViewRendererMap::iterator i = m_renderers.begin();
Chris@1089 582 i != m_renderers.end(); ++i) {
Chris@1089 583 delete i->second;
Chris@1089 584 }
Chris@1089 585 m_renderers.clear();
Chris@95 586 }
Chris@95 587
Chris@95 588 void
Chris@122 589 SpectrogramLayer::preferenceChanged(PropertyContainer::PropertyName name)
Chris@122 590 {
Chris@587 591 SVDEBUG << "SpectrogramLayer::preferenceChanged(" << name << ")" << endl;
Chris@122 592
Chris@122 593 if (name == "Window Type") {
Chris@122 594 setWindowType(Preferences::getInstance()->getWindowType());
Chris@122 595 return;
Chris@122 596 }
Chris@490 597 if (name == "Spectrogram Y Smoothing") {
Chris@1086 598 setWindowSize(m_windowSize);
Chris@490 599 invalidateImageCaches();
Chris@490 600 invalidateMagnitudes();
Chris@490 601 emit layerParametersChanged();
Chris@490 602 }
Chris@490 603 if (name == "Spectrogram X Smoothing") {
Chris@478 604 invalidateImageCaches();
Chris@122 605 invalidateMagnitudes();
Chris@122 606 emit layerParametersChanged();
Chris@122 607 }
Chris@122 608 if (name == "Tuning Frequency") {
Chris@122 609 emit layerParametersChanged();
Chris@122 610 }
Chris@122 611 }
Chris@122 612
Chris@122 613 void
Chris@0 614 SpectrogramLayer::setChannel(int ch)
Chris@0 615 {
Chris@0 616 if (m_channel == ch) return;
Chris@0 617
Chris@478 618 invalidateImageCaches();
Chris@0 619 m_channel = ch;
Chris@1088 620 invalidateFFTModel();
Chris@9 621
Chris@0 622 emit layerParametersChanged();
Chris@0 623 }
Chris@0 624
Chris@0 625 int
Chris@0 626 SpectrogramLayer::getChannel() const
Chris@0 627 {
Chris@0 628 return m_channel;
Chris@0 629 }
Chris@0 630
Chris@1086 631 int
Chris@1086 632 SpectrogramLayer::getFFTOversampling() const
Chris@1086 633 {
Chris@1086 634 if (m_binDisplay != AllBins) {
Chris@1086 635 return 1;
Chris@1086 636 }
Chris@1086 637
Chris@1086 638 Preferences::SpectrogramSmoothing smoothing =
Chris@1086 639 Preferences::getInstance()->getSpectrogramSmoothing();
Chris@1086 640
Chris@1086 641 if (smoothing == Preferences::NoSpectrogramSmoothing ||
Chris@1086 642 smoothing == Preferences::SpectrogramInterpolated) {
Chris@1086 643 return 1;
Chris@1086 644 }
Chris@1086 645
Chris@1086 646 return 4;
Chris@1086 647 }
Chris@1086 648
Chris@1087 649 int
Chris@1087 650 SpectrogramLayer::getFFTSize() const
Chris@1087 651 {
Chris@1087 652 return m_windowSize * getFFTOversampling();
Chris@1087 653 }
Chris@1087 654
Chris@0 655 void
Chris@805 656 SpectrogramLayer::setWindowSize(int ws)
Chris@0 657 {
Chris@1087 658 if (m_windowSize == ws) return;
Chris@0 659
Chris@478 660 invalidateImageCaches();
Chris@0 661
Chris@0 662 m_windowSize = ws;
Chris@0 663
Chris@1088 664 invalidateFFTModel();
Chris@9 665
Chris@9 666 emit layerParametersChanged();
Chris@0 667 }
Chris@0 668
Chris@805 669 int
Chris@0 670 SpectrogramLayer::getWindowSize() const
Chris@0 671 {
Chris@0 672 return m_windowSize;
Chris@0 673 }
Chris@0 674
Chris@0 675 void
Chris@805 676 SpectrogramLayer::setWindowHopLevel(int v)
Chris@0 677 {
Chris@97 678 if (m_windowHopLevel == v) return;
Chris@0 679
Chris@478 680 invalidateImageCaches();
Chris@0 681
Chris@97 682 m_windowHopLevel = v;
Chris@0 683
Chris@1088 684 invalidateFFTModel();
Chris@9 685
Chris@9 686 emit layerParametersChanged();
Chris@9 687
Chris@110 688 // fillCache();
Chris@0 689 }
Chris@0 690
Chris@805 691 int
Chris@97 692 SpectrogramLayer::getWindowHopLevel() const
Chris@0 693 {
Chris@97 694 return m_windowHopLevel;
Chris@0 695 }
Chris@0 696
Chris@0 697 void
Chris@0 698 SpectrogramLayer::setWindowType(WindowType w)
Chris@0 699 {
Chris@0 700 if (m_windowType == w) return;
Chris@0 701
Chris@478 702 invalidateImageCaches();
Chris@0 703
Chris@0 704 m_windowType = w;
Chris@110 705
Chris@1088 706 invalidateFFTModel();
Chris@9 707
Chris@9 708 emit layerParametersChanged();
Chris@0 709 }
Chris@0 710
Chris@0 711 WindowType
Chris@0 712 SpectrogramLayer::getWindowType() const
Chris@0 713 {
Chris@0 714 return m_windowType;
Chris@0 715 }
Chris@0 716
Chris@0 717 void
Chris@0 718 SpectrogramLayer::setGain(float gain)
Chris@0 719 {
Chris@587 720 // SVDEBUG << "SpectrogramLayer::setGain(" << gain << ") (my gain is now "
Chris@585 721 // << m_gain << ")" << endl;
Chris@55 722
Chris@40 723 if (m_gain == gain) return;
Chris@0 724
Chris@478 725 invalidateImageCaches();
Chris@0 726
Chris@0 727 m_gain = gain;
Chris@0 728
Chris@9 729 emit layerParametersChanged();
Chris@0 730 }
Chris@0 731
Chris@0 732 float
Chris@0 733 SpectrogramLayer::getGain() const
Chris@0 734 {
Chris@0 735 return m_gain;
Chris@0 736 }
Chris@0 737
Chris@0 738 void
Chris@37 739 SpectrogramLayer::setThreshold(float threshold)
Chris@37 740 {
Chris@40 741 if (m_threshold == threshold) return;
Chris@37 742
Chris@478 743 invalidateImageCaches();
Chris@37 744
Chris@37 745 m_threshold = threshold;
Chris@37 746
Chris@37 747 emit layerParametersChanged();
Chris@37 748 }
Chris@37 749
Chris@37 750 float
Chris@37 751 SpectrogramLayer::getThreshold() const
Chris@37 752 {
Chris@37 753 return m_threshold;
Chris@37 754 }
Chris@37 755
Chris@37 756 void
Chris@805 757 SpectrogramLayer::setMinFrequency(int mf)
Chris@37 758 {
Chris@37 759 if (m_minFrequency == mf) return;
Chris@37 760
Chris@587 761 // SVDEBUG << "SpectrogramLayer::setMinFrequency: " << mf << endl;
Chris@187 762
Chris@478 763 invalidateImageCaches();
Chris@119 764 invalidateMagnitudes();
Chris@37 765
Chris@37 766 m_minFrequency = mf;
Chris@37 767
Chris@37 768 emit layerParametersChanged();
Chris@37 769 }
Chris@37 770
Chris@805 771 int
Chris@37 772 SpectrogramLayer::getMinFrequency() const
Chris@37 773 {
Chris@37 774 return m_minFrequency;
Chris@37 775 }
Chris@37 776
Chris@37 777 void
Chris@805 778 SpectrogramLayer::setMaxFrequency(int mf)
Chris@0 779 {
Chris@0 780 if (m_maxFrequency == mf) return;
Chris@0 781
Chris@587 782 // SVDEBUG << "SpectrogramLayer::setMaxFrequency: " << mf << endl;
Chris@187 783
Chris@478 784 invalidateImageCaches();
Chris@119 785 invalidateMagnitudes();
Chris@0 786
Chris@0 787 m_maxFrequency = mf;
Chris@0 788
Chris@9 789 emit layerParametersChanged();
Chris@0 790 }
Chris@0 791
Chris@805 792 int
Chris@0 793 SpectrogramLayer::getMaxFrequency() const
Chris@0 794 {
Chris@0 795 return m_maxFrequency;
Chris@0 796 }
Chris@0 797
Chris@0 798 void
Chris@9 799 SpectrogramLayer::setColourRotation(int r)
Chris@9 800 {
Chris@478 801 invalidateImageCaches();
Chris@9 802
Chris@9 803 if (r < 0) r = 0;
Chris@9 804 if (r > 256) r = 256;
Chris@9 805 int distance = r - m_colourRotation;
Chris@9 806
Chris@9 807 if (distance != 0) {
Chris@197 808 rotatePalette(-distance);
Chris@9 809 m_colourRotation = r;
Chris@9 810 }
Chris@9 811
Chris@9 812 emit layerParametersChanged();
Chris@9 813 }
Chris@9 814
Chris@9 815 void
Chris@0 816 SpectrogramLayer::setColourScale(ColourScale colourScale)
Chris@0 817 {
Chris@0 818 if (m_colourScale == colourScale) return;
Chris@0 819
Chris@478 820 invalidateImageCaches();
Chris@0 821
Chris@0 822 m_colourScale = colourScale;
Chris@0 823
Chris@9 824 emit layerParametersChanged();
Chris@0 825 }
Chris@0 826
Chris@0 827 SpectrogramLayer::ColourScale
Chris@0 828 SpectrogramLayer::getColourScale() const
Chris@0 829 {
Chris@0 830 return m_colourScale;
Chris@0 831 }
Chris@0 832
Chris@0 833 void
Chris@197 834 SpectrogramLayer::setColourMap(int map)
Chris@0 835 {
Chris@197 836 if (m_colourMap == map) return;
Chris@0 837
Chris@478 838 invalidateImageCaches();
Chris@0 839
Chris@197 840 m_colourMap = map;
Chris@197 841 initialisePalette();
Chris@9 842
Chris@0 843 emit layerParametersChanged();
Chris@0 844 }
Chris@0 845
Chris@196 846 int
Chris@197 847 SpectrogramLayer::getColourMap() const
Chris@0 848 {
Chris@197 849 return m_colourMap;
Chris@0 850 }
Chris@0 851
Chris@0 852 void
Chris@0 853 SpectrogramLayer::setFrequencyScale(FrequencyScale frequencyScale)
Chris@0 854 {
Chris@0 855 if (m_frequencyScale == frequencyScale) return;
Chris@0 856
Chris@478 857 invalidateImageCaches();
Chris@0 858 m_frequencyScale = frequencyScale;
Chris@9 859
Chris@9 860 emit layerParametersChanged();
Chris@0 861 }
Chris@0 862
Chris@0 863 SpectrogramLayer::FrequencyScale
Chris@0 864 SpectrogramLayer::getFrequencyScale() const
Chris@0 865 {
Chris@0 866 return m_frequencyScale;
Chris@0 867 }
Chris@0 868
Chris@0 869 void
Chris@37 870 SpectrogramLayer::setBinDisplay(BinDisplay binDisplay)
Chris@35 871 {
Chris@37 872 if (m_binDisplay == binDisplay) return;
Chris@35 873
Chris@478 874 invalidateImageCaches();
Chris@37 875 m_binDisplay = binDisplay;
Chris@35 876
Chris@35 877 emit layerParametersChanged();
Chris@35 878 }
Chris@35 879
Chris@37 880 SpectrogramLayer::BinDisplay
Chris@37 881 SpectrogramLayer::getBinDisplay() const
Chris@35 882 {
Chris@37 883 return m_binDisplay;
Chris@35 884 }
Chris@35 885
Chris@35 886 void
Chris@1063 887 SpectrogramLayer::setNormalization(ColumnOp::Normalization n)
Chris@36 888 {
Chris@862 889 if (m_normalization == n) return;
Chris@36 890
Chris@478 891 invalidateImageCaches();
Chris@119 892 invalidateMagnitudes();
Chris@862 893 m_normalization = n;
Chris@36 894
Chris@36 895 emit layerParametersChanged();
Chris@36 896 }
Chris@36 897
Chris@1063 898 ColumnOp::Normalization
Chris@862 899 SpectrogramLayer::getNormalization() const
Chris@36 900 {
Chris@862 901 return m_normalization;
Chris@36 902 }
Chris@36 903
Chris@36 904 void
Chris@918 905 SpectrogramLayer::setLayerDormant(const LayerGeometryProvider *v, bool dormant)
Chris@29 906 {
Chris@33 907 if (dormant) {
Chris@33 908
Chris@331 909 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@985 910 cerr << "SpectrogramLayer::setLayerDormant(" << dormant << ")"
Chris@585 911 << endl;
Chris@331 912 #endif
Chris@331 913
Chris@131 914 if (isLayerDormant(v)) {
Chris@131 915 return;
Chris@131 916 }
Chris@131 917
Chris@131 918 Layer::setLayerDormant(v, true);
Chris@33 919
Chris@920 920 const View *view = v->getView();
Chris@920 921
Chris@478 922 invalidateImageCaches();
Chris@988 923
Chris@1030 924 m_imageCaches.erase(view->getId());
Chris@1030 925
Chris@1088 926 //!!! in theory we should call invalidateFFTModel() if and
Chris@1088 927 //!!! only if there are no remaining views in which we are not
Chris@1088 928 //!!! dormant
Chris@33 929
Chris@33 930 } else {
Chris@33 931
Chris@131 932 Layer::setLayerDormant(v, false);
Chris@33 933 }
Chris@29 934 }
Chris@29 935
Chris@29 936 void
Chris@0 937 SpectrogramLayer::cacheInvalid()
Chris@0 938 {
Chris@391 939 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@985 940 cerr << "SpectrogramLayer::cacheInvalid()" << endl;
Chris@391 941 #endif
Chris@391 942
Chris@478 943 invalidateImageCaches();
Chris@119 944 invalidateMagnitudes();
Chris@0 945 }
Chris@0 946
Chris@0 947 void
Chris@1037 948 SpectrogramLayer::cacheInvalid(
Chris@1037 949 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@1037 950 sv_frame_t from, sv_frame_t to
Chris@1037 951 #else
Chris@1037 952 sv_frame_t , sv_frame_t
Chris@1037 953 #endif
Chris@1037 954 )
Chris@0 955 {
Chris@391 956 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@985 957 cerr << "SpectrogramLayer::cacheInvalid(" << from << ", " << to << ")" << endl;
Chris@391 958 #endif
Chris@391 959
Chris@1030 960 // We used to call invalidateMagnitudes(from, to) to invalidate
Chris@1030 961 // only those caches whose views contained some of the (from, to)
Chris@1030 962 // range. That's the right thing to do; it has been lost in
Chris@1030 963 // pulling out the image cache code, but it might not matter very
Chris@1030 964 // much, since the underlying models for spectrogram layers don't
Chris@1030 965 // change very often. Let's see.
Chris@1030 966 invalidateImageCaches();
Chris@391 967 invalidateMagnitudes();
Chris@0 968 }
Chris@0 969
Chris@224 970 bool
Chris@224 971 SpectrogramLayer::hasLightBackground() const
Chris@224 972 {
Chris@287 973 return ColourMapper(m_colourMap, 1.f, 255.f).hasLightBackground();
Chris@224 974 }
Chris@224 975
Chris@0 976 void
Chris@197 977 SpectrogramLayer::initialisePalette()
Chris@0 978 {
Chris@10 979 int formerRotation = m_colourRotation;
Chris@10 980
Chris@197 981 if (m_colourMap == (int)ColourMapper::BlackOnWhite) {
Chris@197 982 m_palette.setColour(NO_VALUE, Qt::white);
Chris@38 983 } else {
Chris@197 984 m_palette.setColour(NO_VALUE, Qt::black);
Chris@38 985 }
Chris@0 986
Chris@197 987 ColourMapper mapper(m_colourMap, 1.f, 255.f);
Chris@196 988
Chris@0 989 for (int pixel = 1; pixel < 256; ++pixel) {
Chris@907 990 m_palette.setColour((unsigned char)pixel, mapper.map(pixel));
Chris@0 991 }
Chris@9 992
Chris@196 993 m_crosshairColour = mapper.getContrastingColour();
Chris@196 994
Chris@9 995 m_colourRotation = 0;
Chris@197 996 rotatePalette(m_colourRotation - formerRotation);
Chris@10 997 m_colourRotation = formerRotation;
Chris@478 998
Chris@478 999 m_drawBuffer = QImage();
Chris@9 1000 }
Chris@9 1001
Chris@9 1002 void
Chris@197 1003 SpectrogramLayer::rotatePalette(int distance)
Chris@9 1004 {
Chris@31 1005 QColor newPixels[256];
Chris@9 1006
Chris@197 1007 newPixels[NO_VALUE] = m_palette.getColour(NO_VALUE);
Chris@9 1008
Chris@9 1009 for (int pixel = 1; pixel < 256; ++pixel) {
Chris@9 1010 int target = pixel + distance;
Chris@9 1011 while (target < 1) target += 255;
Chris@9 1012 while (target > 255) target -= 255;
Chris@907 1013 newPixels[target] = m_palette.getColour((unsigned char)pixel);
Chris@9 1014 }
Chris@9 1015
Chris@9 1016 for (int pixel = 0; pixel < 256; ++pixel) {
Chris@907 1017 m_palette.setColour((unsigned char)pixel, newPixels[pixel]);
Chris@9 1018 }
Chris@478 1019
Chris@478 1020 m_drawBuffer = QImage();
Chris@0 1021 }
Chris@0 1022
Chris@38 1023 unsigned char
Chris@918 1024 SpectrogramLayer::getDisplayValue(LayerGeometryProvider *v, double input) const
Chris@38 1025 {
Chris@38 1026 int value;
Chris@37 1027
Chris@907 1028 double min = 0.0;
Chris@907 1029 double max = 1.0;
Chris@120 1030
Chris@1063 1031 if (m_normalization == ColumnOp::NormalizeVisibleArea) {
Chris@1030 1032 min = m_viewMags[v->getId()].getMin();
Chris@1030 1033 max = m_viewMags[v->getId()].getMax();
Chris@1063 1034 } else if (m_normalization != ColumnOp::NormalizeColumns) {
Chris@224 1035 if (m_colourScale == LinearColourScale //||
Chris@224 1036 // m_colourScale == MeterColourScale) {
Chris@224 1037 ) {
Chris@907 1038 max = 0.1;
Chris@120 1039 }
Chris@120 1040 }
Chris@120 1041
Chris@907 1042 double thresh = -80.0;
Chris@907 1043
Chris@907 1044 if (max == 0.0) max = 1.0;
Chris@907 1045 if (max == min) min = max - 0.0001;
Chris@119 1046
Chris@40 1047 switch (m_colourScale) {
Chris@40 1048
Chris@40 1049 default:
Chris@40 1050 case LinearColourScale:
Chris@907 1051 value = int(((input - min) / (max - min)) * 255.0) + 1;
Chris@40 1052 break;
Chris@40 1053
Chris@40 1054 case MeterColourScale:
Chris@210 1055 value = AudioLevel::multiplier_to_preview
Chris@210 1056 ((input - min) / (max - min), 254) + 1;
Chris@40 1057 break;
Chris@119 1058
Chris@210 1059 case dBSquaredColourScale:
Chris@215 1060 input = ((input - min) * (input - min)) / ((max - min) * (max - min));
Chris@907 1061 if (input > 0.0) {
Chris@907 1062 input = 10.0 * log10(input);
Chris@133 1063 } else {
Chris@133 1064 input = thresh;
Chris@133 1065 }
Chris@907 1066 if (min > 0.0) {
Chris@907 1067 thresh = 10.0 * log10(min * min);
Chris@907 1068 if (thresh < -80.0) thresh = -80.0;
Chris@119 1069 }
Chris@119 1070 input = (input - thresh) / (-thresh);
Chris@907 1071 if (input < 0.0) input = 0.0;
Chris@907 1072 if (input > 1.0) input = 1.0;
Chris@907 1073 value = int(input * 255.0) + 1;
Chris@119 1074 break;
Chris@40 1075
Chris@215 1076 case dBColourScale:
Chris@215 1077 //!!! experiment with normalizing the visible area this way.
Chris@215 1078 //In any case, we need to have some indication of what the dB
Chris@215 1079 //scale is relative to.
Chris@215 1080 input = (input - min) / (max - min);
Chris@907 1081 if (input > 0.0) {
Chris@907 1082 input = 10.0 * log10(input);
Chris@215 1083 } else {
Chris@215 1084 input = thresh;
Chris@215 1085 }
Chris@907 1086 if (min > 0.0) {
Chris@907 1087 thresh = 10.0 * log10(min);
Chris@907 1088 if (thresh < -80.0) thresh = -80.0;
Chris@215 1089 }
Chris@215 1090 input = (input - thresh) / (-thresh);
Chris@907 1091 if (input < 0.0) input = 0.0;
Chris@907 1092 if (input > 1.0) input = 1.0;
Chris@907 1093 value = int(input * 255.0) + 1;
Chris@215 1094 break;
Chris@215 1095
Chris@40 1096 case PhaseColourScale:
Chris@40 1097 value = int((input * 127.0 / M_PI) + 128);
Chris@40 1098 break;
Chris@0 1099 }
Chris@210 1100
Chris@38 1101 if (value > UCHAR_MAX) value = UCHAR_MAX;
Chris@38 1102 if (value < 0) value = 0;
Chris@907 1103 return (unsigned char)value;
Chris@0 1104 }
Chris@0 1105
Chris@905 1106 double
Chris@40 1107 SpectrogramLayer::getEffectiveMinFrequency() const
Chris@40 1108 {
Chris@907 1109 sv_samplerate_t sr = m_model->getSampleRate();
Chris@1087 1110 double minf = double(sr) / getFFTSize();
Chris@40 1111
Chris@40 1112 if (m_minFrequency > 0.0) {
Chris@1087 1113 int minbin = int((double(m_minFrequency) * getFFTSize()) / sr + 0.01);
Chris@40 1114 if (minbin < 1) minbin = 1;
Chris@1087 1115 minf = minbin * sr / getFFTSize();
Chris@40 1116 }
Chris@40 1117
Chris@40 1118 return minf;
Chris@40 1119 }
Chris@40 1120
Chris@905 1121 double
Chris@40 1122 SpectrogramLayer::getEffectiveMaxFrequency() const
Chris@40 1123 {
Chris@907 1124 sv_samplerate_t sr = m_model->getSampleRate();
Chris@905 1125 double maxf = double(sr) / 2;
Chris@40 1126
Chris@40 1127 if (m_maxFrequency > 0.0) {
Chris@1087 1128 int maxbin = int((double(m_maxFrequency) * getFFTSize()) / sr + 0.1);
Chris@1087 1129 if (maxbin > getFFTSize() / 2) maxbin = getFFTSize() / 2;
Chris@1087 1130 maxf = maxbin * sr / getFFTSize();
Chris@40 1131 }
Chris@40 1132
Chris@40 1133 return maxf;
Chris@40 1134 }
Chris@40 1135
Chris@0 1136 bool
Chris@918 1137 SpectrogramLayer::getYBinRange(LayerGeometryProvider *v, int y, double &q0, double &q1) const
Chris@0 1138 {
Chris@382 1139 Profiler profiler("SpectrogramLayer::getYBinRange");
Chris@382 1140
Chris@918 1141 int h = v->getPaintHeight();
Chris@0 1142 if (y < 0 || y >= h) return false;
Chris@0 1143
Chris@907 1144 sv_samplerate_t sr = m_model->getSampleRate();
Chris@905 1145 double minf = getEffectiveMinFrequency();
Chris@905 1146 double maxf = getEffectiveMaxFrequency();
Chris@0 1147
Chris@38 1148 bool logarithmic = (m_frequencyScale == LogFrequencyScale);
Chris@38 1149
Chris@44 1150 q0 = v->getFrequencyForY(y, minf, maxf, logarithmic);
Chris@44 1151 q1 = v->getFrequencyForY(y - 1, minf, maxf, logarithmic);
Chris@38 1152
Chris@1086 1153 // Now map these on to ("proportions of") actual bins
Chris@490 1154
Chris@1087 1155 q0 = (q0 * getFFTSize()) / sr;
Chris@1087 1156 q1 = (q1 * getFFTSize()) / sr;
Chris@0 1157
Chris@0 1158 return true;
Chris@0 1159 }
Chris@486 1160
Chris@1085 1161 double
Chris@1090 1162 SpectrogramLayer::getYForBin(LayerGeometryProvider *, double) const {
Chris@1085 1163 //!!! not implemented
Chris@1085 1164 throw std::logic_error("not implemented");
Chris@1085 1165 }
Chris@1085 1166
Chris@1085 1167 double
Chris@1085 1168 SpectrogramLayer::getBinForY(LayerGeometryProvider *v, double y) const
Chris@1085 1169 {
Chris@1085 1170 //!!! overlap with range methods above (but using double arg)
Chris@1085 1171 //!!! tidy this
Chris@38 1172
Chris@1085 1173 int h = v->getPaintHeight();
Chris@1085 1174 if (y < 0 || y >= h) return false;
Chris@1085 1175
Chris@1085 1176 sv_samplerate_t sr = m_model->getSampleRate();
Chris@1085 1177 double minf = getEffectiveMinFrequency();
Chris@1085 1178 double maxf = getEffectiveMaxFrequency();
Chris@1085 1179
Chris@1085 1180 bool logarithmic = (m_frequencyScale == LogFrequencyScale);
Chris@1085 1181
Chris@1085 1182 double q = v->getFrequencyForY(y, minf, maxf, logarithmic);
Chris@1085 1183
Chris@1086 1184 // Now map on to ("proportions of") actual bins
Chris@1085 1185
Chris@1087 1186 q = (q * getFFTSize()) / sr;
Chris@1085 1187
Chris@1085 1188 return q;
Chris@1085 1189 }
Chris@1085 1190
Chris@0 1191 bool
Chris@918 1192 SpectrogramLayer::getXBinRange(LayerGeometryProvider *v, int x, double &s0, double &s1) const
Chris@0 1193 {
Chris@907 1194 sv_frame_t modelStart = m_model->getStartFrame();
Chris@907 1195 sv_frame_t modelEnd = m_model->getEndFrame();
Chris@0 1196
Chris@0 1197 // Each pixel column covers an exact range of sample frames:
Chris@907 1198 sv_frame_t f0 = v->getFrameForX(x) - modelStart;
Chris@907 1199 sv_frame_t f1 = v->getFrameForX(x + 1) - modelStart - 1;
Chris@20 1200
Chris@41 1201 if (f1 < int(modelStart) || f0 > int(modelEnd)) {
Chris@41 1202 return false;
Chris@41 1203 }
Chris@20 1204
Chris@0 1205 // And that range may be drawn from a possibly non-integral
Chris@0 1206 // range of spectrogram windows:
Chris@0 1207
Chris@805 1208 int windowIncrement = getWindowIncrement();
Chris@905 1209 s0 = double(f0) / windowIncrement;
Chris@905 1210 s1 = double(f1) / windowIncrement;
Chris@0 1211
Chris@0 1212 return true;
Chris@0 1213 }
Chris@0 1214
Chris@0 1215 bool
Chris@918 1216 SpectrogramLayer::getXBinSourceRange(LayerGeometryProvider *v, int x, RealTime &min, RealTime &max) const
Chris@0 1217 {
Chris@905 1218 double s0 = 0, s1 = 0;
Chris@44 1219 if (!getXBinRange(v, x, s0, s1)) return false;
Chris@0 1220
Chris@0 1221 int s0i = int(s0 + 0.001);
Chris@0 1222 int s1i = int(s1);
Chris@0 1223
Chris@0 1224 int windowIncrement = getWindowIncrement();
Chris@0 1225 int w0 = s0i * windowIncrement - (m_windowSize - windowIncrement)/2;
Chris@0 1226 int w1 = s1i * windowIncrement + windowIncrement +
Chris@0 1227 (m_windowSize - windowIncrement)/2 - 1;
Chris@0 1228
Chris@0 1229 min = RealTime::frame2RealTime(w0, m_model->getSampleRate());
Chris@0 1230 max = RealTime::frame2RealTime(w1, m_model->getSampleRate());
Chris@0 1231 return true;
Chris@0 1232 }
Chris@0 1233
Chris@0 1234 bool
Chris@918 1235 SpectrogramLayer::getYBinSourceRange(LayerGeometryProvider *v, int y, double &freqMin, double &freqMax)
Chris@0 1236 const
Chris@0 1237 {
Chris@905 1238 double q0 = 0, q1 = 0;
Chris@44 1239 if (!getYBinRange(v, y, q0, q1)) return false;
Chris@0 1240
Chris@0 1241 int q0i = int(q0 + 0.001);
Chris@0 1242 int q1i = int(q1);
Chris@0 1243
Chris@907 1244 sv_samplerate_t sr = m_model->getSampleRate();
Chris@0 1245
Chris@0 1246 for (int q = q0i; q <= q1i; ++q) {
Chris@1087 1247 if (q == q0i) freqMin = (sr * q) / getFFTSize();
Chris@1087 1248 if (q == q1i) freqMax = (sr * (q+1)) / getFFTSize();
Chris@0 1249 }
Chris@0 1250 return true;
Chris@0 1251 }
Chris@35 1252
Chris@35 1253 bool
Chris@918 1254 SpectrogramLayer::getAdjustedYBinSourceRange(LayerGeometryProvider *v, int x, int y,
Chris@905 1255 double &freqMin, double &freqMax,
Chris@905 1256 double &adjFreqMin, double &adjFreqMax)
Chris@35 1257 const
Chris@35 1258 {
Chris@277 1259 if (!m_model || !m_model->isOK() || !m_model->isReady()) {
Chris@277 1260 return false;
Chris@277 1261 }
Chris@277 1262
Chris@1088 1263 FFTModel *fft = getFFTModel();
Chris@114 1264 if (!fft) return false;
Chris@110 1265
Chris@905 1266 double s0 = 0, s1 = 0;
Chris@44 1267 if (!getXBinRange(v, x, s0, s1)) return false;
Chris@35 1268
Chris@905 1269 double q0 = 0, q1 = 0;
Chris@44 1270 if (!getYBinRange(v, y, q0, q1)) return false;
Chris@35 1271
Chris@35 1272 int s0i = int(s0 + 0.001);
Chris@35 1273 int s1i = int(s1);
Chris@35 1274
Chris@35 1275 int q0i = int(q0 + 0.001);
Chris@35 1276 int q1i = int(q1);
Chris@35 1277
Chris@907 1278 sv_samplerate_t sr = m_model->getSampleRate();
Chris@35 1279
Chris@35 1280 bool haveAdj = false;
Chris@35 1281
Chris@37 1282 bool peaksOnly = (m_binDisplay == PeakBins ||
Chris@37 1283 m_binDisplay == PeakFrequencies);
Chris@37 1284
Chris@35 1285 for (int q = q0i; q <= q1i; ++q) {
Chris@35 1286
Chris@35 1287 for (int s = s0i; s <= s1i; ++s) {
Chris@35 1288
Chris@905 1289 double binfreq = (double(sr) * q) / m_windowSize;
Chris@35 1290 if (q == q0i) freqMin = binfreq;
Chris@35 1291 if (q == q1i) freqMax = binfreq;
Chris@37 1292
Chris@114 1293 if (peaksOnly && !fft->isLocalPeak(s, q)) continue;
Chris@38 1294
Chris@1086 1295 if (!fft->isOverThreshold
Chris@1087 1296 (s, q, float(m_threshold * double(getFFTSize())/2.0))) {
Chris@1086 1297 continue;
Chris@1086 1298 }
Chris@907 1299
Chris@907 1300 double freq = binfreq;
Chris@40 1301
Chris@114 1302 if (s < int(fft->getWidth()) - 1) {
Chris@38 1303
Chris@277 1304 fft->estimateStableFrequency(s, q, freq);
Chris@35 1305
Chris@38 1306 if (!haveAdj || freq < adjFreqMin) adjFreqMin = freq;
Chris@38 1307 if (!haveAdj || freq > adjFreqMax) adjFreqMax = freq;
Chris@35 1308
Chris@35 1309 haveAdj = true;
Chris@35 1310 }
Chris@35 1311 }
Chris@35 1312 }
Chris@35 1313
Chris@35 1314 if (!haveAdj) {
Chris@40 1315 adjFreqMin = adjFreqMax = 0.0;
Chris@35 1316 }
Chris@35 1317
Chris@35 1318 return haveAdj;
Chris@35 1319 }
Chris@0 1320
Chris@0 1321 bool
Chris@918 1322 SpectrogramLayer::getXYBinSourceRange(LayerGeometryProvider *v, int x, int y,
Chris@905 1323 double &min, double &max,
Chris@905 1324 double &phaseMin, double &phaseMax) const
Chris@0 1325 {
Chris@277 1326 if (!m_model || !m_model->isOK() || !m_model->isReady()) {
Chris@277 1327 return false;
Chris@277 1328 }
Chris@277 1329
Chris@905 1330 double q0 = 0, q1 = 0;
Chris@44 1331 if (!getYBinRange(v, y, q0, q1)) return false;
Chris@0 1332
Chris@905 1333 double s0 = 0, s1 = 0;
Chris@44 1334 if (!getXBinRange(v, x, s0, s1)) return false;
Chris@0 1335
Chris@0 1336 int q0i = int(q0 + 0.001);
Chris@0 1337 int q1i = int(q1);
Chris@0 1338
Chris@0 1339 int s0i = int(s0 + 0.001);
Chris@0 1340 int s1i = int(s1);
Chris@0 1341
Chris@37 1342 bool rv = false;
Chris@37 1343
Chris@1088 1344 FFTModel *fft = getFFTModel();
Chris@0 1345
Chris@114 1346 if (fft) {
Chris@114 1347
Chris@114 1348 int cw = fft->getWidth();
Chris@114 1349 int ch = fft->getHeight();
Chris@0 1350
Chris@110 1351 min = 0.0;
Chris@110 1352 max = 0.0;
Chris@110 1353 phaseMin = 0.0;
Chris@110 1354 phaseMax = 0.0;
Chris@110 1355 bool have = false;
Chris@0 1356
Chris@110 1357 for (int q = q0i; q <= q1i; ++q) {
Chris@110 1358 for (int s = s0i; s <= s1i; ++s) {
Chris@110 1359 if (s >= 0 && q >= 0 && s < cw && q < ch) {
Chris@117 1360
Chris@905 1361 double value;
Chris@38 1362
Chris@114 1363 value = fft->getPhaseAt(s, q);
Chris@110 1364 if (!have || value < phaseMin) { phaseMin = value; }
Chris@110 1365 if (!have || value > phaseMax) { phaseMax = value; }
Chris@91 1366
Chris@1087 1367 value = fft->getMagnitudeAt(s, q) / (getFFTSize()/2.0);
Chris@110 1368 if (!have || value < min) { min = value; }
Chris@110 1369 if (!have || value > max) { max = value; }
Chris@110 1370
Chris@110 1371 have = true;
Chris@110 1372 }
Chris@110 1373 }
Chris@110 1374 }
Chris@110 1375
Chris@110 1376 if (have) {
Chris@110 1377 rv = true;
Chris@110 1378 }
Chris@0 1379 }
Chris@0 1380
Chris@37 1381 return rv;
Chris@0 1382 }
Chris@114 1383
Chris@130 1384 FFTModel *
Chris@1088 1385 SpectrogramLayer::getFFTModel() const
Chris@114 1386 {
Chris@114 1387 if (!m_model) return 0;
Chris@114 1388
Chris@1087 1389 int fftSize = getFFTSize();
Chris@114 1390
Chris@1088 1391 //!!! it is now surely slower to do this on every getFFTModel()
Chris@1088 1392 //!!! request than it would be to recreate the model immediately
Chris@1088 1393 //!!! when something changes instead of just invalidating it
Chris@920 1394
Chris@1088 1395 if (m_fftModel &&
Chris@1088 1396 m_fftModel->getHeight() == fftSize / 2 + 1 &&
Chris@1088 1397 m_fftModel->getWindowIncrement() == getWindowIncrement()) {
Chris@1088 1398 return m_fftModel;
Chris@114 1399 }
Chris@1088 1400
Chris@1088 1401 delete m_peakCache;
Chris@1088 1402 m_peakCache = 0;
Chris@1088 1403
Chris@1088 1404 delete m_fftModel;
Chris@1088 1405 m_fftModel = new FFTModel(m_model,
Chris@1088 1406 m_channel,
Chris@1088 1407 m_windowType,
Chris@1088 1408 m_windowSize,
Chris@1088 1409 getWindowIncrement(),
Chris@1088 1410 fftSize);
Chris@1088 1411
Chris@1088 1412 if (!m_fftModel->isOK()) {
Chris@1088 1413 QMessageBox::critical
Chris@1088 1414 (0, tr("FFT cache failed"),
Chris@1088 1415 tr("Failed to create the FFT model for this spectrogram.\n"
Chris@1088 1416 "There may be insufficient memory or disc space to continue."));
Chris@1088 1417 delete m_fftModel;
Chris@1088 1418 m_fftModel = 0;
Chris@1088 1419 return 0;
Chris@114 1420 }
Chris@114 1421
Chris@1088 1422 ((SpectrogramLayer *)this)->sliceableModelReplaced(0, m_fftModel);
Chris@1088 1423
Chris@1088 1424 return m_fftModel;
Chris@114 1425 }
Chris@114 1426
Chris@484 1427 Dense3DModelPeakCache *
Chris@1088 1428 SpectrogramLayer::getPeakCache() const
Chris@484 1429 {
Chris@1088 1430 //!!! see comment in getFFTModel
Chris@1088 1431
Chris@1088 1432 if (!m_peakCache) {
Chris@1088 1433 FFTModel *f = getFFTModel();
Chris@484 1434 if (!f) return 0;
Chris@1088 1435 m_peakCache = new Dense3DModelPeakCache(f, m_peakCacheDivisor);
Chris@484 1436 }
Chris@1088 1437 return m_peakCache;
Chris@484 1438 }
Chris@484 1439
Chris@193 1440 const Model *
Chris@193 1441 SpectrogramLayer::getSliceableModel() const
Chris@193 1442 {
Chris@1088 1443 return m_fftModel;
Chris@193 1444 }
Chris@193 1445
Chris@114 1446 void
Chris@1088 1447 SpectrogramLayer::invalidateFFTModel()
Chris@114 1448 {
Chris@1044 1449 #ifdef DEBUG_SPECTROGRAM
Chris@1088 1450 cerr << "SpectrogramLayer::invalidateFFTModel called" << endl;
Chris@1044 1451 #endif
Chris@1088 1452
Chris@1088 1453 emit sliceableModelReplaced(m_fftModel, 0);
Chris@1088 1454
Chris@1088 1455 delete m_fftModel;
Chris@1088 1456 delete m_peakCache;
Chris@1088 1457
Chris@1088 1458 m_fftModel = 0;
Chris@1088 1459 m_peakCache = 0;
Chris@114 1460 }
Chris@114 1461
Chris@0 1462 void
Chris@119 1463 SpectrogramLayer::invalidateMagnitudes()
Chris@119 1464 {
Chris@1044 1465 #ifdef DEBUG_SPECTROGRAM
Chris@1044 1466 cerr << "SpectrogramLayer::invalidateMagnitudes called" << endl;
Chris@1044 1467 #endif
Chris@119 1468 m_viewMags.clear();
Chris@1025 1469 for (vector<MagnitudeRange>::iterator i = m_columnMags.begin();
Chris@119 1470 i != m_columnMags.end(); ++i) {
Chris@119 1471 *i = MagnitudeRange();
Chris@119 1472 }
Chris@119 1473 }
Chris@119 1474
Chris@119 1475 bool
Chris@918 1476 SpectrogramLayer::updateViewMagnitudes(LayerGeometryProvider *v) const
Chris@119 1477 {
Chris@119 1478 MagnitudeRange mag;
Chris@119 1479
Chris@918 1480 int x0 = 0, x1 = v->getPaintWidth();
Chris@905 1481 double s00 = 0, s01 = 0, s10 = 0, s11 = 0;
Chris@119 1482
Chris@203 1483 if (!getXBinRange(v, x0, s00, s01)) {
Chris@907 1484 s00 = s01 = double(m_model->getStartFrame()) / getWindowIncrement();
Chris@203 1485 }
Chris@203 1486
Chris@203 1487 if (!getXBinRange(v, x1, s10, s11)) {
Chris@907 1488 s10 = s11 = double(m_model->getEndFrame()) / getWindowIncrement();
Chris@203 1489 }
Chris@119 1490
Chris@1025 1491 int s0 = int(min(s00, s10) + 0.0001);
Chris@1025 1492 int s1 = int(max(s01, s11) + 0.0001);
Chris@203 1493
Chris@587 1494 // SVDEBUG << "SpectrogramLayer::updateViewMagnitudes: x0 = " << x0 << ", x1 = " << x1 << ", s00 = " << s00 << ", s11 = " << s11 << " s0 = " << s0 << ", s1 = " << s1 << endl;
Chris@119 1495
Chris@248 1496 if (int(m_columnMags.size()) <= s1) {
Chris@119 1497 m_columnMags.resize(s1 + 1);
Chris@119 1498 }
Chris@119 1499
Chris@119 1500 for (int s = s0; s <= s1; ++s) {
Chris@119 1501 if (m_columnMags[s].isSet()) {
Chris@119 1502 mag.sample(m_columnMags[s]);
Chris@119 1503 }
Chris@119 1504 }
Chris@119 1505
Chris@184 1506 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@985 1507 cerr << "SpectrogramLayer::updateViewMagnitudes returning from cols "
Chris@1044 1508 << s0 << " -> " << s1 << " inclusive" << endl;
Chris@1044 1509 cerr << "SpectrogramLayer::updateViewMagnitudes: for view id " << v->getId()
Chris@1044 1510 << ": min is " << mag.getMin() << ", max is " << mag.getMax() << endl;
Chris@184 1511 #endif
Chris@119 1512
Chris@119 1513 if (!mag.isSet()) return false;
Chris@1030 1514 if (mag == m_viewMags[v->getId()]) return false;
Chris@1030 1515 m_viewMags[v->getId()] = mag;
Chris@119 1516 return true;
Chris@119 1517 }
Chris@119 1518
Chris@119 1519 void
Chris@389 1520 SpectrogramLayer::setSynchronousPainting(bool synchronous)
Chris@389 1521 {
Chris@389 1522 m_synchronous = synchronous;
Chris@389 1523 }
Chris@389 1524
Chris@1030 1525 ScrollableImageCache &
Chris@1030 1526 SpectrogramLayer::getImageCacheReference(const LayerGeometryProvider *view) const
Chris@1030 1527 {
Chris@1030 1528 if (m_imageCaches.find(view->getId()) == m_imageCaches.end()) {
Chris@1090 1529 m_imageCaches[view->getId()] = ScrollableImageCache();
Chris@1030 1530 }
Chris@1030 1531 return m_imageCaches.at(view->getId());
Chris@1030 1532 }
Chris@1030 1533
Chris@389 1534 void
Chris@1089 1535 SpectrogramLayer::paintAlternative(LayerGeometryProvider *v, QPainter &paint, QRect rect) const
Chris@1089 1536 {
Chris@1089 1537 Colour3DPlotRenderer *renderer = getRenderer(v);
Chris@1089 1538
Chris@1090 1539 //!!! not time-constrained for now
Chris@1090 1540 Colour3DPlotRenderer::RenderResult result = renderer->render(v, paint, rect);
Chris@1090 1541
Chris@1090 1542 //!!! do
Chris@1090 1543 (void)result;
Chris@1089 1544 }
Chris@1089 1545
Chris@1089 1546 Colour3DPlotRenderer *
Chris@1089 1547 SpectrogramLayer::getRenderer(LayerGeometryProvider *v) const
Chris@1089 1548 {
Chris@1089 1549 if (m_renderers.find(v->getId()) == m_renderers.end()) {
Chris@1089 1550
Chris@1089 1551 Colour3DPlotRenderer::Sources sources;
Chris@1089 1552 sources.verticalBinLayer = this;
Chris@1090 1553 sources.fft = getFFTModel();
Chris@1090 1554 sources.source = sources.fft;
Chris@1090 1555 sources.peaks = getPeakCache();
Chris@1089 1556
Chris@1089 1557 ::ColourScale::Parameters cparams;
Chris@1089 1558 //!!! todo
Chris@1089 1559
Chris@1089 1560 Colour3DPlotRenderer::Parameters params;
Chris@1089 1561 params.colourScale = ::ColourScale(cparams);
Chris@1089 1562 params.normalization = m_normalization;
Chris@1089 1563 //!!! map properly:
Chris@1089 1564 params.binDisplay = (Colour3DPlotRenderer::BinDisplay)(int)m_binDisplay;
Chris@1089 1565 params.binScale = (Colour3DPlotRenderer::BinScale)(int)m_frequencyScale;
Chris@1089 1566 //!!! and the rest
Chris@1089 1567
Chris@1089 1568 m_renderers[v->getId()] = new Colour3DPlotRenderer(sources, params);
Chris@1089 1569 }
Chris@1089 1570
Chris@1089 1571 return m_renderers[v->getId()];
Chris@1089 1572 }
Chris@1089 1573
Chris@1089 1574 void
Chris@916 1575 SpectrogramLayer::paint(LayerGeometryProvider *v, QPainter &paint, QRect rect) const
Chris@0 1576 {
Chris@334 1577 Profiler profiler("SpectrogramLayer::paint", false);
Chris@334 1578
Chris@0 1579 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@1026 1580 cerr << "SpectrogramLayer::paint() entering: m_model is " << m_model << ", zoom level is " << v->getZoomLevel() << endl;
Chris@95 1581
Chris@1026 1582 cerr << "SpectrogramLayer::paint(): rect is " << rect.x() << "," << rect.y() << " " << rect.width() << "x" << rect.height() << endl;
Chris@0 1583 #endif
Chris@95 1584
Chris@907 1585 sv_frame_t startFrame = v->getStartFrame();
Chris@44 1586
Chris@0 1587 if (!m_model || !m_model->isOK() || !m_model->isReady()) {
Chris@0 1588 return;
Chris@0 1589 }
Chris@0 1590
Chris@47 1591 if (isLayerDormant(v)) {
Chris@587 1592 SVDEBUG << "SpectrogramLayer::paint(): Layer is dormant, making it undormant again" << endl;
Chris@29 1593 }
Chris@29 1594
Chris@1090 1595 paintAlternative(v, paint, rect);
Chris@1090 1596 return;
Chris@1089 1597
Chris@1089 1598 //!!!
Chris@1089 1599
Chris@48 1600 // Need to do this even if !isLayerDormant, as that could mean v
Chris@48 1601 // is not in the dormancy map at all -- we need it to be present
Chris@48 1602 // and accountable for when determining whether we need the cache
Chris@48 1603 // in the cache-fill thread above.
Chris@806 1604 //!!! no inter use cache-fill thread
Chris@131 1605 const_cast<SpectrogramLayer *>(this)->Layer::setLayerDormant(v, false);
Chris@48 1606
Chris@1087 1607 int fftSize = getFFTSize();
Chris@920 1608
Chris@920 1609 const View *view = v->getView();
Chris@1030 1610 ScrollableImageCache &cache = getImageCacheReference(view);
Chris@95 1611
Chris@95 1612 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@1030 1613 cerr << "SpectrogramLayer::paint(): image cache valid area from " << cache.getValidLeft() << " width " << cache.getValidWidth() << ", height " << cache.getSize().height() << endl;
Chris@1030 1614 if (rect.x() + rect.width() + 1 < cache.getValidLeft() ||
Chris@1030 1615 rect.x() > cache.getValidRight()) {
Chris@1030 1616 cerr << "SpectrogramLayer: NOTE: requested rect is not contiguous with cache valid area" << endl;
Chris@1030 1617 }
Chris@95 1618 #endif
Chris@95 1619
Chris@44 1620 int zoomLevel = v->getZoomLevel();
Chris@0 1621
Chris@1030 1622 int x0 = v->getXForViewX(rect.x());
Chris@1030 1623 int x1 = v->getXForViewX(rect.x() + rect.width());
Chris@1030 1624 if (x0 < 0) x0 = 0;
Chris@1030 1625 if (x1 > v->getPaintWidth()) x1 = v->getPaintWidth();
Chris@1022 1626
Chris@1022 1627 if (updateViewMagnitudes(v)) {
Chris@1022 1628 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@1030 1629 cerr << "SpectrogramLayer: magnitude range changed to [" << m_viewMags[v->getId()].getMin() << "->" << m_viewMags[v->getId()].getMax() << "]" << endl;
Chris@1022 1630 #endif
Chris@1063 1631 if (m_normalization == ColumnOp::NormalizeVisibleArea) {
Chris@1030 1632 cache.invalidate();
Chris@1022 1633 }
Chris@1022 1634 }
Chris@1030 1635
Chris@1030 1636 if (cache.getZoomLevel() != zoomLevel ||
Chris@1030 1637 cache.getSize() != v->getPaintSize()) {
Chris@1031 1638 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@1031 1639 cerr << "SpectrogramLayer: resizing image cache from "
Chris@1031 1640 << cache.getSize().width() << "x" << cache.getSize().height()
Chris@1031 1641 << " to "
Chris@1031 1642 << v->getPaintSize().width() << "x" << v->getPaintSize().height()
Chris@1031 1643 << " and updating zoom level from " << cache.getZoomLevel()
Chris@1031 1644 << " to " << zoomLevel
Chris@1031 1645 << endl;
Chris@1031 1646 #endif
Chris@1030 1647 cache.resize(v->getPaintSize());
Chris@1031 1648 cache.setZoomLevel(zoomLevel);
Chris@1031 1649 cache.setStartFrame(startFrame);
Chris@1030 1650 }
Chris@1023 1651
Chris@1030 1652 if (cache.isValid()) {
Chris@482 1653
Chris@1030 1654 if (v->getXForFrame(cache.getStartFrame()) ==
Chris@1030 1655 v->getXForFrame(startFrame) &&
Chris@1030 1656 cache.getValidLeft() <= x0 &&
Chris@1030 1657 cache.getValidRight() >= x1) {
Chris@1022 1658
Chris@0 1659 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@1030 1660 cerr << "SpectrogramLayer: image cache hit!" << endl;
Chris@0 1661 #endif
Chris@0 1662
Chris@1030 1663 paint.drawImage(rect, cache.getImage(), rect);
Chris@1030 1664
Chris@1030 1665 illuminateLocalFeatures(v, paint);
Chris@1030 1666 return;
Chris@1030 1667
Chris@1030 1668 } else {
Chris@1030 1669
Chris@1030 1670 // cache doesn't begin at the right frame or doesn't
Chris@1030 1671 // contain the complete view, but might be scrollable or
Chris@1030 1672 // partially usable
Chris@1022 1673
Chris@0 1674 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@1030 1675 cerr << "SpectrogramLayer: scrolling the image cache if applicable" << endl;
Chris@0 1676 #endif
Chris@0 1677
Chris@1090 1678 cache.scrollTo(v, startFrame);
Chris@1030 1679
Chris@0 1680 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@1031 1681 cerr << "SpectrogramLayer: after scrolling, cache valid from "
Chris@1030 1682 << cache.getValidLeft() << " width " << cache.getValidWidth()
Chris@1030 1683 << endl;
Chris@0 1684 #endif
Chris@1030 1685 }
Chris@0 1686 }
Chris@95 1687
Chris@1030 1688 bool rightToLeft = false;
Chris@1030 1689
Chris@1030 1690 if (!cache.isValid()) {
Chris@1028 1691 if (!m_synchronous) {
Chris@1028 1692 // When rendering the whole thing, start from somewhere near
Chris@1028 1693 // the middle so that the region of interest appears first
Chris@1038 1694
Chris@1038 1695 //!!! (perhaps we should have some cunning test to avoid
Chris@1038 1696 //!!! doing this if past repaints have appeared fast
Chris@1038 1697 //!!! enough to do the whole width in one shot)
Chris@1030 1698 if (x0 == 0 && x1 == v->getPaintWidth()) {
Chris@1037 1699 x0 = int(x1 * 0.3);
Chris@1030 1700 }
Chris@1028 1701 }
Chris@1030 1702 } else {
Chris@1030 1703 // When rendering only a part of the cache, we need to make
Chris@1030 1704 // sure that the part we're rendering is adjacent to (or
Chris@1030 1705 // overlapping) a valid area of cache, if we have one. The
Chris@1030 1706 // alternative is to ditch the valid area of cache and render
Chris@1030 1707 // only the requested area, but that's risky because this can
Chris@1030 1708 // happen when just waving the pointer over a small part of
Chris@1030 1709 // the view -- if we lose the partly-built cache every time
Chris@1030 1710 // the user does that, we'll never finish building it.
Chris@1030 1711 int left = x0;
Chris@1030 1712 int width = x1 - x0;
Chris@1030 1713 bool isLeftOfValidArea = false;
Chris@1031 1714 cache.adjustToTouchValidArea(left, width, isLeftOfValidArea);
Chris@1030 1715 x0 = left;
Chris@1030 1716 x1 = x0 + width;
Chris@1030 1717
Chris@1030 1718 // That call also told us whether we should be painting
Chris@1030 1719 // sub-regions of our target region in right-to-left order in
Chris@1030 1720 // order to ensure contiguity
Chris@1030 1721 rightToLeft = isLeftOfValidArea;
Chris@95 1722 }
Chris@1030 1723
Chris@224 1724 // We always paint the full height when refreshing the cache.
Chris@224 1725 // Smaller heights can be used when painting direct from cache
Chris@224 1726 // (further up in this function), but we want to ensure the cache
Chris@224 1727 // is coherent without having to worry about vertical matching of
Chris@224 1728 // required and valid areas as well as horizontal.
Chris@918 1729 int h = v->getPaintHeight();
Chris@1025 1730
Chris@1024 1731 int repaintWidth = x1 - x0;
Chris@0 1732
Chris@95 1733 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@1026 1734 cerr << "SpectrogramLayer: x0 " << x0 << ", x1 " << x1
Chris@1026 1735 << ", repaintWidth " << repaintWidth << ", h " << h
Chris@1030 1736 << ", rightToLeft " << rightToLeft << endl;
Chris@95 1737 #endif
Chris@95 1738
Chris@907 1739 sv_samplerate_t sr = m_model->getSampleRate();
Chris@122 1740
Chris@122 1741 // Set minFreq and maxFreq to the frequency extents of the possibly
Chris@122 1742 // zero-padded visible bin range, and displayMinFreq and displayMaxFreq
Chris@122 1743 // to the actual scale frequency extents (presumably not zero padded).
Chris@253 1744
Chris@1086 1745 // If we are zero padding (i.e. oversampling) we want to use the
Chris@1086 1746 // zero-padded equivalents of the bins that we would be using if
Chris@1086 1747 // not zero padded, to avoid spaces at the top and bottom of the
Chris@1086 1748 // display.
Chris@35 1749
Chris@1087 1750 int maxbin = fftSize / 2;
Chris@35 1751 if (m_maxFrequency > 0) {
Chris@1087 1752 maxbin = int((double(m_maxFrequency) * fftSize) / sr + 0.001);
Chris@1087 1753 if (maxbin > fftSize / 2) maxbin = fftSize / 2;
Chris@35 1754 }
Chris@111 1755
Chris@805 1756 int minbin = 1;
Chris@37 1757 if (m_minFrequency > 0) {
Chris@1087 1758 minbin = int((double(m_minFrequency) * fftSize) / sr + 0.001);
Chris@682 1759 // cerr << "m_minFrequency = " << m_minFrequency << " -> minbin = " << minbin << endl;
Chris@40 1760 if (minbin < 1) minbin = 1;
Chris@184 1761 if (minbin >= maxbin) minbin = maxbin - 1;
Chris@37 1762 }
Chris@37 1763
Chris@1086 1764 int over = getFFTOversampling();
Chris@1086 1765 minbin = minbin * over;
Chris@1086 1766 maxbin = (maxbin + 1) * over - 1;
Chris@253 1767
Chris@905 1768 double minFreq = (double(minbin) * sr) / fftSize;
Chris@905 1769 double maxFreq = (double(maxbin) * sr) / fftSize;
Chris@905 1770
Chris@905 1771 double displayMinFreq = minFreq;
Chris@905 1772 double displayMaxFreq = maxFreq;
Chris@122 1773
Chris@1087 1774 //!!! if (fftSize != getFFTSize()) {
Chris@1087 1775 // displayMinFreq = getEffectiveMinFrequency();
Chris@1087 1776 // displayMaxFreq = getEffectiveMaxFrequency();
Chris@1087 1777 // }
Chris@122 1778
Chris@682 1779 // cerr << "(giving actual minFreq " << minFreq << " and display minFreq " << displayMinFreq << ")" << endl;
Chris@253 1780
Chris@518 1781 int increment = getWindowIncrement();
Chris@40 1782
Chris@40 1783 bool logarithmic = (m_frequencyScale == LogFrequencyScale);
Chris@1026 1784
Chris@1030 1785 MagnitudeRange overallMag = m_viewMags[v->getId()];
Chris@119 1786 bool overallMagChanged = false;
Chris@119 1787
Chris@137 1788 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@1026 1789 cerr << "SpectrogramLayer: " << ((double(v->getFrameForX(1) - v->getFrameForX(0))) / increment) << " bin(s) per pixel" << endl;
Chris@137 1790 #endif
Chris@137 1791
Chris@1024 1792 if (repaintWidth == 0) {
Chris@1024 1793 SVDEBUG << "*** NOTE: repaintWidth == 0" << endl;
Chris@331 1794 }
Chris@331 1795
Chris@382 1796 Profiler outerprof("SpectrogramLayer::paint: all cols");
Chris@382 1797
Chris@481 1798 // The draw buffer contains a fragment at either our pixel
Chris@481 1799 // resolution (if there is more than one time-bin per pixel) or
Chris@481 1800 // time-bin resolution (if a time-bin spans more than one pixel).
Chris@481 1801 // We need to ensure that it starts and ends at points where a
Chris@481 1802 // time-bin boundary occurs at an exact pixel boundary, and with a
Chris@481 1803 // certain amount of overlap across existing pixels so that we can
Chris@481 1804 // scale and draw from it without smoothing errors at the edges.
Chris@481 1805
Chris@481 1806 // If (getFrameForX(x) / increment) * increment ==
Chris@481 1807 // getFrameForX(x), then x is a time-bin boundary. We want two
Chris@481 1808 // such boundaries at either side of the draw buffer -- one which
Chris@481 1809 // we draw up to, and one which we subsequently crop at.
Chris@481 1810
Chris@1039 1811 bool bufferIsBinResolution = false;
Chris@1039 1812 if (increment > zoomLevel) bufferIsBinResolution = true;
Chris@481 1813
Chris@907 1814 sv_frame_t leftBoundaryFrame = -1, leftCropFrame = -1;
Chris@907 1815 sv_frame_t rightBoundaryFrame = -1, rightCropFrame = -1;
Chris@481 1816
Chris@481 1817 int bufwid;
Chris@481 1818
Chris@1039 1819 if (bufferIsBinResolution) {
Chris@481 1820
Chris@482 1821 for (int x = x0; ; --x) {
Chris@907 1822 sv_frame_t f = v->getFrameForX(x);
Chris@481 1823 if ((f / increment) * increment == f) {
Chris@481 1824 if (leftCropFrame == -1) leftCropFrame = f;
Chris@1024 1825 else if (x < x0 - 2) {
Chris@1024 1826 leftBoundaryFrame = f;
Chris@1024 1827 break;
Chris@1024 1828 }
Chris@481 1829 }
Chris@481 1830 }
Chris@1024 1831 for (int x = x0 + repaintWidth; ; ++x) {
Chris@907 1832 sv_frame_t f = v->getFrameForX(x);
Chris@481 1833 if ((f / increment) * increment == f) {
Chris@481 1834 if (rightCropFrame == -1) rightCropFrame = f;
Chris@1024 1835 else if (x > x0 + repaintWidth + 2) {
Chris@1024 1836 rightBoundaryFrame = f;
Chris@1024 1837 break;
Chris@1024 1838 }
Chris@481 1839 }
Chris@481 1840 }
Chris@485 1841 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@481 1842 cerr << "Left: crop: " << leftCropFrame << " (bin " << leftCropFrame/increment << "); boundary: " << leftBoundaryFrame << " (bin " << leftBoundaryFrame/increment << ")" << endl;
Chris@481 1843 cerr << "Right: crop: " << rightCropFrame << " (bin " << rightCropFrame/increment << "); boundary: " << rightBoundaryFrame << " (bin " << rightBoundaryFrame/increment << ")" << endl;
Chris@485 1844 #endif
Chris@481 1845
Chris@907 1846 bufwid = int((rightBoundaryFrame - leftBoundaryFrame) / increment);
Chris@481 1847
Chris@481 1848 } else {
Chris@481 1849
Chris@1024 1850 bufwid = repaintWidth;
Chris@481 1851 }
Chris@481 1852
Chris@907 1853 vector<int> binforx(bufwid);
Chris@907 1854 vector<double> binfory(h);
Chris@907 1855
Chris@484 1856 bool usePeaksCache = false;
Chris@484 1857
Chris@1039 1858 if (bufferIsBinResolution) {
Chris@481 1859 for (int x = 0; x < bufwid; ++x) {
Chris@907 1860 binforx[x] = int(leftBoundaryFrame / increment) + x;
Chris@481 1861 }
Chris@481 1862 m_drawBuffer = QImage(bufwid, h, QImage::Format_Indexed8);
Chris@481 1863 } else {
Chris@481 1864 for (int x = 0; x < bufwid; ++x) {
Chris@905 1865 double s0 = 0, s1 = 0;
Chris@481 1866 if (getXBinRange(v, x + x0, s0, s1)) {
Chris@481 1867 binforx[x] = int(s0 + 0.0001);
Chris@481 1868 } else {
Chris@487 1869 binforx[x] = -1; //???
Chris@481 1870 }
Chris@481 1871 }
Chris@1031 1872 if (m_drawBuffer.width() < bufwid || m_drawBuffer.height() != h) {
Chris@481 1873 m_drawBuffer = QImage(bufwid, h, QImage::Format_Indexed8);
Chris@480 1874 }
Chris@1054 1875 usePeaksCache = (increment * m_peakCacheDivisor) < zoomLevel;
Chris@487 1876 if (m_colourScale == PhaseColourScale) usePeaksCache = false;
Chris@480 1877 }
Chris@481 1878
Chris@481 1879 for (int pixel = 0; pixel < 256; ++pixel) {
Chris@907 1880 m_drawBuffer.setColor((unsigned char)pixel,
Chris@907 1881 m_palette.getColour((unsigned char)pixel).rgb());
Chris@481 1882 }
Chris@481 1883
Chris@481 1884 m_drawBuffer.fill(0);
Chris@1024 1885 int attainedBufwid = bufwid;
Chris@1039 1886
Chris@1039 1887 double softTimeLimit;
Chris@1039 1888
Chris@1039 1889 if (m_synchronous) {
Chris@1039 1890
Chris@1039 1891 // must paint the whole thing for synchronous mode, so give
Chris@1039 1892 // "no timeout"
Chris@1039 1893 softTimeLimit = 0.0;
Chris@1039 1894
Chris@1039 1895 } else if (bufferIsBinResolution) {
Chris@1039 1896
Chris@1039 1897 // calculating boundaries later will be too fiddly for partial
Chris@1039 1898 // paints, and painting should be fast anyway when this is the
Chris@1039 1899 // case because it means we're well zoomed in
Chris@1039 1900 softTimeLimit = 0.0;
Chris@1039 1901
Chris@1039 1902 } else {
Chris@1039 1903
Chris@1039 1904 // neither limitation applies, so use a short soft limit
Chris@1039 1905
Chris@1039 1906 if (m_binDisplay == PeakFrequencies) {
Chris@1039 1907 softTimeLimit = 0.15;
Chris@1039 1908 } else {
Chris@1039 1909 softTimeLimit = 0.1;
Chris@1039 1910 }
Chris@1039 1911 }
Chris@1039 1912
Chris@488 1913 if (m_binDisplay != PeakFrequencies) {
Chris@488 1914
Chris@488 1915 for (int y = 0; y < h; ++y) {
Chris@905 1916 double q0 = 0, q1 = 0;
Chris@1086 1917 if (!getYBinRange(v, h-y-1, q0, q1)) {
Chris@488 1918 binfory[y] = -1;
Chris@488 1919 } else {
Chris@490 1920 binfory[y] = q0;
Chris@488 1921 }
Chris@480 1922 }
Chris@488 1923
Chris@1024 1924 attainedBufwid =
Chris@1026 1925 paintDrawBuffer(v, bufwid, h, binforx, binfory,
Chris@1026 1926 usePeaksCache,
Chris@1026 1927 overallMag, overallMagChanged,
Chris@1039 1928 rightToLeft,
Chris@1039 1929 softTimeLimit);
Chris@488 1930
Chris@488 1931 } else {
Chris@488 1932
Chris@1024 1933 attainedBufwid =
Chris@1024 1934 paintDrawBufferPeakFrequencies(v, bufwid, h, binforx,
Chris@1024 1935 minbin, maxbin,
Chris@1024 1936 displayMinFreq, displayMaxFreq,
Chris@1024 1937 logarithmic,
Chris@1026 1938 overallMag, overallMagChanged,
Chris@1039 1939 rightToLeft,
Chris@1039 1940 softTimeLimit);
Chris@480 1941 }
Chris@481 1942
Chris@1024 1943 int failedToRepaint = bufwid - attainedBufwid;
Chris@1031 1944
Chris@1031 1945 int paintedLeft = x0;
Chris@1031 1946 int paintedWidth = x1 - x0;
Chris@1031 1947
Chris@1025 1948 if (failedToRepaint > 0) {
Chris@1031 1949
Chris@1025 1950 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@1026 1951 cerr << "SpectrogramLayer::paint(): Failed to repaint " << failedToRepaint << " of " << bufwid
Chris@1029 1952 << " columns in time (so managed to repaint " << bufwid - failedToRepaint << ")" << endl;
Chris@1025 1953 #endif
Chris@1031 1954
Chris@1031 1955 if (rightToLeft) {
Chris@1031 1956 paintedLeft += failedToRepaint;
Chris@1031 1957 }
Chris@1031 1958
Chris@1031 1959 paintedWidth -= failedToRepaint;
Chris@1031 1960
Chris@1031 1961 if (paintedWidth < 0) {
Chris@1031 1962 paintedWidth = 0;
Chris@1031 1963 }
Chris@1031 1964
Chris@1025 1965 } else if (failedToRepaint < 0) {
Chris@1024 1966 cerr << "WARNING: failedToRepaint < 0 (= " << failedToRepaint << ")"
Chris@1024 1967 << endl;
Chris@1024 1968 failedToRepaint = 0;
Chris@1024 1969 }
Chris@1031 1970
Chris@119 1971 if (overallMagChanged) {
Chris@1030 1972 m_viewMags[v->getId()] = overallMag;
Chris@209 1973 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@1030 1974 cerr << "SpectrogramLayer: Overall mag is now [" << m_viewMags[v->getId()].getMin() << "->" << m_viewMags[v->getId()].getMax() << "] - will be updating" << endl;
Chris@209 1975 #endif
Chris@119 1976 }
Chris@119 1977
Chris@382 1978 outerprof.end();
Chris@382 1979
Chris@382 1980 Profiler profiler2("SpectrogramLayer::paint: draw image");
Chris@137 1981
Chris@1031 1982 if (paintedWidth > 0) {
Chris@1024 1983
Chris@224 1984 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@1031 1985 cerr << "SpectrogramLayer: Copying " << paintedWidth << "x" << h
Chris@1031 1986 << " from draw buffer at " << paintedLeft - x0 << "," << 0
Chris@1031 1987 << " to " << paintedWidth << "x" << h << " on cache at "
Chris@585 1988 << x0 << "," << 0 << endl;
Chris@224 1989 #endif
Chris@224 1990
Chris@1039 1991 if (bufferIsBinResolution) {
Chris@1031 1992
Chris@481 1993 int scaledLeft = v->getXForFrame(leftBoundaryFrame);
Chris@481 1994 int scaledRight = v->getXForFrame(rightBoundaryFrame);
Chris@1031 1995
Chris@485 1996 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@1026 1997 cerr << "SpectrogramLayer: Rescaling image from " << bufwid
Chris@481 1998 << "x" << h << " to "
Chris@481 1999 << scaledRight-scaledLeft << "x" << h << endl;
Chris@485 2000 #endif
Chris@1031 2001
Chris@490 2002 Preferences::SpectrogramXSmoothing xsmoothing =
Chris@490 2003 Preferences::getInstance()->getSpectrogramXSmoothing();
Chris@1026 2004
Chris@481 2005 QImage scaled = m_drawBuffer.scaled
Chris@481 2006 (scaledRight - scaledLeft, h,
Chris@490 2007 Qt::IgnoreAspectRatio,
Chris@490 2008 ((xsmoothing == Preferences::SpectrogramXInterpolated) ?
Chris@490 2009 Qt::SmoothTransformation : Qt::FastTransformation));
Chris@1026 2010
Chris@481 2011 int scaledLeftCrop = v->getXForFrame(leftCropFrame);
Chris@481 2012 int scaledRightCrop = v->getXForFrame(rightCropFrame);
Chris@1031 2013
Chris@485 2014 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@1026 2015 cerr << "SpectrogramLayer: Drawing image region of width " << scaledRightCrop - scaledLeftCrop << " to "
Chris@481 2016 << scaledLeftCrop << " from " << scaledLeftCrop - scaledLeft << endl;
Chris@485 2017 #endif
Chris@1030 2018
Chris@1040 2019 int targetLeft = scaledLeftCrop;
Chris@1040 2020 if (targetLeft < 0) {
Chris@1040 2021 targetLeft = 0;
Chris@1040 2022 }
Chris@1040 2023
Chris@1040 2024 int targetWidth = scaledRightCrop - targetLeft;
Chris@1040 2025 if (targetLeft + targetWidth > cache.getSize().width()) {
Chris@1040 2026 targetWidth = cache.getSize().width() - targetLeft;
Chris@1040 2027 }
Chris@1031 2028
Chris@1040 2029 int sourceLeft = targetLeft - scaledLeft;
Chris@1040 2030 if (sourceLeft < 0) {
Chris@1040 2031 sourceLeft = 0;
Chris@1040 2032 }
Chris@1040 2033
Chris@1040 2034 int sourceWidth = targetWidth;
Chris@1040 2035
Chris@1040 2036 if (targetWidth > 0) {
Chris@1040 2037 cache.drawImage
Chris@1040 2038 (targetLeft,
Chris@1040 2039 targetWidth,
Chris@1040 2040 scaled,
Chris@1040 2041 sourceLeft,
Chris@1040 2042 sourceWidth);
Chris@1040 2043 }
Chris@1024 2044
Chris@481 2045 } else {
Chris@1024 2046
Chris@1031 2047 cache.drawImage(paintedLeft, paintedWidth,
Chris@1030 2048 m_drawBuffer,
Chris@1031 2049 paintedLeft - x0, paintedWidth);
Chris@481 2050 }
Chris@331 2051 }
Chris@331 2052
Chris@1026 2053 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@1030 2054 cerr << "SpectrogramLayer: Cache valid area now from " << cache.getValidLeft()
Chris@1030 2055 << " width " << cache.getValidWidth() << ", height "
Chris@1030 2056 << cache.getSize().height() << endl;
Chris@1026 2057 #endif
Chris@1030 2058
Chris@1030 2059 QRect pr = rect & cache.getValidArea();
Chris@337 2060
Chris@337 2061 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@1026 2062 cerr << "SpectrogramLayer: Copying " << pr.width() << "x" << pr.height()
Chris@337 2063 << " from cache at " << pr.x() << "," << pr.y()
Chris@585 2064 << " to window" << endl;
Chris@337 2065 #endif
Chris@337 2066
Chris@1030 2067 paint.drawImage(pr.x(), pr.y(), cache.getImage(),
Chris@479 2068 pr.x(), pr.y(), pr.width(), pr.height());
Chris@337 2069
Chris@389 2070 if (!m_synchronous) {
Chris@389 2071
Chris@1063 2072 if ((m_normalization != ColumnOp::NormalizeVisibleArea) || !overallMagChanged) {
Chris@1031 2073
Chris@1031 2074 QRect areaLeft(0, 0, cache.getValidLeft(), h);
Chris@1031 2075 QRect areaRight(cache.getValidRight(), 0,
Chris@1031 2076 cache.getSize().width() - cache.getValidRight(), h);
Chris@1031 2077
Chris@1031 2078 bool haveSpaceLeft = (areaLeft.width() > 0);
Chris@1031 2079 bool haveSpaceRight = (areaRight.width() > 0);
Chris@1031 2080
Chris@1031 2081 bool updateLeft = haveSpaceLeft;
Chris@1031 2082 bool updateRight = haveSpaceRight;
Chris@1031 2083
Chris@1031 2084 if (updateLeft && updateRight) {
Chris@1031 2085 if (rightToLeft) {
Chris@1031 2086 // we just did something adjoining the cache on
Chris@1031 2087 // its left side, so now do something on its right
Chris@1031 2088 updateLeft = false;
Chris@1031 2089 } else {
Chris@1031 2090 updateRight = false;
Chris@1031 2091 }
Chris@389 2092 }
Chris@389 2093
Chris@1031 2094 if (updateLeft) {
Chris@1031 2095 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@1031 2096 cerr << "SpectrogramLayer::paint() updating left ("
Chris@1031 2097 << areaLeft.x() << ", "
Chris@1031 2098 << areaLeft.width() << ")" << endl;
Chris@1031 2099 #endif
Chris@1031 2100 v->updatePaintRect(areaLeft);
Chris@1031 2101 }
Chris@1031 2102
Chris@1031 2103 if (updateRight) {
Chris@389 2104 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@985 2105 cerr << "SpectrogramLayer::paint() updating right ("
Chris@1031 2106 << areaRight.x() << ", "
Chris@1031 2107 << areaRight.width() << ")" << endl;
Chris@389 2108 #endif
Chris@1031 2109 v->updatePaintRect(areaRight);
Chris@389 2110 }
Chris@1031 2111
Chris@389 2112 } else {
Chris@389 2113 // overallMagChanged
Chris@682 2114 cerr << "\noverallMagChanged - updating all\n" << endl;
Chris@1030 2115 cache.invalidate();
Chris@1030 2116 v->updatePaintRect(v->getPaintRect());
Chris@119 2117 }
Chris@95 2118 }
Chris@0 2119
Chris@121 2120 illuminateLocalFeatures(v, paint);
Chris@120 2121
Chris@0 2122 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@985 2123 cerr << "SpectrogramLayer::paint() returning" << endl;
Chris@0 2124 #endif
Chris@0 2125 }
Chris@0 2126
Chris@1024 2127 int
Chris@918 2128 SpectrogramLayer::paintDrawBufferPeakFrequencies(LayerGeometryProvider *v,
Chris@488 2129 int w,
Chris@488 2130 int h,
Chris@907 2131 const vector<int> &binforx,
Chris@488 2132 int minbin,
Chris@488 2133 int maxbin,
Chris@905 2134 double displayMinFreq,
Chris@905 2135 double displayMaxFreq,
Chris@491 2136 bool logarithmic,
Chris@491 2137 MagnitudeRange &overallMag,
Chris@1026 2138 bool &overallMagChanged,
Chris@1039 2139 bool rightToLeft,
Chris@1039 2140 double softTimeLimit) const
Chris@488 2141 {
Chris@488 2142 Profiler profiler("SpectrogramLayer::paintDrawBufferPeakFrequencies");
Chris@488 2143
Chris@488 2144 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@1026 2145 cerr << "SpectrogramLayer::paintDrawBufferPeakFrequencies: minbin " << minbin << ", maxbin " << maxbin << "; w " << w << ", h " << h << endl;
Chris@488 2146 #endif
Chris@488 2147 if (minbin < 0) minbin = 0;
Chris@488 2148 if (maxbin < 0) maxbin = minbin+1;
Chris@488 2149
Chris@1088 2150 FFTModel *fft = getFFTModel();
Chris@1024 2151 if (!fft) return 0;
Chris@488 2152
Chris@488 2153 FFTModel::PeakSet peakfreqs;
Chris@1064 2154 vector<float> preparedColumn;
Chris@1064 2155
Chris@848 2156 int psx = -1;
Chris@545 2157
Chris@1027 2158 int minColumns = 4;
Chris@1039 2159 bool haveTimeLimits = (softTimeLimit > 0.0);
Chris@1037 2160 double hardTimeLimit = softTimeLimit * 2.0;
Chris@1037 2161 bool overridingSoftLimit = false;
Chris@1027 2162 auto startTime = chrono::steady_clock::now();
Chris@1027 2163
Chris@1026 2164 int start = 0;
Chris@1026 2165 int finish = w;
Chris@1026 2166 int step = 1;
Chris@1026 2167
Chris@1026 2168 if (rightToLeft) {
Chris@1026 2169 start = w-1;
Chris@1026 2170 finish = -1;
Chris@1026 2171 step = -1;
Chris@1026 2172 }
Chris@1026 2173
Chris@1027 2174 int columnCount = 0;
Chris@1027 2175
Chris@1026 2176 for (int x = start; x != finish; x += step) {
Chris@488 2177
Chris@1027 2178 ++columnCount;
Chris@1027 2179
Chris@488 2180 if (binforx[x] < 0) continue;
Chris@488 2181
Chris@488 2182 int sx0 = binforx[x];
Chris@488 2183 int sx1 = sx0;
Chris@488 2184 if (x+1 < w) sx1 = binforx[x+1];
Chris@488 2185 if (sx0 < 0) sx0 = sx1 - 1;
Chris@488 2186 if (sx0 < 0) continue;
Chris@488 2187 if (sx1 <= sx0) sx1 = sx0 + 1;
Chris@488 2188
Chris@1064 2189 vector<float> pixelPeakColumn;
Chris@1064 2190
Chris@488 2191 for (int sx = sx0; sx < sx1; ++sx) {
Chris@488 2192
Chris@1064 2193 if (sx < 0 || sx >= int(fft->getWidth())) {
Chris@1064 2194 continue;
Chris@1064 2195 }
Chris@488 2196
Chris@488 2197 if (sx != psx) {
Chris@1064 2198
Chris@1064 2199 ColumnOp::Column column;
Chris@1064 2200
Chris@1064 2201 column = getColumnFromFFTModel(fft,
Chris@1064 2202 sx,
Chris@1064 2203 minbin,
Chris@1064 2204 maxbin - minbin + 1);
Chris@1064 2205
Chris@1064 2206 if (m_colourScale != PhaseColourScale) {
Chris@1087 2207 column = ColumnOp::fftScale(column, getFFTSize());
Chris@1064 2208 }
Chris@1064 2209
Chris@1064 2210 recordColumnExtents(column,
Chris@1064 2211 sx,
Chris@1064 2212 overallMag,
Chris@1064 2213 overallMagChanged);
Chris@1064 2214
Chris@1064 2215 if (m_colourScale != PhaseColourScale) {
Chris@1064 2216 column = ColumnOp::normalize(column, m_normalization);
Chris@1064 2217 }
Chris@1064 2218
Chris@1064 2219 preparedColumn = ColumnOp::applyGain(column, m_gain);
Chris@1064 2220
Chris@1064 2221 psx = sx;
Chris@1064 2222 }
Chris@1064 2223
Chris@1064 2224 if (sx == sx0) {
Chris@1064 2225 pixelPeakColumn = preparedColumn;
Chris@488 2226 peakfreqs = fft->getPeakFrequencies(FFTModel::AllPeaks, sx,
Chris@488 2227 minbin, maxbin - 1);
Chris@1064 2228 } else {
Chris@1064 2229 for (int i = 0; in_range_for(pixelPeakColumn, i); ++i) {
Chris@1064 2230 pixelPeakColumn[i] = std::max(pixelPeakColumn[i],
Chris@1064 2231 preparedColumn[i]);
Chris@488 2232 }
Chris@488 2233 }
Chris@488 2234 }
Chris@1065 2235
Chris@1065 2236 if (!pixelPeakColumn.empty()) {
Chris@1065 2237 for (FFTModel::PeakSet::const_iterator pi = peakfreqs.begin();
Chris@1065 2238 pi != peakfreqs.end(); ++pi) {
Chris@1065 2239
Chris@1065 2240 int bin = pi->first;
Chris@1065 2241 double freq = pi->second;
Chris@1065 2242
Chris@1065 2243 if (bin < minbin) continue;
Chris@1065 2244 if (bin > maxbin) break;
Chris@1065 2245
Chris@1065 2246 double value = pixelPeakColumn[bin - minbin];
Chris@1065 2247
Chris@1065 2248 double y = v->getYForFrequency
Chris@1065 2249 (freq, displayMinFreq, displayMaxFreq, logarithmic);
Chris@1065 2250
Chris@1065 2251 int iy = int(y + 0.5);
Chris@1065 2252 if (iy < 0 || iy >= h) continue;
Chris@1065 2253
Chris@1065 2254 m_drawBuffer.setPixel(x, iy, getDisplayValue(v, value));
Chris@1065 2255 }
Chris@1065 2256 }
Chris@1064 2257
Chris@1039 2258 if (haveTimeLimits) {
Chris@1027 2259 if (columnCount >= minColumns) {
Chris@1027 2260 auto t = chrono::steady_clock::now();
Chris@1027 2261 double diff = chrono::duration<double>(t - startTime).count();
Chris@1037 2262 if (diff > hardTimeLimit) {
Chris@1027 2263 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@1037 2264 cerr << "SpectrogramLayer::paintDrawBufferPeakFrequencies: hard limit " << hardTimeLimit << " sec exceeded after "
Chris@1031 2265 << columnCount << " columns with time " << diff << endl;
Chris@1027 2266 #endif
Chris@1027 2267 return columnCount;
Chris@1037 2268 } else if (diff > softTimeLimit && !overridingSoftLimit) {
Chris@1037 2269 // If we're more than half way through by the time
Chris@1037 2270 // we reach the soft limit, ignore it (though
Chris@1037 2271 // still respect the hard limit, above). Otherwise
Chris@1037 2272 // respect the soft limit and return now.
Chris@1037 2273 if (columnCount > w/2) {
Chris@1037 2274 overridingSoftLimit = true;
Chris@1037 2275 } else {
Chris@1037 2276 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@1037 2277 cerr << "SpectrogramLayer::paintDrawBufferPeakFrequencies: soft limit " << softTimeLimit << " sec exceeded after "
Chris@1037 2278 << columnCount << " columns with time " << diff << endl;
Chris@1037 2279 #endif
Chris@1037 2280 return columnCount;
Chris@1037 2281 }
Chris@1037 2282 }
Chris@1027 2283 }
Chris@1027 2284 }
Chris@488 2285 }
Chris@1024 2286
Chris@1027 2287 return columnCount;
Chris@488 2288 }
Chris@488 2289
Chris@1058 2290 vector<float>
Chris@1058 2291 SpectrogramLayer::getColumnFromFFTModel(FFTModel *fft,
Chris@1058 2292 int sx, // column number in model
Chris@1058 2293 int minbin,
Chris@1058 2294 int bincount) const
Chris@1058 2295 {
Chris@1058 2296 vector<float> values(bincount, 0.f);
Chris@1058 2297
Chris@1058 2298 if (m_colourScale == PhaseColourScale) {
Chris@1058 2299 fft->getPhasesAt(sx, values.data(), minbin, bincount);
Chris@1058 2300 } else {
Chris@1058 2301 fft->getMagnitudesAt(sx, values.data(), minbin, bincount);
Chris@1058 2302 }
Chris@1058 2303
Chris@1061 2304 return values;
Chris@1058 2305 }
Chris@1058 2306
Chris@1058 2307 vector<float>
Chris@1058 2308 SpectrogramLayer::getColumnFromGenericModel(DenseThreeDimensionalModel *model,
Chris@1058 2309 int sx, // column number in model
Chris@1058 2310 int minbin,
Chris@1058 2311 int bincount) const
Chris@1058 2312 {
Chris@1058 2313 if (m_colourScale == PhaseColourScale) {
Chris@1058 2314 throw std::logic_error("can't use phase scale with generic 3d model");
Chris@1058 2315 }
Chris@1058 2316
Chris@1058 2317 auto col = model->getColumn(sx);
Chris@1058 2318
Chris@1061 2319 return vector<float>(col.data() + minbin,
Chris@1061 2320 col.data() + minbin + bincount);
Chris@1058 2321 }
Chris@1058 2322
Chris@1058 2323 void
Chris@1058 2324 SpectrogramLayer::recordColumnExtents(const vector<float> &col,
Chris@1058 2325 int sx, // column index, for m_columnMags
Chris@1058 2326 MagnitudeRange &overallMag,
Chris@1059 2327 bool &overallMagChanged) const
Chris@1058 2328 {
Chris@1060 2329 if (!in_range_for(m_columnMags, sx)) {
Chris@1063 2330 m_columnMags.resize(sx + 1);
Chris@1058 2331 }
Chris@1058 2332 MagnitudeRange mr;
Chris@1058 2333 for (auto v: col) {
Chris@1058 2334 mr.sample(v);
Chris@1058 2335 }
Chris@1058 2336 m_columnMags[sx] = mr;
Chris@1058 2337 if (overallMag.sample(mr)) {
Chris@1058 2338 overallMagChanged = true;
Chris@1058 2339 }
Chris@1058 2340 }
Chris@1058 2341
Chris@1024 2342 int
Chris@918 2343 SpectrogramLayer::paintDrawBuffer(LayerGeometryProvider *v,
Chris@481 2344 int w,
Chris@481 2345 int h,
Chris@907 2346 const vector<int> &binforx,
Chris@907 2347 const vector<double> &binfory,
Chris@491 2348 bool usePeaksCache,
Chris@491 2349 MagnitudeRange &overallMag,
Chris@1026 2350 bool &overallMagChanged,
Chris@1039 2351 bool rightToLeft,
Chris@1039 2352 double softTimeLimit) const
Chris@480 2353 {
Chris@481 2354 Profiler profiler("SpectrogramLayer::paintDrawBuffer");
Chris@480 2355
Chris@490 2356 int minbin = int(binfory[0] + 0.0001);
Chris@907 2357 int maxbin = int(binfory[h-1]);
Chris@480 2358
Chris@485 2359 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@1026 2360 cerr << "SpectrogramLayer::paintDrawBuffer: minbin " << minbin << ", maxbin " << maxbin << "; w " << w << ", h " << h << endl;
Chris@485 2361 #endif
Chris@480 2362 if (minbin < 0) minbin = 0;
Chris@480 2363 if (maxbin < 0) maxbin = minbin+1;
Chris@480 2364
Chris@1060 2365 DenseThreeDimensionalModel *peakCacheModel = 0;
Chris@1060 2366 FFTModel *fftModel = 0;
Chris@484 2367 DenseThreeDimensionalModel *sourceModel = 0;
Chris@1060 2368
Chris@485 2369 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@1026 2370 cerr << "SpectrogramLayer::paintDrawBuffer: Note: bin display = " << m_binDisplay << ", w = " << w << ", binforx[" << w-1 << "] = " << binforx[w-1] << ", binforx[0] = " << binforx[0] << endl;
Chris@485 2371 #endif
Chris@1060 2372
Chris@1060 2373 int divisor = 1;
Chris@1054 2374 if (usePeaksCache) {
Chris@1088 2375 peakCacheModel = getPeakCache();
Chris@1054 2376 divisor = m_peakCacheDivisor;
Chris@1060 2377 sourceModel = peakCacheModel;
Chris@484 2378 } else {
Chris@1088 2379 fftModel = getFFTModel();
Chris@1060 2380 sourceModel = fftModel;
Chris@484 2381 }
Chris@484 2382
Chris@1024 2383 if (!sourceModel) return 0;
Chris@484 2384
Chris@490 2385 bool interpolate = false;
Chris@490 2386 Preferences::SpectrogramSmoothing smoothing =
Chris@490 2387 Preferences::getInstance()->getSpectrogramSmoothing();
Chris@490 2388 if (smoothing == Preferences::SpectrogramInterpolated ||
Chris@490 2389 smoothing == Preferences::SpectrogramZeroPaddedAndInterpolated) {
Chris@490 2390 if (m_binDisplay != PeakBins &&
Chris@490 2391 m_binDisplay != PeakFrequencies) {
Chris@490 2392 interpolate = true;
Chris@490 2393 }
Chris@490 2394 }
Chris@490 2395
Chris@480 2396 int psx = -1;
Chris@545 2397
Chris@1027 2398 int minColumns = 4;
Chris@1039 2399 bool haveTimeLimits = (softTimeLimit > 0.0);
Chris@1037 2400 double hardTimeLimit = softTimeLimit * 2.0;
Chris@1037 2401 bool overridingSoftLimit = false;
Chris@1027 2402 auto startTime = chrono::steady_clock::now();
Chris@1027 2403
Chris@1026 2404 int start = 0;
Chris@1026 2405 int finish = w;
Chris@1026 2406 int step = 1;
Chris@1026 2407
Chris@1026 2408 if (rightToLeft) {
Chris@1026 2409 start = w-1;
Chris@1026 2410 finish = -1;
Chris@1026 2411 step = -1;
Chris@1026 2412 }
Chris@1027 2413
Chris@1027 2414 int columnCount = 0;
Chris@1026 2415
Chris@1060 2416 vector<float> preparedColumn;
Chris@1060 2417
Chris@1026 2418 for (int x = start; x != finish; x += step) {
Chris@1027 2419
Chris@1056 2420 // x is the on-canvas pixel coord; sx (later) will be the
Chris@1056 2421 // source column index
Chris@1056 2422
Chris@1027 2423 ++columnCount;
Chris@480 2424
Chris@482 2425 if (binforx[x] < 0) continue;
Chris@482 2426
Chris@484 2427 int sx0 = binforx[x] / divisor;
Chris@483 2428 int sx1 = sx0;
Chris@484 2429 if (x+1 < w) sx1 = binforx[x+1] / divisor;
Chris@483 2430 if (sx0 < 0) sx0 = sx1 - 1;
Chris@483 2431 if (sx0 < 0) continue;
Chris@483 2432 if (sx1 <= sx0) sx1 = sx0 + 1;
Chris@483 2433
Chris@1064 2434 vector<float> pixelPeakColumn;
Chris@1064 2435
Chris@483 2436 for (int sx = sx0; sx < sx1; ++sx) {
Chris@483 2437
Chris@518 2438 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@682 2439 // cerr << "sx = " << sx << endl;
Chris@518 2440 #endif
Chris@518 2441
Chris@1064 2442 if (sx < 0 || sx >= sourceModel->getWidth()) {
Chris@1064 2443 continue;
Chris@1064 2444 }
Chris@488 2445
Chris@483 2446 if (sx != psx) {
Chris@1060 2447
Chris@1064 2448 // order:
Chris@1064 2449 // get column -> scale -> record extents ->
Chris@1063 2450 // normalise -> peak pick -> apply display gain ->
Chris@1063 2451 // distribute/interpolate
Chris@1063 2452
Chris@1063 2453 ColumnOp::Column column;
Chris@1063 2454
Chris@1060 2455 if (peakCacheModel) {
Chris@1060 2456 column = getColumnFromGenericModel(peakCacheModel,
Chris@1060 2457 sx,
Chris@1060 2458 minbin,
Chris@1060 2459 maxbin - minbin + 1);
Chris@484 2460 } else {
Chris@1060 2461 column = getColumnFromFFTModel(fftModel,
Chris@1060 2462 sx,
Chris@1060 2463 minbin,
Chris@1060 2464 maxbin - minbin + 1);
Chris@484 2465 }
Chris@1060 2466
Chris@1064 2467 if (m_colourScale != PhaseColourScale) {
Chris@1087 2468 column = ColumnOp::fftScale(column, getFFTSize());
Chris@1064 2469 }
Chris@1063 2470
Chris@1062 2471 recordColumnExtents(column,
Chris@1062 2472 sx,
Chris@1062 2473 overallMag,
Chris@1062 2474 overallMagChanged);
Chris@1062 2475
Chris@1064 2476 if (m_colourScale != PhaseColourScale) {
Chris@1064 2477 column = ColumnOp::normalize(column, m_normalization);
Chris@1064 2478 }
Chris@1063 2479
Chris@1063 2480 if (m_binDisplay == PeakBins) {
Chris@1063 2481 column = ColumnOp::peakPick(column);
Chris@1063 2482 }
Chris@1063 2483
Chris@1062 2484 preparedColumn =
Chris@1064 2485 ColumnOp::distribute(ColumnOp::applyGain(column, m_gain),
Chris@1064 2486 h,
Chris@1064 2487 binfory,
Chris@1064 2488 minbin,
Chris@1064 2489 interpolate);
Chris@1060 2490
Chris@483 2491 psx = sx;
Chris@483 2492 }
Chris@483 2493
Chris@1064 2494 if (sx == sx0) {
Chris@1064 2495 pixelPeakColumn = preparedColumn;
Chris@1064 2496 } else {
Chris@1064 2497 for (int i = 0; in_range_for(pixelPeakColumn, i); ++i) {
Chris@1064 2498 pixelPeakColumn[i] = std::max(pixelPeakColumn[i],
Chris@1064 2499 preparedColumn[i]);
Chris@1064 2500 }
Chris@1064 2501 }
Chris@483 2502 }
Chris@483 2503
Chris@1065 2504 if (!pixelPeakColumn.empty()) {
Chris@1065 2505 for (int y = 0; y < h; ++y) {
Chris@1065 2506 m_drawBuffer.setPixel(x,
Chris@1065 2507 h-y-1,
Chris@1065 2508 getDisplayValue(v, pixelPeakColumn[y]));
Chris@1065 2509 }
Chris@480 2510 }
Chris@1025 2511
Chris@1039 2512 if (haveTimeLimits) {
Chris@1027 2513 if (columnCount >= minColumns) {
Chris@1025 2514 auto t = chrono::steady_clock::now();
Chris@1025 2515 double diff = chrono::duration<double>(t - startTime).count();
Chris@1037 2516 if (diff > hardTimeLimit) {
Chris@1025 2517 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@1037 2518 cerr << "SpectrogramLayer::paintDrawBuffer: hard limit " << hardTimeLimit << " sec exceeded after "
Chris@1031 2519 << columnCount << " columns with time " << diff << endl;
Chris@1025 2520 #endif
Chris@1027 2521 return columnCount;
Chris@1037 2522 } else if (diff > softTimeLimit && !overridingSoftLimit) {
Chris@1037 2523 // If we're more than half way through by the time
Chris@1037 2524 // we reach the soft limit, ignore it (though
Chris@1037 2525 // still respect the hard limit, above). Otherwise
Chris@1037 2526 // respect the soft limit and return now.
Chris@1037 2527 if (columnCount > w/2) {
Chris@1037 2528 overridingSoftLimit = true;
Chris@1037 2529 } else {
Chris@1037 2530 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@1037 2531 cerr << "SpectrogramLayer::paintDrawBuffer: soft limit " << softTimeLimit << " sec exceeded after "
Chris@1037 2532 << columnCount << " columns with time " << diff << endl;
Chris@1037 2533 #endif
Chris@1037 2534 return columnCount;
Chris@1037 2535 }
Chris@1037 2536 }
Chris@1025 2537 }
Chris@1025 2538 }
Chris@480 2539 }
Chris@1024 2540
Chris@1027 2541 return columnCount;
Chris@480 2542 }
Chris@477 2543
Chris@121 2544 void
Chris@918 2545 SpectrogramLayer::illuminateLocalFeatures(LayerGeometryProvider *v, QPainter &paint) const
Chris@121 2546 {
Chris@382 2547 Profiler profiler("SpectrogramLayer::illuminateLocalFeatures");
Chris@382 2548
Chris@121 2549 QPoint localPos;
Chris@121 2550 if (!v->shouldIlluminateLocalFeatures(this, localPos) || !m_model) {
Chris@121 2551 return;
Chris@121 2552 }
Chris@121 2553
Chris@682 2554 // cerr << "SpectrogramLayer: illuminateLocalFeatures("
Chris@682 2555 // << localPos.x() << "," << localPos.y() << ")" << endl;
Chris@121 2556
Chris@905 2557 double s0, s1;
Chris@905 2558 double f0, f1;
Chris@121 2559
Chris@121 2560 if (getXBinRange(v, localPos.x(), s0, s1) &&
Chris@121 2561 getYBinSourceRange(v, localPos.y(), f0, f1)) {
Chris@121 2562
Chris@121 2563 int s0i = int(s0 + 0.001);
Chris@121 2564 int s1i = int(s1);
Chris@121 2565
Chris@121 2566 int x0 = v->getXForFrame(s0i * getWindowIncrement());
Chris@121 2567 int x1 = v->getXForFrame((s1i + 1) * getWindowIncrement());
Chris@121 2568
Chris@248 2569 int y1 = int(getYForFrequency(v, f1));
Chris@248 2570 int y0 = int(getYForFrequency(v, f0));
Chris@121 2571
Chris@682 2572 // cerr << "SpectrogramLayer: illuminate "
Chris@682 2573 // << x0 << "," << y1 << " -> " << x1 << "," << y0 << endl;
Chris@121 2574
Chris@287 2575 paint.setPen(v->getForeground());
Chris@133 2576
Chris@133 2577 //!!! should we be using paintCrosshairs for this?
Chris@133 2578
Chris@121 2579 paint.drawRect(x0, y1, x1 - x0 + 1, y0 - y1 + 1);
Chris@121 2580 }
Chris@121 2581 }
Chris@121 2582
Chris@905 2583 double
Chris@918 2584 SpectrogramLayer::getYForFrequency(const LayerGeometryProvider *v, double frequency) const
Chris@42 2585 {
Chris@44 2586 return v->getYForFrequency(frequency,
Chris@44 2587 getEffectiveMinFrequency(),
Chris@44 2588 getEffectiveMaxFrequency(),
Chris@44 2589 m_frequencyScale == LogFrequencyScale);
Chris@42 2590 }
Chris@42 2591
Chris@905 2592 double
Chris@918 2593 SpectrogramLayer::getFrequencyForY(const LayerGeometryProvider *v, int y) const
Chris@42 2594 {
Chris@44 2595 return v->getFrequencyForY(y,
Chris@44 2596 getEffectiveMinFrequency(),
Chris@44 2597 getEffectiveMaxFrequency(),
Chris@44 2598 m_frequencyScale == LogFrequencyScale);
Chris@42 2599 }
Chris@42 2600
Chris@0 2601 int
Chris@1090 2602 SpectrogramLayer::getCompletion(LayerGeometryProvider *) const
Chris@0 2603 {
Chris@1088 2604 if (!m_fftModel) return 100;
Chris@1088 2605 int completion = m_fftModel->getCompletion();
Chris@224 2606 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@985 2607 cerr << "SpectrogramLayer::getCompletion: completion = " << completion << endl;
Chris@224 2608 #endif
Chris@0 2609 return completion;
Chris@0 2610 }
Chris@0 2611
Chris@583 2612 QString
Chris@1090 2613 SpectrogramLayer::getError(LayerGeometryProvider *) const
Chris@583 2614 {
Chris@1088 2615 if (!m_fftModel) return "";
Chris@1088 2616 return m_fftModel->getError();
Chris@583 2617 }
Chris@583 2618
Chris@28 2619 bool
Chris@905 2620 SpectrogramLayer::getValueExtents(double &min, double &max,
Chris@101 2621 bool &logarithmic, QString &unit) const
Chris@79 2622 {
Chris@133 2623 if (!m_model) return false;
Chris@133 2624
Chris@907 2625 sv_samplerate_t sr = m_model->getSampleRate();
Chris@1087 2626 min = double(sr) / getFFTSize();
Chris@905 2627 max = double(sr) / 2;
Chris@133 2628
Chris@101 2629 logarithmic = (m_frequencyScale == LogFrequencyScale);
Chris@79 2630 unit = "Hz";
Chris@79 2631 return true;
Chris@79 2632 }
Chris@79 2633
Chris@79 2634 bool
Chris@905 2635 SpectrogramLayer::getDisplayExtents(double &min, double &max) const
Chris@101 2636 {
Chris@101 2637 min = getEffectiveMinFrequency();
Chris@101 2638 max = getEffectiveMaxFrequency();
Chris@253 2639
Chris@587 2640 // SVDEBUG << "SpectrogramLayer::getDisplayExtents: " << min << "->" << max << endl;
Chris@101 2641 return true;
Chris@101 2642 }
Chris@101 2643
Chris@101 2644 bool
Chris@905 2645 SpectrogramLayer::setDisplayExtents(double min, double max)
Chris@120 2646 {
Chris@120 2647 if (!m_model) return false;
Chris@187 2648
Chris@587 2649 // SVDEBUG << "SpectrogramLayer::setDisplayExtents: " << min << "->" << max << endl;
Chris@187 2650
Chris@120 2651 if (min < 0) min = 0;
Chris@907 2652 if (max > m_model->getSampleRate()/2.0) max = m_model->getSampleRate()/2.0;
Chris@120 2653
Chris@907 2654 int minf = int(lrint(min));
Chris@907 2655 int maxf = int(lrint(max));
Chris@120 2656
Chris@120 2657 if (m_minFrequency == minf && m_maxFrequency == maxf) return true;
Chris@120 2658
Chris@478 2659 invalidateImageCaches();
Chris@120 2660 invalidateMagnitudes();
Chris@120 2661
Chris@120 2662 m_minFrequency = minf;
Chris@120 2663 m_maxFrequency = maxf;
Chris@120 2664
Chris@120 2665 emit layerParametersChanged();
Chris@120 2666
Chris@133 2667 int vs = getCurrentVerticalZoomStep();
Chris@133 2668 if (vs != m_lastEmittedZoomStep) {
Chris@133 2669 emit verticalZoomChanged();
Chris@133 2670 m_lastEmittedZoomStep = vs;
Chris@133 2671 }
Chris@133 2672
Chris@120 2673 return true;
Chris@120 2674 }
Chris@120 2675
Chris@120 2676 bool
Chris@918 2677 SpectrogramLayer::getYScaleValue(const LayerGeometryProvider *v, int y,
Chris@905 2678 double &value, QString &unit) const
Chris@261 2679 {
Chris@261 2680 value = getFrequencyForY(v, y);
Chris@261 2681 unit = "Hz";
Chris@261 2682 return true;
Chris@261 2683 }
Chris@261 2684
Chris@261 2685 bool
Chris@918 2686 SpectrogramLayer::snapToFeatureFrame(LayerGeometryProvider *,
Chris@907 2687 sv_frame_t &frame,
Chris@805 2688 int &resolution,
Chris@28 2689 SnapType snap) const
Chris@13 2690 {
Chris@13 2691 resolution = getWindowIncrement();
Chris@907 2692 sv_frame_t left = (frame / resolution) * resolution;
Chris@907 2693 sv_frame_t right = left + resolution;
Chris@28 2694
Chris@28 2695 switch (snap) {
Chris@28 2696 case SnapLeft: frame = left; break;
Chris@28 2697 case SnapRight: frame = right; break;
Chris@28 2698 case SnapNearest:
Chris@28 2699 case SnapNeighbouring:
Chris@28 2700 if (frame - left > right - frame) frame = right;
Chris@28 2701 else frame = left;
Chris@28 2702 break;
Chris@28 2703 }
Chris@28 2704
Chris@28 2705 return true;
Chris@28 2706 }
Chris@13 2707
Chris@283 2708 void
Chris@918 2709 SpectrogramLayer::measureDoubleClick(LayerGeometryProvider *v, QMouseEvent *e)
Chris@283 2710 {
Chris@920 2711 const View *view = v->getView();
Chris@1030 2712 ScrollableImageCache &cache = getImageCacheReference(view);
Chris@1030 2713
Chris@1030 2714 cerr << "cache width: " << cache.getSize().width() << ", height: "
Chris@1030 2715 << cache.getSize().height() << endl;
Chris@1030 2716
Chris@1030 2717 QImage image = cache.getImage();
Chris@283 2718
Chris@283 2719 ImageRegionFinder finder;
Chris@283 2720 QRect rect = finder.findRegionExtents(&image, e->pos());
Chris@283 2721 if (rect.isValid()) {
Chris@283 2722 MeasureRect mr;
Chris@283 2723 setMeasureRectFromPixrect(v, mr, rect);
Chris@283 2724 CommandHistory::getInstance()->addCommand
Chris@283 2725 (new AddMeasurementRectCommand(this, mr));
Chris@283 2726 }
Chris@283 2727 }
Chris@283 2728
Chris@77 2729 bool
Chris@918 2730 SpectrogramLayer::getCrosshairExtents(LayerGeometryProvider *v, QPainter &paint,
Chris@77 2731 QPoint cursorPos,
Chris@1025 2732 vector<QRect> &extents) const
Chris@77 2733 {
Chris@918 2734 QRect vertical(cursorPos.x() - 12, 0, 12, v->getPaintHeight());
Chris@77 2735 extents.push_back(vertical);
Chris@77 2736
Chris@77 2737 QRect horizontal(0, cursorPos.y(), cursorPos.x(), 1);
Chris@77 2738 extents.push_back(horizontal);
Chris@77 2739
Chris@608 2740 int sw = getVerticalScaleWidth(v, m_haveDetailedScale, paint);
Chris@264 2741
Chris@280 2742 QRect freq(sw, cursorPos.y() - paint.fontMetrics().ascent() - 2,
Chris@280 2743 paint.fontMetrics().width("123456 Hz") + 2,
Chris@280 2744 paint.fontMetrics().height());
Chris@280 2745 extents.push_back(freq);
Chris@264 2746
Chris@279 2747 QRect pitch(sw, cursorPos.y() + 2,
Chris@279 2748 paint.fontMetrics().width("C#10+50c") + 2,
Chris@279 2749 paint.fontMetrics().height());
Chris@279 2750 extents.push_back(pitch);
Chris@279 2751
Chris@280 2752 QRect rt(cursorPos.x(),
Chris@918 2753 v->getPaintHeight() - paint.fontMetrics().height() - 2,
Chris@280 2754 paint.fontMetrics().width("1234.567 s"),
Chris@280 2755 paint.fontMetrics().height());
Chris@280 2756 extents.push_back(rt);
Chris@280 2757
Chris@280 2758 int w(paint.fontMetrics().width("1234567890") + 2);
Chris@280 2759 QRect frame(cursorPos.x() - w - 2,
Chris@918 2760 v->getPaintHeight() - paint.fontMetrics().height() - 2,
Chris@280 2761 w,
Chris@280 2762 paint.fontMetrics().height());
Chris@280 2763 extents.push_back(frame);
Chris@280 2764
Chris@77 2765 return true;
Chris@77 2766 }
Chris@77 2767
Chris@77 2768 void
Chris@918 2769 SpectrogramLayer::paintCrosshairs(LayerGeometryProvider *v, QPainter &paint,
Chris@77 2770 QPoint cursorPos) const
Chris@77 2771 {
Chris@77 2772 paint.save();
Chris@283 2773
Chris@608 2774 int sw = getVerticalScaleWidth(v, m_haveDetailedScale, paint);
Chris@283 2775
Chris@282 2776 QFont fn = paint.font();
Chris@282 2777 if (fn.pointSize() > 8) {
Chris@282 2778 fn.setPointSize(fn.pointSize() - 1);
Chris@282 2779 paint.setFont(fn);
Chris@282 2780 }
Chris@77 2781 paint.setPen(m_crosshairColour);
Chris@77 2782
Chris@77 2783 paint.drawLine(0, cursorPos.y(), cursorPos.x() - 1, cursorPos.y());
Chris@918 2784 paint.drawLine(cursorPos.x(), 0, cursorPos.x(), v->getPaintHeight());
Chris@77 2785
Chris@905 2786 double fundamental = getFrequencyForY(v, cursorPos.y());
Chris@77 2787
Chris@1078 2788 PaintAssistant::drawVisibleText(v, paint,
Chris@278 2789 sw + 2,
Chris@278 2790 cursorPos.y() - 2,
Chris@278 2791 QString("%1 Hz").arg(fundamental),
Chris@1078 2792 PaintAssistant::OutlinedText);
Chris@278 2793
Chris@279 2794 if (Pitch::isFrequencyInMidiRange(fundamental)) {
Chris@279 2795 QString pitchLabel = Pitch::getPitchLabelForFrequency(fundamental);
Chris@1078 2796 PaintAssistant::drawVisibleText(v, paint,
Chris@279 2797 sw + 2,
Chris@279 2798 cursorPos.y() + paint.fontMetrics().ascent() + 2,
Chris@279 2799 pitchLabel,
Chris@1078 2800 PaintAssistant::OutlinedText);
Chris@279 2801 }
Chris@279 2802
Chris@907 2803 sv_frame_t frame = v->getFrameForX(cursorPos.x());
Chris@279 2804 RealTime rt = RealTime::frame2RealTime(frame, m_model->getSampleRate());
Chris@280 2805 QString rtLabel = QString("%1 s").arg(rt.toText(true).c_str());
Chris@280 2806 QString frameLabel = QString("%1").arg(frame);
Chris@1078 2807 PaintAssistant::drawVisibleText(v, paint,
Chris@280 2808 cursorPos.x() - paint.fontMetrics().width(frameLabel) - 2,
Chris@918 2809 v->getPaintHeight() - 2,
Chris@280 2810 frameLabel,
Chris@1078 2811 PaintAssistant::OutlinedText);
Chris@1078 2812 PaintAssistant::drawVisibleText(v, paint,
Chris@280 2813 cursorPos.x() + 2,
Chris@918 2814 v->getPaintHeight() - 2,
Chris@280 2815 rtLabel,
Chris@1078 2816 PaintAssistant::OutlinedText);
Chris@264 2817
Chris@77 2818 int harmonic = 2;
Chris@77 2819
Chris@77 2820 while (harmonic < 100) {
Chris@77 2821
Chris@907 2822 int hy = int(lrint(getYForFrequency(v, fundamental * harmonic)));
Chris@918 2823 if (hy < 0 || hy > v->getPaintHeight()) break;
Chris@77 2824
Chris@77 2825 int len = 7;
Chris@77 2826
Chris@77 2827 if (harmonic % 2 == 0) {
Chris@77 2828 if (harmonic % 4 == 0) {
Chris@77 2829 len = 12;
Chris@77 2830 } else {
Chris@77 2831 len = 10;
Chris@77 2832 }
Chris@77 2833 }
Chris@77 2834
Chris@77 2835 paint.drawLine(cursorPos.x() - len,
Chris@907 2836 hy,
Chris@77 2837 cursorPos.x(),
Chris@907 2838 hy);
Chris@77 2839
Chris@77 2840 ++harmonic;
Chris@77 2841 }
Chris@77 2842
Chris@77 2843 paint.restore();
Chris@77 2844 }
Chris@77 2845
Chris@25 2846 QString
Chris@918 2847 SpectrogramLayer::getFeatureDescription(LayerGeometryProvider *v, QPoint &pos) const
Chris@25 2848 {
Chris@25 2849 int x = pos.x();
Chris@25 2850 int y = pos.y();
Chris@0 2851
Chris@25 2852 if (!m_model || !m_model->isOK()) return "";
Chris@0 2853
Chris@905 2854 double magMin = 0, magMax = 0;
Chris@905 2855 double phaseMin = 0, phaseMax = 0;
Chris@905 2856 double freqMin = 0, freqMax = 0;
Chris@905 2857 double adjFreqMin = 0, adjFreqMax = 0;
Chris@25 2858 QString pitchMin, pitchMax;
Chris@0 2859 RealTime rtMin, rtMax;
Chris@0 2860
Chris@38 2861 bool haveValues = false;
Chris@0 2862
Chris@44 2863 if (!getXBinSourceRange(v, x, rtMin, rtMax)) {
Chris@38 2864 return "";
Chris@38 2865 }
Chris@44 2866 if (getXYBinSourceRange(v, x, y, magMin, magMax, phaseMin, phaseMax)) {
Chris@38 2867 haveValues = true;
Chris@38 2868 }
Chris@0 2869
Chris@35 2870 QString adjFreqText = "", adjPitchText = "";
Chris@35 2871
Chris@38 2872 if (m_binDisplay == PeakFrequencies) {
Chris@35 2873
Chris@44 2874 if (!getAdjustedYBinSourceRange(v, x, y, freqMin, freqMax,
Chris@38 2875 adjFreqMin, adjFreqMax)) {
Chris@38 2876 return "";
Chris@38 2877 }
Chris@35 2878
Chris@35 2879 if (adjFreqMin != adjFreqMax) {
Chris@65 2880 adjFreqText = tr("Peak Frequency:\t%1 - %2 Hz\n")
Chris@35 2881 .arg(adjFreqMin).arg(adjFreqMax);
Chris@35 2882 } else {
Chris@65 2883 adjFreqText = tr("Peak Frequency:\t%1 Hz\n")
Chris@35 2884 .arg(adjFreqMin);
Chris@38 2885 }
Chris@38 2886
Chris@38 2887 QString pmin = Pitch::getPitchLabelForFrequency(adjFreqMin);
Chris@38 2888 QString pmax = Pitch::getPitchLabelForFrequency(adjFreqMax);
Chris@38 2889
Chris@38 2890 if (pmin != pmax) {
Chris@65 2891 adjPitchText = tr("Peak Pitch:\t%3 - %4\n").arg(pmin).arg(pmax);
Chris@38 2892 } else {
Chris@65 2893 adjPitchText = tr("Peak Pitch:\t%2\n").arg(pmin);
Chris@35 2894 }
Chris@35 2895
Chris@35 2896 } else {
Chris@35 2897
Chris@44 2898 if (!getYBinSourceRange(v, y, freqMin, freqMax)) return "";
Chris@35 2899 }
Chris@35 2900
Chris@25 2901 QString text;
Chris@25 2902
Chris@25 2903 if (rtMin != rtMax) {
Chris@25 2904 text += tr("Time:\t%1 - %2\n")
Chris@25 2905 .arg(rtMin.toText(true).c_str())
Chris@25 2906 .arg(rtMax.toText(true).c_str());
Chris@25 2907 } else {
Chris@25 2908 text += tr("Time:\t%1\n")
Chris@25 2909 .arg(rtMin.toText(true).c_str());
Chris@0 2910 }
Chris@0 2911
Chris@25 2912 if (freqMin != freqMax) {
Chris@65 2913 text += tr("%1Bin Frequency:\t%2 - %3 Hz\n%4Bin Pitch:\t%5 - %6\n")
Chris@65 2914 .arg(adjFreqText)
Chris@25 2915 .arg(freqMin)
Chris@25 2916 .arg(freqMax)
Chris@65 2917 .arg(adjPitchText)
Chris@65 2918 .arg(Pitch::getPitchLabelForFrequency(freqMin))
Chris@65 2919 .arg(Pitch::getPitchLabelForFrequency(freqMax));
Chris@65 2920 } else {
Chris@65 2921 text += tr("%1Bin Frequency:\t%2 Hz\n%3Bin Pitch:\t%4\n")
Chris@35 2922 .arg(adjFreqText)
Chris@25 2923 .arg(freqMin)
Chris@65 2924 .arg(adjPitchText)
Chris@65 2925 .arg(Pitch::getPitchLabelForFrequency(freqMin));
Chris@25 2926 }
Chris@25 2927
Chris@38 2928 if (haveValues) {
Chris@905 2929 double dbMin = AudioLevel::multiplier_to_dB(magMin);
Chris@905 2930 double dbMax = AudioLevel::multiplier_to_dB(magMax);
Chris@43 2931 QString dbMinString;
Chris@43 2932 QString dbMaxString;
Chris@43 2933 if (dbMin == AudioLevel::DB_FLOOR) {
Chris@43 2934 dbMinString = tr("-Inf");
Chris@43 2935 } else {
Chris@907 2936 dbMinString = QString("%1").arg(lrint(dbMin));
Chris@43 2937 }
Chris@43 2938 if (dbMax == AudioLevel::DB_FLOOR) {
Chris@43 2939 dbMaxString = tr("-Inf");
Chris@43 2940 } else {
Chris@907 2941 dbMaxString = QString("%1").arg(lrint(dbMax));
Chris@43 2942 }
Chris@907 2943 if (lrint(dbMin) != lrint(dbMax)) {
Chris@199 2944 text += tr("dB:\t%1 - %2").arg(dbMinString).arg(dbMaxString);
Chris@25 2945 } else {
Chris@199 2946 text += tr("dB:\t%1").arg(dbMinString);
Chris@25 2947 }
Chris@38 2948 if (phaseMin != phaseMax) {
Chris@38 2949 text += tr("\nPhase:\t%1 - %2").arg(phaseMin).arg(phaseMax);
Chris@38 2950 } else {
Chris@38 2951 text += tr("\nPhase:\t%1").arg(phaseMin);
Chris@38 2952 }
Chris@25 2953 }
Chris@25 2954
Chris@25 2955 return text;
Chris@0 2956 }
Chris@25 2957
Chris@0 2958 int
Chris@40 2959 SpectrogramLayer::getColourScaleWidth(QPainter &paint) const
Chris@40 2960 {
Chris@40 2961 int cw;
Chris@40 2962
Chris@119 2963 cw = paint.fontMetrics().width("-80dB");
Chris@119 2964
Chris@40 2965 return cw;
Chris@40 2966 }
Chris@40 2967
Chris@40 2968 int
Chris@918 2969 SpectrogramLayer::getVerticalScaleWidth(LayerGeometryProvider *, bool detailed, QPainter &paint) const
Chris@0 2970 {
Chris@0 2971 if (!m_model || !m_model->isOK()) return 0;
Chris@0 2972
Chris@607 2973 int cw = 0;
Chris@607 2974 if (detailed) cw = getColourScaleWidth(paint);
Chris@40 2975
Chris@0 2976 int tw = paint.fontMetrics().width(QString("%1")
Chris@0 2977 .arg(m_maxFrequency > 0 ?
Chris@0 2978 m_maxFrequency - 1 :
Chris@0 2979 m_model->getSampleRate() / 2));
Chris@0 2980
Chris@234 2981 int fw = paint.fontMetrics().width(tr("43Hz"));
Chris@0 2982 if (tw < fw) tw = fw;
Chris@40 2983
Chris@40 2984 int tickw = (m_frequencyScale == LogFrequencyScale ? 10 : 4);
Chris@0 2985
Chris@40 2986 return cw + tickw + tw + 13;
Chris@0 2987 }
Chris@0 2988
Chris@0 2989 void
Chris@918 2990 SpectrogramLayer::paintVerticalScale(LayerGeometryProvider *v, bool detailed, QPainter &paint, QRect rect) const
Chris@0 2991 {
Chris@0 2992 if (!m_model || !m_model->isOK()) {
Chris@0 2993 return;
Chris@0 2994 }
Chris@0 2995
Chris@382 2996 Profiler profiler("SpectrogramLayer::paintVerticalScale");
Chris@122 2997
Chris@120 2998 //!!! cache this?
Chris@120 2999
Chris@0 3000 int h = rect.height(), w = rect.width();
Chris@0 3001
Chris@40 3002 int tickw = (m_frequencyScale == LogFrequencyScale ? 10 : 4);
Chris@40 3003 int pkw = (m_frequencyScale == LogFrequencyScale ? 10 : 0);
Chris@40 3004
Chris@1087 3005 int bins = getFFTSize() / 2;
Chris@907 3006 sv_samplerate_t sr = m_model->getSampleRate();
Chris@0 3007
Chris@0 3008 if (m_maxFrequency > 0) {
Chris@1087 3009 bins = int((double(m_maxFrequency) * getFFTSize()) / sr + 0.1);
Chris@1087 3010 if (bins > getFFTSize() / 2) bins = getFFTSize() / 2;
Chris@0 3011 }
Chris@0 3012
Chris@607 3013 int cw = 0;
Chris@607 3014
Chris@607 3015 if (detailed) cw = getColourScaleWidth(paint);
Chris@119 3016 int cbw = paint.fontMetrics().width("dB");
Chris@40 3017
Chris@0 3018 int py = -1;
Chris@0 3019 int textHeight = paint.fontMetrics().height();
Chris@0 3020 int toff = -textHeight + paint.fontMetrics().ascent() + 2;
Chris@0 3021
Chris@607 3022 if (detailed && (h > textHeight * 3 + 10)) {
Chris@119 3023
Chris@119 3024 int topLines = 2;
Chris@119 3025 if (m_colourScale == PhaseColourScale) topLines = 1;
Chris@119 3026
Chris@119 3027 int ch = h - textHeight * (topLines + 1) - 8;
Chris@119 3028 // paint.drawRect(4, textHeight + 4, cw - 1, ch + 1);
Chris@119 3029 paint.drawRect(4 + cw - cbw, textHeight * topLines + 4, cbw - 1, ch + 1);
Chris@40 3030
Chris@40 3031 QString top, bottom;
Chris@1030 3032 double min = m_viewMags[v->getId()].getMin();
Chris@1030 3033 double max = m_viewMags[v->getId()].getMax();
Chris@905 3034
Chris@905 3035 double dBmin = AudioLevel::multiplier_to_dB(min);
Chris@905 3036 double dBmax = AudioLevel::multiplier_to_dB(max);
Chris@119 3037
Chris@1044 3038 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@1044 3039 cerr << "paintVerticalScale: for view id " << v->getId()
Chris@1044 3040 << ": min = " << min << ", max = " << max
Chris@1044 3041 << ", dBmin = " << dBmin << ", dBmax = " << dBmax << endl;
Chris@1044 3042 #endif
Chris@1044 3043
Chris@120 3044 if (dBmax < -60.f) dBmax = -60.f;
Chris@907 3045 else top = QString("%1").arg(lrint(dBmax));
Chris@120 3046
Chris@120 3047 if (dBmin < dBmax - 60.f) dBmin = dBmax - 60.f;
Chris@907 3048 bottom = QString("%1").arg(lrint(dBmin));
Chris@119 3049
Chris@119 3050 //!!! & phase etc
Chris@119 3051
Chris@119 3052 if (m_colourScale != PhaseColourScale) {
Chris@119 3053 paint.drawText((cw + 6 - paint.fontMetrics().width("dBFS")) / 2,
Chris@119 3054 2 + textHeight + toff, "dBFS");
Chris@119 3055 }
Chris@119 3056
Chris@119 3057 // paint.drawText((cw + 6 - paint.fontMetrics().width(top)) / 2,
Chris@119 3058 paint.drawText(3 + cw - cbw - paint.fontMetrics().width(top),
Chris@119 3059 2 + textHeight * topLines + toff + textHeight/2, top);
Chris@119 3060
Chris@119 3061 paint.drawText(3 + cw - cbw - paint.fontMetrics().width(bottom),
Chris@119 3062 h + toff - 3 - textHeight/2, bottom);
Chris@40 3063
Chris@40 3064 paint.save();
Chris@40 3065 paint.setBrush(Qt::NoBrush);
Chris@119 3066
Chris@119 3067 int lasty = 0;
Chris@119 3068 int lastdb = 0;
Chris@119 3069
Chris@40 3070 for (int i = 0; i < ch; ++i) {
Chris@119 3071
Chris@905 3072 double dBval = dBmin + (((dBmax - dBmin) * i) / (ch - 1));
Chris@119 3073 int idb = int(dBval);
Chris@119 3074
Chris@905 3075 double value = AudioLevel::dB_to_multiplier(dBval);
Chris@119 3076 int colour = getDisplayValue(v, value * m_gain);
Chris@210 3077
Chris@907 3078 paint.setPen(m_palette.getColour((unsigned char)colour));
Chris@119 3079
Chris@119 3080 int y = textHeight * topLines + 4 + ch - i;
Chris@119 3081
Chris@119 3082 paint.drawLine(5 + cw - cbw, y, cw + 2, y);
Chris@119 3083
Chris@119 3084 if (i == 0) {
Chris@119 3085 lasty = y;
Chris@119 3086 lastdb = idb;
Chris@119 3087 } else if (i < ch - paint.fontMetrics().ascent() &&
Chris@120 3088 idb != lastdb &&
Chris@119 3089 ((abs(y - lasty) > textHeight &&
Chris@119 3090 idb % 10 == 0) ||
Chris@119 3091 (abs(y - lasty) > paint.fontMetrics().ascent() &&
Chris@119 3092 idb % 5 == 0))) {
Chris@287 3093 paint.setPen(v->getBackground());
Chris@119 3094 QString text = QString("%1").arg(idb);
Chris@119 3095 paint.drawText(3 + cw - cbw - paint.fontMetrics().width(text),
Chris@119 3096 y + toff + textHeight/2, text);
Chris@287 3097 paint.setPen(v->getForeground());
Chris@119 3098 paint.drawLine(5 + cw - cbw, y, 8 + cw - cbw, y);
Chris@119 3099 lasty = y;
Chris@119 3100 lastdb = idb;
Chris@119 3101 }
Chris@40 3102 }
Chris@40 3103 paint.restore();
Chris@40 3104 }
Chris@40 3105
Chris@40 3106 paint.drawLine(cw + 7, 0, cw + 7, h);
Chris@40 3107
Chris@0 3108 int bin = -1;
Chris@0 3109
Chris@918 3110 for (int y = 0; y < v->getPaintHeight(); ++y) {
Chris@0 3111
Chris@905 3112 double q0, q1;
Chris@918 3113 if (!getYBinRange(v, v->getPaintHeight() - y, q0, q1)) continue;
Chris@0 3114
Chris@0 3115 int vy;
Chris@0 3116
Chris@0 3117 if (int(q0) > bin) {
Chris@0 3118 vy = y;
Chris@0 3119 bin = int(q0);
Chris@0 3120 } else {
Chris@0 3121 continue;
Chris@0 3122 }
Chris@0 3123
Chris@1087 3124 int freq = int((sr * bin) / getFFTSize());
Chris@0 3125
Chris@0 3126 if (py >= 0 && (vy - py) < textHeight - 1) {
Chris@40 3127 if (m_frequencyScale == LinearFrequencyScale) {
Chris@40 3128 paint.drawLine(w - tickw, h - vy, w, h - vy);
Chris@40 3129 }
Chris@0 3130 continue;
Chris@0 3131 }
Chris@0 3132
Chris@0 3133 QString text = QString("%1").arg(freq);
Chris@234 3134 if (bin == 1) text = tr("%1Hz").arg(freq); // bin 0 is DC
Chris@40 3135 paint.drawLine(cw + 7, h - vy, w - pkw - 1, h - vy);
Chris@0 3136
Chris@0 3137 if (h - vy - textHeight >= -2) {
Chris@1025 3138 int tx = w - 3 - paint.fontMetrics().width(text) - max(tickw, pkw);
Chris@0 3139 paint.drawText(tx, h - vy + toff, text);
Chris@0 3140 }
Chris@0 3141
Chris@0 3142 py = vy;
Chris@0 3143 }
Chris@40 3144
Chris@40 3145 if (m_frequencyScale == LogFrequencyScale) {
Chris@40 3146
Chris@277 3147 // piano keyboard
Chris@277 3148
Chris@690 3149 PianoScale().paintPianoVertical
Chris@690 3150 (v, paint, QRect(w - pkw - 1, 0, pkw, h),
Chris@690 3151 getEffectiveMinFrequency(), getEffectiveMaxFrequency());
Chris@40 3152 }
Chris@608 3153
Chris@608 3154 m_haveDetailedScale = detailed;
Chris@0 3155 }
Chris@0 3156
Chris@187 3157 class SpectrogramRangeMapper : public RangeMapper
Chris@187 3158 {
Chris@187 3159 public:
Chris@901 3160 SpectrogramRangeMapper(sv_samplerate_t sr, int /* fftsize */) :
Chris@901 3161 m_dist(sr / 2),
Chris@901 3162 m_s2(sqrt(sqrt(2))) { }
Chris@187 3163 ~SpectrogramRangeMapper() { }
Chris@187 3164
Chris@901 3165 virtual int getPositionForValue(double value) const {
Chris@901 3166
Chris@901 3167 double dist = m_dist;
Chris@187 3168
Chris@187 3169 int n = 0;
Chris@187 3170
Chris@901 3171 while (dist > (value + 0.00001) && dist > 0.1) {
Chris@187 3172 dist /= m_s2;
Chris@187 3173 ++n;
Chris@187 3174 }
Chris@187 3175
Chris@187 3176 return n;
Chris@187 3177 }
Chris@724 3178
Chris@901 3179 virtual int getPositionForValueUnclamped(double value) const {
Chris@724 3180 // We don't really support this
Chris@724 3181 return getPositionForValue(value);
Chris@724 3182 }
Chris@187 3183
Chris@901 3184 virtual double getValueForPosition(int position) const {
Chris@187 3185
Chris@187 3186 // Vertical zoom step 0 shows the entire range from DC ->
Chris@187 3187 // Nyquist frequency. Step 1 shows 2^(1/4) of the range of
Chris@187 3188 // step 0, and so on until the visible range is smaller than
Chris@187 3189 // the frequency step between bins at the current fft size.
Chris@187 3190
Chris@901 3191 double dist = m_dist;
Chris@187 3192
Chris@187 3193 int n = 0;
Chris@187 3194 while (n < position) {
Chris@187 3195 dist /= m_s2;
Chris@187 3196 ++n;
Chris@187 3197 }
Chris@187 3198
Chris@187 3199 return dist;
Chris@187 3200 }
Chris@187 3201
Chris@901 3202 virtual double getValueForPositionUnclamped(int position) const {
Chris@724 3203 // We don't really support this
Chris@724 3204 return getValueForPosition(position);
Chris@724 3205 }
Chris@724 3206
Chris@187 3207 virtual QString getUnit() const { return "Hz"; }
Chris@187 3208
Chris@187 3209 protected:
Chris@901 3210 double m_dist;
Chris@901 3211 double m_s2;
Chris@187 3212 };
Chris@187 3213
Chris@133 3214 int
Chris@133 3215 SpectrogramLayer::getVerticalZoomSteps(int &defaultStep) const
Chris@133 3216 {
Chris@135 3217 if (!m_model) return 0;
Chris@187 3218
Chris@907 3219 sv_samplerate_t sr = m_model->getSampleRate();
Chris@187 3220
Chris@1087 3221 SpectrogramRangeMapper mapper(sr, getFFTSize());
Chris@1087 3222
Chris@1087 3223 // int maxStep = mapper.getPositionForValue((double(sr) / getFFTSize()) + 0.001);
Chris@187 3224 int maxStep = mapper.getPositionForValue(0);
Chris@905 3225 int minStep = mapper.getPositionForValue(double(sr) / 2);
Chris@250 3226
Chris@805 3227 int initialMax = m_initialMaxFrequency;
Chris@907 3228 if (initialMax == 0) initialMax = int(sr / 2);
Chris@250 3229
Chris@250 3230 defaultStep = mapper.getPositionForValue(initialMax) - minStep;
Chris@250 3231
Chris@587 3232 // SVDEBUG << "SpectrogramLayer::getVerticalZoomSteps: " << maxStep - minStep << " (" << maxStep <<"-" << minStep << "), default is " << defaultStep << " (from initial max freq " << initialMax << ")" << endl;
Chris@187 3233
Chris@187 3234 return maxStep - minStep;
Chris@133 3235 }
Chris@133 3236
Chris@133 3237 int
Chris@133 3238 SpectrogramLayer::getCurrentVerticalZoomStep() const
Chris@133 3239 {
Chris@133 3240 if (!m_model) return 0;
Chris@133 3241
Chris@905 3242 double dmin, dmax;
Chris@133 3243 getDisplayExtents(dmin, dmax);
Chris@133 3244
Chris@1087 3245 SpectrogramRangeMapper mapper(m_model->getSampleRate(), getFFTSize());
Chris@187 3246 int n = mapper.getPositionForValue(dmax - dmin);
Chris@587 3247 // SVDEBUG << "SpectrogramLayer::getCurrentVerticalZoomStep: " << n << endl;
Chris@133 3248 return n;
Chris@133 3249 }
Chris@133 3250
Chris@133 3251 void
Chris@133 3252 SpectrogramLayer::setVerticalZoomStep(int step)
Chris@133 3253 {
Chris@187 3254 if (!m_model) return;
Chris@187 3255
Chris@905 3256 double dmin = m_minFrequency, dmax = m_maxFrequency;
Chris@253 3257 // getDisplayExtents(dmin, dmax);
Chris@253 3258
Chris@682 3259 // cerr << "current range " << dmin << " -> " << dmax << ", range " << dmax-dmin << ", mid " << (dmax + dmin)/2 << endl;
Chris@133 3260
Chris@907 3261 sv_samplerate_t sr = m_model->getSampleRate();
Chris@1087 3262 SpectrogramRangeMapper mapper(sr, getFFTSize());
Chris@905 3263 double newdist = mapper.getValueForPosition(step);
Chris@905 3264
Chris@905 3265 double newmin, newmax;
Chris@253 3266
Chris@253 3267 if (m_frequencyScale == LogFrequencyScale) {
Chris@253 3268
Chris@253 3269 // need to pick newmin and newmax such that
Chris@253 3270 //
Chris@253 3271 // (log(newmin) + log(newmax)) / 2 == logmid
Chris@253 3272 // and
Chris@253 3273 // newmax - newmin = newdist
Chris@253 3274 //
Chris@253 3275 // so log(newmax - newdist) + log(newmax) == 2logmid
Chris@253 3276 // log(newmax(newmax - newdist)) == 2logmid
Chris@253 3277 // newmax.newmax - newmax.newdist == exp(2logmid)
Chris@253 3278 // newmax^2 + (-newdist)newmax + -exp(2logmid) == 0
Chris@253 3279 // quadratic with a = 1, b = -newdist, c = -exp(2logmid), all known
Chris@253 3280 //
Chris@253 3281 // positive root
Chris@253 3282 // newmax = (newdist + sqrt(newdist^2 + 4exp(2logmid))) / 2
Chris@253 3283 //
Chris@253 3284 // but logmid = (log(dmin) + log(dmax)) / 2
Chris@253 3285 // so exp(2logmid) = exp(log(dmin) + log(dmax))
Chris@253 3286 // = exp(log(dmin.dmax))
Chris@253 3287 // = dmin.dmax
Chris@253 3288 // so newmax = (newdist + sqrtf(newdist^2 + 4dmin.dmax)) / 2
Chris@253 3289
Chris@907 3290 newmax = (newdist + sqrt(newdist*newdist + 4*dmin*dmax)) / 2;
Chris@253 3291 newmin = newmax - newdist;
Chris@253 3292
Chris@682 3293 // cerr << "newmin = " << newmin << ", newmax = " << newmax << endl;
Chris@253 3294
Chris@253 3295 } else {
Chris@905 3296 double dmid = (dmax + dmin) / 2;
Chris@253 3297 newmin = dmid - newdist / 2;
Chris@253 3298 newmax = dmid + newdist / 2;
Chris@253 3299 }
Chris@187 3300
Chris@905 3301 double mmin, mmax;
Chris@187 3302 mmin = 0;
Chris@905 3303 mmax = double(sr) / 2;
Chris@133 3304
Chris@187 3305 if (newmin < mmin) {
Chris@187 3306 newmax += (mmin - newmin);
Chris@187 3307 newmin = mmin;
Chris@187 3308 }
Chris@187 3309 if (newmax > mmax) {
Chris@187 3310 newmax = mmax;
Chris@187 3311 }
Chris@133 3312
Chris@587 3313 // SVDEBUG << "SpectrogramLayer::setVerticalZoomStep: " << step << ": " << newmin << " -> " << newmax << " (range " << newdist << ")" << endl;
Chris@253 3314
Chris@907 3315 setMinFrequency(int(lrint(newmin)));
Chris@907 3316 setMaxFrequency(int(lrint(newmax)));
Chris@187 3317 }
Chris@187 3318
Chris@187 3319 RangeMapper *
Chris@187 3320 SpectrogramLayer::getNewVerticalZoomRangeMapper() const
Chris@187 3321 {
Chris@187 3322 if (!m_model) return 0;
Chris@1087 3323 return new SpectrogramRangeMapper(m_model->getSampleRate(), getFFTSize());
Chris@133 3324 }
Chris@133 3325
Chris@273 3326 void
Chris@918 3327 SpectrogramLayer::updateMeasureRectYCoords(LayerGeometryProvider *v, const MeasureRect &r) const
Chris@273 3328 {
Chris@273 3329 int y0 = 0;
Chris@907 3330 if (r.startY > 0.0) y0 = int(getYForFrequency(v, r.startY));
Chris@273 3331
Chris@273 3332 int y1 = y0;
Chris@907 3333 if (r.endY > 0.0) y1 = int(getYForFrequency(v, r.endY));
Chris@273 3334
Chris@587 3335 // SVDEBUG << "SpectrogramLayer::updateMeasureRectYCoords: start " << r.startY << " -> " << y0 << ", end " << r.endY << " -> " << y1 << endl;
Chris@273 3336
Chris@273 3337 r.pixrect = QRect(r.pixrect.x(), y0, r.pixrect.width(), y1 - y0);
Chris@273 3338 }
Chris@273 3339
Chris@273 3340 void
Chris@918 3341 SpectrogramLayer::setMeasureRectYCoord(LayerGeometryProvider *v, MeasureRect &r, bool start, int y) const
Chris@273 3342 {
Chris@273 3343 if (start) {
Chris@273 3344 r.startY = getFrequencyForY(v, y);
Chris@273 3345 r.endY = r.startY;
Chris@273 3346 } else {
Chris@273 3347 r.endY = getFrequencyForY(v, y);
Chris@273 3348 }
Chris@587 3349 // SVDEBUG << "SpectrogramLayer::setMeasureRectYCoord: start " << r.startY << " <- " << y << ", end " << r.endY << " <- " << y << endl;
Chris@273 3350
Chris@273 3351 }
Chris@273 3352
Chris@316 3353 void
Chris@316 3354 SpectrogramLayer::toXml(QTextStream &stream,
Chris@316 3355 QString indent, QString extraAttributes) const
Chris@6 3356 {
Chris@6 3357 QString s;
Chris@6 3358
Chris@6 3359 s += QString("channel=\"%1\" "
Chris@6 3360 "windowSize=\"%2\" "
Chris@153 3361 "windowHopLevel=\"%3\" "
Chris@153 3362 "gain=\"%4\" "
Chris@153 3363 "threshold=\"%5\" ")
Chris@6 3364 .arg(m_channel)
Chris@6 3365 .arg(m_windowSize)
Chris@97 3366 .arg(m_windowHopLevel)
Chris@37 3367 .arg(m_gain)
Chris@37 3368 .arg(m_threshold);
Chris@37 3369
Chris@37 3370 s += QString("minFrequency=\"%1\" "
Chris@37 3371 "maxFrequency=\"%2\" "
Chris@37 3372 "colourScale=\"%3\" "
Chris@37 3373 "colourScheme=\"%4\" "
Chris@37 3374 "colourRotation=\"%5\" "
Chris@37 3375 "frequencyScale=\"%6\" "
Chris@761 3376 "binDisplay=\"%7\" ")
Chris@37 3377 .arg(m_minFrequency)
Chris@6 3378 .arg(m_maxFrequency)
Chris@6 3379 .arg(m_colourScale)
Chris@197 3380 .arg(m_colourMap)
Chris@37 3381 .arg(m_colourRotation)
Chris@35 3382 .arg(m_frequencyScale)
Chris@761 3383 .arg(m_binDisplay);
Chris@761 3384
Chris@1009 3385 // New-style normalization attributes, allowing for more types of
Chris@1009 3386 // normalization in future: write out the column normalization
Chris@1009 3387 // type separately, and then whether we are normalizing visible
Chris@1009 3388 // area as well afterwards
Chris@1009 3389
Chris@1009 3390 s += QString("columnNormalization=\"%1\" ")
Chris@1063 3391 .arg(m_normalization == ColumnOp::NormalizeColumns ? "peak" :
Chris@1063 3392 m_normalization == ColumnOp::NormalizeHybrid ? "hybrid" : "none");
Chris@1009 3393
Chris@1009 3394 // Old-style normalization attribute. We *don't* write out
Chris@1009 3395 // normalizeHybrid here because the only release that would accept
Chris@1009 3396 // it (Tony v1.0) has a totally different scale factor for
Chris@1009 3397 // it. We'll just have to accept that session files from Tony
Chris@1009 3398 // v2.0+ will look odd in Tony v1.0
Chris@1009 3399
Chris@1009 3400 s += QString("normalizeColumns=\"%1\" ")
Chris@1063 3401 .arg(m_normalization == ColumnOp::NormalizeColumns ? "true" : "false");
Chris@1009 3402
Chris@1009 3403 // And this applies to both old- and new-style attributes
Chris@1009 3404
Chris@1009 3405 s += QString("normalizeVisibleArea=\"%1\" ")
Chris@1063 3406 .arg(m_normalization == ColumnOp::NormalizeVisibleArea ? "true" : "false");
Chris@1009 3407
Chris@316 3408 Layer::toXml(stream, indent, extraAttributes + " " + s);
Chris@6 3409 }
Chris@6 3410
Chris@11 3411 void
Chris@11 3412 SpectrogramLayer::setProperties(const QXmlAttributes &attributes)
Chris@11 3413 {
Chris@11 3414 bool ok = false;
Chris@11 3415
Chris@11 3416 int channel = attributes.value("channel").toInt(&ok);
Chris@11 3417 if (ok) setChannel(channel);
Chris@11 3418
Chris@805 3419 int windowSize = attributes.value("windowSize").toUInt(&ok);
Chris@11 3420 if (ok) setWindowSize(windowSize);
Chris@11 3421
Chris@805 3422 int windowHopLevel = attributes.value("windowHopLevel").toUInt(&ok);
Chris@97 3423 if (ok) setWindowHopLevel(windowHopLevel);
Chris@97 3424 else {
Chris@805 3425 int windowOverlap = attributes.value("windowOverlap").toUInt(&ok);
Chris@97 3426 // a percentage value
Chris@97 3427 if (ok) {
Chris@97 3428 if (windowOverlap == 0) setWindowHopLevel(0);
Chris@97 3429 else if (windowOverlap == 25) setWindowHopLevel(1);
Chris@97 3430 else if (windowOverlap == 50) setWindowHopLevel(2);
Chris@97 3431 else if (windowOverlap == 75) setWindowHopLevel(3);
Chris@97 3432 else if (windowOverlap == 90) setWindowHopLevel(4);
Chris@97 3433 }
Chris@97 3434 }
Chris@11 3435
Chris@11 3436 float gain = attributes.value("gain").toFloat(&ok);
Chris@11 3437 if (ok) setGain(gain);
Chris@11 3438
Chris@37 3439 float threshold = attributes.value("threshold").toFloat(&ok);
Chris@37 3440 if (ok) setThreshold(threshold);
Chris@37 3441
Chris@805 3442 int minFrequency = attributes.value("minFrequency").toUInt(&ok);
Chris@187 3443 if (ok) {
Chris@587 3444 SVDEBUG << "SpectrogramLayer::setProperties: setting min freq to " << minFrequency << endl;
Chris@187 3445 setMinFrequency(minFrequency);
Chris@187 3446 }
Chris@37 3447
Chris@805 3448 int maxFrequency = attributes.value("maxFrequency").toUInt(&ok);
Chris@187 3449 if (ok) {
Chris@587 3450 SVDEBUG << "SpectrogramLayer::setProperties: setting max freq to " << maxFrequency << endl;
Chris@187 3451 setMaxFrequency(maxFrequency);
Chris@187 3452 }
Chris@11 3453
Chris@11 3454 ColourScale colourScale = (ColourScale)
Chris@11 3455 attributes.value("colourScale").toInt(&ok);
Chris@11 3456 if (ok) setColourScale(colourScale);
Chris@11 3457
Chris@197 3458 int colourMap = attributes.value("colourScheme").toInt(&ok);
Chris@197 3459 if (ok) setColourMap(colourMap);
Chris@11 3460
Chris@37 3461 int colourRotation = attributes.value("colourRotation").toInt(&ok);
Chris@37 3462 if (ok) setColourRotation(colourRotation);
Chris@37 3463
Chris@11 3464 FrequencyScale frequencyScale = (FrequencyScale)
Chris@11 3465 attributes.value("frequencyScale").toInt(&ok);
Chris@11 3466 if (ok) setFrequencyScale(frequencyScale);
Chris@35 3467
Chris@37 3468 BinDisplay binDisplay = (BinDisplay)
Chris@37 3469 attributes.value("binDisplay").toInt(&ok);
Chris@37 3470 if (ok) setBinDisplay(binDisplay);
Chris@36 3471
Chris@1009 3472 bool haveNewStyleNormalization = false;
Chris@1009 3473
Chris@1009 3474 QString columnNormalization = attributes.value("columnNormalization");
Chris@1009 3475
Chris@1009 3476 if (columnNormalization != "") {
Chris@1009 3477
Chris@1009 3478 haveNewStyleNormalization = true;
Chris@1009 3479
Chris@1009 3480 if (columnNormalization == "peak") {
Chris@1063 3481 setNormalization(ColumnOp::NormalizeColumns);
Chris@1009 3482 } else if (columnNormalization == "hybrid") {
Chris@1063 3483 setNormalization(ColumnOp::NormalizeHybrid);
Chris@1009 3484 } else if (columnNormalization == "none") {
Chris@1009 3485 // do nothing
Chris@1009 3486 } else {
Chris@1009 3487 cerr << "NOTE: Unknown or unsupported columnNormalization attribute \""
Chris@1009 3488 << columnNormalization << "\"" << endl;
Chris@1009 3489 }
Chris@1009 3490 }
Chris@1009 3491
Chris@1009 3492 if (!haveNewStyleNormalization) {
Chris@1009 3493
Chris@1009 3494 bool normalizeColumns =
Chris@1009 3495 (attributes.value("normalizeColumns").trimmed() == "true");
Chris@1009 3496 if (normalizeColumns) {
Chris@1063 3497 setNormalization(ColumnOp::NormalizeColumns);
Chris@1009 3498 }
Chris@1009 3499
Chris@1009 3500 bool normalizeHybrid =
Chris@1009 3501 (attributes.value("normalizeHybrid").trimmed() == "true");
Chris@1009 3502 if (normalizeHybrid) {
Chris@1063 3503 setNormalization(ColumnOp::NormalizeHybrid);
Chris@1009 3504 }
Chris@862 3505 }
Chris@153 3506
Chris@153 3507 bool normalizeVisibleArea =
Chris@153 3508 (attributes.value("normalizeVisibleArea").trimmed() == "true");
Chris@862 3509 if (normalizeVisibleArea) {
Chris@1063 3510 setNormalization(ColumnOp::NormalizeVisibleArea);
Chris@862 3511 }
Chris@761 3512
Chris@1063 3513 if (!haveNewStyleNormalization && m_normalization == ColumnOp::NormalizeHybrid) {
Chris@1009 3514 // Tony v1.0 is (and hopefully will remain!) the only released
Chris@1009 3515 // SV-a-like to use old-style attributes when saving sessions
Chris@1009 3516 // that ask for hybrid normalization. It saves them with the
Chris@1009 3517 // wrong gain factor, so hack in a fix for that here -- this
Chris@1009 3518 // gives us backward but not forward compatibility.
Chris@1087 3519 setGain(m_gain / float(getFFTSize() / 2));
Chris@862 3520 }
Chris@11 3521 }
Chris@11 3522