annotate layer/SpectrogramLayer.cpp @ 1211:7a19738b9762 3.0-integration

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