annotate layer/SpectrogramLayer.cpp @ 1094:8a815776151c spectrogram-minor-refactor

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