annotate layer/SpectrogramLayer.cpp @ 484:a926fca1f280

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