annotate layer/SpectrogramLayer.cpp @ 1102:36a981a0fa31 spectrogram-minor-refactor

Delete renderers
author Chris Cannam
date Wed, 13 Jul 2016 13:44:11 +0100
parents 1364cbf4453d
children d84a0033b305
rev   line source
Chris@58 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@0 2
Chris@0 3 /*
Chris@59 4 Sonic Visualiser
Chris@59 5 An audio file viewer and annotation editor.
Chris@59 6 Centre for Digital Music, Queen Mary, University of London.
Chris@484 7 This file copyright 2006-2009 Chris Cannam and QMUL.
Chris@0 8
Chris@59 9 This program is free software; you can redistribute it and/or
Chris@59 10 modify it under the terms of the GNU General Public License as
Chris@59 11 published by the Free Software Foundation; either version 2 of the
Chris@59 12 License, or (at your option) any later version. See the file
Chris@59 13 COPYING included with this distribution for more information.
Chris@0 14 */
Chris@0 15
Chris@0 16 #include "SpectrogramLayer.h"
Chris@0 17
Chris@128 18 #include "view/View.h"
Chris@0 19 #include "base/Profiler.h"
Chris@0 20 #include "base/AudioLevel.h"
Chris@0 21 #include "base/Window.h"
Chris@24 22 #include "base/Pitch.h"
Chris@118 23 #include "base/Preferences.h"
Chris@167 24 #include "base/RangeMapper.h"
Chris@253 25 #include "base/LogRange.h"
Chris@1063 26 #include "base/ColumnOp.h"
Chris@376 27 #include "widgets/CommandHistory.h"
Chris@1078 28 #include "data/model/Dense3DModelPeakCache.h"
Chris@1078 29
Chris@376 30 #include "ColourMapper.h"
Chris@283 31 #include "ImageRegionFinder.h"
Chris@690 32 #include "PianoScale.h"
Chris@1078 33 #include "PaintAssistant.h"
Chris@1089 34 #include "Colour3DPlotRenderer.h"
Chris@0 35
Chris@0 36 #include <QPainter>
Chris@0 37 #include <QImage>
Chris@0 38 #include <QPixmap>
Chris@0 39 #include <QRect>
Chris@92 40 #include <QApplication>
Chris@178 41 #include <QMessageBox>
Chris@283 42 #include <QMouseEvent>
Chris@316 43 #include <QTextStream>
Chris@1017 44 #include <QSettings>
Chris@0 45
Chris@0 46 #include <iostream>
Chris@0 47
Chris@0 48 #include <cassert>
Chris@0 49 #include <cmath>
Chris@0 50
Chris@545 51 #ifndef __GNUC__
Chris@545 52 #include <alloca.h>
Chris@545 53 #endif
Chris@545 54
Chris@1090 55 #define DEBUG_SPECTROGRAM 1
Chris@1044 56 #define DEBUG_SPECTROGRAM_REPAINT 1
Chris@1025 57
Chris@1025 58 using namespace std;
Chris@907 59
Chris@44 60 SpectrogramLayer::SpectrogramLayer(Configuration config) :
Chris@0 61 m_model(0),
Chris@0 62 m_channel(0),
Chris@0 63 m_windowSize(1024),
Chris@0 64 m_windowType(HanningWindow),
Chris@97 65 m_windowHopLevel(2),
Chris@0 66 m_gain(1.0),
Chris@215 67 m_initialGain(1.0),
Chris@37 68 m_threshold(0.0),
Chris@215 69 m_initialThreshold(0.0),
Chris@9 70 m_colourRotation(0),
Chris@215 71 m_initialRotation(0),
Chris@119 72 m_minFrequency(10),
Chris@0 73 m_maxFrequency(8000),
Chris@135 74 m_initialMaxFrequency(8000),
Chris@1092 75 m_colourScale(ColourScale::LogColourScale),
Chris@197 76 m_colourMap(0),
Chris@1093 77 m_binScale(Colour3DPlotRenderer::LinearBinScale),
Chris@1093 78 m_binDisplay(Colour3DPlotRenderer::AllBins),
Chris@1063 79 m_normalization(ColumnOp::NoNormalization),
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@1088 85 m_peakCache(0),
Chris@1088 86 m_peakCacheDivisor(8)
Chris@0 87 {
Chris@1017 88 QString colourConfigName = "spectrogram-colour";
Chris@1017 89 int colourConfigDefault = int(ColourMapper::Green);
Chris@1017 90
Chris@215 91 if (config == FullRangeDb) {
Chris@215 92 m_initialMaxFrequency = 0;
Chris@215 93 setMaxFrequency(0);
Chris@215 94 } else if (config == MelodicRange) {
Chris@0 95 setWindowSize(8192);
Chris@97 96 setWindowHopLevel(4);
Chris@215 97 m_initialMaxFrequency = 1500;
Chris@215 98 setMaxFrequency(1500);
Chris@215 99 setMinFrequency(40);
Chris@1092 100 setColourScale(ColourScale::LinearColourScale);
Chris@215 101 setColourMap(ColourMapper::Sunset);
Chris@1093 102 setBinScale(Colour3DPlotRenderer::LogBinScale);
Chris@1017 103 colourConfigName = "spectrogram-melodic-colour";
Chris@1017 104 colourConfigDefault = int(ColourMapper::Sunset);
Chris@224 105 // setGain(20);
Chris@37 106 } else if (config == MelodicPeaks) {
Chris@37 107 setWindowSize(4096);
Chris@97 108 setWindowHopLevel(5);
Chris@135 109 m_initialMaxFrequency = 2000;
Chris@40 110 setMaxFrequency(2000);
Chris@37 111 setMinFrequency(40);
Chris@1093 112 setBinScale(Colour3DPlotRenderer::LogBinScale);
Chris@1092 113 setColourScale(ColourScale::LinearColourScale);
Chris@1093 114 setBinDisplay(Colour3DPlotRenderer::PeakFrequencies);
Chris@1063 115 setNormalization(ColumnOp::NormalizeColumns);
Chris@1017 116 colourConfigName = "spectrogram-melodic-colour";
Chris@1017 117 colourConfigDefault = int(ColourMapper::Sunset);
Chris@0 118 }
Chris@110 119
Chris@1017 120 QSettings settings;
Chris@1017 121 settings.beginGroup("Preferences");
Chris@1017 122 setColourMap(settings.value(colourConfigName, colourConfigDefault).toInt());
Chris@1017 123 settings.endGroup();
Chris@1017 124
Chris@122 125 Preferences *prefs = Preferences::getInstance();
Chris@122 126 connect(prefs, SIGNAL(propertyChanged(PropertyContainer::PropertyName)),
Chris@122 127 this, SLOT(preferenceChanged(PropertyContainer::PropertyName)));
Chris@122 128 setWindowType(prefs->getWindowType());
Chris@122 129
Chris@197 130 initialisePalette();
Chris@0 131 }
Chris@0 132
Chris@0 133 SpectrogramLayer::~SpectrogramLayer()
Chris@0 134 {
Chris@1102 135 invalidateImageCaches();
Chris@1088 136 invalidateFFTModel();
Chris@0 137 }
Chris@0 138
Chris@0 139 void
Chris@0 140 SpectrogramLayer::setModel(const DenseTimeValueModel *model)
Chris@0 141 {
Chris@682 142 // cerr << "SpectrogramLayer(" << this << "): setModel(" << model << ")" << endl;
Chris@34 143
Chris@110 144 if (model == m_model) return;
Chris@110 145
Chris@0 146 m_model = model;
Chris@1088 147 invalidateFFTModel();
Chris@0 148
Chris@0 149 if (!m_model || !m_model->isOK()) return;
Chris@0 150
Chris@320 151 connectSignals(m_model);
Chris@0 152
Chris@0 153 connect(m_model, SIGNAL(modelChanged()), this, SLOT(cacheInvalid()));
Chris@906 154 connect(m_model, SIGNAL(modelChangedWithin(sv_frame_t, sv_frame_t)),
Chris@906 155 this, SLOT(cacheInvalid(sv_frame_t, sv_frame_t)));
Chris@0 156
Chris@0 157 emit modelReplaced();
Chris@110 158 }
Chris@115 159
Chris@0 160 Layer::PropertyList
Chris@0 161 SpectrogramLayer::getProperties() const
Chris@0 162 {
Chris@0 163 PropertyList list;
Chris@87 164 list.push_back("Colour");
Chris@87 165 list.push_back("Colour Scale");
Chris@87 166 list.push_back("Window Size");
Chris@97 167 list.push_back("Window Increment");
Chris@862 168 list.push_back("Normalization");
Chris@87 169 list.push_back("Bin Display");
Chris@87 170 list.push_back("Threshold");
Chris@87 171 list.push_back("Gain");
Chris@87 172 list.push_back("Colour Rotation");
Chris@153 173 // list.push_back("Min Frequency");
Chris@153 174 // list.push_back("Max Frequency");
Chris@87 175 list.push_back("Frequency Scale");
Chris@0 176 return list;
Chris@0 177 }
Chris@0 178
Chris@87 179 QString
Chris@87 180 SpectrogramLayer::getPropertyLabel(const PropertyName &name) const
Chris@87 181 {
Chris@87 182 if (name == "Colour") return tr("Colour");
Chris@87 183 if (name == "Colour Scale") return tr("Colour Scale");
Chris@87 184 if (name == "Window Size") return tr("Window Size");
Chris@112 185 if (name == "Window Increment") return tr("Window Overlap");
Chris@862 186 if (name == "Normalization") return tr("Normalization");
Chris@87 187 if (name == "Bin Display") return tr("Bin Display");
Chris@87 188 if (name == "Threshold") return tr("Threshold");
Chris@87 189 if (name == "Gain") return tr("Gain");
Chris@87 190 if (name == "Colour Rotation") return tr("Colour Rotation");
Chris@87 191 if (name == "Min Frequency") return tr("Min Frequency");
Chris@87 192 if (name == "Max Frequency") return tr("Max Frequency");
Chris@87 193 if (name == "Frequency Scale") return tr("Frequency Scale");
Chris@87 194 return "";
Chris@87 195 }
Chris@87 196
Chris@335 197 QString
Chris@862 198 SpectrogramLayer::getPropertyIconName(const PropertyName &) const
Chris@335 199 {
Chris@335 200 return "";
Chris@335 201 }
Chris@335 202
Chris@0 203 Layer::PropertyType
Chris@0 204 SpectrogramLayer::getPropertyType(const PropertyName &name) const
Chris@0 205 {
Chris@87 206 if (name == "Gain") return RangeProperty;
Chris@87 207 if (name == "Colour Rotation") return RangeProperty;
Chris@87 208 if (name == "Threshold") return RangeProperty;
Chris@0 209 return ValueProperty;
Chris@0 210 }
Chris@0 211
Chris@0 212 QString
Chris@0 213 SpectrogramLayer::getPropertyGroupName(const PropertyName &name) const
Chris@0 214 {
Chris@153 215 if (name == "Bin Display" ||
Chris@153 216 name == "Frequency Scale") return tr("Bins");
Chris@87 217 if (name == "Window Size" ||
Chris@1086 218 name == "Window Increment") return tr("Window");
Chris@87 219 if (name == "Colour" ||
Chris@87 220 name == "Threshold" ||
Chris@87 221 name == "Colour Rotation") return tr("Colour");
Chris@862 222 if (name == "Normalization" ||
Chris@153 223 name == "Gain" ||
Chris@87 224 name == "Colour Scale") return tr("Scale");
Chris@0 225 return QString();
Chris@0 226 }
Chris@0 227
Chris@0 228 int
Chris@0 229 SpectrogramLayer::getPropertyRangeAndValue(const PropertyName &name,
Chris@216 230 int *min, int *max, int *deflt) const
Chris@0 231 {
Chris@216 232 int val = 0;
Chris@216 233
Chris@216 234 int garbage0, garbage1, garbage2;
Chris@55 235 if (!min) min = &garbage0;
Chris@55 236 if (!max) max = &garbage1;
Chris@216 237 if (!deflt) deflt = &garbage2;
Chris@10 238
Chris@87 239 if (name == "Gain") {
Chris@0 240
Chris@0 241 *min = -50;
Chris@0 242 *max = 50;
Chris@0 243
Chris@906 244 *deflt = int(lrint(log10(m_initialGain) * 20.0));
Chris@216 245 if (*deflt < *min) *deflt = *min;
Chris@216 246 if (*deflt > *max) *deflt = *max;
Chris@216 247
Chris@906 248 val = int(lrint(log10(m_gain) * 20.0));
Chris@216 249 if (val < *min) val = *min;
Chris@216 250 if (val > *max) val = *max;
Chris@0 251
Chris@87 252 } else if (name == "Threshold") {
Chris@37 253
Chris@37 254 *min = -50;
Chris@37 255 *max = 0;
Chris@37 256
Chris@906 257 *deflt = int(lrint(AudioLevel::multiplier_to_dB(m_initialThreshold)));
Chris@216 258 if (*deflt < *min) *deflt = *min;
Chris@216 259 if (*deflt > *max) *deflt = *max;
Chris@216 260
Chris@906 261 val = int(lrint(AudioLevel::multiplier_to_dB(m_threshold)));
Chris@216 262 if (val < *min) val = *min;
Chris@216 263 if (val > *max) val = *max;
Chris@37 264
Chris@87 265 } else if (name == "Colour Rotation") {
Chris@9 266
Chris@9 267 *min = 0;
Chris@9 268 *max = 256;
Chris@216 269 *deflt = m_initialRotation;
Chris@216 270
Chris@216 271 val = m_colourRotation;
Chris@9 272
Chris@87 273 } else if (name == "Colour Scale") {
Chris@0 274
Chris@1099 275 // linear, meter, db^2, db, phase
Chris@0 276 *min = 0;
Chris@176 277 *max = 4;
Chris@1092 278 *deflt = 2;
Chris@216 279
Chris@216 280 val = (int)m_colourScale;
Chris@0 281
Chris@87 282 } else if (name == "Colour") {
Chris@0 283
Chris@0 284 *min = 0;
Chris@196 285 *max = ColourMapper::getColourMapCount() - 1;
Chris@216 286 *deflt = 0;
Chris@216 287
Chris@216 288 val = m_colourMap;
Chris@0 289
Chris@87 290 } else if (name == "Window Size") {
Chris@0 291
Chris@0 292 *min = 0;
Chris@0 293 *max = 10;
Chris@216 294 *deflt = 5;
Chris@0 295
Chris@216 296 val = 0;
Chris@0 297 int ws = m_windowSize;
Chris@216 298 while (ws > 32) { ws >>= 1; val ++; }
Chris@0 299
Chris@97 300 } else if (name == "Window Increment") {
Chris@0 301
Chris@0 302 *min = 0;
Chris@97 303 *max = 5;
Chris@216 304 *deflt = 2;
Chris@216 305
Chris@216 306 val = m_windowHopLevel;
Chris@0 307
Chris@87 308 } else if (name == "Min Frequency") {
Chris@37 309
Chris@37 310 *min = 0;
Chris@37 311 *max = 9;
Chris@216 312 *deflt = 1;
Chris@37 313
Chris@37 314 switch (m_minFrequency) {
Chris@216 315 case 0: default: val = 0; break;
Chris@216 316 case 10: val = 1; break;
Chris@216 317 case 20: val = 2; break;
Chris@216 318 case 40: val = 3; break;
Chris@216 319 case 100: val = 4; break;
Chris@216 320 case 250: val = 5; break;
Chris@216 321 case 500: val = 6; break;
Chris@216 322 case 1000: val = 7; break;
Chris@216 323 case 4000: val = 8; break;
Chris@216 324 case 10000: val = 9; break;
Chris@37 325 }
Chris@37 326
Chris@87 327 } else if (name == "Max Frequency") {
Chris@0 328
Chris@0 329 *min = 0;
Chris@0 330 *max = 9;
Chris@216 331 *deflt = 6;
Chris@0 332
Chris@0 333 switch (m_maxFrequency) {
Chris@216 334 case 500: val = 0; break;
Chris@216 335 case 1000: val = 1; break;
Chris@216 336 case 1500: val = 2; break;
Chris@216 337 case 2000: val = 3; break;
Chris@216 338 case 4000: val = 4; break;
Chris@216 339 case 6000: val = 5; break;
Chris@216 340 case 8000: val = 6; break;
Chris@216 341 case 12000: val = 7; break;
Chris@216 342 case 16000: val = 8; break;
Chris@216 343 default: val = 9; break;
Chris@0 344 }
Chris@0 345
Chris@87 346 } else if (name == "Frequency Scale") {
Chris@0 347
Chris@0 348 *min = 0;
Chris@0 349 *max = 1;
Chris@1093 350 *deflt = int(Colour3DPlotRenderer::LinearBinScale);
Chris@1093 351 val = (int)m_binScale;
Chris@0 352
Chris@87 353 } else if (name == "Bin Display") {
Chris@35 354
Chris@35 355 *min = 0;
Chris@35 356 *max = 2;
Chris@1093 357 *deflt = int(Colour3DPlotRenderer::AllBins);
Chris@216 358 val = (int)m_binDisplay;
Chris@35 359
Chris@862 360 } else if (name == "Normalization") {
Chris@36 361
Chris@862 362 *min = 0;
Chris@862 363 *max = 3;
Chris@1063 364 *deflt = int(ColumnOp::NoNormalization);
Chris@862 365 val = (int)m_normalization;
Chris@120 366
Chris@0 367 } else {
Chris@216 368 val = Layer::getPropertyRangeAndValue(name, min, max, deflt);
Chris@0 369 }
Chris@0 370
Chris@216 371 return val;
Chris@0 372 }
Chris@0 373
Chris@0 374 QString
Chris@0 375 SpectrogramLayer::getPropertyValueLabel(const PropertyName &name,
Chris@9 376 int value) const
Chris@0 377 {
Chris@87 378 if (name == "Colour") {
Chris@196 379 return ColourMapper::getColourMapName(value);
Chris@0 380 }
Chris@87 381 if (name == "Colour Scale") {
Chris@0 382 switch (value) {
Chris@0 383 default:
Chris@37 384 case 0: return tr("Linear");
Chris@37 385 case 1: return tr("Meter");
Chris@215 386 case 2: return tr("dBV^2");
Chris@215 387 case 3: return tr("dBV");
Chris@119 388 case 4: return tr("Phase");
Chris@0 389 }
Chris@0 390 }
Chris@862 391 if (name == "Normalization") {
Chris@862 392 return ""; // icon only
Chris@862 393 }
Chris@87 394 if (name == "Window Size") {
Chris@0 395 return QString("%1").arg(32 << value);
Chris@0 396 }
Chris@97 397 if (name == "Window Increment") {
Chris@0 398 switch (value) {
Chris@0 399 default:
Chris@112 400 case 0: return tr("None");
Chris@112 401 case 1: return tr("25 %");
Chris@112 402 case 2: return tr("50 %");
Chris@112 403 case 3: return tr("75 %");
Chris@112 404 case 4: return tr("87.5 %");
Chris@112 405 case 5: return tr("93.75 %");
Chris@0 406 }
Chris@0 407 }
Chris@87 408 if (name == "Min Frequency") {
Chris@37 409 switch (value) {
Chris@37 410 default:
Chris@38 411 case 0: return tr("No min");
Chris@37 412 case 1: return tr("10 Hz");
Chris@37 413 case 2: return tr("20 Hz");
Chris@37 414 case 3: return tr("40 Hz");
Chris@37 415 case 4: return tr("100 Hz");
Chris@37 416 case 5: return tr("250 Hz");
Chris@37 417 case 6: return tr("500 Hz");
Chris@37 418 case 7: return tr("1 KHz");
Chris@37 419 case 8: return tr("4 KHz");
Chris@37 420 case 9: return tr("10 KHz");
Chris@37 421 }
Chris@37 422 }
Chris@87 423 if (name == "Max Frequency") {
Chris@0 424 switch (value) {
Chris@0 425 default:
Chris@0 426 case 0: return tr("500 Hz");
Chris@0 427 case 1: return tr("1 KHz");
Chris@0 428 case 2: return tr("1.5 KHz");
Chris@0 429 case 3: return tr("2 KHz");
Chris@0 430 case 4: return tr("4 KHz");
Chris@0 431 case 5: return tr("6 KHz");
Chris@0 432 case 6: return tr("8 KHz");
Chris@0 433 case 7: return tr("12 KHz");
Chris@0 434 case 8: return tr("16 KHz");
Chris@38 435 case 9: return tr("No max");
Chris@0 436 }
Chris@0 437 }
Chris@87 438 if (name == "Frequency Scale") {
Chris@0 439 switch (value) {
Chris@0 440 default:
Chris@0 441 case 0: return tr("Linear");
Chris@0 442 case 1: return tr("Log");
Chris@0 443 }
Chris@0 444 }
Chris@87 445 if (name == "Bin Display") {
Chris@35 446 switch (value) {
Chris@35 447 default:
Chris@37 448 case 0: return tr("All Bins");
Chris@37 449 case 1: return tr("Peak Bins");
Chris@37 450 case 2: return tr("Frequencies");
Chris@35 451 }
Chris@35 452 }
Chris@0 453 return tr("<unknown>");
Chris@0 454 }
Chris@0 455
Chris@862 456 QString
Chris@862 457 SpectrogramLayer::getPropertyValueIconName(const PropertyName &name,
Chris@862 458 int value) const
Chris@862 459 {
Chris@862 460 if (name == "Normalization") {
Chris@862 461 switch(value) {
Chris@862 462 default:
Chris@862 463 case 0: return "normalise-none";
Chris@862 464 case 1: return "normalise-columns";
Chris@862 465 case 2: return "normalise";
Chris@862 466 case 3: return "normalise-hybrid";
Chris@862 467 }
Chris@862 468 }
Chris@862 469 return "";
Chris@862 470 }
Chris@862 471
Chris@167 472 RangeMapper *
Chris@167 473 SpectrogramLayer::getNewPropertyRangeMapper(const PropertyName &name) const
Chris@167 474 {
Chris@167 475 if (name == "Gain") {
Chris@167 476 return new LinearRangeMapper(-50, 50, -25, 25, tr("dB"));
Chris@167 477 }
Chris@167 478 if (name == "Threshold") {
Chris@167 479 return new LinearRangeMapper(-50, 0, -50, 0, tr("dB"));
Chris@167 480 }
Chris@167 481 return 0;
Chris@167 482 }
Chris@167 483
Chris@0 484 void
Chris@0 485 SpectrogramLayer::setProperty(const PropertyName &name, int value)
Chris@0 486 {
Chris@87 487 if (name == "Gain") {
Chris@906 488 setGain(float(pow(10, float(value)/20.0)));
Chris@87 489 } else if (name == "Threshold") {
Chris@37 490 if (value == -50) setThreshold(0.0);
Chris@906 491 else setThreshold(float(AudioLevel::dB_to_multiplier(value)));
Chris@87 492 } else if (name == "Colour Rotation") {
Chris@9 493 setColourRotation(value);
Chris@87 494 } else if (name == "Colour") {
Chris@197 495 setColourMap(value);
Chris@87 496 } else if (name == "Window Size") {
Chris@0 497 setWindowSize(32 << value);
Chris@97 498 } else if (name == "Window Increment") {
Chris@97 499 setWindowHopLevel(value);
Chris@87 500 } else if (name == "Min Frequency") {
Chris@37 501 switch (value) {
Chris@37 502 default:
Chris@37 503 case 0: setMinFrequency(0); break;
Chris@37 504 case 1: setMinFrequency(10); break;
Chris@37 505 case 2: setMinFrequency(20); break;
Chris@37 506 case 3: setMinFrequency(40); break;
Chris@37 507 case 4: setMinFrequency(100); break;
Chris@37 508 case 5: setMinFrequency(250); break;
Chris@37 509 case 6: setMinFrequency(500); break;
Chris@37 510 case 7: setMinFrequency(1000); break;
Chris@37 511 case 8: setMinFrequency(4000); break;
Chris@37 512 case 9: setMinFrequency(10000); break;
Chris@37 513 }
Chris@133 514 int vs = getCurrentVerticalZoomStep();
Chris@133 515 if (vs != m_lastEmittedZoomStep) {
Chris@133 516 emit verticalZoomChanged();
Chris@133 517 m_lastEmittedZoomStep = vs;
Chris@133 518 }
Chris@87 519 } else if (name == "Max Frequency") {
Chris@0 520 switch (value) {
Chris@0 521 case 0: setMaxFrequency(500); break;
Chris@0 522 case 1: setMaxFrequency(1000); break;
Chris@0 523 case 2: setMaxFrequency(1500); break;
Chris@0 524 case 3: setMaxFrequency(2000); break;
Chris@0 525 case 4: setMaxFrequency(4000); break;
Chris@0 526 case 5: setMaxFrequency(6000); break;
Chris@0 527 case 6: setMaxFrequency(8000); break;
Chris@0 528 case 7: setMaxFrequency(12000); break;
Chris@0 529 case 8: setMaxFrequency(16000); break;
Chris@0 530 default:
Chris@0 531 case 9: setMaxFrequency(0); break;
Chris@0 532 }
Chris@133 533 int vs = getCurrentVerticalZoomStep();
Chris@133 534 if (vs != m_lastEmittedZoomStep) {
Chris@133 535 emit verticalZoomChanged();
Chris@133 536 m_lastEmittedZoomStep = vs;
Chris@133 537 }
Chris@87 538 } else if (name == "Colour Scale") {
Chris@0 539 switch (value) {
Chris@0 540 default:
Chris@1092 541 case 0: setColourScale(ColourScale::LinearColourScale); break;
Chris@1092 542 case 1: setColourScale(ColourScale::MeterColourScale); break;
Chris@1092 543 case 2: setColourScale(ColourScale::LogColourScale); break; //!!! dB^2
Chris@1092 544 case 3: setColourScale(ColourScale::LogColourScale); break;
Chris@1092 545 case 4: setColourScale(ColourScale::PhaseColourScale); break;
Chris@0 546 }
Chris@87 547 } else if (name == "Frequency Scale") {
Chris@0 548 switch (value) {
Chris@0 549 default:
Chris@1093 550 case 0: setBinScale(Colour3DPlotRenderer::LinearBinScale); break;
Chris@1093 551 case 1: setBinScale(Colour3DPlotRenderer::LogBinScale); break;
Chris@0 552 }
Chris@87 553 } else if (name == "Bin Display") {
Chris@35 554 switch (value) {
Chris@35 555 default:
Chris@1093 556 case 0: setBinDisplay(Colour3DPlotRenderer::AllBins); break;
Chris@1093 557 case 1: setBinDisplay(Colour3DPlotRenderer::PeakBins); break;
Chris@1093 558 case 2: setBinDisplay(Colour3DPlotRenderer::PeakFrequencies); break;
Chris@35 559 }
Chris@862 560 } else if (name == "Normalization") {
Chris@862 561 switch (value) {
Chris@862 562 default:
Chris@1063 563 case 0: setNormalization(ColumnOp::NoNormalization); break;
Chris@1063 564 case 1: setNormalization(ColumnOp::NormalizeColumns); break;
Chris@1063 565 case 2: setNormalization(ColumnOp::NormalizeVisibleArea); break;
Chris@1063 566 case 3: setNormalization(ColumnOp::NormalizeHybrid); break;
Chris@862 567 }
Chris@0 568 }
Chris@0 569 }
Chris@0 570
Chris@0 571 void
Chris@478 572 SpectrogramLayer::invalidateImageCaches()
Chris@95 573 {
Chris@1044 574 #ifdef DEBUG_SPECTROGRAM
Chris@1044 575 cerr << "SpectrogramLayer::invalidateImageCaches called" << endl;
Chris@1044 576 #endif
Chris@478 577 for (ViewImageCache::iterator i = m_imageCaches.begin();
Chris@478 578 i != m_imageCaches.end(); ++i) {
Chris@1030 579 i->second.invalidate();
Chris@95 580 }
Chris@1089 581
Chris@1089 582 //!!!
Chris@1089 583 for (ViewRendererMap::iterator i = m_renderers.begin();
Chris@1089 584 i != m_renderers.end(); ++i) {
Chris@1089 585 delete i->second;
Chris@1089 586 }
Chris@1089 587 m_renderers.clear();
Chris@95 588 }
Chris@95 589
Chris@95 590 void
Chris@122 591 SpectrogramLayer::preferenceChanged(PropertyContainer::PropertyName name)
Chris@122 592 {
Chris@587 593 SVDEBUG << "SpectrogramLayer::preferenceChanged(" << name << ")" << endl;
Chris@122 594
Chris@122 595 if (name == "Window Type") {
Chris@122 596 setWindowType(Preferences::getInstance()->getWindowType());
Chris@122 597 return;
Chris@122 598 }
Chris@490 599 if (name == "Spectrogram Y Smoothing") {
Chris@1086 600 setWindowSize(m_windowSize);
Chris@490 601 invalidateImageCaches();
Chris@490 602 invalidateMagnitudes();
Chris@490 603 emit layerParametersChanged();
Chris@490 604 }
Chris@490 605 if (name == "Spectrogram X Smoothing") {
Chris@478 606 invalidateImageCaches();
Chris@122 607 invalidateMagnitudes();
Chris@122 608 emit layerParametersChanged();
Chris@122 609 }
Chris@122 610 if (name == "Tuning Frequency") {
Chris@122 611 emit layerParametersChanged();
Chris@122 612 }
Chris@122 613 }
Chris@122 614
Chris@122 615 void
Chris@0 616 SpectrogramLayer::setChannel(int ch)
Chris@0 617 {
Chris@0 618 if (m_channel == ch) return;
Chris@0 619
Chris@478 620 invalidateImageCaches();
Chris@0 621 m_channel = ch;
Chris@1088 622 invalidateFFTModel();
Chris@9 623
Chris@0 624 emit layerParametersChanged();
Chris@0 625 }
Chris@0 626
Chris@0 627 int
Chris@0 628 SpectrogramLayer::getChannel() const
Chris@0 629 {
Chris@0 630 return m_channel;
Chris@0 631 }
Chris@0 632
Chris@1086 633 int
Chris@1086 634 SpectrogramLayer::getFFTOversampling() const
Chris@1086 635 {
Chris@1093 636 if (m_binDisplay != Colour3DPlotRenderer::AllBins) {
Chris@1086 637 return 1;
Chris@1086 638 }
Chris@1086 639
Chris@1086 640 Preferences::SpectrogramSmoothing smoothing =
Chris@1086 641 Preferences::getInstance()->getSpectrogramSmoothing();
Chris@1086 642
Chris@1086 643 if (smoothing == Preferences::NoSpectrogramSmoothing ||
Chris@1086 644 smoothing == Preferences::SpectrogramInterpolated) {
Chris@1086 645 return 1;
Chris@1086 646 }
Chris@1086 647
Chris@1086 648 return 4;
Chris@1086 649 }
Chris@1086 650
Chris@1087 651 int
Chris@1087 652 SpectrogramLayer::getFFTSize() const
Chris@1087 653 {
Chris@1087 654 return m_windowSize * getFFTOversampling();
Chris@1087 655 }
Chris@1087 656
Chris@0 657 void
Chris@805 658 SpectrogramLayer::setWindowSize(int ws)
Chris@0 659 {
Chris@1087 660 if (m_windowSize == ws) return;
Chris@0 661
Chris@478 662 invalidateImageCaches();
Chris@0 663
Chris@0 664 m_windowSize = ws;
Chris@0 665
Chris@1088 666 invalidateFFTModel();
Chris@9 667
Chris@9 668 emit layerParametersChanged();
Chris@0 669 }
Chris@0 670
Chris@805 671 int
Chris@0 672 SpectrogramLayer::getWindowSize() const
Chris@0 673 {
Chris@0 674 return m_windowSize;
Chris@0 675 }
Chris@0 676
Chris@0 677 void
Chris@805 678 SpectrogramLayer::setWindowHopLevel(int v)
Chris@0 679 {
Chris@97 680 if (m_windowHopLevel == v) return;
Chris@0 681
Chris@478 682 invalidateImageCaches();
Chris@0 683
Chris@97 684 m_windowHopLevel = v;
Chris@0 685
Chris@1088 686 invalidateFFTModel();
Chris@9 687
Chris@9 688 emit layerParametersChanged();
Chris@9 689
Chris@110 690 // fillCache();
Chris@0 691 }
Chris@0 692
Chris@805 693 int
Chris@97 694 SpectrogramLayer::getWindowHopLevel() const
Chris@0 695 {
Chris@97 696 return m_windowHopLevel;
Chris@0 697 }
Chris@0 698
Chris@0 699 void
Chris@0 700 SpectrogramLayer::setWindowType(WindowType w)
Chris@0 701 {
Chris@0 702 if (m_windowType == w) return;
Chris@0 703
Chris@478 704 invalidateImageCaches();
Chris@0 705
Chris@0 706 m_windowType = w;
Chris@110 707
Chris@1088 708 invalidateFFTModel();
Chris@9 709
Chris@9 710 emit layerParametersChanged();
Chris@0 711 }
Chris@0 712
Chris@0 713 WindowType
Chris@0 714 SpectrogramLayer::getWindowType() const
Chris@0 715 {
Chris@0 716 return m_windowType;
Chris@0 717 }
Chris@0 718
Chris@0 719 void
Chris@0 720 SpectrogramLayer::setGain(float gain)
Chris@0 721 {
Chris@587 722 // SVDEBUG << "SpectrogramLayer::setGain(" << gain << ") (my gain is now "
Chris@585 723 // << m_gain << ")" << endl;
Chris@55 724
Chris@40 725 if (m_gain == gain) return;
Chris@0 726
Chris@478 727 invalidateImageCaches();
Chris@0 728
Chris@0 729 m_gain = gain;
Chris@0 730
Chris@9 731 emit layerParametersChanged();
Chris@0 732 }
Chris@0 733
Chris@0 734 float
Chris@0 735 SpectrogramLayer::getGain() const
Chris@0 736 {
Chris@0 737 return m_gain;
Chris@0 738 }
Chris@0 739
Chris@0 740 void
Chris@37 741 SpectrogramLayer::setThreshold(float threshold)
Chris@37 742 {
Chris@40 743 if (m_threshold == threshold) return;
Chris@37 744
Chris@478 745 invalidateImageCaches();
Chris@37 746
Chris@37 747 m_threshold = threshold;
Chris@37 748
Chris@37 749 emit layerParametersChanged();
Chris@37 750 }
Chris@37 751
Chris@37 752 float
Chris@37 753 SpectrogramLayer::getThreshold() const
Chris@37 754 {
Chris@37 755 return m_threshold;
Chris@37 756 }
Chris@37 757
Chris@37 758 void
Chris@805 759 SpectrogramLayer::setMinFrequency(int mf)
Chris@37 760 {
Chris@37 761 if (m_minFrequency == mf) return;
Chris@37 762
Chris@587 763 // SVDEBUG << "SpectrogramLayer::setMinFrequency: " << mf << endl;
Chris@187 764
Chris@478 765 invalidateImageCaches();
Chris@119 766 invalidateMagnitudes();
Chris@37 767
Chris@37 768 m_minFrequency = mf;
Chris@37 769
Chris@37 770 emit layerParametersChanged();
Chris@37 771 }
Chris@37 772
Chris@805 773 int
Chris@37 774 SpectrogramLayer::getMinFrequency() const
Chris@37 775 {
Chris@37 776 return m_minFrequency;
Chris@37 777 }
Chris@37 778
Chris@37 779 void
Chris@805 780 SpectrogramLayer::setMaxFrequency(int mf)
Chris@0 781 {
Chris@0 782 if (m_maxFrequency == mf) return;
Chris@0 783
Chris@587 784 // SVDEBUG << "SpectrogramLayer::setMaxFrequency: " << mf << endl;
Chris@187 785
Chris@478 786 invalidateImageCaches();
Chris@119 787 invalidateMagnitudes();
Chris@0 788
Chris@0 789 m_maxFrequency = mf;
Chris@0 790
Chris@9 791 emit layerParametersChanged();
Chris@0 792 }
Chris@0 793
Chris@805 794 int
Chris@0 795 SpectrogramLayer::getMaxFrequency() const
Chris@0 796 {
Chris@0 797 return m_maxFrequency;
Chris@0 798 }
Chris@0 799
Chris@0 800 void
Chris@9 801 SpectrogramLayer::setColourRotation(int r)
Chris@9 802 {
Chris@478 803 invalidateImageCaches();
Chris@9 804
Chris@9 805 if (r < 0) r = 0;
Chris@9 806 if (r > 256) r = 256;
Chris@9 807 int distance = r - m_colourRotation;
Chris@9 808
Chris@9 809 if (distance != 0) {
Chris@197 810 rotatePalette(-distance);
Chris@9 811 m_colourRotation = r;
Chris@9 812 }
Chris@9 813
Chris@9 814 emit layerParametersChanged();
Chris@9 815 }
Chris@9 816
Chris@9 817 void
Chris@1092 818 SpectrogramLayer::setColourScale(ColourScale::Scale colourScale)
Chris@0 819 {
Chris@0 820 if (m_colourScale == colourScale) return;
Chris@0 821
Chris@478 822 invalidateImageCaches();
Chris@0 823
Chris@0 824 m_colourScale = colourScale;
Chris@0 825
Chris@9 826 emit layerParametersChanged();
Chris@0 827 }
Chris@0 828
Chris@1092 829 ColourScale::Scale
Chris@0 830 SpectrogramLayer::getColourScale() const
Chris@0 831 {
Chris@0 832 return m_colourScale;
Chris@0 833 }
Chris@0 834
Chris@0 835 void
Chris@197 836 SpectrogramLayer::setColourMap(int map)
Chris@0 837 {
Chris@197 838 if (m_colourMap == map) return;
Chris@0 839
Chris@478 840 invalidateImageCaches();
Chris@0 841
Chris@197 842 m_colourMap = map;
Chris@197 843 initialisePalette();
Chris@9 844
Chris@0 845 emit layerParametersChanged();
Chris@0 846 }
Chris@0 847
Chris@196 848 int
Chris@197 849 SpectrogramLayer::getColourMap() const
Chris@0 850 {
Chris@197 851 return m_colourMap;
Chris@0 852 }
Chris@0 853
Chris@0 854 void
Chris@1093 855 SpectrogramLayer::setBinScale(Colour3DPlotRenderer::BinScale binScale)
Chris@0 856 {
Chris@1093 857 if (m_binScale == binScale) return;
Chris@0 858
Chris@478 859 invalidateImageCaches();
Chris@1093 860 m_binScale = binScale;
Chris@9 861
Chris@9 862 emit layerParametersChanged();
Chris@0 863 }
Chris@0 864
Chris@1093 865 Colour3DPlotRenderer::BinScale
Chris@1093 866 SpectrogramLayer::getBinScale() const
Chris@0 867 {
Chris@1093 868 return m_binScale;
Chris@0 869 }
Chris@0 870
Chris@0 871 void
Chris@1093 872 SpectrogramLayer::setBinDisplay(Colour3DPlotRenderer::BinDisplay binDisplay)
Chris@35 873 {
Chris@37 874 if (m_binDisplay == binDisplay) return;
Chris@35 875
Chris@478 876 invalidateImageCaches();
Chris@37 877 m_binDisplay = binDisplay;
Chris@35 878
Chris@35 879 emit layerParametersChanged();
Chris@35 880 }
Chris@35 881
Chris@1093 882 Colour3DPlotRenderer::BinDisplay
Chris@37 883 SpectrogramLayer::getBinDisplay() const
Chris@35 884 {
Chris@37 885 return m_binDisplay;
Chris@35 886 }
Chris@35 887
Chris@35 888 void
Chris@1063 889 SpectrogramLayer::setNormalization(ColumnOp::Normalization n)
Chris@36 890 {
Chris@862 891 if (m_normalization == n) return;
Chris@36 892
Chris@478 893 invalidateImageCaches();
Chris@119 894 invalidateMagnitudes();
Chris@862 895 m_normalization = n;
Chris@36 896
Chris@36 897 emit layerParametersChanged();
Chris@36 898 }
Chris@36 899
Chris@1063 900 ColumnOp::Normalization
Chris@862 901 SpectrogramLayer::getNormalization() const
Chris@36 902 {
Chris@862 903 return m_normalization;
Chris@36 904 }
Chris@36 905
Chris@36 906 void
Chris@918 907 SpectrogramLayer::setLayerDormant(const LayerGeometryProvider *v, bool dormant)
Chris@29 908 {
Chris@33 909 if (dormant) {
Chris@33 910
Chris@331 911 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@985 912 cerr << "SpectrogramLayer::setLayerDormant(" << dormant << ")"
Chris@585 913 << endl;
Chris@331 914 #endif
Chris@331 915
Chris@131 916 if (isLayerDormant(v)) {
Chris@131 917 return;
Chris@131 918 }
Chris@131 919
Chris@131 920 Layer::setLayerDormant(v, true);
Chris@33 921
Chris@920 922 const View *view = v->getView();
Chris@920 923
Chris@478 924 invalidateImageCaches();
Chris@988 925
Chris@1030 926 m_imageCaches.erase(view->getId());
Chris@1030 927
Chris@1088 928 //!!! in theory we should call invalidateFFTModel() if and
Chris@1088 929 //!!! only if there are no remaining views in which we are not
Chris@1088 930 //!!! dormant
Chris@33 931
Chris@33 932 } else {
Chris@33 933
Chris@131 934 Layer::setLayerDormant(v, false);
Chris@33 935 }
Chris@29 936 }
Chris@29 937
Chris@29 938 void
Chris@0 939 SpectrogramLayer::cacheInvalid()
Chris@0 940 {
Chris@391 941 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@985 942 cerr << "SpectrogramLayer::cacheInvalid()" << endl;
Chris@391 943 #endif
Chris@391 944
Chris@478 945 invalidateImageCaches();
Chris@119 946 invalidateMagnitudes();
Chris@0 947 }
Chris@0 948
Chris@0 949 void
Chris@1037 950 SpectrogramLayer::cacheInvalid(
Chris@1037 951 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@1037 952 sv_frame_t from, sv_frame_t to
Chris@1037 953 #else
Chris@1037 954 sv_frame_t , sv_frame_t
Chris@1037 955 #endif
Chris@1037 956 )
Chris@0 957 {
Chris@391 958 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@985 959 cerr << "SpectrogramLayer::cacheInvalid(" << from << ", " << to << ")" << endl;
Chris@391 960 #endif
Chris@391 961
Chris@1030 962 // We used to call invalidateMagnitudes(from, to) to invalidate
Chris@1030 963 // only those caches whose views contained some of the (from, to)
Chris@1030 964 // range. That's the right thing to do; it has been lost in
Chris@1030 965 // pulling out the image cache code, but it might not matter very
Chris@1030 966 // much, since the underlying models for spectrogram layers don't
Chris@1030 967 // change very often. Let's see.
Chris@1030 968 invalidateImageCaches();
Chris@391 969 invalidateMagnitudes();
Chris@0 970 }
Chris@0 971
Chris@224 972 bool
Chris@224 973 SpectrogramLayer::hasLightBackground() const
Chris@224 974 {
Chris@287 975 return ColourMapper(m_colourMap, 1.f, 255.f).hasLightBackground();
Chris@224 976 }
Chris@224 977
Chris@0 978 void
Chris@197 979 SpectrogramLayer::initialisePalette()
Chris@0 980 {
Chris@10 981 int formerRotation = m_colourRotation;
Chris@10 982
Chris@197 983 if (m_colourMap == (int)ColourMapper::BlackOnWhite) {
Chris@197 984 m_palette.setColour(NO_VALUE, Qt::white);
Chris@38 985 } else {
Chris@197 986 m_palette.setColour(NO_VALUE, Qt::black);
Chris@38 987 }
Chris@0 988
Chris@197 989 ColourMapper mapper(m_colourMap, 1.f, 255.f);
Chris@196 990
Chris@0 991 for (int pixel = 1; pixel < 256; ++pixel) {
Chris@907 992 m_palette.setColour((unsigned char)pixel, mapper.map(pixel));
Chris@0 993 }
Chris@9 994
Chris@196 995 m_crosshairColour = mapper.getContrastingColour();
Chris@196 996
Chris@9 997 m_colourRotation = 0;
Chris@197 998 rotatePalette(m_colourRotation - formerRotation);
Chris@10 999 m_colourRotation = formerRotation;
Chris@478 1000
Chris@478 1001 m_drawBuffer = QImage();
Chris@9 1002 }
Chris@9 1003
Chris@9 1004 void
Chris@197 1005 SpectrogramLayer::rotatePalette(int distance)
Chris@9 1006 {
Chris@31 1007 QColor newPixels[256];
Chris@9 1008
Chris@197 1009 newPixels[NO_VALUE] = m_palette.getColour(NO_VALUE);
Chris@9 1010
Chris@9 1011 for (int pixel = 1; pixel < 256; ++pixel) {
Chris@9 1012 int target = pixel + distance;
Chris@9 1013 while (target < 1) target += 255;
Chris@9 1014 while (target > 255) target -= 255;
Chris@907 1015 newPixels[target] = m_palette.getColour((unsigned char)pixel);
Chris@9 1016 }
Chris@9 1017
Chris@9 1018 for (int pixel = 0; pixel < 256; ++pixel) {
Chris@907 1019 m_palette.setColour((unsigned char)pixel, newPixels[pixel]);
Chris@9 1020 }
Chris@478 1021
Chris@478 1022 m_drawBuffer = QImage();
Chris@0 1023 }
Chris@0 1024
Chris@38 1025 unsigned char
Chris@918 1026 SpectrogramLayer::getDisplayValue(LayerGeometryProvider *v, double input) const
Chris@38 1027 {
Chris@1092 1028 int value = 0;
Chris@37 1029
Chris@907 1030 double min = 0.0;
Chris@907 1031 double max = 1.0;
Chris@120 1032
Chris@1063 1033 if (m_normalization == ColumnOp::NormalizeVisibleArea) {
Chris@1030 1034 min = m_viewMags[v->getId()].getMin();
Chris@1030 1035 max = m_viewMags[v->getId()].getMax();
Chris@1063 1036 } else if (m_normalization != ColumnOp::NormalizeColumns) {
Chris@1092 1037 if (m_colourScale == ColourScale::LinearColourScale //||
Chris@224 1038 // m_colourScale == MeterColourScale) {
Chris@224 1039 ) {
Chris@907 1040 max = 0.1;
Chris@120 1041 }
Chris@120 1042 }
Chris@120 1043
Chris@907 1044 double thresh = -80.0;
Chris@907 1045
Chris@907 1046 if (max == 0.0) max = 1.0;
Chris@907 1047 if (max == min) min = max - 0.0001;
Chris@119 1048
Chris@40 1049 switch (m_colourScale) {
Chris@40 1050
Chris@1092 1051 case ColourScale::LinearColourScale:
Chris@907 1052 value = int(((input - min) / (max - min)) * 255.0) + 1;
Chris@40 1053 break;
Chris@40 1054
Chris@1092 1055 case ColourScale::MeterColourScale:
Chris@210 1056 value = AudioLevel::multiplier_to_preview
Chris@210 1057 ((input - min) / (max - min), 254) + 1;
Chris@40 1058 break;
Chris@119 1059
Chris@1092 1060 //!!! check this
Chris@1092 1061 /* case dBSquaredColourScale:
Chris@215 1062 input = ((input - min) * (input - min)) / ((max - min) * (max - min));
Chris@907 1063 if (input > 0.0) {
Chris@907 1064 input = 10.0 * log10(input);
Chris@133 1065 } else {
Chris@133 1066 input = thresh;
Chris@133 1067 }
Chris@907 1068 if (min > 0.0) {
Chris@907 1069 thresh = 10.0 * log10(min * min);
Chris@907 1070 if (thresh < -80.0) thresh = -80.0;
Chris@119 1071 }
Chris@119 1072 input = (input - thresh) / (-thresh);
Chris@907 1073 if (input < 0.0) input = 0.0;
Chris@907 1074 if (input > 1.0) input = 1.0;
Chris@907 1075 value = int(input * 255.0) + 1;
Chris@119 1076 break;
Chris@1092 1077 */
Chris@1092 1078 case ColourScale::LogColourScale:
Chris@215 1079 //!!! experiment with normalizing the visible area this way.
Chris@215 1080 //In any case, we need to have some indication of what the dB
Chris@215 1081 //scale is relative to.
Chris@215 1082 input = (input - min) / (max - min);
Chris@907 1083 if (input > 0.0) {
Chris@907 1084 input = 10.0 * log10(input);
Chris@215 1085 } else {
Chris@215 1086 input = thresh;
Chris@215 1087 }
Chris@907 1088 if (min > 0.0) {
Chris@907 1089 thresh = 10.0 * log10(min);
Chris@907 1090 if (thresh < -80.0) thresh = -80.0;
Chris@215 1091 }
Chris@215 1092 input = (input - thresh) / (-thresh);
Chris@907 1093 if (input < 0.0) input = 0.0;
Chris@907 1094 if (input > 1.0) input = 1.0;
Chris@907 1095 value = int(input * 255.0) + 1;
Chris@215 1096 break;
Chris@215 1097
Chris@1092 1098 case ColourScale::PhaseColourScale:
Chris@40 1099 value = int((input * 127.0 / M_PI) + 128);
Chris@40 1100 break;
Chris@1092 1101
Chris@1092 1102 case ColourScale::PlusMinusOneScale:
Chris@1092 1103 case ColourScale::AbsoluteScale:
Chris@1092 1104 default:
Chris@1092 1105 ;
Chris@0 1106 }
Chris@210 1107
Chris@38 1108 if (value > UCHAR_MAX) value = UCHAR_MAX;
Chris@38 1109 if (value < 0) value = 0;
Chris@907 1110 return (unsigned char)value;
Chris@0 1111 }
Chris@0 1112
Chris@905 1113 double
Chris@40 1114 SpectrogramLayer::getEffectiveMinFrequency() const
Chris@40 1115 {
Chris@907 1116 sv_samplerate_t sr = m_model->getSampleRate();
Chris@1087 1117 double minf = double(sr) / getFFTSize();
Chris@40 1118
Chris@40 1119 if (m_minFrequency > 0.0) {
Chris@1087 1120 int minbin = int((double(m_minFrequency) * getFFTSize()) / sr + 0.01);
Chris@40 1121 if (minbin < 1) minbin = 1;
Chris@1087 1122 minf = minbin * sr / getFFTSize();
Chris@40 1123 }
Chris@40 1124
Chris@40 1125 return minf;
Chris@40 1126 }
Chris@40 1127
Chris@905 1128 double
Chris@40 1129 SpectrogramLayer::getEffectiveMaxFrequency() const
Chris@40 1130 {
Chris@907 1131 sv_samplerate_t sr = m_model->getSampleRate();
Chris@905 1132 double maxf = double(sr) / 2;
Chris@40 1133
Chris@40 1134 if (m_maxFrequency > 0.0) {
Chris@1087 1135 int maxbin = int((double(m_maxFrequency) * getFFTSize()) / sr + 0.1);
Chris@1087 1136 if (maxbin > getFFTSize() / 2) maxbin = getFFTSize() / 2;
Chris@1087 1137 maxf = maxbin * sr / getFFTSize();
Chris@40 1138 }
Chris@40 1139
Chris@40 1140 return maxf;
Chris@40 1141 }
Chris@40 1142
Chris@0 1143 bool
Chris@918 1144 SpectrogramLayer::getYBinRange(LayerGeometryProvider *v, int y, double &q0, double &q1) const
Chris@0 1145 {
Chris@382 1146 Profiler profiler("SpectrogramLayer::getYBinRange");
Chris@382 1147
Chris@918 1148 int h = v->getPaintHeight();
Chris@0 1149 if (y < 0 || y >= h) return false;
Chris@0 1150
Chris@907 1151 sv_samplerate_t sr = m_model->getSampleRate();
Chris@905 1152 double minf = getEffectiveMinFrequency();
Chris@905 1153 double maxf = getEffectiveMaxFrequency();
Chris@0 1154
Chris@1093 1155 bool logarithmic = (m_binScale == Colour3DPlotRenderer::LogBinScale);
Chris@38 1156
Chris@44 1157 q0 = v->getFrequencyForY(y, minf, maxf, logarithmic);
Chris@44 1158 q1 = v->getFrequencyForY(y - 1, minf, maxf, logarithmic);
Chris@38 1159
Chris@1086 1160 // Now map these on to ("proportions of") actual bins
Chris@490 1161
Chris@1087 1162 q0 = (q0 * getFFTSize()) / sr;
Chris@1087 1163 q1 = (q1 * getFFTSize()) / sr;
Chris@0 1164
Chris@0 1165 return true;
Chris@0 1166 }
Chris@486 1167
Chris@1085 1168 double
Chris@1090 1169 SpectrogramLayer::getYForBin(LayerGeometryProvider *, double) const {
Chris@1085 1170 //!!! not implemented
Chris@1085 1171 throw std::logic_error("not implemented");
Chris@1085 1172 }
Chris@1085 1173
Chris@1085 1174 double
Chris@1085 1175 SpectrogramLayer::getBinForY(LayerGeometryProvider *v, double y) const
Chris@1085 1176 {
Chris@1085 1177 //!!! overlap with range methods above (but using double arg)
Chris@1085 1178 //!!! tidy this
Chris@38 1179
Chris@1085 1180 int h = v->getPaintHeight();
Chris@1085 1181 if (y < 0 || y >= h) return false;
Chris@1085 1182
Chris@1085 1183 sv_samplerate_t sr = m_model->getSampleRate();
Chris@1085 1184 double minf = getEffectiveMinFrequency();
Chris@1085 1185 double maxf = getEffectiveMaxFrequency();
Chris@1085 1186
Chris@1093 1187 bool logarithmic = (m_binScale == Colour3DPlotRenderer::LogBinScale);
Chris@1085 1188
Chris@1085 1189 double q = v->getFrequencyForY(y, minf, maxf, logarithmic);
Chris@1085 1190
Chris@1086 1191 // Now map on to ("proportions of") actual bins
Chris@1085 1192
Chris@1087 1193 q = (q * getFFTSize()) / sr;
Chris@1085 1194
Chris@1085 1195 return q;
Chris@1085 1196 }
Chris@1085 1197
Chris@0 1198 bool
Chris@918 1199 SpectrogramLayer::getXBinRange(LayerGeometryProvider *v, int x, double &s0, double &s1) const
Chris@0 1200 {
Chris@907 1201 sv_frame_t modelStart = m_model->getStartFrame();
Chris@907 1202 sv_frame_t modelEnd = m_model->getEndFrame();
Chris@0 1203
Chris@0 1204 // Each pixel column covers an exact range of sample frames:
Chris@907 1205 sv_frame_t f0 = v->getFrameForX(x) - modelStart;
Chris@907 1206 sv_frame_t f1 = v->getFrameForX(x + 1) - modelStart - 1;
Chris@20 1207
Chris@41 1208 if (f1 < int(modelStart) || f0 > int(modelEnd)) {
Chris@41 1209 return false;
Chris@41 1210 }
Chris@20 1211
Chris@0 1212 // And that range may be drawn from a possibly non-integral
Chris@0 1213 // range of spectrogram windows:
Chris@0 1214
Chris@805 1215 int windowIncrement = getWindowIncrement();
Chris@905 1216 s0 = double(f0) / windowIncrement;
Chris@905 1217 s1 = double(f1) / windowIncrement;
Chris@0 1218
Chris@0 1219 return true;
Chris@0 1220 }
Chris@0 1221
Chris@0 1222 bool
Chris@918 1223 SpectrogramLayer::getXBinSourceRange(LayerGeometryProvider *v, int x, RealTime &min, RealTime &max) const
Chris@0 1224 {
Chris@905 1225 double s0 = 0, s1 = 0;
Chris@44 1226 if (!getXBinRange(v, x, s0, s1)) return false;
Chris@0 1227
Chris@0 1228 int s0i = int(s0 + 0.001);
Chris@0 1229 int s1i = int(s1);
Chris@0 1230
Chris@0 1231 int windowIncrement = getWindowIncrement();
Chris@0 1232 int w0 = s0i * windowIncrement - (m_windowSize - windowIncrement)/2;
Chris@0 1233 int w1 = s1i * windowIncrement + windowIncrement +
Chris@0 1234 (m_windowSize - windowIncrement)/2 - 1;
Chris@0 1235
Chris@0 1236 min = RealTime::frame2RealTime(w0, m_model->getSampleRate());
Chris@0 1237 max = RealTime::frame2RealTime(w1, m_model->getSampleRate());
Chris@0 1238 return true;
Chris@0 1239 }
Chris@0 1240
Chris@0 1241 bool
Chris@918 1242 SpectrogramLayer::getYBinSourceRange(LayerGeometryProvider *v, int y, double &freqMin, double &freqMax)
Chris@0 1243 const
Chris@0 1244 {
Chris@905 1245 double q0 = 0, q1 = 0;
Chris@44 1246 if (!getYBinRange(v, y, q0, q1)) return false;
Chris@0 1247
Chris@0 1248 int q0i = int(q0 + 0.001);
Chris@0 1249 int q1i = int(q1);
Chris@0 1250
Chris@907 1251 sv_samplerate_t sr = m_model->getSampleRate();
Chris@0 1252
Chris@0 1253 for (int q = q0i; q <= q1i; ++q) {
Chris@1087 1254 if (q == q0i) freqMin = (sr * q) / getFFTSize();
Chris@1087 1255 if (q == q1i) freqMax = (sr * (q+1)) / getFFTSize();
Chris@0 1256 }
Chris@0 1257 return true;
Chris@0 1258 }
Chris@35 1259
Chris@35 1260 bool
Chris@918 1261 SpectrogramLayer::getAdjustedYBinSourceRange(LayerGeometryProvider *v, int x, int y,
Chris@905 1262 double &freqMin, double &freqMax,
Chris@905 1263 double &adjFreqMin, double &adjFreqMax)
Chris@35 1264 const
Chris@35 1265 {
Chris@277 1266 if (!m_model || !m_model->isOK() || !m_model->isReady()) {
Chris@277 1267 return false;
Chris@277 1268 }
Chris@277 1269
Chris@1088 1270 FFTModel *fft = getFFTModel();
Chris@114 1271 if (!fft) return false;
Chris@110 1272
Chris@905 1273 double s0 = 0, s1 = 0;
Chris@44 1274 if (!getXBinRange(v, x, s0, s1)) return false;
Chris@35 1275
Chris@905 1276 double q0 = 0, q1 = 0;
Chris@44 1277 if (!getYBinRange(v, y, q0, q1)) return false;
Chris@35 1278
Chris@35 1279 int s0i = int(s0 + 0.001);
Chris@35 1280 int s1i = int(s1);
Chris@35 1281
Chris@35 1282 int q0i = int(q0 + 0.001);
Chris@35 1283 int q1i = int(q1);
Chris@35 1284
Chris@907 1285 sv_samplerate_t sr = m_model->getSampleRate();
Chris@35 1286
Chris@35 1287 bool haveAdj = false;
Chris@35 1288
Chris@1093 1289 bool peaksOnly = (m_binDisplay == Colour3DPlotRenderer::PeakBins ||
Chris@1093 1290 m_binDisplay == Colour3DPlotRenderer::PeakFrequencies);
Chris@37 1291
Chris@35 1292 for (int q = q0i; q <= q1i; ++q) {
Chris@35 1293
Chris@35 1294 for (int s = s0i; s <= s1i; ++s) {
Chris@35 1295
Chris@905 1296 double binfreq = (double(sr) * q) / m_windowSize;
Chris@35 1297 if (q == q0i) freqMin = binfreq;
Chris@35 1298 if (q == q1i) freqMax = binfreq;
Chris@37 1299
Chris@114 1300 if (peaksOnly && !fft->isLocalPeak(s, q)) continue;
Chris@38 1301
Chris@1086 1302 if (!fft->isOverThreshold
Chris@1087 1303 (s, q, float(m_threshold * double(getFFTSize())/2.0))) {
Chris@1086 1304 continue;
Chris@1086 1305 }
Chris@907 1306
Chris@907 1307 double freq = binfreq;
Chris@40 1308
Chris@114 1309 if (s < int(fft->getWidth()) - 1) {
Chris@38 1310
Chris@277 1311 fft->estimateStableFrequency(s, q, freq);
Chris@35 1312
Chris@38 1313 if (!haveAdj || freq < adjFreqMin) adjFreqMin = freq;
Chris@38 1314 if (!haveAdj || freq > adjFreqMax) adjFreqMax = freq;
Chris@35 1315
Chris@35 1316 haveAdj = true;
Chris@35 1317 }
Chris@35 1318 }
Chris@35 1319 }
Chris@35 1320
Chris@35 1321 if (!haveAdj) {
Chris@40 1322 adjFreqMin = adjFreqMax = 0.0;
Chris@35 1323 }
Chris@35 1324
Chris@35 1325 return haveAdj;
Chris@35 1326 }
Chris@0 1327
Chris@0 1328 bool
Chris@918 1329 SpectrogramLayer::getXYBinSourceRange(LayerGeometryProvider *v, int x, int y,
Chris@905 1330 double &min, double &max,
Chris@905 1331 double &phaseMin, double &phaseMax) const
Chris@0 1332 {
Chris@277 1333 if (!m_model || !m_model->isOK() || !m_model->isReady()) {
Chris@277 1334 return false;
Chris@277 1335 }
Chris@277 1336
Chris@905 1337 double q0 = 0, q1 = 0;
Chris@44 1338 if (!getYBinRange(v, y, q0, q1)) return false;
Chris@0 1339
Chris@905 1340 double s0 = 0, s1 = 0;
Chris@44 1341 if (!getXBinRange(v, x, s0, s1)) return false;
Chris@0 1342
Chris@0 1343 int q0i = int(q0 + 0.001);
Chris@0 1344 int q1i = int(q1);
Chris@0 1345
Chris@0 1346 int s0i = int(s0 + 0.001);
Chris@0 1347 int s1i = int(s1);
Chris@0 1348
Chris@37 1349 bool rv = false;
Chris@37 1350
Chris@1088 1351 FFTModel *fft = getFFTModel();
Chris@0 1352
Chris@114 1353 if (fft) {
Chris@114 1354
Chris@114 1355 int cw = fft->getWidth();
Chris@114 1356 int ch = fft->getHeight();
Chris@0 1357
Chris@110 1358 min = 0.0;
Chris@110 1359 max = 0.0;
Chris@110 1360 phaseMin = 0.0;
Chris@110 1361 phaseMax = 0.0;
Chris@110 1362 bool have = false;
Chris@0 1363
Chris@110 1364 for (int q = q0i; q <= q1i; ++q) {
Chris@110 1365 for (int s = s0i; s <= s1i; ++s) {
Chris@110 1366 if (s >= 0 && q >= 0 && s < cw && q < ch) {
Chris@117 1367
Chris@905 1368 double value;
Chris@38 1369
Chris@114 1370 value = fft->getPhaseAt(s, q);
Chris@110 1371 if (!have || value < phaseMin) { phaseMin = value; }
Chris@110 1372 if (!have || value > phaseMax) { phaseMax = value; }
Chris@91 1373
Chris@1087 1374 value = fft->getMagnitudeAt(s, q) / (getFFTSize()/2.0);
Chris@110 1375 if (!have || value < min) { min = value; }
Chris@110 1376 if (!have || value > max) { max = value; }
Chris@110 1377
Chris@110 1378 have = true;
Chris@110 1379 }
Chris@110 1380 }
Chris@110 1381 }
Chris@110 1382
Chris@110 1383 if (have) {
Chris@110 1384 rv = true;
Chris@110 1385 }
Chris@0 1386 }
Chris@0 1387
Chris@37 1388 return rv;
Chris@0 1389 }
Chris@114 1390
Chris@130 1391 FFTModel *
Chris@1088 1392 SpectrogramLayer::getFFTModel() const
Chris@114 1393 {
Chris@114 1394 if (!m_model) return 0;
Chris@114 1395
Chris@1087 1396 int fftSize = getFFTSize();
Chris@114 1397
Chris@1088 1398 //!!! it is now surely slower to do this on every getFFTModel()
Chris@1088 1399 //!!! request than it would be to recreate the model immediately
Chris@1088 1400 //!!! when something changes instead of just invalidating it
Chris@920 1401
Chris@1088 1402 if (m_fftModel &&
Chris@1088 1403 m_fftModel->getHeight() == fftSize / 2 + 1 &&
Chris@1088 1404 m_fftModel->getWindowIncrement() == getWindowIncrement()) {
Chris@1088 1405 return m_fftModel;
Chris@114 1406 }
Chris@1088 1407
Chris@1088 1408 delete m_peakCache;
Chris@1088 1409 m_peakCache = 0;
Chris@1088 1410
Chris@1088 1411 delete m_fftModel;
Chris@1088 1412 m_fftModel = new FFTModel(m_model,
Chris@1088 1413 m_channel,
Chris@1088 1414 m_windowType,
Chris@1088 1415 m_windowSize,
Chris@1088 1416 getWindowIncrement(),
Chris@1088 1417 fftSize);
Chris@1088 1418
Chris@1088 1419 if (!m_fftModel->isOK()) {
Chris@1088 1420 QMessageBox::critical
Chris@1088 1421 (0, tr("FFT cache failed"),
Chris@1088 1422 tr("Failed to create the FFT model for this spectrogram.\n"
Chris@1088 1423 "There may be insufficient memory or disc space to continue."));
Chris@1088 1424 delete m_fftModel;
Chris@1088 1425 m_fftModel = 0;
Chris@1088 1426 return 0;
Chris@114 1427 }
Chris@114 1428
Chris@1088 1429 ((SpectrogramLayer *)this)->sliceableModelReplaced(0, m_fftModel);
Chris@1088 1430
Chris@1088 1431 return m_fftModel;
Chris@114 1432 }
Chris@114 1433
Chris@484 1434 Dense3DModelPeakCache *
Chris@1088 1435 SpectrogramLayer::getPeakCache() const
Chris@484 1436 {
Chris@1088 1437 //!!! see comment in getFFTModel
Chris@1088 1438
Chris@1088 1439 if (!m_peakCache) {
Chris@1088 1440 FFTModel *f = getFFTModel();
Chris@484 1441 if (!f) return 0;
Chris@1088 1442 m_peakCache = new Dense3DModelPeakCache(f, m_peakCacheDivisor);
Chris@484 1443 }
Chris@1088 1444 return m_peakCache;
Chris@484 1445 }
Chris@484 1446
Chris@193 1447 const Model *
Chris@193 1448 SpectrogramLayer::getSliceableModel() const
Chris@193 1449 {
Chris@1088 1450 return m_fftModel;
Chris@193 1451 }
Chris@193 1452
Chris@114 1453 void
Chris@1088 1454 SpectrogramLayer::invalidateFFTModel()
Chris@114 1455 {
Chris@1044 1456 #ifdef DEBUG_SPECTROGRAM
Chris@1088 1457 cerr << "SpectrogramLayer::invalidateFFTModel called" << endl;
Chris@1044 1458 #endif
Chris@1088 1459
Chris@1088 1460 emit sliceableModelReplaced(m_fftModel, 0);
Chris@1088 1461
Chris@1088 1462 delete m_fftModel;
Chris@1088 1463 delete m_peakCache;
Chris@1088 1464
Chris@1088 1465 m_fftModel = 0;
Chris@1088 1466 m_peakCache = 0;
Chris@114 1467 }
Chris@114 1468
Chris@0 1469 void
Chris@119 1470 SpectrogramLayer::invalidateMagnitudes()
Chris@119 1471 {
Chris@1044 1472 #ifdef DEBUG_SPECTROGRAM
Chris@1044 1473 cerr << "SpectrogramLayer::invalidateMagnitudes called" << endl;
Chris@1044 1474 #endif
Chris@119 1475 m_viewMags.clear();
Chris@1025 1476 for (vector<MagnitudeRange>::iterator i = m_columnMags.begin();
Chris@119 1477 i != m_columnMags.end(); ++i) {
Chris@119 1478 *i = MagnitudeRange();
Chris@119 1479 }
Chris@119 1480 }
Chris@119 1481
Chris@119 1482 bool
Chris@918 1483 SpectrogramLayer::updateViewMagnitudes(LayerGeometryProvider *v) const
Chris@119 1484 {
Chris@119 1485 MagnitudeRange mag;
Chris@119 1486
Chris@918 1487 int x0 = 0, x1 = v->getPaintWidth();
Chris@905 1488 double s00 = 0, s01 = 0, s10 = 0, s11 = 0;
Chris@119 1489
Chris@203 1490 if (!getXBinRange(v, x0, s00, s01)) {
Chris@907 1491 s00 = s01 = double(m_model->getStartFrame()) / getWindowIncrement();
Chris@203 1492 }
Chris@203 1493
Chris@203 1494 if (!getXBinRange(v, x1, s10, s11)) {
Chris@907 1495 s10 = s11 = double(m_model->getEndFrame()) / getWindowIncrement();
Chris@203 1496 }
Chris@119 1497
Chris@1025 1498 int s0 = int(min(s00, s10) + 0.0001);
Chris@1025 1499 int s1 = int(max(s01, s11) + 0.0001);
Chris@203 1500
Chris@587 1501 // SVDEBUG << "SpectrogramLayer::updateViewMagnitudes: x0 = " << x0 << ", x1 = " << x1 << ", s00 = " << s00 << ", s11 = " << s11 << " s0 = " << s0 << ", s1 = " << s1 << endl;
Chris@119 1502
Chris@248 1503 if (int(m_columnMags.size()) <= s1) {
Chris@119 1504 m_columnMags.resize(s1 + 1);
Chris@119 1505 }
Chris@119 1506
Chris@119 1507 for (int s = s0; s <= s1; ++s) {
Chris@119 1508 if (m_columnMags[s].isSet()) {
Chris@119 1509 mag.sample(m_columnMags[s]);
Chris@119 1510 }
Chris@119 1511 }
Chris@119 1512
Chris@184 1513 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@985 1514 cerr << "SpectrogramLayer::updateViewMagnitudes returning from cols "
Chris@1044 1515 << s0 << " -> " << s1 << " inclusive" << endl;
Chris@1044 1516 cerr << "SpectrogramLayer::updateViewMagnitudes: for view id " << v->getId()
Chris@1044 1517 << ": min is " << mag.getMin() << ", max is " << mag.getMax() << endl;
Chris@184 1518 #endif
Chris@119 1519
Chris@119 1520 if (!mag.isSet()) return false;
Chris@1030 1521 if (mag == m_viewMags[v->getId()]) return false;
Chris@1030 1522 m_viewMags[v->getId()] = mag;
Chris@119 1523 return true;
Chris@119 1524 }
Chris@119 1525
Chris@119 1526 void
Chris@389 1527 SpectrogramLayer::setSynchronousPainting(bool synchronous)
Chris@389 1528 {
Chris@389 1529 m_synchronous = synchronous;
Chris@389 1530 }
Chris@389 1531
Chris@1030 1532 ScrollableImageCache &
Chris@1030 1533 SpectrogramLayer::getImageCacheReference(const LayerGeometryProvider *view) const
Chris@1030 1534 {
Chris@1101 1535 //!!! to go?
Chris@1030 1536 if (m_imageCaches.find(view->getId()) == m_imageCaches.end()) {
Chris@1090 1537 m_imageCaches[view->getId()] = ScrollableImageCache();
Chris@1030 1538 }
Chris@1030 1539 return m_imageCaches.at(view->getId());
Chris@1030 1540 }
Chris@1030 1541
Chris@389 1542 void
Chris@1089 1543 SpectrogramLayer::paintAlternative(LayerGeometryProvider *v, QPainter &paint, QRect rect) const
Chris@1089 1544 {
Chris@1093 1545 static int depth = 0;
Chris@1093 1546
Chris@1089 1547 Colour3DPlotRenderer *renderer = getRenderer(v);
Chris@1089 1548
Chris@1091 1549 if (m_synchronous) {
Chris@1091 1550 (void)renderer->render(v, paint, rect);
Chris@1091 1551 return;
Chris@1091 1552 }
Chris@1093 1553
Chris@1093 1554 ++depth;
Chris@1093 1555 cerr << "paint depth " << depth << endl;
Chris@1091 1556
Chris@1098 1557 (void)renderer->renderTimeConstrained(v, paint, rect);
Chris@1091 1558
Chris@1091 1559 //!!! + mag range
Chris@1091 1560
Chris@1096 1561 QRect uncached = renderer->getLargestUncachedRect();
Chris@1096 1562 if (uncached.width() > 0) {
Chris@1096 1563 cerr << "updating rect at " << uncached.x() << " width "
Chris@1096 1564 << uncached.width() << endl;
Chris@1096 1565 v->updatePaintRect(uncached);
Chris@1093 1566 }
Chris@1093 1567
Chris@1093 1568 cerr << "exiting paint depth " << depth << endl;
Chris@1093 1569 --depth;
Chris@1089 1570 }
Chris@1089 1571
Chris@1089 1572 Colour3DPlotRenderer *
Chris@1089 1573 SpectrogramLayer::getRenderer(LayerGeometryProvider *v) const
Chris@1089 1574 {
Chris@1089 1575 if (m_renderers.find(v->getId()) == m_renderers.end()) {
Chris@1089 1576
Chris@1089 1577 Colour3DPlotRenderer::Sources sources;
Chris@1089 1578 sources.verticalBinLayer = this;
Chris@1090 1579 sources.fft = getFFTModel();
Chris@1090 1580 sources.source = sources.fft;
Chris@1090 1581 sources.peaks = getPeakCache();
Chris@1089 1582
Chris@1092 1583 ColourScale::Parameters cparams;
Chris@1092 1584 cparams.colourMap = m_colourMap;
Chris@1092 1585 cparams.scale = m_colourScale;
Chris@1093 1586 cparams.threshold = m_threshold;
Chris@1093 1587 cparams.gain = m_gain;
Chris@1093 1588
Chris@1093 1589 if (m_colourScale != ColourScale::PhaseColourScale &&
Chris@1093 1590 m_normalization == ColumnOp::NoNormalization) {
Chris@1093 1591 cparams.gain *= 2.f / float(getFFTSize());
Chris@1093 1592 }
Chris@1089 1593
Chris@1089 1594 Colour3DPlotRenderer::Parameters params;
Chris@1092 1595 params.colourScale = ColourScale(cparams);
Chris@1089 1596 params.normalization = m_normalization;
Chris@1093 1597 params.binDisplay = m_binDisplay;
Chris@1093 1598 params.binScale = m_binScale;
Chris@1093 1599 params.alwaysOpaque = true;
Chris@1093 1600 params.invertVertical = false;
Chris@1093 1601
Chris@1093 1602 Preferences::SpectrogramSmoothing smoothing =
Chris@1093 1603 Preferences::getInstance()->getSpectrogramSmoothing();
Chris@1093 1604 params.interpolate =
Chris@1093 1605 (smoothing == Preferences::SpectrogramInterpolated ||
Chris@1093 1606 smoothing == Preferences::SpectrogramZeroPaddedAndInterpolated);
Chris@1089 1607
Chris@1089 1608 m_renderers[v->getId()] = new Colour3DPlotRenderer(sources, params);
Chris@1089 1609 }
Chris@1089 1610
Chris@1089 1611 return m_renderers[v->getId()];
Chris@1089 1612 }
Chris@1089 1613
Chris@1089 1614 void
Chris@916 1615 SpectrogramLayer::paint(LayerGeometryProvider *v, QPainter &paint, QRect rect) const
Chris@0 1616 {
Chris@334 1617 Profiler profiler("SpectrogramLayer::paint", false);
Chris@334 1618
Chris@0 1619 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@1026 1620 cerr << "SpectrogramLayer::paint() entering: m_model is " << m_model << ", zoom level is " << v->getZoomLevel() << endl;
Chris@95 1621
Chris@1026 1622 cerr << "SpectrogramLayer::paint(): rect is " << rect.x() << "," << rect.y() << " " << rect.width() << "x" << rect.height() << endl;
Chris@0 1623 #endif
Chris@95 1624
Chris@907 1625 sv_frame_t startFrame = v->getStartFrame();
Chris@44 1626
Chris@0 1627 if (!m_model || !m_model->isOK() || !m_model->isReady()) {
Chris@0 1628 return;
Chris@0 1629 }
Chris@0 1630
Chris@47 1631 if (isLayerDormant(v)) {
Chris@587 1632 SVDEBUG << "SpectrogramLayer::paint(): Layer is dormant, making it undormant again" << endl;
Chris@29 1633 }
Chris@29 1634
Chris@1090 1635 paintAlternative(v, paint, rect);
Chris@1090 1636 return;
Chris@1089 1637
Chris@1089 1638 //!!!
Chris@1089 1639
Chris@48 1640 // Need to do this even if !isLayerDormant, as that could mean v
Chris@48 1641 // is not in the dormancy map at all -- we need it to be present
Chris@48 1642 // and accountable for when determining whether we need the cache
Chris@48 1643 // in the cache-fill thread above.
Chris@806 1644 //!!! no inter use cache-fill thread
Chris@131 1645 const_cast<SpectrogramLayer *>(this)->Layer::setLayerDormant(v, false);
Chris@48 1646
Chris@1087 1647 int fftSize = getFFTSize();
Chris@920 1648
Chris@920 1649 const View *view = v->getView();
Chris@1030 1650 ScrollableImageCache &cache = getImageCacheReference(view);
Chris@95 1651
Chris@95 1652 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@1030 1653 cerr << "SpectrogramLayer::paint(): image cache valid area from " << cache.getValidLeft() << " width " << cache.getValidWidth() << ", height " << cache.getSize().height() << endl;
Chris@1030 1654 if (rect.x() + rect.width() + 1 < cache.getValidLeft() ||
Chris@1030 1655 rect.x() > cache.getValidRight()) {
Chris@1030 1656 cerr << "SpectrogramLayer: NOTE: requested rect is not contiguous with cache valid area" << endl;
Chris@1030 1657 }
Chris@95 1658 #endif
Chris@95 1659
Chris@44 1660 int zoomLevel = v->getZoomLevel();
Chris@0 1661
Chris@1030 1662 int x0 = v->getXForViewX(rect.x());
Chris@1030 1663 int x1 = v->getXForViewX(rect.x() + rect.width());
Chris@1030 1664 if (x0 < 0) x0 = 0;
Chris@1030 1665 if (x1 > v->getPaintWidth()) x1 = v->getPaintWidth();
Chris@1022 1666
Chris@1022 1667 if (updateViewMagnitudes(v)) {
Chris@1022 1668 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@1030 1669 cerr << "SpectrogramLayer: magnitude range changed to [" << m_viewMags[v->getId()].getMin() << "->" << m_viewMags[v->getId()].getMax() << "]" << endl;
Chris@1022 1670 #endif
Chris@1063 1671 if (m_normalization == ColumnOp::NormalizeVisibleArea) {
Chris@1030 1672 cache.invalidate();
Chris@1022 1673 }
Chris@1022 1674 }
Chris@1030 1675
Chris@1030 1676 if (cache.getZoomLevel() != zoomLevel ||
Chris@1030 1677 cache.getSize() != v->getPaintSize()) {
Chris@1031 1678 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@1031 1679 cerr << "SpectrogramLayer: resizing image cache from "
Chris@1031 1680 << cache.getSize().width() << "x" << cache.getSize().height()
Chris@1031 1681 << " to "
Chris@1031 1682 << v->getPaintSize().width() << "x" << v->getPaintSize().height()
Chris@1031 1683 << " and updating zoom level from " << cache.getZoomLevel()
Chris@1031 1684 << " to " << zoomLevel
Chris@1031 1685 << endl;
Chris@1031 1686 #endif
Chris@1030 1687 cache.resize(v->getPaintSize());
Chris@1031 1688 cache.setZoomLevel(zoomLevel);
Chris@1031 1689 cache.setStartFrame(startFrame);
Chris@1030 1690 }
Chris@1023 1691
Chris@1030 1692 if (cache.isValid()) {
Chris@482 1693
Chris@1030 1694 if (v->getXForFrame(cache.getStartFrame()) ==
Chris@1030 1695 v->getXForFrame(startFrame) &&
Chris@1030 1696 cache.getValidLeft() <= x0 &&
Chris@1030 1697 cache.getValidRight() >= x1) {
Chris@1022 1698
Chris@0 1699 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@1030 1700 cerr << "SpectrogramLayer: image cache hit!" << endl;
Chris@0 1701 #endif
Chris@0 1702
Chris@1030 1703 paint.drawImage(rect, cache.getImage(), rect);
Chris@1030 1704
Chris@1030 1705 illuminateLocalFeatures(v, paint);
Chris@1030 1706 return;
Chris@1030 1707
Chris@1030 1708 } else {
Chris@1030 1709
Chris@1030 1710 // cache doesn't begin at the right frame or doesn't
Chris@1030 1711 // contain the complete view, but might be scrollable or
Chris@1030 1712 // partially usable
Chris@1022 1713
Chris@0 1714 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@1030 1715 cerr << "SpectrogramLayer: scrolling the image cache if applicable" << endl;
Chris@0 1716 #endif
Chris@0 1717
Chris@1090 1718 cache.scrollTo(v, startFrame);
Chris@1030 1719
Chris@0 1720 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@1031 1721 cerr << "SpectrogramLayer: after scrolling, cache valid from "
Chris@1030 1722 << cache.getValidLeft() << " width " << cache.getValidWidth()
Chris@1030 1723 << endl;
Chris@0 1724 #endif
Chris@1030 1725 }
Chris@0 1726 }
Chris@95 1727
Chris@1030 1728 bool rightToLeft = false;
Chris@1030 1729
Chris@1030 1730 if (!cache.isValid()) {
Chris@1028 1731 if (!m_synchronous) {
Chris@1028 1732 // When rendering the whole thing, start from somewhere near
Chris@1028 1733 // the middle so that the region of interest appears first
Chris@1038 1734
Chris@1038 1735 //!!! (perhaps we should have some cunning test to avoid
Chris@1038 1736 //!!! doing this if past repaints have appeared fast
Chris@1038 1737 //!!! enough to do the whole width in one shot)
Chris@1030 1738 if (x0 == 0 && x1 == v->getPaintWidth()) {
Chris@1037 1739 x0 = int(x1 * 0.3);
Chris@1030 1740 }
Chris@1028 1741 }
Chris@1030 1742 } else {
Chris@1030 1743 // When rendering only a part of the cache, we need to make
Chris@1030 1744 // sure that the part we're rendering is adjacent to (or
Chris@1030 1745 // overlapping) a valid area of cache, if we have one. The
Chris@1030 1746 // alternative is to ditch the valid area of cache and render
Chris@1030 1747 // only the requested area, but that's risky because this can
Chris@1030 1748 // happen when just waving the pointer over a small part of
Chris@1030 1749 // the view -- if we lose the partly-built cache every time
Chris@1030 1750 // the user does that, we'll never finish building it.
Chris@1030 1751 int left = x0;
Chris@1030 1752 int width = x1 - x0;
Chris@1030 1753 bool isLeftOfValidArea = false;
Chris@1031 1754 cache.adjustToTouchValidArea(left, width, isLeftOfValidArea);
Chris@1030 1755 x0 = left;
Chris@1030 1756 x1 = x0 + width;
Chris@1030 1757
Chris@1030 1758 // That call also told us whether we should be painting
Chris@1030 1759 // sub-regions of our target region in right-to-left order in
Chris@1030 1760 // order to ensure contiguity
Chris@1030 1761 rightToLeft = isLeftOfValidArea;
Chris@95 1762 }
Chris@1030 1763
Chris@224 1764 // We always paint the full height when refreshing the cache.
Chris@224 1765 // Smaller heights can be used when painting direct from cache
Chris@224 1766 // (further up in this function), but we want to ensure the cache
Chris@224 1767 // is coherent without having to worry about vertical matching of
Chris@224 1768 // required and valid areas as well as horizontal.
Chris@918 1769 int h = v->getPaintHeight();
Chris@1025 1770
Chris@1024 1771 int repaintWidth = x1 - x0;
Chris@0 1772
Chris@95 1773 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@1026 1774 cerr << "SpectrogramLayer: x0 " << x0 << ", x1 " << x1
Chris@1026 1775 << ", repaintWidth " << repaintWidth << ", h " << h
Chris@1030 1776 << ", rightToLeft " << rightToLeft << endl;
Chris@95 1777 #endif
Chris@95 1778
Chris@907 1779 sv_samplerate_t sr = m_model->getSampleRate();
Chris@122 1780
Chris@122 1781 // Set minFreq and maxFreq to the frequency extents of the possibly
Chris@122 1782 // zero-padded visible bin range, and displayMinFreq and displayMaxFreq
Chris@122 1783 // to the actual scale frequency extents (presumably not zero padded).
Chris@253 1784
Chris@1086 1785 // If we are zero padding (i.e. oversampling) we want to use the
Chris@1086 1786 // zero-padded equivalents of the bins that we would be using if
Chris@1086 1787 // not zero padded, to avoid spaces at the top and bottom of the
Chris@1086 1788 // display.
Chris@35 1789
Chris@1087 1790 int maxbin = fftSize / 2;
Chris@35 1791 if (m_maxFrequency > 0) {
Chris@1087 1792 maxbin = int((double(m_maxFrequency) * fftSize) / sr + 0.001);
Chris@1087 1793 if (maxbin > fftSize / 2) maxbin = fftSize / 2;
Chris@35 1794 }
Chris@111 1795
Chris@805 1796 int minbin = 1;
Chris@37 1797 if (m_minFrequency > 0) {
Chris@1087 1798 minbin = int((double(m_minFrequency) * fftSize) / sr + 0.001);
Chris@682 1799 // cerr << "m_minFrequency = " << m_minFrequency << " -> minbin = " << minbin << endl;
Chris@40 1800 if (minbin < 1) minbin = 1;
Chris@184 1801 if (minbin >= maxbin) minbin = maxbin - 1;
Chris@37 1802 }
Chris@37 1803
Chris@1086 1804 int over = getFFTOversampling();
Chris@1086 1805 minbin = minbin * over;
Chris@1086 1806 maxbin = (maxbin + 1) * over - 1;
Chris@253 1807
Chris@905 1808 double minFreq = (double(minbin) * sr) / fftSize;
Chris@905 1809 double maxFreq = (double(maxbin) * sr) / fftSize;
Chris@905 1810
Chris@905 1811 double displayMinFreq = minFreq;
Chris@905 1812 double displayMaxFreq = maxFreq;
Chris@122 1813
Chris@1087 1814 //!!! if (fftSize != getFFTSize()) {
Chris@1087 1815 // displayMinFreq = getEffectiveMinFrequency();
Chris@1087 1816 // displayMaxFreq = getEffectiveMaxFrequency();
Chris@1087 1817 // }
Chris@122 1818
Chris@682 1819 // cerr << "(giving actual minFreq " << minFreq << " and display minFreq " << displayMinFreq << ")" << endl;
Chris@253 1820
Chris@518 1821 int increment = getWindowIncrement();
Chris@40 1822
Chris@1093 1823 bool logarithmic = (m_binScale == Colour3DPlotRenderer::LogBinScale);
Chris@1026 1824
Chris@1030 1825 MagnitudeRange overallMag = m_viewMags[v->getId()];
Chris@119 1826 bool overallMagChanged = false;
Chris@119 1827
Chris@137 1828 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@1026 1829 cerr << "SpectrogramLayer: " << ((double(v->getFrameForX(1) - v->getFrameForX(0))) / increment) << " bin(s) per pixel" << endl;
Chris@137 1830 #endif
Chris@137 1831
Chris@1024 1832 if (repaintWidth == 0) {
Chris@1024 1833 SVDEBUG << "*** NOTE: repaintWidth == 0" << endl;
Chris@331 1834 }
Chris@331 1835
Chris@382 1836 Profiler outerprof("SpectrogramLayer::paint: all cols");
Chris@382 1837
Chris@481 1838 // The draw buffer contains a fragment at either our pixel
Chris@481 1839 // resolution (if there is more than one time-bin per pixel) or
Chris@481 1840 // time-bin resolution (if a time-bin spans more than one pixel).
Chris@481 1841 // We need to ensure that it starts and ends at points where a
Chris@481 1842 // time-bin boundary occurs at an exact pixel boundary, and with a
Chris@481 1843 // certain amount of overlap across existing pixels so that we can
Chris@481 1844 // scale and draw from it without smoothing errors at the edges.
Chris@481 1845
Chris@481 1846 // If (getFrameForX(x) / increment) * increment ==
Chris@481 1847 // getFrameForX(x), then x is a time-bin boundary. We want two
Chris@481 1848 // such boundaries at either side of the draw buffer -- one which
Chris@481 1849 // we draw up to, and one which we subsequently crop at.
Chris@481 1850
Chris@1039 1851 bool bufferIsBinResolution = false;
Chris@1039 1852 if (increment > zoomLevel) bufferIsBinResolution = true;
Chris@481 1853
Chris@907 1854 sv_frame_t leftBoundaryFrame = -1, leftCropFrame = -1;
Chris@907 1855 sv_frame_t rightBoundaryFrame = -1, rightCropFrame = -1;
Chris@481 1856
Chris@481 1857 int bufwid;
Chris@481 1858
Chris@1039 1859 if (bufferIsBinResolution) {
Chris@481 1860
Chris@482 1861 for (int x = x0; ; --x) {
Chris@907 1862 sv_frame_t f = v->getFrameForX(x);
Chris@481 1863 if ((f / increment) * increment == f) {
Chris@481 1864 if (leftCropFrame == -1) leftCropFrame = f;
Chris@1024 1865 else if (x < x0 - 2) {
Chris@1024 1866 leftBoundaryFrame = f;
Chris@1024 1867 break;
Chris@1024 1868 }
Chris@481 1869 }
Chris@481 1870 }
Chris@1024 1871 for (int x = x0 + repaintWidth; ; ++x) {
Chris@907 1872 sv_frame_t f = v->getFrameForX(x);
Chris@481 1873 if ((f / increment) * increment == f) {
Chris@481 1874 if (rightCropFrame == -1) rightCropFrame = f;
Chris@1024 1875 else if (x > x0 + repaintWidth + 2) {
Chris@1024 1876 rightBoundaryFrame = f;
Chris@1024 1877 break;
Chris@1024 1878 }
Chris@481 1879 }
Chris@481 1880 }
Chris@485 1881 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@481 1882 cerr << "Left: crop: " << leftCropFrame << " (bin " << leftCropFrame/increment << "); boundary: " << leftBoundaryFrame << " (bin " << leftBoundaryFrame/increment << ")" << endl;
Chris@481 1883 cerr << "Right: crop: " << rightCropFrame << " (bin " << rightCropFrame/increment << "); boundary: " << rightBoundaryFrame << " (bin " << rightBoundaryFrame/increment << ")" << endl;
Chris@485 1884 #endif
Chris@481 1885
Chris@907 1886 bufwid = int((rightBoundaryFrame - leftBoundaryFrame) / increment);
Chris@481 1887
Chris@481 1888 } else {
Chris@481 1889
Chris@1024 1890 bufwid = repaintWidth;
Chris@481 1891 }
Chris@481 1892
Chris@907 1893 vector<int> binforx(bufwid);
Chris@907 1894 vector<double> binfory(h);
Chris@907 1895
Chris@484 1896 bool usePeaksCache = false;
Chris@484 1897
Chris@1039 1898 if (bufferIsBinResolution) {
Chris@481 1899 for (int x = 0; x < bufwid; ++x) {
Chris@907 1900 binforx[x] = int(leftBoundaryFrame / increment) + x;
Chris@481 1901 }
Chris@481 1902 m_drawBuffer = QImage(bufwid, h, QImage::Format_Indexed8);
Chris@481 1903 } else {
Chris@481 1904 for (int x = 0; x < bufwid; ++x) {
Chris@905 1905 double s0 = 0, s1 = 0;
Chris@481 1906 if (getXBinRange(v, x + x0, s0, s1)) {
Chris@481 1907 binforx[x] = int(s0 + 0.0001);
Chris@481 1908 } else {
Chris@487 1909 binforx[x] = -1; //???
Chris@481 1910 }
Chris@481 1911 }
Chris@1031 1912 if (m_drawBuffer.width() < bufwid || m_drawBuffer.height() != h) {
Chris@481 1913 m_drawBuffer = QImage(bufwid, h, QImage::Format_Indexed8);
Chris@480 1914 }
Chris@1054 1915 usePeaksCache = (increment * m_peakCacheDivisor) < zoomLevel;
Chris@1092 1916 if (m_colourScale == ColourScale::PhaseColourScale) usePeaksCache = false;
Chris@480 1917 }
Chris@481 1918
Chris@481 1919 for (int pixel = 0; pixel < 256; ++pixel) {
Chris@907 1920 m_drawBuffer.setColor((unsigned char)pixel,
Chris@907 1921 m_palette.getColour((unsigned char)pixel).rgb());
Chris@481 1922 }
Chris@481 1923
Chris@481 1924 m_drawBuffer.fill(0);
Chris@1024 1925 int attainedBufwid = bufwid;
Chris@1039 1926
Chris@1039 1927 double softTimeLimit;
Chris@1039 1928
Chris@1039 1929 if (m_synchronous) {
Chris@1039 1930
Chris@1039 1931 // must paint the whole thing for synchronous mode, so give
Chris@1039 1932 // "no timeout"
Chris@1039 1933 softTimeLimit = 0.0;
Chris@1039 1934
Chris@1039 1935 } else if (bufferIsBinResolution) {
Chris@1039 1936
Chris@1039 1937 // calculating boundaries later will be too fiddly for partial
Chris@1039 1938 // paints, and painting should be fast anyway when this is the
Chris@1039 1939 // case because it means we're well zoomed in
Chris@1039 1940 softTimeLimit = 0.0;
Chris@1039 1941
Chris@1039 1942 } else {
Chris@1039 1943
Chris@1039 1944 // neither limitation applies, so use a short soft limit
Chris@1039 1945
Chris@1093 1946 if (m_binDisplay == Colour3DPlotRenderer::PeakFrequencies) {
Chris@1039 1947 softTimeLimit = 0.15;
Chris@1039 1948 } else {
Chris@1039 1949 softTimeLimit = 0.1;
Chris@1039 1950 }
Chris@1039 1951 }
Chris@1039 1952
Chris@1093 1953 if (m_binDisplay != Colour3DPlotRenderer::PeakFrequencies) {
Chris@488 1954
Chris@488 1955 for (int y = 0; y < h; ++y) {
Chris@905 1956 double q0 = 0, q1 = 0;
Chris@1086 1957 if (!getYBinRange(v, h-y-1, q0, q1)) {
Chris@488 1958 binfory[y] = -1;
Chris@488 1959 } else {
Chris@490 1960 binfory[y] = q0;
Chris@488 1961 }
Chris@480 1962 }
Chris@488 1963
Chris@1024 1964 attainedBufwid =
Chris@1026 1965 paintDrawBuffer(v, bufwid, h, binforx, binfory,
Chris@1026 1966 usePeaksCache,
Chris@1026 1967 overallMag, overallMagChanged,
Chris@1039 1968 rightToLeft,
Chris@1039 1969 softTimeLimit);
Chris@488 1970
Chris@488 1971 } else {
Chris@488 1972
Chris@1024 1973 attainedBufwid =
Chris@1024 1974 paintDrawBufferPeakFrequencies(v, bufwid, h, binforx,
Chris@1024 1975 minbin, maxbin,
Chris@1024 1976 displayMinFreq, displayMaxFreq,
Chris@1024 1977 logarithmic,
Chris@1026 1978 overallMag, overallMagChanged,
Chris@1039 1979 rightToLeft,
Chris@1039 1980 softTimeLimit);
Chris@480 1981 }
Chris@481 1982
Chris@1024 1983 int failedToRepaint = bufwid - attainedBufwid;
Chris@1031 1984
Chris@1031 1985 int paintedLeft = x0;
Chris@1031 1986 int paintedWidth = x1 - x0;
Chris@1031 1987
Chris@1025 1988 if (failedToRepaint > 0) {
Chris@1031 1989
Chris@1025 1990 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@1026 1991 cerr << "SpectrogramLayer::paint(): Failed to repaint " << failedToRepaint << " of " << bufwid
Chris@1029 1992 << " columns in time (so managed to repaint " << bufwid - failedToRepaint << ")" << endl;
Chris@1025 1993 #endif
Chris@1031 1994
Chris@1031 1995 if (rightToLeft) {
Chris@1031 1996 paintedLeft += failedToRepaint;
Chris@1031 1997 }
Chris@1031 1998
Chris@1031 1999 paintedWidth -= failedToRepaint;
Chris@1031 2000
Chris@1031 2001 if (paintedWidth < 0) {
Chris@1031 2002 paintedWidth = 0;
Chris@1031 2003 }
Chris@1031 2004
Chris@1025 2005 } else if (failedToRepaint < 0) {
Chris@1024 2006 cerr << "WARNING: failedToRepaint < 0 (= " << failedToRepaint << ")"
Chris@1024 2007 << endl;
Chris@1024 2008 failedToRepaint = 0;
Chris@1024 2009 }
Chris@1031 2010
Chris@119 2011 if (overallMagChanged) {
Chris@1030 2012 m_viewMags[v->getId()] = overallMag;
Chris@209 2013 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@1030 2014 cerr << "SpectrogramLayer: Overall mag is now [" << m_viewMags[v->getId()].getMin() << "->" << m_viewMags[v->getId()].getMax() << "] - will be updating" << endl;
Chris@209 2015 #endif
Chris@119 2016 }
Chris@119 2017
Chris@382 2018 outerprof.end();
Chris@382 2019
Chris@382 2020 Profiler profiler2("SpectrogramLayer::paint: draw image");
Chris@137 2021
Chris@1031 2022 if (paintedWidth > 0) {
Chris@1024 2023
Chris@224 2024 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@1031 2025 cerr << "SpectrogramLayer: Copying " << paintedWidth << "x" << h
Chris@1031 2026 << " from draw buffer at " << paintedLeft - x0 << "," << 0
Chris@1031 2027 << " to " << paintedWidth << "x" << h << " on cache at "
Chris@585 2028 << x0 << "," << 0 << endl;
Chris@224 2029 #endif
Chris@224 2030
Chris@1039 2031 if (bufferIsBinResolution) {
Chris@1031 2032
Chris@481 2033 int scaledLeft = v->getXForFrame(leftBoundaryFrame);
Chris@481 2034 int scaledRight = v->getXForFrame(rightBoundaryFrame);
Chris@1031 2035
Chris@485 2036 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@1026 2037 cerr << "SpectrogramLayer: Rescaling image from " << bufwid
Chris@481 2038 << "x" << h << " to "
Chris@481 2039 << scaledRight-scaledLeft << "x" << h << endl;
Chris@485 2040 #endif
Chris@1031 2041
Chris@490 2042 Preferences::SpectrogramXSmoothing xsmoothing =
Chris@490 2043 Preferences::getInstance()->getSpectrogramXSmoothing();
Chris@1026 2044
Chris@481 2045 QImage scaled = m_drawBuffer.scaled
Chris@481 2046 (scaledRight - scaledLeft, h,
Chris@490 2047 Qt::IgnoreAspectRatio,
Chris@490 2048 ((xsmoothing == Preferences::SpectrogramXInterpolated) ?
Chris@490 2049 Qt::SmoothTransformation : Qt::FastTransformation));
Chris@1026 2050
Chris@481 2051 int scaledLeftCrop = v->getXForFrame(leftCropFrame);
Chris@481 2052 int scaledRightCrop = v->getXForFrame(rightCropFrame);
Chris@1031 2053
Chris@485 2054 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@1026 2055 cerr << "SpectrogramLayer: Drawing image region of width " << scaledRightCrop - scaledLeftCrop << " to "
Chris@481 2056 << scaledLeftCrop << " from " << scaledLeftCrop - scaledLeft << endl;
Chris@485 2057 #endif
Chris@1030 2058
Chris@1040 2059 int targetLeft = scaledLeftCrop;
Chris@1040 2060 if (targetLeft < 0) {
Chris@1040 2061 targetLeft = 0;
Chris@1040 2062 }
Chris@1040 2063
Chris@1040 2064 int targetWidth = scaledRightCrop - targetLeft;
Chris@1040 2065 if (targetLeft + targetWidth > cache.getSize().width()) {
Chris@1040 2066 targetWidth = cache.getSize().width() - targetLeft;
Chris@1040 2067 }
Chris@1031 2068
Chris@1040 2069 int sourceLeft = targetLeft - scaledLeft;
Chris@1040 2070 if (sourceLeft < 0) {
Chris@1040 2071 sourceLeft = 0;
Chris@1040 2072 }
Chris@1040 2073
Chris@1040 2074 int sourceWidth = targetWidth;
Chris@1040 2075
Chris@1040 2076 if (targetWidth > 0) {
Chris@1040 2077 cache.drawImage
Chris@1040 2078 (targetLeft,
Chris@1040 2079 targetWidth,
Chris@1040 2080 scaled,
Chris@1040 2081 sourceLeft,
Chris@1040 2082 sourceWidth);
Chris@1040 2083 }
Chris@1024 2084
Chris@481 2085 } else {
Chris@1024 2086
Chris@1031 2087 cache.drawImage(paintedLeft, paintedWidth,
Chris@1030 2088 m_drawBuffer,
Chris@1031 2089 paintedLeft - x0, paintedWidth);
Chris@481 2090 }
Chris@331 2091 }
Chris@331 2092
Chris@1026 2093 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@1030 2094 cerr << "SpectrogramLayer: Cache valid area now from " << cache.getValidLeft()
Chris@1030 2095 << " width " << cache.getValidWidth() << ", height "
Chris@1030 2096 << cache.getSize().height() << endl;
Chris@1026 2097 #endif
Chris@1030 2098
Chris@1030 2099 QRect pr = rect & cache.getValidArea();
Chris@337 2100
Chris@337 2101 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@1026 2102 cerr << "SpectrogramLayer: Copying " << pr.width() << "x" << pr.height()
Chris@337 2103 << " from cache at " << pr.x() << "," << pr.y()
Chris@585 2104 << " to window" << endl;
Chris@337 2105 #endif
Chris@337 2106
Chris@1030 2107 paint.drawImage(pr.x(), pr.y(), cache.getImage(),
Chris@479 2108 pr.x(), pr.y(), pr.width(), pr.height());
Chris@337 2109
Chris@389 2110 if (!m_synchronous) {
Chris@389 2111
Chris@1063 2112 if ((m_normalization != ColumnOp::NormalizeVisibleArea) || !overallMagChanged) {
Chris@1031 2113
Chris@1031 2114 QRect areaLeft(0, 0, cache.getValidLeft(), h);
Chris@1031 2115 QRect areaRight(cache.getValidRight(), 0,
Chris@1031 2116 cache.getSize().width() - cache.getValidRight(), h);
Chris@1031 2117
Chris@1031 2118 bool haveSpaceLeft = (areaLeft.width() > 0);
Chris@1031 2119 bool haveSpaceRight = (areaRight.width() > 0);
Chris@1031 2120
Chris@1031 2121 bool updateLeft = haveSpaceLeft;
Chris@1031 2122 bool updateRight = haveSpaceRight;
Chris@1031 2123
Chris@1031 2124 if (updateLeft && updateRight) {
Chris@1031 2125 if (rightToLeft) {
Chris@1031 2126 // we just did something adjoining the cache on
Chris@1031 2127 // its left side, so now do something on its right
Chris@1031 2128 updateLeft = false;
Chris@1031 2129 } else {
Chris@1031 2130 updateRight = false;
Chris@1031 2131 }
Chris@389 2132 }
Chris@389 2133
Chris@1031 2134 if (updateLeft) {
Chris@1031 2135 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@1031 2136 cerr << "SpectrogramLayer::paint() updating left ("
Chris@1031 2137 << areaLeft.x() << ", "
Chris@1031 2138 << areaLeft.width() << ")" << endl;
Chris@1031 2139 #endif
Chris@1031 2140 v->updatePaintRect(areaLeft);
Chris@1031 2141 }
Chris@1031 2142
Chris@1031 2143 if (updateRight) {
Chris@389 2144 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@985 2145 cerr << "SpectrogramLayer::paint() updating right ("
Chris@1031 2146 << areaRight.x() << ", "
Chris@1031 2147 << areaRight.width() << ")" << endl;
Chris@389 2148 #endif
Chris@1031 2149 v->updatePaintRect(areaRight);
Chris@389 2150 }
Chris@1031 2151
Chris@389 2152 } else {
Chris@389 2153 // overallMagChanged
Chris@682 2154 cerr << "\noverallMagChanged - updating all\n" << endl;
Chris@1030 2155 cache.invalidate();
Chris@1030 2156 v->updatePaintRect(v->getPaintRect());
Chris@119 2157 }
Chris@95 2158 }
Chris@0 2159
Chris@121 2160 illuminateLocalFeatures(v, paint);
Chris@120 2161
Chris@0 2162 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@985 2163 cerr << "SpectrogramLayer::paint() returning" << endl;
Chris@0 2164 #endif
Chris@0 2165 }
Chris@0 2166
Chris@1024 2167 int
Chris@918 2168 SpectrogramLayer::paintDrawBufferPeakFrequencies(LayerGeometryProvider *v,
Chris@488 2169 int w,
Chris@488 2170 int h,
Chris@907 2171 const vector<int> &binforx,
Chris@488 2172 int minbin,
Chris@488 2173 int maxbin,
Chris@905 2174 double displayMinFreq,
Chris@905 2175 double displayMaxFreq,
Chris@491 2176 bool logarithmic,
Chris@491 2177 MagnitudeRange &overallMag,
Chris@1026 2178 bool &overallMagChanged,
Chris@1039 2179 bool rightToLeft,
Chris@1039 2180 double softTimeLimit) const
Chris@488 2181 {
Chris@488 2182 Profiler profiler("SpectrogramLayer::paintDrawBufferPeakFrequencies");
Chris@488 2183
Chris@488 2184 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@1026 2185 cerr << "SpectrogramLayer::paintDrawBufferPeakFrequencies: minbin " << minbin << ", maxbin " << maxbin << "; w " << w << ", h " << h << endl;
Chris@488 2186 #endif
Chris@488 2187 if (minbin < 0) minbin = 0;
Chris@488 2188 if (maxbin < 0) maxbin = minbin+1;
Chris@488 2189
Chris@1088 2190 FFTModel *fft = getFFTModel();
Chris@1024 2191 if (!fft) return 0;
Chris@488 2192
Chris@488 2193 FFTModel::PeakSet peakfreqs;
Chris@1064 2194 vector<float> preparedColumn;
Chris@1064 2195
Chris@848 2196 int psx = -1;
Chris@545 2197
Chris@1027 2198 int minColumns = 4;
Chris@1039 2199 bool haveTimeLimits = (softTimeLimit > 0.0);
Chris@1037 2200 double hardTimeLimit = softTimeLimit * 2.0;
Chris@1037 2201 bool overridingSoftLimit = false;
Chris@1027 2202 auto startTime = chrono::steady_clock::now();
Chris@1027 2203
Chris@1026 2204 int start = 0;
Chris@1026 2205 int finish = w;
Chris@1026 2206 int step = 1;
Chris@1026 2207
Chris@1026 2208 if (rightToLeft) {
Chris@1026 2209 start = w-1;
Chris@1026 2210 finish = -1;
Chris@1026 2211 step = -1;
Chris@1026 2212 }
Chris@1026 2213
Chris@1027 2214 int columnCount = 0;
Chris@1027 2215
Chris@1026 2216 for (int x = start; x != finish; x += step) {
Chris@488 2217
Chris@1027 2218 ++columnCount;
Chris@1027 2219
Chris@488 2220 if (binforx[x] < 0) continue;
Chris@488 2221
Chris@488 2222 int sx0 = binforx[x];
Chris@488 2223 int sx1 = sx0;
Chris@488 2224 if (x+1 < w) sx1 = binforx[x+1];
Chris@488 2225 if (sx0 < 0) sx0 = sx1 - 1;
Chris@488 2226 if (sx0 < 0) continue;
Chris@488 2227 if (sx1 <= sx0) sx1 = sx0 + 1;
Chris@488 2228
Chris@1064 2229 vector<float> pixelPeakColumn;
Chris@1064 2230
Chris@488 2231 for (int sx = sx0; sx < sx1; ++sx) {
Chris@488 2232
Chris@1064 2233 if (sx < 0 || sx >= int(fft->getWidth())) {
Chris@1064 2234 continue;
Chris@1064 2235 }
Chris@488 2236
Chris@488 2237 if (sx != psx) {
Chris@1064 2238
Chris@1064 2239 ColumnOp::Column column;
Chris@1064 2240
Chris@1064 2241 column = getColumnFromFFTModel(fft,
Chris@1064 2242 sx,
Chris@1064 2243 minbin,
Chris@1064 2244 maxbin - minbin + 1);
Chris@1064 2245
Chris@1092 2246 if (m_colourScale != ColourScale::PhaseColourScale) {
Chris@1087 2247 column = ColumnOp::fftScale(column, getFFTSize());
Chris@1064 2248 }
Chris@1064 2249
Chris@1064 2250 recordColumnExtents(column,
Chris@1064 2251 sx,
Chris@1064 2252 overallMag,
Chris@1064 2253 overallMagChanged);
Chris@1064 2254
Chris@1092 2255 if (m_colourScale != ColourScale::PhaseColourScale) {
Chris@1064 2256 column = ColumnOp::normalize(column, m_normalization);
Chris@1064 2257 }
Chris@1064 2258
Chris@1064 2259 preparedColumn = ColumnOp::applyGain(column, m_gain);
Chris@1064 2260
Chris@1064 2261 psx = sx;
Chris@1064 2262 }
Chris@1064 2263
Chris@1064 2264 if (sx == sx0) {
Chris@1064 2265 pixelPeakColumn = preparedColumn;
Chris@488 2266 peakfreqs = fft->getPeakFrequencies(FFTModel::AllPeaks, sx,
Chris@488 2267 minbin, maxbin - 1);
Chris@1064 2268 } else {
Chris@1064 2269 for (int i = 0; in_range_for(pixelPeakColumn, i); ++i) {
Chris@1064 2270 pixelPeakColumn[i] = std::max(pixelPeakColumn[i],
Chris@1064 2271 preparedColumn[i]);
Chris@488 2272 }
Chris@488 2273 }
Chris@488 2274 }
Chris@1065 2275
Chris@1065 2276 if (!pixelPeakColumn.empty()) {
Chris@1065 2277 for (FFTModel::PeakSet::const_iterator pi = peakfreqs.begin();
Chris@1065 2278 pi != peakfreqs.end(); ++pi) {
Chris@1065 2279
Chris@1065 2280 int bin = pi->first;
Chris@1065 2281 double freq = pi->second;
Chris@1065 2282
Chris@1065 2283 if (bin < minbin) continue;
Chris@1065 2284 if (bin > maxbin) break;
Chris@1065 2285
Chris@1065 2286 double value = pixelPeakColumn[bin - minbin];
Chris@1065 2287
Chris@1065 2288 double y = v->getYForFrequency
Chris@1065 2289 (freq, displayMinFreq, displayMaxFreq, logarithmic);
Chris@1065 2290
Chris@1065 2291 int iy = int(y + 0.5);
Chris@1065 2292 if (iy < 0 || iy >= h) continue;
Chris@1065 2293
Chris@1065 2294 m_drawBuffer.setPixel(x, iy, getDisplayValue(v, value));
Chris@1065 2295 }
Chris@1065 2296 }
Chris@1064 2297
Chris@1039 2298 if (haveTimeLimits) {
Chris@1027 2299 if (columnCount >= minColumns) {
Chris@1027 2300 auto t = chrono::steady_clock::now();
Chris@1027 2301 double diff = chrono::duration<double>(t - startTime).count();
Chris@1037 2302 if (diff > hardTimeLimit) {
Chris@1027 2303 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@1037 2304 cerr << "SpectrogramLayer::paintDrawBufferPeakFrequencies: hard limit " << hardTimeLimit << " sec exceeded after "
Chris@1031 2305 << columnCount << " columns with time " << diff << endl;
Chris@1027 2306 #endif
Chris@1027 2307 return columnCount;
Chris@1037 2308 } else if (diff > softTimeLimit && !overridingSoftLimit) {
Chris@1037 2309 // If we're more than half way through by the time
Chris@1037 2310 // we reach the soft limit, ignore it (though
Chris@1037 2311 // still respect the hard limit, above). Otherwise
Chris@1037 2312 // respect the soft limit and return now.
Chris@1037 2313 if (columnCount > w/2) {
Chris@1037 2314 overridingSoftLimit = true;
Chris@1037 2315 } else {
Chris@1037 2316 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@1037 2317 cerr << "SpectrogramLayer::paintDrawBufferPeakFrequencies: soft limit " << softTimeLimit << " sec exceeded after "
Chris@1037 2318 << columnCount << " columns with time " << diff << endl;
Chris@1037 2319 #endif
Chris@1037 2320 return columnCount;
Chris@1037 2321 }
Chris@1037 2322 }
Chris@1027 2323 }
Chris@1027 2324 }
Chris@488 2325 }
Chris@1024 2326
Chris@1027 2327 return columnCount;
Chris@488 2328 }
Chris@488 2329
Chris@1058 2330 vector<float>
Chris@1058 2331 SpectrogramLayer::getColumnFromFFTModel(FFTModel *fft,
Chris@1058 2332 int sx, // column number in model
Chris@1058 2333 int minbin,
Chris@1058 2334 int bincount) const
Chris@1058 2335 {
Chris@1058 2336 vector<float> values(bincount, 0.f);
Chris@1058 2337
Chris@1092 2338 if (m_colourScale == ColourScale::PhaseColourScale) {
Chris@1058 2339 fft->getPhasesAt(sx, values.data(), minbin, bincount);
Chris@1058 2340 } else {
Chris@1058 2341 fft->getMagnitudesAt(sx, values.data(), minbin, bincount);
Chris@1058 2342 }
Chris@1058 2343
Chris@1061 2344 return values;
Chris@1058 2345 }
Chris@1058 2346
Chris@1058 2347 vector<float>
Chris@1058 2348 SpectrogramLayer::getColumnFromGenericModel(DenseThreeDimensionalModel *model,
Chris@1058 2349 int sx, // column number in model
Chris@1058 2350 int minbin,
Chris@1058 2351 int bincount) const
Chris@1058 2352 {
Chris@1092 2353 if (m_colourScale == ColourScale::PhaseColourScale) {
Chris@1058 2354 throw std::logic_error("can't use phase scale with generic 3d model");
Chris@1058 2355 }
Chris@1058 2356
Chris@1058 2357 auto col = model->getColumn(sx);
Chris@1058 2358
Chris@1061 2359 return vector<float>(col.data() + minbin,
Chris@1061 2360 col.data() + minbin + bincount);
Chris@1058 2361 }
Chris@1058 2362
Chris@1058 2363 void
Chris@1058 2364 SpectrogramLayer::recordColumnExtents(const vector<float> &col,
Chris@1058 2365 int sx, // column index, for m_columnMags
Chris@1058 2366 MagnitudeRange &overallMag,
Chris@1059 2367 bool &overallMagChanged) const
Chris@1058 2368 {
Chris@1060 2369 if (!in_range_for(m_columnMags, sx)) {
Chris@1063 2370 m_columnMags.resize(sx + 1);
Chris@1058 2371 }
Chris@1058 2372 MagnitudeRange mr;
Chris@1058 2373 for (auto v: col) {
Chris@1058 2374 mr.sample(v);
Chris@1058 2375 }
Chris@1058 2376 m_columnMags[sx] = mr;
Chris@1058 2377 if (overallMag.sample(mr)) {
Chris@1058 2378 overallMagChanged = true;
Chris@1058 2379 }
Chris@1058 2380 }
Chris@1058 2381
Chris@1024 2382 int
Chris@918 2383 SpectrogramLayer::paintDrawBuffer(LayerGeometryProvider *v,
Chris@481 2384 int w,
Chris@481 2385 int h,
Chris@907 2386 const vector<int> &binforx,
Chris@907 2387 const vector<double> &binfory,
Chris@491 2388 bool usePeaksCache,
Chris@491 2389 MagnitudeRange &overallMag,
Chris@1026 2390 bool &overallMagChanged,
Chris@1039 2391 bool rightToLeft,
Chris@1039 2392 double softTimeLimit) const
Chris@480 2393 {
Chris@481 2394 Profiler profiler("SpectrogramLayer::paintDrawBuffer");
Chris@480 2395
Chris@490 2396 int minbin = int(binfory[0] + 0.0001);
Chris@907 2397 int maxbin = int(binfory[h-1]);
Chris@480 2398
Chris@485 2399 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@1026 2400 cerr << "SpectrogramLayer::paintDrawBuffer: minbin " << minbin << ", maxbin " << maxbin << "; w " << w << ", h " << h << endl;
Chris@485 2401 #endif
Chris@480 2402 if (minbin < 0) minbin = 0;
Chris@480 2403 if (maxbin < 0) maxbin = minbin+1;
Chris@480 2404
Chris@1060 2405 DenseThreeDimensionalModel *peakCacheModel = 0;
Chris@1060 2406 FFTModel *fftModel = 0;
Chris@484 2407 DenseThreeDimensionalModel *sourceModel = 0;
Chris@1060 2408
Chris@485 2409 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@1026 2410 cerr << "SpectrogramLayer::paintDrawBuffer: Note: bin display = " << m_binDisplay << ", w = " << w << ", binforx[" << w-1 << "] = " << binforx[w-1] << ", binforx[0] = " << binforx[0] << endl;
Chris@485 2411 #endif
Chris@1060 2412
Chris@1060 2413 int divisor = 1;
Chris@1054 2414 if (usePeaksCache) {
Chris@1088 2415 peakCacheModel = getPeakCache();
Chris@1054 2416 divisor = m_peakCacheDivisor;
Chris@1060 2417 sourceModel = peakCacheModel;
Chris@484 2418 } else {
Chris@1088 2419 fftModel = getFFTModel();
Chris@1060 2420 sourceModel = fftModel;
Chris@484 2421 }
Chris@484 2422
Chris@1024 2423 if (!sourceModel) return 0;
Chris@484 2424
Chris@490 2425 bool interpolate = false;
Chris@490 2426 Preferences::SpectrogramSmoothing smoothing =
Chris@490 2427 Preferences::getInstance()->getSpectrogramSmoothing();
Chris@490 2428 if (smoothing == Preferences::SpectrogramInterpolated ||
Chris@490 2429 smoothing == Preferences::SpectrogramZeroPaddedAndInterpolated) {
Chris@1093 2430 if (m_binDisplay != Colour3DPlotRenderer::PeakBins &&
Chris@1093 2431 m_binDisplay != Colour3DPlotRenderer::PeakFrequencies) {
Chris@490 2432 interpolate = true;
Chris@490 2433 }
Chris@490 2434 }
Chris@490 2435
Chris@480 2436 int psx = -1;
Chris@545 2437
Chris@1027 2438 int minColumns = 4;
Chris@1039 2439 bool haveTimeLimits = (softTimeLimit > 0.0);
Chris@1037 2440 double hardTimeLimit = softTimeLimit * 2.0;
Chris@1037 2441 bool overridingSoftLimit = false;
Chris@1027 2442 auto startTime = chrono::steady_clock::now();
Chris@1027 2443
Chris@1026 2444 int start = 0;
Chris@1026 2445 int finish = w;
Chris@1026 2446 int step = 1;
Chris@1026 2447
Chris@1026 2448 if (rightToLeft) {
Chris@1026 2449 start = w-1;
Chris@1026 2450 finish = -1;
Chris@1026 2451 step = -1;
Chris@1026 2452 }
Chris@1027 2453
Chris@1027 2454 int columnCount = 0;
Chris@1026 2455
Chris@1060 2456 vector<float> preparedColumn;
Chris@1060 2457
Chris@1026 2458 for (int x = start; x != finish; x += step) {
Chris@1027 2459
Chris@1056 2460 // x is the on-canvas pixel coord; sx (later) will be the
Chris@1056 2461 // source column index
Chris@1056 2462
Chris@1027 2463 ++columnCount;
Chris@480 2464
Chris@482 2465 if (binforx[x] < 0) continue;
Chris@482 2466
Chris@484 2467 int sx0 = binforx[x] / divisor;
Chris@483 2468 int sx1 = sx0;
Chris@484 2469 if (x+1 < w) sx1 = binforx[x+1] / divisor;
Chris@483 2470 if (sx0 < 0) sx0 = sx1 - 1;
Chris@483 2471 if (sx0 < 0) continue;
Chris@483 2472 if (sx1 <= sx0) sx1 = sx0 + 1;
Chris@483 2473
Chris@1064 2474 vector<float> pixelPeakColumn;
Chris@1064 2475
Chris@483 2476 for (int sx = sx0; sx < sx1; ++sx) {
Chris@483 2477
Chris@518 2478 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@682 2479 // cerr << "sx = " << sx << endl;
Chris@518 2480 #endif
Chris@518 2481
Chris@1064 2482 if (sx < 0 || sx >= sourceModel->getWidth()) {
Chris@1064 2483 continue;
Chris@1064 2484 }
Chris@488 2485
Chris@483 2486 if (sx != psx) {
Chris@1060 2487
Chris@1064 2488 // order:
Chris@1064 2489 // get column -> scale -> record extents ->
Chris@1063 2490 // normalise -> peak pick -> apply display gain ->
Chris@1063 2491 // distribute/interpolate
Chris@1063 2492
Chris@1063 2493 ColumnOp::Column column;
Chris@1063 2494
Chris@1060 2495 if (peakCacheModel) {
Chris@1060 2496 column = getColumnFromGenericModel(peakCacheModel,
Chris@1060 2497 sx,
Chris@1060 2498 minbin,
Chris@1060 2499 maxbin - minbin + 1);
Chris@484 2500 } else {
Chris@1060 2501 column = getColumnFromFFTModel(fftModel,
Chris@1060 2502 sx,
Chris@1060 2503 minbin,
Chris@1060 2504 maxbin - minbin + 1);
Chris@484 2505 }
Chris@1060 2506
Chris@1092 2507 if (m_colourScale != ColourScale::PhaseColourScale) {
Chris@1087 2508 column = ColumnOp::fftScale(column, getFFTSize());
Chris@1064 2509 }
Chris@1063 2510
Chris@1062 2511 recordColumnExtents(column,
Chris@1062 2512 sx,
Chris@1062 2513 overallMag,
Chris@1062 2514 overallMagChanged);
Chris@1062 2515
Chris@1092 2516 if (m_colourScale != ColourScale::PhaseColourScale) {
Chris@1064 2517 column = ColumnOp::normalize(column, m_normalization);
Chris@1064 2518 }
Chris@1063 2519
Chris@1093 2520 if (m_binDisplay == Colour3DPlotRenderer::PeakBins) {
Chris@1063 2521 column = ColumnOp::peakPick(column);
Chris@1063 2522 }
Chris@1063 2523
Chris@1062 2524 preparedColumn =
Chris@1064 2525 ColumnOp::distribute(ColumnOp::applyGain(column, m_gain),
Chris@1064 2526 h,
Chris@1064 2527 binfory,
Chris@1064 2528 minbin,
Chris@1064 2529 interpolate);
Chris@1060 2530
Chris@483 2531 psx = sx;
Chris@483 2532 }
Chris@483 2533
Chris@1064 2534 if (sx == sx0) {
Chris@1064 2535 pixelPeakColumn = preparedColumn;
Chris@1064 2536 } else {
Chris@1064 2537 for (int i = 0; in_range_for(pixelPeakColumn, i); ++i) {
Chris@1064 2538 pixelPeakColumn[i] = std::max(pixelPeakColumn[i],
Chris@1064 2539 preparedColumn[i]);
Chris@1064 2540 }
Chris@1064 2541 }
Chris@483 2542 }
Chris@483 2543
Chris@1065 2544 if (!pixelPeakColumn.empty()) {
Chris@1065 2545 for (int y = 0; y < h; ++y) {
Chris@1065 2546 m_drawBuffer.setPixel(x,
Chris@1065 2547 h-y-1,
Chris@1065 2548 getDisplayValue(v, pixelPeakColumn[y]));
Chris@1065 2549 }
Chris@480 2550 }
Chris@1025 2551
Chris@1039 2552 if (haveTimeLimits) {
Chris@1027 2553 if (columnCount >= minColumns) {
Chris@1025 2554 auto t = chrono::steady_clock::now();
Chris@1025 2555 double diff = chrono::duration<double>(t - startTime).count();
Chris@1037 2556 if (diff > hardTimeLimit) {
Chris@1025 2557 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@1037 2558 cerr << "SpectrogramLayer::paintDrawBuffer: hard limit " << hardTimeLimit << " sec exceeded after "
Chris@1031 2559 << columnCount << " columns with time " << diff << endl;
Chris@1025 2560 #endif
Chris@1027 2561 return columnCount;
Chris@1037 2562 } else if (diff > softTimeLimit && !overridingSoftLimit) {
Chris@1037 2563 // If we're more than half way through by the time
Chris@1037 2564 // we reach the soft limit, ignore it (though
Chris@1037 2565 // still respect the hard limit, above). Otherwise
Chris@1037 2566 // respect the soft limit and return now.
Chris@1037 2567 if (columnCount > w/2) {
Chris@1037 2568 overridingSoftLimit = true;
Chris@1037 2569 } else {
Chris@1037 2570 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@1037 2571 cerr << "SpectrogramLayer::paintDrawBuffer: soft limit " << softTimeLimit << " sec exceeded after "
Chris@1037 2572 << columnCount << " columns with time " << diff << endl;
Chris@1037 2573 #endif
Chris@1037 2574 return columnCount;
Chris@1037 2575 }
Chris@1037 2576 }
Chris@1025 2577 }
Chris@1025 2578 }
Chris@480 2579 }
Chris@1024 2580
Chris@1027 2581 return columnCount;
Chris@480 2582 }
Chris@477 2583
Chris@121 2584 void
Chris@918 2585 SpectrogramLayer::illuminateLocalFeatures(LayerGeometryProvider *v, QPainter &paint) const
Chris@121 2586 {
Chris@382 2587 Profiler profiler("SpectrogramLayer::illuminateLocalFeatures");
Chris@382 2588
Chris@121 2589 QPoint localPos;
Chris@121 2590 if (!v->shouldIlluminateLocalFeatures(this, localPos) || !m_model) {
Chris@121 2591 return;
Chris@121 2592 }
Chris@121 2593
Chris@682 2594 // cerr << "SpectrogramLayer: illuminateLocalFeatures("
Chris@682 2595 // << localPos.x() << "," << localPos.y() << ")" << endl;
Chris@121 2596
Chris@905 2597 double s0, s1;
Chris@905 2598 double f0, f1;
Chris@121 2599
Chris@121 2600 if (getXBinRange(v, localPos.x(), s0, s1) &&
Chris@121 2601 getYBinSourceRange(v, localPos.y(), f0, f1)) {
Chris@121 2602
Chris@121 2603 int s0i = int(s0 + 0.001);
Chris@121 2604 int s1i = int(s1);
Chris@121 2605
Chris@121 2606 int x0 = v->getXForFrame(s0i * getWindowIncrement());
Chris@121 2607 int x1 = v->getXForFrame((s1i + 1) * getWindowIncrement());
Chris@121 2608
Chris@248 2609 int y1 = int(getYForFrequency(v, f1));
Chris@248 2610 int y0 = int(getYForFrequency(v, f0));
Chris@121 2611
Chris@682 2612 // cerr << "SpectrogramLayer: illuminate "
Chris@682 2613 // << x0 << "," << y1 << " -> " << x1 << "," << y0 << endl;
Chris@121 2614
Chris@287 2615 paint.setPen(v->getForeground());
Chris@133 2616
Chris@133 2617 //!!! should we be using paintCrosshairs for this?
Chris@133 2618
Chris@121 2619 paint.drawRect(x0, y1, x1 - x0 + 1, y0 - y1 + 1);
Chris@121 2620 }
Chris@121 2621 }
Chris@121 2622
Chris@905 2623 double
Chris@918 2624 SpectrogramLayer::getYForFrequency(const LayerGeometryProvider *v, double frequency) const
Chris@42 2625 {
Chris@44 2626 return v->getYForFrequency(frequency,
Chris@44 2627 getEffectiveMinFrequency(),
Chris@44 2628 getEffectiveMaxFrequency(),
Chris@1093 2629 m_binScale == Colour3DPlotRenderer::LogBinScale);
Chris@42 2630 }
Chris@42 2631
Chris@905 2632 double
Chris@918 2633 SpectrogramLayer::getFrequencyForY(const LayerGeometryProvider *v, int y) const
Chris@42 2634 {
Chris@44 2635 return v->getFrequencyForY(y,
Chris@44 2636 getEffectiveMinFrequency(),
Chris@44 2637 getEffectiveMaxFrequency(),
Chris@1093 2638 m_binScale == Colour3DPlotRenderer::LogBinScale);
Chris@42 2639 }
Chris@42 2640
Chris@0 2641 int
Chris@1090 2642 SpectrogramLayer::getCompletion(LayerGeometryProvider *) const
Chris@0 2643 {
Chris@1088 2644 if (!m_fftModel) return 100;
Chris@1088 2645 int completion = m_fftModel->getCompletion();
Chris@224 2646 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@985 2647 cerr << "SpectrogramLayer::getCompletion: completion = " << completion << endl;
Chris@224 2648 #endif
Chris@0 2649 return completion;
Chris@0 2650 }
Chris@0 2651
Chris@583 2652 QString
Chris@1090 2653 SpectrogramLayer::getError(LayerGeometryProvider *) const
Chris@583 2654 {
Chris@1088 2655 if (!m_fftModel) return "";
Chris@1088 2656 return m_fftModel->getError();
Chris@583 2657 }
Chris@583 2658
Chris@28 2659 bool
Chris@905 2660 SpectrogramLayer::getValueExtents(double &min, double &max,
Chris@101 2661 bool &logarithmic, QString &unit) const
Chris@79 2662 {
Chris@133 2663 if (!m_model) return false;
Chris@133 2664
Chris@907 2665 sv_samplerate_t sr = m_model->getSampleRate();
Chris@1087 2666 min = double(sr) / getFFTSize();
Chris@905 2667 max = double(sr) / 2;
Chris@133 2668
Chris@1093 2669 logarithmic = (m_binScale == Colour3DPlotRenderer::LogBinScale);
Chris@79 2670 unit = "Hz";
Chris@79 2671 return true;
Chris@79 2672 }
Chris@79 2673
Chris@79 2674 bool
Chris@905 2675 SpectrogramLayer::getDisplayExtents(double &min, double &max) const
Chris@101 2676 {
Chris@101 2677 min = getEffectiveMinFrequency();
Chris@101 2678 max = getEffectiveMaxFrequency();
Chris@253 2679
Chris@587 2680 // SVDEBUG << "SpectrogramLayer::getDisplayExtents: " << min << "->" << max << endl;
Chris@101 2681 return true;
Chris@101 2682 }
Chris@101 2683
Chris@101 2684 bool
Chris@905 2685 SpectrogramLayer::setDisplayExtents(double min, double max)
Chris@120 2686 {
Chris@120 2687 if (!m_model) return false;
Chris@187 2688
Chris@587 2689 // SVDEBUG << "SpectrogramLayer::setDisplayExtents: " << min << "->" << max << endl;
Chris@187 2690
Chris@120 2691 if (min < 0) min = 0;
Chris@907 2692 if (max > m_model->getSampleRate()/2.0) max = m_model->getSampleRate()/2.0;
Chris@120 2693
Chris@907 2694 int minf = int(lrint(min));
Chris@907 2695 int maxf = int(lrint(max));
Chris@120 2696
Chris@120 2697 if (m_minFrequency == minf && m_maxFrequency == maxf) return true;
Chris@120 2698
Chris@478 2699 invalidateImageCaches();
Chris@120 2700 invalidateMagnitudes();
Chris@120 2701
Chris@120 2702 m_minFrequency = minf;
Chris@120 2703 m_maxFrequency = maxf;
Chris@120 2704
Chris@120 2705 emit layerParametersChanged();
Chris@120 2706
Chris@133 2707 int vs = getCurrentVerticalZoomStep();
Chris@133 2708 if (vs != m_lastEmittedZoomStep) {
Chris@133 2709 emit verticalZoomChanged();
Chris@133 2710 m_lastEmittedZoomStep = vs;
Chris@133 2711 }
Chris@133 2712
Chris@120 2713 return true;
Chris@120 2714 }
Chris@120 2715
Chris@120 2716 bool
Chris@918 2717 SpectrogramLayer::getYScaleValue(const LayerGeometryProvider *v, int y,
Chris@905 2718 double &value, QString &unit) const
Chris@261 2719 {
Chris@261 2720 value = getFrequencyForY(v, y);
Chris@261 2721 unit = "Hz";
Chris@261 2722 return true;
Chris@261 2723 }
Chris@261 2724
Chris@261 2725 bool
Chris@918 2726 SpectrogramLayer::snapToFeatureFrame(LayerGeometryProvider *,
Chris@907 2727 sv_frame_t &frame,
Chris@805 2728 int &resolution,
Chris@28 2729 SnapType snap) const
Chris@13 2730 {
Chris@13 2731 resolution = getWindowIncrement();
Chris@907 2732 sv_frame_t left = (frame / resolution) * resolution;
Chris@907 2733 sv_frame_t right = left + resolution;
Chris@28 2734
Chris@28 2735 switch (snap) {
Chris@28 2736 case SnapLeft: frame = left; break;
Chris@28 2737 case SnapRight: frame = right; break;
Chris@28 2738 case SnapNearest:
Chris@28 2739 case SnapNeighbouring:
Chris@28 2740 if (frame - left > right - frame) frame = right;
Chris@28 2741 else frame = left;
Chris@28 2742 break;
Chris@28 2743 }
Chris@28 2744
Chris@28 2745 return true;
Chris@28 2746 }
Chris@13 2747
Chris@283 2748 void
Chris@918 2749 SpectrogramLayer::measureDoubleClick(LayerGeometryProvider *v, QMouseEvent *e)
Chris@283 2750 {
Chris@920 2751 const View *view = v->getView();
Chris@1030 2752 ScrollableImageCache &cache = getImageCacheReference(view);
Chris@1030 2753
Chris@1030 2754 cerr << "cache width: " << cache.getSize().width() << ", height: "
Chris@1030 2755 << cache.getSize().height() << endl;
Chris@1030 2756
Chris@1030 2757 QImage image = cache.getImage();
Chris@283 2758
Chris@283 2759 ImageRegionFinder finder;
Chris@283 2760 QRect rect = finder.findRegionExtents(&image, e->pos());
Chris@283 2761 if (rect.isValid()) {
Chris@283 2762 MeasureRect mr;
Chris@283 2763 setMeasureRectFromPixrect(v, mr, rect);
Chris@283 2764 CommandHistory::getInstance()->addCommand
Chris@283 2765 (new AddMeasurementRectCommand(this, mr));
Chris@283 2766 }
Chris@283 2767 }
Chris@283 2768
Chris@77 2769 bool
Chris@918 2770 SpectrogramLayer::getCrosshairExtents(LayerGeometryProvider *v, QPainter &paint,
Chris@77 2771 QPoint cursorPos,
Chris@1025 2772 vector<QRect> &extents) const
Chris@77 2773 {
Chris@918 2774 QRect vertical(cursorPos.x() - 12, 0, 12, v->getPaintHeight());
Chris@77 2775 extents.push_back(vertical);
Chris@77 2776
Chris@77 2777 QRect horizontal(0, cursorPos.y(), cursorPos.x(), 1);
Chris@77 2778 extents.push_back(horizontal);
Chris@77 2779
Chris@608 2780 int sw = getVerticalScaleWidth(v, m_haveDetailedScale, paint);
Chris@264 2781
Chris@280 2782 QRect freq(sw, cursorPos.y() - paint.fontMetrics().ascent() - 2,
Chris@280 2783 paint.fontMetrics().width("123456 Hz") + 2,
Chris@280 2784 paint.fontMetrics().height());
Chris@280 2785 extents.push_back(freq);
Chris@264 2786
Chris@279 2787 QRect pitch(sw, cursorPos.y() + 2,
Chris@279 2788 paint.fontMetrics().width("C#10+50c") + 2,
Chris@279 2789 paint.fontMetrics().height());
Chris@279 2790 extents.push_back(pitch);
Chris@279 2791
Chris@280 2792 QRect rt(cursorPos.x(),
Chris@918 2793 v->getPaintHeight() - paint.fontMetrics().height() - 2,
Chris@280 2794 paint.fontMetrics().width("1234.567 s"),
Chris@280 2795 paint.fontMetrics().height());
Chris@280 2796 extents.push_back(rt);
Chris@280 2797
Chris@280 2798 int w(paint.fontMetrics().width("1234567890") + 2);
Chris@280 2799 QRect frame(cursorPos.x() - w - 2,
Chris@918 2800 v->getPaintHeight() - paint.fontMetrics().height() - 2,
Chris@280 2801 w,
Chris@280 2802 paint.fontMetrics().height());
Chris@280 2803 extents.push_back(frame);
Chris@280 2804
Chris@77 2805 return true;
Chris@77 2806 }
Chris@77 2807
Chris@77 2808 void
Chris@918 2809 SpectrogramLayer::paintCrosshairs(LayerGeometryProvider *v, QPainter &paint,
Chris@77 2810 QPoint cursorPos) const
Chris@77 2811 {
Chris@77 2812 paint.save();
Chris@283 2813
Chris@608 2814 int sw = getVerticalScaleWidth(v, m_haveDetailedScale, paint);
Chris@283 2815
Chris@282 2816 QFont fn = paint.font();
Chris@282 2817 if (fn.pointSize() > 8) {
Chris@282 2818 fn.setPointSize(fn.pointSize() - 1);
Chris@282 2819 paint.setFont(fn);
Chris@282 2820 }
Chris@77 2821 paint.setPen(m_crosshairColour);
Chris@77 2822
Chris@77 2823 paint.drawLine(0, cursorPos.y(), cursorPos.x() - 1, cursorPos.y());
Chris@918 2824 paint.drawLine(cursorPos.x(), 0, cursorPos.x(), v->getPaintHeight());
Chris@77 2825
Chris@905 2826 double fundamental = getFrequencyForY(v, cursorPos.y());
Chris@77 2827
Chris@1078 2828 PaintAssistant::drawVisibleText(v, paint,
Chris@278 2829 sw + 2,
Chris@278 2830 cursorPos.y() - 2,
Chris@278 2831 QString("%1 Hz").arg(fundamental),
Chris@1078 2832 PaintAssistant::OutlinedText);
Chris@278 2833
Chris@279 2834 if (Pitch::isFrequencyInMidiRange(fundamental)) {
Chris@279 2835 QString pitchLabel = Pitch::getPitchLabelForFrequency(fundamental);
Chris@1078 2836 PaintAssistant::drawVisibleText(v, paint,
Chris@279 2837 sw + 2,
Chris@279 2838 cursorPos.y() + paint.fontMetrics().ascent() + 2,
Chris@279 2839 pitchLabel,
Chris@1078 2840 PaintAssistant::OutlinedText);
Chris@279 2841 }
Chris@279 2842
Chris@907 2843 sv_frame_t frame = v->getFrameForX(cursorPos.x());
Chris@279 2844 RealTime rt = RealTime::frame2RealTime(frame, m_model->getSampleRate());
Chris@280 2845 QString rtLabel = QString("%1 s").arg(rt.toText(true).c_str());
Chris@280 2846 QString frameLabel = QString("%1").arg(frame);
Chris@1078 2847 PaintAssistant::drawVisibleText(v, paint,
Chris@280 2848 cursorPos.x() - paint.fontMetrics().width(frameLabel) - 2,
Chris@918 2849 v->getPaintHeight() - 2,
Chris@280 2850 frameLabel,
Chris@1078 2851 PaintAssistant::OutlinedText);
Chris@1078 2852 PaintAssistant::drawVisibleText(v, paint,
Chris@280 2853 cursorPos.x() + 2,
Chris@918 2854 v->getPaintHeight() - 2,
Chris@280 2855 rtLabel,
Chris@1078 2856 PaintAssistant::OutlinedText);
Chris@264 2857
Chris@77 2858 int harmonic = 2;
Chris@77 2859
Chris@77 2860 while (harmonic < 100) {
Chris@77 2861
Chris@907 2862 int hy = int(lrint(getYForFrequency(v, fundamental * harmonic)));
Chris@918 2863 if (hy < 0 || hy > v->getPaintHeight()) break;
Chris@77 2864
Chris@77 2865 int len = 7;
Chris@77 2866
Chris@77 2867 if (harmonic % 2 == 0) {
Chris@77 2868 if (harmonic % 4 == 0) {
Chris@77 2869 len = 12;
Chris@77 2870 } else {
Chris@77 2871 len = 10;
Chris@77 2872 }
Chris@77 2873 }
Chris@77 2874
Chris@77 2875 paint.drawLine(cursorPos.x() - len,
Chris@907 2876 hy,
Chris@77 2877 cursorPos.x(),
Chris@907 2878 hy);
Chris@77 2879
Chris@77 2880 ++harmonic;
Chris@77 2881 }
Chris@77 2882
Chris@77 2883 paint.restore();
Chris@77 2884 }
Chris@77 2885
Chris@25 2886 QString
Chris@918 2887 SpectrogramLayer::getFeatureDescription(LayerGeometryProvider *v, QPoint &pos) const
Chris@25 2888 {
Chris@25 2889 int x = pos.x();
Chris@25 2890 int y = pos.y();
Chris@0 2891
Chris@25 2892 if (!m_model || !m_model->isOK()) return "";
Chris@0 2893
Chris@905 2894 double magMin = 0, magMax = 0;
Chris@905 2895 double phaseMin = 0, phaseMax = 0;
Chris@905 2896 double freqMin = 0, freqMax = 0;
Chris@905 2897 double adjFreqMin = 0, adjFreqMax = 0;
Chris@25 2898 QString pitchMin, pitchMax;
Chris@0 2899 RealTime rtMin, rtMax;
Chris@0 2900
Chris@38 2901 bool haveValues = false;
Chris@0 2902
Chris@44 2903 if (!getXBinSourceRange(v, x, rtMin, rtMax)) {
Chris@38 2904 return "";
Chris@38 2905 }
Chris@44 2906 if (getXYBinSourceRange(v, x, y, magMin, magMax, phaseMin, phaseMax)) {
Chris@38 2907 haveValues = true;
Chris@38 2908 }
Chris@0 2909
Chris@35 2910 QString adjFreqText = "", adjPitchText = "";
Chris@35 2911
Chris@1093 2912 if (m_binDisplay == Colour3DPlotRenderer::PeakFrequencies) {
Chris@35 2913
Chris@44 2914 if (!getAdjustedYBinSourceRange(v, x, y, freqMin, freqMax,
Chris@38 2915 adjFreqMin, adjFreqMax)) {
Chris@38 2916 return "";
Chris@38 2917 }
Chris@35 2918
Chris@35 2919 if (adjFreqMin != adjFreqMax) {
Chris@65 2920 adjFreqText = tr("Peak Frequency:\t%1 - %2 Hz\n")
Chris@35 2921 .arg(adjFreqMin).arg(adjFreqMax);
Chris@35 2922 } else {
Chris@65 2923 adjFreqText = tr("Peak Frequency:\t%1 Hz\n")
Chris@35 2924 .arg(adjFreqMin);
Chris@38 2925 }
Chris@38 2926
Chris@38 2927 QString pmin = Pitch::getPitchLabelForFrequency(adjFreqMin);
Chris@38 2928 QString pmax = Pitch::getPitchLabelForFrequency(adjFreqMax);
Chris@38 2929
Chris@38 2930 if (pmin != pmax) {
Chris@65 2931 adjPitchText = tr("Peak Pitch:\t%3 - %4\n").arg(pmin).arg(pmax);
Chris@38 2932 } else {
Chris@65 2933 adjPitchText = tr("Peak Pitch:\t%2\n").arg(pmin);
Chris@35 2934 }
Chris@35 2935
Chris@35 2936 } else {
Chris@35 2937
Chris@44 2938 if (!getYBinSourceRange(v, y, freqMin, freqMax)) return "";
Chris@35 2939 }
Chris@35 2940
Chris@25 2941 QString text;
Chris@25 2942
Chris@25 2943 if (rtMin != rtMax) {
Chris@25 2944 text += tr("Time:\t%1 - %2\n")
Chris@25 2945 .arg(rtMin.toText(true).c_str())
Chris@25 2946 .arg(rtMax.toText(true).c_str());
Chris@25 2947 } else {
Chris@25 2948 text += tr("Time:\t%1\n")
Chris@25 2949 .arg(rtMin.toText(true).c_str());
Chris@0 2950 }
Chris@0 2951
Chris@25 2952 if (freqMin != freqMax) {
Chris@65 2953 text += tr("%1Bin Frequency:\t%2 - %3 Hz\n%4Bin Pitch:\t%5 - %6\n")
Chris@65 2954 .arg(adjFreqText)
Chris@25 2955 .arg(freqMin)
Chris@25 2956 .arg(freqMax)
Chris@65 2957 .arg(adjPitchText)
Chris@65 2958 .arg(Pitch::getPitchLabelForFrequency(freqMin))
Chris@65 2959 .arg(Pitch::getPitchLabelForFrequency(freqMax));
Chris@65 2960 } else {
Chris@65 2961 text += tr("%1Bin Frequency:\t%2 Hz\n%3Bin Pitch:\t%4\n")
Chris@35 2962 .arg(adjFreqText)
Chris@25 2963 .arg(freqMin)
Chris@65 2964 .arg(adjPitchText)
Chris@65 2965 .arg(Pitch::getPitchLabelForFrequency(freqMin));
Chris@25 2966 }
Chris@25 2967
Chris@38 2968 if (haveValues) {
Chris@905 2969 double dbMin = AudioLevel::multiplier_to_dB(magMin);
Chris@905 2970 double dbMax = AudioLevel::multiplier_to_dB(magMax);
Chris@43 2971 QString dbMinString;
Chris@43 2972 QString dbMaxString;
Chris@43 2973 if (dbMin == AudioLevel::DB_FLOOR) {
Chris@43 2974 dbMinString = tr("-Inf");
Chris@43 2975 } else {
Chris@907 2976 dbMinString = QString("%1").arg(lrint(dbMin));
Chris@43 2977 }
Chris@43 2978 if (dbMax == AudioLevel::DB_FLOOR) {
Chris@43 2979 dbMaxString = tr("-Inf");
Chris@43 2980 } else {
Chris@907 2981 dbMaxString = QString("%1").arg(lrint(dbMax));
Chris@43 2982 }
Chris@907 2983 if (lrint(dbMin) != lrint(dbMax)) {
Chris@199 2984 text += tr("dB:\t%1 - %2").arg(dbMinString).arg(dbMaxString);
Chris@25 2985 } else {
Chris@199 2986 text += tr("dB:\t%1").arg(dbMinString);
Chris@25 2987 }
Chris@38 2988 if (phaseMin != phaseMax) {
Chris@38 2989 text += tr("\nPhase:\t%1 - %2").arg(phaseMin).arg(phaseMax);
Chris@38 2990 } else {
Chris@38 2991 text += tr("\nPhase:\t%1").arg(phaseMin);
Chris@38 2992 }
Chris@25 2993 }
Chris@25 2994
Chris@25 2995 return text;
Chris@0 2996 }
Chris@25 2997
Chris@0 2998 int
Chris@40 2999 SpectrogramLayer::getColourScaleWidth(QPainter &paint) const
Chris@40 3000 {
Chris@40 3001 int cw;
Chris@40 3002
Chris@119 3003 cw = paint.fontMetrics().width("-80dB");
Chris@119 3004
Chris@40 3005 return cw;
Chris@40 3006 }
Chris@40 3007
Chris@40 3008 int
Chris@918 3009 SpectrogramLayer::getVerticalScaleWidth(LayerGeometryProvider *, bool detailed, QPainter &paint) const
Chris@0 3010 {
Chris@0 3011 if (!m_model || !m_model->isOK()) return 0;
Chris@0 3012
Chris@607 3013 int cw = 0;
Chris@607 3014 if (detailed) cw = getColourScaleWidth(paint);
Chris@40 3015
Chris@0 3016 int tw = paint.fontMetrics().width(QString("%1")
Chris@0 3017 .arg(m_maxFrequency > 0 ?
Chris@0 3018 m_maxFrequency - 1 :
Chris@0 3019 m_model->getSampleRate() / 2));
Chris@0 3020
Chris@234 3021 int fw = paint.fontMetrics().width(tr("43Hz"));
Chris@0 3022 if (tw < fw) tw = fw;
Chris@40 3023
Chris@1093 3024 int tickw = (m_binScale == Colour3DPlotRenderer::LogBinScale ? 10 : 4);
Chris@0 3025
Chris@40 3026 return cw + tickw + tw + 13;
Chris@0 3027 }
Chris@0 3028
Chris@0 3029 void
Chris@918 3030 SpectrogramLayer::paintVerticalScale(LayerGeometryProvider *v, bool detailed, QPainter &paint, QRect rect) const
Chris@0 3031 {
Chris@0 3032 if (!m_model || !m_model->isOK()) {
Chris@0 3033 return;
Chris@0 3034 }
Chris@0 3035
Chris@382 3036 Profiler profiler("SpectrogramLayer::paintVerticalScale");
Chris@122 3037
Chris@120 3038 //!!! cache this?
Chris@120 3039
Chris@0 3040 int h = rect.height(), w = rect.width();
Chris@0 3041
Chris@1093 3042 int tickw = (m_binScale == Colour3DPlotRenderer::LogBinScale ? 10 : 4);
Chris@1093 3043 int pkw = (m_binScale == Colour3DPlotRenderer::LogBinScale ? 10 : 0);
Chris@40 3044
Chris@1087 3045 int bins = getFFTSize() / 2;
Chris@907 3046 sv_samplerate_t sr = m_model->getSampleRate();
Chris@0 3047
Chris@0 3048 if (m_maxFrequency > 0) {
Chris@1087 3049 bins = int((double(m_maxFrequency) * getFFTSize()) / sr + 0.1);
Chris@1087 3050 if (bins > getFFTSize() / 2) bins = getFFTSize() / 2;
Chris@0 3051 }
Chris@0 3052
Chris@607 3053 int cw = 0;
Chris@607 3054
Chris@607 3055 if (detailed) cw = getColourScaleWidth(paint);
Chris@119 3056 int cbw = paint.fontMetrics().width("dB");
Chris@40 3057
Chris@0 3058 int py = -1;
Chris@0 3059 int textHeight = paint.fontMetrics().height();
Chris@0 3060 int toff = -textHeight + paint.fontMetrics().ascent() + 2;
Chris@0 3061
Chris@607 3062 if (detailed && (h > textHeight * 3 + 10)) {
Chris@119 3063
Chris@119 3064 int topLines = 2;
Chris@1092 3065 if (m_colourScale == ColourScale::PhaseColourScale) topLines = 1;
Chris@119 3066
Chris@119 3067 int ch = h - textHeight * (topLines + 1) - 8;
Chris@119 3068 // paint.drawRect(4, textHeight + 4, cw - 1, ch + 1);
Chris@119 3069 paint.drawRect(4 + cw - cbw, textHeight * topLines + 4, cbw - 1, ch + 1);
Chris@40 3070
Chris@40 3071 QString top, bottom;
Chris@1030 3072 double min = m_viewMags[v->getId()].getMin();
Chris@1030 3073 double max = m_viewMags[v->getId()].getMax();
Chris@905 3074
Chris@905 3075 double dBmin = AudioLevel::multiplier_to_dB(min);
Chris@905 3076 double dBmax = AudioLevel::multiplier_to_dB(max);
Chris@119 3077
Chris@1044 3078 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@1044 3079 cerr << "paintVerticalScale: for view id " << v->getId()
Chris@1044 3080 << ": min = " << min << ", max = " << max
Chris@1044 3081 << ", dBmin = " << dBmin << ", dBmax = " << dBmax << endl;
Chris@1044 3082 #endif
Chris@1044 3083
Chris@120 3084 if (dBmax < -60.f) dBmax = -60.f;
Chris@907 3085 else top = QString("%1").arg(lrint(dBmax));
Chris@120 3086
Chris@120 3087 if (dBmin < dBmax - 60.f) dBmin = dBmax - 60.f;
Chris@907 3088 bottom = QString("%1").arg(lrint(dBmin));
Chris@119 3089
Chris@119 3090 //!!! & phase etc
Chris@119 3091
Chris@1092 3092 if (m_colourScale != ColourScale::PhaseColourScale) {
Chris@119 3093 paint.drawText((cw + 6 - paint.fontMetrics().width("dBFS")) / 2,
Chris@119 3094 2 + textHeight + toff, "dBFS");
Chris@119 3095 }
Chris@119 3096
Chris@119 3097 // paint.drawText((cw + 6 - paint.fontMetrics().width(top)) / 2,
Chris@119 3098 paint.drawText(3 + cw - cbw - paint.fontMetrics().width(top),
Chris@119 3099 2 + textHeight * topLines + toff + textHeight/2, top);
Chris@119 3100
Chris@119 3101 paint.drawText(3 + cw - cbw - paint.fontMetrics().width(bottom),
Chris@119 3102 h + toff - 3 - textHeight/2, bottom);
Chris@40 3103
Chris@40 3104 paint.save();
Chris@40 3105 paint.setBrush(Qt::NoBrush);
Chris@119 3106
Chris@119 3107 int lasty = 0;
Chris@119 3108 int lastdb = 0;
Chris@119 3109
Chris@40 3110 for (int i = 0; i < ch; ++i) {
Chris@119 3111
Chris@905 3112 double dBval = dBmin + (((dBmax - dBmin) * i) / (ch - 1));
Chris@119 3113 int idb = int(dBval);
Chris@119 3114
Chris@905 3115 double value = AudioLevel::dB_to_multiplier(dBval);
Chris@119 3116 int colour = getDisplayValue(v, value * m_gain);
Chris@210 3117
Chris@907 3118 paint.setPen(m_palette.getColour((unsigned char)colour));
Chris@119 3119
Chris@119 3120 int y = textHeight * topLines + 4 + ch - i;
Chris@119 3121
Chris@119 3122 paint.drawLine(5 + cw - cbw, y, cw + 2, y);
Chris@119 3123
Chris@119 3124 if (i == 0) {
Chris@119 3125 lasty = y;
Chris@119 3126 lastdb = idb;
Chris@119 3127 } else if (i < ch - paint.fontMetrics().ascent() &&
Chris@120 3128 idb != lastdb &&
Chris@119 3129 ((abs(y - lasty) > textHeight &&
Chris@119 3130 idb % 10 == 0) ||
Chris@119 3131 (abs(y - lasty) > paint.fontMetrics().ascent() &&
Chris@119 3132 idb % 5 == 0))) {
Chris@287 3133 paint.setPen(v->getBackground());
Chris@119 3134 QString text = QString("%1").arg(idb);
Chris@119 3135 paint.drawText(3 + cw - cbw - paint.fontMetrics().width(text),
Chris@119 3136 y + toff + textHeight/2, text);
Chris@287 3137 paint.setPen(v->getForeground());
Chris@119 3138 paint.drawLine(5 + cw - cbw, y, 8 + cw - cbw, y);
Chris@119 3139 lasty = y;
Chris@119 3140 lastdb = idb;
Chris@119 3141 }
Chris@40 3142 }
Chris@40 3143 paint.restore();
Chris@40 3144 }
Chris@40 3145
Chris@40 3146 paint.drawLine(cw + 7, 0, cw + 7, h);
Chris@40 3147
Chris@0 3148 int bin = -1;
Chris@0 3149
Chris@918 3150 for (int y = 0; y < v->getPaintHeight(); ++y) {
Chris@0 3151
Chris@905 3152 double q0, q1;
Chris@918 3153 if (!getYBinRange(v, v->getPaintHeight() - y, q0, q1)) continue;
Chris@0 3154
Chris@0 3155 int vy;
Chris@0 3156
Chris@0 3157 if (int(q0) > bin) {
Chris@0 3158 vy = y;
Chris@0 3159 bin = int(q0);
Chris@0 3160 } else {
Chris@0 3161 continue;
Chris@0 3162 }
Chris@0 3163
Chris@1087 3164 int freq = int((sr * bin) / getFFTSize());
Chris@0 3165
Chris@0 3166 if (py >= 0 && (vy - py) < textHeight - 1) {
Chris@1093 3167 if (m_binScale == Colour3DPlotRenderer::LinearBinScale) {
Chris@40 3168 paint.drawLine(w - tickw, h - vy, w, h - vy);
Chris@40 3169 }
Chris@0 3170 continue;
Chris@0 3171 }
Chris@0 3172
Chris@0 3173 QString text = QString("%1").arg(freq);
Chris@234 3174 if (bin == 1) text = tr("%1Hz").arg(freq); // bin 0 is DC
Chris@40 3175 paint.drawLine(cw + 7, h - vy, w - pkw - 1, h - vy);
Chris@0 3176
Chris@0 3177 if (h - vy - textHeight >= -2) {
Chris@1025 3178 int tx = w - 3 - paint.fontMetrics().width(text) - max(tickw, pkw);
Chris@0 3179 paint.drawText(tx, h - vy + toff, text);
Chris@0 3180 }
Chris@0 3181
Chris@0 3182 py = vy;
Chris@0 3183 }
Chris@40 3184
Chris@1093 3185 if (m_binScale == Colour3DPlotRenderer::LogBinScale) {
Chris@40 3186
Chris@277 3187 // piano keyboard
Chris@277 3188
Chris@690 3189 PianoScale().paintPianoVertical
Chris@690 3190 (v, paint, QRect(w - pkw - 1, 0, pkw, h),
Chris@690 3191 getEffectiveMinFrequency(), getEffectiveMaxFrequency());
Chris@40 3192 }
Chris@608 3193
Chris@608 3194 m_haveDetailedScale = detailed;
Chris@0 3195 }
Chris@0 3196
Chris@187 3197 class SpectrogramRangeMapper : public RangeMapper
Chris@187 3198 {
Chris@187 3199 public:
Chris@901 3200 SpectrogramRangeMapper(sv_samplerate_t sr, int /* fftsize */) :
Chris@901 3201 m_dist(sr / 2),
Chris@901 3202 m_s2(sqrt(sqrt(2))) { }
Chris@187 3203 ~SpectrogramRangeMapper() { }
Chris@187 3204
Chris@901 3205 virtual int getPositionForValue(double value) const {
Chris@901 3206
Chris@901 3207 double dist = m_dist;
Chris@187 3208
Chris@187 3209 int n = 0;
Chris@187 3210
Chris@901 3211 while (dist > (value + 0.00001) && dist > 0.1) {
Chris@187 3212 dist /= m_s2;
Chris@187 3213 ++n;
Chris@187 3214 }
Chris@187 3215
Chris@187 3216 return n;
Chris@187 3217 }
Chris@724 3218
Chris@901 3219 virtual int getPositionForValueUnclamped(double value) const {
Chris@724 3220 // We don't really support this
Chris@724 3221 return getPositionForValue(value);
Chris@724 3222 }
Chris@187 3223
Chris@901 3224 virtual double getValueForPosition(int position) const {
Chris@187 3225
Chris@187 3226 // Vertical zoom step 0 shows the entire range from DC ->
Chris@187 3227 // Nyquist frequency. Step 1 shows 2^(1/4) of the range of
Chris@187 3228 // step 0, and so on until the visible range is smaller than
Chris@187 3229 // the frequency step between bins at the current fft size.
Chris@187 3230
Chris@901 3231 double dist = m_dist;
Chris@187 3232
Chris@187 3233 int n = 0;
Chris@187 3234 while (n < position) {
Chris@187 3235 dist /= m_s2;
Chris@187 3236 ++n;
Chris@187 3237 }
Chris@187 3238
Chris@187 3239 return dist;
Chris@187 3240 }
Chris@187 3241
Chris@901 3242 virtual double getValueForPositionUnclamped(int position) const {
Chris@724 3243 // We don't really support this
Chris@724 3244 return getValueForPosition(position);
Chris@724 3245 }
Chris@724 3246
Chris@187 3247 virtual QString getUnit() const { return "Hz"; }
Chris@187 3248
Chris@187 3249 protected:
Chris@901 3250 double m_dist;
Chris@901 3251 double m_s2;
Chris@187 3252 };
Chris@187 3253
Chris@133 3254 int
Chris@133 3255 SpectrogramLayer::getVerticalZoomSteps(int &defaultStep) const
Chris@133 3256 {
Chris@135 3257 if (!m_model) return 0;
Chris@187 3258
Chris@907 3259 sv_samplerate_t sr = m_model->getSampleRate();
Chris@187 3260
Chris@1087 3261 SpectrogramRangeMapper mapper(sr, getFFTSize());
Chris@1087 3262
Chris@1087 3263 // int maxStep = mapper.getPositionForValue((double(sr) / getFFTSize()) + 0.001);
Chris@187 3264 int maxStep = mapper.getPositionForValue(0);
Chris@905 3265 int minStep = mapper.getPositionForValue(double(sr) / 2);
Chris@250 3266
Chris@805 3267 int initialMax = m_initialMaxFrequency;
Chris@907 3268 if (initialMax == 0) initialMax = int(sr / 2);
Chris@250 3269
Chris@250 3270 defaultStep = mapper.getPositionForValue(initialMax) - minStep;
Chris@250 3271
Chris@587 3272 // SVDEBUG << "SpectrogramLayer::getVerticalZoomSteps: " << maxStep - minStep << " (" << maxStep <<"-" << minStep << "), default is " << defaultStep << " (from initial max freq " << initialMax << ")" << endl;
Chris@187 3273
Chris@187 3274 return maxStep - minStep;
Chris@133 3275 }
Chris@133 3276
Chris@133 3277 int
Chris@133 3278 SpectrogramLayer::getCurrentVerticalZoomStep() const
Chris@133 3279 {
Chris@133 3280 if (!m_model) return 0;
Chris@133 3281
Chris@905 3282 double dmin, dmax;
Chris@133 3283 getDisplayExtents(dmin, dmax);
Chris@133 3284
Chris@1087 3285 SpectrogramRangeMapper mapper(m_model->getSampleRate(), getFFTSize());
Chris@187 3286 int n = mapper.getPositionForValue(dmax - dmin);
Chris@587 3287 // SVDEBUG << "SpectrogramLayer::getCurrentVerticalZoomStep: " << n << endl;
Chris@133 3288 return n;
Chris@133 3289 }
Chris@133 3290
Chris@133 3291 void
Chris@133 3292 SpectrogramLayer::setVerticalZoomStep(int step)
Chris@133 3293 {
Chris@187 3294 if (!m_model) return;
Chris@187 3295
Chris@905 3296 double dmin = m_minFrequency, dmax = m_maxFrequency;
Chris@253 3297 // getDisplayExtents(dmin, dmax);
Chris@253 3298
Chris@682 3299 // cerr << "current range " << dmin << " -> " << dmax << ", range " << dmax-dmin << ", mid " << (dmax + dmin)/2 << endl;
Chris@133 3300
Chris@907 3301 sv_samplerate_t sr = m_model->getSampleRate();
Chris@1087 3302 SpectrogramRangeMapper mapper(sr, getFFTSize());
Chris@905 3303 double newdist = mapper.getValueForPosition(step);
Chris@905 3304
Chris@905 3305 double newmin, newmax;
Chris@253 3306
Chris@1093 3307 if (m_binScale == Colour3DPlotRenderer::LogBinScale) {
Chris@253 3308
Chris@253 3309 // need to pick newmin and newmax such that
Chris@253 3310 //
Chris@253 3311 // (log(newmin) + log(newmax)) / 2 == logmid
Chris@253 3312 // and
Chris@253 3313 // newmax - newmin = newdist
Chris@253 3314 //
Chris@253 3315 // so log(newmax - newdist) + log(newmax) == 2logmid
Chris@253 3316 // log(newmax(newmax - newdist)) == 2logmid
Chris@253 3317 // newmax.newmax - newmax.newdist == exp(2logmid)
Chris@253 3318 // newmax^2 + (-newdist)newmax + -exp(2logmid) == 0
Chris@253 3319 // quadratic with a = 1, b = -newdist, c = -exp(2logmid), all known
Chris@253 3320 //
Chris@253 3321 // positive root
Chris@253 3322 // newmax = (newdist + sqrt(newdist^2 + 4exp(2logmid))) / 2
Chris@253 3323 //
Chris@253 3324 // but logmid = (log(dmin) + log(dmax)) / 2
Chris@253 3325 // so exp(2logmid) = exp(log(dmin) + log(dmax))
Chris@253 3326 // = exp(log(dmin.dmax))
Chris@253 3327 // = dmin.dmax
Chris@253 3328 // so newmax = (newdist + sqrtf(newdist^2 + 4dmin.dmax)) / 2
Chris@253 3329
Chris@907 3330 newmax = (newdist + sqrt(newdist*newdist + 4*dmin*dmax)) / 2;
Chris@253 3331 newmin = newmax - newdist;
Chris@253 3332
Chris@682 3333 // cerr << "newmin = " << newmin << ", newmax = " << newmax << endl;
Chris@253 3334
Chris@253 3335 } else {
Chris@905 3336 double dmid = (dmax + dmin) / 2;
Chris@253 3337 newmin = dmid - newdist / 2;
Chris@253 3338 newmax = dmid + newdist / 2;
Chris@253 3339 }
Chris@187 3340
Chris@905 3341 double mmin, mmax;
Chris@187 3342 mmin = 0;
Chris@905 3343 mmax = double(sr) / 2;
Chris@133 3344
Chris@187 3345 if (newmin < mmin) {
Chris@187 3346 newmax += (mmin - newmin);
Chris@187 3347 newmin = mmin;
Chris@187 3348 }
Chris@187 3349 if (newmax > mmax) {
Chris@187 3350 newmax = mmax;
Chris@187 3351 }
Chris@133 3352
Chris@587 3353 // SVDEBUG << "SpectrogramLayer::setVerticalZoomStep: " << step << ": " << newmin << " -> " << newmax << " (range " << newdist << ")" << endl;
Chris@253 3354
Chris@907 3355 setMinFrequency(int(lrint(newmin)));
Chris@907 3356 setMaxFrequency(int(lrint(newmax)));
Chris@187 3357 }
Chris@187 3358
Chris@187 3359 RangeMapper *
Chris@187 3360 SpectrogramLayer::getNewVerticalZoomRangeMapper() const
Chris@187 3361 {
Chris@187 3362 if (!m_model) return 0;
Chris@1087 3363 return new SpectrogramRangeMapper(m_model->getSampleRate(), getFFTSize());
Chris@133 3364 }
Chris@133 3365
Chris@273 3366 void
Chris@918 3367 SpectrogramLayer::updateMeasureRectYCoords(LayerGeometryProvider *v, const MeasureRect &r) const
Chris@273 3368 {
Chris@273 3369 int y0 = 0;
Chris@907 3370 if (r.startY > 0.0) y0 = int(getYForFrequency(v, r.startY));
Chris@273 3371
Chris@273 3372 int y1 = y0;
Chris@907 3373 if (r.endY > 0.0) y1 = int(getYForFrequency(v, r.endY));
Chris@273 3374
Chris@587 3375 // SVDEBUG << "SpectrogramLayer::updateMeasureRectYCoords: start " << r.startY << " -> " << y0 << ", end " << r.endY << " -> " << y1 << endl;
Chris@273 3376
Chris@273 3377 r.pixrect = QRect(r.pixrect.x(), y0, r.pixrect.width(), y1 - y0);
Chris@273 3378 }
Chris@273 3379
Chris@273 3380 void
Chris@918 3381 SpectrogramLayer::setMeasureRectYCoord(LayerGeometryProvider *v, MeasureRect &r, bool start, int y) const
Chris@273 3382 {
Chris@273 3383 if (start) {
Chris@273 3384 r.startY = getFrequencyForY(v, y);
Chris@273 3385 r.endY = r.startY;
Chris@273 3386 } else {
Chris@273 3387 r.endY = getFrequencyForY(v, y);
Chris@273 3388 }
Chris@587 3389 // SVDEBUG << "SpectrogramLayer::setMeasureRectYCoord: start " << r.startY << " <- " << y << ", end " << r.endY << " <- " << y << endl;
Chris@273 3390
Chris@273 3391 }
Chris@273 3392
Chris@1092 3393 static ColourScale::Scale
Chris@1092 3394 convertInColourScale(int fileScale)
Chris@1092 3395 {
Chris@1092 3396 switch (fileScale) {
Chris@1092 3397 case 0: return ColourScale::LinearColourScale;
Chris@1092 3398 case 1: return ColourScale::MeterColourScale;
Chris@1092 3399 case 2: return ColourScale::LogColourScale; //!!!
Chris@1092 3400 case 3: return ColourScale::LogColourScale;
Chris@1092 3401 case 4: return ColourScale::PhaseColourScale;
Chris@1092 3402 default: return ColourScale::LinearColourScale;
Chris@1092 3403 }
Chris@1092 3404 }
Chris@1092 3405
Chris@1092 3406 static int
Chris@1092 3407 convertOutColourScale(ColourScale::Scale scale)
Chris@1092 3408 {
Chris@1092 3409 switch (scale) {
Chris@1092 3410 case ColourScale::LinearColourScale: return 0;
Chris@1092 3411 case ColourScale::MeterColourScale: return 1;
Chris@1092 3412 case ColourScale::LogColourScale: return 3; //!!!
Chris@1092 3413 case ColourScale::PhaseColourScale: return 4;
Chris@1092 3414 case ColourScale::PlusMinusOneScale:
Chris@1092 3415 case ColourScale::AbsoluteScale:
Chris@1092 3416 default: return 0;
Chris@1092 3417 }
Chris@1092 3418 }
Chris@1092 3419
Chris@316 3420 void
Chris@316 3421 SpectrogramLayer::toXml(QTextStream &stream,
Chris@316 3422 QString indent, QString extraAttributes) const
Chris@6 3423 {
Chris@6 3424 QString s;
Chris@6 3425
Chris@6 3426 s += QString("channel=\"%1\" "
Chris@6 3427 "windowSize=\"%2\" "
Chris@153 3428 "windowHopLevel=\"%3\" "
Chris@153 3429 "gain=\"%4\" "
Chris@153 3430 "threshold=\"%5\" ")
Chris@6 3431 .arg(m_channel)
Chris@6 3432 .arg(m_windowSize)
Chris@97 3433 .arg(m_windowHopLevel)
Chris@37 3434 .arg(m_gain)
Chris@37 3435 .arg(m_threshold);
Chris@37 3436
Chris@37 3437 s += QString("minFrequency=\"%1\" "
Chris@37 3438 "maxFrequency=\"%2\" "
Chris@37 3439 "colourScale=\"%3\" "
Chris@37 3440 "colourScheme=\"%4\" "
Chris@37 3441 "colourRotation=\"%5\" "
Chris@37 3442 "frequencyScale=\"%6\" "
Chris@761 3443 "binDisplay=\"%7\" ")
Chris@37 3444 .arg(m_minFrequency)
Chris@6 3445 .arg(m_maxFrequency)
Chris@1092 3446 .arg(convertOutColourScale(m_colourScale))
Chris@197 3447 .arg(m_colourMap)
Chris@37 3448 .arg(m_colourRotation)
Chris@1093 3449 .arg(m_binScale)
Chris@761 3450 .arg(m_binDisplay);
Chris@761 3451
Chris@1009 3452 // New-style normalization attributes, allowing for more types of
Chris@1009 3453 // normalization in future: write out the column normalization
Chris@1009 3454 // type separately, and then whether we are normalizing visible
Chris@1009 3455 // area as well afterwards
Chris@1009 3456
Chris@1009 3457 s += QString("columnNormalization=\"%1\" ")
Chris@1063 3458 .arg(m_normalization == ColumnOp::NormalizeColumns ? "peak" :
Chris@1063 3459 m_normalization == ColumnOp::NormalizeHybrid ? "hybrid" : "none");
Chris@1009 3460
Chris@1009 3461 // Old-style normalization attribute. We *don't* write out
Chris@1009 3462 // normalizeHybrid here because the only release that would accept
Chris@1009 3463 // it (Tony v1.0) has a totally different scale factor for
Chris@1009 3464 // it. We'll just have to accept that session files from Tony
Chris@1009 3465 // v2.0+ will look odd in Tony v1.0
Chris@1009 3466
Chris@1009 3467 s += QString("normalizeColumns=\"%1\" ")
Chris@1063 3468 .arg(m_normalization == ColumnOp::NormalizeColumns ? "true" : "false");
Chris@1009 3469
Chris@1009 3470 // And this applies to both old- and new-style attributes
Chris@1009 3471
Chris@1009 3472 s += QString("normalizeVisibleArea=\"%1\" ")
Chris@1063 3473 .arg(m_normalization == ColumnOp::NormalizeVisibleArea ? "true" : "false");
Chris@1009 3474
Chris@316 3475 Layer::toXml(stream, indent, extraAttributes + " " + s);
Chris@6 3476 }
Chris@6 3477
Chris@11 3478 void
Chris@11 3479 SpectrogramLayer::setProperties(const QXmlAttributes &attributes)
Chris@11 3480 {
Chris@11 3481 bool ok = false;
Chris@11 3482
Chris@11 3483 int channel = attributes.value("channel").toInt(&ok);
Chris@11 3484 if (ok) setChannel(channel);
Chris@11 3485
Chris@805 3486 int windowSize = attributes.value("windowSize").toUInt(&ok);
Chris@11 3487 if (ok) setWindowSize(windowSize);
Chris@11 3488
Chris@805 3489 int windowHopLevel = attributes.value("windowHopLevel").toUInt(&ok);
Chris@97 3490 if (ok) setWindowHopLevel(windowHopLevel);
Chris@97 3491 else {
Chris@805 3492 int windowOverlap = attributes.value("windowOverlap").toUInt(&ok);
Chris@97 3493 // a percentage value
Chris@97 3494 if (ok) {
Chris@97 3495 if (windowOverlap == 0) setWindowHopLevel(0);
Chris@97 3496 else if (windowOverlap == 25) setWindowHopLevel(1);
Chris@97 3497 else if (windowOverlap == 50) setWindowHopLevel(2);
Chris@97 3498 else if (windowOverlap == 75) setWindowHopLevel(3);
Chris@97 3499 else if (windowOverlap == 90) setWindowHopLevel(4);
Chris@97 3500 }
Chris@97 3501 }
Chris@11 3502
Chris@11 3503 float gain = attributes.value("gain").toFloat(&ok);
Chris@11 3504 if (ok) setGain(gain);
Chris@11 3505
Chris@37 3506 float threshold = attributes.value("threshold").toFloat(&ok);
Chris@37 3507 if (ok) setThreshold(threshold);
Chris@37 3508
Chris@805 3509 int minFrequency = attributes.value("minFrequency").toUInt(&ok);
Chris@187 3510 if (ok) {
Chris@587 3511 SVDEBUG << "SpectrogramLayer::setProperties: setting min freq to " << minFrequency << endl;
Chris@187 3512 setMinFrequency(minFrequency);
Chris@187 3513 }
Chris@37 3514
Chris@805 3515 int maxFrequency = attributes.value("maxFrequency").toUInt(&ok);
Chris@187 3516 if (ok) {
Chris@587 3517 SVDEBUG << "SpectrogramLayer::setProperties: setting max freq to " << maxFrequency << endl;
Chris@187 3518 setMaxFrequency(maxFrequency);
Chris@187 3519 }
Chris@11 3520
Chris@1092 3521 ColourScale::Scale colourScale = convertInColourScale
Chris@1092 3522 (attributes.value("colourScale").toInt(&ok));
Chris@11 3523 if (ok) setColourScale(colourScale);
Chris@11 3524
Chris@197 3525 int colourMap = attributes.value("colourScheme").toInt(&ok);
Chris@197 3526 if (ok) setColourMap(colourMap);
Chris@11 3527
Chris@37 3528 int colourRotation = attributes.value("colourRotation").toInt(&ok);
Chris@37 3529 if (ok) setColourRotation(colourRotation);
Chris@37 3530
Chris@1093 3531 Colour3DPlotRenderer::BinScale binScale = (Colour3DPlotRenderer::BinScale)
Chris@11 3532 attributes.value("frequencyScale").toInt(&ok);
Chris@1093 3533 if (ok) setBinScale(binScale);
Chris@1093 3534
Chris@1093 3535 Colour3DPlotRenderer::BinDisplay binDisplay = (Colour3DPlotRenderer::BinDisplay)
Chris@37 3536 attributes.value("binDisplay").toInt(&ok);
Chris@37 3537 if (ok) setBinDisplay(binDisplay);
Chris@36 3538
Chris@1009 3539 bool haveNewStyleNormalization = false;
Chris@1009 3540
Chris@1009 3541 QString columnNormalization = attributes.value("columnNormalization");
Chris@1009 3542
Chris@1009 3543 if (columnNormalization != "") {
Chris@1009 3544
Chris@1009 3545 haveNewStyleNormalization = true;
Chris@1009 3546
Chris@1009 3547 if (columnNormalization == "peak") {
Chris@1063 3548 setNormalization(ColumnOp::NormalizeColumns);
Chris@1009 3549 } else if (columnNormalization == "hybrid") {
Chris@1063 3550 setNormalization(ColumnOp::NormalizeHybrid);
Chris@1009 3551 } else if (columnNormalization == "none") {
Chris@1009 3552 // do nothing
Chris@1009 3553 } else {
Chris@1009 3554 cerr << "NOTE: Unknown or unsupported columnNormalization attribute \""
Chris@1009 3555 << columnNormalization << "\"" << endl;
Chris@1009 3556 }
Chris@1009 3557 }
Chris@1009 3558
Chris@1009 3559 if (!haveNewStyleNormalization) {
Chris@1009 3560
Chris@1009 3561 bool normalizeColumns =
Chris@1009 3562 (attributes.value("normalizeColumns").trimmed() == "true");
Chris@1009 3563 if (normalizeColumns) {
Chris@1063 3564 setNormalization(ColumnOp::NormalizeColumns);
Chris@1009 3565 }
Chris@1009 3566
Chris@1009 3567 bool normalizeHybrid =
Chris@1009 3568 (attributes.value("normalizeHybrid").trimmed() == "true");
Chris@1009 3569 if (normalizeHybrid) {
Chris@1063 3570 setNormalization(ColumnOp::NormalizeHybrid);
Chris@1009 3571 }
Chris@862 3572 }
Chris@153 3573
Chris@153 3574 bool normalizeVisibleArea =
Chris@1099 3575 (attributes.value("normalizeVisibleArea").trimmed() == "true");
Chris@862 3576 if (normalizeVisibleArea) {
Chris@1063 3577 setNormalization(ColumnOp::NormalizeVisibleArea);
Chris@862 3578 }
Chris@761 3579
Chris@1063 3580 if (!haveNewStyleNormalization && m_normalization == ColumnOp::NormalizeHybrid) {
Chris@1009 3581 // Tony v1.0 is (and hopefully will remain!) the only released
Chris@1009 3582 // SV-a-like to use old-style attributes when saving sessions
Chris@1009 3583 // that ask for hybrid normalization. It saves them with the
Chris@1009 3584 // wrong gain factor, so hack in a fix for that here -- this
Chris@1009 3585 // gives us backward but not forward compatibility.
Chris@1087 3586 setGain(m_gain / float(getFFTSize() / 2));
Chris@862 3587 }
Chris@11 3588 }
Chris@11 3589