annotate layer/SpectrogramLayer.cpp @ 330:846746e4e865 spectrogram-cache-rejig

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