annotate layer/SpectrogramLayer.cpp @ 381:44670ce11a0c spectrogram-cache-rejig

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