annotate layer/SpectrogramLayer.cpp @ 862:1986c9b0d9c3 normalize_hybrid_option

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