annotate layer/SpectrogramLayer.cpp @ 973:bc23c2cfff65 osx-retina

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