annotate layer/SpectrogramLayer.cpp @ 1059:e1c2dcc7790e spectrogram-minor-refactor

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