annotate layer/SpectrogramLayer.cpp @ 1056:b4fd6c67fce5 3.0-integration

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