annotate layer/SpectrogramLayer.cpp @ 1140:ca910d8070c8 spectrogram-minor-refactor

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