annotate layer/SpectrogramLayer.cpp @ 1473:886c1cd48f9d by-id

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