annotate layer/SpectrogramLayer.cpp @ 1058:9a13bc339fa9 spectrogram-minor-refactor

Mid-refactor to pull out the bulk of paintDrawBuffer into chunks
author Chris Cannam
date Mon, 13 Jun 2016 16:17:44 +0100
parents b4fd6c67fce5
children e1c2dcc7790e
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@1058 2355 void
Chris@1058 2356 SpectrogramLayer::normalise(vector<float> &values, Normalization norm) const
Chris@1058 2357 {
Chris@1058 2358 if (norm == NormalizeColumns ||
Chris@1058 2359 norm == NormalizeHybrid) {
Chris@1058 2360
Chris@1058 2361 float max = 0.f;
Chris@1058 2362 for (int i = 0; in_range_for(i, values); ++i) {
Chris@1058 2363 if (i == 0 || values[i] > max) {
Chris@1058 2364 max = values[i];
Chris@1058 2365 }
Chris@1058 2366 }
Chris@1058 2367 if (max > 0.f) {
Chris@1058 2368 float scale = 1.f / max;
Chris@1058 2369 if (norm == NormalizeHybrid) {
Chris@1058 2370 scale = scale * log10f(max + 1.f);
Chris@1058 2371 }
Chris@1058 2372 for (int i = 0; in_range_for(i, values); ++i) {
Chris@1058 2373 values[i] *= scale;
Chris@1058 2374 }
Chris@1058 2375 }
Chris@1058 2376 }
Chris@1058 2377 }
Chris@1058 2378
Chris@1058 2379 vector<float>
Chris@1058 2380 SpectrogramLayer::getColumnFromFFTModel(FFTModel *fft,
Chris@1058 2381 int sx, // column number in model
Chris@1058 2382 int minbin,
Chris@1058 2383 int bincount) const
Chris@1058 2384 {
Chris@1058 2385 vector<float> values(bincount, 0.f);
Chris@1058 2386
Chris@1058 2387 if (m_colourScale == PhaseColourScale) {
Chris@1058 2388 fft->getPhasesAt(sx, values.data(), minbin, bincount);
Chris@1058 2389 } else {
Chris@1058 2390 fft->getMagnitudesAt(sx, values.data(), minbin, bincount);
Chris@1058 2391 }
Chris@1058 2392
Chris@1058 2393 return move(values);
Chris@1058 2394 }
Chris@1058 2395
Chris@1058 2396 vector<float>
Chris@1058 2397 SpectrogramLayer::getColumnFromGenericModel(DenseThreeDimensionalModel *model,
Chris@1058 2398 int sx, // column number in model
Chris@1058 2399 int minbin,
Chris@1058 2400 int bincount) const
Chris@1058 2401 {
Chris@1058 2402 if (m_colourScale == PhaseColourScale) {
Chris@1058 2403 throw std::logic_error("can't use phase scale with generic 3d model");
Chris@1058 2404 }
Chris@1058 2405
Chris@1058 2406 auto col = model->getColumn(sx);
Chris@1058 2407
Chris@1058 2408 return move(vector<float>(col.data() + minbin,
Chris@1058 2409 col.data() + minbin + bincount));
Chris@1058 2410 }
Chris@1058 2411
Chris@1058 2412 void
Chris@1058 2413 SpectrogramLayer::scaleColumn(vector<float> &col)
Chris@1058 2414 {
Chris@1058 2415 if (m_normalization != NormalizeColumns &&
Chris@1058 2416 m_normalization != NormalizeHybrid) {
Chris@1058 2417 float scale = 2.f / float(m_fftSize);
Chris@1058 2418 int n = int(col.size());
Chris@1058 2419 for (int i = 0; i < n; ++i) {
Chris@1058 2420 col[i] *= scale;
Chris@1058 2421 }
Chris@1058 2422 }
Chris@1058 2423 }
Chris@1058 2424
Chris@1058 2425 static bool
Chris@1058 2426 is_peak(const vector<float> &values, int ix)
Chris@1058 2427 {
Chris@1058 2428 if (!in_range_for(ix-1, values)) return false;
Chris@1058 2429 if (!in_range_for(ix+1, values)) return false;
Chris@1058 2430 if (values[ix] < values[ix+1]) return false;
Chris@1058 2431 if (values[ix] < values[ix-1]) return false;
Chris@1058 2432 return true;
Chris@1058 2433 }
Chris@1058 2434
Chris@1058 2435 vector<float>
Chris@1058 2436 SpectrogramLayer::distributeColumn(const vector<float> &in,
Chris@1058 2437 int h,
Chris@1058 2438 const vector<double> &binfory,
Chris@1058 2439 int minbin,
Chris@1058 2440 bool interpolate)
Chris@1058 2441 {
Chris@1058 2442 vector<float> out(h, 0.f);
Chris@1058 2443 int bins = int(in.size());
Chris@1058 2444
Chris@1058 2445 for (int y = 0; y < h; ++y) {
Chris@1058 2446
Chris@1058 2447 double sy0 = binfory[y] - minbin;
Chris@1058 2448 double sy1 = sy0 + 1;
Chris@1058 2449 if (y+1 < h) {
Chris@1058 2450 sy1 = binfory[y+1] - minbin;
Chris@1058 2451 }
Chris@1058 2452
Chris@1058 2453 if (interpolate && fabs(sy1 - sy0) < 1.0) {
Chris@1058 2454
Chris@1058 2455 double centre = (sy0 + sy1) / 2;
Chris@1058 2456 double dist = (centre - 0.5) - rint(centre - 0.5);
Chris@1058 2457 int bin = int(centre);
Chris@1058 2458
Chris@1058 2459 int other = (dist < 0 ? (bin-1) : (bin+1));
Chris@1058 2460
Chris@1058 2461 if (bin < 0) bin = 0;
Chris@1058 2462 if (bin >= bins) bin = bins-1;
Chris@1058 2463
Chris@1058 2464 if (other < 0 || other >= bins) {
Chris@1058 2465 other = bin;
Chris@1058 2466 }
Chris@1058 2467
Chris@1058 2468 if (m_binDisplay == PeakBins) {
Chris@1058 2469
Chris@1058 2470 if (is_peak(in, bin)) {
Chris@1058 2471 out[y] = in[bin];
Chris@1058 2472 } else if (other != bin && is_peak(in, other)) {
Chris@1058 2473 out[y] = in[other];
Chris@1058 2474 }
Chris@1058 2475
Chris@1058 2476 } else {
Chris@1058 2477
Chris@1058 2478 double prop = 1.0 - fabs(dist);
Chris@1058 2479
Chris@1058 2480 double v0 = in[bin];
Chris@1058 2481 double v1 = in[other];
Chris@1058 2482
Chris@1058 2483 out[y] = float(prop * v0 + (1.0 - prop) * v1);
Chris@1058 2484 }
Chris@1058 2485
Chris@1058 2486 } else { // not interpolating this one
Chris@1058 2487
Chris@1058 2488 int by0 = int(sy0 + 0.0001);
Chris@1058 2489 int by1 = int(sy1 + 0.0001);
Chris@1058 2490 if (by1 < by0 + 1) by1 = by0 + 1;
Chris@1058 2491
Chris@1058 2492 for (int bin = by0; bin < by1; ++bin) {
Chris@1058 2493
Chris@1058 2494 if (m_binDisplay == PeakBins && !is_peak(in, bin)) {
Chris@1058 2495 continue;
Chris@1058 2496 }
Chris@1058 2497
Chris@1058 2498 float value = in[bin];
Chris@1058 2499
Chris@1058 2500 if (value > out[y] || m_colourScale == PhaseColourScale) {
Chris@1058 2501 out[y] = value;
Chris@1058 2502 }
Chris@1058 2503 }
Chris@1058 2504 }
Chris@1058 2505 }
Chris@1058 2506
Chris@1058 2507 return out;
Chris@1058 2508 }
Chris@1058 2509
Chris@1058 2510 void
Chris@1058 2511 SpectrogramLayer::recordColumnExtents(const vector<float> &col,
Chris@1058 2512 int sx, // column index, for m_columnMags
Chris@1058 2513 MagnitudeRange &overallMag,
Chris@1058 2514 bool &overallMagChanged)
Chris@1058 2515 {
Chris@1058 2516 //!!! this differs from previous logic when in peak mode - as the
Chris@1058 2517 //!!! zeros between peaks are now sampled, where they were not
Chris@1058 2518 //!!! before
Chris@1058 2519
Chris@1058 2520 if (!in_range_for(sx, m_columnMags)) {
Chris@1058 2521 throw logic_error("sx out of range for m_columnMags");
Chris@1058 2522 }
Chris@1058 2523 MagnitudeRange mr;
Chris@1058 2524 for (auto v: col) {
Chris@1058 2525 mr.sample(v);
Chris@1058 2526 }
Chris@1058 2527 m_columnMags[sx] = mr;
Chris@1058 2528 if (overallMag.sample(mr)) {
Chris@1058 2529 overallMagChanged = true;
Chris@1058 2530 }
Chris@1058 2531 }
Chris@1058 2532
Chris@1058 2533 // order:
Chris@1058 2534 // get column -> scale -> distribute/interpolate -> record extents -> normalise -> apply display gain
Chris@1058 2535
Chris@1024 2536 int
Chris@918 2537 SpectrogramLayer::paintDrawBuffer(LayerGeometryProvider *v,
Chris@481 2538 int w,
Chris@481 2539 int h,
Chris@907 2540 const vector<int> &binforx,
Chris@907 2541 const vector<double> &binfory,
Chris@491 2542 bool usePeaksCache,
Chris@491 2543 MagnitudeRange &overallMag,
Chris@1026 2544 bool &overallMagChanged,
Chris@1039 2545 bool rightToLeft,
Chris@1039 2546 double softTimeLimit) const
Chris@480 2547 {
Chris@481 2548 Profiler profiler("SpectrogramLayer::paintDrawBuffer");
Chris@480 2549
Chris@490 2550 int minbin = int(binfory[0] + 0.0001);
Chris@907 2551 int maxbin = int(binfory[h-1]);
Chris@480 2552
Chris@485 2553 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@1026 2554 cerr << "SpectrogramLayer::paintDrawBuffer: minbin " << minbin << ", maxbin " << maxbin << "; w " << w << ", h " << h << endl;
Chris@485 2555 #endif
Chris@480 2556 if (minbin < 0) minbin = 0;
Chris@480 2557 if (maxbin < 0) maxbin = minbin+1;
Chris@480 2558
Chris@484 2559 DenseThreeDimensionalModel *sourceModel = 0;
Chris@484 2560 FFTModel *fft = 0;
Chris@484 2561 int divisor = 1;
Chris@485 2562 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@1026 2563 cerr << "SpectrogramLayer::paintDrawBuffer: Note: bin display = " << m_binDisplay << ", w = " << w << ", binforx[" << w-1 << "] = " << binforx[w-1] << ", binforx[0] = " << binforx[0] << endl;
Chris@485 2564 #endif
Chris@1054 2565 if (usePeaksCache) {
Chris@484 2566 sourceModel = getPeakCache(v);
Chris@1054 2567 divisor = m_peakCacheDivisor;
Chris@484 2568 minbin = 0;
Chris@484 2569 maxbin = sourceModel->getHeight();
Chris@484 2570 } else {
Chris@484 2571 sourceModel = fft = getFFTModel(v);
Chris@484 2572 }
Chris@484 2573
Chris@1024 2574 if (!sourceModel) return 0;
Chris@484 2575
Chris@490 2576 bool interpolate = false;
Chris@490 2577 Preferences::SpectrogramSmoothing smoothing =
Chris@490 2578 Preferences::getInstance()->getSpectrogramSmoothing();
Chris@490 2579 if (smoothing == Preferences::SpectrogramInterpolated ||
Chris@490 2580 smoothing == Preferences::SpectrogramZeroPaddedAndInterpolated) {
Chris@490 2581 if (m_binDisplay != PeakBins &&
Chris@490 2582 m_binDisplay != PeakFrequencies) {
Chris@490 2583 interpolate = true;
Chris@490 2584 }
Chris@490 2585 }
Chris@490 2586
Chris@480 2587 int psx = -1;
Chris@545 2588
Chris@545 2589 #ifdef __GNUC__
Chris@490 2590 float autoarray[maxbin - minbin + 1];
Chris@545 2591 float peaks[h];
Chris@545 2592 #else
Chris@545 2593 float *autoarray = (float *)alloca((maxbin - minbin + 1) * sizeof(float));
Chris@545 2594 float *peaks = (float *)alloca(h * sizeof(float));
Chris@545 2595 #endif
Chris@545 2596
Chris@490 2597 const float *values = autoarray;
Chris@484 2598 DenseThreeDimensionalModel::Column c;
Chris@480 2599
Chris@1027 2600 int minColumns = 4;
Chris@1039 2601 bool haveTimeLimits = (softTimeLimit > 0.0);
Chris@1037 2602 double hardTimeLimit = softTimeLimit * 2.0;
Chris@1037 2603 bool overridingSoftLimit = false;
Chris@1027 2604 auto startTime = chrono::steady_clock::now();
Chris@1027 2605
Chris@1026 2606 int start = 0;
Chris@1026 2607 int finish = w;
Chris@1026 2608 int step = 1;
Chris@1026 2609
Chris@1026 2610 if (rightToLeft) {
Chris@1026 2611 start = w-1;
Chris@1026 2612 finish = -1;
Chris@1026 2613 step = -1;
Chris@1026 2614 }
Chris@1027 2615
Chris@1027 2616 int columnCount = 0;
Chris@1026 2617
Chris@1026 2618 for (int x = start; x != finish; x += step) {
Chris@1027 2619
Chris@1056 2620 // x is the on-canvas pixel coord; sx (later) will be the
Chris@1056 2621 // source column index
Chris@1056 2622
Chris@1027 2623 ++columnCount;
Chris@480 2624
Chris@482 2625 if (binforx[x] < 0) continue;
Chris@482 2626
Chris@487 2627 float columnMax = 0.f;
Chris@487 2628
Chris@484 2629 int sx0 = binforx[x] / divisor;
Chris@483 2630 int sx1 = sx0;
Chris@484 2631 if (x+1 < w) sx1 = binforx[x+1] / divisor;
Chris@483 2632 if (sx0 < 0) sx0 = sx1 - 1;
Chris@483 2633 if (sx0 < 0) continue;
Chris@483 2634 if (sx1 <= sx0) sx1 = sx0 + 1;
Chris@483 2635
Chris@483 2636 for (int y = 0; y < h; ++y) peaks[y] = 0.f;
Chris@480 2637
Chris@483 2638 for (int sx = sx0; sx < sx1; ++sx) {
Chris@483 2639
Chris@518 2640 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@682 2641 // cerr << "sx = " << sx << endl;
Chris@518 2642 #endif
Chris@518 2643
Chris@484 2644 if (sx < 0 || sx >= int(sourceModel->getWidth())) continue;
Chris@483 2645
Chris@488 2646 MagnitudeRange mag;
Chris@488 2647
Chris@483 2648 if (sx != psx) {
Chris@484 2649 if (fft) {
Chris@485 2650 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@1026 2651 // cerr << "Retrieving column " << sx << " from fft directly" << endl;
Chris@485 2652 #endif
Chris@487 2653 if (m_colourScale == PhaseColourScale) {
Chris@490 2654 fft->getPhasesAt(sx, autoarray, minbin, maxbin - minbin + 1);
Chris@862 2655 } else if (m_normalization == NormalizeColumns) {
Chris@490 2656 fft->getNormalizedMagnitudesAt(sx, autoarray, minbin, maxbin - minbin + 1);
Chris@862 2657 } else if (m_normalization == NormalizeHybrid) {
Chris@1056 2658 float max = fft->getNormalizedMagnitudesAt
Chris@1056 2659 (sx, autoarray, minbin, maxbin - minbin + 1);
Chris@988 2660 float scale = log10f(max + 1.f);
Chris@719 2661 for (int i = minbin; i <= maxbin; ++i) {
Chris@862 2662 autoarray[i - minbin] *= scale;
Chris@719 2663 }
Chris@487 2664 } else {
Chris@490 2665 fft->getMagnitudesAt(sx, autoarray, minbin, maxbin - minbin + 1);
Chris@487 2666 }
Chris@484 2667 } else {
Chris@485 2668 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@1026 2669 // cerr << "Retrieving column " << sx << " from peaks cache" << endl;
Chris@485 2670 #endif
Chris@484 2671 c = sourceModel->getColumn(sx);
Chris@862 2672 if (m_normalization == NormalizeColumns ||
Chris@862 2673 m_normalization == NormalizeHybrid) {
Chris@487 2674 for (int y = 0; y < h; ++y) {
Chris@487 2675 if (c[y] > columnMax) columnMax = c[y];
Chris@487 2676 }
Chris@487 2677 }
Chris@1019 2678 values = c.data() + minbin;
Chris@484 2679 }
Chris@483 2680 psx = sx;
Chris@483 2681 }
Chris@483 2682
Chris@483 2683 for (int y = 0; y < h; ++y) {
Chris@480 2684
Chris@905 2685 double sy0 = binfory[y];
Chris@905 2686 double sy1 = sy0 + 1;
Chris@481 2687 if (y+1 < h) sy1 = binfory[y+1];
Chris@490 2688
Chris@907 2689 double value = 0.0;
Chris@907 2690
Chris@907 2691 if (interpolate && fabs(sy1 - sy0) < 1.0) {
Chris@490 2692
Chris@905 2693 double centre = (sy0 + sy1) / 2;
Chris@907 2694 double dist = (centre - 0.5) - rint(centre - 0.5);
Chris@490 2695 int bin = int(centre);
Chris@490 2696 int other = (dist < 0 ? (bin-1) : (bin+1));
Chris@490 2697 if (bin < minbin) bin = minbin;
Chris@490 2698 if (bin > maxbin) bin = maxbin;
Chris@490 2699 if (other < minbin || other > maxbin) other = bin;
Chris@907 2700 double prop = 1.0 - fabs(dist);
Chris@905 2701
Chris@905 2702 double v0 = values[bin - minbin];
Chris@905 2703 double v1 = values[other - minbin];
Chris@490 2704 if (m_binDisplay == PeakBins) {
Chris@490 2705 if (bin == minbin || bin == maxbin ||
Chris@490 2706 v0 < values[bin-minbin-1] ||
Chris@907 2707 v0 < values[bin-minbin+1]) v0 = 0.0;
Chris@490 2708 if (other == minbin || other == maxbin ||
Chris@490 2709 v1 < values[other-minbin-1] ||
Chris@907 2710 v1 < values[other-minbin+1]) v1 = 0.0;
Chris@489 2711 }
Chris@907 2712 if (v0 == 0.0 && v1 == 0.0) continue;
Chris@907 2713 value = prop * v0 + (1.0 - prop) * v1;
Chris@484 2714
Chris@488 2715 if (m_colourScale != PhaseColourScale) {
Chris@862 2716 if (m_normalization != NormalizeColumns &&
Chris@862 2717 m_normalization != NormalizeHybrid) {
Chris@907 2718 value /= (m_fftSize/2.0);
Chris@488 2719 }
Chris@907 2720 mag.sample(float(value));
Chris@488 2721 value *= m_gain;
Chris@488 2722 }
Chris@488 2723
Chris@907 2724 peaks[y] = float(value);
Chris@490 2725
Chris@490 2726 } else {
Chris@490 2727
Chris@490 2728 int by0 = int(sy0 + 0.0001);
Chris@490 2729 int by1 = int(sy1 + 0.0001);
Chris@490 2730 if (by1 < by0 + 1) by1 = by0 + 1;
Chris@490 2731
Chris@490 2732 for (int bin = by0; bin < by1; ++bin) {
Chris@490 2733
Chris@490 2734 value = values[bin - minbin];
Chris@490 2735 if (m_binDisplay == PeakBins) {
Chris@490 2736 if (bin == minbin || bin == maxbin ||
Chris@490 2737 value < values[bin-minbin-1] ||
Chris@490 2738 value < values[bin-minbin+1]) continue;
Chris@480 2739 }
Chris@490 2740
Chris@490 2741 if (m_colourScale != PhaseColourScale) {
Chris@862 2742 if (m_normalization != NormalizeColumns &&
Chris@862 2743 m_normalization != NormalizeHybrid) {
Chris@907 2744 value /= (m_fftSize/2.0);
Chris@490 2745 }
Chris@907 2746 mag.sample(float(value));
Chris@490 2747 value *= m_gain;
Chris@490 2748 }
Chris@490 2749
Chris@907 2750 if (value > peaks[y]) {
Chris@907 2751 peaks[y] = float(value); //!!! not right for phase!
Chris@907 2752 }
Chris@480 2753 }
Chris@480 2754 }
Chris@483 2755 }
Chris@488 2756
Chris@488 2757 if (mag.isSet()) {
Chris@488 2758 if (sx >= int(m_columnMags.size())) {
Chris@540 2759 #ifdef DEBUG_SPECTROGRAM
Chris@682 2760 cerr << "INTERNAL ERROR: " << sx << " >= "
Chris@488 2761 << m_columnMags.size()
Chris@488 2762 << " at SpectrogramLayer.cpp::paintDrawBuffer"
Chris@682 2763 << endl;
Chris@540 2764 #endif
Chris@490 2765 } else {
Chris@490 2766 m_columnMags[sx].sample(mag);
Chris@491 2767 if (overallMag.sample(mag)) overallMagChanged = true;
Chris@488 2768 }
Chris@488 2769 }
Chris@483 2770 }
Chris@483 2771
Chris@1056 2772 // at this point we have updated m_columnMags and overallMag
Chris@1056 2773 // -- used elsewhere for calculating the overall view range
Chris@1056 2774 // for NormalizeVisibleArea mode -- and calculated "peaks"
Chris@1056 2775 // (the possibly scaled and interpolated value array from
Chris@1056 2776 // which we actually draw the column) and "columnMax" (maximum
Chris@1056 2777 // value used for normalisation)
Chris@1056 2778
Chris@483 2779 for (int y = 0; y < h; ++y) {
Chris@483 2780
Chris@905 2781 double peak = peaks[y];
Chris@483 2782
Chris@488 2783 if (m_colourScale != PhaseColourScale &&
Chris@862 2784 (m_normalization == NormalizeColumns ||
Chris@862 2785 m_normalization == NormalizeHybrid) &&
Chris@488 2786 columnMax > 0.f) {
Chris@488 2787 peak /= columnMax;
Chris@862 2788 if (m_normalization == NormalizeHybrid) {
Chris@862 2789 peak *= log10(columnMax + 1.f);
Chris@719 2790 }
Chris@480 2791 }
Chris@483 2792
Chris@483 2793 unsigned char peakpix = getDisplayValue(v, peak);
Chris@480 2794
Chris@480 2795 m_drawBuffer.setPixel(x, h-y-1, peakpix);
Chris@480 2796 }
Chris@1025 2797
Chris@1039 2798 if (haveTimeLimits) {
Chris@1027 2799 if (columnCount >= minColumns) {
Chris@1025 2800 auto t = chrono::steady_clock::now();
Chris@1025 2801 double diff = chrono::duration<double>(t - startTime).count();
Chris@1037 2802 if (diff > hardTimeLimit) {
Chris@1025 2803 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@1037 2804 cerr << "SpectrogramLayer::paintDrawBuffer: hard limit " << hardTimeLimit << " sec exceeded after "
Chris@1031 2805 << columnCount << " columns with time " << diff << endl;
Chris@1025 2806 #endif
Chris@1027 2807 return columnCount;
Chris@1037 2808 } else if (diff > softTimeLimit && !overridingSoftLimit) {
Chris@1037 2809 // If we're more than half way through by the time
Chris@1037 2810 // we reach the soft limit, ignore it (though
Chris@1037 2811 // still respect the hard limit, above). Otherwise
Chris@1037 2812 // respect the soft limit and return now.
Chris@1037 2813 if (columnCount > w/2) {
Chris@1037 2814 overridingSoftLimit = true;
Chris@1037 2815 } else {
Chris@1037 2816 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@1037 2817 cerr << "SpectrogramLayer::paintDrawBuffer: soft limit " << softTimeLimit << " sec exceeded after "
Chris@1037 2818 << columnCount << " columns with time " << diff << endl;
Chris@1037 2819 #endif
Chris@1037 2820 return columnCount;
Chris@1037 2821 }
Chris@1037 2822 }
Chris@1025 2823 }
Chris@1025 2824 }
Chris@480 2825 }
Chris@1024 2826
Chris@1027 2827 return columnCount;
Chris@480 2828 }
Chris@477 2829
Chris@121 2830 void
Chris@918 2831 SpectrogramLayer::illuminateLocalFeatures(LayerGeometryProvider *v, QPainter &paint) const
Chris@121 2832 {
Chris@382 2833 Profiler profiler("SpectrogramLayer::illuminateLocalFeatures");
Chris@382 2834
Chris@121 2835 QPoint localPos;
Chris@121 2836 if (!v->shouldIlluminateLocalFeatures(this, localPos) || !m_model) {
Chris@121 2837 return;
Chris@121 2838 }
Chris@121 2839
Chris@682 2840 // cerr << "SpectrogramLayer: illuminateLocalFeatures("
Chris@682 2841 // << localPos.x() << "," << localPos.y() << ")" << endl;
Chris@121 2842
Chris@905 2843 double s0, s1;
Chris@905 2844 double f0, f1;
Chris@121 2845
Chris@121 2846 if (getXBinRange(v, localPos.x(), s0, s1) &&
Chris@121 2847 getYBinSourceRange(v, localPos.y(), f0, f1)) {
Chris@121 2848
Chris@121 2849 int s0i = int(s0 + 0.001);
Chris@121 2850 int s1i = int(s1);
Chris@121 2851
Chris@121 2852 int x0 = v->getXForFrame(s0i * getWindowIncrement());
Chris@121 2853 int x1 = v->getXForFrame((s1i + 1) * getWindowIncrement());
Chris@121 2854
Chris@248 2855 int y1 = int(getYForFrequency(v, f1));
Chris@248 2856 int y0 = int(getYForFrequency(v, f0));
Chris@121 2857
Chris@682 2858 // cerr << "SpectrogramLayer: illuminate "
Chris@682 2859 // << x0 << "," << y1 << " -> " << x1 << "," << y0 << endl;
Chris@121 2860
Chris@287 2861 paint.setPen(v->getForeground());
Chris@133 2862
Chris@133 2863 //!!! should we be using paintCrosshairs for this?
Chris@133 2864
Chris@121 2865 paint.drawRect(x0, y1, x1 - x0 + 1, y0 - y1 + 1);
Chris@121 2866 }
Chris@121 2867 }
Chris@121 2868
Chris@905 2869 double
Chris@918 2870 SpectrogramLayer::getYForFrequency(const LayerGeometryProvider *v, double frequency) const
Chris@42 2871 {
Chris@44 2872 return v->getYForFrequency(frequency,
Chris@44 2873 getEffectiveMinFrequency(),
Chris@44 2874 getEffectiveMaxFrequency(),
Chris@44 2875 m_frequencyScale == LogFrequencyScale);
Chris@42 2876 }
Chris@42 2877
Chris@905 2878 double
Chris@918 2879 SpectrogramLayer::getFrequencyForY(const LayerGeometryProvider *v, int y) const
Chris@42 2880 {
Chris@44 2881 return v->getFrequencyForY(y,
Chris@44 2882 getEffectiveMinFrequency(),
Chris@44 2883 getEffectiveMaxFrequency(),
Chris@44 2884 m_frequencyScale == LogFrequencyScale);
Chris@42 2885 }
Chris@42 2886
Chris@0 2887 int
Chris@918 2888 SpectrogramLayer::getCompletion(LayerGeometryProvider *v) const
Chris@0 2889 {
Chris@920 2890 const View *view = v->getView();
Chris@920 2891
Chris@1030 2892 if (m_fftModels.find(view->getId()) == m_fftModels.end()) return 100;
Chris@1030 2893
Chris@1030 2894 int completion = m_fftModels[view->getId()]->getCompletion();
Chris@224 2895 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@985 2896 cerr << "SpectrogramLayer::getCompletion: completion = " << completion << endl;
Chris@224 2897 #endif
Chris@0 2898 return completion;
Chris@0 2899 }
Chris@0 2900
Chris@583 2901 QString
Chris@918 2902 SpectrogramLayer::getError(LayerGeometryProvider *v) const
Chris@583 2903 {
Chris@920 2904 const View *view = v->getView();
Chris@1030 2905 if (m_fftModels.find(view->getId()) == m_fftModels.end()) return "";
Chris@1030 2906 return m_fftModels[view->getId()]->getError();
Chris@583 2907 }
Chris@583 2908
Chris@28 2909 bool
Chris@905 2910 SpectrogramLayer::getValueExtents(double &min, double &max,
Chris@101 2911 bool &logarithmic, QString &unit) const
Chris@79 2912 {
Chris@133 2913 if (!m_model) return false;
Chris@133 2914
Chris@907 2915 sv_samplerate_t sr = m_model->getSampleRate();
Chris@905 2916 min = double(sr) / m_fftSize;
Chris@905 2917 max = double(sr) / 2;
Chris@133 2918
Chris@101 2919 logarithmic = (m_frequencyScale == LogFrequencyScale);
Chris@79 2920 unit = "Hz";
Chris@79 2921 return true;
Chris@79 2922 }
Chris@79 2923
Chris@79 2924 bool
Chris@905 2925 SpectrogramLayer::getDisplayExtents(double &min, double &max) const
Chris@101 2926 {
Chris@101 2927 min = getEffectiveMinFrequency();
Chris@101 2928 max = getEffectiveMaxFrequency();
Chris@253 2929
Chris@587 2930 // SVDEBUG << "SpectrogramLayer::getDisplayExtents: " << min << "->" << max << endl;
Chris@101 2931 return true;
Chris@101 2932 }
Chris@101 2933
Chris@101 2934 bool
Chris@905 2935 SpectrogramLayer::setDisplayExtents(double min, double max)
Chris@120 2936 {
Chris@120 2937 if (!m_model) return false;
Chris@187 2938
Chris@587 2939 // SVDEBUG << "SpectrogramLayer::setDisplayExtents: " << min << "->" << max << endl;
Chris@187 2940
Chris@120 2941 if (min < 0) min = 0;
Chris@907 2942 if (max > m_model->getSampleRate()/2.0) max = m_model->getSampleRate()/2.0;
Chris@120 2943
Chris@907 2944 int minf = int(lrint(min));
Chris@907 2945 int maxf = int(lrint(max));
Chris@120 2946
Chris@120 2947 if (m_minFrequency == minf && m_maxFrequency == maxf) return true;
Chris@120 2948
Chris@478 2949 invalidateImageCaches();
Chris@120 2950 invalidateMagnitudes();
Chris@120 2951
Chris@120 2952 m_minFrequency = minf;
Chris@120 2953 m_maxFrequency = maxf;
Chris@120 2954
Chris@120 2955 emit layerParametersChanged();
Chris@120 2956
Chris@133 2957 int vs = getCurrentVerticalZoomStep();
Chris@133 2958 if (vs != m_lastEmittedZoomStep) {
Chris@133 2959 emit verticalZoomChanged();
Chris@133 2960 m_lastEmittedZoomStep = vs;
Chris@133 2961 }
Chris@133 2962
Chris@120 2963 return true;
Chris@120 2964 }
Chris@120 2965
Chris@120 2966 bool
Chris@918 2967 SpectrogramLayer::getYScaleValue(const LayerGeometryProvider *v, int y,
Chris@905 2968 double &value, QString &unit) const
Chris@261 2969 {
Chris@261 2970 value = getFrequencyForY(v, y);
Chris@261 2971 unit = "Hz";
Chris@261 2972 return true;
Chris@261 2973 }
Chris@261 2974
Chris@261 2975 bool
Chris@918 2976 SpectrogramLayer::snapToFeatureFrame(LayerGeometryProvider *,
Chris@907 2977 sv_frame_t &frame,
Chris@805 2978 int &resolution,
Chris@28 2979 SnapType snap) const
Chris@13 2980 {
Chris@13 2981 resolution = getWindowIncrement();
Chris@907 2982 sv_frame_t left = (frame / resolution) * resolution;
Chris@907 2983 sv_frame_t right = left + resolution;
Chris@28 2984
Chris@28 2985 switch (snap) {
Chris@28 2986 case SnapLeft: frame = left; break;
Chris@28 2987 case SnapRight: frame = right; break;
Chris@28 2988 case SnapNearest:
Chris@28 2989 case SnapNeighbouring:
Chris@28 2990 if (frame - left > right - frame) frame = right;
Chris@28 2991 else frame = left;
Chris@28 2992 break;
Chris@28 2993 }
Chris@28 2994
Chris@28 2995 return true;
Chris@28 2996 }
Chris@13 2997
Chris@283 2998 void
Chris@918 2999 SpectrogramLayer::measureDoubleClick(LayerGeometryProvider *v, QMouseEvent *e)
Chris@283 3000 {
Chris@920 3001 const View *view = v->getView();
Chris@1030 3002 ScrollableImageCache &cache = getImageCacheReference(view);
Chris@1030 3003
Chris@1030 3004 cerr << "cache width: " << cache.getSize().width() << ", height: "
Chris@1030 3005 << cache.getSize().height() << endl;
Chris@1030 3006
Chris@1030 3007 QImage image = cache.getImage();
Chris@283 3008
Chris@283 3009 ImageRegionFinder finder;
Chris@283 3010 QRect rect = finder.findRegionExtents(&image, e->pos());
Chris@283 3011 if (rect.isValid()) {
Chris@283 3012 MeasureRect mr;
Chris@283 3013 setMeasureRectFromPixrect(v, mr, rect);
Chris@283 3014 CommandHistory::getInstance()->addCommand
Chris@283 3015 (new AddMeasurementRectCommand(this, mr));
Chris@283 3016 }
Chris@283 3017 }
Chris@283 3018
Chris@77 3019 bool
Chris@918 3020 SpectrogramLayer::getCrosshairExtents(LayerGeometryProvider *v, QPainter &paint,
Chris@77 3021 QPoint cursorPos,
Chris@1025 3022 vector<QRect> &extents) const
Chris@77 3023 {
Chris@918 3024 QRect vertical(cursorPos.x() - 12, 0, 12, v->getPaintHeight());
Chris@77 3025 extents.push_back(vertical);
Chris@77 3026
Chris@77 3027 QRect horizontal(0, cursorPos.y(), cursorPos.x(), 1);
Chris@77 3028 extents.push_back(horizontal);
Chris@77 3029
Chris@608 3030 int sw = getVerticalScaleWidth(v, m_haveDetailedScale, paint);
Chris@264 3031
Chris@280 3032 QRect freq(sw, cursorPos.y() - paint.fontMetrics().ascent() - 2,
Chris@280 3033 paint.fontMetrics().width("123456 Hz") + 2,
Chris@280 3034 paint.fontMetrics().height());
Chris@280 3035 extents.push_back(freq);
Chris@264 3036
Chris@279 3037 QRect pitch(sw, cursorPos.y() + 2,
Chris@279 3038 paint.fontMetrics().width("C#10+50c") + 2,
Chris@279 3039 paint.fontMetrics().height());
Chris@279 3040 extents.push_back(pitch);
Chris@279 3041
Chris@280 3042 QRect rt(cursorPos.x(),
Chris@918 3043 v->getPaintHeight() - paint.fontMetrics().height() - 2,
Chris@280 3044 paint.fontMetrics().width("1234.567 s"),
Chris@280 3045 paint.fontMetrics().height());
Chris@280 3046 extents.push_back(rt);
Chris@280 3047
Chris@280 3048 int w(paint.fontMetrics().width("1234567890") + 2);
Chris@280 3049 QRect frame(cursorPos.x() - w - 2,
Chris@918 3050 v->getPaintHeight() - paint.fontMetrics().height() - 2,
Chris@280 3051 w,
Chris@280 3052 paint.fontMetrics().height());
Chris@280 3053 extents.push_back(frame);
Chris@280 3054
Chris@77 3055 return true;
Chris@77 3056 }
Chris@77 3057
Chris@77 3058 void
Chris@918 3059 SpectrogramLayer::paintCrosshairs(LayerGeometryProvider *v, QPainter &paint,
Chris@77 3060 QPoint cursorPos) const
Chris@77 3061 {
Chris@77 3062 paint.save();
Chris@283 3063
Chris@608 3064 int sw = getVerticalScaleWidth(v, m_haveDetailedScale, paint);
Chris@283 3065
Chris@282 3066 QFont fn = paint.font();
Chris@282 3067 if (fn.pointSize() > 8) {
Chris@282 3068 fn.setPointSize(fn.pointSize() - 1);
Chris@282 3069 paint.setFont(fn);
Chris@282 3070 }
Chris@77 3071 paint.setPen(m_crosshairColour);
Chris@77 3072
Chris@77 3073 paint.drawLine(0, cursorPos.y(), cursorPos.x() - 1, cursorPos.y());
Chris@918 3074 paint.drawLine(cursorPos.x(), 0, cursorPos.x(), v->getPaintHeight());
Chris@77 3075
Chris@905 3076 double fundamental = getFrequencyForY(v, cursorPos.y());
Chris@77 3077
Chris@278 3078 v->drawVisibleText(paint,
Chris@278 3079 sw + 2,
Chris@278 3080 cursorPos.y() - 2,
Chris@278 3081 QString("%1 Hz").arg(fundamental),
Chris@278 3082 View::OutlinedText);
Chris@278 3083
Chris@279 3084 if (Pitch::isFrequencyInMidiRange(fundamental)) {
Chris@279 3085 QString pitchLabel = Pitch::getPitchLabelForFrequency(fundamental);
Chris@279 3086 v->drawVisibleText(paint,
Chris@279 3087 sw + 2,
Chris@279 3088 cursorPos.y() + paint.fontMetrics().ascent() + 2,
Chris@279 3089 pitchLabel,
Chris@279 3090 View::OutlinedText);
Chris@279 3091 }
Chris@279 3092
Chris@907 3093 sv_frame_t frame = v->getFrameForX(cursorPos.x());
Chris@279 3094 RealTime rt = RealTime::frame2RealTime(frame, m_model->getSampleRate());
Chris@280 3095 QString rtLabel = QString("%1 s").arg(rt.toText(true).c_str());
Chris@280 3096 QString frameLabel = QString("%1").arg(frame);
Chris@280 3097 v->drawVisibleText(paint,
Chris@280 3098 cursorPos.x() - paint.fontMetrics().width(frameLabel) - 2,
Chris@918 3099 v->getPaintHeight() - 2,
Chris@280 3100 frameLabel,
Chris@280 3101 View::OutlinedText);
Chris@280 3102 v->drawVisibleText(paint,
Chris@280 3103 cursorPos.x() + 2,
Chris@918 3104 v->getPaintHeight() - 2,
Chris@280 3105 rtLabel,
Chris@280 3106 View::OutlinedText);
Chris@264 3107
Chris@77 3108 int harmonic = 2;
Chris@77 3109
Chris@77 3110 while (harmonic < 100) {
Chris@77 3111
Chris@907 3112 int hy = int(lrint(getYForFrequency(v, fundamental * harmonic)));
Chris@918 3113 if (hy < 0 || hy > v->getPaintHeight()) break;
Chris@77 3114
Chris@77 3115 int len = 7;
Chris@77 3116
Chris@77 3117 if (harmonic % 2 == 0) {
Chris@77 3118 if (harmonic % 4 == 0) {
Chris@77 3119 len = 12;
Chris@77 3120 } else {
Chris@77 3121 len = 10;
Chris@77 3122 }
Chris@77 3123 }
Chris@77 3124
Chris@77 3125 paint.drawLine(cursorPos.x() - len,
Chris@907 3126 hy,
Chris@77 3127 cursorPos.x(),
Chris@907 3128 hy);
Chris@77 3129
Chris@77 3130 ++harmonic;
Chris@77 3131 }
Chris@77 3132
Chris@77 3133 paint.restore();
Chris@77 3134 }
Chris@77 3135
Chris@25 3136 QString
Chris@918 3137 SpectrogramLayer::getFeatureDescription(LayerGeometryProvider *v, QPoint &pos) const
Chris@25 3138 {
Chris@25 3139 int x = pos.x();
Chris@25 3140 int y = pos.y();
Chris@0 3141
Chris@25 3142 if (!m_model || !m_model->isOK()) return "";
Chris@0 3143
Chris@905 3144 double magMin = 0, magMax = 0;
Chris@905 3145 double phaseMin = 0, phaseMax = 0;
Chris@905 3146 double freqMin = 0, freqMax = 0;
Chris@905 3147 double adjFreqMin = 0, adjFreqMax = 0;
Chris@25 3148 QString pitchMin, pitchMax;
Chris@0 3149 RealTime rtMin, rtMax;
Chris@0 3150
Chris@38 3151 bool haveValues = false;
Chris@0 3152
Chris@44 3153 if (!getXBinSourceRange(v, x, rtMin, rtMax)) {
Chris@38 3154 return "";
Chris@38 3155 }
Chris@44 3156 if (getXYBinSourceRange(v, x, y, magMin, magMax, phaseMin, phaseMax)) {
Chris@38 3157 haveValues = true;
Chris@38 3158 }
Chris@0 3159
Chris@35 3160 QString adjFreqText = "", adjPitchText = "";
Chris@35 3161
Chris@38 3162 if (m_binDisplay == PeakFrequencies) {
Chris@35 3163
Chris@44 3164 if (!getAdjustedYBinSourceRange(v, x, y, freqMin, freqMax,
Chris@38 3165 adjFreqMin, adjFreqMax)) {
Chris@38 3166 return "";
Chris@38 3167 }
Chris@35 3168
Chris@35 3169 if (adjFreqMin != adjFreqMax) {
Chris@65 3170 adjFreqText = tr("Peak Frequency:\t%1 - %2 Hz\n")
Chris@35 3171 .arg(adjFreqMin).arg(adjFreqMax);
Chris@35 3172 } else {
Chris@65 3173 adjFreqText = tr("Peak Frequency:\t%1 Hz\n")
Chris@35 3174 .arg(adjFreqMin);
Chris@38 3175 }
Chris@38 3176
Chris@38 3177 QString pmin = Pitch::getPitchLabelForFrequency(adjFreqMin);
Chris@38 3178 QString pmax = Pitch::getPitchLabelForFrequency(adjFreqMax);
Chris@38 3179
Chris@38 3180 if (pmin != pmax) {
Chris@65 3181 adjPitchText = tr("Peak Pitch:\t%3 - %4\n").arg(pmin).arg(pmax);
Chris@38 3182 } else {
Chris@65 3183 adjPitchText = tr("Peak Pitch:\t%2\n").arg(pmin);
Chris@35 3184 }
Chris@35 3185
Chris@35 3186 } else {
Chris@35 3187
Chris@44 3188 if (!getYBinSourceRange(v, y, freqMin, freqMax)) return "";
Chris@35 3189 }
Chris@35 3190
Chris@25 3191 QString text;
Chris@25 3192
Chris@25 3193 if (rtMin != rtMax) {
Chris@25 3194 text += tr("Time:\t%1 - %2\n")
Chris@25 3195 .arg(rtMin.toText(true).c_str())
Chris@25 3196 .arg(rtMax.toText(true).c_str());
Chris@25 3197 } else {
Chris@25 3198 text += tr("Time:\t%1\n")
Chris@25 3199 .arg(rtMin.toText(true).c_str());
Chris@0 3200 }
Chris@0 3201
Chris@25 3202 if (freqMin != freqMax) {
Chris@65 3203 text += tr("%1Bin Frequency:\t%2 - %3 Hz\n%4Bin Pitch:\t%5 - %6\n")
Chris@65 3204 .arg(adjFreqText)
Chris@25 3205 .arg(freqMin)
Chris@25 3206 .arg(freqMax)
Chris@65 3207 .arg(adjPitchText)
Chris@65 3208 .arg(Pitch::getPitchLabelForFrequency(freqMin))
Chris@65 3209 .arg(Pitch::getPitchLabelForFrequency(freqMax));
Chris@65 3210 } else {
Chris@65 3211 text += tr("%1Bin Frequency:\t%2 Hz\n%3Bin Pitch:\t%4\n")
Chris@35 3212 .arg(adjFreqText)
Chris@25 3213 .arg(freqMin)
Chris@65 3214 .arg(adjPitchText)
Chris@65 3215 .arg(Pitch::getPitchLabelForFrequency(freqMin));
Chris@25 3216 }
Chris@25 3217
Chris@38 3218 if (haveValues) {
Chris@905 3219 double dbMin = AudioLevel::multiplier_to_dB(magMin);
Chris@905 3220 double dbMax = AudioLevel::multiplier_to_dB(magMax);
Chris@43 3221 QString dbMinString;
Chris@43 3222 QString dbMaxString;
Chris@43 3223 if (dbMin == AudioLevel::DB_FLOOR) {
Chris@43 3224 dbMinString = tr("-Inf");
Chris@43 3225 } else {
Chris@907 3226 dbMinString = QString("%1").arg(lrint(dbMin));
Chris@43 3227 }
Chris@43 3228 if (dbMax == AudioLevel::DB_FLOOR) {
Chris@43 3229 dbMaxString = tr("-Inf");
Chris@43 3230 } else {
Chris@907 3231 dbMaxString = QString("%1").arg(lrint(dbMax));
Chris@43 3232 }
Chris@907 3233 if (lrint(dbMin) != lrint(dbMax)) {
Chris@199 3234 text += tr("dB:\t%1 - %2").arg(dbMinString).arg(dbMaxString);
Chris@25 3235 } else {
Chris@199 3236 text += tr("dB:\t%1").arg(dbMinString);
Chris@25 3237 }
Chris@38 3238 if (phaseMin != phaseMax) {
Chris@38 3239 text += tr("\nPhase:\t%1 - %2").arg(phaseMin).arg(phaseMax);
Chris@38 3240 } else {
Chris@38 3241 text += tr("\nPhase:\t%1").arg(phaseMin);
Chris@38 3242 }
Chris@25 3243 }
Chris@25 3244
Chris@25 3245 return text;
Chris@0 3246 }
Chris@25 3247
Chris@0 3248 int
Chris@40 3249 SpectrogramLayer::getColourScaleWidth(QPainter &paint) const
Chris@40 3250 {
Chris@40 3251 int cw;
Chris@40 3252
Chris@119 3253 cw = paint.fontMetrics().width("-80dB");
Chris@119 3254
Chris@40 3255 return cw;
Chris@40 3256 }
Chris@40 3257
Chris@40 3258 int
Chris@918 3259 SpectrogramLayer::getVerticalScaleWidth(LayerGeometryProvider *, bool detailed, QPainter &paint) const
Chris@0 3260 {
Chris@0 3261 if (!m_model || !m_model->isOK()) return 0;
Chris@0 3262
Chris@607 3263 int cw = 0;
Chris@607 3264 if (detailed) cw = getColourScaleWidth(paint);
Chris@40 3265
Chris@0 3266 int tw = paint.fontMetrics().width(QString("%1")
Chris@0 3267 .arg(m_maxFrequency > 0 ?
Chris@0 3268 m_maxFrequency - 1 :
Chris@0 3269 m_model->getSampleRate() / 2));
Chris@0 3270
Chris@234 3271 int fw = paint.fontMetrics().width(tr("43Hz"));
Chris@0 3272 if (tw < fw) tw = fw;
Chris@40 3273
Chris@40 3274 int tickw = (m_frequencyScale == LogFrequencyScale ? 10 : 4);
Chris@0 3275
Chris@40 3276 return cw + tickw + tw + 13;
Chris@0 3277 }
Chris@0 3278
Chris@0 3279 void
Chris@918 3280 SpectrogramLayer::paintVerticalScale(LayerGeometryProvider *v, bool detailed, QPainter &paint, QRect rect) const
Chris@0 3281 {
Chris@0 3282 if (!m_model || !m_model->isOK()) {
Chris@0 3283 return;
Chris@0 3284 }
Chris@0 3285
Chris@382 3286 Profiler profiler("SpectrogramLayer::paintVerticalScale");
Chris@122 3287
Chris@120 3288 //!!! cache this?
Chris@120 3289
Chris@0 3290 int h = rect.height(), w = rect.width();
Chris@0 3291
Chris@40 3292 int tickw = (m_frequencyScale == LogFrequencyScale ? 10 : 4);
Chris@40 3293 int pkw = (m_frequencyScale == LogFrequencyScale ? 10 : 0);
Chris@40 3294
Chris@805 3295 int bins = m_fftSize / 2;
Chris@907 3296 sv_samplerate_t sr = m_model->getSampleRate();
Chris@0 3297
Chris@0 3298 if (m_maxFrequency > 0) {
Chris@107 3299 bins = int((double(m_maxFrequency) * m_fftSize) / sr + 0.1);
Chris@107 3300 if (bins > m_fftSize / 2) bins = m_fftSize / 2;
Chris@0 3301 }
Chris@0 3302
Chris@607 3303 int cw = 0;
Chris@607 3304
Chris@607 3305 if (detailed) cw = getColourScaleWidth(paint);
Chris@119 3306 int cbw = paint.fontMetrics().width("dB");
Chris@40 3307
Chris@0 3308 int py = -1;
Chris@0 3309 int textHeight = paint.fontMetrics().height();
Chris@0 3310 int toff = -textHeight + paint.fontMetrics().ascent() + 2;
Chris@0 3311
Chris@607 3312 if (detailed && (h > textHeight * 3 + 10)) {
Chris@119 3313
Chris@119 3314 int topLines = 2;
Chris@119 3315 if (m_colourScale == PhaseColourScale) topLines = 1;
Chris@119 3316
Chris@119 3317 int ch = h - textHeight * (topLines + 1) - 8;
Chris@119 3318 // paint.drawRect(4, textHeight + 4, cw - 1, ch + 1);
Chris@119 3319 paint.drawRect(4 + cw - cbw, textHeight * topLines + 4, cbw - 1, ch + 1);
Chris@40 3320
Chris@40 3321 QString top, bottom;
Chris@1030 3322 double min = m_viewMags[v->getId()].getMin();
Chris@1030 3323 double max = m_viewMags[v->getId()].getMax();
Chris@905 3324
Chris@905 3325 double dBmin = AudioLevel::multiplier_to_dB(min);
Chris@905 3326 double dBmax = AudioLevel::multiplier_to_dB(max);
Chris@119 3327
Chris@1044 3328 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@1044 3329 cerr << "paintVerticalScale: for view id " << v->getId()
Chris@1044 3330 << ": min = " << min << ", max = " << max
Chris@1044 3331 << ", dBmin = " << dBmin << ", dBmax = " << dBmax << endl;
Chris@1044 3332 #endif
Chris@1044 3333
Chris@120 3334 if (dBmax < -60.f) dBmax = -60.f;
Chris@907 3335 else top = QString("%1").arg(lrint(dBmax));
Chris@120 3336
Chris@120 3337 if (dBmin < dBmax - 60.f) dBmin = dBmax - 60.f;
Chris@907 3338 bottom = QString("%1").arg(lrint(dBmin));
Chris@119 3339
Chris@119 3340 //!!! & phase etc
Chris@119 3341
Chris@119 3342 if (m_colourScale != PhaseColourScale) {
Chris@119 3343 paint.drawText((cw + 6 - paint.fontMetrics().width("dBFS")) / 2,
Chris@119 3344 2 + textHeight + toff, "dBFS");
Chris@119 3345 }
Chris@119 3346
Chris@119 3347 // paint.drawText((cw + 6 - paint.fontMetrics().width(top)) / 2,
Chris@119 3348 paint.drawText(3 + cw - cbw - paint.fontMetrics().width(top),
Chris@119 3349 2 + textHeight * topLines + toff + textHeight/2, top);
Chris@119 3350
Chris@119 3351 paint.drawText(3 + cw - cbw - paint.fontMetrics().width(bottom),
Chris@119 3352 h + toff - 3 - textHeight/2, bottom);
Chris@40 3353
Chris@40 3354 paint.save();
Chris@40 3355 paint.setBrush(Qt::NoBrush);
Chris@119 3356
Chris@119 3357 int lasty = 0;
Chris@119 3358 int lastdb = 0;
Chris@119 3359
Chris@40 3360 for (int i = 0; i < ch; ++i) {
Chris@119 3361
Chris@905 3362 double dBval = dBmin + (((dBmax - dBmin) * i) / (ch - 1));
Chris@119 3363 int idb = int(dBval);
Chris@119 3364
Chris@905 3365 double value = AudioLevel::dB_to_multiplier(dBval);
Chris@119 3366 int colour = getDisplayValue(v, value * m_gain);
Chris@210 3367
Chris@907 3368 paint.setPen(m_palette.getColour((unsigned char)colour));
Chris@119 3369
Chris@119 3370 int y = textHeight * topLines + 4 + ch - i;
Chris@119 3371
Chris@119 3372 paint.drawLine(5 + cw - cbw, y, cw + 2, y);
Chris@119 3373
Chris@119 3374 if (i == 0) {
Chris@119 3375 lasty = y;
Chris@119 3376 lastdb = idb;
Chris@119 3377 } else if (i < ch - paint.fontMetrics().ascent() &&
Chris@120 3378 idb != lastdb &&
Chris@119 3379 ((abs(y - lasty) > textHeight &&
Chris@119 3380 idb % 10 == 0) ||
Chris@119 3381 (abs(y - lasty) > paint.fontMetrics().ascent() &&
Chris@119 3382 idb % 5 == 0))) {
Chris@287 3383 paint.setPen(v->getBackground());
Chris@119 3384 QString text = QString("%1").arg(idb);
Chris@119 3385 paint.drawText(3 + cw - cbw - paint.fontMetrics().width(text),
Chris@119 3386 y + toff + textHeight/2, text);
Chris@287 3387 paint.setPen(v->getForeground());
Chris@119 3388 paint.drawLine(5 + cw - cbw, y, 8 + cw - cbw, y);
Chris@119 3389 lasty = y;
Chris@119 3390 lastdb = idb;
Chris@119 3391 }
Chris@40 3392 }
Chris@40 3393 paint.restore();
Chris@40 3394 }
Chris@40 3395
Chris@40 3396 paint.drawLine(cw + 7, 0, cw + 7, h);
Chris@40 3397
Chris@0 3398 int bin = -1;
Chris@0 3399
Chris@918 3400 for (int y = 0; y < v->getPaintHeight(); ++y) {
Chris@0 3401
Chris@905 3402 double q0, q1;
Chris@918 3403 if (!getYBinRange(v, v->getPaintHeight() - y, q0, q1)) continue;
Chris@0 3404
Chris@0 3405 int vy;
Chris@0 3406
Chris@0 3407 if (int(q0) > bin) {
Chris@0 3408 vy = y;
Chris@0 3409 bin = int(q0);
Chris@0 3410 } else {
Chris@0 3411 continue;
Chris@0 3412 }
Chris@0 3413
Chris@907 3414 int freq = int((sr * bin) / m_fftSize);
Chris@0 3415
Chris@0 3416 if (py >= 0 && (vy - py) < textHeight - 1) {
Chris@40 3417 if (m_frequencyScale == LinearFrequencyScale) {
Chris@40 3418 paint.drawLine(w - tickw, h - vy, w, h - vy);
Chris@40 3419 }
Chris@0 3420 continue;
Chris@0 3421 }
Chris@0 3422
Chris@0 3423 QString text = QString("%1").arg(freq);
Chris@234 3424 if (bin == 1) text = tr("%1Hz").arg(freq); // bin 0 is DC
Chris@40 3425 paint.drawLine(cw + 7, h - vy, w - pkw - 1, h - vy);
Chris@0 3426
Chris@0 3427 if (h - vy - textHeight >= -2) {
Chris@1025 3428 int tx = w - 3 - paint.fontMetrics().width(text) - max(tickw, pkw);
Chris@0 3429 paint.drawText(tx, h - vy + toff, text);
Chris@0 3430 }
Chris@0 3431
Chris@0 3432 py = vy;
Chris@0 3433 }
Chris@40 3434
Chris@40 3435 if (m_frequencyScale == LogFrequencyScale) {
Chris@40 3436
Chris@277 3437 // piano keyboard
Chris@277 3438
Chris@690 3439 PianoScale().paintPianoVertical
Chris@690 3440 (v, paint, QRect(w - pkw - 1, 0, pkw, h),
Chris@690 3441 getEffectiveMinFrequency(), getEffectiveMaxFrequency());
Chris@40 3442 }
Chris@608 3443
Chris@608 3444 m_haveDetailedScale = detailed;
Chris@0 3445 }
Chris@0 3446
Chris@187 3447 class SpectrogramRangeMapper : public RangeMapper
Chris@187 3448 {
Chris@187 3449 public:
Chris@901 3450 SpectrogramRangeMapper(sv_samplerate_t sr, int /* fftsize */) :
Chris@901 3451 m_dist(sr / 2),
Chris@901 3452 m_s2(sqrt(sqrt(2))) { }
Chris@187 3453 ~SpectrogramRangeMapper() { }
Chris@187 3454
Chris@901 3455 virtual int getPositionForValue(double value) const {
Chris@901 3456
Chris@901 3457 double dist = m_dist;
Chris@187 3458
Chris@187 3459 int n = 0;
Chris@187 3460
Chris@901 3461 while (dist > (value + 0.00001) && dist > 0.1) {
Chris@187 3462 dist /= m_s2;
Chris@187 3463 ++n;
Chris@187 3464 }
Chris@187 3465
Chris@187 3466 return n;
Chris@187 3467 }
Chris@724 3468
Chris@901 3469 virtual int getPositionForValueUnclamped(double value) const {
Chris@724 3470 // We don't really support this
Chris@724 3471 return getPositionForValue(value);
Chris@724 3472 }
Chris@187 3473
Chris@901 3474 virtual double getValueForPosition(int position) const {
Chris@187 3475
Chris@187 3476 // Vertical zoom step 0 shows the entire range from DC ->
Chris@187 3477 // Nyquist frequency. Step 1 shows 2^(1/4) of the range of
Chris@187 3478 // step 0, and so on until the visible range is smaller than
Chris@187 3479 // the frequency step between bins at the current fft size.
Chris@187 3480
Chris@901 3481 double dist = m_dist;
Chris@187 3482
Chris@187 3483 int n = 0;
Chris@187 3484 while (n < position) {
Chris@187 3485 dist /= m_s2;
Chris@187 3486 ++n;
Chris@187 3487 }
Chris@187 3488
Chris@187 3489 return dist;
Chris@187 3490 }
Chris@187 3491
Chris@901 3492 virtual double getValueForPositionUnclamped(int position) const {
Chris@724 3493 // We don't really support this
Chris@724 3494 return getValueForPosition(position);
Chris@724 3495 }
Chris@724 3496
Chris@187 3497 virtual QString getUnit() const { return "Hz"; }
Chris@187 3498
Chris@187 3499 protected:
Chris@901 3500 double m_dist;
Chris@901 3501 double m_s2;
Chris@187 3502 };
Chris@187 3503
Chris@133 3504 int
Chris@133 3505 SpectrogramLayer::getVerticalZoomSteps(int &defaultStep) const
Chris@133 3506 {
Chris@135 3507 if (!m_model) return 0;
Chris@187 3508
Chris@907 3509 sv_samplerate_t sr = m_model->getSampleRate();
Chris@187 3510
Chris@187 3511 SpectrogramRangeMapper mapper(sr, m_fftSize);
Chris@187 3512
Chris@905 3513 // int maxStep = mapper.getPositionForValue((double(sr) / m_fftSize) + 0.001);
Chris@187 3514 int maxStep = mapper.getPositionForValue(0);
Chris@905 3515 int minStep = mapper.getPositionForValue(double(sr) / 2);
Chris@250 3516
Chris@805 3517 int initialMax = m_initialMaxFrequency;
Chris@907 3518 if (initialMax == 0) initialMax = int(sr / 2);
Chris@250 3519
Chris@250 3520 defaultStep = mapper.getPositionForValue(initialMax) - minStep;
Chris@250 3521
Chris@587 3522 // SVDEBUG << "SpectrogramLayer::getVerticalZoomSteps: " << maxStep - minStep << " (" << maxStep <<"-" << minStep << "), default is " << defaultStep << " (from initial max freq " << initialMax << ")" << endl;
Chris@187 3523
Chris@187 3524 return maxStep - minStep;
Chris@133 3525 }
Chris@133 3526
Chris@133 3527 int
Chris@133 3528 SpectrogramLayer::getCurrentVerticalZoomStep() const
Chris@133 3529 {
Chris@133 3530 if (!m_model) return 0;
Chris@133 3531
Chris@905 3532 double dmin, dmax;
Chris@133 3533 getDisplayExtents(dmin, dmax);
Chris@133 3534
Chris@187 3535 SpectrogramRangeMapper mapper(m_model->getSampleRate(), m_fftSize);
Chris@187 3536 int n = mapper.getPositionForValue(dmax - dmin);
Chris@587 3537 // SVDEBUG << "SpectrogramLayer::getCurrentVerticalZoomStep: " << n << endl;
Chris@133 3538 return n;
Chris@133 3539 }
Chris@133 3540
Chris@133 3541 void
Chris@133 3542 SpectrogramLayer::setVerticalZoomStep(int step)
Chris@133 3543 {
Chris@187 3544 if (!m_model) return;
Chris@187 3545
Chris@905 3546 double dmin = m_minFrequency, dmax = m_maxFrequency;
Chris@253 3547 // getDisplayExtents(dmin, dmax);
Chris@253 3548
Chris@682 3549 // cerr << "current range " << dmin << " -> " << dmax << ", range " << dmax-dmin << ", mid " << (dmax + dmin)/2 << endl;
Chris@133 3550
Chris@907 3551 sv_samplerate_t sr = m_model->getSampleRate();
Chris@187 3552 SpectrogramRangeMapper mapper(sr, m_fftSize);
Chris@905 3553 double newdist = mapper.getValueForPosition(step);
Chris@905 3554
Chris@905 3555 double newmin, newmax;
Chris@253 3556
Chris@253 3557 if (m_frequencyScale == LogFrequencyScale) {
Chris@253 3558
Chris@253 3559 // need to pick newmin and newmax such that
Chris@253 3560 //
Chris@253 3561 // (log(newmin) + log(newmax)) / 2 == logmid
Chris@253 3562 // and
Chris@253 3563 // newmax - newmin = newdist
Chris@253 3564 //
Chris@253 3565 // so log(newmax - newdist) + log(newmax) == 2logmid
Chris@253 3566 // log(newmax(newmax - newdist)) == 2logmid
Chris@253 3567 // newmax.newmax - newmax.newdist == exp(2logmid)
Chris@253 3568 // newmax^2 + (-newdist)newmax + -exp(2logmid) == 0
Chris@253 3569 // quadratic with a = 1, b = -newdist, c = -exp(2logmid), all known
Chris@253 3570 //
Chris@253 3571 // positive root
Chris@253 3572 // newmax = (newdist + sqrt(newdist^2 + 4exp(2logmid))) / 2
Chris@253 3573 //
Chris@253 3574 // but logmid = (log(dmin) + log(dmax)) / 2
Chris@253 3575 // so exp(2logmid) = exp(log(dmin) + log(dmax))
Chris@253 3576 // = exp(log(dmin.dmax))
Chris@253 3577 // = dmin.dmax
Chris@253 3578 // so newmax = (newdist + sqrtf(newdist^2 + 4dmin.dmax)) / 2
Chris@253 3579
Chris@907 3580 newmax = (newdist + sqrt(newdist*newdist + 4*dmin*dmax)) / 2;
Chris@253 3581 newmin = newmax - newdist;
Chris@253 3582
Chris@682 3583 // cerr << "newmin = " << newmin << ", newmax = " << newmax << endl;
Chris@253 3584
Chris@253 3585 } else {
Chris@905 3586 double dmid = (dmax + dmin) / 2;
Chris@253 3587 newmin = dmid - newdist / 2;
Chris@253 3588 newmax = dmid + newdist / 2;
Chris@253 3589 }
Chris@187 3590
Chris@905 3591 double mmin, mmax;
Chris@187 3592 mmin = 0;
Chris@905 3593 mmax = double(sr) / 2;
Chris@133 3594
Chris@187 3595 if (newmin < mmin) {
Chris@187 3596 newmax += (mmin - newmin);
Chris@187 3597 newmin = mmin;
Chris@187 3598 }
Chris@187 3599 if (newmax > mmax) {
Chris@187 3600 newmax = mmax;
Chris@187 3601 }
Chris@133 3602
Chris@587 3603 // SVDEBUG << "SpectrogramLayer::setVerticalZoomStep: " << step << ": " << newmin << " -> " << newmax << " (range " << newdist << ")" << endl;
Chris@253 3604
Chris@907 3605 setMinFrequency(int(lrint(newmin)));
Chris@907 3606 setMaxFrequency(int(lrint(newmax)));
Chris@187 3607 }
Chris@187 3608
Chris@187 3609 RangeMapper *
Chris@187 3610 SpectrogramLayer::getNewVerticalZoomRangeMapper() const
Chris@187 3611 {
Chris@187 3612 if (!m_model) return 0;
Chris@187 3613 return new SpectrogramRangeMapper(m_model->getSampleRate(), m_fftSize);
Chris@133 3614 }
Chris@133 3615
Chris@273 3616 void
Chris@918 3617 SpectrogramLayer::updateMeasureRectYCoords(LayerGeometryProvider *v, const MeasureRect &r) const
Chris@273 3618 {
Chris@273 3619 int y0 = 0;
Chris@907 3620 if (r.startY > 0.0) y0 = int(getYForFrequency(v, r.startY));
Chris@273 3621
Chris@273 3622 int y1 = y0;
Chris@907 3623 if (r.endY > 0.0) y1 = int(getYForFrequency(v, r.endY));
Chris@273 3624
Chris@587 3625 // SVDEBUG << "SpectrogramLayer::updateMeasureRectYCoords: start " << r.startY << " -> " << y0 << ", end " << r.endY << " -> " << y1 << endl;
Chris@273 3626
Chris@273 3627 r.pixrect = QRect(r.pixrect.x(), y0, r.pixrect.width(), y1 - y0);
Chris@273 3628 }
Chris@273 3629
Chris@273 3630 void
Chris@918 3631 SpectrogramLayer::setMeasureRectYCoord(LayerGeometryProvider *v, MeasureRect &r, bool start, int y) const
Chris@273 3632 {
Chris@273 3633 if (start) {
Chris@273 3634 r.startY = getFrequencyForY(v, y);
Chris@273 3635 r.endY = r.startY;
Chris@273 3636 } else {
Chris@273 3637 r.endY = getFrequencyForY(v, y);
Chris@273 3638 }
Chris@587 3639 // SVDEBUG << "SpectrogramLayer::setMeasureRectYCoord: start " << r.startY << " <- " << y << ", end " << r.endY << " <- " << y << endl;
Chris@273 3640
Chris@273 3641 }
Chris@273 3642
Chris@316 3643 void
Chris@316 3644 SpectrogramLayer::toXml(QTextStream &stream,
Chris@316 3645 QString indent, QString extraAttributes) const
Chris@6 3646 {
Chris@6 3647 QString s;
Chris@6 3648
Chris@6 3649 s += QString("channel=\"%1\" "
Chris@6 3650 "windowSize=\"%2\" "
Chris@153 3651 "windowHopLevel=\"%3\" "
Chris@153 3652 "gain=\"%4\" "
Chris@153 3653 "threshold=\"%5\" ")
Chris@6 3654 .arg(m_channel)
Chris@6 3655 .arg(m_windowSize)
Chris@97 3656 .arg(m_windowHopLevel)
Chris@37 3657 .arg(m_gain)
Chris@37 3658 .arg(m_threshold);
Chris@37 3659
Chris@37 3660 s += QString("minFrequency=\"%1\" "
Chris@37 3661 "maxFrequency=\"%2\" "
Chris@37 3662 "colourScale=\"%3\" "
Chris@37 3663 "colourScheme=\"%4\" "
Chris@37 3664 "colourRotation=\"%5\" "
Chris@37 3665 "frequencyScale=\"%6\" "
Chris@761 3666 "binDisplay=\"%7\" ")
Chris@37 3667 .arg(m_minFrequency)
Chris@6 3668 .arg(m_maxFrequency)
Chris@6 3669 .arg(m_colourScale)
Chris@197 3670 .arg(m_colourMap)
Chris@37 3671 .arg(m_colourRotation)
Chris@35 3672 .arg(m_frequencyScale)
Chris@761 3673 .arg(m_binDisplay);
Chris@761 3674
Chris@1009 3675 // New-style normalization attributes, allowing for more types of
Chris@1009 3676 // normalization in future: write out the column normalization
Chris@1009 3677 // type separately, and then whether we are normalizing visible
Chris@1009 3678 // area as well afterwards
Chris@1009 3679
Chris@1009 3680 s += QString("columnNormalization=\"%1\" ")
Chris@1009 3681 .arg(m_normalization == NormalizeColumns ? "peak" :
Chris@1009 3682 m_normalization == NormalizeHybrid ? "hybrid" : "none");
Chris@1009 3683
Chris@1009 3684 // Old-style normalization attribute. We *don't* write out
Chris@1009 3685 // normalizeHybrid here because the only release that would accept
Chris@1009 3686 // it (Tony v1.0) has a totally different scale factor for
Chris@1009 3687 // it. We'll just have to accept that session files from Tony
Chris@1009 3688 // v2.0+ will look odd in Tony v1.0
Chris@1009 3689
Chris@1009 3690 s += QString("normalizeColumns=\"%1\" ")
Chris@1009 3691 .arg(m_normalization == NormalizeColumns ? "true" : "false");
Chris@1009 3692
Chris@1009 3693 // And this applies to both old- and new-style attributes
Chris@1009 3694
Chris@1009 3695 s += QString("normalizeVisibleArea=\"%1\" ")
Chris@1009 3696 .arg(m_normalization == NormalizeVisibleArea ? "true" : "false");
Chris@1009 3697
Chris@316 3698 Layer::toXml(stream, indent, extraAttributes + " " + s);
Chris@6 3699 }
Chris@6 3700
Chris@11 3701 void
Chris@11 3702 SpectrogramLayer::setProperties(const QXmlAttributes &attributes)
Chris@11 3703 {
Chris@11 3704 bool ok = false;
Chris@11 3705
Chris@11 3706 int channel = attributes.value("channel").toInt(&ok);
Chris@11 3707 if (ok) setChannel(channel);
Chris@11 3708
Chris@805 3709 int windowSize = attributes.value("windowSize").toUInt(&ok);
Chris@11 3710 if (ok) setWindowSize(windowSize);
Chris@11 3711
Chris@805 3712 int windowHopLevel = attributes.value("windowHopLevel").toUInt(&ok);
Chris@97 3713 if (ok) setWindowHopLevel(windowHopLevel);
Chris@97 3714 else {
Chris@805 3715 int windowOverlap = attributes.value("windowOverlap").toUInt(&ok);
Chris@97 3716 // a percentage value
Chris@97 3717 if (ok) {
Chris@97 3718 if (windowOverlap == 0) setWindowHopLevel(0);
Chris@97 3719 else if (windowOverlap == 25) setWindowHopLevel(1);
Chris@97 3720 else if (windowOverlap == 50) setWindowHopLevel(2);
Chris@97 3721 else if (windowOverlap == 75) setWindowHopLevel(3);
Chris@97 3722 else if (windowOverlap == 90) setWindowHopLevel(4);
Chris@97 3723 }
Chris@97 3724 }
Chris@11 3725
Chris@11 3726 float gain = attributes.value("gain").toFloat(&ok);
Chris@11 3727 if (ok) setGain(gain);
Chris@11 3728
Chris@37 3729 float threshold = attributes.value("threshold").toFloat(&ok);
Chris@37 3730 if (ok) setThreshold(threshold);
Chris@37 3731
Chris@805 3732 int minFrequency = attributes.value("minFrequency").toUInt(&ok);
Chris@187 3733 if (ok) {
Chris@587 3734 SVDEBUG << "SpectrogramLayer::setProperties: setting min freq to " << minFrequency << endl;
Chris@187 3735 setMinFrequency(minFrequency);
Chris@187 3736 }
Chris@37 3737
Chris@805 3738 int maxFrequency = attributes.value("maxFrequency").toUInt(&ok);
Chris@187 3739 if (ok) {
Chris@587 3740 SVDEBUG << "SpectrogramLayer::setProperties: setting max freq to " << maxFrequency << endl;
Chris@187 3741 setMaxFrequency(maxFrequency);
Chris@187 3742 }
Chris@11 3743
Chris@11 3744 ColourScale colourScale = (ColourScale)
Chris@11 3745 attributes.value("colourScale").toInt(&ok);
Chris@11 3746 if (ok) setColourScale(colourScale);
Chris@11 3747
Chris@197 3748 int colourMap = attributes.value("colourScheme").toInt(&ok);
Chris@197 3749 if (ok) setColourMap(colourMap);
Chris@11 3750
Chris@37 3751 int colourRotation = attributes.value("colourRotation").toInt(&ok);
Chris@37 3752 if (ok) setColourRotation(colourRotation);
Chris@37 3753
Chris@11 3754 FrequencyScale frequencyScale = (FrequencyScale)
Chris@11 3755 attributes.value("frequencyScale").toInt(&ok);
Chris@11 3756 if (ok) setFrequencyScale(frequencyScale);
Chris@35 3757
Chris@37 3758 BinDisplay binDisplay = (BinDisplay)
Chris@37 3759 attributes.value("binDisplay").toInt(&ok);
Chris@37 3760 if (ok) setBinDisplay(binDisplay);
Chris@36 3761
Chris@1009 3762 bool haveNewStyleNormalization = false;
Chris@1009 3763
Chris@1009 3764 QString columnNormalization = attributes.value("columnNormalization");
Chris@1009 3765
Chris@1009 3766 if (columnNormalization != "") {
Chris@1009 3767
Chris@1009 3768 haveNewStyleNormalization = true;
Chris@1009 3769
Chris@1009 3770 if (columnNormalization == "peak") {
Chris@1009 3771 setNormalization(NormalizeColumns);
Chris@1009 3772 } else if (columnNormalization == "hybrid") {
Chris@1009 3773 setNormalization(NormalizeHybrid);
Chris@1009 3774 } else if (columnNormalization == "none") {
Chris@1009 3775 // do nothing
Chris@1009 3776 } else {
Chris@1009 3777 cerr << "NOTE: Unknown or unsupported columnNormalization attribute \""
Chris@1009 3778 << columnNormalization << "\"" << endl;
Chris@1009 3779 }
Chris@1009 3780 }
Chris@1009 3781
Chris@1009 3782 if (!haveNewStyleNormalization) {
Chris@1009 3783
Chris@1009 3784 bool normalizeColumns =
Chris@1009 3785 (attributes.value("normalizeColumns").trimmed() == "true");
Chris@1009 3786 if (normalizeColumns) {
Chris@1009 3787 setNormalization(NormalizeColumns);
Chris@1009 3788 }
Chris@1009 3789
Chris@1009 3790 bool normalizeHybrid =
Chris@1009 3791 (attributes.value("normalizeHybrid").trimmed() == "true");
Chris@1009 3792 if (normalizeHybrid) {
Chris@1009 3793 setNormalization(NormalizeHybrid);
Chris@1009 3794 }
Chris@862 3795 }
Chris@153 3796
Chris@153 3797 bool normalizeVisibleArea =
Chris@153 3798 (attributes.value("normalizeVisibleArea").trimmed() == "true");
Chris@862 3799 if (normalizeVisibleArea) {
Chris@862 3800 setNormalization(NormalizeVisibleArea);
Chris@862 3801 }
Chris@761 3802
Chris@1009 3803 if (!haveNewStyleNormalization && m_normalization == NormalizeHybrid) {
Chris@1009 3804 // Tony v1.0 is (and hopefully will remain!) the only released
Chris@1009 3805 // SV-a-like to use old-style attributes when saving sessions
Chris@1009 3806 // that ask for hybrid normalization. It saves them with the
Chris@1009 3807 // wrong gain factor, so hack in a fix for that here -- this
Chris@1009 3808 // gives us backward but not forward compatibility.
Chris@1009 3809 setGain(m_gain / float(m_fftSize / 2));
Chris@862 3810 }
Chris@11 3811 }
Chris@11 3812