annotate layer/SpectrogramLayer.cpp @ 1062:38ecdd5924ac spectrogram-minor-refactor

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