annotate layer/SpectrogramLayer.cpp @ 1087:6d990a24ac1b spectrogram-minor-refactor

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