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

More layer updates
author Chris Cannam
date Mon, 01 Jul 2019 14:17:13 +0100
parents 66bf1abfefc1
children dbff4b290bf0
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@1147 27 #include "base/Strings.h"
Chris@1212 28 #include "base/StorageAdviser.h"
Chris@1212 29 #include "base/Exceptions.h"
Chris@376 30 #include "widgets/CommandHistory.h"
Chris@1078 31 #include "data/model/Dense3DModelPeakCache.h"
Chris@1078 32
Chris@376 33 #include "ColourMapper.h"
Chris@690 34 #include "PianoScale.h"
Chris@1078 35 #include "PaintAssistant.h"
Chris@1089 36 #include "Colour3DPlotRenderer.h"
Chris@0 37
Chris@0 38 #include <QPainter>
Chris@0 39 #include <QImage>
Chris@0 40 #include <QPixmap>
Chris@0 41 #include <QRect>
Chris@92 42 #include <QApplication>
Chris@178 43 #include <QMessageBox>
Chris@283 44 #include <QMouseEvent>
Chris@316 45 #include <QTextStream>
Chris@1017 46 #include <QSettings>
Chris@0 47
Chris@0 48 #include <iostream>
Chris@0 49
Chris@0 50 #include <cassert>
Chris@0 51 #include <cmath>
Chris@0 52
Chris@1143 53 //#define DEBUG_SPECTROGRAM 1
Chris@1143 54 //#define DEBUG_SPECTROGRAM_REPAINT 1
Chris@1025 55
Chris@1025 56 using namespace std;
Chris@907 57
Chris@44 58 SpectrogramLayer::SpectrogramLayer(Configuration config) :
Chris@1408 59 m_model(nullptr),
Chris@0 60 m_channel(0),
Chris@0 61 m_windowSize(1024),
Chris@0 62 m_windowType(HanningWindow),
Chris@97 63 m_windowHopLevel(2),
Chris@1379 64 m_oversampling(1),
Chris@0 65 m_gain(1.0),
Chris@215 66 m_initialGain(1.0),
Chris@1128 67 m_threshold(1.0e-8f),
Chris@1128 68 m_initialThreshold(1.0e-8f),
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@1105 74 m_colourScale(ColourScaleType::Log),
Chris@1137 75 m_colourScaleMultiple(1.0),
Chris@197 76 m_colourMap(0),
Chris@1362 77 m_colourInverted(false),
Chris@1103 78 m_binScale(BinScale::Linear),
Chris@1103 79 m_binDisplay(BinDisplay::AllBins),
Chris@1104 80 m_normalization(ColumnNormalization::None),
Chris@1104 81 m_normalizeVisibleArea(false),
Chris@133 82 m_lastEmittedZoomStep(-1),
Chris@390 83 m_synchronous(false),
Chris@608 84 m_haveDetailedScale(false),
Chris@193 85 m_exiting(false),
Chris@1408 86 m_fftModel(nullptr),
Chris@1408 87 m_wholeCache(nullptr),
Chris@1408 88 m_peakCache(nullptr),
Chris@1088 89 m_peakCacheDivisor(8)
Chris@0 90 {
Chris@1017 91 QString colourConfigName = "spectrogram-colour";
Chris@1017 92 int colourConfigDefault = int(ColourMapper::Green);
Chris@1017 93
Chris@215 94 if (config == FullRangeDb) {
Chris@215 95 m_initialMaxFrequency = 0;
Chris@215 96 setMaxFrequency(0);
Chris@215 97 } else if (config == MelodicRange) {
Chris@1234 98 setWindowSize(8192);
Chris@1234 99 setWindowHopLevel(4);
Chris@215 100 m_initialMaxFrequency = 1500;
Chris@1234 101 setMaxFrequency(1500);
Chris@215 102 setMinFrequency(40);
Chris@1234 103 setColourScale(ColourScaleType::Linear);
Chris@215 104 setColourMap(ColourMapper::Sunset);
Chris@1103 105 setBinScale(BinScale::Log);
Chris@1017 106 colourConfigName = "spectrogram-melodic-colour";
Chris@1017 107 colourConfigDefault = int(ColourMapper::Sunset);
Chris@224 108 // setGain(20);
Chris@37 109 } else if (config == MelodicPeaks) {
Chris@1234 110 setWindowSize(4096);
Chris@1234 111 setWindowHopLevel(5);
Chris@135 112 m_initialMaxFrequency = 2000;
Chris@1234 113 setMaxFrequency(2000);
Chris@1234 114 setMinFrequency(40);
Chris@1234 115 setBinScale(BinScale::Log);
Chris@1234 116 setColourScale(ColourScaleType::Linear);
Chris@1234 117 setBinDisplay(BinDisplay::PeakFrequencies);
Chris@1104 118 setNormalization(ColumnNormalization::Max1);
Chris@1017 119 colourConfigName = "spectrogram-melodic-colour";
Chris@1017 120 colourConfigDefault = int(ColourMapper::Sunset);
Chris@0 121 }
Chris@110 122
Chris@1017 123 QSettings settings;
Chris@1017 124 settings.beginGroup("Preferences");
Chris@1017 125 setColourMap(settings.value(colourConfigName, colourConfigDefault).toInt());
Chris@1017 126 settings.endGroup();
Chris@1017 127
Chris@122 128 Preferences *prefs = Preferences::getInstance();
Chris@122 129 connect(prefs, SIGNAL(propertyChanged(PropertyContainer::PropertyName)),
Chris@122 130 this, SLOT(preferenceChanged(PropertyContainer::PropertyName)));
Chris@122 131 setWindowType(prefs->getWindowType());
Chris@0 132 }
Chris@0 133
Chris@0 134 SpectrogramLayer::~SpectrogramLayer()
Chris@0 135 {
Chris@1106 136 invalidateRenderers();
Chris@1242 137 deleteDerivedModels();
Chris@1242 138 }
Chris@1242 139
Chris@1242 140 void
Chris@1242 141 SpectrogramLayer::deleteDerivedModels()
Chris@1242 142 {
Chris@1242 143 if (m_fftModel) m_fftModel->aboutToDelete();
Chris@1242 144 if (m_peakCache) m_peakCache->aboutToDelete();
Chris@1242 145 if (m_wholeCache) m_wholeCache->aboutToDelete();
Chris@1211 146
Chris@1211 147 delete m_fftModel;
Chris@1211 148 delete m_peakCache;
Chris@1212 149 delete m_wholeCache;
Chris@1242 150
Chris@1408 151 m_fftModel = nullptr;
Chris@1408 152 m_peakCache = nullptr;
Chris@1408 153 m_wholeCache = nullptr;
Chris@0 154 }
Chris@0 155
Chris@1137 156 pair<ColourScaleType, double>
Chris@1104 157 SpectrogramLayer::convertToColourScale(int value)
Chris@1104 158 {
Chris@1104 159 switch (value) {
Chris@1137 160 case 0: return { ColourScaleType::Linear, 1.0 };
Chris@1137 161 case 1: return { ColourScaleType::Meter, 1.0 };
Chris@1137 162 case 2: return { ColourScaleType::Log, 2.0 }; // dB^2 (i.e. log of power)
Chris@1137 163 case 3: return { ColourScaleType::Log, 1.0 }; // dB (of magnitude)
Chris@1137 164 case 4: return { ColourScaleType::Phase, 1.0 };
Chris@1137 165 default: return { ColourScaleType::Linear, 1.0 };
Chris@1104 166 }
Chris@1104 167 }
Chris@1104 168
Chris@1104 169 int
Chris@1137 170 SpectrogramLayer::convertFromColourScale(ColourScaleType scale, double multiple)
Chris@1104 171 {
Chris@1104 172 switch (scale) {
Chris@1105 173 case ColourScaleType::Linear: return 0;
Chris@1105 174 case ColourScaleType::Meter: return 1;
Chris@1137 175 case ColourScaleType::Log: return (multiple > 1.5 ? 2 : 3);
Chris@1105 176 case ColourScaleType::Phase: return 4;
Chris@1105 177 case ColourScaleType::PlusMinusOne:
Chris@1105 178 case ColourScaleType::Absolute:
Chris@1104 179 default: return 0;
Chris@1104 180 }
Chris@1104 181 }
Chris@1104 182
Chris@1104 183 std::pair<ColumnNormalization, bool>
Chris@1104 184 SpectrogramLayer::convertToColumnNorm(int value)
Chris@1104 185 {
Chris@1104 186 switch (value) {
Chris@1104 187 default:
Chris@1104 188 case 0: return { ColumnNormalization::None, false };
Chris@1104 189 case 1: return { ColumnNormalization::Max1, false };
Chris@1104 190 case 2: return { ColumnNormalization::None, true }; // visible area
Chris@1104 191 case 3: return { ColumnNormalization::Hybrid, false };
Chris@1104 192 }
Chris@1104 193 }
Chris@1104 194
Chris@1104 195 int
Chris@1104 196 SpectrogramLayer::convertFromColumnNorm(ColumnNormalization norm, bool visible)
Chris@1104 197 {
Chris@1104 198 if (visible) return 2;
Chris@1104 199 switch (norm) {
Chris@1104 200 case ColumnNormalization::None: return 0;
Chris@1104 201 case ColumnNormalization::Max1: return 1;
Chris@1104 202 case ColumnNormalization::Hybrid: return 3;
Chris@1104 203
Chris@1104 204 case ColumnNormalization::Sum1:
Chris@1251 205 case ColumnNormalization::Range01:
Chris@1104 206 default: return 0;
Chris@1104 207 }
Chris@1104 208 }
Chris@1104 209
Chris@0 210 void
Chris@1471 211 SpectrogramLayer::setModel(ModelId modelId)
Chris@0 212 {
Chris@1471 213 auto newModel = ModelById::getAs<DenseTimeValueModel>(modelId);
Chris@1471 214 if (!modelId.isNone() && !newModel) {
Chris@1471 215 throw std::logic_error("Not a DenseTimeValueModel");
Chris@1471 216 }
Chris@1471 217
Chris@1471 218 if (modelId == m_model) return;
Chris@1471 219 m_model = modelId;
Chris@1471 220
Chris@1471 221 if (newModel) {
Chris@1471 222 recreateFFTModel();
Chris@1471 223
Chris@1471 224 connectSignals(m_model);
Chris@1471 225
Chris@1471 226 connect(newModel.get(), SIGNAL(modelChanged()),
Chris@1471 227 this, SLOT(cacheInvalid()));
Chris@1471 228 connect(newModel.get(), SIGNAL(modelChangedWithin(sv_frame_t, sv_frame_t)),
Chris@1471 229 this, SLOT(cacheInvalid(sv_frame_t, sv_frame_t)));
Chris@1471 230 }
Chris@1471 231
Chris@0 232 emit modelReplaced();
Chris@110 233 }
Chris@115 234
Chris@0 235 Layer::PropertyList
Chris@0 236 SpectrogramLayer::getProperties() const
Chris@0 237 {
Chris@0 238 PropertyList list;
Chris@87 239 list.push_back("Colour");
Chris@87 240 list.push_back("Colour Scale");
Chris@87 241 list.push_back("Window Size");
Chris@97 242 list.push_back("Window Increment");
Chris@1379 243 list.push_back("Oversampling");
Chris@862 244 list.push_back("Normalization");
Chris@87 245 list.push_back("Bin Display");
Chris@87 246 list.push_back("Threshold");
Chris@87 247 list.push_back("Gain");
Chris@87 248 list.push_back("Colour Rotation");
Chris@153 249 // list.push_back("Min Frequency");
Chris@153 250 // list.push_back("Max Frequency");
Chris@87 251 list.push_back("Frequency Scale");
Chris@0 252 return list;
Chris@0 253 }
Chris@0 254
Chris@87 255 QString
Chris@87 256 SpectrogramLayer::getPropertyLabel(const PropertyName &name) const
Chris@87 257 {
Chris@87 258 if (name == "Colour") return tr("Colour");
Chris@87 259 if (name == "Colour Scale") return tr("Colour Scale");
Chris@87 260 if (name == "Window Size") return tr("Window Size");
Chris@112 261 if (name == "Window Increment") return tr("Window Overlap");
Chris@1379 262 if (name == "Oversampling") return tr("Oversampling");
Chris@862 263 if (name == "Normalization") return tr("Normalization");
Chris@87 264 if (name == "Bin Display") return tr("Bin Display");
Chris@87 265 if (name == "Threshold") return tr("Threshold");
Chris@87 266 if (name == "Gain") return tr("Gain");
Chris@87 267 if (name == "Colour Rotation") return tr("Colour Rotation");
Chris@87 268 if (name == "Min Frequency") return tr("Min Frequency");
Chris@87 269 if (name == "Max Frequency") return tr("Max Frequency");
Chris@87 270 if (name == "Frequency Scale") return tr("Frequency Scale");
Chris@87 271 return "";
Chris@87 272 }
Chris@87 273
Chris@335 274 QString
Chris@862 275 SpectrogramLayer::getPropertyIconName(const PropertyName &) const
Chris@335 276 {
Chris@335 277 return "";
Chris@335 278 }
Chris@335 279
Chris@0 280 Layer::PropertyType
Chris@0 281 SpectrogramLayer::getPropertyType(const PropertyName &name) const
Chris@0 282 {
Chris@87 283 if (name == "Gain") return RangeProperty;
Chris@87 284 if (name == "Colour Rotation") return RangeProperty;
Chris@87 285 if (name == "Threshold") return RangeProperty;
Chris@1198 286 if (name == "Colour") return ColourMapProperty;
Chris@0 287 return ValueProperty;
Chris@0 288 }
Chris@0 289
Chris@0 290 QString
Chris@0 291 SpectrogramLayer::getPropertyGroupName(const PropertyName &name) const
Chris@0 292 {
Chris@153 293 if (name == "Bin Display" ||
Chris@153 294 name == "Frequency Scale") return tr("Bins");
Chris@87 295 if (name == "Window Size" ||
Chris@1379 296 name == "Window Increment" ||
Chris@1379 297 name == "Oversampling") return tr("Window");
Chris@87 298 if (name == "Colour" ||
Chris@1234 299 name == "Threshold" ||
Chris@1234 300 name == "Colour Rotation") return tr("Colour");
Chris@862 301 if (name == "Normalization" ||
Chris@153 302 name == "Gain" ||
Chris@1234 303 name == "Colour Scale") return tr("Scale");
Chris@0 304 return QString();
Chris@0 305 }
Chris@0 306
Chris@0 307 int
Chris@0 308 SpectrogramLayer::getPropertyRangeAndValue(const PropertyName &name,
Chris@1234 309 int *min, int *max, int *deflt) const
Chris@0 310 {
Chris@216 311 int val = 0;
Chris@216 312
Chris@216 313 int garbage0, garbage1, garbage2;
Chris@55 314 if (!min) min = &garbage0;
Chris@55 315 if (!max) max = &garbage1;
Chris@216 316 if (!deflt) deflt = &garbage2;
Chris@10 317
Chris@87 318 if (name == "Gain") {
Chris@0 319
Chris@1234 320 *min = -50;
Chris@1234 321 *max = 50;
Chris@0 322
Chris@906 323 *deflt = int(lrint(log10(m_initialGain) * 20.0));
Chris@1234 324 if (*deflt < *min) *deflt = *min;
Chris@1234 325 if (*deflt > *max) *deflt = *max;
Chris@1234 326
Chris@1234 327 val = int(lrint(log10(m_gain) * 20.0));
Chris@1234 328 if (val < *min) val = *min;
Chris@1234 329 if (val > *max) val = *max;
Chris@0 330
Chris@87 331 } else if (name == "Threshold") {
Chris@37 332
Chris@1234 333 *min = -81;
Chris@1234 334 *max = -1;
Chris@37 335
Chris@906 336 *deflt = int(lrint(AudioLevel::multiplier_to_dB(m_initialThreshold)));
Chris@1234 337 if (*deflt < *min) *deflt = *min;
Chris@1234 338 if (*deflt > *max) *deflt = *max;
Chris@1234 339
Chris@1234 340 val = int(lrint(AudioLevel::multiplier_to_dB(m_threshold)));
Chris@1234 341 if (val < *min) val = *min;
Chris@1234 342 if (val > *max) val = *max;
Chris@37 343
Chris@87 344 } else if (name == "Colour Rotation") {
Chris@9 345
Chris@1234 346 *min = 0;
Chris@1234 347 *max = 256;
Chris@216 348 *deflt = m_initialRotation;
Chris@216 349
Chris@1234 350 val = m_colourRotation;
Chris@9 351
Chris@87 352 } else if (name == "Colour Scale") {
Chris@0 353
Chris@1099 354 // linear, meter, db^2, db, phase
Chris@1234 355 *min = 0;
Chris@1234 356 *max = 4;
Chris@1092 357 *deflt = 2;
Chris@216 358
Chris@1234 359 val = convertFromColourScale(m_colourScale, m_colourScaleMultiple);
Chris@0 360
Chris@87 361 } else if (name == "Colour") {
Chris@0 362
Chris@1234 363 *min = 0;
Chris@1234 364 *max = ColourMapper::getColourMapCount() - 1;
Chris@216 365 *deflt = 0;
Chris@216 366
Chris@1234 367 val = m_colourMap;
Chris@0 368
Chris@87 369 } else if (name == "Window Size") {
Chris@0 370
Chris@1234 371 *min = 0;
Chris@1234 372 *max = 10;
Chris@216 373 *deflt = 5;
Chris@1234 374
Chris@1234 375 val = 0;
Chris@1234 376 int ws = m_windowSize;
Chris@1234 377 while (ws > 32) { ws >>= 1; val ++; }
Chris@0 378
Chris@97 379 } else if (name == "Window Increment") {
Chris@1234 380
Chris@1234 381 *min = 0;
Chris@1234 382 *max = 5;
Chris@216 383 *deflt = 2;
Chris@216 384
Chris@216 385 val = m_windowHopLevel;
Chris@1379 386
Chris@1379 387 } else if (name == "Oversampling") {
Chris@1379 388
Chris@1379 389 *min = 0;
Chris@1379 390 *max = 3;
Chris@1379 391 *deflt = 0;
Chris@1379 392
Chris@1379 393 val = 0;
Chris@1379 394 int ov = m_oversampling;
Chris@1379 395 while (ov > 1) { ov >>= 1; val ++; }
Chris@1379 396
Chris@87 397 } else if (name == "Min Frequency") {
Chris@37 398
Chris@1234 399 *min = 0;
Chris@1234 400 *max = 9;
Chris@216 401 *deflt = 1;
Chris@37 402
Chris@1234 403 switch (m_minFrequency) {
Chris@1234 404 case 0: default: val = 0; break;
Chris@1234 405 case 10: val = 1; break;
Chris@1234 406 case 20: val = 2; break;
Chris@1234 407 case 40: val = 3; break;
Chris@1234 408 case 100: val = 4; break;
Chris@1234 409 case 250: val = 5; break;
Chris@1234 410 case 500: val = 6; break;
Chris@1234 411 case 1000: val = 7; break;
Chris@1234 412 case 4000: val = 8; break;
Chris@1234 413 case 10000: val = 9; break;
Chris@1234 414 }
Chris@37 415
Chris@87 416 } else if (name == "Max Frequency") {
Chris@0 417
Chris@1234 418 *min = 0;
Chris@1234 419 *max = 9;
Chris@216 420 *deflt = 6;
Chris@0 421
Chris@1234 422 switch (m_maxFrequency) {
Chris@1234 423 case 500: val = 0; break;
Chris@1234 424 case 1000: val = 1; break;
Chris@1234 425 case 1500: val = 2; break;
Chris@1234 426 case 2000: val = 3; break;
Chris@1234 427 case 4000: val = 4; break;
Chris@1234 428 case 6000: val = 5; break;
Chris@1234 429 case 8000: val = 6; break;
Chris@1234 430 case 12000: val = 7; break;
Chris@1234 431 case 16000: val = 8; break;
Chris@1234 432 default: val = 9; break;
Chris@1234 433 }
Chris@0 434
Chris@87 435 } else if (name == "Frequency Scale") {
Chris@0 436
Chris@1234 437 *min = 0;
Chris@1234 438 *max = 1;
Chris@1103 439 *deflt = int(BinScale::Linear);
Chris@1234 440 val = (int)m_binScale;
Chris@0 441
Chris@87 442 } else if (name == "Bin Display") {
Chris@35 443
Chris@1234 444 *min = 0;
Chris@1234 445 *max = 2;
Chris@1103 446 *deflt = int(BinDisplay::AllBins);
Chris@1234 447 val = (int)m_binDisplay;
Chris@35 448
Chris@862 449 } else if (name == "Normalization") {
Chris@1234 450
Chris@862 451 *min = 0;
Chris@862 452 *max = 3;
Chris@1104 453 *deflt = 0;
Chris@1104 454
Chris@1104 455 val = convertFromColumnNorm(m_normalization, m_normalizeVisibleArea);
Chris@120 456
Chris@0 457 } else {
Chris@1234 458 val = Layer::getPropertyRangeAndValue(name, min, max, deflt);
Chris@0 459 }
Chris@0 460
Chris@216 461 return val;
Chris@0 462 }
Chris@0 463
Chris@0 464 QString
Chris@0 465 SpectrogramLayer::getPropertyValueLabel(const PropertyName &name,
Chris@1234 466 int value) const
Chris@0 467 {
Chris@87 468 if (name == "Colour") {
Chris@1362 469 return ColourMapper::getColourMapLabel(value);
Chris@0 470 }
Chris@87 471 if (name == "Colour Scale") {
Chris@1234 472 switch (value) {
Chris@1234 473 default:
Chris@1234 474 case 0: return tr("Linear");
Chris@1234 475 case 1: return tr("Meter");
Chris@1234 476 case 2: return tr("dBV^2");
Chris@1234 477 case 3: return tr("dBV");
Chris@1234 478 case 4: return tr("Phase");
Chris@1234 479 }
Chris@0 480 }
Chris@862 481 if (name == "Normalization") {
Chris@1209 482 switch(value) {
Chris@1209 483 default:
Chris@1209 484 case 0: return tr("None");
Chris@1209 485 case 1: return tr("Col");
Chris@1209 486 case 2: return tr("View");
Chris@1209 487 case 3: return tr("Hybrid");
Chris@1209 488 }
Chris@1209 489 // return ""; // icon only
Chris@862 490 }
Chris@87 491 if (name == "Window Size") {
Chris@1234 492 return QString("%1").arg(32 << value);
Chris@0 493 }
Chris@97 494 if (name == "Window Increment") {
Chris@1234 495 switch (value) {
Chris@1234 496 default:
Chris@1234 497 case 0: return tr("None");
Chris@1234 498 case 1: return tr("25 %");
Chris@1234 499 case 2: return tr("50 %");
Chris@1234 500 case 3: return tr("75 %");
Chris@1234 501 case 4: return tr("87.5 %");
Chris@1234 502 case 5: return tr("93.75 %");
Chris@1234 503 }
Chris@0 504 }
Chris@1379 505 if (name == "Oversampling") {
Chris@1379 506 switch (value) {
Chris@1379 507 default:
Chris@1379 508 case 0: return tr("1x");
Chris@1379 509 case 1: return tr("2x");
Chris@1379 510 case 2: return tr("4x");
Chris@1379 511 case 3: return tr("8x");
Chris@1379 512 }
Chris@1379 513 }
Chris@87 514 if (name == "Min Frequency") {
Chris@1234 515 switch (value) {
Chris@1234 516 default:
Chris@1234 517 case 0: return tr("No min");
Chris@1234 518 case 1: return tr("10 Hz");
Chris@1234 519 case 2: return tr("20 Hz");
Chris@1234 520 case 3: return tr("40 Hz");
Chris@1234 521 case 4: return tr("100 Hz");
Chris@1234 522 case 5: return tr("250 Hz");
Chris@1234 523 case 6: return tr("500 Hz");
Chris@1234 524 case 7: return tr("1 KHz");
Chris@1234 525 case 8: return tr("4 KHz");
Chris@1234 526 case 9: return tr("10 KHz");
Chris@1234 527 }
Chris@37 528 }
Chris@87 529 if (name == "Max Frequency") {
Chris@1234 530 switch (value) {
Chris@1234 531 default:
Chris@1234 532 case 0: return tr("500 Hz");
Chris@1234 533 case 1: return tr("1 KHz");
Chris@1234 534 case 2: return tr("1.5 KHz");
Chris@1234 535 case 3: return tr("2 KHz");
Chris@1234 536 case 4: return tr("4 KHz");
Chris@1234 537 case 5: return tr("6 KHz");
Chris@1234 538 case 6: return tr("8 KHz");
Chris@1234 539 case 7: return tr("12 KHz");
Chris@1234 540 case 8: return tr("16 KHz");
Chris@1234 541 case 9: return tr("No max");
Chris@1234 542 }
Chris@0 543 }
Chris@87 544 if (name == "Frequency Scale") {
Chris@1234 545 switch (value) {
Chris@1234 546 default:
Chris@1234 547 case 0: return tr("Linear");
Chris@1234 548 case 1: return tr("Log");
Chris@1234 549 }
Chris@0 550 }
Chris@87 551 if (name == "Bin Display") {
Chris@1234 552 switch (value) {
Chris@1234 553 default:
Chris@1234 554 case 0: return tr("All Bins");
Chris@1234 555 case 1: return tr("Peak Bins");
Chris@1234 556 case 2: return tr("Frequencies");
Chris@1234 557 }
Chris@35 558 }
Chris@0 559 return tr("<unknown>");
Chris@0 560 }
Chris@0 561
Chris@862 562 QString
Chris@862 563 SpectrogramLayer::getPropertyValueIconName(const PropertyName &name,
Chris@862 564 int value) const
Chris@862 565 {
Chris@862 566 if (name == "Normalization") {
Chris@862 567 switch(value) {
Chris@862 568 default:
Chris@862 569 case 0: return "normalise-none";
Chris@862 570 case 1: return "normalise-columns";
Chris@862 571 case 2: return "normalise";
Chris@862 572 case 3: return "normalise-hybrid";
Chris@862 573 }
Chris@862 574 }
Chris@862 575 return "";
Chris@862 576 }
Chris@862 577
Chris@167 578 RangeMapper *
Chris@167 579 SpectrogramLayer::getNewPropertyRangeMapper(const PropertyName &name) const
Chris@167 580 {
Chris@167 581 if (name == "Gain") {
Chris@167 582 return new LinearRangeMapper(-50, 50, -25, 25, tr("dB"));
Chris@167 583 }
Chris@167 584 if (name == "Threshold") {
Chris@1147 585 return new LinearRangeMapper(-81, -1, -81, -1, tr("dB"), false,
Chris@1147 586 { { -81, Strings::minus_infinity } });
Chris@167 587 }
Chris@1408 588 return nullptr;
Chris@167 589 }
Chris@167 590
Chris@0 591 void
Chris@0 592 SpectrogramLayer::setProperty(const PropertyName &name, int value)
Chris@0 593 {
Chris@87 594 if (name == "Gain") {
Chris@1234 595 setGain(float(pow(10, float(value)/20.0)));
Chris@87 596 } else if (name == "Threshold") {
Chris@1234 597 if (value == -81) setThreshold(0.0);
Chris@1234 598 else setThreshold(float(AudioLevel::dB_to_multiplier(value)));
Chris@87 599 } else if (name == "Colour Rotation") {
Chris@1234 600 setColourRotation(value);
Chris@87 601 } else if (name == "Colour") {
Chris@197 602 setColourMap(value);
Chris@87 603 } else if (name == "Window Size") {
Chris@1234 604 setWindowSize(32 << value);
Chris@97 605 } else if (name == "Window Increment") {
Chris@97 606 setWindowHopLevel(value);
Chris@1379 607 } else if (name == "Oversampling") {
Chris@1379 608 setOversampling(1 << value);
Chris@87 609 } else if (name == "Min Frequency") {
Chris@1234 610 switch (value) {
Chris@1234 611 default:
Chris@1234 612 case 0: setMinFrequency(0); break;
Chris@1234 613 case 1: setMinFrequency(10); break;
Chris@1234 614 case 2: setMinFrequency(20); break;
Chris@1234 615 case 3: setMinFrequency(40); break;
Chris@1234 616 case 4: setMinFrequency(100); break;
Chris@1234 617 case 5: setMinFrequency(250); break;
Chris@1234 618 case 6: setMinFrequency(500); break;
Chris@1234 619 case 7: setMinFrequency(1000); break;
Chris@1234 620 case 8: setMinFrequency(4000); break;
Chris@1234 621 case 9: setMinFrequency(10000); break;
Chris@1234 622 }
Chris@133 623 int vs = getCurrentVerticalZoomStep();
Chris@133 624 if (vs != m_lastEmittedZoomStep) {
Chris@133 625 emit verticalZoomChanged();
Chris@133 626 m_lastEmittedZoomStep = vs;
Chris@133 627 }
Chris@87 628 } else if (name == "Max Frequency") {
Chris@1234 629 switch (value) {
Chris@1234 630 case 0: setMaxFrequency(500); break;
Chris@1234 631 case 1: setMaxFrequency(1000); break;
Chris@1234 632 case 2: setMaxFrequency(1500); break;
Chris@1234 633 case 3: setMaxFrequency(2000); break;
Chris@1234 634 case 4: setMaxFrequency(4000); break;
Chris@1234 635 case 5: setMaxFrequency(6000); break;
Chris@1234 636 case 6: setMaxFrequency(8000); break;
Chris@1234 637 case 7: setMaxFrequency(12000); break;
Chris@1234 638 case 8: setMaxFrequency(16000); break;
Chris@1234 639 default:
Chris@1234 640 case 9: setMaxFrequency(0); break;
Chris@1234 641 }
Chris@133 642 int vs = getCurrentVerticalZoomStep();
Chris@133 643 if (vs != m_lastEmittedZoomStep) {
Chris@133 644 emit verticalZoomChanged();
Chris@133 645 m_lastEmittedZoomStep = vs;
Chris@133 646 }
Chris@87 647 } else if (name == "Colour Scale") {
Chris@1137 648 setColourScaleMultiple(1.0);
Chris@1234 649 switch (value) {
Chris@1234 650 default:
Chris@1234 651 case 0: setColourScale(ColourScaleType::Linear); break;
Chris@1234 652 case 1: setColourScale(ColourScaleType::Meter); break;
Chris@1234 653 case 2:
Chris@1137 654 setColourScale(ColourScaleType::Log);
Chris@1137 655 setColourScaleMultiple(2.0);
Chris@1137 656 break;
Chris@1234 657 case 3: setColourScale(ColourScaleType::Log); break;
Chris@1234 658 case 4: setColourScale(ColourScaleType::Phase); break;
Chris@1234 659 }
Chris@87 660 } else if (name == "Frequency Scale") {
Chris@1234 661 switch (value) {
Chris@1234 662 default:
Chris@1234 663 case 0: setBinScale(BinScale::Linear); break;
Chris@1234 664 case 1: setBinScale(BinScale::Log); break;
Chris@1234 665 }
Chris@87 666 } else if (name == "Bin Display") {
Chris@1234 667 switch (value) {
Chris@1234 668 default:
Chris@1234 669 case 0: setBinDisplay(BinDisplay::AllBins); break;
Chris@1234 670 case 1: setBinDisplay(BinDisplay::PeakBins); break;
Chris@1234 671 case 2: setBinDisplay(BinDisplay::PeakFrequencies); break;
Chris@1234 672 }
Chris@862 673 } else if (name == "Normalization") {
Chris@1104 674 auto n = convertToColumnNorm(value);
Chris@1104 675 setNormalization(n.first);
Chris@1104 676 setNormalizeVisibleArea(n.second);
Chris@0 677 }
Chris@0 678 }
Chris@0 679
Chris@0 680 void
Chris@1106 681 SpectrogramLayer::invalidateRenderers()
Chris@95 682 {
Chris@1044 683 #ifdef DEBUG_SPECTROGRAM
Chris@1106 684 cerr << "SpectrogramLayer::invalidateRenderers called" << endl;
Chris@1044 685 #endif
Chris@1106 686
Chris@1089 687 for (ViewRendererMap::iterator i = m_renderers.begin();
Chris@1089 688 i != m_renderers.end(); ++i) {
Chris@1089 689 delete i->second;
Chris@1089 690 }
Chris@1089 691 m_renderers.clear();
Chris@95 692 }
Chris@95 693
Chris@95 694 void
Chris@122 695 SpectrogramLayer::preferenceChanged(PropertyContainer::PropertyName name)
Chris@122 696 {
Chris@587 697 SVDEBUG << "SpectrogramLayer::preferenceChanged(" << name << ")" << endl;
Chris@122 698
Chris@122 699 if (name == "Window Type") {
Chris@122 700 setWindowType(Preferences::getInstance()->getWindowType());
Chris@122 701 return;
Chris@122 702 }
Chris@490 703 if (name == "Spectrogram Y Smoothing") {
Chris@1106 704 invalidateRenderers();
Chris@490 705 invalidateMagnitudes();
Chris@490 706 emit layerParametersChanged();
Chris@490 707 }
Chris@490 708 if (name == "Spectrogram X Smoothing") {
Chris@1106 709 invalidateRenderers();
Chris@122 710 invalidateMagnitudes();
Chris@122 711 emit layerParametersChanged();
Chris@122 712 }
Chris@122 713 if (name == "Tuning Frequency") {
Chris@122 714 emit layerParametersChanged();
Chris@122 715 }
Chris@122 716 }
Chris@122 717
Chris@122 718 void
Chris@0 719 SpectrogramLayer::setChannel(int ch)
Chris@0 720 {
Chris@0 721 if (m_channel == ch) return;
Chris@0 722
Chris@1106 723 invalidateRenderers();
Chris@0 724 m_channel = ch;
Chris@1211 725 recreateFFTModel();
Chris@9 726
Chris@0 727 emit layerParametersChanged();
Chris@0 728 }
Chris@0 729
Chris@0 730 int
Chris@0 731 SpectrogramLayer::getChannel() const
Chris@0 732 {
Chris@0 733 return m_channel;
Chris@0 734 }
Chris@0 735
Chris@1086 736 int
Chris@1379 737 SpectrogramLayer::getFFTSize() const
Chris@1086 738 {
Chris@1379 739 return m_windowSize * m_oversampling;
Chris@1379 740 }
Chris@1379 741
Chris@1379 742 void
Chris@1379 743 SpectrogramLayer::setWindowSize(int ws)
Chris@1379 744 {
Chris@1379 745 if (m_windowSize == ws) return;
Chris@1379 746 invalidateRenderers();
Chris@1379 747 m_windowSize = ws;
Chris@1379 748 recreateFFTModel();
Chris@1379 749 emit layerParametersChanged();
Chris@1379 750 }
Chris@1379 751
Chris@1379 752 int
Chris@1379 753 SpectrogramLayer::getWindowSize() const
Chris@1379 754 {
Chris@1379 755 return m_windowSize;
Chris@1379 756 }
Chris@1379 757
Chris@1379 758 void
Chris@1379 759 SpectrogramLayer::setWindowHopLevel(int v)
Chris@1379 760 {
Chris@1379 761 if (m_windowHopLevel == v) return;
Chris@1379 762 invalidateRenderers();
Chris@1379 763 m_windowHopLevel = v;
Chris@1379 764 recreateFFTModel();
Chris@1379 765 emit layerParametersChanged();
Chris@1379 766 }
Chris@1379 767
Chris@1379 768 int
Chris@1379 769 SpectrogramLayer::getWindowHopLevel() const
Chris@1379 770 {
Chris@1379 771 return m_windowHopLevel;
Chris@1379 772 }
Chris@1379 773
Chris@1379 774 void
Chris@1379 775 SpectrogramLayer::setOversampling(int oversampling)
Chris@1379 776 {
Chris@1379 777 if (m_oversampling == oversampling) return;
Chris@1379 778 invalidateRenderers();
Chris@1379 779 m_oversampling = oversampling;
Chris@1379 780 recreateFFTModel();
Chris@1379 781 emit layerParametersChanged();
Chris@1379 782 }
Chris@1379 783
Chris@1379 784 int
Chris@1379 785 SpectrogramLayer::getOversampling() const
Chris@1379 786 {
Chris@1379 787 return m_oversampling;
Chris@0 788 }
Chris@0 789
Chris@0 790 void
Chris@0 791 SpectrogramLayer::setWindowType(WindowType w)
Chris@0 792 {
Chris@0 793 if (m_windowType == w) return;
Chris@0 794
Chris@1106 795 invalidateRenderers();
Chris@0 796
Chris@0 797 m_windowType = w;
Chris@110 798
Chris@1211 799 recreateFFTModel();
Chris@9 800
Chris@9 801 emit layerParametersChanged();
Chris@0 802 }
Chris@0 803
Chris@0 804 WindowType
Chris@0 805 SpectrogramLayer::getWindowType() const
Chris@0 806 {
Chris@0 807 return m_windowType;
Chris@0 808 }
Chris@0 809
Chris@0 810 void
Chris@0 811 SpectrogramLayer::setGain(float gain)
Chris@0 812 {
Chris@587 813 // SVDEBUG << "SpectrogramLayer::setGain(" << gain << ") (my gain is now "
Chris@1234 814 // << m_gain << ")" << endl;
Chris@55 815
Chris@40 816 if (m_gain == gain) return;
Chris@0 817
Chris@1106 818 invalidateRenderers();
Chris@0 819
Chris@0 820 m_gain = gain;
Chris@0 821
Chris@9 822 emit layerParametersChanged();
Chris@0 823 }
Chris@0 824
Chris@0 825 float
Chris@0 826 SpectrogramLayer::getGain() const
Chris@0 827 {
Chris@0 828 return m_gain;
Chris@0 829 }
Chris@0 830
Chris@0 831 void
Chris@37 832 SpectrogramLayer::setThreshold(float threshold)
Chris@37 833 {
Chris@40 834 if (m_threshold == threshold) return;
Chris@37 835
Chris@1106 836 invalidateRenderers();
Chris@37 837
Chris@37 838 m_threshold = threshold;
Chris@37 839
Chris@37 840 emit layerParametersChanged();
Chris@37 841 }
Chris@37 842
Chris@37 843 float
Chris@37 844 SpectrogramLayer::getThreshold() const
Chris@37 845 {
Chris@37 846 return m_threshold;
Chris@37 847 }
Chris@37 848
Chris@37 849 void
Chris@805 850 SpectrogramLayer::setMinFrequency(int mf)
Chris@37 851 {
Chris@37 852 if (m_minFrequency == mf) return;
Chris@37 853
Chris@587 854 // SVDEBUG << "SpectrogramLayer::setMinFrequency: " << mf << endl;
Chris@187 855
Chris@1106 856 invalidateRenderers();
Chris@119 857 invalidateMagnitudes();
Chris@37 858
Chris@37 859 m_minFrequency = mf;
Chris@37 860
Chris@37 861 emit layerParametersChanged();
Chris@37 862 }
Chris@37 863
Chris@805 864 int
Chris@37 865 SpectrogramLayer::getMinFrequency() const
Chris@37 866 {
Chris@37 867 return m_minFrequency;
Chris@37 868 }
Chris@37 869
Chris@37 870 void
Chris@805 871 SpectrogramLayer::setMaxFrequency(int mf)
Chris@0 872 {
Chris@0 873 if (m_maxFrequency == mf) return;
Chris@0 874
Chris@587 875 // SVDEBUG << "SpectrogramLayer::setMaxFrequency: " << mf << endl;
Chris@187 876
Chris@1106 877 invalidateRenderers();
Chris@119 878 invalidateMagnitudes();
Chris@0 879
Chris@0 880 m_maxFrequency = mf;
Chris@0 881
Chris@9 882 emit layerParametersChanged();
Chris@0 883 }
Chris@0 884
Chris@805 885 int
Chris@0 886 SpectrogramLayer::getMaxFrequency() const
Chris@0 887 {
Chris@0 888 return m_maxFrequency;
Chris@0 889 }
Chris@0 890
Chris@0 891 void
Chris@9 892 SpectrogramLayer::setColourRotation(int r)
Chris@9 893 {
Chris@9 894 if (r < 0) r = 0;
Chris@9 895 if (r > 256) r = 256;
Chris@9 896 int distance = r - m_colourRotation;
Chris@9 897
Chris@9 898 if (distance != 0) {
Chris@1234 899 m_colourRotation = r;
Chris@9 900 }
Chris@1141 901
Chris@1141 902 // Initially the idea with colour rotation was that we would just
Chris@1141 903 // rotate the palette of an already-generated cache. That's not
Chris@1141 904 // really practical now that cacheing is handled in a separate
Chris@1141 905 // class in which the main cache no longer has a palette.
Chris@1141 906 invalidateRenderers();
Chris@1112 907
Chris@9 908 emit layerParametersChanged();
Chris@9 909 }
Chris@9 910
Chris@9 911 void
Chris@1105 912 SpectrogramLayer::setColourScale(ColourScaleType colourScale)
Chris@0 913 {
Chris@0 914 if (m_colourScale == colourScale) return;
Chris@0 915
Chris@1106 916 invalidateRenderers();
Chris@0 917
Chris@0 918 m_colourScale = colourScale;
Chris@0 919
Chris@9 920 emit layerParametersChanged();
Chris@0 921 }
Chris@0 922
Chris@1105 923 ColourScaleType
Chris@0 924 SpectrogramLayer::getColourScale() const
Chris@0 925 {
Chris@0 926 return m_colourScale;
Chris@0 927 }
Chris@0 928
Chris@0 929 void
Chris@1137 930 SpectrogramLayer::setColourScaleMultiple(double multiple)
Chris@1137 931 {
Chris@1137 932 if (m_colourScaleMultiple == multiple) return;
Chris@1137 933
Chris@1137 934 invalidateRenderers();
Chris@1137 935
Chris@1137 936 m_colourScaleMultiple = multiple;
Chris@1137 937
Chris@1137 938 emit layerParametersChanged();
Chris@1137 939 }
Chris@1137 940
Chris@1137 941 double
Chris@1137 942 SpectrogramLayer::getColourScaleMultiple() const
Chris@1137 943 {
Chris@1137 944 return m_colourScaleMultiple;
Chris@1137 945 }
Chris@1137 946
Chris@1137 947 void
Chris@197 948 SpectrogramLayer::setColourMap(int map)
Chris@0 949 {
Chris@197 950 if (m_colourMap == map) return;
Chris@0 951
Chris@1106 952 invalidateRenderers();
Chris@0 953
Chris@197 954 m_colourMap = map;
Chris@9 955
Chris@0 956 emit layerParametersChanged();
Chris@0 957 }
Chris@0 958
Chris@196 959 int
Chris@197 960 SpectrogramLayer::getColourMap() const
Chris@0 961 {
Chris@197 962 return m_colourMap;
Chris@0 963 }
Chris@0 964
Chris@0 965 void
Chris@1103 966 SpectrogramLayer::setBinScale(BinScale binScale)
Chris@0 967 {
Chris@1093 968 if (m_binScale == binScale) return;
Chris@0 969
Chris@1106 970 invalidateRenderers();
Chris@1093 971 m_binScale = binScale;
Chris@9 972
Chris@9 973 emit layerParametersChanged();
Chris@0 974 }
Chris@0 975
Chris@1103 976 BinScale
Chris@1093 977 SpectrogramLayer::getBinScale() const
Chris@0 978 {
Chris@1093 979 return m_binScale;
Chris@0 980 }
Chris@0 981
Chris@0 982 void
Chris@1103 983 SpectrogramLayer::setBinDisplay(BinDisplay binDisplay)
Chris@35 984 {
Chris@37 985 if (m_binDisplay == binDisplay) return;
Chris@35 986
Chris@1106 987 invalidateRenderers();
Chris@37 988 m_binDisplay = binDisplay;
Chris@35 989
Chris@35 990 emit layerParametersChanged();
Chris@35 991 }
Chris@35 992
Chris@1103 993 BinDisplay
Chris@37 994 SpectrogramLayer::getBinDisplay() const
Chris@35 995 {
Chris@37 996 return m_binDisplay;
Chris@35 997 }
Chris@35 998
Chris@35 999 void
Chris@1104 1000 SpectrogramLayer::setNormalization(ColumnNormalization n)
Chris@36 1001 {
Chris@862 1002 if (m_normalization == n) return;
Chris@36 1003
Chris@1106 1004 invalidateRenderers();
Chris@119 1005 invalidateMagnitudes();
Chris@862 1006 m_normalization = n;
Chris@36 1007
Chris@36 1008 emit layerParametersChanged();
Chris@36 1009 }
Chris@36 1010
Chris@1104 1011 ColumnNormalization
Chris@862 1012 SpectrogramLayer::getNormalization() const
Chris@36 1013 {
Chris@862 1014 return m_normalization;
Chris@36 1015 }
Chris@36 1016
Chris@36 1017 void
Chris@1104 1018 SpectrogramLayer::setNormalizeVisibleArea(bool n)
Chris@1104 1019 {
Chris@1104 1020 if (m_normalizeVisibleArea == n) return;
Chris@1104 1021
Chris@1106 1022 invalidateRenderers();
Chris@1104 1023 invalidateMagnitudes();
Chris@1104 1024 m_normalizeVisibleArea = n;
Chris@1104 1025
Chris@1104 1026 emit layerParametersChanged();
Chris@1104 1027 }
Chris@1104 1028
Chris@1104 1029 bool
Chris@1104 1030 SpectrogramLayer::getNormalizeVisibleArea() const
Chris@1104 1031 {
Chris@1104 1032 return m_normalizeVisibleArea;
Chris@1104 1033 }
Chris@1104 1034
Chris@1104 1035 void
Chris@918 1036 SpectrogramLayer::setLayerDormant(const LayerGeometryProvider *v, bool dormant)
Chris@29 1037 {
Chris@33 1038 if (dormant) {
Chris@33 1039
Chris@331 1040 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@985 1041 cerr << "SpectrogramLayer::setLayerDormant(" << dormant << ")"
Chris@585 1042 << endl;
Chris@331 1043 #endif
Chris@331 1044
Chris@131 1045 if (isLayerDormant(v)) {
Chris@131 1046 return;
Chris@131 1047 }
Chris@131 1048
Chris@131 1049 Layer::setLayerDormant(v, true);
Chris@33 1050
Chris@1234 1051 invalidateRenderers();
Chris@1234 1052
Chris@33 1053 } else {
Chris@33 1054
Chris@131 1055 Layer::setLayerDormant(v, false);
Chris@33 1056 }
Chris@29 1057 }
Chris@29 1058
Chris@1232 1059 bool
Chris@1232 1060 SpectrogramLayer::isLayerScrollable(const LayerGeometryProvider *) const
Chris@1232 1061 {
Chris@1419 1062 // we do our own cacheing, and don't want to be responsible for
Chris@1419 1063 // guaranteeing to get an invisible seam if someone else scrolls
Chris@1419 1064 // us and we just fill in
Chris@1232 1065 return false;
Chris@1232 1066 }
Chris@1232 1067
Chris@29 1068 void
Chris@0 1069 SpectrogramLayer::cacheInvalid()
Chris@0 1070 {
Chris@391 1071 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@985 1072 cerr << "SpectrogramLayer::cacheInvalid()" << endl;
Chris@391 1073 #endif
Chris@391 1074
Chris@1106 1075 invalidateRenderers();
Chris@119 1076 invalidateMagnitudes();
Chris@0 1077 }
Chris@0 1078
Chris@0 1079 void
Chris@1037 1080 SpectrogramLayer::cacheInvalid(
Chris@1037 1081 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@1037 1082 sv_frame_t from, sv_frame_t to
Chris@1037 1083 #else
Chris@1037 1084 sv_frame_t , sv_frame_t
Chris@1037 1085 #endif
Chris@1037 1086 )
Chris@0 1087 {
Chris@391 1088 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@985 1089 cerr << "SpectrogramLayer::cacheInvalid(" << from << ", " << to << ")" << endl;
Chris@391 1090 #endif
Chris@391 1091
Chris@1030 1092 // We used to call invalidateMagnitudes(from, to) to invalidate
Chris@1030 1093 // only those caches whose views contained some of the (from, to)
Chris@1030 1094 // range. That's the right thing to do; it has been lost in
Chris@1030 1095 // pulling out the image cache code, but it might not matter very
Chris@1030 1096 // much, since the underlying models for spectrogram layers don't
Chris@1030 1097 // change very often. Let's see.
Chris@1106 1098 invalidateRenderers();
Chris@391 1099 invalidateMagnitudes();
Chris@0 1100 }
Chris@0 1101
Chris@224 1102 bool
Chris@224 1103 SpectrogramLayer::hasLightBackground() const
Chris@224 1104 {
Chris@1362 1105 return ColourMapper(m_colourMap, m_colourInverted, 1.f, 255.f)
Chris@1362 1106 .hasLightBackground();
Chris@224 1107 }
Chris@224 1108
Chris@905 1109 double
Chris@40 1110 SpectrogramLayer::getEffectiveMinFrequency() const
Chris@40 1111 {
Chris@907 1112 sv_samplerate_t sr = m_model->getSampleRate();
Chris@1087 1113 double minf = double(sr) / getFFTSize();
Chris@40 1114
Chris@40 1115 if (m_minFrequency > 0.0) {
Chris@1234 1116 int minbin = int((double(m_minFrequency) * getFFTSize()) / sr + 0.01);
Chris@1234 1117 if (minbin < 1) minbin = 1;
Chris@1234 1118 minf = minbin * sr / getFFTSize();
Chris@40 1119 }
Chris@40 1120
Chris@40 1121 return minf;
Chris@40 1122 }
Chris@40 1123
Chris@905 1124 double
Chris@40 1125 SpectrogramLayer::getEffectiveMaxFrequency() const
Chris@40 1126 {
Chris@907 1127 sv_samplerate_t sr = m_model->getSampleRate();
Chris@905 1128 double maxf = double(sr) / 2;
Chris@40 1129
Chris@40 1130 if (m_maxFrequency > 0.0) {
Chris@1234 1131 int maxbin = int((double(m_maxFrequency) * getFFTSize()) / sr + 0.1);
Chris@1234 1132 if (maxbin > getFFTSize() / 2) maxbin = getFFTSize() / 2;
Chris@1234 1133 maxf = maxbin * sr / getFFTSize();
Chris@40 1134 }
Chris@40 1135
Chris@40 1136 return maxf;
Chris@40 1137 }
Chris@40 1138
Chris@0 1139 bool
Chris@918 1140 SpectrogramLayer::getYBinRange(LayerGeometryProvider *v, int y, double &q0, double &q1) const
Chris@0 1141 {
Chris@382 1142 Profiler profiler("SpectrogramLayer::getYBinRange");
Chris@918 1143 int h = v->getPaintHeight();
Chris@0 1144 if (y < 0 || y >= h) return false;
Chris@1117 1145 q0 = getBinForY(v, y);
Chris@1117 1146 q1 = getBinForY(v, y-1);
Chris@1117 1147 return true;
Chris@1117 1148 }
Chris@1117 1149
Chris@1117 1150 double
Chris@1117 1151 SpectrogramLayer::getYForBin(const LayerGeometryProvider *v, double bin) const
Chris@1117 1152 {
Chris@1117 1153 double minf = getEffectiveMinFrequency();
Chris@1117 1154 double maxf = getEffectiveMaxFrequency();
Chris@1117 1155 bool logarithmic = (m_binScale == BinScale::Log);
Chris@1117 1156 sv_samplerate_t sr = m_model->getSampleRate();
Chris@1117 1157
Chris@1117 1158 double freq = (bin * sr) / getFFTSize();
Chris@1117 1159
Chris@1117 1160 double y = v->getYForFrequency(freq, minf, maxf, logarithmic);
Chris@1117 1161
Chris@1117 1162 return y;
Chris@1117 1163 }
Chris@1117 1164
Chris@1117 1165 double
Chris@1117 1166 SpectrogramLayer::getBinForY(const LayerGeometryProvider *v, double y) const
Chris@1117 1167 {
Chris@907 1168 sv_samplerate_t sr = m_model->getSampleRate();
Chris@905 1169 double minf = getEffectiveMinFrequency();
Chris@905 1170 double maxf = getEffectiveMaxFrequency();
Chris@0 1171
Chris@1103 1172 bool logarithmic = (m_binScale == BinScale::Log);
Chris@38 1173
Chris@1117 1174 double freq = v->getFrequencyForY(y, minf, maxf, logarithmic);
Chris@1117 1175
Chris@1117 1176 // Now map on to ("proportion of") actual bins
Chris@1117 1177 double bin = (freq * getFFTSize()) / sr;
Chris@1117 1178
Chris@1117 1179 return bin;
Chris@1085 1180 }
Chris@1085 1181
Chris@0 1182 bool
Chris@918 1183 SpectrogramLayer::getXBinRange(LayerGeometryProvider *v, int x, double &s0, double &s1) const
Chris@0 1184 {
Chris@907 1185 sv_frame_t modelStart = m_model->getStartFrame();
Chris@907 1186 sv_frame_t modelEnd = m_model->getEndFrame();
Chris@0 1187
Chris@0 1188 // Each pixel column covers an exact range of sample frames:
Chris@907 1189 sv_frame_t f0 = v->getFrameForX(x) - modelStart;
Chris@907 1190 sv_frame_t f1 = v->getFrameForX(x + 1) - modelStart - 1;
Chris@20 1191
Chris@41 1192 if (f1 < int(modelStart) || f0 > int(modelEnd)) {
Chris@1234 1193 return false;
Chris@41 1194 }
Chris@20 1195
Chris@0 1196 // And that range may be drawn from a possibly non-integral
Chris@0 1197 // range of spectrogram windows:
Chris@0 1198
Chris@805 1199 int windowIncrement = getWindowIncrement();
Chris@905 1200 s0 = double(f0) / windowIncrement;
Chris@905 1201 s1 = double(f1) / windowIncrement;
Chris@0 1202
Chris@0 1203 return true;
Chris@0 1204 }
Chris@0 1205
Chris@0 1206 bool
Chris@918 1207 SpectrogramLayer::getXBinSourceRange(LayerGeometryProvider *v, int x, RealTime &min, RealTime &max) const
Chris@0 1208 {
Chris@905 1209 double s0 = 0, s1 = 0;
Chris@44 1210 if (!getXBinRange(v, x, s0, s1)) return false;
Chris@0 1211
Chris@0 1212 int s0i = int(s0 + 0.001);
Chris@0 1213 int s1i = int(s1);
Chris@0 1214
Chris@0 1215 int windowIncrement = getWindowIncrement();
Chris@0 1216 int w0 = s0i * windowIncrement - (m_windowSize - windowIncrement)/2;
Chris@0 1217 int w1 = s1i * windowIncrement + windowIncrement +
Chris@1234 1218 (m_windowSize - windowIncrement)/2 - 1;
Chris@0 1219
Chris@0 1220 min = RealTime::frame2RealTime(w0, m_model->getSampleRate());
Chris@0 1221 max = RealTime::frame2RealTime(w1, m_model->getSampleRate());
Chris@0 1222 return true;
Chris@0 1223 }
Chris@0 1224
Chris@0 1225 bool
Chris@918 1226 SpectrogramLayer::getYBinSourceRange(LayerGeometryProvider *v, int y, double &freqMin, double &freqMax)
Chris@0 1227 const
Chris@0 1228 {
Chris@905 1229 double q0 = 0, q1 = 0;
Chris@44 1230 if (!getYBinRange(v, y, q0, q1)) return false;
Chris@0 1231
Chris@0 1232 int q0i = int(q0 + 0.001);
Chris@0 1233 int q1i = int(q1);
Chris@0 1234
Chris@907 1235 sv_samplerate_t sr = m_model->getSampleRate();
Chris@0 1236
Chris@0 1237 for (int q = q0i; q <= q1i; ++q) {
Chris@1234 1238 if (q == q0i) freqMin = (sr * q) / getFFTSize();
Chris@1234 1239 if (q == q1i) freqMax = (sr * (q+1)) / getFFTSize();
Chris@0 1240 }
Chris@0 1241 return true;
Chris@0 1242 }
Chris@35 1243
Chris@35 1244 bool
Chris@918 1245 SpectrogramLayer::getAdjustedYBinSourceRange(LayerGeometryProvider *v, int x, int y,
Chris@1234 1246 double &freqMin, double &freqMax,
Chris@1234 1247 double &adjFreqMin, double &adjFreqMax)
Chris@35 1248 const
Chris@35 1249 {
Chris@277 1250 if (!m_model || !m_model->isOK() || !m_model->isReady()) {
Chris@1234 1251 return false;
Chris@277 1252 }
Chris@277 1253
Chris@1088 1254 FFTModel *fft = getFFTModel();
Chris@114 1255 if (!fft) return false;
Chris@110 1256
Chris@905 1257 double s0 = 0, s1 = 0;
Chris@44 1258 if (!getXBinRange(v, x, s0, s1)) return false;
Chris@35 1259
Chris@905 1260 double q0 = 0, q1 = 0;
Chris@44 1261 if (!getYBinRange(v, y, q0, q1)) return false;
Chris@35 1262
Chris@35 1263 int s0i = int(s0 + 0.001);
Chris@35 1264 int s1i = int(s1);
Chris@35 1265
Chris@35 1266 int q0i = int(q0 + 0.001);
Chris@35 1267 int q1i = int(q1);
Chris@35 1268
Chris@907 1269 sv_samplerate_t sr = m_model->getSampleRate();
Chris@35 1270
Chris@35 1271 bool haveAdj = false;
Chris@35 1272
Chris@1103 1273 bool peaksOnly = (m_binDisplay == BinDisplay::PeakBins ||
Chris@1234 1274 m_binDisplay == BinDisplay::PeakFrequencies);
Chris@37 1275
Chris@35 1276 for (int q = q0i; q <= q1i; ++q) {
Chris@35 1277
Chris@1234 1278 for (int s = s0i; s <= s1i; ++s) {
Chris@1234 1279
Chris@1464 1280 double binfreq = (double(sr) * q) / getFFTSize();
Chris@1234 1281 if (q == q0i) freqMin = binfreq;
Chris@1234 1282 if (q == q1i) freqMax = binfreq;
Chris@1234 1283
Chris@1234 1284 if (peaksOnly && !fft->isLocalPeak(s, q)) continue;
Chris@1234 1285
Chris@1234 1286 if (!fft->isOverThreshold
Chris@1087 1287 (s, q, float(m_threshold * double(getFFTSize())/2.0))) {
Chris@1086 1288 continue;
Chris@1086 1289 }
Chris@907 1290
Chris@907 1291 double freq = binfreq;
Chris@1234 1292
Chris@1234 1293 if (s < int(fft->getWidth()) - 1) {
Chris@38 1294
Chris@277 1295 fft->estimateStableFrequency(s, q, freq);
Chris@1234 1296
Chris@1234 1297 if (!haveAdj || freq < adjFreqMin) adjFreqMin = freq;
Chris@1234 1298 if (!haveAdj || freq > adjFreqMax) adjFreqMax = freq;
Chris@1234 1299
Chris@1234 1300 haveAdj = true;
Chris@1234 1301 }
Chris@1234 1302 }
Chris@35 1303 }
Chris@35 1304
Chris@35 1305 if (!haveAdj) {
Chris@1234 1306 adjFreqMin = adjFreqMax = 0.0;
Chris@35 1307 }
Chris@35 1308
Chris@35 1309 return haveAdj;
Chris@35 1310 }
Chris@0 1311
Chris@0 1312 bool
Chris@918 1313 SpectrogramLayer::getXYBinSourceRange(LayerGeometryProvider *v, int x, int y,
Chris@1234 1314 double &min, double &max,
Chris@1234 1315 double &phaseMin, double &phaseMax) const
Chris@0 1316 {
Chris@277 1317 if (!m_model || !m_model->isOK() || !m_model->isReady()) {
Chris@1234 1318 return false;
Chris@277 1319 }
Chris@277 1320
Chris@905 1321 double q0 = 0, q1 = 0;
Chris@44 1322 if (!getYBinRange(v, y, q0, q1)) return false;
Chris@0 1323
Chris@905 1324 double s0 = 0, s1 = 0;
Chris@44 1325 if (!getXBinRange(v, x, s0, s1)) return false;
Chris@0 1326
Chris@0 1327 int q0i = int(q0 + 0.001);
Chris@0 1328 int q1i = int(q1);
Chris@0 1329
Chris@0 1330 int s0i = int(s0 + 0.001);
Chris@0 1331 int s1i = int(s1);
Chris@0 1332
Chris@37 1333 bool rv = false;
Chris@37 1334
Chris@1088 1335 FFTModel *fft = getFFTModel();
Chris@0 1336
Chris@114 1337 if (fft) {
Chris@114 1338
Chris@114 1339 int cw = fft->getWidth();
Chris@114 1340 int ch = fft->getHeight();
Chris@0 1341
Chris@110 1342 min = 0.0;
Chris@110 1343 max = 0.0;
Chris@110 1344 phaseMin = 0.0;
Chris@110 1345 phaseMax = 0.0;
Chris@110 1346 bool have = false;
Chris@0 1347
Chris@110 1348 for (int q = q0i; q <= q1i; ++q) {
Chris@110 1349 for (int s = s0i; s <= s1i; ++s) {
Chris@110 1350 if (s >= 0 && q >= 0 && s < cw && q < ch) {
Chris@117 1351
Chris@905 1352 double value;
Chris@38 1353
Chris@114 1354 value = fft->getPhaseAt(s, q);
Chris@110 1355 if (!have || value < phaseMin) { phaseMin = value; }
Chris@110 1356 if (!have || value > phaseMax) { phaseMax = value; }
Chris@91 1357
Chris@1087 1358 value = fft->getMagnitudeAt(s, q) / (getFFTSize()/2.0);
Chris@110 1359 if (!have || value < min) { min = value; }
Chris@110 1360 if (!have || value > max) { max = value; }
Chris@110 1361
Chris@110 1362 have = true;
Chris@1234 1363 }
Chris@110 1364 }
Chris@110 1365 }
Chris@110 1366
Chris@110 1367 if (have) {
Chris@110 1368 rv = true;
Chris@110 1369 }
Chris@0 1370 }
Chris@0 1371
Chris@37 1372 return rv;
Chris@0 1373 }
Chris@1234 1374
Chris@1211 1375 void
Chris@1211 1376 SpectrogramLayer::recreateFFTModel()
Chris@114 1377 {
Chris@1242 1378 SVDEBUG << "SpectrogramLayer::recreateFFTModel called" << endl;
Chris@1211 1379
Chris@1211 1380 if (!m_model || !m_model->isOK()) {
Chris@1408 1381 emit sliceableModelReplaced(m_fftModel, nullptr);
Chris@1242 1382 deleteDerivedModels();
Chris@1211 1383 return;
Chris@114 1384 }
Chris@1211 1385
Chris@1242 1386 if (m_fftModel) m_fftModel->aboutToDelete();
Chris@1242 1387
Chris@1242 1388 if (m_peakCache) m_peakCache->aboutToDelete();
Chris@1211 1389 delete m_peakCache;
Chris@1408 1390 m_peakCache = nullptr;
Chris@1212 1391
Chris@1242 1392 if (m_wholeCache) m_wholeCache->aboutToDelete();
Chris@1212 1393 delete m_wholeCache;
Chris@1408 1394 m_wholeCache = nullptr;
Chris@1211 1395
Chris@1242 1396 FFTModel *newModel = new FFTModel(m_model,
Chris@1242 1397 m_channel,
Chris@1242 1398 m_windowType,
Chris@1242 1399 m_windowSize,
Chris@1242 1400 getWindowIncrement(),
Chris@1242 1401 getFFTSize());
Chris@1242 1402
Chris@1242 1403 if (!newModel->isOK()) {
Chris@1088 1404 QMessageBox::critical
Chris@1408 1405 (nullptr, tr("FFT cache failed"),
Chris@1088 1406 tr("Failed to create the FFT model for this spectrogram.\n"
Chris@1088 1407 "There may be insufficient memory or disc space to continue."));
Chris@1242 1408 delete newModel;
Chris@1088 1409 delete m_fftModel;
Chris@1408 1410 m_fftModel = nullptr;
Chris@1211 1411 return;
Chris@114 1412 }
Chris@1212 1413
Chris@1242 1414 FFTModel *oldModel = m_fftModel;
Chris@1242 1415 m_fftModel = newModel;
Chris@1242 1416
Chris@1450 1417 bool createWholeCache = false;
Chris@1450 1418 checkCacheSpace(&m_peakCacheDivisor, &createWholeCache);
Chris@1450 1419
Chris@1450 1420 if (createWholeCache) {
Chris@1212 1421 m_wholeCache = new Dense3DModelPeakCache(m_fftModel, 1);
Chris@1212 1422 m_peakCache = new Dense3DModelPeakCache(m_wholeCache, m_peakCacheDivisor);
Chris@1212 1423 } else {
Chris@1212 1424 m_peakCache = new Dense3DModelPeakCache(m_fftModel, m_peakCacheDivisor);
Chris@1212 1425 }
Chris@1211 1426
Chris@1211 1427 emit sliceableModelReplaced(oldModel, m_fftModel);
Chris@1211 1428 delete oldModel;
Chris@484 1429 }
Chris@484 1430
Chris@1450 1431 void
Chris@1450 1432 SpectrogramLayer::checkCacheSpace(int *suggestedPeakDivisor,
Chris@1450 1433 bool *createWholeCache) const
Chris@1212 1434 {
Chris@1450 1435 *suggestedPeakDivisor = 8;
Chris@1450 1436 *createWholeCache = false;
Chris@1450 1437
Chris@1450 1438 if (!m_fftModel) return;
Chris@1212 1439
Chris@1212 1440 size_t sz =
Chris@1212 1441 size_t(m_fftModel->getWidth()) *
Chris@1212 1442 size_t(m_fftModel->getHeight()) *
Chris@1212 1443 sizeof(float);
Chris@1212 1444
Chris@1212 1445 try {
Chris@1212 1446 SVDEBUG << "Requesting advice from StorageAdviser on whether to create whole-model cache" << endl;
Chris@1450 1447 // The lower amount here is the amount required for the
Chris@1450 1448 // slightly higher-resolution version of the peak cache
Chris@1450 1449 // without a whole-model cache; the higher amount is that for
Chris@1450 1450 // the whole-model cache. The factors of 1024 are because
Chris@1450 1451 // StorageAdviser rather stupidly works in kilobytes
Chris@1212 1452 StorageAdviser::Recommendation recommendation =
Chris@1212 1453 StorageAdviser::recommend
Chris@1212 1454 (StorageAdviser::Criteria(StorageAdviser::SpeedCritical |
Chris@1212 1455 StorageAdviser::PrecisionCritical |
Chris@1212 1456 StorageAdviser::FrequentLookupLikely),
Chris@1450 1457 (sz / 8) / 1024, sz / 1024);
Chris@1450 1458 if (recommendation & StorageAdviser::UseDisc) {
Chris@1212 1459 SVDEBUG << "Seems inadvisable to create whole-model cache" << endl;
Chris@1450 1460 } else if (recommendation & StorageAdviser::ConserveSpace) {
Chris@1450 1461 SVDEBUG << "Seems inadvisable to create whole-model cache but acceptable to use the slightly higher-resolution peak cache" << endl;
Chris@1450 1462 *suggestedPeakDivisor = 4;
Chris@1450 1463 } else {
Chris@1212 1464 SVDEBUG << "Seems fine to create whole-model cache" << endl;
Chris@1450 1465 *createWholeCache = true;
Chris@1212 1466 }
Chris@1212 1467 } catch (const InsufficientDiscSpace &) {
Chris@1212 1468 SVDEBUG << "Seems like a terrible idea to create whole-model cache" << endl;
Chris@1212 1469 }
Chris@1212 1470 }
Chris@1212 1471
Chris@193 1472 const Model *
Chris@193 1473 SpectrogramLayer::getSliceableModel() const
Chris@193 1474 {
Chris@1088 1475 return m_fftModel;
Chris@193 1476 }
Chris@193 1477
Chris@114 1478 void
Chris@119 1479 SpectrogramLayer::invalidateMagnitudes()
Chris@119 1480 {
Chris@1044 1481 #ifdef DEBUG_SPECTROGRAM
Chris@1044 1482 cerr << "SpectrogramLayer::invalidateMagnitudes called" << endl;
Chris@1044 1483 #endif
Chris@119 1484 m_viewMags.clear();
Chris@119 1485 }
Chris@1134 1486
Chris@119 1487 void
Chris@389 1488 SpectrogramLayer::setSynchronousPainting(bool synchronous)
Chris@389 1489 {
Chris@389 1490 m_synchronous = synchronous;
Chris@389 1491 }
Chris@389 1492
Chris@1089 1493 Colour3DPlotRenderer *
Chris@1089 1494 SpectrogramLayer::getRenderer(LayerGeometryProvider *v) const
Chris@1089 1495 {
Chris@1136 1496 int viewId = v->getId();
Chris@1136 1497
Chris@1136 1498 if (m_renderers.find(viewId) == m_renderers.end()) {
Chris@1089 1499
Chris@1089 1500 Colour3DPlotRenderer::Sources sources;
Chris@1089 1501 sources.verticalBinLayer = this;
Chris@1090 1502 sources.fft = getFFTModel();
Chris@1090 1503 sources.source = sources.fft;
Chris@1212 1504 if (m_peakCache) sources.peakCaches.push_back(m_peakCache);
Chris@1212 1505 if (m_wholeCache) sources.peakCaches.push_back(m_wholeCache);
Chris@1089 1506
Chris@1092 1507 ColourScale::Parameters cparams;
Chris@1092 1508 cparams.colourMap = m_colourMap;
Chris@1137 1509 cparams.scaleType = m_colourScale;
Chris@1137 1510 cparams.multiple = m_colourScaleMultiple;
Chris@1129 1511
Chris@1129 1512 if (m_colourScale != ColourScaleType::Phase) {
Chris@1129 1513 cparams.gain = m_gain;
Chris@1129 1514 cparams.threshold = m_threshold;
Chris@1129 1515 }
Chris@1093 1516
Chris@1234 1517 double minValue = 0.0f;
Chris@1234 1518 double maxValue = 1.0f;
Chris@1136 1519
Chris@1136 1520 if (m_normalizeVisibleArea && m_viewMags[viewId].isSet()) {
Chris@1136 1521 minValue = m_viewMags[viewId].getMin();
Chris@1136 1522 maxValue = m_viewMags[viewId].getMax();
Chris@1136 1523 } else if (m_colourScale == ColourScaleType::Linear &&
Chris@1136 1524 m_normalization == ColumnNormalization::None) {
Chris@1136 1525 maxValue = 0.1f;
Chris@1093 1526 }
Chris@1129 1527
Chris@1136 1528 if (maxValue <= minValue) {
Chris@1136 1529 maxValue = minValue + 0.1f;
Chris@1136 1530 }
Chris@1136 1531 if (maxValue <= m_threshold) {
Chris@1136 1532 maxValue = m_threshold + 0.1f;
Chris@1136 1533 }
Chris@1136 1534
Chris@1136 1535 cparams.minValue = minValue;
Chris@1136 1536 cparams.maxValue = maxValue;
Chris@1136 1537
Chris@1234 1538 m_lastRenderedMags[viewId] = MagnitudeRange(float(minValue),
Chris@1234 1539 float(maxValue));
Chris@1136 1540
Chris@1089 1541 Colour3DPlotRenderer::Parameters params;
Chris@1092 1542 params.colourScale = ColourScale(cparams);
Chris@1089 1543 params.normalization = m_normalization;
Chris@1093 1544 params.binDisplay = m_binDisplay;
Chris@1093 1545 params.binScale = m_binScale;
Chris@1141 1546 params.alwaysOpaque = true;
Chris@1093 1547 params.invertVertical = false;
Chris@1125 1548 params.scaleFactor = 1.0;
Chris@1112 1549 params.colourRotation = m_colourRotation;
Chris@1093 1550
Chris@1145 1551 if (m_colourScale != ColourScaleType::Phase &&
Chris@1145 1552 m_normalization != ColumnNormalization::Hybrid) {
Chris@1403 1553 params.scaleFactor *= 2.f / float(getWindowSize());
Chris@1125 1554 }
Chris@1125 1555
Chris@1093 1556 Preferences::SpectrogramSmoothing smoothing =
Chris@1093 1557 Preferences::getInstance()->getSpectrogramSmoothing();
Chris@1093 1558 params.interpolate =
Chris@1399 1559 (smoothing != Preferences::NoSpectrogramSmoothing);
Chris@1089 1560
Chris@1234 1561 m_renderers[viewId] = new Colour3DPlotRenderer(sources, params);
Chris@1239 1562
Chris@1239 1563 m_crosshairColour =
Chris@1362 1564 ColourMapper(m_colourMap, m_colourInverted, 1.f, 255.f)
Chris@1362 1565 .getContrastingColour();
Chris@1089 1566 }
Chris@1089 1567
Chris@1234 1568 return m_renderers[viewId];
Chris@1089 1569 }
Chris@1089 1570
Chris@1089 1571 void
Chris@1106 1572 SpectrogramLayer::paintWithRenderer(LayerGeometryProvider *v, QPainter &paint, QRect rect) const
Chris@1106 1573 {
Chris@1121 1574 Colour3DPlotRenderer *renderer = getRenderer(v);
Chris@1121 1575
Chris@1121 1576 Colour3DPlotRenderer::RenderResult result;
Chris@1122 1577 MagnitudeRange magRange;
Chris@1122 1578 int viewId = v->getId();
Chris@1122 1579
Chris@1136 1580 bool continuingPaint = !renderer->geometryChanged(v);
Chris@1136 1581
Chris@1136 1582 if (continuingPaint) {
Chris@1122 1583 magRange = m_viewMags[viewId];
Chris@1122 1584 }
Chris@1106 1585
Chris@1106 1586 if (m_synchronous) {
Chris@1121 1587
Chris@1121 1588 result = renderer->render(v, paint, rect);
Chris@1121 1589
Chris@1121 1590 } else {
Chris@1121 1591
Chris@1121 1592 result = renderer->renderTimeConstrained(v, paint, rect);
Chris@1121 1593
Chris@1143 1594 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@1122 1595 cerr << "rect width from this paint: " << result.rendered.width()
Chris@1122 1596 << ", mag range in this paint: " << result.range.getMin() << " -> "
Chris@1121 1597 << result.range.getMax() << endl;
Chris@1143 1598 #endif
Chris@1121 1599
Chris@1121 1600 QRect uncached = renderer->getLargestUncachedRect(v);
Chris@1121 1601 if (uncached.width() > 0) {
Chris@1121 1602 v->updatePaintRect(uncached);
Chris@1121 1603 }
Chris@1106 1604 }
Chris@1106 1605
Chris@1122 1606 magRange.sample(result.range);
Chris@1122 1607
Chris@1122 1608 if (magRange.isSet()) {
Chris@1136 1609 if (m_viewMags[viewId] != magRange) {
Chris@1122 1610 m_viewMags[viewId] = magRange;
Chris@1143 1611 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@1136 1612 cerr << "mag range in this view has changed: "
Chris@1136 1613 << magRange.getMin() << " -> " << magRange.getMax() << endl;
Chris@1143 1614 #endif
Chris@1122 1615 }
Chris@1122 1616 }
Chris@1136 1617
Chris@1136 1618 if (!continuingPaint && m_normalizeVisibleArea &&
Chris@1136 1619 m_viewMags[viewId] != m_lastRenderedMags[viewId]) {
Chris@1143 1620 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@1136 1621 cerr << "mag range has changed from last rendered range: re-rendering"
Chris@1136 1622 << endl;
Chris@1143 1623 #endif
Chris@1136 1624 delete m_renderers[viewId];
Chris@1136 1625 m_renderers.erase(viewId);
Chris@1136 1626 v->updatePaintRect(v->getPaintRect());
Chris@1136 1627 }
Chris@1106 1628 }
Chris@1106 1629
Chris@1106 1630 void
Chris@916 1631 SpectrogramLayer::paint(LayerGeometryProvider *v, QPainter &paint, QRect rect) const
Chris@0 1632 {
Chris@334 1633 Profiler profiler("SpectrogramLayer::paint", false);
Chris@334 1634
Chris@0 1635 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@1026 1636 cerr << "SpectrogramLayer::paint() entering: m_model is " << m_model << ", zoom level is " << v->getZoomLevel() << endl;
Chris@95 1637
Chris@1026 1638 cerr << "SpectrogramLayer::paint(): rect is " << rect.x() << "," << rect.y() << " " << rect.width() << "x" << rect.height() << endl;
Chris@0 1639 #endif
Chris@95 1640
Chris@0 1641 if (!m_model || !m_model->isOK() || !m_model->isReady()) {
Chris@1234 1642 return;
Chris@29 1643 }
Chris@29 1644
Chris@1106 1645 paintWithRenderer(v, paint, rect);
Chris@1140 1646
Chris@1140 1647 illuminateLocalFeatures(v, paint);
Chris@480 1648 }
Chris@477 1649
Chris@121 1650 void
Chris@918 1651 SpectrogramLayer::illuminateLocalFeatures(LayerGeometryProvider *v, QPainter &paint) const
Chris@121 1652 {
Chris@382 1653 Profiler profiler("SpectrogramLayer::illuminateLocalFeatures");
Chris@382 1654
Chris@121 1655 QPoint localPos;
Chris@121 1656 if (!v->shouldIlluminateLocalFeatures(this, localPos) || !m_model) {
Chris@121 1657 return;
Chris@121 1658 }
Chris@121 1659
Chris@1143 1660 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@1134 1661 cerr << "SpectrogramLayer: illuminateLocalFeatures("
Chris@1134 1662 << localPos.x() << "," << localPos.y() << ")" << endl;
Chris@1143 1663 #endif
Chris@121 1664
Chris@905 1665 double s0, s1;
Chris@905 1666 double f0, f1;
Chris@121 1667
Chris@121 1668 if (getXBinRange(v, localPos.x(), s0, s1) &&
Chris@121 1669 getYBinSourceRange(v, localPos.y(), f0, f1)) {
Chris@121 1670
Chris@121 1671 int s0i = int(s0 + 0.001);
Chris@121 1672 int s1i = int(s1);
Chris@121 1673
Chris@121 1674 int x0 = v->getXForFrame(s0i * getWindowIncrement());
Chris@121 1675 int x1 = v->getXForFrame((s1i + 1) * getWindowIncrement());
Chris@121 1676
Chris@248 1677 int y1 = int(getYForFrequency(v, f1));
Chris@248 1678 int y0 = int(getYForFrequency(v, f0));
Chris@121 1679
Chris@1143 1680 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@1134 1681 cerr << "SpectrogramLayer: illuminate "
Chris@1134 1682 << x0 << "," << y1 << " -> " << x1 << "," << y0 << endl;
Chris@1143 1683 #endif
Chris@121 1684
Chris@287 1685 paint.setPen(v->getForeground());
Chris@133 1686
Chris@133 1687 //!!! should we be using paintCrosshairs for this?
Chris@133 1688
Chris@121 1689 paint.drawRect(x0, y1, x1 - x0 + 1, y0 - y1 + 1);
Chris@121 1690 }
Chris@121 1691 }
Chris@121 1692
Chris@905 1693 double
Chris@918 1694 SpectrogramLayer::getYForFrequency(const LayerGeometryProvider *v, double frequency) const
Chris@42 1695 {
Chris@44 1696 return v->getYForFrequency(frequency,
Chris@1234 1697 getEffectiveMinFrequency(),
Chris@1234 1698 getEffectiveMaxFrequency(),
Chris@1234 1699 m_binScale == BinScale::Log);
Chris@42 1700 }
Chris@42 1701
Chris@905 1702 double
Chris@918 1703 SpectrogramLayer::getFrequencyForY(const LayerGeometryProvider *v, int y) const
Chris@42 1704 {
Chris@44 1705 return v->getFrequencyForY(y,
Chris@1234 1706 getEffectiveMinFrequency(),
Chris@1234 1707 getEffectiveMaxFrequency(),
Chris@1234 1708 m_binScale == BinScale::Log);
Chris@42 1709 }
Chris@42 1710
Chris@0 1711 int
Chris@1090 1712 SpectrogramLayer::getCompletion(LayerGeometryProvider *) const
Chris@0 1713 {
Chris@1088 1714 if (!m_fftModel) return 100;
Chris@1088 1715 int completion = m_fftModel->getCompletion();
Chris@224 1716 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@985 1717 cerr << "SpectrogramLayer::getCompletion: completion = " << completion << endl;
Chris@224 1718 #endif
Chris@0 1719 return completion;
Chris@0 1720 }
Chris@0 1721
Chris@583 1722 QString
Chris@1090 1723 SpectrogramLayer::getError(LayerGeometryProvider *) const
Chris@583 1724 {
Chris@1088 1725 if (!m_fftModel) return "";
Chris@1088 1726 return m_fftModel->getError();
Chris@583 1727 }
Chris@583 1728
Chris@28 1729 bool
Chris@905 1730 SpectrogramLayer::getValueExtents(double &min, double &max,
Chris@101 1731 bool &logarithmic, QString &unit) const
Chris@79 1732 {
Chris@133 1733 if (!m_model) return false;
Chris@133 1734
Chris@907 1735 sv_samplerate_t sr = m_model->getSampleRate();
Chris@1087 1736 min = double(sr) / getFFTSize();
Chris@905 1737 max = double(sr) / 2;
Chris@133 1738
Chris@1103 1739 logarithmic = (m_binScale == BinScale::Log);
Chris@79 1740 unit = "Hz";
Chris@79 1741 return true;
Chris@79 1742 }
Chris@79 1743
Chris@79 1744 bool
Chris@905 1745 SpectrogramLayer::getDisplayExtents(double &min, double &max) const
Chris@101 1746 {
Chris@101 1747 min = getEffectiveMinFrequency();
Chris@101 1748 max = getEffectiveMaxFrequency();
Chris@253 1749
Chris@587 1750 // SVDEBUG << "SpectrogramLayer::getDisplayExtents: " << min << "->" << max << endl;
Chris@101 1751 return true;
Chris@101 1752 }
Chris@101 1753
Chris@101 1754 bool
Chris@905 1755 SpectrogramLayer::setDisplayExtents(double min, double max)
Chris@120 1756 {
Chris@120 1757 if (!m_model) return false;
Chris@187 1758
Chris@587 1759 // SVDEBUG << "SpectrogramLayer::setDisplayExtents: " << min << "->" << max << endl;
Chris@187 1760
Chris@120 1761 if (min < 0) min = 0;
Chris@907 1762 if (max > m_model->getSampleRate()/2.0) max = m_model->getSampleRate()/2.0;
Chris@120 1763
Chris@907 1764 int minf = int(lrint(min));
Chris@907 1765 int maxf = int(lrint(max));
Chris@120 1766
Chris@120 1767 if (m_minFrequency == minf && m_maxFrequency == maxf) return true;
Chris@120 1768
Chris@1106 1769 invalidateRenderers();
Chris@120 1770 invalidateMagnitudes();
Chris@120 1771
Chris@120 1772 m_minFrequency = minf;
Chris@120 1773 m_maxFrequency = maxf;
Chris@120 1774
Chris@120 1775 emit layerParametersChanged();
Chris@120 1776
Chris@133 1777 int vs = getCurrentVerticalZoomStep();
Chris@133 1778 if (vs != m_lastEmittedZoomStep) {
Chris@133 1779 emit verticalZoomChanged();
Chris@133 1780 m_lastEmittedZoomStep = vs;
Chris@133 1781 }
Chris@133 1782
Chris@120 1783 return true;
Chris@120 1784 }
Chris@120 1785
Chris@120 1786 bool
Chris@918 1787 SpectrogramLayer::getYScaleValue(const LayerGeometryProvider *v, int y,
Chris@905 1788 double &value, QString &unit) const
Chris@261 1789 {
Chris@261 1790 value = getFrequencyForY(v, y);
Chris@261 1791 unit = "Hz";
Chris@261 1792 return true;
Chris@261 1793 }
Chris@261 1794
Chris@261 1795 bool
Chris@918 1796 SpectrogramLayer::snapToFeatureFrame(LayerGeometryProvider *,
Chris@907 1797 sv_frame_t &frame,
Chris@1234 1798 int &resolution,
Chris@1234 1799 SnapType snap) const
Chris@13 1800 {
Chris@13 1801 resolution = getWindowIncrement();
Chris@907 1802 sv_frame_t left = (frame / resolution) * resolution;
Chris@907 1803 sv_frame_t right = left + resolution;
Chris@28 1804
Chris@28 1805 switch (snap) {
Chris@28 1806 case SnapLeft: frame = left; break;
Chris@28 1807 case SnapRight: frame = right; break;
Chris@28 1808 case SnapNeighbouring:
Chris@1234 1809 if (frame - left > right - frame) frame = right;
Chris@1234 1810 else frame = left;
Chris@1234 1811 break;
Chris@28 1812 }
Chris@28 1813
Chris@28 1814 return true;
Chris@28 1815 }
Chris@13 1816
Chris@283 1817 void
Chris@1139 1818 SpectrogramLayer::measureDoubleClick(LayerGeometryProvider *v, QMouseEvent *e)
Chris@283 1819 {
Chris@1139 1820 const Colour3DPlotRenderer *renderer = getRenderer(v);
Chris@1139 1821 if (!renderer) return;
Chris@1139 1822
Chris@1139 1823 QRect rect = renderer->findSimilarRegionExtents(e->pos());
Chris@283 1824 if (rect.isValid()) {
Chris@283 1825 MeasureRect mr;
Chris@283 1826 setMeasureRectFromPixrect(v, mr, rect);
Chris@283 1827 CommandHistory::getInstance()->addCommand
Chris@283 1828 (new AddMeasurementRectCommand(this, mr));
Chris@283 1829 }
Chris@283 1830 }
Chris@283 1831
Chris@77 1832 bool
Chris@918 1833 SpectrogramLayer::getCrosshairExtents(LayerGeometryProvider *v, QPainter &paint,
Chris@77 1834 QPoint cursorPos,
Chris@1025 1835 vector<QRect> &extents) const
Chris@77 1836 {
Chris@918 1837 QRect vertical(cursorPos.x() - 12, 0, 12, v->getPaintHeight());
Chris@77 1838 extents.push_back(vertical);
Chris@77 1839
Chris@77 1840 QRect horizontal(0, cursorPos.y(), cursorPos.x(), 1);
Chris@77 1841 extents.push_back(horizontal);
Chris@77 1842
Chris@608 1843 int sw = getVerticalScaleWidth(v, m_haveDetailedScale, paint);
Chris@264 1844
Chris@280 1845 QRect freq(sw, cursorPos.y() - paint.fontMetrics().ascent() - 2,
Chris@280 1846 paint.fontMetrics().width("123456 Hz") + 2,
Chris@280 1847 paint.fontMetrics().height());
Chris@280 1848 extents.push_back(freq);
Chris@264 1849
Chris@279 1850 QRect pitch(sw, cursorPos.y() + 2,
Chris@279 1851 paint.fontMetrics().width("C#10+50c") + 2,
Chris@279 1852 paint.fontMetrics().height());
Chris@279 1853 extents.push_back(pitch);
Chris@279 1854
Chris@280 1855 QRect rt(cursorPos.x(),
Chris@918 1856 v->getPaintHeight() - paint.fontMetrics().height() - 2,
Chris@280 1857 paint.fontMetrics().width("1234.567 s"),
Chris@280 1858 paint.fontMetrics().height());
Chris@280 1859 extents.push_back(rt);
Chris@280 1860
Chris@280 1861 int w(paint.fontMetrics().width("1234567890") + 2);
Chris@280 1862 QRect frame(cursorPos.x() - w - 2,
Chris@918 1863 v->getPaintHeight() - paint.fontMetrics().height() - 2,
Chris@280 1864 w,
Chris@280 1865 paint.fontMetrics().height());
Chris@280 1866 extents.push_back(frame);
Chris@280 1867
Chris@77 1868 return true;
Chris@77 1869 }
Chris@77 1870
Chris@77 1871 void
Chris@918 1872 SpectrogramLayer::paintCrosshairs(LayerGeometryProvider *v, QPainter &paint,
Chris@77 1873 QPoint cursorPos) const
Chris@77 1874 {
Chris@77 1875 paint.save();
Chris@1437 1876
Chris@608 1877 int sw = getVerticalScaleWidth(v, m_haveDetailedScale, paint);
Chris@283 1878
Chris@282 1879 QFont fn = paint.font();
Chris@282 1880 if (fn.pointSize() > 8) {
Chris@282 1881 fn.setPointSize(fn.pointSize() - 1);
Chris@282 1882 paint.setFont(fn);
Chris@282 1883 }
Chris@77 1884 paint.setPen(m_crosshairColour);
Chris@77 1885
Chris@77 1886 paint.drawLine(0, cursorPos.y(), cursorPos.x() - 1, cursorPos.y());
Chris@918 1887 paint.drawLine(cursorPos.x(), 0, cursorPos.x(), v->getPaintHeight());
Chris@77 1888
Chris@905 1889 double fundamental = getFrequencyForY(v, cursorPos.y());
Chris@77 1890
Chris@1078 1891 PaintAssistant::drawVisibleText(v, paint,
Chris@278 1892 sw + 2,
Chris@278 1893 cursorPos.y() - 2,
Chris@278 1894 QString("%1 Hz").arg(fundamental),
Chris@1078 1895 PaintAssistant::OutlinedText);
Chris@278 1896
Chris@279 1897 if (Pitch::isFrequencyInMidiRange(fundamental)) {
Chris@279 1898 QString pitchLabel = Pitch::getPitchLabelForFrequency(fundamental);
Chris@1078 1899 PaintAssistant::drawVisibleText(v, paint,
Chris@279 1900 sw + 2,
Chris@279 1901 cursorPos.y() + paint.fontMetrics().ascent() + 2,
Chris@279 1902 pitchLabel,
Chris@1078 1903 PaintAssistant::OutlinedText);
Chris@279 1904 }
Chris@279 1905
Chris@907 1906 sv_frame_t frame = v->getFrameForX(cursorPos.x());
Chris@279 1907 RealTime rt = RealTime::frame2RealTime(frame, m_model->getSampleRate());
Chris@280 1908 QString rtLabel = QString("%1 s").arg(rt.toText(true).c_str());
Chris@280 1909 QString frameLabel = QString("%1").arg(frame);
Chris@1078 1910 PaintAssistant::drawVisibleText(v, paint,
Chris@280 1911 cursorPos.x() - paint.fontMetrics().width(frameLabel) - 2,
Chris@918 1912 v->getPaintHeight() - 2,
Chris@280 1913 frameLabel,
Chris@1078 1914 PaintAssistant::OutlinedText);
Chris@1078 1915 PaintAssistant::drawVisibleText(v, paint,
Chris@280 1916 cursorPos.x() + 2,
Chris@918 1917 v->getPaintHeight() - 2,
Chris@280 1918 rtLabel,
Chris@1078 1919 PaintAssistant::OutlinedText);
Chris@264 1920
Chris@77 1921 int harmonic = 2;
Chris@77 1922
Chris@77 1923 while (harmonic < 100) {
Chris@77 1924
Chris@907 1925 int hy = int(lrint(getYForFrequency(v, fundamental * harmonic)));
Chris@918 1926 if (hy < 0 || hy > v->getPaintHeight()) break;
Chris@77 1927
Chris@77 1928 int len = 7;
Chris@77 1929
Chris@77 1930 if (harmonic % 2 == 0) {
Chris@77 1931 if (harmonic % 4 == 0) {
Chris@77 1932 len = 12;
Chris@77 1933 } else {
Chris@77 1934 len = 10;
Chris@77 1935 }
Chris@77 1936 }
Chris@77 1937
Chris@77 1938 paint.drawLine(cursorPos.x() - len,
Chris@907 1939 hy,
Chris@77 1940 cursorPos.x(),
Chris@907 1941 hy);
Chris@77 1942
Chris@77 1943 ++harmonic;
Chris@77 1944 }
Chris@77 1945
Chris@77 1946 paint.restore();
Chris@77 1947 }
Chris@77 1948
Chris@25 1949 QString
Chris@918 1950 SpectrogramLayer::getFeatureDescription(LayerGeometryProvider *v, QPoint &pos) const
Chris@25 1951 {
Chris@25 1952 int x = pos.x();
Chris@25 1953 int y = pos.y();
Chris@0 1954
Chris@25 1955 if (!m_model || !m_model->isOK()) return "";
Chris@0 1956
Chris@905 1957 double magMin = 0, magMax = 0;
Chris@905 1958 double phaseMin = 0, phaseMax = 0;
Chris@905 1959 double freqMin = 0, freqMax = 0;
Chris@905 1960 double adjFreqMin = 0, adjFreqMax = 0;
Chris@25 1961 QString pitchMin, pitchMax;
Chris@0 1962 RealTime rtMin, rtMax;
Chris@0 1963
Chris@38 1964 bool haveValues = false;
Chris@0 1965
Chris@44 1966 if (!getXBinSourceRange(v, x, rtMin, rtMax)) {
Chris@1234 1967 return "";
Chris@38 1968 }
Chris@44 1969 if (getXYBinSourceRange(v, x, y, magMin, magMax, phaseMin, phaseMax)) {
Chris@1234 1970 haveValues = true;
Chris@38 1971 }
Chris@0 1972
Chris@35 1973 QString adjFreqText = "", adjPitchText = "";
Chris@35 1974
Chris@1103 1975 if (m_binDisplay == BinDisplay::PeakFrequencies) {
Chris@35 1976
Chris@1234 1977 if (!getAdjustedYBinSourceRange(v, x, y, freqMin, freqMax,
Chris@1234 1978 adjFreqMin, adjFreqMax)) {
Chris@1234 1979 return "";
Chris@1234 1980 }
Chris@1234 1981
Chris@1234 1982 if (adjFreqMin != adjFreqMax) {
Chris@1234 1983 adjFreqText = tr("Peak Frequency:\t%1 - %2 Hz\n")
Chris@1234 1984 .arg(adjFreqMin).arg(adjFreqMax);
Chris@1234 1985 } else {
Chris@1234 1986 adjFreqText = tr("Peak Frequency:\t%1 Hz\n")
Chris@1234 1987 .arg(adjFreqMin);
Chris@1234 1988 }
Chris@1234 1989
Chris@1234 1990 QString pmin = Pitch::getPitchLabelForFrequency(adjFreqMin);
Chris@1234 1991 QString pmax = Pitch::getPitchLabelForFrequency(adjFreqMax);
Chris@1234 1992
Chris@1234 1993 if (pmin != pmax) {
Chris@1234 1994 adjPitchText = tr("Peak Pitch:\t%3 - %4\n").arg(pmin).arg(pmax);
Chris@1234 1995 } else {
Chris@1234 1996 adjPitchText = tr("Peak Pitch:\t%2\n").arg(pmin);
Chris@1234 1997 }
Chris@35 1998
Chris@35 1999 } else {
Chris@1234 2000
Chris@1234 2001 if (!getYBinSourceRange(v, y, freqMin, freqMax)) return "";
Chris@35 2002 }
Chris@35 2003
Chris@25 2004 QString text;
Chris@25 2005
Chris@25 2006 if (rtMin != rtMax) {
Chris@1234 2007 text += tr("Time:\t%1 - %2\n")
Chris@1234 2008 .arg(rtMin.toText(true).c_str())
Chris@1234 2009 .arg(rtMax.toText(true).c_str());
Chris@25 2010 } else {
Chris@1234 2011 text += tr("Time:\t%1\n")
Chris@1234 2012 .arg(rtMin.toText(true).c_str());
Chris@0 2013 }
Chris@0 2014
Chris@25 2015 if (freqMin != freqMax) {
Chris@1234 2016 text += tr("%1Bin Frequency:\t%2 - %3 Hz\n%4Bin Pitch:\t%5 - %6\n")
Chris@1234 2017 .arg(adjFreqText)
Chris@1234 2018 .arg(freqMin)
Chris@1234 2019 .arg(freqMax)
Chris@1234 2020 .arg(adjPitchText)
Chris@1234 2021 .arg(Pitch::getPitchLabelForFrequency(freqMin))
Chris@1234 2022 .arg(Pitch::getPitchLabelForFrequency(freqMax));
Chris@65 2023 } else {
Chris@1234 2024 text += tr("%1Bin Frequency:\t%2 Hz\n%3Bin Pitch:\t%4\n")
Chris@1234 2025 .arg(adjFreqText)
Chris@1234 2026 .arg(freqMin)
Chris@1234 2027 .arg(adjPitchText)
Chris@1234 2028 .arg(Pitch::getPitchLabelForFrequency(freqMin));
Chris@1234 2029 }
Chris@25 2030
Chris@38 2031 if (haveValues) {
Chris@1234 2032 double dbMin = AudioLevel::multiplier_to_dB(magMin);
Chris@1234 2033 double dbMax = AudioLevel::multiplier_to_dB(magMax);
Chris@1234 2034 QString dbMinString;
Chris@1234 2035 QString dbMaxString;
Chris@1234 2036 if (dbMin == AudioLevel::DB_FLOOR) {
Chris@1234 2037 dbMinString = Strings::minus_infinity;
Chris@1234 2038 } else {
Chris@1234 2039 dbMinString = QString("%1").arg(lrint(dbMin));
Chris@1234 2040 }
Chris@1234 2041 if (dbMax == AudioLevel::DB_FLOOR) {
Chris@1234 2042 dbMaxString = Strings::minus_infinity;
Chris@1234 2043 } else {
Chris@1234 2044 dbMaxString = QString("%1").arg(lrint(dbMax));
Chris@1234 2045 }
Chris@1234 2046 if (lrint(dbMin) != lrint(dbMax)) {
Chris@1234 2047 text += tr("dB:\t%1 - %2").arg(dbMinString).arg(dbMaxString);
Chris@1234 2048 } else {
Chris@1234 2049 text += tr("dB:\t%1").arg(dbMinString);
Chris@1234 2050 }
Chris@1234 2051 if (phaseMin != phaseMax) {
Chris@1234 2052 text += tr("\nPhase:\t%1 - %2").arg(phaseMin).arg(phaseMax);
Chris@1234 2053 } else {
Chris@1234 2054 text += tr("\nPhase:\t%1").arg(phaseMin);
Chris@1234 2055 }
Chris@25 2056 }
Chris@25 2057
Chris@25 2058 return text;
Chris@0 2059 }
Chris@25 2060
Chris@0 2061 int
Chris@40 2062 SpectrogramLayer::getColourScaleWidth(QPainter &paint) const
Chris@40 2063 {
Chris@40 2064 int cw;
Chris@40 2065
Chris@119 2066 cw = paint.fontMetrics().width("-80dB");
Chris@119 2067
Chris@40 2068 return cw;
Chris@40 2069 }
Chris@40 2070
Chris@40 2071 int
Chris@918 2072 SpectrogramLayer::getVerticalScaleWidth(LayerGeometryProvider *, bool detailed, QPainter &paint) const
Chris@0 2073 {
Chris@0 2074 if (!m_model || !m_model->isOK()) return 0;
Chris@0 2075
Chris@607 2076 int cw = 0;
Chris@607 2077 if (detailed) cw = getColourScaleWidth(paint);
Chris@40 2078
Chris@0 2079 int tw = paint.fontMetrics().width(QString("%1")
Chris@1234 2080 .arg(m_maxFrequency > 0 ?
Chris@1234 2081 m_maxFrequency - 1 :
Chris@1234 2082 m_model->getSampleRate() / 2));
Chris@0 2083
Chris@234 2084 int fw = paint.fontMetrics().width(tr("43Hz"));
Chris@0 2085 if (tw < fw) tw = fw;
Chris@40 2086
Chris@1103 2087 int tickw = (m_binScale == BinScale::Log ? 10 : 4);
Chris@0 2088
Chris@40 2089 return cw + tickw + tw + 13;
Chris@0 2090 }
Chris@0 2091
Chris@0 2092 void
Chris@1142 2093 SpectrogramLayer::paintVerticalScale(LayerGeometryProvider *v, bool detailed,
Chris@1142 2094 QPainter &paint, QRect rect) const
Chris@0 2095 {
Chris@0 2096 if (!m_model || !m_model->isOK()) {
Chris@1234 2097 return;
Chris@0 2098 }
Chris@0 2099
Chris@382 2100 Profiler profiler("SpectrogramLayer::paintVerticalScale");
Chris@122 2101
Chris@120 2102 //!!! cache this?
Chris@1142 2103
Chris@0 2104 int h = rect.height(), w = rect.width();
Chris@1142 2105 int textHeight = paint.fontMetrics().height();
Chris@1142 2106
Chris@1142 2107 if (detailed && (h > textHeight * 3 + 10)) {
Chris@1142 2108 paintDetailedScale(v, paint, rect);
Chris@1142 2109 }
Chris@1142 2110 m_haveDetailedScale = detailed;
Chris@0 2111
Chris@1103 2112 int tickw = (m_binScale == BinScale::Log ? 10 : 4);
Chris@1103 2113 int pkw = (m_binScale == BinScale::Log ? 10 : 0);
Chris@40 2114
Chris@1087 2115 int bins = getFFTSize() / 2;
Chris@907 2116 sv_samplerate_t sr = m_model->getSampleRate();
Chris@0 2117
Chris@0 2118 if (m_maxFrequency > 0) {
Chris@1234 2119 bins = int((double(m_maxFrequency) * getFFTSize()) / sr + 0.1);
Chris@1234 2120 if (bins > getFFTSize() / 2) bins = getFFTSize() / 2;
Chris@0 2121 }
Chris@0 2122
Chris@607 2123 int cw = 0;
Chris@607 2124 if (detailed) cw = getColourScaleWidth(paint);
Chris@40 2125
Chris@0 2126 int py = -1;
Chris@0 2127 int toff = -textHeight + paint.fontMetrics().ascent() + 2;
Chris@0 2128
Chris@40 2129 paint.drawLine(cw + 7, 0, cw + 7, h);
Chris@40 2130
Chris@0 2131 int bin = -1;
Chris@0 2132
Chris@918 2133 for (int y = 0; y < v->getPaintHeight(); ++y) {
Chris@0 2134
Chris@1234 2135 double q0, q1;
Chris@1234 2136 if (!getYBinRange(v, v->getPaintHeight() - y, q0, q1)) continue;
Chris@1234 2137
Chris@1234 2138 int vy;
Chris@1234 2139
Chris@1234 2140 if (int(q0) > bin) {
Chris@1234 2141 vy = y;
Chris@1234 2142 bin = int(q0);
Chris@1234 2143 } else {
Chris@1234 2144 continue;
Chris@1234 2145 }
Chris@1234 2146
Chris@1234 2147 int freq = int((sr * bin) / getFFTSize());
Chris@1234 2148
Chris@1234 2149 if (py >= 0 && (vy - py) < textHeight - 1) {
Chris@1234 2150 if (m_binScale == BinScale::Linear) {
Chris@1234 2151 paint.drawLine(w - tickw, h - vy, w, h - vy);
Chris@1234 2152 }
Chris@1234 2153 continue;
Chris@1234 2154 }
Chris@1234 2155
Chris@1234 2156 QString text = QString("%1").arg(freq);
Chris@1234 2157 if (bin == 1) text = tr("%1Hz").arg(freq); // bin 0 is DC
Chris@1234 2158 paint.drawLine(cw + 7, h - vy, w - pkw - 1, h - vy);
Chris@1234 2159
Chris@1234 2160 if (h - vy - textHeight >= -2) {
Chris@1234 2161 int tx = w - 3 - paint.fontMetrics().width(text) - max(tickw, pkw);
Chris@1234 2162 paint.drawText(tx, h - vy + toff, text);
Chris@1234 2163 }
Chris@1234 2164
Chris@1234 2165 py = vy;
Chris@0 2166 }
Chris@40 2167
Chris@1103 2168 if (m_binScale == BinScale::Log) {
Chris@40 2169
Chris@277 2170 // piano keyboard
Chris@277 2171
Chris@690 2172 PianoScale().paintPianoVertical
Chris@690 2173 (v, paint, QRect(w - pkw - 1, 0, pkw, h),
Chris@690 2174 getEffectiveMinFrequency(), getEffectiveMaxFrequency());
Chris@40 2175 }
Chris@608 2176
Chris@608 2177 m_haveDetailedScale = detailed;
Chris@0 2178 }
Chris@0 2179
Chris@1142 2180 void
Chris@1142 2181 SpectrogramLayer::paintDetailedScale(LayerGeometryProvider *v,
Chris@1142 2182 QPainter &paint, QRect rect) const
Chris@1142 2183 {
Chris@1142 2184 // The colour scale
Chris@1143 2185
Chris@1143 2186 if (m_colourScale == ColourScaleType::Phase) {
Chris@1143 2187 paintDetailedScalePhase(v, paint, rect);
Chris@1143 2188 return;
Chris@1143 2189 }
Chris@1142 2190
Chris@1142 2191 int h = rect.height();
Chris@1142 2192 int textHeight = paint.fontMetrics().height();
Chris@1142 2193 int toff = -textHeight + paint.fontMetrics().ascent() + 2;
Chris@1142 2194
Chris@1142 2195 int cw = getColourScaleWidth(paint);
Chris@1142 2196 int cbw = paint.fontMetrics().width("dB");
Chris@1142 2197
Chris@1142 2198 int topLines = 2;
Chris@1142 2199
Chris@1142 2200 int ch = h - textHeight * (topLines + 1) - 8;
Chris@1234 2201 // paint.drawRect(4, textHeight + 4, cw - 1, ch + 1);
Chris@1142 2202 paint.drawRect(4 + cw - cbw, textHeight * topLines + 4, cbw - 1, ch + 1);
Chris@1142 2203
Chris@1142 2204 QString top, bottom;
Chris@1142 2205 double min = m_viewMags[v->getId()].getMin();
Chris@1142 2206 double max = m_viewMags[v->getId()].getMax();
Chris@1142 2207
Chris@1142 2208 if (min < m_threshold) min = m_threshold;
Chris@1142 2209 if (max <= min) max = min + 0.1;
Chris@1142 2210
Chris@1142 2211 double dBmin = AudioLevel::multiplier_to_dB(min);
Chris@1142 2212 double dBmax = AudioLevel::multiplier_to_dB(max);
Chris@1142 2213
Chris@1142 2214 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@1142 2215 cerr << "paintVerticalScale: for view id " << v->getId()
Chris@1142 2216 << ": min = " << min << ", max = " << max
Chris@1142 2217 << ", dBmin = " << dBmin << ", dBmax = " << dBmax << endl;
Chris@1142 2218 #endif
Chris@1142 2219
Chris@1142 2220 if (dBmax < -60.f) dBmax = -60.f;
Chris@1142 2221 else top = QString("%1").arg(lrint(dBmax));
Chris@1142 2222
Chris@1142 2223 if (dBmin < dBmax - 60.f) dBmin = dBmax - 60.f;
Chris@1142 2224 bottom = QString("%1").arg(lrint(dBmin));
Chris@1142 2225
Chris@1142 2226 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@1142 2227 cerr << "adjusted dB range to min = " << dBmin << ", max = " << dBmax
Chris@1142 2228 << endl;
Chris@1142 2229 #endif
Chris@1142 2230
Chris@1143 2231 paint.drawText((cw + 6 - paint.fontMetrics().width("dBFS")) / 2,
Chris@1143 2232 2 + textHeight + toff, "dBFS");
Chris@1142 2233
Chris@1142 2234 paint.drawText(3 + cw - cbw - paint.fontMetrics().width(top),
Chris@1142 2235 2 + textHeight * topLines + toff + textHeight/2, top);
Chris@1142 2236
Chris@1142 2237 paint.drawText(3 + cw - cbw - paint.fontMetrics().width(bottom),
Chris@1142 2238 h + toff - 3 - textHeight/2, bottom);
Chris@1142 2239
Chris@1142 2240 paint.save();
Chris@1142 2241 paint.setBrush(Qt::NoBrush);
Chris@1142 2242
Chris@1142 2243 int lasty = 0;
Chris@1142 2244 int lastdb = 0;
Chris@1142 2245
Chris@1142 2246 for (int i = 0; i < ch; ++i) {
Chris@1142 2247
Chris@1142 2248 double dBval = dBmin + (((dBmax - dBmin) * i) / (ch - 1));
Chris@1142 2249 int idb = int(dBval);
Chris@1142 2250
Chris@1142 2251 double value = AudioLevel::dB_to_multiplier(dBval);
Chris@1142 2252 paint.setPen(getRenderer(v)->getColour(value));
Chris@1142 2253
Chris@1142 2254 int y = textHeight * topLines + 4 + ch - i;
Chris@1142 2255
Chris@1142 2256 paint.drawLine(5 + cw - cbw, y, cw + 2, y);
Chris@1144 2257
Chris@1142 2258 if (i == 0) {
Chris@1142 2259 lasty = y;
Chris@1142 2260 lastdb = idb;
Chris@1142 2261 } else if (i < ch - paint.fontMetrics().ascent() &&
Chris@1142 2262 idb != lastdb &&
Chris@1142 2263 ((abs(y - lasty) > textHeight &&
Chris@1142 2264 idb % 10 == 0) ||
Chris@1142 2265 (abs(y - lasty) > paint.fontMetrics().ascent() &&
Chris@1142 2266 idb % 5 == 0))) {
Chris@1144 2267 paint.setPen(v->getForeground());
Chris@1142 2268 QString text = QString("%1").arg(idb);
Chris@1142 2269 paint.drawText(3 + cw - cbw - paint.fontMetrics().width(text),
Chris@1142 2270 y + toff + textHeight/2, text);
Chris@1142 2271 paint.drawLine(5 + cw - cbw, y, 8 + cw - cbw, y);
Chris@1142 2272 lasty = y;
Chris@1142 2273 lastdb = idb;
Chris@1142 2274 }
Chris@1142 2275 }
Chris@1142 2276 paint.restore();
Chris@1142 2277 }
Chris@1142 2278
Chris@1143 2279 void
Chris@1143 2280 SpectrogramLayer::paintDetailedScalePhase(LayerGeometryProvider *v,
Chris@1143 2281 QPainter &paint, QRect rect) const
Chris@1143 2282 {
Chris@1143 2283 // The colour scale in phase mode
Chris@1143 2284
Chris@1143 2285 int h = rect.height();
Chris@1143 2286 int textHeight = paint.fontMetrics().height();
Chris@1143 2287 int toff = -textHeight + paint.fontMetrics().ascent() + 2;
Chris@1143 2288
Chris@1143 2289 int cw = getColourScaleWidth(paint);
Chris@1143 2290
Chris@1143 2291 // Phase is not measured in dB of course, but this places the
Chris@1143 2292 // scale at the same position as in the magnitude spectrogram
Chris@1143 2293 int cbw = paint.fontMetrics().width("dB");
Chris@1143 2294
Chris@1143 2295 int topLines = 1;
Chris@1143 2296
Chris@1143 2297 int ch = h - textHeight * (topLines + 1) - 8;
Chris@1143 2298 paint.drawRect(4 + cw - cbw, textHeight * topLines + 4, cbw - 1, ch + 1);
Chris@1143 2299
Chris@1147 2300 QString top = Strings::pi, bottom = Strings::minus_pi, middle = "0";
Chris@1143 2301
Chris@1143 2302 double min = -M_PI;
Chris@1143 2303 double max = M_PI;
Chris@1143 2304
Chris@1143 2305 paint.drawText(3 + cw - cbw - paint.fontMetrics().width(top),
Chris@1143 2306 2 + textHeight * topLines + toff + textHeight/2, top);
Chris@1143 2307
Chris@1143 2308 paint.drawText(3 + cw - cbw - paint.fontMetrics().width(middle),
Chris@1143 2309 2 + textHeight * topLines + ch/2 + toff + textHeight/2, middle);
Chris@1143 2310
Chris@1143 2311 paint.drawText(3 + cw - cbw - paint.fontMetrics().width(bottom),
Chris@1143 2312 h + toff - 3 - textHeight/2, bottom);
Chris@1143 2313
Chris@1143 2314 paint.save();
Chris@1143 2315 paint.setBrush(Qt::NoBrush);
Chris@1143 2316
Chris@1143 2317 for (int i = 0; i < ch; ++i) {
Chris@1143 2318 double val = min + (((max - min) * i) / (ch - 1));
Chris@1143 2319 paint.setPen(getRenderer(v)->getColour(val));
Chris@1143 2320 int y = textHeight * topLines + 4 + ch - i;
Chris@1143 2321 paint.drawLine(5 + cw - cbw, y, cw + 2, y);
Chris@1143 2322 }
Chris@1143 2323 paint.restore();
Chris@1143 2324 }
Chris@1143 2325
Chris@187 2326 class SpectrogramRangeMapper : public RangeMapper
Chris@187 2327 {
Chris@187 2328 public:
Chris@901 2329 SpectrogramRangeMapper(sv_samplerate_t sr, int /* fftsize */) :
Chris@901 2330 m_dist(sr / 2),
Chris@901 2331 m_s2(sqrt(sqrt(2))) { }
Chris@1405 2332 ~SpectrogramRangeMapper() override { }
Chris@187 2333
Chris@1405 2334 int getPositionForValue(double value) const override {
Chris@901 2335
Chris@901 2336 double dist = m_dist;
Chris@187 2337
Chris@187 2338 int n = 0;
Chris@187 2339
Chris@901 2340 while (dist > (value + 0.00001) && dist > 0.1) {
Chris@187 2341 dist /= m_s2;
Chris@187 2342 ++n;
Chris@187 2343 }
Chris@187 2344
Chris@187 2345 return n;
Chris@187 2346 }
Chris@724 2347
Chris@1405 2348 int getPositionForValueUnclamped(double value) const override {
Chris@724 2349 // We don't really support this
Chris@724 2350 return getPositionForValue(value);
Chris@724 2351 }
Chris@187 2352
Chris@1405 2353 double getValueForPosition(int position) const override {
Chris@187 2354
Chris@187 2355 // Vertical zoom step 0 shows the entire range from DC ->
Chris@187 2356 // Nyquist frequency. Step 1 shows 2^(1/4) of the range of
Chris@187 2357 // step 0, and so on until the visible range is smaller than
Chris@187 2358 // the frequency step between bins at the current fft size.
Chris@187 2359
Chris@901 2360 double dist = m_dist;
Chris@187 2361
Chris@187 2362 int n = 0;
Chris@187 2363 while (n < position) {
Chris@187 2364 dist /= m_s2;
Chris@187 2365 ++n;
Chris@187 2366 }
Chris@187 2367
Chris@187 2368 return dist;
Chris@187 2369 }
Chris@187 2370
Chris@1405 2371 double getValueForPositionUnclamped(int position) const override {
Chris@724 2372 // We don't really support this
Chris@724 2373 return getValueForPosition(position);
Chris@724 2374 }
Chris@724 2375
Chris@1405 2376 QString getUnit() const override { return "Hz"; }
Chris@187 2377
Chris@187 2378 protected:
Chris@901 2379 double m_dist;
Chris@901 2380 double m_s2;
Chris@187 2381 };
Chris@187 2382
Chris@133 2383 int
Chris@133 2384 SpectrogramLayer::getVerticalZoomSteps(int &defaultStep) const
Chris@133 2385 {
Chris@135 2386 if (!m_model) return 0;
Chris@187 2387
Chris@907 2388 sv_samplerate_t sr = m_model->getSampleRate();
Chris@187 2389
Chris@1087 2390 SpectrogramRangeMapper mapper(sr, getFFTSize());
Chris@1087 2391
Chris@1087 2392 // int maxStep = mapper.getPositionForValue((double(sr) / getFFTSize()) + 0.001);
Chris@187 2393 int maxStep = mapper.getPositionForValue(0);
Chris@905 2394 int minStep = mapper.getPositionForValue(double(sr) / 2);
Chris@250 2395
Chris@805 2396 int initialMax = m_initialMaxFrequency;
Chris@907 2397 if (initialMax == 0) initialMax = int(sr / 2);
Chris@250 2398
Chris@250 2399 defaultStep = mapper.getPositionForValue(initialMax) - minStep;
Chris@250 2400
Chris@587 2401 // SVDEBUG << "SpectrogramLayer::getVerticalZoomSteps: " << maxStep - minStep << " (" << maxStep <<"-" << minStep << "), default is " << defaultStep << " (from initial max freq " << initialMax << ")" << endl;
Chris@187 2402
Chris@187 2403 return maxStep - minStep;
Chris@133 2404 }
Chris@133 2405
Chris@133 2406 int
Chris@133 2407 SpectrogramLayer::getCurrentVerticalZoomStep() const
Chris@133 2408 {
Chris@133 2409 if (!m_model) return 0;
Chris@133 2410
Chris@905 2411 double dmin, dmax;
Chris@133 2412 getDisplayExtents(dmin, dmax);
Chris@133 2413
Chris@1087 2414 SpectrogramRangeMapper mapper(m_model->getSampleRate(), getFFTSize());
Chris@187 2415 int n = mapper.getPositionForValue(dmax - dmin);
Chris@587 2416 // SVDEBUG << "SpectrogramLayer::getCurrentVerticalZoomStep: " << n << endl;
Chris@133 2417 return n;
Chris@133 2418 }
Chris@133 2419
Chris@133 2420 void
Chris@133 2421 SpectrogramLayer::setVerticalZoomStep(int step)
Chris@133 2422 {
Chris@187 2423 if (!m_model) return;
Chris@187 2424
Chris@905 2425 double dmin = m_minFrequency, dmax = m_maxFrequency;
Chris@253 2426 // getDisplayExtents(dmin, dmax);
Chris@253 2427
Chris@682 2428 // cerr << "current range " << dmin << " -> " << dmax << ", range " << dmax-dmin << ", mid " << (dmax + dmin)/2 << endl;
Chris@133 2429
Chris@907 2430 sv_samplerate_t sr = m_model->getSampleRate();
Chris@1087 2431 SpectrogramRangeMapper mapper(sr, getFFTSize());
Chris@905 2432 double newdist = mapper.getValueForPosition(step);
Chris@905 2433
Chris@905 2434 double newmin, newmax;
Chris@253 2435
Chris@1103 2436 if (m_binScale == BinScale::Log) {
Chris@253 2437
Chris@253 2438 // need to pick newmin and newmax such that
Chris@253 2439 //
Chris@253 2440 // (log(newmin) + log(newmax)) / 2 == logmid
Chris@253 2441 // and
Chris@253 2442 // newmax - newmin = newdist
Chris@253 2443 //
Chris@253 2444 // so log(newmax - newdist) + log(newmax) == 2logmid
Chris@253 2445 // log(newmax(newmax - newdist)) == 2logmid
Chris@253 2446 // newmax.newmax - newmax.newdist == exp(2logmid)
Chris@253 2447 // newmax^2 + (-newdist)newmax + -exp(2logmid) == 0
Chris@253 2448 // quadratic with a = 1, b = -newdist, c = -exp(2logmid), all known
Chris@253 2449 //
Chris@253 2450 // positive root
Chris@253 2451 // newmax = (newdist + sqrt(newdist^2 + 4exp(2logmid))) / 2
Chris@253 2452 //
Chris@253 2453 // but logmid = (log(dmin) + log(dmax)) / 2
Chris@253 2454 // so exp(2logmid) = exp(log(dmin) + log(dmax))
Chris@253 2455 // = exp(log(dmin.dmax))
Chris@253 2456 // = dmin.dmax
Chris@253 2457 // so newmax = (newdist + sqrtf(newdist^2 + 4dmin.dmax)) / 2
Chris@253 2458
Chris@907 2459 newmax = (newdist + sqrt(newdist*newdist + 4*dmin*dmax)) / 2;
Chris@253 2460 newmin = newmax - newdist;
Chris@253 2461
Chris@682 2462 // cerr << "newmin = " << newmin << ", newmax = " << newmax << endl;
Chris@253 2463
Chris@253 2464 } else {
Chris@905 2465 double dmid = (dmax + dmin) / 2;
Chris@253 2466 newmin = dmid - newdist / 2;
Chris@253 2467 newmax = dmid + newdist / 2;
Chris@253 2468 }
Chris@187 2469
Chris@905 2470 double mmin, mmax;
Chris@187 2471 mmin = 0;
Chris@905 2472 mmax = double(sr) / 2;
Chris@133 2473
Chris@187 2474 if (newmin < mmin) {
Chris@187 2475 newmax += (mmin - newmin);
Chris@187 2476 newmin = mmin;
Chris@187 2477 }
Chris@187 2478 if (newmax > mmax) {
Chris@187 2479 newmax = mmax;
Chris@187 2480 }
Chris@133 2481
Chris@587 2482 // SVDEBUG << "SpectrogramLayer::setVerticalZoomStep: " << step << ": " << newmin << " -> " << newmax << " (range " << newdist << ")" << endl;
Chris@253 2483
Chris@907 2484 setMinFrequency(int(lrint(newmin)));
Chris@907 2485 setMaxFrequency(int(lrint(newmax)));
Chris@187 2486 }
Chris@187 2487
Chris@187 2488 RangeMapper *
Chris@187 2489 SpectrogramLayer::getNewVerticalZoomRangeMapper() const
Chris@187 2490 {
Chris@1408 2491 if (!m_model) return nullptr;
Chris@1087 2492 return new SpectrogramRangeMapper(m_model->getSampleRate(), getFFTSize());
Chris@133 2493 }
Chris@133 2494
Chris@273 2495 void
Chris@918 2496 SpectrogramLayer::updateMeasureRectYCoords(LayerGeometryProvider *v, const MeasureRect &r) const
Chris@273 2497 {
Chris@273 2498 int y0 = 0;
Chris@907 2499 if (r.startY > 0.0) y0 = int(getYForFrequency(v, r.startY));
Chris@273 2500
Chris@273 2501 int y1 = y0;
Chris@907 2502 if (r.endY > 0.0) y1 = int(getYForFrequency(v, r.endY));
Chris@273 2503
Chris@587 2504 // SVDEBUG << "SpectrogramLayer::updateMeasureRectYCoords: start " << r.startY << " -> " << y0 << ", end " << r.endY << " -> " << y1 << endl;
Chris@273 2505
Chris@273 2506 r.pixrect = QRect(r.pixrect.x(), y0, r.pixrect.width(), y1 - y0);
Chris@273 2507 }
Chris@273 2508
Chris@273 2509 void
Chris@918 2510 SpectrogramLayer::setMeasureRectYCoord(LayerGeometryProvider *v, MeasureRect &r, bool start, int y) const
Chris@273 2511 {
Chris@273 2512 if (start) {
Chris@273 2513 r.startY = getFrequencyForY(v, y);
Chris@273 2514 r.endY = r.startY;
Chris@273 2515 } else {
Chris@273 2516 r.endY = getFrequencyForY(v, y);
Chris@273 2517 }
Chris@587 2518 // SVDEBUG << "SpectrogramLayer::setMeasureRectYCoord: start " << r.startY << " <- " << y << ", end " << r.endY << " <- " << y << endl;
Chris@273 2519
Chris@273 2520 }
Chris@273 2521
Chris@316 2522 void
Chris@316 2523 SpectrogramLayer::toXml(QTextStream &stream,
Chris@316 2524 QString indent, QString extraAttributes) const
Chris@6 2525 {
Chris@6 2526 QString s;
Chris@6 2527
Chris@6 2528 s += QString("channel=\"%1\" "
Chris@1234 2529 "windowSize=\"%2\" "
Chris@1234 2530 "windowHopLevel=\"%3\" "
Chris@1382 2531 "oversampling=\"%4\" "
Chris@1382 2532 "gain=\"%5\" "
Chris@1382 2533 "threshold=\"%6\" ")
Chris@1234 2534 .arg(m_channel)
Chris@1234 2535 .arg(m_windowSize)
Chris@1234 2536 .arg(m_windowHopLevel)
Chris@1382 2537 .arg(m_oversampling)
Chris@1234 2538 .arg(m_gain)
Chris@1234 2539 .arg(m_threshold);
Chris@37 2540
Chris@37 2541 s += QString("minFrequency=\"%1\" "
Chris@1234 2542 "maxFrequency=\"%2\" "
Chris@1234 2543 "colourScale=\"%3\" "
Chris@1362 2544 "colourRotation=\"%4\" "
Chris@1362 2545 "frequencyScale=\"%5\" "
Chris@1362 2546 "binDisplay=\"%6\" ")
Chris@1234 2547 .arg(m_minFrequency)
Chris@1234 2548 .arg(m_maxFrequency)
Chris@1234 2549 .arg(convertFromColourScale(m_colourScale, m_colourScaleMultiple))
Chris@1234 2550 .arg(m_colourRotation)
Chris@1234 2551 .arg(int(m_binScale))
Chris@1234 2552 .arg(int(m_binDisplay));
Chris@761 2553
Chris@1362 2554 // New-style colour map attribute, by string id rather than by
Chris@1362 2555 // number
Chris@1362 2556
Chris@1362 2557 s += QString("colourMap=\"%1\" ")
Chris@1362 2558 .arg(ColourMapper::getColourMapId(m_colourMap));
Chris@1362 2559
Chris@1362 2560 // Old-style colour map attribute
Chris@1362 2561
Chris@1362 2562 s += QString("colourScheme=\"%1\" ")
Chris@1362 2563 .arg(ColourMapper::getBackwardCompatibilityColourMap(m_colourMap));
Chris@1362 2564
Chris@1009 2565 // New-style normalization attributes, allowing for more types of
Chris@1009 2566 // normalization in future: write out the column normalization
Chris@1009 2567 // type separately, and then whether we are normalizing visible
Chris@1009 2568 // area as well afterwards
Chris@1009 2569
Chris@1009 2570 s += QString("columnNormalization=\"%1\" ")
Chris@1104 2571 .arg(m_normalization == ColumnNormalization::Max1 ? "peak" :
Chris@1104 2572 m_normalization == ColumnNormalization::Hybrid ? "hybrid" : "none");
Chris@1009 2573
Chris@1009 2574 // Old-style normalization attribute. We *don't* write out
Chris@1009 2575 // normalizeHybrid here because the only release that would accept
Chris@1009 2576 // it (Tony v1.0) has a totally different scale factor for
Chris@1009 2577 // it. We'll just have to accept that session files from Tony
Chris@1009 2578 // v2.0+ will look odd in Tony v1.0
Chris@1009 2579
Chris@1009 2580 s += QString("normalizeColumns=\"%1\" ")
Chris@1234 2581 .arg(m_normalization == ColumnNormalization::Max1 ? "true" : "false");
Chris@1009 2582
Chris@1009 2583 // And this applies to both old- and new-style attributes
Chris@1009 2584
Chris@1009 2585 s += QString("normalizeVisibleArea=\"%1\" ")
Chris@1104 2586 .arg(m_normalizeVisibleArea ? "true" : "false");
Chris@1009 2587
Chris@316 2588 Layer::toXml(stream, indent, extraAttributes + " " + s);
Chris@6 2589 }
Chris@6 2590
Chris@11 2591 void
Chris@11 2592 SpectrogramLayer::setProperties(const QXmlAttributes &attributes)
Chris@11 2593 {
Chris@11 2594 bool ok = false;
Chris@11 2595
Chris@11 2596 int channel = attributes.value("channel").toInt(&ok);
Chris@11 2597 if (ok) setChannel(channel);
Chris@11 2598
Chris@805 2599 int windowSize = attributes.value("windowSize").toUInt(&ok);
Chris@11 2600 if (ok) setWindowSize(windowSize);
Chris@11 2601
Chris@805 2602 int windowHopLevel = attributes.value("windowHopLevel").toUInt(&ok);
Chris@97 2603 if (ok) setWindowHopLevel(windowHopLevel);
Chris@97 2604 else {
Chris@805 2605 int windowOverlap = attributes.value("windowOverlap").toUInt(&ok);
Chris@97 2606 // a percentage value
Chris@97 2607 if (ok) {
Chris@97 2608 if (windowOverlap == 0) setWindowHopLevel(0);
Chris@97 2609 else if (windowOverlap == 25) setWindowHopLevel(1);
Chris@97 2610 else if (windowOverlap == 50) setWindowHopLevel(2);
Chris@97 2611 else if (windowOverlap == 75) setWindowHopLevel(3);
Chris@97 2612 else if (windowOverlap == 90) setWindowHopLevel(4);
Chris@97 2613 }
Chris@97 2614 }
Chris@11 2615
Chris@1382 2616 int oversampling = attributes.value("oversampling").toUInt(&ok);
Chris@1382 2617 if (ok) setOversampling(oversampling);
Chris@1382 2618
Chris@11 2619 float gain = attributes.value("gain").toFloat(&ok);
Chris@11 2620 if (ok) setGain(gain);
Chris@11 2621
Chris@37 2622 float threshold = attributes.value("threshold").toFloat(&ok);
Chris@37 2623 if (ok) setThreshold(threshold);
Chris@37 2624
Chris@805 2625 int minFrequency = attributes.value("minFrequency").toUInt(&ok);
Chris@187 2626 if (ok) {
Chris@587 2627 SVDEBUG << "SpectrogramLayer::setProperties: setting min freq to " << minFrequency << endl;
Chris@187 2628 setMinFrequency(minFrequency);
Chris@187 2629 }
Chris@37 2630
Chris@805 2631 int maxFrequency = attributes.value("maxFrequency").toUInt(&ok);
Chris@187 2632 if (ok) {
Chris@587 2633 SVDEBUG << "SpectrogramLayer::setProperties: setting max freq to " << maxFrequency << endl;
Chris@187 2634 setMaxFrequency(maxFrequency);
Chris@187 2635 }
Chris@11 2636
Chris@1137 2637 auto colourScale = convertToColourScale
Chris@1092 2638 (attributes.value("colourScale").toInt(&ok));
Chris@1137 2639 if (ok) {
Chris@1137 2640 setColourScale(colourScale.first);
Chris@1137 2641 setColourScaleMultiple(colourScale.second);
Chris@1137 2642 }
Chris@11 2643
Chris@1362 2644 QString colourMapId = attributes.value("colourMap");
Chris@1362 2645 int colourMap = ColourMapper::getColourMapById(colourMapId);
Chris@1362 2646 if (colourMap >= 0) {
Chris@1362 2647 setColourMap(colourMap);
Chris@1362 2648 } else {
Chris@1362 2649 colourMap = attributes.value("colourScheme").toInt(&ok);
Chris@1362 2650 if (ok && colourMap < ColourMapper::getColourMapCount()) {
Chris@1362 2651 setColourMap(colourMap);
Chris@1362 2652 }
Chris@1362 2653 }
Chris@11 2654
Chris@37 2655 int colourRotation = attributes.value("colourRotation").toInt(&ok);
Chris@37 2656 if (ok) setColourRotation(colourRotation);
Chris@37 2657
Chris@1103 2658 BinScale binScale = (BinScale)
Chris@1234 2659 attributes.value("frequencyScale").toInt(&ok);
Chris@1093 2660 if (ok) setBinScale(binScale);
Chris@1093 2661
Chris@1103 2662 BinDisplay binDisplay = (BinDisplay)
Chris@1234 2663 attributes.value("binDisplay").toInt(&ok);
Chris@37 2664 if (ok) setBinDisplay(binDisplay);
Chris@36 2665
Chris@1009 2666 bool haveNewStyleNormalization = false;
Chris@1009 2667
Chris@1009 2668 QString columnNormalization = attributes.value("columnNormalization");
Chris@1009 2669
Chris@1009 2670 if (columnNormalization != "") {
Chris@1009 2671
Chris@1009 2672 haveNewStyleNormalization = true;
Chris@1009 2673
Chris@1009 2674 if (columnNormalization == "peak") {
Chris@1104 2675 setNormalization(ColumnNormalization::Max1);
Chris@1009 2676 } else if (columnNormalization == "hybrid") {
Chris@1104 2677 setNormalization(ColumnNormalization::Hybrid);
Chris@1009 2678 } else if (columnNormalization == "none") {
Chris@1104 2679 setNormalization(ColumnNormalization::None);
Chris@1009 2680 } else {
Chris@1265 2681 SVCERR << "NOTE: Unknown or unsupported columnNormalization attribute \""
Chris@1009 2682 << columnNormalization << "\"" << endl;
Chris@1009 2683 }
Chris@1009 2684 }
Chris@1009 2685
Chris@1009 2686 if (!haveNewStyleNormalization) {
Chris@1009 2687
Chris@1009 2688 bool normalizeColumns =
Chris@1009 2689 (attributes.value("normalizeColumns").trimmed() == "true");
Chris@1009 2690 if (normalizeColumns) {
Chris@1104 2691 setNormalization(ColumnNormalization::Max1);
Chris@1009 2692 }
Chris@1009 2693
Chris@1009 2694 bool normalizeHybrid =
Chris@1009 2695 (attributes.value("normalizeHybrid").trimmed() == "true");
Chris@1009 2696 if (normalizeHybrid) {
Chris@1104 2697 setNormalization(ColumnNormalization::Hybrid);
Chris@1009 2698 }
Chris@862 2699 }
Chris@153 2700
Chris@153 2701 bool normalizeVisibleArea =
Chris@1099 2702 (attributes.value("normalizeVisibleArea").trimmed() == "true");
Chris@1104 2703 setNormalizeVisibleArea(normalizeVisibleArea);
Chris@1104 2704
Chris@1104 2705 if (!haveNewStyleNormalization && m_normalization == ColumnNormalization::Hybrid) {
Chris@1009 2706 // Tony v1.0 is (and hopefully will remain!) the only released
Chris@1009 2707 // SV-a-like to use old-style attributes when saving sessions
Chris@1009 2708 // that ask for hybrid normalization. It saves them with the
Chris@1009 2709 // wrong gain factor, so hack in a fix for that here -- this
Chris@1009 2710 // gives us backward but not forward compatibility.
Chris@1087 2711 setGain(m_gain / float(getFFTSize() / 2));
Chris@862 2712 }
Chris@11 2713 }
Chris@11 2714