annotate layer/SpectrogramLayer.cpp @ 374:64e84e5efb76 spectrogram-cache-rejig

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