annotate layer/SpectrogramLayer.cpp @ 479:0033dbfb92e3

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