annotate layer/SpectrogramLayer.cpp @ 1239:a496986aa61b

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