annotate layer/SpectrogramLayer.cpp @ 1101:1364cbf4453d spectrogram-minor-refactor

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