annotate layer/SpectrogramLayer.cpp @ 391:4f0f273c8f82

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