annotate layer/SpectrogramLayer.cpp @ 1209:f7bb22999d2e 3.0-integration

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