annotate layer/SpectrogramLayer.cpp @ 480:567b94e627b8

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