annotate layer/SpectrogramLayer.cpp @ 332:6440e280122e spectrogram-cache-rejig

* Some bits and bobs of cache mangling -- closer to working, but still not
author Chris Cannam
date Mon, 19 Nov 2007 15:50:37 +0000
parents 846746e4e865
children 64e84e5efb76
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@332 1759 float ratio = float(v->getZoomLevel()) / float(getWindowIncrement());
Chris@332 1760 int imageWidth = lrintf(v->width() * ratio);
Chris@332 1761
Chris@329 1762 PixmapCache *cache = m_pixmapCaches[v];
Chris@329 1763 if (!cache) {
Chris@329 1764 cache = new PixmapCache;
Chris@330 1765 cache->pixmap = 0;
Chris@330 1766 m_pixmapCaches[v] = cache;
Chris@330 1767 cache->mutex.lock();
Chris@332 1768 cache->pixmap = new QImage(imageWidth, v->height(),
Chris@330 1769 QImage::Format_RGB32);
Chris@330 1770 cache->mutex.unlock();
Chris@0 1771 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@329 1772 std::cerr << "SpectrogramLayer::paint: Created new cache, size "
Chris@329 1773 << v->width() << "x" << v->height() << " ("
Chris@329 1774 << v->width() * v->height() * 4 << " bytes)" << std::endl;
Chris@329 1775 #endif
Chris@329 1776 return; //!!! prod paint thread
Chris@329 1777 }
Chris@329 1778
Chris@332 1779 long myStartFrame = v->getStartFrame();
Chris@332 1780 long cacheStartFrame = cache->startFrame;
Chris@332 1781 long xoff = (myStartFrame - cacheStartFrame) / getWindowIncrement();
Chris@332 1782
Chris@332 1783 QRect sourceRect = QRect(lrintf(rect.x() * ratio) + xoff,
Chris@332 1784 rect.y(),
Chris@332 1785 lrintf(rect.width() * ratio),
Chris@332 1786 rect.height());
Chris@332 1787
Chris@329 1788 if (!cache->mutex.tryLock()) {
Chris@329 1789 std::cerr << "lock unavailable" << std::endl;
Chris@332 1790
Chris@332 1791 paint.drawImage(rect,
Chris@332 1792 cache->pixmap->copy(sourceRect)
Chris@332 1793 .scaled(rect.size(),
Chris@332 1794 Qt::IgnoreAspectRatio,
Chris@332 1795 Qt::SmoothTransformation));
Chris@332 1796
Chris@332 1797 // paint.drawImage(rect,// & cache->validArea,
Chris@332 1798 // *cache->pixmap,
Chris@332 1799 // rect);// & cache->validArea);
Chris@329 1800 // v->update(rect);
Chris@329 1801 return;
Chris@329 1802 }
Chris@329 1803
Chris@332 1804 if (cache->pixmap->width() != imageWidth ||
Chris@330 1805 cache->pixmap->height() != v->height()) {
Chris@330 1806 QImage *newImage = new QImage
Chris@332 1807 (cache->pixmap->scaled(imageWidth, v->height(),
Chris@332 1808 Qt::IgnoreAspectRatio,
Chris@332 1809 Qt::SmoothTransformation));
Chris@329 1810 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@330 1811 std::cerr << "SpectrogramLayer::paint: Rescaling cache to size "
Chris@332 1812 << imageWidth << "x" << v->height() << " ("
Chris@332 1813 << imageWidth * v->height() * 4 << " bytes)" << std::endl;
Chris@329 1814 #endif
Chris@330 1815 delete cache->pixmap;
Chris@330 1816 cache->pixmap = newImage;
Chris@329 1817 }
Chris@329 1818
Chris@330 1819 // The cache has the correct resolution for the view in the y
Chris@330 1820 // axis, but with a resolution of one pixel per increment in the x
Chris@330 1821 // axis. We may need to rescale when drawing.
Chris@330 1822
Chris@330 1823 paint.setRenderHint(QPainter::SmoothPixmapTransform, true);
Chris@330 1824
Chris@330 1825 //!!! not quite right in any case except integer ratio -- either
Chris@330 1826 //source or target rect will have edges at non-integral
Chris@330 1827 //coordinates
Chris@330 1828
Chris@330 1829 std::cerr << "my start frame = " << myStartFrame << ", cache start frame = " << cacheStartFrame << std::endl;
Chris@330 1830
Chris@330 1831 std::cerr << "ratio = " << ratio << " (zoom " << v->getZoomLevel()
Chris@330 1832 << ", incr " << getWindowIncrement() << "); source rect "
Chris@330 1833 << sourceRect.x() << "," << sourceRect.y()
Chris@330 1834 << " " << sourceRect.width() << "x" << sourceRect.height()
Chris@330 1835 << ", target rect "
Chris@330 1836 << rect.x() << "," << rect.y()
Chris@330 1837 << " " << rect.width() << "x" << rect.height()
Chris@330 1838 << " (image " << cache->pixmap->width() << "x"
Chris@330 1839 << cache->pixmap->height() << ")"
Chris@330 1840 << std::endl;
Chris@330 1841
Chris@329 1842 paint.drawImage(rect,// & cache->validArea,
Chris@332 1843 cache->pixmap->copy(sourceRect)
Chris@332 1844 .scaled(rect.size(),
Chris@332 1845 Qt::IgnoreAspectRatio,
Chris@332 1846 Qt::SmoothTransformation));
Chris@332 1847 // *cache->pixmap,
Chris@332 1848 // sourceRect);// & cache->validArea);
Chris@329 1849
Chris@329 1850 if (cache->startFrame != v->getStartFrame()) {
Chris@329 1851 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@329 1852 std::cerr << "SpectrogramLayer::paint(): Cache start frame "
Chris@329 1853 << cache->startFrame << " differs from view start frame "
Chris@329 1854 << v->getStartFrame() << std::endl;
Chris@329 1855 #endif
Chris@329 1856 cache->mutex.unlock();
Chris@329 1857 // v->update(); //!!! and prod
Chris@329 1858 return;
Chris@329 1859 }
Chris@329 1860
Chris@329 1861 if (!cache->validArea.contains(rect)) {
Chris@329 1862 //!!! don't want to be sending events; just want to flag up
Chris@329 1863 //that we need a repaint and let some higher power arrange to
Chris@329 1864 //do it
Chris@329 1865 // v->update(rect); //!!!??? prod paint thread
Chris@329 1866 }
Chris@329 1867
Chris@329 1868 // paint.drawImage(rect & cache->validArea,
Chris@329 1869 // cache->pixmap,
Chris@329 1870 // rect & cache->validArea);
Chris@329 1871
Chris@329 1872 cache->mutex.unlock();
Chris@329 1873 }
Chris@329 1874
Chris@329 1875
Chris@329 1876 void
Chris@329 1877 SpectrogramLayer::PaintThread::run()
Chris@329 1878 {
Chris@329 1879 while (!m_exiting) {
Chris@329 1880
Chris@329 1881 std::set<View *> views;
Chris@329 1882
Chris@329 1883 m_layer->m_pixmapCacheMutex.lock();
Chris@329 1884 for (ViewPixmapCache::iterator i = m_layer->m_pixmapCaches.begin();
Chris@329 1885 i != m_layer->m_pixmapCaches.end(); ++i) {
Chris@329 1886 views.insert(const_cast<View *>(i->first)); //!!!
Chris@329 1887 }
Chris@329 1888 m_layer->m_pixmapCacheMutex.unlock();
Chris@330 1889
Chris@330 1890 bool workToDo = false;
Chris@329 1891
Chris@329 1892 for (std::set<View *>::iterator vi = views.begin();
Chris@329 1893 vi != views.end(); ++vi) {
Chris@329 1894
Chris@330 1895 if (m_layer->paintCache(*vi)) {
Chris@330 1896 if (!m_exiting) {
Chris@330 1897 std::cerr << "Cache complete: asking view to update"
Chris@330 1898 << std::endl;
Chris@330 1899 (*vi)->update();
Chris@330 1900 }
Chris@330 1901 } else {
Chris@330 1902 workToDo = true;
Chris@330 1903 }
Chris@330 1904
Chris@329 1905 if (m_exiting) break;
Chris@329 1906 }
Chris@329 1907
Chris@330 1908 if (!m_exiting) {
Chris@330 1909 //!!! wait on condition
Chris@332 1910 if (workToDo) ; // usleep(100);
Chris@330 1911 else sleep(1);
Chris@330 1912 }
Chris@329 1913 }
Chris@329 1914 }
Chris@329 1915
Chris@329 1916 // return true if no more work to do, or can do no more
Chris@329 1917
Chris@329 1918 bool
Chris@329 1919 SpectrogramLayer::paintCache(View *v) const
Chris@329 1920 {
Chris@330 1921 Profiler profiler("SpectrogramLayer::paintCache", false);
Chris@329 1922
Chris@329 1923 m_pixmapCacheMutex.lock();
Chris@329 1924
Chris@329 1925 PixmapCache *cacheptr = m_pixmapCaches[v];
Chris@330 1926 if (!cacheptr || !cacheptr->pixmap || cacheptr->pixmap->width() == 0) {
Chris@329 1927 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@329 1928 std::cerr << "SpectrogramLayer::paintCache(): No cache to paint onto"
Chris@329 1929 << std::endl;
Chris@329 1930 #endif
Chris@329 1931 m_pixmapCacheMutex.unlock();
Chris@329 1932 return false;
Chris@329 1933 }
Chris@329 1934
Chris@329 1935 PixmapCache &cache = *cacheptr;
Chris@329 1936 QMutexLocker locker(&cache.mutex);
Chris@329 1937 m_pixmapCacheMutex.unlock();
Chris@329 1938
Chris@330 1939 QImage &image = *cache.pixmap; //!!! rename to cache.image
Chris@330 1940
Chris@330 1941 const DenseTimeValueModel *model = m_model;
Chris@330 1942 if (!model || !model->isOK() || !model->isReady()) {
Chris@330 1943 return false;
Chris@330 1944 }
Chris@330 1945
Chris@330 1946 //!!! mutex here -- we have the specific pixmap cache mutex held,
Chris@330 1947 //but only that one currently and there's no mutex for this stuff
Chris@330 1948 //in the other thread
Chris@330 1949
Chris@330 1950 //!!! what a big pile of crap
Chris@330 1951 long startFrame = v->getStartFrame();
Chris@330 1952 int increment = getWindowIncrement(); // signed for arithmetic purposes
Chris@330 1953 size_t fftNominal = m_fftSize; // the user-specified fft size
Chris@330 1954 size_t fftSize = getFFTSize(v); // the actual fft size (maybe padded)
Chris@330 1955 FFTModel *fft = getFFTModel(v);
Chris@330 1956 int sr = model->getSampleRate();
Chris@330 1957 size_t fmin = m_minFrequency;
Chris@330 1958 size_t fmax = m_maxFrequency;
Chris@330 1959 FrequencyScale frequencyScale = m_frequencyScale;
Chris@330 1960 BinDisplay binDisplay = m_binDisplay;
Chris@330 1961 float threshold = m_threshold;
Chris@330 1962 bool normalizeColumns = m_normalizeColumns;
Chris@330 1963 bool normalizeVisibleArea = m_normalizeVisibleArea;
Chris@330 1964 ColourScale colourScale = m_colourScale;
Chris@330 1965 float gain = m_gain;
Chris@330 1966 const Palette &palette = m_palette;
Chris@330 1967 int zpl = getZeroPadLevel(v) + 1;
Chris@330 1968
Chris@330 1969 if (!fft) {
Chris@330 1970 std::cerr << "ERROR: SpectrogramLayer::paintCache(): No FFT model, returning" << std::endl;
Chris@330 1971 return true;
Chris@330 1972 }
Chris@330 1973
Chris@330 1974
Chris@330 1975 std::cerr << "SpectrogramLayer::paintCache: start frame "
Chris@330 1976 << startFrame << ", increment " << increment << std::endl;
Chris@330 1977
Chris@330 1978
Chris@330 1979 int w = image.width();
Chris@330 1980 int h = image.height();
Chris@330 1981
Chris@330 1982
Chris@330 1983 // Establish rect to paint. We want to paint a not-too-large area
Chris@330 1984 // that is not known to be valid, adjacent to an area that is
Chris@330 1985 // known to be valid, such that the resulting known-valid area is
Chris@330 1986 // rectangular still. This doesn't really do that.
Chris@329 1987
Chris@330 1988 QRect rect = QRect(0, 0, w, h);
Chris@330 1989
Chris@330 1990 QPainter paint(cache.pixmap);
Chris@329 1991
Chris@329 1992 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@330 1993 std::cerr << "SpectrogramLayer::paintCache(): m_model is " << model << ", window increment " << increment << std::endl;
Chris@95 1994
Chris@95 1995 std::cerr << "rect is " << rect.x() << "," << rect.y() << " " << rect.width() << "x" << rect.height() << std::endl;
Chris@0 1996 #endif
Chris@95 1997
Chris@133 1998 if (startFrame < 0) m_candidateFillStartFrame = 0;
Chris@133 1999 else m_candidateFillStartFrame = startFrame;
Chris@44 2000
Chris@329 2001 /*
Chris@47 2002 if (isLayerDormant(v)) {
Chris@329 2003 std::cerr << "SpectrogramLayer::paintCache(): Layer is dormant, making it undormant again" << std::endl;
Chris@29 2004 }
Chris@29 2005
Chris@48 2006 // Need to do this even if !isLayerDormant, as that could mean v
Chris@48 2007 // is not in the dormancy map at all -- we need it to be present
Chris@48 2008 // and accountable for when determining whether we need the cache
Chris@48 2009 // in the cache-fill thread above.
Chris@131 2010 //!!! no longer use cache-fill thread
Chris@131 2011 const_cast<SpectrogramLayer *>(this)->Layer::setLayerDormant(v, false);
Chris@329 2012 */
Chris@0 2013
Chris@95 2014 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@330 2015 std::cerr << "SpectrogramLayer::paintCache(): pixmap cache size " << w << "x" << h << std::endl;
Chris@329 2016 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 2017 #endif
Chris@0 2018
Chris@330 2019 bool recreateWholeCache = true;
Chris@330 2020
Chris@330 2021 int x0 = rect.left();
Chris@330 2022 int x1 = rect.right() + 1;
Chris@95 2023
Chris@215 2024 struct timeval tv;
Chris@215 2025 (void)gettimeofday(&tv, 0);
Chris@215 2026 RealTime mainPaintStart = RealTime::fromTimeval(tv);
Chris@215 2027
Chris@215 2028 int paintBlockWidth = m_lastPaintBlockWidth;
Chris@215 2029
Chris@215 2030 if (paintBlockWidth == 0) {
Chris@330 2031 paintBlockWidth = (300000 / increment);
Chris@215 2032 } else {
Chris@215 2033 RealTime lastTime = m_lastPaintTime;
Chris@215 2034 while (lastTime > RealTime::fromMilliseconds(200) &&
Chris@215 2035 paintBlockWidth > 50) {
Chris@215 2036 paintBlockWidth /= 2;
Chris@215 2037 lastTime = lastTime / 2;
Chris@215 2038 }
Chris@215 2039 while (lastTime < RealTime::fromMilliseconds(90) &&
Chris@215 2040 paintBlockWidth < 1500) {
Chris@215 2041 paintBlockWidth *= 2;
Chris@215 2042 lastTime = lastTime * 2;
Chris@215 2043 }
Chris@215 2044 }
Chris@215 2045
Chris@96 2046 if (paintBlockWidth < 20) paintBlockWidth = 20;
Chris@96 2047
Chris@224 2048 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@215 2049 std::cerr << "[" << this << "]: last paint width: " << m_lastPaintBlockWidth << ", last paint time: " << m_lastPaintTime << ", new paint width: " << paintBlockWidth << std::endl;
Chris@224 2050 #endif
Chris@224 2051
Chris@332 2052 recreateWholeCache = checkAndScrollCache(cache, paint,
Chris@332 2053 startFrame, increment,
Chris@332 2054 x0, x1);
Chris@332 2055
Chris@332 2056 selectPaintStrip(cache, x0, x1, paintBlockWidth);
Chris@332 2057
Chris@224 2058 // We always paint the full height when refreshing the cache.
Chris@329 2059 // Smaller heights can be used when painting from cache, but we
Chris@329 2060 // want to ensure the cache is coherent without having to worry
Chris@329 2061 // about vertical matching of required and valid areas as well as
Chris@329 2062 // horizontal.
Chris@224 2063
Chris@330 2064 // int h = v->height();
Chris@330 2065 // int w = x1 - x0;
Chris@329 2066
Chris@329 2067 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@329 2068 std::cerr << "x0 " << x0 << ", x1 " << x1 << ", w " << w << ", h " << h << std::endl;
Chris@329 2069 #endif
Chris@329 2070
Chris@329 2071 // if (m_drawBuffer.width() < w || m_drawBuffer.height() < h) {
Chris@329 2072 // m_drawBuffer = QImage(w, h, QImage::Format_RGB32);
Chris@329 2073 // }
Chris@329 2074
Chris@329 2075 // m_drawBuffer.fill(m_palette.getColour(0).rgb());
Chris@329 2076
Chris@329 2077 // Set minFreq and maxFreq to the frequency extents of the possibly
Chris@329 2078 // zero-padded visible bin range, and displayMinFreq and displayMaxFreq
Chris@329 2079 // to the actual scale frequency extents (presumably not zero padded).
Chris@329 2080
Chris@329 2081 // If we are zero padding, we want to use the zero-padded
Chris@329 2082 // equivalents of the bins that we would be using if not zero
Chris@329 2083 // padded, to avoid spaces at the top and bottom of the display.
Chris@329 2084
Chris@330 2085 //!!! --> we weren't actually using minFreq or maxFreq -- so just
Chris@330 2086 // set our new fmin/fmax to the display extents
Chris@330 2087
Chris@330 2088 // Note fftSize is the actual zero-padded fft size, fftNominal the
Chris@329 2089 // nominal fft size.
Chris@329 2090
Chris@330 2091 size_t maxbin = fftNominal / 2;
Chris@330 2092 if (fmax > 0) {
Chris@330 2093 maxbin = int((double(fmax) * fftNominal) / sr + 0.001);
Chris@330 2094 if (maxbin > fftNominal / 2) maxbin = fftNominal / 2;
Chris@329 2095 }
Chris@329 2096
Chris@329 2097 size_t minbin = 1;
Chris@330 2098 if (fmin > 0) {
Chris@330 2099 minbin = int((double(fmin) * fftNominal) / sr + 0.001);
Chris@329 2100 if (minbin < 1) minbin = 1;
Chris@329 2101 if (minbin >= maxbin) minbin = maxbin - 1;
Chris@329 2102 }
Chris@329 2103
Chris@329 2104 minbin = minbin * zpl;
Chris@329 2105 maxbin = (maxbin + 1) * zpl - 1;
Chris@329 2106
Chris@330 2107 fmin = (float(minbin) * sr) / fftNominal;
Chris@330 2108 fmax = (float(maxbin) * sr) / fftNominal;
Chris@329 2109
Chris@329 2110 float ymag[h];
Chris@329 2111 float ydiv[h];
Chris@329 2112 float yval[maxbin + 1]; //!!! cache this?
Chris@329 2113
Chris@330 2114 bool logarithmic = (frequencyScale == LogFrequencyScale);
Chris@329 2115
Chris@329 2116 for (size_t q = minbin; q <= maxbin; ++q) {
Chris@329 2117 float f0 = (float(q) * sr) / fftSize;
Chris@330 2118 yval[q] = v->getYForFrequency(f0, fmin, fmax, logarithmic);
Chris@329 2119 // std::cerr << "min: " << minFreq << ", max: " << maxFreq << ", yval[" << q << "]: " << yval[q] << std::endl;
Chris@329 2120 }
Chris@329 2121
Chris@329 2122 MagnitudeRange overallMag = m_viewMags[v];
Chris@329 2123 // bool overallMagChanged = false;
Chris@329 2124
Chris@329 2125 // bool fftSuspended = false;
Chris@329 2126
Chris@329 2127 bool interpolate = false;
Chris@329 2128 Preferences::SpectrogramSmoothing smoothing =
Chris@329 2129 Preferences::getInstance()->getSpectrogramSmoothing();
Chris@329 2130 if (smoothing == Preferences::SpectrogramInterpolated ||
Chris@329 2131 smoothing == Preferences::SpectrogramZeroPaddedAndInterpolated) {
Chris@330 2132 if (binDisplay != PeakBins &&
Chris@330 2133 binDisplay != PeakFrequencies) {
Chris@329 2134 interpolate = true;
Chris@329 2135 }
Chris@329 2136 }
Chris@329 2137
Chris@329 2138 bool runOutOfData = false;
Chris@329 2139
Chris@332 2140 std::cerr << "painting from " << x0 << " to " << x1 << std::endl;
Chris@332 2141
Chris@330 2142 for (int x = 0; x < (x1 - x0); ++x) {
Chris@329 2143
Chris@329 2144 if (runOutOfData) break;
Chris@329 2145
Chris@329 2146 for (int y = 0; y < h; ++y) {
Chris@329 2147 ymag[y] = 0.f;
Chris@329 2148 ydiv[y] = 0.f;
Chris@329 2149 }
Chris@329 2150
Chris@330 2151 int col = (startFrame / increment) + x0 + x;
Chris@330 2152
Chris@330 2153 // std::cerr << "x = " << x << ", col = " << col << std::endl;
Chris@330 2154
Chris@330 2155 if (col >= int(fft->getWidth())) {
Chris@330 2156 continue;
Chris@329 2157 }
Chris@329 2158
Chris@330 2159 if (!fft->isColumnAvailable(col)) {
Chris@329 2160 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@330 2161 std::cerr << "Met unavailable column at col " << col << std::endl;
Chris@329 2162 #endif
Chris@330 2163 runOutOfData = true;
Chris@330 2164 break;
Chris@330 2165 }
Chris@329 2166
Chris@329 2167 /*
Chris@329 2168 if (!fftSuspended) {
Chris@329 2169 fft->suspendWrites();
Chris@329 2170 fftSuspended = true;
Chris@329 2171 }
Chris@329 2172 */
Chris@330 2173
Chris@330 2174 MagnitudeRange mag;
Chris@330 2175
Chris@330 2176 FFTModel::PeakSet peaks;
Chris@330 2177 if (binDisplay == PeakFrequencies &&
Chris@330 2178 col < int(fft->getWidth()) - 1) {
Chris@330 2179 peaks = fft->getPeakFrequencies(FFTModel::AllPeaks,
Chris@330 2180 col,
Chris@330 2181 minbin, maxbin - 1);
Chris@330 2182 }
Chris@330 2183
Chris@330 2184 for (size_t q = minbin; q < maxbin; ++q) {
Chris@330 2185
Chris@330 2186 float y0 = yval[q + 1];
Chris@330 2187 float y1 = yval[q];
Chris@330 2188
Chris@330 2189 if (binDisplay == PeakBins) {
Chris@330 2190 if (!fft->isLocalPeak(col, q)) continue;
Chris@329 2191 }
Chris@330 2192 if (binDisplay == PeakFrequencies) {
Chris@330 2193 if (peaks.find(q) == peaks.end()) continue;
Chris@330 2194 }
Chris@330 2195
Chris@330 2196 if (threshold != 0.f &&
Chris@330 2197 !fft->isOverThreshold(col, q, threshold * (fftNominal/2))) {
Chris@330 2198 continue;
Chris@330 2199 }
Chris@330 2200
Chris@330 2201 if (binDisplay == PeakFrequencies) {
Chris@330 2202 y0 = y1 = v->getYForFrequency
Chris@330 2203 (peaks[q], fmin, fmax, logarithmic);
Chris@330 2204 }
Chris@330 2205
Chris@330 2206 int y0i = int(y0 + 0.001);
Chris@330 2207 int y1i = int(y1);
Chris@330 2208
Chris@330 2209 float value;
Chris@330 2210
Chris@330 2211 if (colourScale == PhaseColourScale) {
Chris@330 2212 value = fft->getPhaseAt(col, q);
Chris@330 2213 } else if (normalizeColumns) {
Chris@330 2214 value = fft->getNormalizedMagnitudeAt(col, q);
Chris@330 2215 mag.sample(value);
Chris@330 2216 value *= gain;
Chris@330 2217 } else {
Chris@330 2218 value = fft->getMagnitudeAt(col, q) / (fftNominal/2);
Chris@330 2219 mag.sample(value);
Chris@330 2220 value *= gain;
Chris@330 2221 }
Chris@330 2222
Chris@330 2223 if (interpolate) {
Chris@330 2224
Chris@330 2225 int ypi = y0i;
Chris@330 2226 if (q < maxbin - 1) ypi = int(yval[q + 2]);
Chris@330 2227
Chris@330 2228 for (int y = ypi; y <= y1i; ++y) {
Chris@330 2229
Chris@330 2230 if (y < 0 || y >= h) continue;
Chris@330 2231
Chris@330 2232 float yprop = 1.f;
Chris@330 2233 float iprop = yprop;
Chris@330 2234
Chris@330 2235 if (ypi < y0i && y <= y0i) {
Chris@330 2236
Chris@330 2237 float half = float(y0i - ypi) / 2;
Chris@330 2238 float dist = y - (ypi + half);
Chris@330 2239
Chris@330 2240 if (dist >= 0) {
Chris@330 2241 iprop = (iprop * dist) / half;
Chris@330 2242 ymag[y] += iprop * value;
Chris@330 2243 }
Chris@330 2244 } else {
Chris@330 2245 if (y1i > y0i) {
Chris@330 2246
Chris@330 2247 float half = float(y1i - y0i) / 2;
Chris@330 2248 float dist = y - (y0i + half);
Chris@330 2249
Chris@330 2250 if (dist >= 0) {
Chris@330 2251 iprop = (iprop * (half - dist)) / half;
Chris@330 2252 }
Chris@330 2253 }
Chris@330 2254
Chris@330 2255 ymag[y] += iprop * value;
Chris@330 2256 ydiv[y] += yprop;
Chris@330 2257 }
Chris@329 2258 }
Chris@329 2259
Chris@330 2260 } else {
Chris@330 2261
Chris@330 2262 for (int y = y0i; y <= y1i; ++y) {
Chris@329 2263
Chris@330 2264 if (y < 0 || y >= h) continue;
Chris@329 2265
Chris@330 2266 float yprop = 1.f;
Chris@330 2267 if (y == y0i) yprop *= (y + 1) - y0;
Chris@330 2268 if (y == y1i) yprop *= y1 - y;
Chris@330 2269
Chris@330 2270 for (int y = y0i; y <= y1i; ++y) {
Chris@330 2271
Chris@329 2272 if (y < 0 || y >= h) continue;
Chris@330 2273
Chris@330 2274 float yprop = 1.f;
Chris@329 2275 if (y == y0i) yprop *= (y + 1) - y0;
Chris@329 2276 if (y == y1i) yprop *= y1 - y;
Chris@330 2277 ymag[y] += yprop * value;
Chris@330 2278 ydiv[y] += yprop;
Chris@329 2279 }
Chris@329 2280 }
Chris@330 2281 }
Chris@330 2282 }
Chris@329 2283 /*
Chris@329 2284 if (mag.isSet()) {
Chris@329 2285
Chris@329 2286 if (s >= int(m_columnMags.size())) {
Chris@329 2287 std::cerr << "INTERNAL ERROR: " << s << " >= "
Chris@329 2288 << m_columnMags.size() << " at SpectrogramLayer.cpp:" << __LINE__ << std::endl;
Chris@329 2289 }
Chris@329 2290
Chris@329 2291 m_columnMags[s].sample(mag);
Chris@329 2292
Chris@329 2293 if (overallMag.sample(mag)) {
Chris@329 2294 //!!! scaling would change here
Chris@329 2295 overallMagChanged = true;
Chris@329 2296 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@329 2297 std::cerr << "Overall mag changed (again?) at column " << s << ", to [" << overallMag.getMin() << "->" << overallMag.getMax() << "]" << std::endl;
Chris@329 2298 #endif
Chris@329 2299 }
Chris@329 2300 }
Chris@329 2301 */
Chris@329 2302
Chris@329 2303 for (int y = 0; y < h; ++y) {
Chris@329 2304
Chris@329 2305 if (ydiv[y] > 0.0) {
Chris@329 2306
Chris@329 2307 unsigned char pixel = 0;
Chris@329 2308
Chris@329 2309 float avg = ymag[y] / ydiv[y];
Chris@330 2310 pixel = getDisplayValue(v, avg); //!!!???
Chris@329 2311
Chris@329 2312 // assert(x <= m_drawBuffer.width());
Chris@330 2313 assert(x <= w);
Chris@330 2314 QColor c = palette.getColour(pixel);
Chris@330 2315
Chris@330 2316 if (x0 + x >= w) {
Chris@329 2317 std::cerr << "INTERNAL ERROR: " << x0 << "+"
Chris@330 2318 << x << " >= " << w
Chris@329 2319 << " at SpectrogramLayer.cpp:"
Chris@329 2320 << __LINE__ << std::endl;
Chris@329 2321 continue;
Chris@329 2322 }
Chris@329 2323
Chris@330 2324 if (y >= h) {
Chris@329 2325 std::cerr << "INTERNAL ERROR: " << y
Chris@330 2326 << " >= " << h
Chris@329 2327 << " at SpectrogramLayer.cpp:"
Chris@329 2328 << __LINE__ << std::endl;
Chris@329 2329 continue;
Chris@329 2330 }
Chris@329 2331
Chris@330 2332 assert(x0 + x < w);
Chris@330 2333 assert(y < h);
Chris@330 2334
Chris@330 2335 image.setPixel(x0 + x, y,
Chris@330 2336 qRgb(c.red(), c.green(), c.blue()));
Chris@329 2337 }
Chris@329 2338 }
Chris@329 2339 }
Chris@329 2340 /*
Chris@329 2341 if (overallMagChanged) {
Chris@329 2342 m_viewMags[v] = overallMag;
Chris@329 2343 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@329 2344 std::cerr << "Overall mag is now [" << m_viewMags[v].getMin() << "->" << m_viewMags[v].getMax() << "] - will be updating" << std::endl;
Chris@329 2345 #endif
Chris@329 2346 } else {
Chris@329 2347 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@329 2348 std::cerr << "Overall mag unchanged at [" << m_viewMags[v].getMin() << "->" << m_viewMags[v].getMax() << "]" << std::endl;
Chris@329 2349 #endif
Chris@329 2350 }
Chris@329 2351 */
Chris@96 2352 if (cache.validArea.width() > 0) {
Chris@332 2353
Chris@96 2354 int vx0 = 0, vx1 = 0;
Chris@96 2355 vx0 = cache.validArea.x();
Chris@96 2356 vx1 = cache.validArea.x() + cache.validArea.width();
Chris@96 2357
Chris@96 2358 cache.validArea = QRect
Chris@96 2359 (std::min(vx0, x0), cache.validArea.y(),
Chris@96 2360 std::max(vx1 - std::min(vx0, x0),
Chris@329 2361 x1 - std::min(vx0, x0)),
Chris@96 2362 cache.validArea.height());
Chris@332 2363
Chris@96 2364 } else {
Chris@332 2365
Chris@224 2366 cache.validArea = QRect(x0, 0, x1 - x0, h);
Chris@95 2367 }
Chris@332 2368
Chris@329 2369 /*
Chris@329 2370 Profiler profiler2("SpectrogramLayer::paintCache: draw image", true);
Chris@137 2371
Chris@224 2372 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@224 2373 std::cerr << "Painting " << w << "x" << rect.height()
Chris@224 2374 << " from draw buffer at " << 0 << "," << rect.y()
Chris@224 2375 << " to window at " << x0 << "," << rect.y() << std::endl;
Chris@224 2376 #endif
Chris@224 2377
Chris@224 2378 paint.drawImage(x0, rect.y(), m_drawBuffer, 0, rect.y(), w, rect.height());
Chris@0 2379
Chris@330 2380 if (recreateWholeCache) {
Chris@329 2381 // cache.pixmap = QPixmap(v->width(), h);
Chris@329 2382 cache.pixmap = QImage(v->width(), h, QImage::Format_RGB32);
Chris@0 2383 }
Chris@0 2384
Chris@224 2385 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@224 2386 std::cerr << "Painting " << w << "x" << h
Chris@224 2387 << " from draw buffer at " << 0 << "," << 0
Chris@224 2388 << " to cache at " << x0 << "," << 0 << std::endl;
Chris@224 2389 #endif
Chris@224 2390
Chris@95 2391 QPainter cachePainter(&cache.pixmap);
Chris@224 2392 cachePainter.drawImage(x0, 0, m_drawBuffer, 0, 0, w, h);
Chris@0 2393 cachePainter.end();
Chris@329 2394 */
Chris@329 2395 /*
Chris@120 2396 if (!m_normalizeVisibleArea || !overallMagChanged) {
Chris@329 2397 */
Chris@119 2398 cache.startFrame = startFrame;
Chris@119 2399
Chris@329 2400 bool morePaintingRequired = false;
Chris@329 2401
Chris@119 2402 if (cache.validArea.x() > 0) {
Chris@95 2403 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@329 2404 std::cerr << "SpectrogramLayer::paintCache() updating left (0, "
Chris@158 2405 << cache.validArea.x() << ")" << std::endl;
Chris@95 2406 #endif
Chris@329 2407 // v->update(0, 0, cache.validArea.x(), h);
Chris@329 2408 morePaintingRequired = true;
Chris@119 2409 }
Chris@119 2410
Chris@119 2411 if (cache.validArea.x() + cache.validArea.width() <
Chris@330 2412 image.width()) {
Chris@95 2413 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@329 2414 std::cerr << "SpectrogramLayer::paintCache() updating right ("
Chris@119 2415 << cache.validArea.x() + cache.validArea.width()
Chris@119 2416 << ", "
Chris@330 2417 << image.width() - (cache.validArea.x() +
Chris@119 2418 cache.validArea.width())
Chris@119 2419 << ")" << std::endl;
Chris@95 2420 #endif
Chris@329 2421 // v->update(cache.validArea.x() + cache.validArea.width(),
Chris@329 2422 // 0,
Chris@330 2423 // image.width() - (cache.validArea.x() +
Chris@329 2424 // cache.validArea.width()),
Chris@329 2425 // h);
Chris@329 2426 morePaintingRequired = true;
Chris@119 2427 }
Chris@329 2428 /*
Chris@119 2429 } else {
Chris@119 2430 // overallMagChanged
Chris@119 2431 cache.validArea = QRect();
Chris@329 2432 morePaintingRequired = true;
Chris@329 2433 // v->update();
Chris@95 2434 }
Chris@329 2435 */
Chris@329 2436 // illuminateLocalFeatures(v, paint);
Chris@120 2437
Chris@0 2438 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@329 2439 std::cerr << "SpectrogramLayer::paintCache() returning" << std::endl;
Chris@0 2440 #endif
Chris@131 2441
Chris@330 2442 paint.end();
Chris@330 2443
Chris@215 2444 m_lastPaintBlockWidth = paintBlockWidth;
Chris@215 2445 (void)gettimeofday(&tv, 0);
Chris@215 2446 m_lastPaintTime = RealTime::fromTimeval(tv) - mainPaintStart;
Chris@215 2447
Chris@329 2448 return !morePaintingRequired;
Chris@329 2449
Chris@329 2450 // if (fftSuspended) fft->resume();
Chris@0 2451 }
Chris@0 2452
Chris@332 2453 bool
Chris@332 2454 SpectrogramLayer::checkAndScrollCache(PixmapCache &cache, QPainter &paint,
Chris@332 2455 long startFrame, int increment,
Chris@332 2456 int &x0, int &x1) const
Chris@332 2457 {
Chris@332 2458 int w = cache.pixmap->width();
Chris@332 2459
Chris@332 2460 if (cache.validArea.width() == 0) {
Chris@332 2461 x0 = 0;
Chris@332 2462 x1 = w;
Chris@332 2463 return true; // redraw everything
Chris@332 2464 }
Chris@332 2465
Chris@332 2466 //!!! no, the gui thread will handle resizes -- we just draw
Chris@332 2467 //the area that's available in the cache. If the image is
Chris@332 2468 //enlarged, our valid area should remain the same and we'll
Chris@332 2469 //pick up on the discrepancy naturally (we hope)
Chris@332 2470 // if (cache.pixmap->width() == v->width() &&
Chris@332 2471 // cache.pixmap->height() == v->height()) {
Chris@332 2472
Chris@332 2473 if (cache.startFrame / increment == startFrame / increment &&
Chris@332 2474 cache.validArea.x() <= x0 &&
Chris@332 2475 cache.validArea.x() + cache.validArea.width() >= x1) {
Chris@332 2476
Chris@332 2477 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@332 2478 std::cerr << "SpectrogramLayer: pixmap cache good" << std::endl;
Chris@332 2479 #endif
Chris@332 2480
Chris@332 2481 //!!! set a flag to that effect?
Chris@332 2482
Chris@332 2483 x1 = x0;
Chris@332 2484 return false;
Chris@332 2485
Chris@332 2486 } else {
Chris@332 2487
Chris@332 2488 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@332 2489 std::cerr << "SpectrogramLayer: pixmap cache partially OK" << std::endl;
Chris@332 2490 #endif
Chris@332 2491
Chris@332 2492 int dx = cache.startFrame / increment - startFrame / increment;
Chris@332 2493
Chris@332 2494 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@332 2495 std::cerr << "SpectrogramLayer: dx = " << dx << " (pixmap cache " << cache.pixmap->width() << "x" << cache.pixmap->height() << ")" << std::endl;
Chris@332 2496 #endif
Chris@332 2497
Chris@332 2498 if (dx != 0 && dx > -w && dx < w) {
Chris@332 2499
Chris@332 2500 //#if defined(Q_WS_WIN32) || defined(Q_WS_MAC)
Chris@332 2501 // Copying a pixmap to itself doesn't work
Chris@332 2502 // properly on Windows or Mac (it only works when
Chris@332 2503 // moving in one direction).
Chris@332 2504
Chris@332 2505 //!!! Need a utility function for this
Chris@332 2506
Chris@332 2507 //!!! test again on Win and OS/X for images (above
Chris@332 2508 //comment applies to pixmaps)
Chris@332 2509
Chris@332 2510 //!!! indeed, test again on linux for images too
Chris@332 2511 //-- I bet it doesn't work
Chris@332 2512
Chris@332 2513 std::cerr << "SpectrogramLayer: copying from " << 0
Chris@332 2514 << " to " << dx << std::endl;
Chris@332 2515
Chris@332 2516 static QImage *tmp = 0; //!!! would need to be thread-local
Chris@332 2517 if (!tmp ||
Chris@332 2518 tmp->width() != w ||
Chris@332 2519 tmp->height() != cache.pixmap->height()) {
Chris@332 2520 delete tmp;
Chris@332 2521 tmp = new QImage(w,
Chris@332 2522 cache.pixmap->height(),
Chris@332 2523 QImage::Format_RGB32);
Chris@332 2524 }
Chris@332 2525 QPainter tmpPainter;
Chris@332 2526 tmpPainter.begin(tmp);
Chris@332 2527 tmpPainter.drawImage(0, 0, *cache.pixmap);
Chris@332 2528 tmpPainter.end();
Chris@332 2529 paint.drawImage(dx, 0, *tmp);
Chris@332 2530 //#else
Chris@332 2531 // paint.drawImage(dx, 0, image); //!!! see above
Chris@332 2532 //#endif
Chris@332 2533
Chris@332 2534 int px = cache.validArea.x();
Chris@332 2535 int pw = cache.validArea.width();
Chris@332 2536
Chris@332 2537 if (dx < 0) {
Chris@332 2538 x0 = w + dx;
Chris@332 2539 x1 = w;
Chris@332 2540 px += dx;
Chris@332 2541 if (px < 0) {
Chris@332 2542 pw += px;
Chris@332 2543 px = 0;
Chris@332 2544 if (pw < 0) pw = 0;
Chris@332 2545 }
Chris@332 2546 } else {
Chris@332 2547 x0 = 0;
Chris@332 2548 x1 = dx;
Chris@332 2549 px += dx;
Chris@332 2550 if (px + pw > w) {
Chris@332 2551 pw = int(w) - px;
Chris@332 2552 if (pw < 0) pw = 0;
Chris@332 2553 }
Chris@332 2554 }
Chris@332 2555
Chris@332 2556 cache.validArea =
Chris@332 2557 QRect(px, cache.validArea.y(),
Chris@332 2558 pw, cache.validArea.height());
Chris@332 2559 }
Chris@332 2560 }
Chris@332 2561 // } else {
Chris@332 2562 //#ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@332 2563 // std::cerr << "SpectrogramLayer: pixmap cache useless" << std::endl;
Chris@332 2564 // if (cache.pixmap->width() != v->width()) {
Chris@332 2565 // std::cerr << "(cache width " << cache.pixmap->width()
Chris@332 2566 // << " != " << v->width();
Chris@332 2567 // }
Chris@332 2568 // if (cache.pixmap->height() != v->height()) {
Chris@332 2569 // std::cerr << "(cache height " << cache.pixmap->height()
Chris@332 2570 // << " != " << v->height();
Chris@332 2571 // }
Chris@332 2572 //#endif
Chris@332 2573 // cache.validArea = QRect();
Chris@332 2574 // }
Chris@332 2575
Chris@332 2576 /*!!!
Chris@332 2577 if (updateViewMagnitudes(v)) {
Chris@332 2578 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@332 2579 std::cerr << "SpectrogramLayer: magnitude range changed to [" << m_viewMags[v].getMin() << "->" << m_viewMags[v].getMax() << "]" << std::endl;
Chris@332 2580 #endif
Chris@332 2581 recreateWholeCache = true;
Chris@332 2582 } else {
Chris@332 2583 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@332 2584 std::cerr << "No change in magnitude range [" << m_viewMags[v].getMin() << "->" << m_viewMags[v].getMax() << "]" << std::endl;
Chris@332 2585 #endif
Chris@332 2586 }
Chris@332 2587 */
Chris@332 2588 return false;
Chris@332 2589 }
Chris@332 2590
Chris@332 2591 void
Chris@332 2592 SpectrogramLayer::selectPaintStrip(PixmapCache &cache, int &x0, int &x1,
Chris@332 2593 int paintBlockWidth) const
Chris@332 2594 {
Chris@332 2595 // This function narrows down the region [x0, x1) where x1 >= x0
Chris@332 2596 // to a strip of approximately paintBlockWidth width.
Chris@332 2597
Chris@332 2598 if (x1 - x0 < paintBlockWidth * 3 / 2) return; // it'll do
Chris@332 2599
Chris@332 2600 if (cache.validArea.width() > 0) {
Chris@332 2601
Chris@332 2602 // If part of the cache is known to be valid, select a strip
Chris@332 2603 // immediately to left or right of the valid part
Chris@332 2604
Chris@332 2605 int vx0 = 0, vx1 = 0; // valid extents
Chris@332 2606 vx0 = cache.validArea.x();
Chris@332 2607 vx1 = cache.validArea.x() + cache.validArea.width();
Chris@332 2608
Chris@332 2609 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@332 2610 std::cerr << "x0 " << x0 << ", x1 " << x1 << ", vx0 " << vx0 << ", vx1 " << vx1 << ", paintBlockWidth " << paintBlockWidth << std::endl;
Chris@332 2611 #endif
Chris@332 2612 if (x0 < vx0) {
Chris@332 2613 if (x0 + paintBlockWidth < vx0) {
Chris@332 2614 x0 = vx0 - paintBlockWidth;
Chris@332 2615 // } else {
Chris@332 2616 // x0 = 0;
Chris@332 2617 }
Chris@332 2618 x1 = vx0;
Chris@332 2619 } else if (x0 >= vx1) {
Chris@332 2620 x0 = vx1;
Chris@332 2621 if (x1 > x0 + paintBlockWidth) {
Chris@332 2622 x1 = x0 + paintBlockWidth;
Chris@332 2623 }
Chris@332 2624 } else {
Chris@332 2625 // x0 is within the valid area
Chris@332 2626 if (x1 > vx1) {
Chris@332 2627 x0 = vx1;
Chris@332 2628 if (x0 + paintBlockWidth < x1) {
Chris@332 2629 x1 = x0 + paintBlockWidth;
Chris@332 2630 }
Chris@332 2631 } else {
Chris@332 2632 x1 = x0; // it's all valid, paint nothing
Chris@332 2633 }
Chris@332 2634 }
Chris@332 2635
Chris@332 2636 } else {
Chris@332 2637
Chris@332 2638 // No existing valid area; start at the middle, unless x0 is
Chris@332 2639 // already to the left of frame 0
Chris@332 2640
Chris@332 2641 //!!! no, for now just start at the middle always
Chris@332 2642
Chris@332 2643 if (x1 > x0 + paintBlockWidth) {
Chris@332 2644
Chris@332 2645 // int sfx = x1; // x for frame zero
Chris@332 2646 // if (startFrame < 0) sfx = v->getXForFrame(0); //!!!
Chris@332 2647
Chris@332 2648 // if (startFrame < 0) sfx =
Chris@332 2649 // if (sfx >= x0 && sfx + paintBlockWidth <= x1) {
Chris@332 2650 // x0 = sfx;
Chris@332 2651 // x1 = x0 + paintBlockWidth;
Chris@332 2652 // } else {
Chris@332 2653 int mid = (x1 + x0) / 2;
Chris@332 2654 x0 = mid - paintBlockWidth/2;
Chris@332 2655 x1 = x0 + paintBlockWidth;
Chris@332 2656 // }
Chris@332 2657 }
Chris@332 2658 }
Chris@332 2659 }
Chris@332 2660
Chris@121 2661 void
Chris@121 2662 SpectrogramLayer::illuminateLocalFeatures(View *v, QPainter &paint) const
Chris@121 2663 {
Chris@121 2664 QPoint localPos;
Chris@121 2665 if (!v->shouldIlluminateLocalFeatures(this, localPos) || !m_model) {
Chris@121 2666 return;
Chris@121 2667 }
Chris@121 2668
Chris@180 2669 // std::cerr << "SpectrogramLayer: illuminateLocalFeatures("
Chris@180 2670 // << localPos.x() << "," << localPos.y() << ")" << std::endl;
Chris@121 2671
Chris@121 2672 float s0, s1;
Chris@121 2673 float f0, f1;
Chris@121 2674
Chris@121 2675 if (getXBinRange(v, localPos.x(), s0, s1) &&
Chris@121 2676 getYBinSourceRange(v, localPos.y(), f0, f1)) {
Chris@121 2677
Chris@121 2678 int s0i = int(s0 + 0.001);
Chris@121 2679 int s1i = int(s1);
Chris@121 2680
Chris@121 2681 int x0 = v->getXForFrame(s0i * getWindowIncrement());
Chris@121 2682 int x1 = v->getXForFrame((s1i + 1) * getWindowIncrement());
Chris@121 2683
Chris@248 2684 int y1 = int(getYForFrequency(v, f1));
Chris@248 2685 int y0 = int(getYForFrequency(v, f0));
Chris@121 2686
Chris@180 2687 // std::cerr << "SpectrogramLayer: illuminate "
Chris@180 2688 // << x0 << "," << y1 << " -> " << x1 << "," << y0 << std::endl;
Chris@121 2689
Chris@287 2690 paint.setPen(v->getForeground());
Chris@133 2691
Chris@133 2692 //!!! should we be using paintCrosshairs for this?
Chris@133 2693
Chris@121 2694 paint.drawRect(x0, y1, x1 - x0 + 1, y0 - y1 + 1);
Chris@121 2695 }
Chris@121 2696 }
Chris@121 2697
Chris@42 2698 float
Chris@267 2699 SpectrogramLayer::getYForFrequency(const View *v, float frequency) const
Chris@42 2700 {
Chris@44 2701 return v->getYForFrequency(frequency,
Chris@44 2702 getEffectiveMinFrequency(),
Chris@44 2703 getEffectiveMaxFrequency(),
Chris@44 2704 m_frequencyScale == LogFrequencyScale);
Chris@42 2705 }
Chris@42 2706
Chris@42 2707 float
Chris@267 2708 SpectrogramLayer::getFrequencyForY(const View *v, int y) const
Chris@42 2709 {
Chris@44 2710 return v->getFrequencyForY(y,
Chris@44 2711 getEffectiveMinFrequency(),
Chris@44 2712 getEffectiveMaxFrequency(),
Chris@44 2713 m_frequencyScale == LogFrequencyScale);
Chris@42 2714 }
Chris@42 2715
Chris@0 2716 int
Chris@115 2717 SpectrogramLayer::getCompletion(View *v) const
Chris@0 2718 {
Chris@115 2719 if (m_updateTimer == 0) return 100;
Chris@130 2720 if (m_fftModels.find(v) == m_fftModels.end()) return 100;
Chris@130 2721
Chris@130 2722 size_t completion = m_fftModels[v].first->getCompletion();
Chris@224 2723 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@115 2724 std::cerr << "SpectrogramLayer::getCompletion: completion = " << completion << std::endl;
Chris@224 2725 #endif
Chris@0 2726 return completion;
Chris@0 2727 }
Chris@0 2728
Chris@28 2729 bool
Chris@101 2730 SpectrogramLayer::getValueExtents(float &min, float &max,
Chris@101 2731 bool &logarithmic, QString &unit) const
Chris@79 2732 {
Chris@133 2733 if (!m_model) return false;
Chris@133 2734
Chris@133 2735 int sr = m_model->getSampleRate();
Chris@133 2736 min = float(sr) / m_fftSize;
Chris@133 2737 max = float(sr) / 2;
Chris@133 2738
Chris@101 2739 logarithmic = (m_frequencyScale == LogFrequencyScale);
Chris@79 2740 unit = "Hz";
Chris@79 2741 return true;
Chris@79 2742 }
Chris@79 2743
Chris@79 2744 bool
Chris@101 2745 SpectrogramLayer::getDisplayExtents(float &min, float &max) const
Chris@101 2746 {
Chris@101 2747 min = getEffectiveMinFrequency();
Chris@101 2748 max = getEffectiveMaxFrequency();
Chris@253 2749
Chris@248 2750 // std::cerr << "SpectrogramLayer::getDisplayExtents: " << min << "->" << max << std::endl;
Chris@101 2751 return true;
Chris@101 2752 }
Chris@101 2753
Chris@101 2754 bool
Chris@120 2755 SpectrogramLayer::setDisplayExtents(float min, float max)
Chris@120 2756 {
Chris@120 2757 if (!m_model) return false;
Chris@187 2758
Chris@253 2759 // std::cerr << "SpectrogramLayer::setDisplayExtents: " << min << "->" << max << std::endl;
Chris@187 2760
Chris@120 2761 if (min < 0) min = 0;
Chris@120 2762 if (max > m_model->getSampleRate()/2) max = m_model->getSampleRate()/2;
Chris@120 2763
Chris@120 2764 size_t minf = lrintf(min);
Chris@120 2765 size_t maxf = lrintf(max);
Chris@120 2766
Chris@120 2767 if (m_minFrequency == minf && m_maxFrequency == maxf) return true;
Chris@120 2768
Chris@120 2769 invalidatePixmapCaches();
Chris@120 2770 invalidateMagnitudes();
Chris@120 2771
Chris@120 2772 m_minFrequency = minf;
Chris@120 2773 m_maxFrequency = maxf;
Chris@120 2774
Chris@120 2775 emit layerParametersChanged();
Chris@120 2776
Chris@133 2777 int vs = getCurrentVerticalZoomStep();
Chris@133 2778 if (vs != m_lastEmittedZoomStep) {
Chris@133 2779 emit verticalZoomChanged();
Chris@133 2780 m_lastEmittedZoomStep = vs;
Chris@133 2781 }
Chris@133 2782
Chris@120 2783 return true;
Chris@120 2784 }
Chris@120 2785
Chris@120 2786 bool
Chris@267 2787 SpectrogramLayer::getYScaleValue(const View *v, int y,
Chris@261 2788 float &value, QString &unit) const
Chris@261 2789 {
Chris@261 2790 value = getFrequencyForY(v, y);
Chris@261 2791 unit = "Hz";
Chris@261 2792 return true;
Chris@261 2793 }
Chris@261 2794
Chris@261 2795 bool
Chris@248 2796 SpectrogramLayer::snapToFeatureFrame(View *, int &frame,
Chris@28 2797 size_t &resolution,
Chris@28 2798 SnapType snap) const
Chris@13 2799 {
Chris@13 2800 resolution = getWindowIncrement();
Chris@28 2801 int left = (frame / resolution) * resolution;
Chris@28 2802 int right = left + resolution;
Chris@28 2803
Chris@28 2804 switch (snap) {
Chris@28 2805 case SnapLeft: frame = left; break;
Chris@28 2806 case SnapRight: frame = right; break;
Chris@28 2807 case SnapNearest:
Chris@28 2808 case SnapNeighbouring:
Chris@28 2809 if (frame - left > right - frame) frame = right;
Chris@28 2810 else frame = left;
Chris@28 2811 break;
Chris@28 2812 }
Chris@28 2813
Chris@28 2814 return true;
Chris@28 2815 }
Chris@13 2816
Chris@283 2817 void
Chris@283 2818 SpectrogramLayer::measureDoubleClick(View *v, QMouseEvent *e)
Chris@283 2819 {
Chris@329 2820 //!!! check cache consistency
Chris@329 2821 QMutexLocker locker(&m_pixmapCacheMutex);
Chris@329 2822
Chris@329 2823 PixmapCache *cache = m_pixmapCaches[v];
Chris@330 2824 if (!cache || !cache->pixmap) return;
Chris@330 2825
Chris@330 2826 std::cerr << "cache width: " << cache->pixmap->width() << ", height: "
Chris@330 2827 << cache->pixmap->height() << std::endl;
Chris@330 2828
Chris@330 2829 //!!! lock
Chris@330 2830
Chris@330 2831 QImage image = *cache->pixmap /*.toImage() */;
Chris@283 2832
Chris@283 2833 ImageRegionFinder finder;
Chris@283 2834 QRect rect = finder.findRegionExtents(&image, e->pos());
Chris@283 2835 if (rect.isValid()) {
Chris@283 2836 MeasureRect mr;
Chris@283 2837 setMeasureRectFromPixrect(v, mr, rect);
Chris@283 2838 CommandHistory::getInstance()->addCommand
Chris@283 2839 (new AddMeasurementRectCommand(this, mr));
Chris@283 2840 }
Chris@283 2841 }
Chris@283 2842
Chris@77 2843 bool
Chris@264 2844 SpectrogramLayer::getCrosshairExtents(View *v, QPainter &paint,
Chris@77 2845 QPoint cursorPos,
Chris@77 2846 std::vector<QRect> &extents) const
Chris@77 2847 {
Chris@77 2848 QRect vertical(cursorPos.x() - 12, 0, 12, v->height());
Chris@77 2849 extents.push_back(vertical);
Chris@77 2850
Chris@77 2851 QRect horizontal(0, cursorPos.y(), cursorPos.x(), 1);
Chris@77 2852 extents.push_back(horizontal);
Chris@77 2853
Chris@264 2854 int sw = getVerticalScaleWidth(v, paint);
Chris@264 2855
Chris@280 2856 QRect freq(sw, cursorPos.y() - paint.fontMetrics().ascent() - 2,
Chris@280 2857 paint.fontMetrics().width("123456 Hz") + 2,
Chris@280 2858 paint.fontMetrics().height());
Chris@280 2859 extents.push_back(freq);
Chris@264 2860
Chris@279 2861 QRect pitch(sw, cursorPos.y() + 2,
Chris@279 2862 paint.fontMetrics().width("C#10+50c") + 2,
Chris@279 2863 paint.fontMetrics().height());
Chris@279 2864 extents.push_back(pitch);
Chris@279 2865
Chris@280 2866 QRect rt(cursorPos.x(),
Chris@280 2867 v->height() - paint.fontMetrics().height() - 2,
Chris@280 2868 paint.fontMetrics().width("1234.567 s"),
Chris@280 2869 paint.fontMetrics().height());
Chris@280 2870 extents.push_back(rt);
Chris@280 2871
Chris@280 2872 int w(paint.fontMetrics().width("1234567890") + 2);
Chris@280 2873 QRect frame(cursorPos.x() - w - 2,
Chris@280 2874 v->height() - paint.fontMetrics().height() - 2,
Chris@280 2875 w,
Chris@280 2876 paint.fontMetrics().height());
Chris@280 2877 extents.push_back(frame);
Chris@280 2878
Chris@77 2879 return true;
Chris@77 2880 }
Chris@77 2881
Chris@77 2882 void
Chris@77 2883 SpectrogramLayer::paintCrosshairs(View *v, QPainter &paint,
Chris@77 2884 QPoint cursorPos) const
Chris@77 2885 {
Chris@77 2886 paint.save();
Chris@283 2887
Chris@283 2888 int sw = getVerticalScaleWidth(v, paint);
Chris@283 2889
Chris@282 2890 QFont fn = paint.font();
Chris@282 2891 if (fn.pointSize() > 8) {
Chris@282 2892 fn.setPointSize(fn.pointSize() - 1);
Chris@282 2893 paint.setFont(fn);
Chris@282 2894 }
Chris@77 2895 paint.setPen(m_crosshairColour);
Chris@77 2896
Chris@77 2897 paint.drawLine(0, cursorPos.y(), cursorPos.x() - 1, cursorPos.y());
Chris@77 2898 paint.drawLine(cursorPos.x(), 0, cursorPos.x(), v->height());
Chris@77 2899
Chris@77 2900 float fundamental = getFrequencyForY(v, cursorPos.y());
Chris@77 2901
Chris@278 2902 v->drawVisibleText(paint,
Chris@278 2903 sw + 2,
Chris@278 2904 cursorPos.y() - 2,
Chris@278 2905 QString("%1 Hz").arg(fundamental),
Chris@278 2906 View::OutlinedText);
Chris@278 2907
Chris@279 2908 if (Pitch::isFrequencyInMidiRange(fundamental)) {
Chris@279 2909 QString pitchLabel = Pitch::getPitchLabelForFrequency(fundamental);
Chris@279 2910 v->drawVisibleText(paint,
Chris@279 2911 sw + 2,
Chris@279 2912 cursorPos.y() + paint.fontMetrics().ascent() + 2,
Chris@279 2913 pitchLabel,
Chris@279 2914 View::OutlinedText);
Chris@279 2915 }
Chris@279 2916
Chris@280 2917 long frame = v->getFrameForX(cursorPos.x());
Chris@279 2918 RealTime rt = RealTime::frame2RealTime(frame, m_model->getSampleRate());
Chris@280 2919 QString rtLabel = QString("%1 s").arg(rt.toText(true).c_str());
Chris@280 2920 QString frameLabel = QString("%1").arg(frame);
Chris@280 2921 v->drawVisibleText(paint,
Chris@280 2922 cursorPos.x() - paint.fontMetrics().width(frameLabel) - 2,
Chris@280 2923 v->height() - 2,
Chris@280 2924 frameLabel,
Chris@280 2925 View::OutlinedText);
Chris@280 2926 v->drawVisibleText(paint,
Chris@280 2927 cursorPos.x() + 2,
Chris@280 2928 v->height() - 2,
Chris@280 2929 rtLabel,
Chris@280 2930 View::OutlinedText);
Chris@264 2931
Chris@77 2932 int harmonic = 2;
Chris@77 2933
Chris@77 2934 while (harmonic < 100) {
Chris@77 2935
Chris@77 2936 float hy = lrintf(getYForFrequency(v, fundamental * harmonic));
Chris@77 2937 if (hy < 0 || hy > v->height()) break;
Chris@77 2938
Chris@77 2939 int len = 7;
Chris@77 2940
Chris@77 2941 if (harmonic % 2 == 0) {
Chris@77 2942 if (harmonic % 4 == 0) {
Chris@77 2943 len = 12;
Chris@77 2944 } else {
Chris@77 2945 len = 10;
Chris@77 2946 }
Chris@77 2947 }
Chris@77 2948
Chris@77 2949 paint.drawLine(cursorPos.x() - len,
Chris@248 2950 int(hy),
Chris@77 2951 cursorPos.x(),
Chris@248 2952 int(hy));
Chris@77 2953
Chris@77 2954 ++harmonic;
Chris@77 2955 }
Chris@77 2956
Chris@77 2957 paint.restore();
Chris@77 2958 }
Chris@77 2959
Chris@25 2960 QString
Chris@44 2961 SpectrogramLayer::getFeatureDescription(View *v, QPoint &pos) const
Chris@25 2962 {
Chris@25 2963 int x = pos.x();
Chris@25 2964 int y = pos.y();
Chris@0 2965
Chris@25 2966 if (!m_model || !m_model->isOK()) return "";
Chris@0 2967
Chris@38 2968 float magMin = 0, magMax = 0;
Chris@38 2969 float phaseMin = 0, phaseMax = 0;
Chris@0 2970 float freqMin = 0, freqMax = 0;
Chris@35 2971 float adjFreqMin = 0, adjFreqMax = 0;
Chris@25 2972 QString pitchMin, pitchMax;
Chris@0 2973 RealTime rtMin, rtMax;
Chris@0 2974
Chris@38 2975 bool haveValues = false;
Chris@0 2976
Chris@44 2977 if (!getXBinSourceRange(v, x, rtMin, rtMax)) {
Chris@38 2978 return "";
Chris@38 2979 }
Chris@44 2980 if (getXYBinSourceRange(v, x, y, magMin, magMax, phaseMin, phaseMax)) {
Chris@38 2981 haveValues = true;
Chris@38 2982 }
Chris@0 2983
Chris@35 2984 QString adjFreqText = "", adjPitchText = "";
Chris@35 2985
Chris@38 2986 if (m_binDisplay == PeakFrequencies) {
Chris@35 2987
Chris@44 2988 if (!getAdjustedYBinSourceRange(v, x, y, freqMin, freqMax,
Chris@38 2989 adjFreqMin, adjFreqMax)) {
Chris@38 2990 return "";
Chris@38 2991 }
Chris@35 2992
Chris@35 2993 if (adjFreqMin != adjFreqMax) {
Chris@65 2994 adjFreqText = tr("Peak Frequency:\t%1 - %2 Hz\n")
Chris@35 2995 .arg(adjFreqMin).arg(adjFreqMax);
Chris@35 2996 } else {
Chris@65 2997 adjFreqText = tr("Peak Frequency:\t%1 Hz\n")
Chris@35 2998 .arg(adjFreqMin);
Chris@38 2999 }
Chris@38 3000
Chris@38 3001 QString pmin = Pitch::getPitchLabelForFrequency(adjFreqMin);
Chris@38 3002 QString pmax = Pitch::getPitchLabelForFrequency(adjFreqMax);
Chris@38 3003
Chris@38 3004 if (pmin != pmax) {
Chris@65 3005 adjPitchText = tr("Peak Pitch:\t%3 - %4\n").arg(pmin).arg(pmax);
Chris@38 3006 } else {
Chris@65 3007 adjPitchText = tr("Peak Pitch:\t%2\n").arg(pmin);
Chris@35 3008 }
Chris@35 3009
Chris@35 3010 } else {
Chris@35 3011
Chris@44 3012 if (!getYBinSourceRange(v, y, freqMin, freqMax)) return "";
Chris@35 3013 }
Chris@35 3014
Chris@25 3015 QString text;
Chris@25 3016
Chris@25 3017 if (rtMin != rtMax) {
Chris@25 3018 text += tr("Time:\t%1 - %2\n")
Chris@25 3019 .arg(rtMin.toText(true).c_str())
Chris@25 3020 .arg(rtMax.toText(true).c_str());
Chris@25 3021 } else {
Chris@25 3022 text += tr("Time:\t%1\n")
Chris@25 3023 .arg(rtMin.toText(true).c_str());
Chris@0 3024 }
Chris@0 3025
Chris@25 3026 if (freqMin != freqMax) {
Chris@65 3027 text += tr("%1Bin Frequency:\t%2 - %3 Hz\n%4Bin Pitch:\t%5 - %6\n")
Chris@65 3028 .arg(adjFreqText)
Chris@25 3029 .arg(freqMin)
Chris@25 3030 .arg(freqMax)
Chris@65 3031 .arg(adjPitchText)
Chris@65 3032 .arg(Pitch::getPitchLabelForFrequency(freqMin))
Chris@65 3033 .arg(Pitch::getPitchLabelForFrequency(freqMax));
Chris@65 3034 } else {
Chris@65 3035 text += tr("%1Bin Frequency:\t%2 Hz\n%3Bin Pitch:\t%4\n")
Chris@35 3036 .arg(adjFreqText)
Chris@25 3037 .arg(freqMin)
Chris@65 3038 .arg(adjPitchText)
Chris@65 3039 .arg(Pitch::getPitchLabelForFrequency(freqMin));
Chris@25 3040 }
Chris@25 3041
Chris@38 3042 if (haveValues) {
Chris@38 3043 float dbMin = AudioLevel::multiplier_to_dB(magMin);
Chris@38 3044 float dbMax = AudioLevel::multiplier_to_dB(magMax);
Chris@43 3045 QString dbMinString;
Chris@43 3046 QString dbMaxString;
Chris@43 3047 if (dbMin == AudioLevel::DB_FLOOR) {
Chris@43 3048 dbMinString = tr("-Inf");
Chris@43 3049 } else {
Chris@43 3050 dbMinString = QString("%1").arg(lrintf(dbMin));
Chris@43 3051 }
Chris@43 3052 if (dbMax == AudioLevel::DB_FLOOR) {
Chris@43 3053 dbMaxString = tr("-Inf");
Chris@43 3054 } else {
Chris@43 3055 dbMaxString = QString("%1").arg(lrintf(dbMax));
Chris@43 3056 }
Chris@25 3057 if (lrintf(dbMin) != lrintf(dbMax)) {
Chris@199 3058 text += tr("dB:\t%1 - %2").arg(dbMinString).arg(dbMaxString);
Chris@25 3059 } else {
Chris@199 3060 text += tr("dB:\t%1").arg(dbMinString);
Chris@25 3061 }
Chris@38 3062 if (phaseMin != phaseMax) {
Chris@38 3063 text += tr("\nPhase:\t%1 - %2").arg(phaseMin).arg(phaseMax);
Chris@38 3064 } else {
Chris@38 3065 text += tr("\nPhase:\t%1").arg(phaseMin);
Chris@38 3066 }
Chris@25 3067 }
Chris@25 3068
Chris@25 3069 return text;
Chris@0 3070 }
Chris@25 3071
Chris@0 3072 int
Chris@40 3073 SpectrogramLayer::getColourScaleWidth(QPainter &paint) const
Chris@40 3074 {
Chris@40 3075 int cw;
Chris@40 3076
Chris@119 3077 cw = paint.fontMetrics().width("-80dB");
Chris@119 3078
Chris@40 3079 return cw;
Chris@40 3080 }
Chris@40 3081
Chris@40 3082 int
Chris@248 3083 SpectrogramLayer::getVerticalScaleWidth(View *, QPainter &paint) const
Chris@0 3084 {
Chris@0 3085 if (!m_model || !m_model->isOK()) return 0;
Chris@0 3086
Chris@40 3087 int cw = getColourScaleWidth(paint);
Chris@40 3088
Chris@0 3089 int tw = paint.fontMetrics().width(QString("%1")
Chris@0 3090 .arg(m_maxFrequency > 0 ?
Chris@0 3091 m_maxFrequency - 1 :
Chris@0 3092 m_model->getSampleRate() / 2));
Chris@0 3093
Chris@234 3094 int fw = paint.fontMetrics().width(tr("43Hz"));
Chris@0 3095 if (tw < fw) tw = fw;
Chris@40 3096
Chris@40 3097 int tickw = (m_frequencyScale == LogFrequencyScale ? 10 : 4);
Chris@0 3098
Chris@40 3099 return cw + tickw + tw + 13;
Chris@0 3100 }
Chris@0 3101
Chris@0 3102 void
Chris@44 3103 SpectrogramLayer::paintVerticalScale(View *v, QPainter &paint, QRect rect) const
Chris@0 3104 {
Chris@0 3105 if (!m_model || !m_model->isOK()) {
Chris@0 3106 return;
Chris@0 3107 }
Chris@0 3108
Chris@330 3109 Profiler profiler("SpectrogramLayer::paintVerticalScale", false);
Chris@122 3110
Chris@120 3111 //!!! cache this?
Chris@120 3112
Chris@0 3113 int h = rect.height(), w = rect.width();
Chris@0 3114
Chris@40 3115 int tickw = (m_frequencyScale == LogFrequencyScale ? 10 : 4);
Chris@40 3116 int pkw = (m_frequencyScale == LogFrequencyScale ? 10 : 0);
Chris@40 3117
Chris@107 3118 size_t bins = m_fftSize / 2;
Chris@0 3119 int sr = m_model->getSampleRate();
Chris@0 3120
Chris@0 3121 if (m_maxFrequency > 0) {
Chris@107 3122 bins = int((double(m_maxFrequency) * m_fftSize) / sr + 0.1);
Chris@107 3123 if (bins > m_fftSize / 2) bins = m_fftSize / 2;
Chris@0 3124 }
Chris@0 3125
Chris@40 3126 int cw = getColourScaleWidth(paint);
Chris@119 3127 int cbw = paint.fontMetrics().width("dB");
Chris@40 3128
Chris@0 3129 int py = -1;
Chris@0 3130 int textHeight = paint.fontMetrics().height();
Chris@0 3131 int toff = -textHeight + paint.fontMetrics().ascent() + 2;
Chris@0 3132
Chris@119 3133 if (h > textHeight * 3 + 10) {
Chris@119 3134
Chris@119 3135 int topLines = 2;
Chris@119 3136 if (m_colourScale == PhaseColourScale) topLines = 1;
Chris@119 3137
Chris@119 3138 int ch = h - textHeight * (topLines + 1) - 8;
Chris@119 3139 // paint.drawRect(4, textHeight + 4, cw - 1, ch + 1);
Chris@119 3140 paint.drawRect(4 + cw - cbw, textHeight * topLines + 4, cbw - 1, ch + 1);
Chris@40 3141
Chris@40 3142 QString top, bottom;
Chris@119 3143 float min = m_viewMags[v].getMin();
Chris@119 3144 float max = m_viewMags[v].getMax();
Chris@119 3145
Chris@119 3146 float dBmin = AudioLevel::multiplier_to_dB(min);
Chris@119 3147 float dBmax = AudioLevel::multiplier_to_dB(max);
Chris@119 3148
Chris@120 3149 if (dBmax < -60.f) dBmax = -60.f;
Chris@120 3150 else top = QString("%1").arg(lrintf(dBmax));
Chris@120 3151
Chris@120 3152 if (dBmin < dBmax - 60.f) dBmin = dBmax - 60.f;
Chris@119 3153 bottom = QString("%1").arg(lrintf(dBmin));
Chris@119 3154
Chris@119 3155 //!!! & phase etc
Chris@119 3156
Chris@119 3157 if (m_colourScale != PhaseColourScale) {
Chris@119 3158 paint.drawText((cw + 6 - paint.fontMetrics().width("dBFS")) / 2,
Chris@119 3159 2 + textHeight + toff, "dBFS");
Chris@119 3160 }
Chris@119 3161
Chris@119 3162 // paint.drawText((cw + 6 - paint.fontMetrics().width(top)) / 2,
Chris@119 3163 paint.drawText(3 + cw - cbw - paint.fontMetrics().width(top),
Chris@119 3164 2 + textHeight * topLines + toff + textHeight/2, top);
Chris@119 3165
Chris@119 3166 paint.drawText(3 + cw - cbw - paint.fontMetrics().width(bottom),
Chris@119 3167 h + toff - 3 - textHeight/2, bottom);
Chris@40 3168
Chris@40 3169 paint.save();
Chris@40 3170 paint.setBrush(Qt::NoBrush);
Chris@119 3171
Chris@119 3172 int lasty = 0;
Chris@119 3173 int lastdb = 0;
Chris@119 3174
Chris@40 3175 for (int i = 0; i < ch; ++i) {
Chris@119 3176
Chris@119 3177 float dBval = dBmin + (((dBmax - dBmin) * i) / (ch - 1));
Chris@119 3178 int idb = int(dBval);
Chris@119 3179
Chris@119 3180 float value = AudioLevel::dB_to_multiplier(dBval);
Chris@119 3181 int colour = getDisplayValue(v, value * m_gain);
Chris@210 3182
Chris@197 3183 paint.setPen(m_palette.getColour(colour));
Chris@119 3184
Chris@119 3185 int y = textHeight * topLines + 4 + ch - i;
Chris@119 3186
Chris@119 3187 paint.drawLine(5 + cw - cbw, y, cw + 2, y);
Chris@119 3188
Chris@119 3189 if (i == 0) {
Chris@119 3190 lasty = y;
Chris@119 3191 lastdb = idb;
Chris@119 3192 } else if (i < ch - paint.fontMetrics().ascent() &&
Chris@120 3193 idb != lastdb &&
Chris@119 3194 ((abs(y - lasty) > textHeight &&
Chris@119 3195 idb % 10 == 0) ||
Chris@119 3196 (abs(y - lasty) > paint.fontMetrics().ascent() &&
Chris@119 3197 idb % 5 == 0))) {
Chris@287 3198 paint.setPen(v->getBackground());
Chris@119 3199 QString text = QString("%1").arg(idb);
Chris@119 3200 paint.drawText(3 + cw - cbw - paint.fontMetrics().width(text),
Chris@119 3201 y + toff + textHeight/2, text);
Chris@287 3202 paint.setPen(v->getForeground());
Chris@119 3203 paint.drawLine(5 + cw - cbw, y, 8 + cw - cbw, y);
Chris@119 3204 lasty = y;
Chris@119 3205 lastdb = idb;
Chris@119 3206 }
Chris@40 3207 }
Chris@40 3208 paint.restore();
Chris@40 3209 }
Chris@40 3210
Chris@40 3211 paint.drawLine(cw + 7, 0, cw + 7, h);
Chris@40 3212
Chris@0 3213 int bin = -1;
Chris@0 3214
Chris@44 3215 for (int y = 0; y < v->height(); ++y) {
Chris@0 3216
Chris@0 3217 float q0, q1;
Chris@44 3218 if (!getYBinRange(v, v->height() - y, q0, q1)) continue;
Chris@0 3219
Chris@0 3220 int vy;
Chris@0 3221
Chris@0 3222 if (int(q0) > bin) {
Chris@0 3223 vy = y;
Chris@0 3224 bin = int(q0);
Chris@0 3225 } else {
Chris@0 3226 continue;
Chris@0 3227 }
Chris@0 3228
Chris@107 3229 int freq = (sr * bin) / m_fftSize;
Chris@0 3230
Chris@0 3231 if (py >= 0 && (vy - py) < textHeight - 1) {
Chris@40 3232 if (m_frequencyScale == LinearFrequencyScale) {
Chris@40 3233 paint.drawLine(w - tickw, h - vy, w, h - vy);
Chris@40 3234 }
Chris@0 3235 continue;
Chris@0 3236 }
Chris@0 3237
Chris@0 3238 QString text = QString("%1").arg(freq);
Chris@234 3239 if (bin == 1) text = tr("%1Hz").arg(freq); // bin 0 is DC
Chris@40 3240 paint.drawLine(cw + 7, h - vy, w - pkw - 1, h - vy);
Chris@0 3241
Chris@0 3242 if (h - vy - textHeight >= -2) {
Chris@40 3243 int tx = w - 3 - paint.fontMetrics().width(text) - std::max(tickw, pkw);
Chris@0 3244 paint.drawText(tx, h - vy + toff, text);
Chris@0 3245 }
Chris@0 3246
Chris@0 3247 py = vy;
Chris@0 3248 }
Chris@40 3249
Chris@40 3250 if (m_frequencyScale == LogFrequencyScale) {
Chris@40 3251
Chris@277 3252 // piano keyboard
Chris@277 3253
Chris@40 3254 paint.drawLine(w - pkw - 1, 0, w - pkw - 1, h);
Chris@40 3255
Chris@40 3256 float minf = getEffectiveMinFrequency();
Chris@40 3257 float maxf = getEffectiveMaxFrequency();
Chris@40 3258
Chris@122 3259 int py = h, ppy = h;
Chris@40 3260 paint.setBrush(paint.pen().color());
Chris@40 3261
Chris@40 3262 for (int i = 0; i < 128; ++i) {
Chris@40 3263
Chris@40 3264 float f = Pitch::getFrequencyForPitch(i);
Chris@44 3265 int y = lrintf(v->getYForFrequency(f, minf, maxf, true));
Chris@122 3266
Chris@122 3267 if (y < -2) break;
Chris@122 3268 if (y > h + 2) {
Chris@122 3269 continue;
Chris@122 3270 }
Chris@122 3271
Chris@40 3272 int n = (i % 12);
Chris@122 3273
Chris@122 3274 if (n == 1) {
Chris@122 3275 // C# -- fill the C from here
Chris@284 3276 QColor col = Qt::gray;
Chris@284 3277 if (i == 61) { // filling middle C
Chris@284 3278 col = Qt::blue;
Chris@284 3279 col = col.light(150);
Chris@284 3280 }
Chris@122 3281 if (ppy - y > 2) {
Chris@122 3282 paint.fillRect(w - pkw,
Chris@122 3283 y,
Chris@122 3284 pkw,
Chris@122 3285 (py + ppy) / 2 - y,
Chris@284 3286 col);
Chris@122 3287 }
Chris@122 3288 }
Chris@122 3289
Chris@40 3290 if (n == 1 || n == 3 || n == 6 || n == 8 || n == 10) {
Chris@40 3291 // black notes
Chris@40 3292 paint.drawLine(w - pkw, y, w, y);
Chris@41 3293 int rh = ((py - y) / 4) * 2;
Chris@41 3294 if (rh < 2) rh = 2;
Chris@41 3295 paint.drawRect(w - pkw, y - (py-y)/4, pkw/2, rh);
Chris@40 3296 } else if (n == 0 || n == 5) {
Chris@122 3297 // C, F
Chris@40 3298 if (py < h) {
Chris@40 3299 paint.drawLine(w - pkw, (y + py) / 2, w, (y + py) / 2);
Chris@40 3300 }
Chris@40 3301 }
Chris@40 3302
Chris@122 3303 ppy = py;
Chris@40 3304 py = y;
Chris@40 3305 }
Chris@40 3306 }
Chris@0 3307 }
Chris@0 3308
Chris@187 3309 class SpectrogramRangeMapper : public RangeMapper
Chris@187 3310 {
Chris@187 3311 public:
Chris@248 3312 SpectrogramRangeMapper(int sr, int /* fftsize */) :
Chris@187 3313 m_dist(float(sr) / 2),
Chris@187 3314 m_s2(sqrtf(sqrtf(2))) { }
Chris@187 3315 ~SpectrogramRangeMapper() { }
Chris@187 3316
Chris@187 3317 virtual int getPositionForValue(float value) const {
Chris@187 3318
Chris@187 3319 float dist = m_dist;
Chris@187 3320
Chris@187 3321 int n = 0;
Chris@187 3322
Chris@187 3323 while (dist > (value + 0.00001) && dist > 0.1f) {
Chris@187 3324 dist /= m_s2;
Chris@187 3325 ++n;
Chris@187 3326 }
Chris@187 3327
Chris@187 3328 return n;
Chris@187 3329 }
Chris@187 3330
Chris@187 3331 virtual float getValueForPosition(int position) const {
Chris@187 3332
Chris@187 3333 // Vertical zoom step 0 shows the entire range from DC ->
Chris@187 3334 // Nyquist frequency. Step 1 shows 2^(1/4) of the range of
Chris@187 3335 // step 0, and so on until the visible range is smaller than
Chris@187 3336 // the frequency step between bins at the current fft size.
Chris@187 3337
Chris@187 3338 float dist = m_dist;
Chris@187 3339
Chris@187 3340 int n = 0;
Chris@187 3341 while (n < position) {
Chris@187 3342 dist /= m_s2;
Chris@187 3343 ++n;
Chris@187 3344 }
Chris@187 3345
Chris@187 3346 return dist;
Chris@187 3347 }
Chris@187 3348
Chris@187 3349 virtual QString getUnit() const { return "Hz"; }
Chris@187 3350
Chris@187 3351 protected:
Chris@187 3352 float m_dist;
Chris@187 3353 float m_s2;
Chris@187 3354 };
Chris@187 3355
Chris@133 3356 int
Chris@133 3357 SpectrogramLayer::getVerticalZoomSteps(int &defaultStep) const
Chris@133 3358 {
Chris@135 3359 if (!m_model) return 0;
Chris@187 3360
Chris@187 3361 int sr = m_model->getSampleRate();
Chris@187 3362
Chris@187 3363 SpectrogramRangeMapper mapper(sr, m_fftSize);
Chris@187 3364
Chris@187 3365 // int maxStep = mapper.getPositionForValue((float(sr) / m_fftSize) + 0.001);
Chris@187 3366 int maxStep = mapper.getPositionForValue(0);
Chris@187 3367 int minStep = mapper.getPositionForValue(float(sr) / 2);
Chris@250 3368
Chris@250 3369 size_t initialMax = m_initialMaxFrequency;
Chris@250 3370 if (initialMax == 0) initialMax = sr / 2;
Chris@250 3371
Chris@250 3372 defaultStep = mapper.getPositionForValue(initialMax) - minStep;
Chris@250 3373
Chris@250 3374 // std::cerr << "SpectrogramLayer::getVerticalZoomSteps: " << maxStep - minStep << " (" << maxStep <<"-" << minStep << "), default is " << defaultStep << " (from initial max freq " << initialMax << ")" << std::endl;
Chris@187 3375
Chris@187 3376 return maxStep - minStep;
Chris@133 3377 }
Chris@133 3378
Chris@133 3379 int
Chris@133 3380 SpectrogramLayer::getCurrentVerticalZoomStep() const
Chris@133 3381 {
Chris@133 3382 if (!m_model) return 0;
Chris@133 3383
Chris@133 3384 float dmin, dmax;
Chris@133 3385 getDisplayExtents(dmin, dmax);
Chris@133 3386
Chris@187 3387 SpectrogramRangeMapper mapper(m_model->getSampleRate(), m_fftSize);
Chris@187 3388 int n = mapper.getPositionForValue(dmax - dmin);
Chris@248 3389 // std::cerr << "SpectrogramLayer::getCurrentVerticalZoomStep: " << n << std::endl;
Chris@133 3390 return n;
Chris@133 3391 }
Chris@133 3392
Chris@133 3393 void
Chris@133 3394 SpectrogramLayer::setVerticalZoomStep(int step)
Chris@133 3395 {
Chris@187 3396 if (!m_model) return;
Chris@187 3397
Chris@253 3398 float dmin = m_minFrequency, dmax = m_maxFrequency;
Chris@253 3399 // getDisplayExtents(dmin, dmax);
Chris@253 3400
Chris@253 3401 // std::cerr << "current range " << dmin << " -> " << dmax << ", range " << dmax-dmin << ", mid " << (dmax + dmin)/2 << std::endl;
Chris@133 3402
Chris@133 3403 int sr = m_model->getSampleRate();
Chris@187 3404 SpectrogramRangeMapper mapper(sr, m_fftSize);
Chris@253 3405 float newdist = mapper.getValueForPosition(step);
Chris@253 3406
Chris@253 3407 float newmin, newmax;
Chris@253 3408
Chris@253 3409 if (m_frequencyScale == LogFrequencyScale) {
Chris@253 3410
Chris@253 3411 // need to pick newmin and newmax such that
Chris@253 3412 //
Chris@253 3413 // (log(newmin) + log(newmax)) / 2 == logmid
Chris@253 3414 // and
Chris@253 3415 // newmax - newmin = newdist
Chris@253 3416 //
Chris@253 3417 // so log(newmax - newdist) + log(newmax) == 2logmid
Chris@253 3418 // log(newmax(newmax - newdist)) == 2logmid
Chris@253 3419 // newmax.newmax - newmax.newdist == exp(2logmid)
Chris@253 3420 // newmax^2 + (-newdist)newmax + -exp(2logmid) == 0
Chris@253 3421 // quadratic with a = 1, b = -newdist, c = -exp(2logmid), all known
Chris@253 3422 //
Chris@253 3423 // positive root
Chris@253 3424 // newmax = (newdist + sqrt(newdist^2 + 4exp(2logmid))) / 2
Chris@253 3425 //
Chris@253 3426 // but logmid = (log(dmin) + log(dmax)) / 2
Chris@253 3427 // so exp(2logmid) = exp(log(dmin) + log(dmax))
Chris@253 3428 // = exp(log(dmin.dmax))
Chris@253 3429 // = dmin.dmax
Chris@253 3430 // so newmax = (newdist + sqrtf(newdist^2 + 4dmin.dmax)) / 2
Chris@253 3431
Chris@253 3432 newmax = (newdist + sqrtf(newdist*newdist + 4*dmin*dmax)) / 2;
Chris@253 3433 newmin = newmax - newdist;
Chris@253 3434
Chris@253 3435 // std::cerr << "newmin = " << newmin << ", newmax = " << newmax << std::endl;
Chris@253 3436
Chris@253 3437 } else {
Chris@253 3438 float dmid = (dmax + dmin) / 2;
Chris@253 3439 newmin = dmid - newdist / 2;
Chris@253 3440 newmax = dmid + newdist / 2;
Chris@253 3441 }
Chris@187 3442
Chris@187 3443 float mmin, mmax;
Chris@187 3444 mmin = 0;
Chris@187 3445 mmax = float(sr) / 2;
Chris@133 3446
Chris@187 3447 if (newmin < mmin) {
Chris@187 3448 newmax += (mmin - newmin);
Chris@187 3449 newmin = mmin;
Chris@187 3450 }
Chris@187 3451 if (newmax > mmax) {
Chris@187 3452 newmax = mmax;
Chris@187 3453 }
Chris@133 3454
Chris@253 3455 // std::cerr << "SpectrogramLayer::setVerticalZoomStep: " << step << ": " << newmin << " -> " << newmax << " (range " << newdist << ")" << std::endl;
Chris@253 3456
Chris@253 3457 setMinFrequency(lrintf(newmin));
Chris@253 3458 setMaxFrequency(lrintf(newmax));
Chris@187 3459 }
Chris@187 3460
Chris@187 3461 RangeMapper *
Chris@187 3462 SpectrogramLayer::getNewVerticalZoomRangeMapper() const
Chris@187 3463 {
Chris@187 3464 if (!m_model) return 0;
Chris@187 3465 return new SpectrogramRangeMapper(m_model->getSampleRate(), m_fftSize);
Chris@133 3466 }
Chris@133 3467
Chris@273 3468 void
Chris@273 3469 SpectrogramLayer::updateMeasureRectYCoords(View *v, const MeasureRect &r) const
Chris@273 3470 {
Chris@273 3471 int y0 = 0;
Chris@273 3472 if (r.startY > 0.0) y0 = getYForFrequency(v, r.startY);
Chris@273 3473
Chris@273 3474 int y1 = y0;
Chris@273 3475 if (r.endY > 0.0) y1 = getYForFrequency(v, r.endY);
Chris@273 3476
Chris@273 3477 // std::cerr << "SpectrogramLayer::updateMeasureRectYCoords: start " << r.startY << " -> " << y0 << ", end " << r.endY << " -> " << y1 << std::endl;
Chris@273 3478
Chris@273 3479 r.pixrect = QRect(r.pixrect.x(), y0, r.pixrect.width(), y1 - y0);
Chris@273 3480 }
Chris@273 3481
Chris@273 3482 void
Chris@273 3483 SpectrogramLayer::setMeasureRectYCoord(View *v, MeasureRect &r, bool start, int y) const
Chris@273 3484 {
Chris@273 3485 if (start) {
Chris@273 3486 r.startY = getFrequencyForY(v, y);
Chris@273 3487 r.endY = r.startY;
Chris@273 3488 } else {
Chris@273 3489 r.endY = getFrequencyForY(v, y);
Chris@273 3490 }
Chris@273 3491 // std::cerr << "SpectrogramLayer::setMeasureRectYCoord: start " << r.startY << " <- " << y << ", end " << r.endY << " <- " << y << std::endl;
Chris@273 3492
Chris@273 3493 }
Chris@273 3494
Chris@316 3495 void
Chris@316 3496 SpectrogramLayer::toXml(QTextStream &stream,
Chris@316 3497 QString indent, QString extraAttributes) const
Chris@6 3498 {
Chris@6 3499 QString s;
Chris@6 3500
Chris@6 3501 s += QString("channel=\"%1\" "
Chris@6 3502 "windowSize=\"%2\" "
Chris@153 3503 "windowHopLevel=\"%3\" "
Chris@153 3504 "gain=\"%4\" "
Chris@153 3505 "threshold=\"%5\" ")
Chris@6 3506 .arg(m_channel)
Chris@6 3507 .arg(m_windowSize)
Chris@97 3508 .arg(m_windowHopLevel)
Chris@37 3509 .arg(m_gain)
Chris@37 3510 .arg(m_threshold);
Chris@37 3511
Chris@37 3512 s += QString("minFrequency=\"%1\" "
Chris@37 3513 "maxFrequency=\"%2\" "
Chris@37 3514 "colourScale=\"%3\" "
Chris@37 3515 "colourScheme=\"%4\" "
Chris@37 3516 "colourRotation=\"%5\" "
Chris@37 3517 "frequencyScale=\"%6\" "
Chris@37 3518 "binDisplay=\"%7\" "
Chris@153 3519 "normalizeColumns=\"%8\" "
Chris@153 3520 "normalizeVisibleArea=\"%9\"")
Chris@37 3521 .arg(m_minFrequency)
Chris@6 3522 .arg(m_maxFrequency)
Chris@6 3523 .arg(m_colourScale)
Chris@197 3524 .arg(m_colourMap)
Chris@37 3525 .arg(m_colourRotation)
Chris@35 3526 .arg(m_frequencyScale)
Chris@37 3527 .arg(m_binDisplay)
Chris@153 3528 .arg(m_normalizeColumns ? "true" : "false")
Chris@153 3529 .arg(m_normalizeVisibleArea ? "true" : "false");
Chris@6 3530
Chris@316 3531 Layer::toXml(stream, indent, extraAttributes + " " + s);
Chris@6 3532 }
Chris@6 3533
Chris@11 3534 void
Chris@11 3535 SpectrogramLayer::setProperties(const QXmlAttributes &attributes)
Chris@11 3536 {
Chris@11 3537 bool ok = false;
Chris@11 3538
Chris@11 3539 int channel = attributes.value("channel").toInt(&ok);
Chris@11 3540 if (ok) setChannel(channel);
Chris@11 3541
Chris@11 3542 size_t windowSize = attributes.value("windowSize").toUInt(&ok);
Chris@11 3543 if (ok) setWindowSize(windowSize);
Chris@11 3544
Chris@97 3545 size_t windowHopLevel = attributes.value("windowHopLevel").toUInt(&ok);
Chris@97 3546 if (ok) setWindowHopLevel(windowHopLevel);
Chris@97 3547 else {
Chris@97 3548 size_t windowOverlap = attributes.value("windowOverlap").toUInt(&ok);
Chris@97 3549 // a percentage value
Chris@97 3550 if (ok) {
Chris@97 3551 if (windowOverlap == 0) setWindowHopLevel(0);
Chris@97 3552 else if (windowOverlap == 25) setWindowHopLevel(1);
Chris@97 3553 else if (windowOverlap == 50) setWindowHopLevel(2);
Chris@97 3554 else if (windowOverlap == 75) setWindowHopLevel(3);
Chris@97 3555 else if (windowOverlap == 90) setWindowHopLevel(4);
Chris@97 3556 }
Chris@97 3557 }
Chris@11 3558
Chris@11 3559 float gain = attributes.value("gain").toFloat(&ok);
Chris@11 3560 if (ok) setGain(gain);
Chris@11 3561
Chris@37 3562 float threshold = attributes.value("threshold").toFloat(&ok);
Chris@37 3563 if (ok) setThreshold(threshold);
Chris@37 3564
Chris@37 3565 size_t minFrequency = attributes.value("minFrequency").toUInt(&ok);
Chris@187 3566 if (ok) {
Chris@187 3567 std::cerr << "SpectrogramLayer::setProperties: setting min freq to " << minFrequency << std::endl;
Chris@187 3568 setMinFrequency(minFrequency);
Chris@187 3569 }
Chris@37 3570
Chris@11 3571 size_t maxFrequency = attributes.value("maxFrequency").toUInt(&ok);
Chris@187 3572 if (ok) {
Chris@187 3573 std::cerr << "SpectrogramLayer::setProperties: setting max freq to " << maxFrequency << std::endl;
Chris@187 3574 setMaxFrequency(maxFrequency);
Chris@187 3575 }
Chris@11 3576
Chris@11 3577 ColourScale colourScale = (ColourScale)
Chris@11 3578 attributes.value("colourScale").toInt(&ok);
Chris@11 3579 if (ok) setColourScale(colourScale);
Chris@11 3580
Chris@197 3581 int colourMap = attributes.value("colourScheme").toInt(&ok);
Chris@197 3582 if (ok) setColourMap(colourMap);
Chris@11 3583
Chris@37 3584 int colourRotation = attributes.value("colourRotation").toInt(&ok);
Chris@37 3585 if (ok) setColourRotation(colourRotation);
Chris@37 3586
Chris@11 3587 FrequencyScale frequencyScale = (FrequencyScale)
Chris@11 3588 attributes.value("frequencyScale").toInt(&ok);
Chris@11 3589 if (ok) setFrequencyScale(frequencyScale);
Chris@35 3590
Chris@37 3591 BinDisplay binDisplay = (BinDisplay)
Chris@37 3592 attributes.value("binDisplay").toInt(&ok);
Chris@37 3593 if (ok) setBinDisplay(binDisplay);
Chris@36 3594
Chris@36 3595 bool normalizeColumns =
Chris@36 3596 (attributes.value("normalizeColumns").trimmed() == "true");
Chris@36 3597 setNormalizeColumns(normalizeColumns);
Chris@153 3598
Chris@153 3599 bool normalizeVisibleArea =
Chris@153 3600 (attributes.value("normalizeVisibleArea").trimmed() == "true");
Chris@153 3601 setNormalizeVisibleArea(normalizeVisibleArea);
Chris@11 3602 }
Chris@11 3603