annotate layer/SpectrogramLayer.cpp @ 1503:2c7a480f93ca

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