annotate layer/SpectrogramLayer.cpp @ 1023:74755fa6ea9e colourschemes

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