annotate layer/SpectrogramLayer.cpp @ 1086:163cb9b98104 spectrogram-minor-refactor

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