annotate layer/SpectrogramLayer.cpp @ 213:df791d8c8f58

* Fix real-time plugin corruption when getLatency is called for some plugins * Other minor changes
author Chris Cannam
date Thu, 01 Mar 2007 15:35:27 +0000
parents 748985c7e2c1
children d2334a77db73
rev   line source
Chris@58 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@0 2
Chris@0 3 /*
Chris@59 4 Sonic Visualiser
Chris@59 5 An audio file viewer and annotation editor.
Chris@59 6 Centre for Digital Music, Queen Mary, University of London.
Chris@182 7 This file copyright 2006 Chris Cannam and QMUL.
Chris@0 8
Chris@59 9 This program is free software; you can redistribute it and/or
Chris@59 10 modify it under the terms of the GNU General Public License as
Chris@59 11 published by the Free Software Foundation; either version 2 of the
Chris@59 12 License, or (at your option) any later version. See the file
Chris@59 13 COPYING included with this distribution for more information.
Chris@0 14 */
Chris@0 15
Chris@0 16 #include "SpectrogramLayer.h"
Chris@0 17
Chris@128 18 #include "view/View.h"
Chris@0 19 #include "base/Profiler.h"
Chris@0 20 #include "base/AudioLevel.h"
Chris@0 21 #include "base/Window.h"
Chris@24 22 #include "base/Pitch.h"
Chris@118 23 #include "base/Preferences.h"
Chris@167 24 #include "base/RangeMapper.h"
Chris@196 25 #include "ColourMapper.h"
Chris@0 26
Chris@0 27 #include <QPainter>
Chris@0 28 #include <QImage>
Chris@0 29 #include <QPixmap>
Chris@0 30 #include <QRect>
Chris@0 31 #include <QTimer>
Chris@92 32 #include <QApplication>
Chris@178 33 #include <QMessageBox>
Chris@0 34
Chris@0 35 #include <iostream>
Chris@0 36
Chris@0 37 #include <cassert>
Chris@0 38 #include <cmath>
Chris@0 39
Chris@174 40 //#define DEBUG_SPECTROGRAM_REPAINT 1
Chris@0 41
Chris@44 42 SpectrogramLayer::SpectrogramLayer(Configuration config) :
Chris@0 43 m_model(0),
Chris@0 44 m_channel(0),
Chris@0 45 m_windowSize(1024),
Chris@0 46 m_windowType(HanningWindow),
Chris@97 47 m_windowHopLevel(2),
Chris@109 48 m_zeroPadLevel(0),
Chris@107 49 m_fftSize(1024),
Chris@0 50 m_gain(1.0),
Chris@37 51 m_threshold(0.0),
Chris@9 52 m_colourRotation(0),
Chris@119 53 m_minFrequency(10),
Chris@0 54 m_maxFrequency(8000),
Chris@135 55 m_initialMaxFrequency(8000),
Chris@0 56 m_colourScale(dBColourScale),
Chris@197 57 m_colourMap(0),
Chris@0 58 m_frequencyScale(LinearFrequencyScale),
Chris@37 59 m_binDisplay(AllBins),
Chris@36 60 m_normalizeColumns(false),
Chris@120 61 m_normalizeVisibleArea(false),
Chris@133 62 m_lastEmittedZoomStep(-1),
Chris@0 63 m_updateTimer(0),
Chris@44 64 m_candidateFillStartFrame(0),
Chris@193 65 m_exiting(false),
Chris@193 66 m_sliceableModel(0)
Chris@0 67 {
Chris@0 68 if (config == MelodicRange) {
Chris@0 69 setWindowSize(8192);
Chris@97 70 setWindowHopLevel(4);
Chris@109 71 // setWindowType(ParzenWindow);
Chris@135 72 m_initialMaxFrequency = 1000;
Chris@0 73 setMaxFrequency(1000);
Chris@0 74 setColourScale(LinearColourScale);
Chris@37 75 } else if (config == MelodicPeaks) {
Chris@37 76 setWindowSize(4096);
Chris@97 77 setWindowHopLevel(5);
Chris@109 78 // setWindowType(BlackmanWindow);
Chris@135 79 m_initialMaxFrequency = 2000;
Chris@40 80 setMaxFrequency(2000);
Chris@37 81 setMinFrequency(40);
Chris@37 82 setFrequencyScale(LogFrequencyScale);
Chris@41 83 setColourScale(MeterColourScale);
Chris@37 84 setBinDisplay(PeakFrequencies);
Chris@37 85 setNormalizeColumns(true);
Chris@0 86 }
Chris@110 87
Chris@122 88 Preferences *prefs = Preferences::getInstance();
Chris@122 89 connect(prefs, SIGNAL(propertyChanged(PropertyContainer::PropertyName)),
Chris@122 90 this, SLOT(preferenceChanged(PropertyContainer::PropertyName)));
Chris@122 91 setWindowType(prefs->getWindowType());
Chris@122 92
Chris@197 93 initialisePalette();
Chris@0 94 }
Chris@0 95
Chris@0 96 SpectrogramLayer::~SpectrogramLayer()
Chris@0 97 {
Chris@0 98 delete m_updateTimer;
Chris@0 99 m_updateTimer = 0;
Chris@0 100
Chris@130 101 invalidateFFTModels();
Chris@0 102 }
Chris@0 103
Chris@0 104 void
Chris@0 105 SpectrogramLayer::setModel(const DenseTimeValueModel *model)
Chris@0 106 {
Chris@101 107 // std::cerr << "SpectrogramLayer(" << this << "): setModel(" << model << ")" << std::endl;
Chris@34 108
Chris@110 109 if (model == m_model) return;
Chris@110 110
Chris@0 111 m_model = model;
Chris@130 112 invalidateFFTModels();
Chris@0 113
Chris@0 114 if (!m_model || !m_model->isOK()) return;
Chris@0 115
Chris@0 116 connect(m_model, SIGNAL(modelChanged()), this, SIGNAL(modelChanged()));
Chris@0 117 connect(m_model, SIGNAL(modelChanged(size_t, size_t)),
Chris@0 118 this, SIGNAL(modelChanged(size_t, size_t)));
Chris@0 119
Chris@0 120 connect(m_model, SIGNAL(completionChanged()),
Chris@0 121 this, SIGNAL(modelCompletionChanged()));
Chris@0 122
Chris@0 123 connect(m_model, SIGNAL(modelChanged()), this, SLOT(cacheInvalid()));
Chris@0 124 connect(m_model, SIGNAL(modelChanged(size_t, size_t)),
Chris@0 125 this, SLOT(cacheInvalid(size_t, size_t)));
Chris@0 126
Chris@0 127 emit modelReplaced();
Chris@110 128 }
Chris@115 129
Chris@0 130 Layer::PropertyList
Chris@0 131 SpectrogramLayer::getProperties() const
Chris@0 132 {
Chris@0 133 PropertyList list;
Chris@87 134 list.push_back("Colour");
Chris@87 135 list.push_back("Colour Scale");
Chris@87 136 list.push_back("Window Size");
Chris@97 137 list.push_back("Window Increment");
Chris@87 138 list.push_back("Normalize Columns");
Chris@120 139 list.push_back("Normalize Visible Area");
Chris@87 140 list.push_back("Bin Display");
Chris@87 141 list.push_back("Threshold");
Chris@87 142 list.push_back("Gain");
Chris@87 143 list.push_back("Colour Rotation");
Chris@153 144 // list.push_back("Min Frequency");
Chris@153 145 // list.push_back("Max Frequency");
Chris@87 146 list.push_back("Frequency Scale");
Chris@153 147 //// list.push_back("Zero Padding");
Chris@0 148 return list;
Chris@0 149 }
Chris@0 150
Chris@87 151 QString
Chris@87 152 SpectrogramLayer::getPropertyLabel(const PropertyName &name) const
Chris@87 153 {
Chris@87 154 if (name == "Colour") return tr("Colour");
Chris@87 155 if (name == "Colour Scale") return tr("Colour Scale");
Chris@87 156 if (name == "Window Size") return tr("Window Size");
Chris@112 157 if (name == "Window Increment") return tr("Window Overlap");
Chris@87 158 if (name == "Normalize Columns") return tr("Normalize Columns");
Chris@120 159 if (name == "Normalize Visible Area") return tr("Normalize Visible Area");
Chris@87 160 if (name == "Bin Display") return tr("Bin Display");
Chris@87 161 if (name == "Threshold") return tr("Threshold");
Chris@87 162 if (name == "Gain") return tr("Gain");
Chris@87 163 if (name == "Colour Rotation") return tr("Colour Rotation");
Chris@87 164 if (name == "Min Frequency") return tr("Min Frequency");
Chris@87 165 if (name == "Max Frequency") return tr("Max Frequency");
Chris@87 166 if (name == "Frequency Scale") return tr("Frequency Scale");
Chris@109 167 if (name == "Zero Padding") return tr("Smoothing");
Chris@87 168 return "";
Chris@87 169 }
Chris@87 170
Chris@0 171 Layer::PropertyType
Chris@0 172 SpectrogramLayer::getPropertyType(const PropertyName &name) const
Chris@0 173 {
Chris@87 174 if (name == "Gain") return RangeProperty;
Chris@87 175 if (name == "Colour Rotation") return RangeProperty;
Chris@87 176 if (name == "Normalize Columns") return ToggleProperty;
Chris@120 177 if (name == "Normalize Visible Area") return ToggleProperty;
Chris@87 178 if (name == "Threshold") return RangeProperty;
Chris@109 179 if (name == "Zero Padding") return ToggleProperty;
Chris@0 180 return ValueProperty;
Chris@0 181 }
Chris@0 182
Chris@0 183 QString
Chris@0 184 SpectrogramLayer::getPropertyGroupName(const PropertyName &name) const
Chris@0 185 {
Chris@153 186 if (name == "Bin Display" ||
Chris@153 187 name == "Frequency Scale") return tr("Bins");
Chris@87 188 if (name == "Window Size" ||
Chris@109 189 name == "Window Increment" ||
Chris@109 190 name == "Zero Padding") return tr("Window");
Chris@87 191 if (name == "Colour" ||
Chris@87 192 name == "Threshold" ||
Chris@87 193 name == "Colour Rotation") return tr("Colour");
Chris@87 194 if (name == "Normalize Columns" ||
Chris@120 195 name == "Normalize Visible Area" ||
Chris@153 196 name == "Gain" ||
Chris@87 197 name == "Colour Scale") return tr("Scale");
Chris@0 198 return QString();
Chris@0 199 }
Chris@0 200
Chris@0 201 int
Chris@0 202 SpectrogramLayer::getPropertyRangeAndValue(const PropertyName &name,
Chris@55 203 int *min, int *max) const
Chris@0 204 {
Chris@0 205 int deft = 0;
Chris@0 206
Chris@55 207 int garbage0, garbage1;
Chris@55 208 if (!min) min = &garbage0;
Chris@55 209 if (!max) max = &garbage1;
Chris@10 210
Chris@87 211 if (name == "Gain") {
Chris@0 212
Chris@0 213 *min = -50;
Chris@0 214 *max = 50;
Chris@0 215
Chris@0 216 deft = lrint(log10(m_gain) * 20.0);
Chris@0 217 if (deft < *min) deft = *min;
Chris@0 218 if (deft > *max) deft = *max;
Chris@0 219
Chris@87 220 } else if (name == "Threshold") {
Chris@37 221
Chris@37 222 *min = -50;
Chris@37 223 *max = 0;
Chris@37 224
Chris@37 225 deft = lrintf(AudioLevel::multiplier_to_dB(m_threshold));
Chris@37 226 if (deft < *min) deft = *min;
Chris@37 227 if (deft > *max) deft = *max;
Chris@37 228
Chris@87 229 } else if (name == "Colour Rotation") {
Chris@9 230
Chris@9 231 *min = 0;
Chris@9 232 *max = 256;
Chris@9 233
Chris@9 234 deft = m_colourRotation;
Chris@9 235
Chris@87 236 } else if (name == "Colour Scale") {
Chris@0 237
Chris@0 238 *min = 0;
Chris@176 239 *max = 4;
Chris@0 240
Chris@0 241 deft = (int)m_colourScale;
Chris@0 242
Chris@87 243 } else if (name == "Colour") {
Chris@0 244
Chris@0 245 *min = 0;
Chris@196 246 *max = ColourMapper::getColourMapCount() - 1;
Chris@196 247
Chris@197 248 deft = m_colourMap;
Chris@0 249
Chris@87 250 } else if (name == "Window Size") {
Chris@0 251
Chris@0 252 *min = 0;
Chris@0 253 *max = 10;
Chris@0 254
Chris@0 255 deft = 0;
Chris@0 256 int ws = m_windowSize;
Chris@0 257 while (ws > 32) { ws >>= 1; deft ++; }
Chris@0 258
Chris@97 259 } else if (name == "Window Increment") {
Chris@0 260
Chris@0 261 *min = 0;
Chris@97 262 *max = 5;
Chris@0 263
Chris@97 264 deft = m_windowHopLevel;
Chris@0 265
Chris@109 266 } else if (name == "Zero Padding") {
Chris@109 267
Chris@109 268 *min = 0;
Chris@109 269 *max = 1;
Chris@109 270
Chris@109 271 deft = m_zeroPadLevel > 0 ? 1 : 0;
Chris@109 272
Chris@87 273 } else if (name == "Min Frequency") {
Chris@37 274
Chris@37 275 *min = 0;
Chris@37 276 *max = 9;
Chris@37 277
Chris@37 278 switch (m_minFrequency) {
Chris@37 279 case 0: default: deft = 0; break;
Chris@37 280 case 10: deft = 1; break;
Chris@37 281 case 20: deft = 2; break;
Chris@37 282 case 40: deft = 3; break;
Chris@37 283 case 100: deft = 4; break;
Chris@37 284 case 250: deft = 5; break;
Chris@37 285 case 500: deft = 6; break;
Chris@37 286 case 1000: deft = 7; break;
Chris@37 287 case 4000: deft = 8; break;
Chris@37 288 case 10000: deft = 9; break;
Chris@37 289 }
Chris@37 290
Chris@87 291 } else if (name == "Max Frequency") {
Chris@0 292
Chris@0 293 *min = 0;
Chris@0 294 *max = 9;
Chris@0 295
Chris@0 296 switch (m_maxFrequency) {
Chris@0 297 case 500: deft = 0; break;
Chris@0 298 case 1000: deft = 1; break;
Chris@0 299 case 1500: deft = 2; break;
Chris@0 300 case 2000: deft = 3; break;
Chris@0 301 case 4000: deft = 4; break;
Chris@0 302 case 6000: deft = 5; break;
Chris@0 303 case 8000: deft = 6; break;
Chris@0 304 case 12000: deft = 7; break;
Chris@0 305 case 16000: deft = 8; break;
Chris@0 306 default: deft = 9; break;
Chris@0 307 }
Chris@0 308
Chris@87 309 } else if (name == "Frequency Scale") {
Chris@0 310
Chris@0 311 *min = 0;
Chris@0 312 *max = 1;
Chris@0 313 deft = (int)m_frequencyScale;
Chris@0 314
Chris@87 315 } else if (name == "Bin Display") {
Chris@35 316
Chris@35 317 *min = 0;
Chris@35 318 *max = 2;
Chris@37 319 deft = (int)m_binDisplay;
Chris@35 320
Chris@87 321 } else if (name == "Normalize Columns") {
Chris@36 322
Chris@36 323 deft = (m_normalizeColumns ? 1 : 0);
Chris@36 324
Chris@120 325 } else if (name == "Normalize Visible Area") {
Chris@120 326
Chris@120 327 deft = (m_normalizeVisibleArea ? 1 : 0);
Chris@120 328
Chris@0 329 } else {
Chris@0 330 deft = Layer::getPropertyRangeAndValue(name, min, max);
Chris@0 331 }
Chris@0 332
Chris@0 333 return deft;
Chris@0 334 }
Chris@0 335
Chris@0 336 QString
Chris@0 337 SpectrogramLayer::getPropertyValueLabel(const PropertyName &name,
Chris@9 338 int value) const
Chris@0 339 {
Chris@87 340 if (name == "Colour") {
Chris@196 341 return ColourMapper::getColourMapName(value);
Chris@0 342 }
Chris@87 343 if (name == "Colour Scale") {
Chris@0 344 switch (value) {
Chris@0 345 default:
Chris@37 346 case 0: return tr("Linear");
Chris@37 347 case 1: return tr("Meter");
Chris@37 348 case 2: return tr("dB");
Chris@176 349 case 3: return tr("dB^2");
Chris@119 350 case 4: return tr("Phase");
Chris@0 351 }
Chris@0 352 }
Chris@87 353 if (name == "Window Size") {
Chris@0 354 return QString("%1").arg(32 << value);
Chris@0 355 }
Chris@97 356 if (name == "Window Increment") {
Chris@0 357 switch (value) {
Chris@0 358 default:
Chris@112 359 case 0: return tr("None");
Chris@112 360 case 1: return tr("25 %");
Chris@112 361 case 2: return tr("50 %");
Chris@112 362 case 3: return tr("75 %");
Chris@112 363 case 4: return tr("87.5 %");
Chris@112 364 case 5: return tr("93.75 %");
Chris@0 365 }
Chris@0 366 }
Chris@109 367 if (name == "Zero Padding") {
Chris@109 368 if (value == 0) return tr("None");
Chris@109 369 return QString("%1x").arg(value + 1);
Chris@109 370 }
Chris@87 371 if (name == "Min Frequency") {
Chris@37 372 switch (value) {
Chris@37 373 default:
Chris@38 374 case 0: return tr("No min");
Chris@37 375 case 1: return tr("10 Hz");
Chris@37 376 case 2: return tr("20 Hz");
Chris@37 377 case 3: return tr("40 Hz");
Chris@37 378 case 4: return tr("100 Hz");
Chris@37 379 case 5: return tr("250 Hz");
Chris@37 380 case 6: return tr("500 Hz");
Chris@37 381 case 7: return tr("1 KHz");
Chris@37 382 case 8: return tr("4 KHz");
Chris@37 383 case 9: return tr("10 KHz");
Chris@37 384 }
Chris@37 385 }
Chris@87 386 if (name == "Max Frequency") {
Chris@0 387 switch (value) {
Chris@0 388 default:
Chris@0 389 case 0: return tr("500 Hz");
Chris@0 390 case 1: return tr("1 KHz");
Chris@0 391 case 2: return tr("1.5 KHz");
Chris@0 392 case 3: return tr("2 KHz");
Chris@0 393 case 4: return tr("4 KHz");
Chris@0 394 case 5: return tr("6 KHz");
Chris@0 395 case 6: return tr("8 KHz");
Chris@0 396 case 7: return tr("12 KHz");
Chris@0 397 case 8: return tr("16 KHz");
Chris@38 398 case 9: return tr("No max");
Chris@0 399 }
Chris@0 400 }
Chris@87 401 if (name == "Frequency Scale") {
Chris@0 402 switch (value) {
Chris@0 403 default:
Chris@0 404 case 0: return tr("Linear");
Chris@0 405 case 1: return tr("Log");
Chris@0 406 }
Chris@0 407 }
Chris@87 408 if (name == "Bin Display") {
Chris@35 409 switch (value) {
Chris@35 410 default:
Chris@37 411 case 0: return tr("All Bins");
Chris@37 412 case 1: return tr("Peak Bins");
Chris@37 413 case 2: return tr("Frequencies");
Chris@35 414 }
Chris@35 415 }
Chris@0 416 return tr("<unknown>");
Chris@0 417 }
Chris@0 418
Chris@167 419 RangeMapper *
Chris@167 420 SpectrogramLayer::getNewPropertyRangeMapper(const PropertyName &name) const
Chris@167 421 {
Chris@167 422 if (name == "Gain") {
Chris@167 423 return new LinearRangeMapper(-50, 50, -25, 25, tr("dB"));
Chris@167 424 }
Chris@167 425 if (name == "Threshold") {
Chris@167 426 return new LinearRangeMapper(-50, 0, -50, 0, tr("dB"));
Chris@167 427 }
Chris@167 428 return 0;
Chris@167 429 }
Chris@167 430
Chris@0 431 void
Chris@0 432 SpectrogramLayer::setProperty(const PropertyName &name, int value)
Chris@0 433 {
Chris@87 434 if (name == "Gain") {
Chris@0 435 setGain(pow(10, float(value)/20.0));
Chris@87 436 } else if (name == "Threshold") {
Chris@37 437 if (value == -50) setThreshold(0.0);
Chris@37 438 else setThreshold(AudioLevel::dB_to_multiplier(value));
Chris@87 439 } else if (name == "Colour Rotation") {
Chris@9 440 setColourRotation(value);
Chris@87 441 } else if (name == "Colour") {
Chris@197 442 setColourMap(value);
Chris@87 443 } else if (name == "Window Size") {
Chris@0 444 setWindowSize(32 << value);
Chris@97 445 } else if (name == "Window Increment") {
Chris@97 446 setWindowHopLevel(value);
Chris@109 447 } else if (name == "Zero Padding") {
Chris@109 448 setZeroPadLevel(value > 0.1 ? 3 : 0);
Chris@87 449 } else if (name == "Min Frequency") {
Chris@37 450 switch (value) {
Chris@37 451 default:
Chris@37 452 case 0: setMinFrequency(0); break;
Chris@37 453 case 1: setMinFrequency(10); break;
Chris@37 454 case 2: setMinFrequency(20); break;
Chris@37 455 case 3: setMinFrequency(40); break;
Chris@37 456 case 4: setMinFrequency(100); break;
Chris@37 457 case 5: setMinFrequency(250); break;
Chris@37 458 case 6: setMinFrequency(500); break;
Chris@37 459 case 7: setMinFrequency(1000); break;
Chris@37 460 case 8: setMinFrequency(4000); break;
Chris@37 461 case 9: setMinFrequency(10000); break;
Chris@37 462 }
Chris@133 463 int vs = getCurrentVerticalZoomStep();
Chris@133 464 if (vs != m_lastEmittedZoomStep) {
Chris@133 465 emit verticalZoomChanged();
Chris@133 466 m_lastEmittedZoomStep = vs;
Chris@133 467 }
Chris@87 468 } else if (name == "Max Frequency") {
Chris@0 469 switch (value) {
Chris@0 470 case 0: setMaxFrequency(500); break;
Chris@0 471 case 1: setMaxFrequency(1000); break;
Chris@0 472 case 2: setMaxFrequency(1500); break;
Chris@0 473 case 3: setMaxFrequency(2000); break;
Chris@0 474 case 4: setMaxFrequency(4000); break;
Chris@0 475 case 5: setMaxFrequency(6000); break;
Chris@0 476 case 6: setMaxFrequency(8000); break;
Chris@0 477 case 7: setMaxFrequency(12000); break;
Chris@0 478 case 8: setMaxFrequency(16000); break;
Chris@0 479 default:
Chris@0 480 case 9: setMaxFrequency(0); break;
Chris@0 481 }
Chris@133 482 int vs = getCurrentVerticalZoomStep();
Chris@133 483 if (vs != m_lastEmittedZoomStep) {
Chris@133 484 emit verticalZoomChanged();
Chris@133 485 m_lastEmittedZoomStep = vs;
Chris@133 486 }
Chris@87 487 } else if (name == "Colour Scale") {
Chris@0 488 switch (value) {
Chris@0 489 default:
Chris@0 490 case 0: setColourScale(LinearColourScale); break;
Chris@0 491 case 1: setColourScale(MeterColourScale); break;
Chris@0 492 case 2: setColourScale(dBColourScale); break;
Chris@210 493 case 3: setColourScale(dBSquaredColourScale); break;
Chris@119 494 case 4: setColourScale(PhaseColourScale); break;
Chris@0 495 }
Chris@87 496 } else if (name == "Frequency Scale") {
Chris@0 497 switch (value) {
Chris@0 498 default:
Chris@0 499 case 0: setFrequencyScale(LinearFrequencyScale); break;
Chris@0 500 case 1: setFrequencyScale(LogFrequencyScale); break;
Chris@0 501 }
Chris@87 502 } else if (name == "Bin Display") {
Chris@35 503 switch (value) {
Chris@35 504 default:
Chris@37 505 case 0: setBinDisplay(AllBins); break;
Chris@37 506 case 1: setBinDisplay(PeakBins); break;
Chris@37 507 case 2: setBinDisplay(PeakFrequencies); break;
Chris@35 508 }
Chris@82 509 } else if (name == "Normalize Columns") {
Chris@36 510 setNormalizeColumns(value ? true : false);
Chris@120 511 } else if (name == "Normalize Visible Area") {
Chris@120 512 setNormalizeVisibleArea(value ? true : false);
Chris@0 513 }
Chris@0 514 }
Chris@0 515
Chris@0 516 void
Chris@95 517 SpectrogramLayer::invalidatePixmapCaches()
Chris@95 518 {
Chris@95 519 for (ViewPixmapCache::iterator i = m_pixmapCaches.begin();
Chris@95 520 i != m_pixmapCaches.end(); ++i) {
Chris@95 521 i->second.validArea = QRect();
Chris@95 522 }
Chris@95 523 }
Chris@95 524
Chris@95 525 void
Chris@95 526 SpectrogramLayer::invalidatePixmapCaches(size_t startFrame, size_t endFrame)
Chris@95 527 {
Chris@95 528 for (ViewPixmapCache::iterator i = m_pixmapCaches.begin();
Chris@95 529 i != m_pixmapCaches.end(); ++i) {
Chris@131 530
Chris@95 531 //!!! when are views removed from the map? on setLayerDormant?
Chris@95 532 const View *v = i->first;
Chris@95 533
Chris@115 534 if (startFrame < v->getEndFrame() && int(endFrame) >= v->getStartFrame()) {
Chris@95 535 i->second.validArea = QRect();
Chris@95 536 }
Chris@95 537 }
Chris@95 538 }
Chris@95 539
Chris@95 540 void
Chris@122 541 SpectrogramLayer::preferenceChanged(PropertyContainer::PropertyName name)
Chris@122 542 {
Chris@122 543 std::cerr << "SpectrogramLayer::preferenceChanged(" << name.toStdString() << ")" << std::endl;
Chris@122 544
Chris@122 545 if (name == "Window Type") {
Chris@122 546 setWindowType(Preferences::getInstance()->getWindowType());
Chris@122 547 return;
Chris@122 548 }
Chris@122 549 if (name == "Smooth Spectrogram") {
Chris@122 550 invalidatePixmapCaches();
Chris@122 551 invalidateMagnitudes();
Chris@122 552 emit layerParametersChanged();
Chris@122 553 }
Chris@122 554 if (name == "Tuning Frequency") {
Chris@122 555 emit layerParametersChanged();
Chris@122 556 }
Chris@122 557 }
Chris@122 558
Chris@122 559 void
Chris@0 560 SpectrogramLayer::setChannel(int ch)
Chris@0 561 {
Chris@0 562 if (m_channel == ch) return;
Chris@0 563
Chris@95 564 invalidatePixmapCaches();
Chris@0 565 m_channel = ch;
Chris@130 566 invalidateFFTModels();
Chris@9 567
Chris@0 568 emit layerParametersChanged();
Chris@0 569 }
Chris@0 570
Chris@0 571 int
Chris@0 572 SpectrogramLayer::getChannel() const
Chris@0 573 {
Chris@0 574 return m_channel;
Chris@0 575 }
Chris@0 576
Chris@0 577 void
Chris@0 578 SpectrogramLayer::setWindowSize(size_t ws)
Chris@0 579 {
Chris@0 580 if (m_windowSize == ws) return;
Chris@0 581
Chris@95 582 invalidatePixmapCaches();
Chris@0 583
Chris@0 584 m_windowSize = ws;
Chris@109 585 m_fftSize = ws * (m_zeroPadLevel + 1);
Chris@0 586
Chris@130 587 invalidateFFTModels();
Chris@9 588
Chris@9 589 emit layerParametersChanged();
Chris@0 590 }
Chris@0 591
Chris@0 592 size_t
Chris@0 593 SpectrogramLayer::getWindowSize() const
Chris@0 594 {
Chris@0 595 return m_windowSize;
Chris@0 596 }
Chris@0 597
Chris@0 598 void
Chris@97 599 SpectrogramLayer::setWindowHopLevel(size_t v)
Chris@0 600 {
Chris@97 601 if (m_windowHopLevel == v) return;
Chris@0 602
Chris@95 603 invalidatePixmapCaches();
Chris@0 604
Chris@97 605 m_windowHopLevel = v;
Chris@0 606
Chris@130 607 invalidateFFTModels();
Chris@9 608
Chris@9 609 emit layerParametersChanged();
Chris@9 610
Chris@110 611 // fillCache();
Chris@0 612 }
Chris@0 613
Chris@0 614 size_t
Chris@97 615 SpectrogramLayer::getWindowHopLevel() const
Chris@0 616 {
Chris@97 617 return m_windowHopLevel;
Chris@0 618 }
Chris@0 619
Chris@0 620 void
Chris@109 621 SpectrogramLayer::setZeroPadLevel(size_t v)
Chris@109 622 {
Chris@109 623 if (m_zeroPadLevel == v) return;
Chris@109 624
Chris@109 625 invalidatePixmapCaches();
Chris@109 626
Chris@109 627 m_zeroPadLevel = v;
Chris@109 628 m_fftSize = m_windowSize * (v + 1);
Chris@110 629
Chris@130 630 invalidateFFTModels();
Chris@109 631
Chris@109 632 emit layerParametersChanged();
Chris@109 633 }
Chris@109 634
Chris@109 635 size_t
Chris@109 636 SpectrogramLayer::getZeroPadLevel() const
Chris@109 637 {
Chris@109 638 return m_zeroPadLevel;
Chris@109 639 }
Chris@109 640
Chris@109 641 void
Chris@0 642 SpectrogramLayer::setWindowType(WindowType w)
Chris@0 643 {
Chris@0 644 if (m_windowType == w) return;
Chris@0 645
Chris@95 646 invalidatePixmapCaches();
Chris@0 647
Chris@0 648 m_windowType = w;
Chris@110 649
Chris@130 650 invalidateFFTModels();
Chris@9 651
Chris@9 652 emit layerParametersChanged();
Chris@0 653 }
Chris@0 654
Chris@0 655 WindowType
Chris@0 656 SpectrogramLayer::getWindowType() const
Chris@0 657 {
Chris@0 658 return m_windowType;
Chris@0 659 }
Chris@0 660
Chris@0 661 void
Chris@0 662 SpectrogramLayer::setGain(float gain)
Chris@0 663 {
Chris@101 664 // std::cerr << "SpectrogramLayer::setGain(" << gain << ") (my gain is now "
Chris@101 665 // << m_gain << ")" << std::endl;
Chris@55 666
Chris@40 667 if (m_gain == gain) return;
Chris@0 668
Chris@95 669 invalidatePixmapCaches();
Chris@0 670
Chris@0 671 m_gain = gain;
Chris@0 672
Chris@9 673 emit layerParametersChanged();
Chris@0 674 }
Chris@0 675
Chris@0 676 float
Chris@0 677 SpectrogramLayer::getGain() const
Chris@0 678 {
Chris@0 679 return m_gain;
Chris@0 680 }
Chris@0 681
Chris@0 682 void
Chris@37 683 SpectrogramLayer::setThreshold(float threshold)
Chris@37 684 {
Chris@40 685 if (m_threshold == threshold) return;
Chris@37 686
Chris@95 687 invalidatePixmapCaches();
Chris@37 688
Chris@37 689 m_threshold = threshold;
Chris@37 690
Chris@37 691 emit layerParametersChanged();
Chris@37 692 }
Chris@37 693
Chris@37 694 float
Chris@37 695 SpectrogramLayer::getThreshold() const
Chris@37 696 {
Chris@37 697 return m_threshold;
Chris@37 698 }
Chris@37 699
Chris@37 700 void
Chris@37 701 SpectrogramLayer::setMinFrequency(size_t mf)
Chris@37 702 {
Chris@37 703 if (m_minFrequency == mf) return;
Chris@37 704
Chris@187 705 std::cerr << "SpectrogramLayer::setMinFrequency: " << mf << std::endl;
Chris@187 706
Chris@95 707 invalidatePixmapCaches();
Chris@119 708 invalidateMagnitudes();
Chris@37 709
Chris@37 710 m_minFrequency = mf;
Chris@37 711
Chris@37 712 emit layerParametersChanged();
Chris@37 713 }
Chris@37 714
Chris@37 715 size_t
Chris@37 716 SpectrogramLayer::getMinFrequency() const
Chris@37 717 {
Chris@37 718 return m_minFrequency;
Chris@37 719 }
Chris@37 720
Chris@37 721 void
Chris@0 722 SpectrogramLayer::setMaxFrequency(size_t mf)
Chris@0 723 {
Chris@0 724 if (m_maxFrequency == mf) return;
Chris@0 725
Chris@187 726 std::cerr << "SpectrogramLayer::setMaxFrequency: " << mf << std::endl;
Chris@187 727
Chris@95 728 invalidatePixmapCaches();
Chris@119 729 invalidateMagnitudes();
Chris@0 730
Chris@0 731 m_maxFrequency = mf;
Chris@0 732
Chris@9 733 emit layerParametersChanged();
Chris@0 734 }
Chris@0 735
Chris@0 736 size_t
Chris@0 737 SpectrogramLayer::getMaxFrequency() const
Chris@0 738 {
Chris@0 739 return m_maxFrequency;
Chris@0 740 }
Chris@0 741
Chris@0 742 void
Chris@9 743 SpectrogramLayer::setColourRotation(int r)
Chris@9 744 {
Chris@95 745 invalidatePixmapCaches();
Chris@9 746
Chris@9 747 if (r < 0) r = 0;
Chris@9 748 if (r > 256) r = 256;
Chris@9 749 int distance = r - m_colourRotation;
Chris@9 750
Chris@9 751 if (distance != 0) {
Chris@197 752 rotatePalette(-distance);
Chris@9 753 m_colourRotation = r;
Chris@9 754 }
Chris@9 755
Chris@9 756 emit layerParametersChanged();
Chris@9 757 }
Chris@9 758
Chris@9 759 void
Chris@0 760 SpectrogramLayer::setColourScale(ColourScale colourScale)
Chris@0 761 {
Chris@0 762 if (m_colourScale == colourScale) return;
Chris@0 763
Chris@95 764 invalidatePixmapCaches();
Chris@0 765
Chris@0 766 m_colourScale = colourScale;
Chris@0 767
Chris@9 768 emit layerParametersChanged();
Chris@0 769 }
Chris@0 770
Chris@0 771 SpectrogramLayer::ColourScale
Chris@0 772 SpectrogramLayer::getColourScale() const
Chris@0 773 {
Chris@0 774 return m_colourScale;
Chris@0 775 }
Chris@0 776
Chris@0 777 void
Chris@197 778 SpectrogramLayer::setColourMap(int map)
Chris@0 779 {
Chris@197 780 if (m_colourMap == map) return;
Chris@0 781
Chris@95 782 invalidatePixmapCaches();
Chris@0 783
Chris@197 784 m_colourMap = map;
Chris@197 785 initialisePalette();
Chris@9 786
Chris@0 787 emit layerParametersChanged();
Chris@0 788 }
Chris@0 789
Chris@196 790 int
Chris@197 791 SpectrogramLayer::getColourMap() const
Chris@0 792 {
Chris@197 793 return m_colourMap;
Chris@0 794 }
Chris@0 795
Chris@0 796 void
Chris@0 797 SpectrogramLayer::setFrequencyScale(FrequencyScale frequencyScale)
Chris@0 798 {
Chris@0 799 if (m_frequencyScale == frequencyScale) return;
Chris@0 800
Chris@95 801 invalidatePixmapCaches();
Chris@0 802 m_frequencyScale = frequencyScale;
Chris@9 803
Chris@9 804 emit layerParametersChanged();
Chris@0 805 }
Chris@0 806
Chris@0 807 SpectrogramLayer::FrequencyScale
Chris@0 808 SpectrogramLayer::getFrequencyScale() const
Chris@0 809 {
Chris@0 810 return m_frequencyScale;
Chris@0 811 }
Chris@0 812
Chris@0 813 void
Chris@37 814 SpectrogramLayer::setBinDisplay(BinDisplay binDisplay)
Chris@35 815 {
Chris@37 816 if (m_binDisplay == binDisplay) return;
Chris@35 817
Chris@95 818 invalidatePixmapCaches();
Chris@37 819 m_binDisplay = binDisplay;
Chris@35 820
Chris@35 821 emit layerParametersChanged();
Chris@35 822 }
Chris@35 823
Chris@37 824 SpectrogramLayer::BinDisplay
Chris@37 825 SpectrogramLayer::getBinDisplay() const
Chris@35 826 {
Chris@37 827 return m_binDisplay;
Chris@35 828 }
Chris@35 829
Chris@35 830 void
Chris@36 831 SpectrogramLayer::setNormalizeColumns(bool n)
Chris@36 832 {
Chris@36 833 if (m_normalizeColumns == n) return;
Chris@36 834
Chris@95 835 invalidatePixmapCaches();
Chris@119 836 invalidateMagnitudes();
Chris@36 837 m_normalizeColumns = n;
Chris@36 838
Chris@36 839 emit layerParametersChanged();
Chris@36 840 }
Chris@36 841
Chris@36 842 bool
Chris@36 843 SpectrogramLayer::getNormalizeColumns() const
Chris@36 844 {
Chris@36 845 return m_normalizeColumns;
Chris@36 846 }
Chris@36 847
Chris@36 848 void
Chris@120 849 SpectrogramLayer::setNormalizeVisibleArea(bool n)
Chris@120 850 {
Chris@120 851 if (m_normalizeVisibleArea == n) return;
Chris@120 852
Chris@120 853 invalidatePixmapCaches();
Chris@120 854 invalidateMagnitudes();
Chris@120 855 m_normalizeVisibleArea = n;
Chris@120 856
Chris@120 857 emit layerParametersChanged();
Chris@120 858 }
Chris@120 859
Chris@120 860 bool
Chris@120 861 SpectrogramLayer::getNormalizeVisibleArea() const
Chris@120 862 {
Chris@120 863 return m_normalizeVisibleArea;
Chris@120 864 }
Chris@120 865
Chris@120 866 void
Chris@47 867 SpectrogramLayer::setLayerDormant(const View *v, bool dormant)
Chris@29 868 {
Chris@33 869 if (dormant) {
Chris@33 870
Chris@131 871 if (isLayerDormant(v)) {
Chris@131 872 return;
Chris@131 873 }
Chris@131 874
Chris@131 875 Layer::setLayerDormant(v, true);
Chris@33 876
Chris@95 877 invalidatePixmapCaches();
Chris@95 878 m_pixmapCaches.erase(v);
Chris@114 879
Chris@130 880 if (m_fftModels.find(v) != m_fftModels.end()) {
Chris@193 881
Chris@193 882 if (m_sliceableModel == m_fftModels[v].first) {
Chris@193 883 bool replaced = false;
Chris@193 884 for (ViewFFTMap::iterator i = m_fftModels.begin();
Chris@193 885 i != m_fftModels.end(); ++i) {
Chris@193 886 if (i->second.first != m_sliceableModel) {
Chris@193 887 emit sliceableModelReplaced(m_sliceableModel, i->second.first);
Chris@193 888 replaced = true;
Chris@193 889 break;
Chris@193 890 }
Chris@193 891 }
Chris@193 892 if (!replaced) emit sliceableModelReplaced(m_sliceableModel, 0);
Chris@193 893 }
Chris@193 894
Chris@130 895 delete m_fftModels[v].first;
Chris@130 896 m_fftModels.erase(v);
Chris@114 897 }
Chris@33 898
Chris@33 899 } else {
Chris@33 900
Chris@131 901 Layer::setLayerDormant(v, false);
Chris@33 902 }
Chris@29 903 }
Chris@29 904
Chris@29 905 void
Chris@0 906 SpectrogramLayer::cacheInvalid()
Chris@0 907 {
Chris@95 908 invalidatePixmapCaches();
Chris@119 909 invalidateMagnitudes();
Chris@0 910 }
Chris@0 911
Chris@0 912 void
Chris@0 913 SpectrogramLayer::cacheInvalid(size_t, size_t)
Chris@0 914 {
Chris@0 915 // for now (or forever?)
Chris@0 916 cacheInvalid();
Chris@0 917 }
Chris@0 918
Chris@0 919 void
Chris@0 920 SpectrogramLayer::fillTimerTimedOut()
Chris@0 921 {
Chris@115 922 if (!m_model) return;
Chris@115 923
Chris@115 924 bool allDone = true;
Chris@115 925
Chris@184 926 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@184 927 std::cerr << "SpectrogramLayer::fillTimerTimedOut: have " << m_fftModels.size() << " FFT models associated with views" << std::endl;
Chris@184 928 #endif
Chris@184 929
Chris@130 930 for (ViewFFTMap::iterator i = m_fftModels.begin();
Chris@130 931 i != m_fftModels.end(); ++i) {
Chris@115 932
Chris@115 933 const View *v = i->first;
Chris@130 934 const FFTModel *model = i->second.first;
Chris@115 935 size_t lastFill = i->second.second;
Chris@115 936
Chris@130 937 if (model) {
Chris@130 938
Chris@130 939 size_t fill = model->getFillExtent();
Chris@115 940
Chris@0 941 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@130 942 std::cerr << "SpectrogramLayer::fillTimerTimedOut: extent for " << model << ": " << fill << ", last " << lastFill << ", total " << m_model->getEndFrame() << std::endl;
Chris@0 943 #endif
Chris@115 944
Chris@115 945 if (fill >= lastFill) {
Chris@115 946 if (fill >= m_model->getEndFrame() && lastFill > 0) {
Chris@0 947 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@115 948 std::cerr << "complete!" << std::endl;
Chris@0 949 #endif
Chris@115 950 invalidatePixmapCaches();
Chris@184 951 i->second.second = -1;
Chris@115 952 emit modelChanged();
Chris@115 953
Chris@115 954 } else if (fill > lastFill) {
Chris@0 955 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@115 956 std::cerr << "SpectrogramLayer: emitting modelChanged("
Chris@115 957 << lastFill << "," << fill << ")" << std::endl;
Chris@0 958 #endif
Chris@115 959 invalidatePixmapCaches(lastFill, fill);
Chris@184 960 i->second.second = fill;
Chris@115 961 emit modelChanged(lastFill, fill);
Chris@115 962 }
Chris@115 963 } else {
Chris@0 964 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@115 965 std::cerr << "SpectrogramLayer: going backwards, emitting modelChanged("
Chris@115 966 << m_model->getStartFrame() << "," << m_model->getEndFrame() << ")" << std::endl;
Chris@0 967 #endif
Chris@115 968 invalidatePixmapCaches();
Chris@184 969 i->second.second = fill;
Chris@115 970 emit modelChanged(m_model->getStartFrame(), m_model->getEndFrame());
Chris@115 971 }
Chris@115 972
Chris@115 973 if (i->second.second >= 0) {
Chris@115 974 allDone = false;
Chris@115 975 }
Chris@115 976 }
Chris@0 977 }
Chris@115 978
Chris@115 979 if (allDone) {
Chris@115 980 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@115 981 std::cerr << "SpectrogramLayer: all complete!" << std::endl;
Chris@115 982 #endif
Chris@115 983 delete m_updateTimer;
Chris@115 984 m_updateTimer = 0;
Chris@115 985 }
Chris@0 986 }
Chris@0 987
Chris@0 988 void
Chris@197 989 SpectrogramLayer::initialisePalette()
Chris@0 990 {
Chris@10 991 int formerRotation = m_colourRotation;
Chris@10 992
Chris@197 993 if (m_colourMap == (int)ColourMapper::BlackOnWhite) {
Chris@197 994 m_palette.setColour(NO_VALUE, Qt::white);
Chris@38 995 } else {
Chris@197 996 m_palette.setColour(NO_VALUE, Qt::black);
Chris@38 997 }
Chris@0 998
Chris@197 999 ColourMapper mapper(m_colourMap, 1.f, 255.f);
Chris@196 1000
Chris@0 1001 for (int pixel = 1; pixel < 256; ++pixel) {
Chris@197 1002 m_palette.setColour(pixel, mapper.map(pixel));
Chris@0 1003 }
Chris@9 1004
Chris@196 1005 m_crosshairColour = mapper.getContrastingColour();
Chris@196 1006
Chris@9 1007 m_colourRotation = 0;
Chris@197 1008 rotatePalette(m_colourRotation - formerRotation);
Chris@10 1009 m_colourRotation = formerRotation;
Chris@9 1010 }
Chris@9 1011
Chris@9 1012 void
Chris@197 1013 SpectrogramLayer::rotatePalette(int distance)
Chris@9 1014 {
Chris@31 1015 QColor newPixels[256];
Chris@9 1016
Chris@197 1017 newPixels[NO_VALUE] = m_palette.getColour(NO_VALUE);
Chris@9 1018
Chris@9 1019 for (int pixel = 1; pixel < 256; ++pixel) {
Chris@9 1020 int target = pixel + distance;
Chris@9 1021 while (target < 1) target += 255;
Chris@9 1022 while (target > 255) target -= 255;
Chris@197 1023 newPixels[target] = m_palette.getColour(pixel);
Chris@9 1024 }
Chris@9 1025
Chris@9 1026 for (int pixel = 0; pixel < 256; ++pixel) {
Chris@197 1027 m_palette.setColour(pixel, newPixels[pixel]);
Chris@9 1028 }
Chris@0 1029 }
Chris@0 1030
Chris@38 1031 float
Chris@38 1032 SpectrogramLayer::calculateFrequency(size_t bin,
Chris@38 1033 size_t windowSize,
Chris@38 1034 size_t windowIncrement,
Chris@38 1035 size_t sampleRate,
Chris@38 1036 float oldPhase,
Chris@38 1037 float newPhase,
Chris@38 1038 bool &steadyState)
Chris@38 1039 {
Chris@38 1040 // At frequency f, phase shift of 2pi (one cycle) happens in 1/f sec.
Chris@38 1041 // At hopsize h and sample rate sr, one hop happens in h/sr sec.
Chris@38 1042 // At window size w, for bin b, f is b*sr/w.
Chris@38 1043 // thus 2pi phase shift happens in w/(b*sr) sec.
Chris@38 1044 // We need to know what phase shift we expect from h/sr sec.
Chris@38 1045 // -> 2pi * ((h/sr) / (w/(b*sr)))
Chris@38 1046 // = 2pi * ((h * b * sr) / (w * sr))
Chris@38 1047 // = 2pi * (h * b) / w.
Chris@38 1048
Chris@38 1049 float frequency = (float(bin) * sampleRate) / windowSize;
Chris@38 1050
Chris@38 1051 float expectedPhase =
Chris@38 1052 oldPhase + (2.0 * M_PI * bin * windowIncrement) / windowSize;
Chris@38 1053
Chris@104 1054 float phaseError = princargf(newPhase - expectedPhase);
Chris@38 1055
Chris@142 1056 if (fabsf(phaseError) < (1.1f * (windowIncrement * M_PI) / windowSize)) {
Chris@38 1057
Chris@38 1058 // The new frequency estimate based on the phase error
Chris@38 1059 // resulting from assuming the "native" frequency of this bin
Chris@38 1060
Chris@38 1061 float newFrequency =
Chris@38 1062 (sampleRate * (expectedPhase + phaseError - oldPhase)) /
Chris@38 1063 (2 * M_PI * windowIncrement);
Chris@38 1064
Chris@38 1065 steadyState = true;
Chris@38 1066 return newFrequency;
Chris@38 1067 }
Chris@38 1068
Chris@38 1069 steadyState = false;
Chris@38 1070 return frequency;
Chris@38 1071 }
Chris@38 1072
Chris@38 1073 unsigned char
Chris@119 1074 SpectrogramLayer::getDisplayValue(View *v, float input) const
Chris@38 1075 {
Chris@38 1076 int value;
Chris@37 1077
Chris@120 1078 float min = 0.f;
Chris@120 1079 float max = 1.f;
Chris@120 1080
Chris@120 1081 if (m_normalizeVisibleArea) {
Chris@120 1082 min = m_viewMags[v].getMin();
Chris@120 1083 max = m_viewMags[v].getMax();
Chris@120 1084 } else if (!m_normalizeColumns) {
Chris@120 1085 if (m_colourScale == LinearColourScale ||
Chris@120 1086 m_colourScale == MeterColourScale) {
Chris@120 1087 max = 0.1f;
Chris@120 1088 }
Chris@120 1089 }
Chris@120 1090
Chris@119 1091 float thresh = -80.f;
Chris@119 1092
Chris@119 1093 if (max == 0.f) max = 1.f;
Chris@119 1094 if (max == min) min = max - 0.0001f;
Chris@119 1095
Chris@40 1096 switch (m_colourScale) {
Chris@40 1097
Chris@40 1098 default:
Chris@40 1099 case LinearColourScale:
Chris@119 1100 value = int(((input - min) / (max - min)) * 255.f) + 1;
Chris@40 1101 break;
Chris@40 1102
Chris@40 1103 case MeterColourScale:
Chris@210 1104 value = AudioLevel::multiplier_to_preview
Chris@210 1105 ((input - min) / (max - min), 254) + 1;
Chris@40 1106 break;
Chris@40 1107
Chris@40 1108 case dBColourScale:
Chris@119 1109 //!!! experiment with normalizing the visible area this way.
Chris@119 1110 //In any case, we need to have some indication of what the dB
Chris@119 1111 //scale is relative to.
Chris@133 1112 input = input / max;
Chris@133 1113 if (input > 0.f) {
Chris@133 1114 input = 10.f * log10f(input);
Chris@133 1115 } else {
Chris@133 1116 input = thresh;
Chris@133 1117 }
Chris@119 1118 if (min > 0.f) {
Chris@119 1119 thresh = 10.f * log10f(min);
Chris@119 1120 if (thresh < -80.f) thresh = -80.f;
Chris@119 1121 }
Chris@119 1122 input = (input - thresh) / (-thresh);
Chris@119 1123 if (input < 0.f) input = 0.f;
Chris@119 1124 if (input > 1.f) input = 1.f;
Chris@119 1125 value = int(input * 255.f) + 1;
Chris@119 1126 break;
Chris@119 1127
Chris@210 1128 case dBSquaredColourScale:
Chris@133 1129 input = (input * input) / (max * max);
Chris@133 1130 if (input > 0.f) {
Chris@133 1131 input = 10.f * log10f(input);
Chris@133 1132 } else {
Chris@133 1133 input = thresh;
Chris@133 1134 }
Chris@119 1135 if (min > 0.f) {
Chris@119 1136 thresh = 10.f * log10f(min * min);
Chris@119 1137 if (thresh < -80.f) thresh = -80.f;
Chris@119 1138 }
Chris@119 1139 input = (input - thresh) / (-thresh);
Chris@119 1140 if (input < 0.f) input = 0.f;
Chris@119 1141 if (input > 1.f) input = 1.f;
Chris@119 1142 value = int(input * 255.f) + 1;
Chris@119 1143 break;
Chris@40 1144
Chris@40 1145 case PhaseColourScale:
Chris@40 1146 value = int((input * 127.0 / M_PI) + 128);
Chris@40 1147 break;
Chris@0 1148 }
Chris@210 1149
Chris@38 1150 if (value > UCHAR_MAX) value = UCHAR_MAX;
Chris@38 1151 if (value < 0) value = 0;
Chris@38 1152 return value;
Chris@0 1153 }
Chris@0 1154
Chris@40 1155 float
Chris@40 1156 SpectrogramLayer::getInputForDisplayValue(unsigned char uc) const
Chris@40 1157 {
Chris@153 1158 //!!! unused
Chris@153 1159
Chris@40 1160 int value = uc;
Chris@40 1161 float input;
Chris@40 1162
Chris@120 1163 //!!! incorrect for normalizing visible area (and also out of date)
Chris@120 1164
Chris@40 1165 switch (m_colourScale) {
Chris@40 1166
Chris@40 1167 default:
Chris@40 1168 case LinearColourScale:
Chris@40 1169 input = float(value - 1) / 255.0 / (m_normalizeColumns ? 1 : 50);
Chris@40 1170 break;
Chris@40 1171
Chris@40 1172 case MeterColourScale:
Chris@40 1173 input = AudioLevel::preview_to_multiplier(value - 1, 255)
Chris@40 1174 / (m_normalizeColumns ? 1.0 : 50.0);
Chris@40 1175 break;
Chris@40 1176
Chris@40 1177 case dBColourScale:
Chris@40 1178 input = float(value - 1) / 255.0;
Chris@40 1179 input = (input * 80.0) - 80.0;
Chris@40 1180 input = powf(10.0, input) / 20.0;
Chris@40 1181 value = int(input);
Chris@40 1182 break;
Chris@40 1183
Chris@210 1184 case dBSquaredColourScale:
Chris@119 1185 input = float(value - 1) / 255.0;
Chris@119 1186 input = (input * 80.0) - 80.0;
Chris@119 1187 input = powf(10.0, input) / 20.0;
Chris@119 1188 value = int(input);
Chris@119 1189 break;
Chris@119 1190
Chris@40 1191 case PhaseColourScale:
Chris@40 1192 input = float(value - 128) * M_PI / 127.0;
Chris@40 1193 break;
Chris@40 1194 }
Chris@40 1195
Chris@40 1196 return input;
Chris@40 1197 }
Chris@40 1198
Chris@40 1199 float
Chris@40 1200 SpectrogramLayer::getEffectiveMinFrequency() const
Chris@40 1201 {
Chris@40 1202 int sr = m_model->getSampleRate();
Chris@107 1203 float minf = float(sr) / m_fftSize;
Chris@40 1204
Chris@40 1205 if (m_minFrequency > 0.0) {
Chris@107 1206 size_t minbin = size_t((double(m_minFrequency) * m_fftSize) / sr + 0.01);
Chris@40 1207 if (minbin < 1) minbin = 1;
Chris@107 1208 minf = minbin * sr / m_fftSize;
Chris@40 1209 }
Chris@40 1210
Chris@40 1211 return minf;
Chris@40 1212 }
Chris@40 1213
Chris@40 1214 float
Chris@40 1215 SpectrogramLayer::getEffectiveMaxFrequency() const
Chris@40 1216 {
Chris@40 1217 int sr = m_model->getSampleRate();
Chris@40 1218 float maxf = float(sr) / 2;
Chris@40 1219
Chris@40 1220 if (m_maxFrequency > 0.0) {
Chris@107 1221 size_t maxbin = size_t((double(m_maxFrequency) * m_fftSize) / sr + 0.1);
Chris@107 1222 if (maxbin > m_fftSize / 2) maxbin = m_fftSize / 2;
Chris@107 1223 maxf = maxbin * sr / m_fftSize;
Chris@40 1224 }
Chris@40 1225
Chris@40 1226 return maxf;
Chris@40 1227 }
Chris@40 1228
Chris@0 1229 bool
Chris@44 1230 SpectrogramLayer::getYBinRange(View *v, int y, float &q0, float &q1) const
Chris@0 1231 {
Chris@44 1232 int h = v->height();
Chris@0 1233 if (y < 0 || y >= h) return false;
Chris@0 1234
Chris@38 1235 int sr = m_model->getSampleRate();
Chris@40 1236 float minf = getEffectiveMinFrequency();
Chris@40 1237 float maxf = getEffectiveMaxFrequency();
Chris@0 1238
Chris@38 1239 bool logarithmic = (m_frequencyScale == LogFrequencyScale);
Chris@38 1240
Chris@130 1241 //!!! wrong for smoothing -- wrong fft size for fft model
Chris@114 1242
Chris@44 1243 q0 = v->getFrequencyForY(y, minf, maxf, logarithmic);
Chris@44 1244 q1 = v->getFrequencyForY(y - 1, minf, maxf, logarithmic);
Chris@38 1245
Chris@38 1246 // Now map these on to actual bins
Chris@38 1247
Chris@107 1248 int b0 = int((q0 * m_fftSize) / sr);
Chris@107 1249 int b1 = int((q1 * m_fftSize) / sr);
Chris@0 1250
Chris@40 1251 //!!! this is supposed to return fractions-of-bins, as it were, hence the floats
Chris@38 1252 q0 = b0;
Chris@38 1253 q1 = b1;
Chris@38 1254
Chris@107 1255 // q0 = (b0 * sr) / m_fftSize;
Chris@107 1256 // q1 = (b1 * sr) / m_fftSize;
Chris@0 1257
Chris@0 1258 return true;
Chris@0 1259 }
Chris@38 1260
Chris@0 1261 bool
Chris@44 1262 SpectrogramLayer::getXBinRange(View *v, int x, float &s0, float &s1) const
Chris@0 1263 {
Chris@21 1264 size_t modelStart = m_model->getStartFrame();
Chris@21 1265 size_t modelEnd = m_model->getEndFrame();
Chris@0 1266
Chris@0 1267 // Each pixel column covers an exact range of sample frames:
Chris@44 1268 int f0 = v->getFrameForX(x) - modelStart;
Chris@44 1269 int f1 = v->getFrameForX(x + 1) - modelStart - 1;
Chris@20 1270
Chris@41 1271 if (f1 < int(modelStart) || f0 > int(modelEnd)) {
Chris@41 1272 return false;
Chris@41 1273 }
Chris@20 1274
Chris@0 1275 // And that range may be drawn from a possibly non-integral
Chris@0 1276 // range of spectrogram windows:
Chris@0 1277
Chris@0 1278 size_t windowIncrement = getWindowIncrement();
Chris@0 1279 s0 = float(f0) / windowIncrement;
Chris@0 1280 s1 = float(f1) / windowIncrement;
Chris@0 1281
Chris@0 1282 return true;
Chris@0 1283 }
Chris@0 1284
Chris@0 1285 bool
Chris@44 1286 SpectrogramLayer::getXBinSourceRange(View *v, int x, RealTime &min, RealTime &max) const
Chris@0 1287 {
Chris@0 1288 float s0 = 0, s1 = 0;
Chris@44 1289 if (!getXBinRange(v, x, s0, s1)) return false;
Chris@0 1290
Chris@0 1291 int s0i = int(s0 + 0.001);
Chris@0 1292 int s1i = int(s1);
Chris@0 1293
Chris@0 1294 int windowIncrement = getWindowIncrement();
Chris@0 1295 int w0 = s0i * windowIncrement - (m_windowSize - windowIncrement)/2;
Chris@0 1296 int w1 = s1i * windowIncrement + windowIncrement +
Chris@0 1297 (m_windowSize - windowIncrement)/2 - 1;
Chris@0 1298
Chris@0 1299 min = RealTime::frame2RealTime(w0, m_model->getSampleRate());
Chris@0 1300 max = RealTime::frame2RealTime(w1, m_model->getSampleRate());
Chris@0 1301 return true;
Chris@0 1302 }
Chris@0 1303
Chris@0 1304 bool
Chris@44 1305 SpectrogramLayer::getYBinSourceRange(View *v, int y, float &freqMin, float &freqMax)
Chris@0 1306 const
Chris@0 1307 {
Chris@0 1308 float q0 = 0, q1 = 0;
Chris@44 1309 if (!getYBinRange(v, y, q0, q1)) return false;
Chris@0 1310
Chris@0 1311 int q0i = int(q0 + 0.001);
Chris@0 1312 int q1i = int(q1);
Chris@0 1313
Chris@0 1314 int sr = m_model->getSampleRate();
Chris@0 1315
Chris@0 1316 for (int q = q0i; q <= q1i; ++q) {
Chris@121 1317 if (q == q0i) freqMin = (sr * q) / m_fftSize;
Chris@121 1318 if (q == q1i) freqMax = (sr * (q+1)) / m_fftSize;
Chris@0 1319 }
Chris@0 1320 return true;
Chris@0 1321 }
Chris@35 1322
Chris@35 1323 bool
Chris@44 1324 SpectrogramLayer::getAdjustedYBinSourceRange(View *v, int x, int y,
Chris@35 1325 float &freqMin, float &freqMax,
Chris@35 1326 float &adjFreqMin, float &adjFreqMax)
Chris@35 1327 const
Chris@35 1328 {
Chris@130 1329 FFTModel *fft = getFFTModel(v);
Chris@114 1330 if (!fft) return false;
Chris@110 1331
Chris@35 1332 float s0 = 0, s1 = 0;
Chris@44 1333 if (!getXBinRange(v, x, s0, s1)) return false;
Chris@35 1334
Chris@35 1335 float q0 = 0, q1 = 0;
Chris@44 1336 if (!getYBinRange(v, y, q0, q1)) return false;
Chris@35 1337
Chris@35 1338 int s0i = int(s0 + 0.001);
Chris@35 1339 int s1i = int(s1);
Chris@35 1340
Chris@35 1341 int q0i = int(q0 + 0.001);
Chris@35 1342 int q1i = int(q1);
Chris@35 1343
Chris@35 1344 int sr = m_model->getSampleRate();
Chris@35 1345
Chris@38 1346 size_t windowSize = m_windowSize;
Chris@38 1347 size_t windowIncrement = getWindowIncrement();
Chris@38 1348
Chris@35 1349 bool haveAdj = false;
Chris@35 1350
Chris@37 1351 bool peaksOnly = (m_binDisplay == PeakBins ||
Chris@37 1352 m_binDisplay == PeakFrequencies);
Chris@37 1353
Chris@35 1354 for (int q = q0i; q <= q1i; ++q) {
Chris@35 1355
Chris@35 1356 for (int s = s0i; s <= s1i; ++s) {
Chris@35 1357
Chris@160 1358 if (!fft->isColumnAvailable(s)) continue;
Chris@117 1359
Chris@35 1360 float binfreq = (sr * q) / m_windowSize;
Chris@35 1361 if (q == q0i) freqMin = binfreq;
Chris@35 1362 if (q == q1i) freqMax = binfreq;
Chris@37 1363
Chris@114 1364 if (peaksOnly && !fft->isLocalPeak(s, q)) continue;
Chris@38 1365
Chris@114 1366 if (!fft->isOverThreshold(s, q, m_threshold)) continue;
Chris@38 1367
Chris@38 1368 float freq = binfreq;
Chris@38 1369 bool steady = false;
Chris@40 1370
Chris@114 1371 if (s < int(fft->getWidth()) - 1) {
Chris@38 1372
Chris@38 1373 freq = calculateFrequency(q,
Chris@38 1374 windowSize,
Chris@38 1375 windowIncrement,
Chris@38 1376 sr,
Chris@114 1377 fft->getPhaseAt(s, q),
Chris@114 1378 fft->getPhaseAt(s+1, q),
Chris@38 1379 steady);
Chris@35 1380
Chris@38 1381 if (!haveAdj || freq < adjFreqMin) adjFreqMin = freq;
Chris@38 1382 if (!haveAdj || freq > adjFreqMax) adjFreqMax = freq;
Chris@35 1383
Chris@35 1384 haveAdj = true;
Chris@35 1385 }
Chris@35 1386 }
Chris@35 1387 }
Chris@35 1388
Chris@35 1389 if (!haveAdj) {
Chris@40 1390 adjFreqMin = adjFreqMax = 0.0;
Chris@35 1391 }
Chris@35 1392
Chris@35 1393 return haveAdj;
Chris@35 1394 }
Chris@0 1395
Chris@0 1396 bool
Chris@44 1397 SpectrogramLayer::getXYBinSourceRange(View *v, int x, int y,
Chris@38 1398 float &min, float &max,
Chris@38 1399 float &phaseMin, float &phaseMax) const
Chris@0 1400 {
Chris@0 1401 float q0 = 0, q1 = 0;
Chris@44 1402 if (!getYBinRange(v, y, q0, q1)) return false;
Chris@0 1403
Chris@0 1404 float s0 = 0, s1 = 0;
Chris@44 1405 if (!getXBinRange(v, x, s0, s1)) return false;
Chris@0 1406
Chris@0 1407 int q0i = int(q0 + 0.001);
Chris@0 1408 int q1i = int(q1);
Chris@0 1409
Chris@0 1410 int s0i = int(s0 + 0.001);
Chris@0 1411 int s1i = int(s1);
Chris@0 1412
Chris@37 1413 bool rv = false;
Chris@37 1414
Chris@122 1415 size_t zp = getZeroPadLevel(v);
Chris@122 1416 q0i *= zp + 1;
Chris@122 1417 q1i *= zp + 1;
Chris@122 1418
Chris@130 1419 FFTModel *fft = getFFTModel(v);
Chris@0 1420
Chris@114 1421 if (fft) {
Chris@114 1422
Chris@114 1423 int cw = fft->getWidth();
Chris@114 1424 int ch = fft->getHeight();
Chris@0 1425
Chris@110 1426 min = 0.0;
Chris@110 1427 max = 0.0;
Chris@110 1428 phaseMin = 0.0;
Chris@110 1429 phaseMax = 0.0;
Chris@110 1430 bool have = false;
Chris@0 1431
Chris@110 1432 for (int q = q0i; q <= q1i; ++q) {
Chris@110 1433 for (int s = s0i; s <= s1i; ++s) {
Chris@110 1434 if (s >= 0 && q >= 0 && s < cw && q < ch) {
Chris@117 1435
Chris@160 1436 if (!fft->isColumnAvailable(s)) continue;
Chris@110 1437
Chris@110 1438 float value;
Chris@38 1439
Chris@114 1440 value = fft->getPhaseAt(s, q);
Chris@110 1441 if (!have || value < phaseMin) { phaseMin = value; }
Chris@110 1442 if (!have || value > phaseMax) { phaseMax = value; }
Chris@91 1443
Chris@114 1444 value = fft->getMagnitudeAt(s, q);
Chris@110 1445 if (!have || value < min) { min = value; }
Chris@110 1446 if (!have || value > max) { max = value; }
Chris@110 1447
Chris@110 1448 have = true;
Chris@110 1449 }
Chris@110 1450 }
Chris@110 1451 }
Chris@110 1452
Chris@110 1453 if (have) {
Chris@110 1454 rv = true;
Chris@110 1455 }
Chris@0 1456 }
Chris@0 1457
Chris@37 1458 return rv;
Chris@0 1459 }
Chris@0 1460
Chris@114 1461 size_t
Chris@114 1462 SpectrogramLayer::getZeroPadLevel(const View *v) const
Chris@114 1463 {
Chris@114 1464 //!!! tidy all this stuff
Chris@114 1465
Chris@114 1466 if (m_binDisplay != AllBins) return 0;
Chris@118 1467 if (!Preferences::getInstance()->getSmoothSpectrogram()) return 0;
Chris@114 1468 if (m_frequencyScale == LogFrequencyScale) return 3;
Chris@114 1469
Chris@114 1470 int sr = m_model->getSampleRate();
Chris@114 1471
Chris@184 1472 size_t maxbin = m_fftSize / 2;
Chris@114 1473 if (m_maxFrequency > 0) {
Chris@184 1474 maxbin = int((double(m_maxFrequency) * m_fftSize) / sr + 0.1);
Chris@184 1475 if (maxbin > m_fftSize / 2) maxbin = m_fftSize / 2;
Chris@114 1476 }
Chris@114 1477
Chris@114 1478 size_t minbin = 1;
Chris@114 1479 if (m_minFrequency > 0) {
Chris@114 1480 minbin = int((double(m_minFrequency) * m_fftSize) / sr + 0.1);
Chris@114 1481 if (minbin < 1) minbin = 1;
Chris@184 1482 if (minbin >= maxbin) minbin = maxbin - 1;
Chris@114 1483 }
Chris@114 1484
Chris@118 1485 float perPixel =
Chris@118 1486 float(v->height()) /
Chris@184 1487 float((maxbin - minbin) / (m_zeroPadLevel + 1));
Chris@118 1488
Chris@118 1489 if (perPixel > 2.8) {
Chris@118 1490 return 3; // 4x oversampling
Chris@118 1491 } else if (perPixel > 1.5) {
Chris@118 1492 return 1; // 2x
Chris@114 1493 } else {
Chris@118 1494 return 0; // 1x
Chris@114 1495 }
Chris@114 1496 }
Chris@114 1497
Chris@114 1498 size_t
Chris@114 1499 SpectrogramLayer::getFFTSize(const View *v) const
Chris@114 1500 {
Chris@114 1501 return m_fftSize * (getZeroPadLevel(v) + 1);
Chris@114 1502 }
Chris@114 1503
Chris@130 1504 FFTModel *
Chris@130 1505 SpectrogramLayer::getFFTModel(const View *v) const
Chris@114 1506 {
Chris@114 1507 if (!m_model) return 0;
Chris@114 1508
Chris@114 1509 size_t fftSize = getFFTSize(v);
Chris@114 1510
Chris@130 1511 if (m_fftModels.find(v) != m_fftModels.end()) {
Chris@184 1512 if (m_fftModels[v].first == 0) {
Chris@184 1513 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@184 1514 std::cerr << "SpectrogramLayer::getFFTModel(" << v << "): Found null model" << std::endl;
Chris@184 1515 #endif
Chris@184 1516 return 0;
Chris@184 1517 }
Chris@184 1518 if (m_fftModels[v].first->getHeight() != fftSize / 2 + 1) {
Chris@184 1519 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@184 1520 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 1521 #endif
Chris@130 1522 delete m_fftModels[v].first;
Chris@130 1523 m_fftModels.erase(v);
Chris@184 1524 } else {
Chris@184 1525 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@187 1526 std::cerr << "SpectrogramLayer::getFFTModel(" << v << "): Found a good model of height " << m_fftModels[v].first->getHeight() << std::endl;
Chris@184 1527 #endif
Chris@184 1528 return m_fftModels[v].first;
Chris@114 1529 }
Chris@114 1530 }
Chris@114 1531
Chris@130 1532 if (m_fftModels.find(v) == m_fftModels.end()) {
Chris@169 1533
Chris@169 1534 FFTModel *model = new FFTModel(m_model,
Chris@169 1535 m_channel,
Chris@169 1536 m_windowType,
Chris@169 1537 m_windowSize,
Chris@169 1538 getWindowIncrement(),
Chris@169 1539 fftSize,
Chris@169 1540 true,
Chris@169 1541 m_candidateFillStartFrame);
Chris@169 1542
Chris@178 1543 if (!model->isOK()) {
Chris@178 1544 QMessageBox::critical
Chris@178 1545 (0, tr("FFT cache failed"),
Chris@178 1546 tr("Failed to create the FFT model for this spectrogram.\n"
Chris@178 1547 "There may be insufficient memory or disc space to continue."));
Chris@178 1548 delete model;
Chris@178 1549 m_fftModels[v] = FFTFillPair(0, 0);
Chris@178 1550 return 0;
Chris@178 1551 }
Chris@178 1552
Chris@193 1553 if (!m_sliceableModel) {
Chris@193 1554 std::cerr << "SpectrogramLayer: emitting sliceableModelReplaced(0, " << model << ")" << std::endl;
Chris@193 1555 ((SpectrogramLayer *)this)->sliceableModelReplaced(0, model);
Chris@193 1556 m_sliceableModel = model;
Chris@193 1557 }
Chris@193 1558
Chris@169 1559 m_fftModels[v] = FFTFillPair(model, 0);
Chris@169 1560
Chris@169 1561 model->resume();
Chris@114 1562
Chris@114 1563 delete m_updateTimer;
Chris@114 1564 m_updateTimer = new QTimer((SpectrogramLayer *)this);
Chris@114 1565 connect(m_updateTimer, SIGNAL(timeout()),
Chris@114 1566 this, SLOT(fillTimerTimedOut()));
Chris@114 1567 m_updateTimer->start(200);
Chris@114 1568 }
Chris@114 1569
Chris@130 1570 return m_fftModels[v].first;
Chris@114 1571 }
Chris@114 1572
Chris@193 1573 const Model *
Chris@193 1574 SpectrogramLayer::getSliceableModel() const
Chris@193 1575 {
Chris@193 1576 if (m_sliceableModel) return m_sliceableModel;
Chris@193 1577 if (m_fftModels.empty()) return 0;
Chris@193 1578 m_sliceableModel = m_fftModels.begin()->second.first;
Chris@193 1579 return m_sliceableModel;
Chris@193 1580 }
Chris@193 1581
Chris@114 1582 void
Chris@130 1583 SpectrogramLayer::invalidateFFTModels()
Chris@114 1584 {
Chris@130 1585 for (ViewFFTMap::iterator i = m_fftModels.begin();
Chris@130 1586 i != m_fftModels.end(); ++i) {
Chris@115 1587 delete i->second.first;
Chris@114 1588 }
Chris@114 1589
Chris@130 1590 m_fftModels.clear();
Chris@193 1591
Chris@193 1592 if (m_sliceableModel) {
Chris@193 1593 std::cerr << "SpectrogramLayer: emitting sliceableModelReplaced(" << m_sliceableModel << ", 0)" << std::endl;
Chris@193 1594 emit sliceableModelReplaced(m_sliceableModel, 0);
Chris@193 1595 m_sliceableModel = 0;
Chris@193 1596 }
Chris@114 1597 }
Chris@114 1598
Chris@0 1599 void
Chris@119 1600 SpectrogramLayer::invalidateMagnitudes()
Chris@119 1601 {
Chris@119 1602 m_viewMags.clear();
Chris@119 1603 for (std::vector<MagnitudeRange>::iterator i = m_columnMags.begin();
Chris@119 1604 i != m_columnMags.end(); ++i) {
Chris@119 1605 *i = MagnitudeRange();
Chris@119 1606 }
Chris@119 1607 }
Chris@119 1608
Chris@119 1609 bool
Chris@119 1610 SpectrogramLayer::updateViewMagnitudes(View *v) const
Chris@119 1611 {
Chris@119 1612 MagnitudeRange mag;
Chris@119 1613
Chris@119 1614 int x0 = 0, x1 = v->width();
Chris@119 1615 float s00 = 0, s01 = 0, s10 = 0, s11 = 0;
Chris@119 1616
Chris@203 1617 if (!getXBinRange(v, x0, s00, s01)) {
Chris@203 1618 s00 = s01 = m_model->getStartFrame() / getWindowIncrement();
Chris@203 1619 }
Chris@203 1620
Chris@203 1621 if (!getXBinRange(v, x1, s10, s11)) {
Chris@203 1622 s10 = s11 = m_model->getEndFrame() / getWindowIncrement();
Chris@203 1623 }
Chris@119 1624
Chris@119 1625 int s0 = int(std::min(s00, s10) + 0.0001);
Chris@203 1626 int s1 = int(std::max(s01, s11) + 0.0001);
Chris@203 1627
Chris@203 1628 // std::cerr << "SpectrogramLayer::updateViewMagnitudes: x0 = " << x0 << ", x1 = " << x1 << ", s00 = " << s00 << ", s11 = " << s11 << " s0 = " << s0 << ", s1 = " << s1 << std::endl;
Chris@119 1629
Chris@119 1630 if (m_columnMags.size() <= s1) {
Chris@119 1631 m_columnMags.resize(s1 + 1);
Chris@119 1632 }
Chris@119 1633
Chris@119 1634 for (int s = s0; s <= s1; ++s) {
Chris@119 1635 if (m_columnMags[s].isSet()) {
Chris@119 1636 mag.sample(m_columnMags[s]);
Chris@119 1637 }
Chris@119 1638 }
Chris@119 1639
Chris@184 1640 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@119 1641 std::cerr << "SpectrogramLayer::updateViewMagnitudes returning from cols "
Chris@119 1642 << s0 << " -> " << s1 << " inclusive" << std::endl;
Chris@184 1643 #endif
Chris@119 1644
Chris@119 1645 if (!mag.isSet()) return false;
Chris@119 1646 if (mag == m_viewMags[v]) return false;
Chris@119 1647 m_viewMags[v] = mag;
Chris@119 1648 return true;
Chris@119 1649 }
Chris@119 1650
Chris@119 1651 void
Chris@44 1652 SpectrogramLayer::paint(View *v, QPainter &paint, QRect rect) const
Chris@0 1653 {
Chris@197 1654 if (m_colourMap == (int)ColourMapper::BlackOnWhite) {
Chris@55 1655 v->setLightBackground(true);
Chris@55 1656 } else {
Chris@55 1657 v->setLightBackground(false);
Chris@55 1658 }
Chris@55 1659
Chris@161 1660 Profiler profiler("SpectrogramLayer::paint", true);
Chris@0 1661 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@95 1662 std::cerr << "SpectrogramLayer::paint(): m_model is " << m_model << ", zoom level is " << v->getZoomLevel() << ", m_updateTimer " << m_updateTimer << std::endl;
Chris@95 1663
Chris@95 1664 std::cerr << "rect is " << rect.x() << "," << rect.y() << " " << rect.width() << "x" << rect.height() << std::endl;
Chris@0 1665 #endif
Chris@95 1666
Chris@133 1667 long startFrame = v->getStartFrame();
Chris@133 1668 if (startFrame < 0) m_candidateFillStartFrame = 0;
Chris@133 1669 else m_candidateFillStartFrame = startFrame;
Chris@44 1670
Chris@0 1671 if (!m_model || !m_model->isOK() || !m_model->isReady()) {
Chris@0 1672 return;
Chris@0 1673 }
Chris@0 1674
Chris@47 1675 if (isLayerDormant(v)) {
Chris@48 1676 std::cerr << "SpectrogramLayer::paint(): Layer is dormant, making it undormant again" << std::endl;
Chris@29 1677 }
Chris@29 1678
Chris@48 1679 // Need to do this even if !isLayerDormant, as that could mean v
Chris@48 1680 // is not in the dormancy map at all -- we need it to be present
Chris@48 1681 // and accountable for when determining whether we need the cache
Chris@48 1682 // in the cache-fill thread above.
Chris@131 1683 //!!! no longer use cache-fill thread
Chris@131 1684 const_cast<SpectrogramLayer *>(this)->Layer::setLayerDormant(v, false);
Chris@48 1685
Chris@114 1686 size_t fftSize = getFFTSize(v);
Chris@130 1687 FFTModel *fft = getFFTModel(v);
Chris@114 1688 if (!fft) {
Chris@130 1689 std::cerr << "ERROR: SpectrogramLayer::paint(): No FFT model, returning" << std::endl;
Chris@0 1690 return;
Chris@0 1691 }
Chris@0 1692
Chris@95 1693 PixmapCache &cache = m_pixmapCaches[v];
Chris@95 1694
Chris@95 1695 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@95 1696 std::cerr << "SpectrogramLayer::paint(): pixmap cache valid area " << cache.validArea.x() << ", " << cache.validArea.y() << ", " << cache.validArea.width() << "x" << cache.validArea.height() << std::endl;
Chris@95 1697 #endif
Chris@95 1698
Chris@0 1699 bool stillCacheing = (m_updateTimer != 0);
Chris@0 1700
Chris@0 1701 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@0 1702 std::cerr << "SpectrogramLayer::paint(): Still cacheing = " << stillCacheing << std::endl;
Chris@0 1703 #endif
Chris@0 1704
Chris@44 1705 int zoomLevel = v->getZoomLevel();
Chris@0 1706
Chris@0 1707 int x0 = 0;
Chris@44 1708 int x1 = v->width();
Chris@0 1709 int y0 = 0;
Chris@44 1710 int y1 = v->height();
Chris@0 1711
Chris@0 1712 bool recreateWholePixmapCache = true;
Chris@0 1713
Chris@95 1714 x0 = rect.left();
Chris@95 1715 x1 = rect.right() + 1;
Chris@95 1716 y0 = rect.top();
Chris@95 1717 y1 = rect.bottom() + 1;
Chris@95 1718
Chris@95 1719 if (cache.validArea.width() > 0) {
Chris@95 1720
Chris@95 1721 if (int(cache.zoomLevel) == zoomLevel &&
Chris@95 1722 cache.pixmap.width() == v->width() &&
Chris@95 1723 cache.pixmap.height() == v->height()) {
Chris@95 1724
Chris@95 1725 if (v->getXForFrame(cache.startFrame) ==
Chris@95 1726 v->getXForFrame(startFrame) &&
Chris@95 1727 cache.validArea.x() <= x0 &&
Chris@95 1728 cache.validArea.x() + cache.validArea.width() >= x1) {
Chris@0 1729
Chris@0 1730 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@0 1731 std::cerr << "SpectrogramLayer: pixmap cache good" << std::endl;
Chris@0 1732 #endif
Chris@0 1733
Chris@95 1734 paint.drawPixmap(rect, cache.pixmap, rect);
Chris@121 1735 illuminateLocalFeatures(v, paint);
Chris@0 1736 return;
Chris@0 1737
Chris@0 1738 } else {
Chris@0 1739
Chris@0 1740 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@0 1741 std::cerr << "SpectrogramLayer: pixmap cache partially OK" << std::endl;
Chris@0 1742 #endif
Chris@0 1743
Chris@0 1744 recreateWholePixmapCache = false;
Chris@0 1745
Chris@95 1746 int dx = v->getXForFrame(cache.startFrame) -
Chris@44 1747 v->getXForFrame(startFrame);
Chris@0 1748
Chris@0 1749 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@95 1750 std::cerr << "SpectrogramLayer: dx = " << dx << " (pixmap cache " << cache.pixmap.width() << "x" << cache.pixmap.height() << ")" << std::endl;
Chris@0 1751 #endif
Chris@0 1752
Chris@95 1753 if (dx != 0 &&
Chris@95 1754 dx > -cache.pixmap.width() &&
Chris@95 1755 dx < cache.pixmap.width()) {
Chris@0 1756
Chris@0 1757 #if defined(Q_WS_WIN32) || defined(Q_WS_MAC)
Chris@0 1758 // Copying a pixmap to itself doesn't work
Chris@0 1759 // properly on Windows or Mac (it only works when
Chris@0 1760 // moving in one direction).
Chris@0 1761
Chris@0 1762 //!!! Need a utility function for this
Chris@0 1763
Chris@0 1764 static QPixmap *tmpPixmap = 0;
Chris@0 1765 if (!tmpPixmap ||
Chris@95 1766 tmpPixmap->width() != cache.pixmap.width() ||
Chris@95 1767 tmpPixmap->height() != cache.pixmap.height()) {
Chris@0 1768 delete tmpPixmap;
Chris@95 1769 tmpPixmap = new QPixmap(cache.pixmap.width(),
Chris@95 1770 cache.pixmap.height());
Chris@0 1771 }
Chris@0 1772 QPainter cachePainter;
Chris@0 1773 cachePainter.begin(tmpPixmap);
Chris@95 1774 cachePainter.drawPixmap(0, 0, cache.pixmap);
Chris@0 1775 cachePainter.end();
Chris@95 1776 cachePainter.begin(&cache.pixmap);
Chris@0 1777 cachePainter.drawPixmap(dx, 0, *tmpPixmap);
Chris@0 1778 cachePainter.end();
Chris@0 1779 #else
Chris@95 1780 QPainter cachePainter(&cache.pixmap);
Chris@95 1781 cachePainter.drawPixmap(dx, 0, cache.pixmap);
Chris@0 1782 cachePainter.end();
Chris@0 1783 #endif
Chris@0 1784
Chris@95 1785 int px = cache.validArea.x();
Chris@95 1786 int pw = cache.validArea.width();
Chris@0 1787
Chris@0 1788 if (dx < 0) {
Chris@95 1789 x0 = cache.pixmap.width() + dx;
Chris@95 1790 x1 = cache.pixmap.width();
Chris@95 1791 px += dx;
Chris@95 1792 if (px < 0) {
Chris@95 1793 pw += px;
Chris@95 1794 px = 0;
Chris@95 1795 if (pw < 0) pw = 0;
Chris@95 1796 }
Chris@0 1797 } else {
Chris@0 1798 x0 = 0;
Chris@0 1799 x1 = dx;
Chris@95 1800 px += dx;
Chris@95 1801 if (px + pw > cache.pixmap.width()) {
Chris@95 1802 pw = int(cache.pixmap.width()) - px;
Chris@95 1803 if (pw < 0) pw = 0;
Chris@95 1804 }
Chris@0 1805 }
Chris@95 1806
Chris@95 1807 cache.validArea =
Chris@95 1808 QRect(px, cache.validArea.y(),
Chris@95 1809 pw, cache.validArea.height());
Chris@95 1810
Chris@95 1811 paint.drawPixmap(rect & cache.validArea,
Chris@95 1812 cache.pixmap,
Chris@95 1813 rect & cache.validArea);
Chris@0 1814 }
Chris@0 1815 }
Chris@0 1816 } else {
Chris@0 1817 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@0 1818 std::cerr << "SpectrogramLayer: pixmap cache useless" << std::endl;
Chris@0 1819 #endif
Chris@95 1820 cache.validArea = QRect();
Chris@0 1821 }
Chris@0 1822 }
Chris@95 1823
Chris@92 1824 /*
Chris@0 1825 if (stillCacheing) {
Chris@0 1826 x0 = rect.left();
Chris@0 1827 x1 = rect.right() + 1;
Chris@0 1828 y0 = rect.top();
Chris@0 1829 y1 = rect.bottom() + 1;
Chris@0 1830 }
Chris@92 1831 */
Chris@95 1832
Chris@133 1833 if (updateViewMagnitudes(v)) {
Chris@184 1834 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@133 1835 std::cerr << "SpectrogramLayer: magnitude range changed to [" << m_viewMags[v].getMin() << "->" << m_viewMags[v].getMax() << "]" << std::endl;
Chris@184 1836 #endif
Chris@133 1837 recreateWholePixmapCache = true;
Chris@133 1838 } else {
Chris@184 1839 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@133 1840 std::cerr << "No change in magnitude range [" << m_viewMags[v].getMin() << "->" << m_viewMags[v].getMax() << "]" << std::endl;
Chris@184 1841 #endif
Chris@133 1842 }
Chris@133 1843
Chris@95 1844 if (recreateWholePixmapCache) {
Chris@95 1845 x0 = 0;
Chris@95 1846 x1 = v->width();
Chris@95 1847 }
Chris@95 1848
Chris@188 1849 //!!! This width should really depend on how fast the machine is
Chris@188 1850 //at redrawing the spectrogram. We could fairly easily time that,
Chris@188 1851 //in this function, and adjust accordingly. The following is
Chris@188 1852 //probably about as small as the block width should go.
Chris@96 1853 int paintBlockWidth = (300000 / zoomLevel);
Chris@96 1854 if (paintBlockWidth < 20) paintBlockWidth = 20;
Chris@96 1855
Chris@96 1856 if (cache.validArea.width() > 0) {
Chris@96 1857
Chris@96 1858 int vx0 = 0, vx1 = 0;
Chris@96 1859 vx0 = cache.validArea.x();
Chris@96 1860 vx1 = cache.validArea.x() + cache.validArea.width();
Chris@96 1861
Chris@96 1862 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@96 1863 std::cerr << "x0 " << x0 << ", x1 " << x1 << ", vx0 " << vx0 << ", vx1 " << vx1 << ", paintBlockWidth " << paintBlockWidth << std::endl;
Chris@96 1864 #endif
Chris@96 1865 if (x0 < vx0) {
Chris@96 1866 if (x0 + paintBlockWidth < vx0) {
Chris@96 1867 x0 = vx0 - paintBlockWidth;
Chris@96 1868 } else {
Chris@96 1869 x0 = 0;
Chris@96 1870 }
Chris@96 1871 } else if (x0 > vx1) {
Chris@96 1872 x0 = vx1;
Chris@96 1873 }
Chris@95 1874
Chris@96 1875 if (x1 < vx0) {
Chris@96 1876 x1 = vx0;
Chris@96 1877 } else if (x1 > vx1) {
Chris@96 1878 if (vx1 + paintBlockWidth < x1) {
Chris@96 1879 x1 = vx1 + paintBlockWidth;
Chris@96 1880 } else {
Chris@96 1881 x1 = v->width();
Chris@95 1882 }
Chris@96 1883 }
Chris@95 1884
Chris@96 1885 cache.validArea = QRect
Chris@96 1886 (std::min(vx0, x0), cache.validArea.y(),
Chris@96 1887 std::max(vx1 - std::min(vx0, x0),
Chris@96 1888 x1 - std::min(vx0, x0)),
Chris@96 1889 cache.validArea.height());
Chris@95 1890
Chris@96 1891 } else {
Chris@96 1892 if (x1 > x0 + paintBlockWidth) {
Chris@133 1893 int sfx = x1;
Chris@133 1894 if (startFrame < 0) sfx = v->getXForFrame(0);
Chris@133 1895 if (sfx >= x0 && sfx + paintBlockWidth <= x1) {
Chris@133 1896 x0 = sfx;
Chris@133 1897 x1 = x0 + paintBlockWidth;
Chris@133 1898 } else {
Chris@133 1899 int mid = (x1 + x0) / 2;
Chris@133 1900 x0 = mid - paintBlockWidth/2;
Chris@133 1901 x1 = x0 + paintBlockWidth;
Chris@133 1902 }
Chris@95 1903 }
Chris@96 1904 cache.validArea = QRect(x0, 0, x1 - x0, v->height());
Chris@95 1905 }
Chris@95 1906
Chris@0 1907 int w = x1 - x0;
Chris@0 1908 int h = y1 - y0;
Chris@0 1909
Chris@95 1910 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@95 1911 std::cerr << "x0 " << x0 << ", x1 " << x1 << ", w " << w << ", h " << h << std::endl;
Chris@95 1912 #endif
Chris@95 1913
Chris@95 1914 if (m_drawBuffer.width() < w || m_drawBuffer.height() < h) {
Chris@95 1915 m_drawBuffer = QImage(w, h, QImage::Format_RGB32);
Chris@95 1916 }
Chris@95 1917
Chris@197 1918 m_drawBuffer.fill(m_palette.getColour(0).rgb());
Chris@35 1919
Chris@37 1920 int sr = m_model->getSampleRate();
Chris@122 1921
Chris@122 1922 // Set minFreq and maxFreq to the frequency extents of the possibly
Chris@122 1923 // zero-padded visible bin range, and displayMinFreq and displayMaxFreq
Chris@122 1924 // to the actual scale frequency extents (presumably not zero padded).
Chris@35 1925
Chris@184 1926 size_t maxbin = fftSize / 2;
Chris@35 1927 if (m_maxFrequency > 0) {
Chris@184 1928 maxbin = int((double(m_maxFrequency) * fftSize) / sr + 0.1);
Chris@184 1929 if (maxbin > fftSize / 2) maxbin = fftSize / 2;
Chris@35 1930 }
Chris@111 1931
Chris@40 1932 size_t minbin = 1;
Chris@37 1933 if (m_minFrequency > 0) {
Chris@114 1934 minbin = int((double(m_minFrequency) * fftSize) / sr + 0.1);
Chris@40 1935 if (minbin < 1) minbin = 1;
Chris@184 1936 if (minbin >= maxbin) minbin = maxbin - 1;
Chris@37 1937 }
Chris@37 1938
Chris@114 1939 float minFreq = (float(minbin) * sr) / fftSize;
Chris@184 1940 float maxFreq = (float(maxbin) * sr) / fftSize;
Chris@0 1941
Chris@122 1942 float displayMinFreq = minFreq;
Chris@122 1943 float displayMaxFreq = maxFreq;
Chris@122 1944
Chris@122 1945 if (fftSize != m_fftSize) {
Chris@122 1946 displayMinFreq = getEffectiveMinFrequency();
Chris@122 1947 displayMaxFreq = getEffectiveMaxFrequency();
Chris@122 1948 }
Chris@122 1949
Chris@92 1950 float ymag[h];
Chris@92 1951 float ydiv[h];
Chris@184 1952 float yval[maxbin + 1]; //!!! cache this?
Chris@92 1953
Chris@38 1954 size_t increment = getWindowIncrement();
Chris@40 1955
Chris@40 1956 bool logarithmic = (m_frequencyScale == LogFrequencyScale);
Chris@38 1957
Chris@184 1958 for (size_t q = minbin; q <= maxbin; ++q) {
Chris@114 1959 float f0 = (float(q) * sr) / fftSize;
Chris@122 1960 yval[q] = v->getYForFrequency(f0, displayMinFreq, displayMaxFreq,
Chris@122 1961 logarithmic);
Chris@122 1962 // std::cerr << "min: " << minFreq << ", max: " << maxFreq << ", yval[" << q << "]: " << yval[q] << std::endl;
Chris@92 1963 }
Chris@92 1964
Chris@119 1965 MagnitudeRange overallMag = m_viewMags[v];
Chris@119 1966 bool overallMagChanged = false;
Chris@119 1967
Chris@162 1968 bool fftSuspended = false;
Chris@131 1969
Chris@137 1970 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@137 1971 std::cerr << (float(v->getFrameForX(1) - v->getFrameForX(0)) / increment) << " bins per pixel" << std::endl;
Chris@137 1972 #endif
Chris@137 1973
Chris@35 1974 for (int x = 0; x < w; ++x) {
Chris@35 1975
Chris@35 1976 for (int y = 0; y < h; ++y) {
Chris@134 1977 ymag[y] = 0.f;
Chris@134 1978 ydiv[y] = 0.f;
Chris@35 1979 }
Chris@35 1980
Chris@35 1981 float s0 = 0, s1 = 0;
Chris@35 1982
Chris@44 1983 if (!getXBinRange(v, x0 + x, s0, s1)) {
Chris@95 1984 assert(x <= m_drawBuffer.width());
Chris@35 1985 continue;
Chris@35 1986 }
Chris@35 1987
Chris@35 1988 int s0i = int(s0 + 0.001);
Chris@35 1989 int s1i = int(s1);
Chris@35 1990
Chris@114 1991 if (s1i >= fft->getWidth()) {
Chris@114 1992 if (s0i >= fft->getWidth()) {
Chris@45 1993 continue;
Chris@45 1994 } else {
Chris@45 1995 s1i = s0i;
Chris@45 1996 }
Chris@45 1997 }
Chris@92 1998
Chris@92 1999 for (int s = s0i; s <= s1i; ++s) {
Chris@92 2000
Chris@160 2001 if (!fft->isColumnAvailable(s)) continue;
Chris@162 2002
Chris@162 2003 if (!fftSuspended) {
Chris@162 2004 fft->suspendWrites();
Chris@162 2005 fftSuspended = true;
Chris@162 2006 }
Chris@162 2007
Chris@119 2008 MagnitudeRange mag;
Chris@92 2009
Chris@184 2010 for (size_t q = minbin; q < maxbin; ++q) {
Chris@92 2011
Chris@92 2012 float y0 = yval[q + 1];
Chris@92 2013 float y1 = yval[q];
Chris@92 2014
Chris@40 2015 if (m_binDisplay == PeakBins ||
Chris@40 2016 m_binDisplay == PeakFrequencies) {
Chris@114 2017 if (!fft->isLocalPeak(s, q)) continue;
Chris@40 2018 }
Chris@114 2019
Chris@114 2020 if (m_threshold != 0.f &&
Chris@114 2021 !fft->isOverThreshold(s, q, m_threshold)) {
Chris@114 2022 continue;
Chris@114 2023 }
Chris@40 2024
Chris@35 2025 float sprop = 1.0;
Chris@35 2026 if (s == s0i) sprop *= (s + 1) - s0;
Chris@35 2027 if (s == s1i) sprop *= s1 - s;
Chris@35 2028
Chris@38 2029 if (m_binDisplay == PeakFrequencies &&
Chris@114 2030 s < int(fft->getWidth()) - 1) {
Chris@35 2031
Chris@38 2032 bool steady = false;
Chris@92 2033 float f = calculateFrequency(q,
Chris@38 2034 m_windowSize,
Chris@38 2035 increment,
Chris@38 2036 sr,
Chris@114 2037 fft->getPhaseAt(s, q),
Chris@114 2038 fft->getPhaseAt(s+1, q),
Chris@38 2039 steady);
Chris@40 2040
Chris@44 2041 y0 = y1 = v->getYForFrequency
Chris@122 2042 (f, displayMinFreq, displayMaxFreq, logarithmic);
Chris@35 2043 }
Chris@38 2044
Chris@35 2045 int y0i = int(y0 + 0.001);
Chris@35 2046 int y1i = int(y1);
Chris@35 2047
Chris@92 2048 float value;
Chris@92 2049
Chris@92 2050 if (m_colourScale == PhaseColourScale) {
Chris@114 2051 value = fft->getPhaseAt(s, q);
Chris@92 2052 } else if (m_normalizeColumns) {
Chris@119 2053 value = fft->getNormalizedMagnitudeAt(s, q);
Chris@119 2054 mag.sample(value);
Chris@119 2055 value *= m_gain;
Chris@92 2056 } else {
Chris@119 2057 value = fft->getMagnitudeAt(s, q);
Chris@119 2058 mag.sample(value);
Chris@119 2059 value *= m_gain;
Chris@92 2060 }
Chris@92 2061
Chris@35 2062 for (int y = y0i; y <= y1i; ++y) {
Chris@35 2063
Chris@35 2064 if (y < 0 || y >= h) continue;
Chris@35 2065
Chris@35 2066 float yprop = sprop;
Chris@35 2067 if (y == y0i) yprop *= (y + 1) - y0;
Chris@35 2068 if (y == y1i) yprop *= y1 - y;
Chris@37 2069 ymag[y] += yprop * value;
Chris@35 2070 ydiv[y] += yprop;
Chris@35 2071 }
Chris@35 2072 }
Chris@119 2073
Chris@119 2074 if (mag.isSet()) {
Chris@119 2075
Chris@203 2076
Chris@203 2077 if (s >= m_columnMags.size()) {
Chris@203 2078 std::cerr << "INTERNAL ERROR: " << s << " >= "
Chris@203 2079 << m_columnMags.size() << " at SpectrogramLayer.cpp:2087" << std::endl;
Chris@203 2080 }
Chris@203 2081
Chris@119 2082 m_columnMags[s].sample(mag);
Chris@119 2083
Chris@119 2084 if (overallMag.sample(mag)) {
Chris@119 2085 //!!! scaling would change here
Chris@119 2086 overallMagChanged = true;
Chris@209 2087 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@119 2088 std::cerr << "Overall mag changed (again?) at column " << s << ", to [" << overallMag.getMin() << "->" << overallMag.getMax() << "]" << std::endl;
Chris@209 2089 #endif
Chris@119 2090 }
Chris@119 2091 }
Chris@35 2092 }
Chris@35 2093
Chris@35 2094 for (int y = 0; y < h; ++y) {
Chris@35 2095
Chris@35 2096 if (ydiv[y] > 0.0) {
Chris@40 2097
Chris@40 2098 unsigned char pixel = 0;
Chris@40 2099
Chris@38 2100 float avg = ymag[y] / ydiv[y];
Chris@138 2101 pixel = getDisplayValue(v, avg);
Chris@40 2102
Chris@95 2103 assert(x <= m_drawBuffer.width());
Chris@197 2104 QColor c = m_palette.getColour(pixel);
Chris@95 2105 m_drawBuffer.setPixel(x, y,
Chris@95 2106 qRgb(c.red(), c.green(), c.blue()));
Chris@35 2107 }
Chris@35 2108 }
Chris@35 2109 }
Chris@35 2110
Chris@119 2111 if (overallMagChanged) {
Chris@119 2112 m_viewMags[v] = overallMag;
Chris@209 2113 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@119 2114 std::cerr << "Overall mag is now [" << m_viewMags[v].getMin() << "->" << m_viewMags[v].getMax() << "] - will be updating" << std::endl;
Chris@209 2115 #endif
Chris@119 2116 } else {
Chris@209 2117 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@119 2118 std::cerr << "Overall mag unchanged at [" << m_viewMags[v].getMin() << "->" << m_viewMags[v].getMax() << "]" << std::endl;
Chris@209 2119 #endif
Chris@119 2120 }
Chris@119 2121
Chris@161 2122 Profiler profiler2("SpectrogramLayer::paint: draw image", true);
Chris@137 2123
Chris@95 2124 paint.drawImage(x0, y0, m_drawBuffer, 0, 0, w, h);
Chris@0 2125
Chris@0 2126 if (recreateWholePixmapCache) {
Chris@95 2127 cache.pixmap = QPixmap(v->width(), v->height());
Chris@0 2128 }
Chris@0 2129
Chris@95 2130 QPainter cachePainter(&cache.pixmap);
Chris@95 2131 cachePainter.drawImage(x0, y0, m_drawBuffer, 0, 0, w, h);
Chris@0 2132 cachePainter.end();
Chris@119 2133
Chris@120 2134 if (!m_normalizeVisibleArea || !overallMagChanged) {
Chris@0 2135
Chris@119 2136 cache.startFrame = startFrame;
Chris@119 2137 cache.zoomLevel = zoomLevel;
Chris@119 2138
Chris@119 2139 if (cache.validArea.x() > 0) {
Chris@95 2140 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@158 2141 std::cerr << "SpectrogramLayer::paint() updating left (0, "
Chris@158 2142 << cache.validArea.x() << ")" << std::endl;
Chris@95 2143 #endif
Chris@119 2144 v->update(0, 0, cache.validArea.x(), v->height());
Chris@119 2145 }
Chris@119 2146
Chris@119 2147 if (cache.validArea.x() + cache.validArea.width() <
Chris@119 2148 cache.pixmap.width()) {
Chris@95 2149 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@119 2150 std::cerr << "SpectrogramLayer::paint() updating right ("
Chris@119 2151 << cache.validArea.x() + cache.validArea.width()
Chris@119 2152 << ", "
Chris@119 2153 << cache.pixmap.width() - (cache.validArea.x() +
Chris@119 2154 cache.validArea.width())
Chris@119 2155 << ")" << std::endl;
Chris@95 2156 #endif
Chris@119 2157 v->update(cache.validArea.x() + cache.validArea.width(),
Chris@119 2158 0,
Chris@119 2159 cache.pixmap.width() - (cache.validArea.x() +
Chris@119 2160 cache.validArea.width()),
Chris@119 2161 v->height());
Chris@119 2162 }
Chris@119 2163 } else {
Chris@119 2164 // overallMagChanged
Chris@119 2165 cache.validArea = QRect();
Chris@119 2166 v->update();
Chris@95 2167 }
Chris@0 2168
Chris@121 2169 illuminateLocalFeatures(v, paint);
Chris@120 2170
Chris@0 2171 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@0 2172 std::cerr << "SpectrogramLayer::paint() returning" << std::endl;
Chris@0 2173 #endif
Chris@131 2174
Chris@162 2175 if (fftSuspended) fft->resume();
Chris@0 2176 }
Chris@0 2177
Chris@121 2178 void
Chris@121 2179 SpectrogramLayer::illuminateLocalFeatures(View *v, QPainter &paint) const
Chris@121 2180 {
Chris@121 2181 QPoint localPos;
Chris@121 2182 if (!v->shouldIlluminateLocalFeatures(this, localPos) || !m_model) {
Chris@121 2183 return;
Chris@121 2184 }
Chris@121 2185
Chris@180 2186 // std::cerr << "SpectrogramLayer: illuminateLocalFeatures("
Chris@180 2187 // << localPos.x() << "," << localPos.y() << ")" << std::endl;
Chris@121 2188
Chris@121 2189 float s0, s1;
Chris@121 2190 float f0, f1;
Chris@121 2191
Chris@121 2192 if (getXBinRange(v, localPos.x(), s0, s1) &&
Chris@121 2193 getYBinSourceRange(v, localPos.y(), f0, f1)) {
Chris@121 2194
Chris@121 2195 int s0i = int(s0 + 0.001);
Chris@121 2196 int s1i = int(s1);
Chris@121 2197
Chris@121 2198 int x0 = v->getXForFrame(s0i * getWindowIncrement());
Chris@121 2199 int x1 = v->getXForFrame((s1i + 1) * getWindowIncrement());
Chris@121 2200
Chris@121 2201 int y1 = getYForFrequency(v, f1);
Chris@121 2202 int y0 = getYForFrequency(v, f0);
Chris@121 2203
Chris@180 2204 // std::cerr << "SpectrogramLayer: illuminate "
Chris@180 2205 // << x0 << "," << y1 << " -> " << x1 << "," << y0 << std::endl;
Chris@121 2206
Chris@121 2207 paint.setPen(Qt::white);
Chris@133 2208
Chris@133 2209 //!!! should we be using paintCrosshairs for this?
Chris@133 2210
Chris@121 2211 paint.drawRect(x0, y1, x1 - x0 + 1, y0 - y1 + 1);
Chris@121 2212 }
Chris@121 2213 }
Chris@121 2214
Chris@42 2215 float
Chris@44 2216 SpectrogramLayer::getYForFrequency(View *v, float frequency) const
Chris@42 2217 {
Chris@44 2218 return v->getYForFrequency(frequency,
Chris@44 2219 getEffectiveMinFrequency(),
Chris@44 2220 getEffectiveMaxFrequency(),
Chris@44 2221 m_frequencyScale == LogFrequencyScale);
Chris@42 2222 }
Chris@42 2223
Chris@42 2224 float
Chris@44 2225 SpectrogramLayer::getFrequencyForY(View *v, int y) const
Chris@42 2226 {
Chris@44 2227 return v->getFrequencyForY(y,
Chris@44 2228 getEffectiveMinFrequency(),
Chris@44 2229 getEffectiveMaxFrequency(),
Chris@44 2230 m_frequencyScale == LogFrequencyScale);
Chris@42 2231 }
Chris@42 2232
Chris@0 2233 int
Chris@115 2234 SpectrogramLayer::getCompletion(View *v) const
Chris@0 2235 {
Chris@115 2236 if (m_updateTimer == 0) return 100;
Chris@130 2237 if (m_fftModels.find(v) == m_fftModels.end()) return 100;
Chris@130 2238
Chris@130 2239 size_t completion = m_fftModels[v].first->getCompletion();
Chris@115 2240 std::cerr << "SpectrogramLayer::getCompletion: completion = " << completion << std::endl;
Chris@0 2241 return completion;
Chris@0 2242 }
Chris@0 2243
Chris@28 2244 bool
Chris@101 2245 SpectrogramLayer::getValueExtents(float &min, float &max,
Chris@101 2246 bool &logarithmic, QString &unit) const
Chris@79 2247 {
Chris@133 2248 if (!m_model) return false;
Chris@133 2249
Chris@133 2250 int sr = m_model->getSampleRate();
Chris@133 2251 min = float(sr) / m_fftSize;
Chris@133 2252 max = float(sr) / 2;
Chris@133 2253
Chris@101 2254 logarithmic = (m_frequencyScale == LogFrequencyScale);
Chris@79 2255 unit = "Hz";
Chris@79 2256 return true;
Chris@79 2257 }
Chris@79 2258
Chris@79 2259 bool
Chris@101 2260 SpectrogramLayer::getDisplayExtents(float &min, float &max) const
Chris@101 2261 {
Chris@101 2262 min = getEffectiveMinFrequency();
Chris@101 2263 max = getEffectiveMaxFrequency();
Chris@187 2264 std::cerr << "SpectrogramLayer::getDisplayExtents: " << min << "->" << max << std::endl;
Chris@101 2265 return true;
Chris@101 2266 }
Chris@101 2267
Chris@101 2268 bool
Chris@120 2269 SpectrogramLayer::setDisplayExtents(float min, float max)
Chris@120 2270 {
Chris@120 2271 if (!m_model) return false;
Chris@187 2272
Chris@187 2273 std::cerr << "SpectrogramLayer::setDisplayExtents: " << min << "->" << max << std::endl;
Chris@187 2274
Chris@120 2275 if (min < 0) min = 0;
Chris@120 2276 if (max > m_model->getSampleRate()/2) max = m_model->getSampleRate()/2;
Chris@120 2277
Chris@120 2278 size_t minf = lrintf(min);
Chris@120 2279 size_t maxf = lrintf(max);
Chris@120 2280
Chris@120 2281 if (m_minFrequency == minf && m_maxFrequency == maxf) return true;
Chris@120 2282
Chris@120 2283 invalidatePixmapCaches();
Chris@120 2284 invalidateMagnitudes();
Chris@120 2285
Chris@120 2286 m_minFrequency = minf;
Chris@120 2287 m_maxFrequency = maxf;
Chris@120 2288
Chris@120 2289 emit layerParametersChanged();
Chris@120 2290
Chris@133 2291 int vs = getCurrentVerticalZoomStep();
Chris@133 2292 if (vs != m_lastEmittedZoomStep) {
Chris@133 2293 emit verticalZoomChanged();
Chris@133 2294 m_lastEmittedZoomStep = vs;
Chris@133 2295 }
Chris@133 2296
Chris@120 2297 return true;
Chris@120 2298 }
Chris@120 2299
Chris@120 2300 bool
Chris@44 2301 SpectrogramLayer::snapToFeatureFrame(View *v, int &frame,
Chris@28 2302 size_t &resolution,
Chris@28 2303 SnapType snap) const
Chris@13 2304 {
Chris@13 2305 resolution = getWindowIncrement();
Chris@28 2306 int left = (frame / resolution) * resolution;
Chris@28 2307 int right = left + resolution;
Chris@28 2308
Chris@28 2309 switch (snap) {
Chris@28 2310 case SnapLeft: frame = left; break;
Chris@28 2311 case SnapRight: frame = right; break;
Chris@28 2312 case SnapNearest:
Chris@28 2313 case SnapNeighbouring:
Chris@28 2314 if (frame - left > right - frame) frame = right;
Chris@28 2315 else frame = left;
Chris@28 2316 break;
Chris@28 2317 }
Chris@28 2318
Chris@28 2319 return true;
Chris@28 2320 }
Chris@13 2321
Chris@77 2322 bool
Chris@77 2323 SpectrogramLayer::getCrosshairExtents(View *v, QPainter &paint,
Chris@77 2324 QPoint cursorPos,
Chris@77 2325 std::vector<QRect> &extents) const
Chris@77 2326 {
Chris@77 2327 QRect vertical(cursorPos.x() - 12, 0, 12, v->height());
Chris@77 2328 extents.push_back(vertical);
Chris@77 2329
Chris@77 2330 QRect horizontal(0, cursorPos.y(), cursorPos.x(), 1);
Chris@77 2331 extents.push_back(horizontal);
Chris@77 2332
Chris@77 2333 return true;
Chris@77 2334 }
Chris@77 2335
Chris@77 2336 void
Chris@77 2337 SpectrogramLayer::paintCrosshairs(View *v, QPainter &paint,
Chris@77 2338 QPoint cursorPos) const
Chris@77 2339 {
Chris@77 2340 paint.save();
Chris@77 2341 paint.setPen(m_crosshairColour);
Chris@77 2342
Chris@77 2343 paint.drawLine(0, cursorPos.y(), cursorPos.x() - 1, cursorPos.y());
Chris@77 2344 paint.drawLine(cursorPos.x(), 0, cursorPos.x(), v->height());
Chris@77 2345
Chris@77 2346 float fundamental = getFrequencyForY(v, cursorPos.y());
Chris@77 2347
Chris@77 2348 int harmonic = 2;
Chris@77 2349
Chris@77 2350 while (harmonic < 100) {
Chris@77 2351
Chris@77 2352 float hy = lrintf(getYForFrequency(v, fundamental * harmonic));
Chris@77 2353 if (hy < 0 || hy > v->height()) break;
Chris@77 2354
Chris@77 2355 int len = 7;
Chris@77 2356
Chris@77 2357 if (harmonic % 2 == 0) {
Chris@77 2358 if (harmonic % 4 == 0) {
Chris@77 2359 len = 12;
Chris@77 2360 } else {
Chris@77 2361 len = 10;
Chris@77 2362 }
Chris@77 2363 }
Chris@77 2364
Chris@77 2365 paint.drawLine(cursorPos.x() - len,
Chris@77 2366 hy,
Chris@77 2367 cursorPos.x(),
Chris@77 2368 hy);
Chris@77 2369
Chris@77 2370 ++harmonic;
Chris@77 2371 }
Chris@77 2372
Chris@77 2373 paint.restore();
Chris@77 2374 }
Chris@77 2375
Chris@25 2376 QString
Chris@44 2377 SpectrogramLayer::getFeatureDescription(View *v, QPoint &pos) const
Chris@25 2378 {
Chris@25 2379 int x = pos.x();
Chris@25 2380 int y = pos.y();
Chris@0 2381
Chris@25 2382 if (!m_model || !m_model->isOK()) return "";
Chris@0 2383
Chris@38 2384 float magMin = 0, magMax = 0;
Chris@38 2385 float phaseMin = 0, phaseMax = 0;
Chris@0 2386 float freqMin = 0, freqMax = 0;
Chris@35 2387 float adjFreqMin = 0, adjFreqMax = 0;
Chris@25 2388 QString pitchMin, pitchMax;
Chris@0 2389 RealTime rtMin, rtMax;
Chris@0 2390
Chris@38 2391 bool haveValues = false;
Chris@0 2392
Chris@44 2393 if (!getXBinSourceRange(v, x, rtMin, rtMax)) {
Chris@38 2394 return "";
Chris@38 2395 }
Chris@44 2396 if (getXYBinSourceRange(v, x, y, magMin, magMax, phaseMin, phaseMax)) {
Chris@38 2397 haveValues = true;
Chris@38 2398 }
Chris@0 2399
Chris@35 2400 QString adjFreqText = "", adjPitchText = "";
Chris@35 2401
Chris@38 2402 if (m_binDisplay == PeakFrequencies) {
Chris@35 2403
Chris@44 2404 if (!getAdjustedYBinSourceRange(v, x, y, freqMin, freqMax,
Chris@38 2405 adjFreqMin, adjFreqMax)) {
Chris@38 2406 return "";
Chris@38 2407 }
Chris@35 2408
Chris@35 2409 if (adjFreqMin != adjFreqMax) {
Chris@65 2410 adjFreqText = tr("Peak Frequency:\t%1 - %2 Hz\n")
Chris@35 2411 .arg(adjFreqMin).arg(adjFreqMax);
Chris@35 2412 } else {
Chris@65 2413 adjFreqText = tr("Peak Frequency:\t%1 Hz\n")
Chris@35 2414 .arg(adjFreqMin);
Chris@38 2415 }
Chris@38 2416
Chris@38 2417 QString pmin = Pitch::getPitchLabelForFrequency(adjFreqMin);
Chris@38 2418 QString pmax = Pitch::getPitchLabelForFrequency(adjFreqMax);
Chris@38 2419
Chris@38 2420 if (pmin != pmax) {
Chris@65 2421 adjPitchText = tr("Peak Pitch:\t%3 - %4\n").arg(pmin).arg(pmax);
Chris@38 2422 } else {
Chris@65 2423 adjPitchText = tr("Peak Pitch:\t%2\n").arg(pmin);
Chris@35 2424 }
Chris@35 2425
Chris@35 2426 } else {
Chris@35 2427
Chris@44 2428 if (!getYBinSourceRange(v, y, freqMin, freqMax)) return "";
Chris@35 2429 }
Chris@35 2430
Chris@25 2431 QString text;
Chris@25 2432
Chris@25 2433 if (rtMin != rtMax) {
Chris@25 2434 text += tr("Time:\t%1 - %2\n")
Chris@25 2435 .arg(rtMin.toText(true).c_str())
Chris@25 2436 .arg(rtMax.toText(true).c_str());
Chris@25 2437 } else {
Chris@25 2438 text += tr("Time:\t%1\n")
Chris@25 2439 .arg(rtMin.toText(true).c_str());
Chris@0 2440 }
Chris@0 2441
Chris@25 2442 if (freqMin != freqMax) {
Chris@65 2443 text += tr("%1Bin Frequency:\t%2 - %3 Hz\n%4Bin Pitch:\t%5 - %6\n")
Chris@65 2444 .arg(adjFreqText)
Chris@25 2445 .arg(freqMin)
Chris@25 2446 .arg(freqMax)
Chris@65 2447 .arg(adjPitchText)
Chris@65 2448 .arg(Pitch::getPitchLabelForFrequency(freqMin))
Chris@65 2449 .arg(Pitch::getPitchLabelForFrequency(freqMax));
Chris@65 2450 } else {
Chris@65 2451 text += tr("%1Bin Frequency:\t%2 Hz\n%3Bin Pitch:\t%4\n")
Chris@35 2452 .arg(adjFreqText)
Chris@25 2453 .arg(freqMin)
Chris@65 2454 .arg(adjPitchText)
Chris@65 2455 .arg(Pitch::getPitchLabelForFrequency(freqMin));
Chris@25 2456 }
Chris@25 2457
Chris@38 2458 if (haveValues) {
Chris@38 2459 float dbMin = AudioLevel::multiplier_to_dB(magMin);
Chris@38 2460 float dbMax = AudioLevel::multiplier_to_dB(magMax);
Chris@43 2461 QString dbMinString;
Chris@43 2462 QString dbMaxString;
Chris@43 2463 if (dbMin == AudioLevel::DB_FLOOR) {
Chris@43 2464 dbMinString = tr("-Inf");
Chris@43 2465 } else {
Chris@43 2466 dbMinString = QString("%1").arg(lrintf(dbMin));
Chris@43 2467 }
Chris@43 2468 if (dbMax == AudioLevel::DB_FLOOR) {
Chris@43 2469 dbMaxString = tr("-Inf");
Chris@43 2470 } else {
Chris@43 2471 dbMaxString = QString("%1").arg(lrintf(dbMax));
Chris@43 2472 }
Chris@25 2473 if (lrintf(dbMin) != lrintf(dbMax)) {
Chris@199 2474 text += tr("dB:\t%1 - %2").arg(dbMinString).arg(dbMaxString);
Chris@25 2475 } else {
Chris@199 2476 text += tr("dB:\t%1").arg(dbMinString);
Chris@25 2477 }
Chris@38 2478 if (phaseMin != phaseMax) {
Chris@38 2479 text += tr("\nPhase:\t%1 - %2").arg(phaseMin).arg(phaseMax);
Chris@38 2480 } else {
Chris@38 2481 text += tr("\nPhase:\t%1").arg(phaseMin);
Chris@38 2482 }
Chris@25 2483 }
Chris@25 2484
Chris@25 2485 return text;
Chris@0 2486 }
Chris@25 2487
Chris@0 2488 int
Chris@40 2489 SpectrogramLayer::getColourScaleWidth(QPainter &paint) const
Chris@40 2490 {
Chris@40 2491 int cw;
Chris@40 2492
Chris@119 2493 cw = paint.fontMetrics().width("-80dB");
Chris@119 2494
Chris@40 2495 return cw;
Chris@40 2496 }
Chris@40 2497
Chris@40 2498 int
Chris@44 2499 SpectrogramLayer::getVerticalScaleWidth(View *v, QPainter &paint) const
Chris@0 2500 {
Chris@0 2501 if (!m_model || !m_model->isOK()) return 0;
Chris@0 2502
Chris@40 2503 int cw = getColourScaleWidth(paint);
Chris@40 2504
Chris@0 2505 int tw = paint.fontMetrics().width(QString("%1")
Chris@0 2506 .arg(m_maxFrequency > 0 ?
Chris@0 2507 m_maxFrequency - 1 :
Chris@0 2508 m_model->getSampleRate() / 2));
Chris@0 2509
Chris@0 2510 int fw = paint.fontMetrics().width(QString("43Hz"));
Chris@0 2511 if (tw < fw) tw = fw;
Chris@40 2512
Chris@40 2513 int tickw = (m_frequencyScale == LogFrequencyScale ? 10 : 4);
Chris@0 2514
Chris@40 2515 return cw + tickw + tw + 13;
Chris@0 2516 }
Chris@0 2517
Chris@0 2518 void
Chris@44 2519 SpectrogramLayer::paintVerticalScale(View *v, QPainter &paint, QRect rect) const
Chris@0 2520 {
Chris@0 2521 if (!m_model || !m_model->isOK()) {
Chris@0 2522 return;
Chris@0 2523 }
Chris@0 2524
Chris@161 2525 Profiler profiler("SpectrogramLayer::paintVerticalScale", true);
Chris@122 2526
Chris@120 2527 //!!! cache this?
Chris@120 2528
Chris@0 2529 int h = rect.height(), w = rect.width();
Chris@0 2530
Chris@40 2531 int tickw = (m_frequencyScale == LogFrequencyScale ? 10 : 4);
Chris@40 2532 int pkw = (m_frequencyScale == LogFrequencyScale ? 10 : 0);
Chris@40 2533
Chris@107 2534 size_t bins = m_fftSize / 2;
Chris@0 2535 int sr = m_model->getSampleRate();
Chris@0 2536
Chris@0 2537 if (m_maxFrequency > 0) {
Chris@107 2538 bins = int((double(m_maxFrequency) * m_fftSize) / sr + 0.1);
Chris@107 2539 if (bins > m_fftSize / 2) bins = m_fftSize / 2;
Chris@0 2540 }
Chris@0 2541
Chris@40 2542 int cw = getColourScaleWidth(paint);
Chris@119 2543 int cbw = paint.fontMetrics().width("dB");
Chris@40 2544
Chris@0 2545 int py = -1;
Chris@0 2546 int textHeight = paint.fontMetrics().height();
Chris@0 2547 int toff = -textHeight + paint.fontMetrics().ascent() + 2;
Chris@0 2548
Chris@119 2549 if (h > textHeight * 3 + 10) {
Chris@119 2550
Chris@119 2551 int topLines = 2;
Chris@119 2552 if (m_colourScale == PhaseColourScale) topLines = 1;
Chris@119 2553
Chris@119 2554 int ch = h - textHeight * (topLines + 1) - 8;
Chris@119 2555 // paint.drawRect(4, textHeight + 4, cw - 1, ch + 1);
Chris@119 2556 paint.drawRect(4 + cw - cbw, textHeight * topLines + 4, cbw - 1, ch + 1);
Chris@40 2557
Chris@40 2558 QString top, bottom;
Chris@119 2559 float min = m_viewMags[v].getMin();
Chris@119 2560 float max = m_viewMags[v].getMax();
Chris@119 2561
Chris@119 2562 float dBmin = AudioLevel::multiplier_to_dB(min);
Chris@119 2563 float dBmax = AudioLevel::multiplier_to_dB(max);
Chris@119 2564
Chris@120 2565 if (dBmax < -60.f) dBmax = -60.f;
Chris@120 2566 else top = QString("%1").arg(lrintf(dBmax));
Chris@120 2567
Chris@120 2568 if (dBmin < dBmax - 60.f) dBmin = dBmax - 60.f;
Chris@119 2569 bottom = QString("%1").arg(lrintf(dBmin));
Chris@119 2570
Chris@119 2571 //!!! & phase etc
Chris@119 2572
Chris@119 2573 if (m_colourScale != PhaseColourScale) {
Chris@119 2574 paint.drawText((cw + 6 - paint.fontMetrics().width("dBFS")) / 2,
Chris@119 2575 2 + textHeight + toff, "dBFS");
Chris@119 2576 }
Chris@119 2577
Chris@119 2578 // paint.drawText((cw + 6 - paint.fontMetrics().width(top)) / 2,
Chris@119 2579 paint.drawText(3 + cw - cbw - paint.fontMetrics().width(top),
Chris@119 2580 2 + textHeight * topLines + toff + textHeight/2, top);
Chris@119 2581
Chris@119 2582 paint.drawText(3 + cw - cbw - paint.fontMetrics().width(bottom),
Chris@119 2583 h + toff - 3 - textHeight/2, bottom);
Chris@40 2584
Chris@40 2585 paint.save();
Chris@40 2586 paint.setBrush(Qt::NoBrush);
Chris@119 2587
Chris@119 2588 int lasty = 0;
Chris@119 2589 int lastdb = 0;
Chris@119 2590
Chris@40 2591 for (int i = 0; i < ch; ++i) {
Chris@119 2592
Chris@119 2593 float dBval = dBmin + (((dBmax - dBmin) * i) / (ch - 1));
Chris@119 2594 int idb = int(dBval);
Chris@119 2595
Chris@119 2596 float value = AudioLevel::dB_to_multiplier(dBval);
Chris@119 2597 int colour = getDisplayValue(v, value * m_gain);
Chris@210 2598
Chris@197 2599 paint.setPen(m_palette.getColour(colour));
Chris@119 2600
Chris@119 2601 int y = textHeight * topLines + 4 + ch - i;
Chris@119 2602
Chris@119 2603 paint.drawLine(5 + cw - cbw, y, cw + 2, y);
Chris@119 2604
Chris@119 2605 if (i == 0) {
Chris@119 2606 lasty = y;
Chris@119 2607 lastdb = idb;
Chris@119 2608 } else if (i < ch - paint.fontMetrics().ascent() &&
Chris@120 2609 idb != lastdb &&
Chris@119 2610 ((abs(y - lasty) > textHeight &&
Chris@119 2611 idb % 10 == 0) ||
Chris@119 2612 (abs(y - lasty) > paint.fontMetrics().ascent() &&
Chris@119 2613 idb % 5 == 0))) {
Chris@119 2614 paint.setPen(Qt::black);
Chris@119 2615 QString text = QString("%1").arg(idb);
Chris@119 2616 paint.drawText(3 + cw - cbw - paint.fontMetrics().width(text),
Chris@119 2617 y + toff + textHeight/2, text);
Chris@119 2618 paint.setPen(Qt::white);
Chris@119 2619 paint.drawLine(5 + cw - cbw, y, 8 + cw - cbw, y);
Chris@119 2620 lasty = y;
Chris@119 2621 lastdb = idb;
Chris@119 2622 }
Chris@40 2623 }
Chris@40 2624 paint.restore();
Chris@40 2625 }
Chris@40 2626
Chris@40 2627 paint.drawLine(cw + 7, 0, cw + 7, h);
Chris@40 2628
Chris@0 2629 int bin = -1;
Chris@0 2630
Chris@44 2631 for (int y = 0; y < v->height(); ++y) {
Chris@0 2632
Chris@0 2633 float q0, q1;
Chris@44 2634 if (!getYBinRange(v, v->height() - y, q0, q1)) continue;
Chris@0 2635
Chris@0 2636 int vy;
Chris@0 2637
Chris@0 2638 if (int(q0) > bin) {
Chris@0 2639 vy = y;
Chris@0 2640 bin = int(q0);
Chris@0 2641 } else {
Chris@0 2642 continue;
Chris@0 2643 }
Chris@0 2644
Chris@107 2645 int freq = (sr * bin) / m_fftSize;
Chris@0 2646
Chris@0 2647 if (py >= 0 && (vy - py) < textHeight - 1) {
Chris@40 2648 if (m_frequencyScale == LinearFrequencyScale) {
Chris@40 2649 paint.drawLine(w - tickw, h - vy, w, h - vy);
Chris@40 2650 }
Chris@0 2651 continue;
Chris@0 2652 }
Chris@0 2653
Chris@0 2654 QString text = QString("%1").arg(freq);
Chris@40 2655 if (bin == 1) text = QString("%1Hz").arg(freq); // bin 0 is DC
Chris@40 2656 paint.drawLine(cw + 7, h - vy, w - pkw - 1, h - vy);
Chris@0 2657
Chris@0 2658 if (h - vy - textHeight >= -2) {
Chris@40 2659 int tx = w - 3 - paint.fontMetrics().width(text) - std::max(tickw, pkw);
Chris@0 2660 paint.drawText(tx, h - vy + toff, text);
Chris@0 2661 }
Chris@0 2662
Chris@0 2663 py = vy;
Chris@0 2664 }
Chris@40 2665
Chris@40 2666 if (m_frequencyScale == LogFrequencyScale) {
Chris@40 2667
Chris@40 2668 paint.drawLine(w - pkw - 1, 0, w - pkw - 1, h);
Chris@40 2669
Chris@110 2670 int sr = m_model->getSampleRate();
Chris@40 2671 float minf = getEffectiveMinFrequency();
Chris@40 2672 float maxf = getEffectiveMaxFrequency();
Chris@40 2673
Chris@122 2674 int py = h, ppy = h;
Chris@40 2675 paint.setBrush(paint.pen().color());
Chris@40 2676
Chris@40 2677 for (int i = 0; i < 128; ++i) {
Chris@40 2678
Chris@40 2679 float f = Pitch::getFrequencyForPitch(i);
Chris@44 2680 int y = lrintf(v->getYForFrequency(f, minf, maxf, true));
Chris@122 2681
Chris@122 2682 if (y < -2) break;
Chris@122 2683 if (y > h + 2) {
Chris@122 2684 continue;
Chris@122 2685 }
Chris@122 2686
Chris@40 2687 int n = (i % 12);
Chris@122 2688
Chris@122 2689 if (n == 1) {
Chris@122 2690 // C# -- fill the C from here
Chris@122 2691 if (ppy - y > 2) {
Chris@122 2692 paint.fillRect(w - pkw,
Chris@122 2693 // y - (py - y) / 2 - (py - y) / 4,
Chris@122 2694 y,
Chris@122 2695 pkw,
Chris@122 2696 (py + ppy) / 2 - y,
Chris@122 2697 // py - y + 1,
Chris@122 2698 Qt::gray);
Chris@122 2699 }
Chris@122 2700 }
Chris@122 2701
Chris@40 2702 if (n == 1 || n == 3 || n == 6 || n == 8 || n == 10) {
Chris@40 2703 // black notes
Chris@40 2704 paint.drawLine(w - pkw, y, w, y);
Chris@41 2705 int rh = ((py - y) / 4) * 2;
Chris@41 2706 if (rh < 2) rh = 2;
Chris@41 2707 paint.drawRect(w - pkw, y - (py-y)/4, pkw/2, rh);
Chris@40 2708 } else if (n == 0 || n == 5) {
Chris@122 2709 // C, F
Chris@40 2710 if (py < h) {
Chris@40 2711 paint.drawLine(w - pkw, (y + py) / 2, w, (y + py) / 2);
Chris@40 2712 }
Chris@40 2713 }
Chris@40 2714
Chris@122 2715 ppy = py;
Chris@40 2716 py = y;
Chris@40 2717 }
Chris@40 2718 }
Chris@0 2719 }
Chris@0 2720
Chris@187 2721 class SpectrogramRangeMapper : public RangeMapper
Chris@187 2722 {
Chris@187 2723 public:
Chris@187 2724 SpectrogramRangeMapper(int sr, int fftsize) :
Chris@187 2725 m_dist(float(sr) / 2),
Chris@187 2726 m_s2(sqrtf(sqrtf(2))) { }
Chris@187 2727 ~SpectrogramRangeMapper() { }
Chris@187 2728
Chris@187 2729 virtual int getPositionForValue(float value) const {
Chris@187 2730
Chris@187 2731 float dist = m_dist;
Chris@187 2732
Chris@187 2733 int n = 0;
Chris@187 2734 int discard = 0;
Chris@187 2735
Chris@187 2736 while (dist > (value + 0.00001) && dist > 0.1f) {
Chris@187 2737 dist /= m_s2;
Chris@187 2738 ++n;
Chris@187 2739 }
Chris@187 2740
Chris@187 2741 return n;
Chris@187 2742 }
Chris@187 2743
Chris@187 2744 virtual float getValueForPosition(int position) const {
Chris@187 2745
Chris@187 2746 // Vertical zoom step 0 shows the entire range from DC ->
Chris@187 2747 // Nyquist frequency. Step 1 shows 2^(1/4) of the range of
Chris@187 2748 // step 0, and so on until the visible range is smaller than
Chris@187 2749 // the frequency step between bins at the current fft size.
Chris@187 2750
Chris@187 2751 float dist = m_dist;
Chris@187 2752
Chris@187 2753 int n = 0;
Chris@187 2754 while (n < position) {
Chris@187 2755 dist /= m_s2;
Chris@187 2756 ++n;
Chris@187 2757 }
Chris@187 2758
Chris@187 2759 return dist;
Chris@187 2760 }
Chris@187 2761
Chris@187 2762 virtual QString getUnit() const { return "Hz"; }
Chris@187 2763
Chris@187 2764 protected:
Chris@187 2765 float m_dist;
Chris@187 2766 float m_s2;
Chris@187 2767 };
Chris@187 2768
Chris@133 2769 int
Chris@133 2770 SpectrogramLayer::getVerticalZoomSteps(int &defaultStep) const
Chris@133 2771 {
Chris@135 2772 if (!m_model) return 0;
Chris@187 2773
Chris@187 2774 int sr = m_model->getSampleRate();
Chris@187 2775
Chris@187 2776 SpectrogramRangeMapper mapper(sr, m_fftSize);
Chris@187 2777
Chris@187 2778 // int maxStep = mapper.getPositionForValue((float(sr) / m_fftSize) + 0.001);
Chris@187 2779 int maxStep = mapper.getPositionForValue(0);
Chris@187 2780 int minStep = mapper.getPositionForValue(float(sr) / 2);
Chris@135 2781
Chris@187 2782 defaultStep = mapper.getPositionForValue(m_initialMaxFrequency) - minStep;
Chris@187 2783
Chris@187 2784 std::cerr << "SpectrogramLayer::getVerticalZoomSteps: " << maxStep - minStep << " (" << maxStep <<"-" << minStep << "), default is " << defaultStep << " (from initial max freq " << m_initialMaxFrequency << ")" << std::endl;
Chris@187 2785
Chris@187 2786 return maxStep - minStep;
Chris@133 2787 }
Chris@133 2788
Chris@133 2789 int
Chris@133 2790 SpectrogramLayer::getCurrentVerticalZoomStep() const
Chris@133 2791 {
Chris@133 2792 if (!m_model) return 0;
Chris@133 2793
Chris@133 2794 float dmin, dmax;
Chris@133 2795 getDisplayExtents(dmin, dmax);
Chris@133 2796
Chris@187 2797 SpectrogramRangeMapper mapper(m_model->getSampleRate(), m_fftSize);
Chris@187 2798 int n = mapper.getPositionForValue(dmax - dmin);
Chris@187 2799 std::cerr << "SpectrogramLayer::getCurrentVerticalZoomStep: " << n << std::endl;
Chris@133 2800 return n;
Chris@133 2801 }
Chris@133 2802
Chris@133 2803 void
Chris@133 2804 SpectrogramLayer::setVerticalZoomStep(int step)
Chris@133 2805 {
Chris@133 2806 //!!! does not do the right thing for log scale
Chris@133 2807
Chris@187 2808 if (!m_model) return;
Chris@187 2809
Chris@133 2810 float dmin, dmax;
Chris@133 2811 getDisplayExtents(dmin, dmax);
Chris@133 2812
Chris@133 2813 int sr = m_model->getSampleRate();
Chris@187 2814 SpectrogramRangeMapper mapper(sr, m_fftSize);
Chris@187 2815 float ddist = mapper.getValueForPosition(step);
Chris@133 2816
Chris@133 2817 float dmid = (dmax + dmin) / 2;
Chris@133 2818 float newmin = dmid - ddist / 2;
Chris@133 2819 float newmax = dmid + ddist / 2;
Chris@187 2820
Chris@187 2821 float mmin, mmax;
Chris@187 2822 mmin = 0;
Chris@187 2823 mmax = float(sr) / 2;
Chris@133 2824
Chris@187 2825 if (newmin < mmin) {
Chris@187 2826 newmax += (mmin - newmin);
Chris@187 2827 newmin = mmin;
Chris@187 2828 }
Chris@187 2829 if (newmax > mmax) {
Chris@187 2830 newmax = mmax;
Chris@187 2831 }
Chris@133 2832
Chris@187 2833 std::cerr << "SpectrogramLayer::setVerticalZoomStep: " << step << ": " << newmin << " -> " << newmax << " (range " << ddist << ")" << std::endl;
Chris@187 2834
Chris@187 2835 setMinFrequency(int(newmin));
Chris@187 2836 setMaxFrequency(int(newmax));
Chris@187 2837 }
Chris@187 2838
Chris@187 2839 RangeMapper *
Chris@187 2840 SpectrogramLayer::getNewVerticalZoomRangeMapper() const
Chris@187 2841 {
Chris@187 2842 if (!m_model) return 0;
Chris@187 2843 return new SpectrogramRangeMapper(m_model->getSampleRate(), m_fftSize);
Chris@133 2844 }
Chris@133 2845
Chris@6 2846 QString
Chris@6 2847 SpectrogramLayer::toXmlString(QString indent, QString extraAttributes) const
Chris@6 2848 {
Chris@6 2849 QString s;
Chris@6 2850
Chris@6 2851 s += QString("channel=\"%1\" "
Chris@6 2852 "windowSize=\"%2\" "
Chris@153 2853 "windowHopLevel=\"%3\" "
Chris@153 2854 "gain=\"%4\" "
Chris@153 2855 "threshold=\"%5\" ")
Chris@6 2856 .arg(m_channel)
Chris@6 2857 .arg(m_windowSize)
Chris@97 2858 .arg(m_windowHopLevel)
Chris@37 2859 .arg(m_gain)
Chris@37 2860 .arg(m_threshold);
Chris@37 2861
Chris@37 2862 s += QString("minFrequency=\"%1\" "
Chris@37 2863 "maxFrequency=\"%2\" "
Chris@37 2864 "colourScale=\"%3\" "
Chris@37 2865 "colourScheme=\"%4\" "
Chris@37 2866 "colourRotation=\"%5\" "
Chris@37 2867 "frequencyScale=\"%6\" "
Chris@37 2868 "binDisplay=\"%7\" "
Chris@153 2869 "normalizeColumns=\"%8\" "
Chris@153 2870 "normalizeVisibleArea=\"%9\"")
Chris@37 2871 .arg(m_minFrequency)
Chris@6 2872 .arg(m_maxFrequency)
Chris@6 2873 .arg(m_colourScale)
Chris@197 2874 .arg(m_colourMap)
Chris@37 2875 .arg(m_colourRotation)
Chris@35 2876 .arg(m_frequencyScale)
Chris@37 2877 .arg(m_binDisplay)
Chris@153 2878 .arg(m_normalizeColumns ? "true" : "false")
Chris@153 2879 .arg(m_normalizeVisibleArea ? "true" : "false");
Chris@6 2880
Chris@6 2881 return Layer::toXmlString(indent, extraAttributes + " " + s);
Chris@6 2882 }
Chris@6 2883
Chris@11 2884 void
Chris@11 2885 SpectrogramLayer::setProperties(const QXmlAttributes &attributes)
Chris@11 2886 {
Chris@11 2887 bool ok = false;
Chris@11 2888
Chris@11 2889 int channel = attributes.value("channel").toInt(&ok);
Chris@11 2890 if (ok) setChannel(channel);
Chris@11 2891
Chris@11 2892 size_t windowSize = attributes.value("windowSize").toUInt(&ok);
Chris@11 2893 if (ok) setWindowSize(windowSize);
Chris@11 2894
Chris@97 2895 size_t windowHopLevel = attributes.value("windowHopLevel").toUInt(&ok);
Chris@97 2896 if (ok) setWindowHopLevel(windowHopLevel);
Chris@97 2897 else {
Chris@97 2898 size_t windowOverlap = attributes.value("windowOverlap").toUInt(&ok);
Chris@97 2899 // a percentage value
Chris@97 2900 if (ok) {
Chris@97 2901 if (windowOverlap == 0) setWindowHopLevel(0);
Chris@97 2902 else if (windowOverlap == 25) setWindowHopLevel(1);
Chris@97 2903 else if (windowOverlap == 50) setWindowHopLevel(2);
Chris@97 2904 else if (windowOverlap == 75) setWindowHopLevel(3);
Chris@97 2905 else if (windowOverlap == 90) setWindowHopLevel(4);
Chris@97 2906 }
Chris@97 2907 }
Chris@11 2908
Chris@11 2909 float gain = attributes.value("gain").toFloat(&ok);
Chris@11 2910 if (ok) setGain(gain);
Chris@11 2911
Chris@37 2912 float threshold = attributes.value("threshold").toFloat(&ok);
Chris@37 2913 if (ok) setThreshold(threshold);
Chris@37 2914
Chris@37 2915 size_t minFrequency = attributes.value("minFrequency").toUInt(&ok);
Chris@187 2916 if (ok) {
Chris@187 2917 std::cerr << "SpectrogramLayer::setProperties: setting min freq to " << minFrequency << std::endl;
Chris@187 2918 setMinFrequency(minFrequency);
Chris@187 2919 }
Chris@37 2920
Chris@11 2921 size_t maxFrequency = attributes.value("maxFrequency").toUInt(&ok);
Chris@187 2922 if (ok) {
Chris@187 2923 std::cerr << "SpectrogramLayer::setProperties: setting max freq to " << maxFrequency << std::endl;
Chris@187 2924 setMaxFrequency(maxFrequency);
Chris@187 2925 }
Chris@11 2926
Chris@11 2927 ColourScale colourScale = (ColourScale)
Chris@11 2928 attributes.value("colourScale").toInt(&ok);
Chris@11 2929 if (ok) setColourScale(colourScale);
Chris@11 2930
Chris@197 2931 int colourMap = attributes.value("colourScheme").toInt(&ok);
Chris@197 2932 if (ok) setColourMap(colourMap);
Chris@11 2933
Chris@37 2934 int colourRotation = attributes.value("colourRotation").toInt(&ok);
Chris@37 2935 if (ok) setColourRotation(colourRotation);
Chris@37 2936
Chris@11 2937 FrequencyScale frequencyScale = (FrequencyScale)
Chris@11 2938 attributes.value("frequencyScale").toInt(&ok);
Chris@11 2939 if (ok) setFrequencyScale(frequencyScale);
Chris@35 2940
Chris@37 2941 BinDisplay binDisplay = (BinDisplay)
Chris@37 2942 attributes.value("binDisplay").toInt(&ok);
Chris@37 2943 if (ok) setBinDisplay(binDisplay);
Chris@36 2944
Chris@36 2945 bool normalizeColumns =
Chris@36 2946 (attributes.value("normalizeColumns").trimmed() == "true");
Chris@36 2947 setNormalizeColumns(normalizeColumns);
Chris@153 2948
Chris@153 2949 bool normalizeVisibleArea =
Chris@153 2950 (attributes.value("normalizeVisibleArea").trimmed() == "true");
Chris@153 2951 setNormalizeVisibleArea(normalizeVisibleArea);
Chris@11 2952 }
Chris@11 2953