annotate layer/SpectrogramLayer.cpp @ 1089:c8683d94442a spectrogram-minor-refactor

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