annotate layer/SpectrogramLayer.cpp @ 204:942cfa1df966

* Fix occasional misplacement of zoom wheels * Disable tearoff in context menu (it always got activated when you just clicked and released)
author Chris Cannam
date Mon, 26 Feb 2007 14:18:21 +0000
parents 258af0c4dc28
children b6af2eb00780
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@119 493 case 3: setColourScale(OtherColourScale); 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
Chris@119 1101 // (input * (m_normalizeColumns ? 1.0 : 50.0) * 255.0) + 1;
Chris@119 1102 value = int(((input - min) / (max - min)) * 255.f) + 1;
Chris@40 1103 break;
Chris@40 1104
Chris@40 1105 case MeterColourScale:
Chris@119 1106 // value = AudioLevel::multiplier_to_preview
Chris@119 1107 // (input * (m_normalizeColumns ? 1.0 : 50.0), 255) + 1;
Chris@119 1108 value = AudioLevel::multiplier_to_preview((input - min) / (max - min), 255) + 1;
Chris@40 1109 break;
Chris@40 1110
Chris@40 1111 case dBColourScale:
Chris@119 1112 //!!! experiment with normalizing the visible area this way.
Chris@119 1113 //In any case, we need to have some indication of what the dB
Chris@119 1114 //scale is relative to.
Chris@133 1115 input = input / max;
Chris@133 1116 if (input > 0.f) {
Chris@133 1117 input = 10.f * log10f(input);
Chris@133 1118 } else {
Chris@133 1119 input = thresh;
Chris@133 1120 }
Chris@119 1121 if (min > 0.f) {
Chris@119 1122 thresh = 10.f * log10f(min);
Chris@119 1123 if (thresh < -80.f) thresh = -80.f;
Chris@119 1124 }
Chris@119 1125 input = (input - thresh) / (-thresh);
Chris@119 1126 if (input < 0.f) input = 0.f;
Chris@119 1127 if (input > 1.f) input = 1.f;
Chris@119 1128 value = int(input * 255.f) + 1;
Chris@119 1129 break;
Chris@119 1130
Chris@119 1131 case OtherColourScale:
Chris@119 1132 //!!! the "Other" scale is just where our current experiments go
Chris@119 1133 //!!! power rather than v
Chris@133 1134 input = (input * input) / (max * max);
Chris@133 1135 if (input > 0.f) {
Chris@133 1136 input = 10.f * log10f(input);
Chris@133 1137 } else {
Chris@133 1138 input = thresh;
Chris@133 1139 }
Chris@119 1140 if (min > 0.f) {
Chris@119 1141 thresh = 10.f * log10f(min * min);
Chris@119 1142 if (thresh < -80.f) thresh = -80.f;
Chris@119 1143 }
Chris@119 1144 input = (input - thresh) / (-thresh);
Chris@119 1145 if (input < 0.f) input = 0.f;
Chris@119 1146 if (input > 1.f) input = 1.f;
Chris@119 1147 value = int(input * 255.f) + 1;
Chris@119 1148 break;
Chris@119 1149
Chris@119 1150 /*!!!
Chris@119 1151 input = 10.f * log10f(input * input);
Chris@119 1152 input = 1.f / (1.f + expf(- (input + 20.f) / 10.f));
Chris@119 1153
Chris@119 1154 if (input < 0.f) input = 0.f;
Chris@119 1155 if (input > 1.f) input = 1.f;
Chris@119 1156 value = int(input * 255.f) + 1;
Chris@119 1157 */
Chris@40 1158 break;
Chris@40 1159
Chris@40 1160 case PhaseColourScale:
Chris@40 1161 value = int((input * 127.0 / M_PI) + 128);
Chris@40 1162 break;
Chris@0 1163 }
Chris@38 1164
Chris@38 1165 if (value > UCHAR_MAX) value = UCHAR_MAX;
Chris@38 1166 if (value < 0) value = 0;
Chris@38 1167 return value;
Chris@0 1168 }
Chris@0 1169
Chris@40 1170 float
Chris@40 1171 SpectrogramLayer::getInputForDisplayValue(unsigned char uc) const
Chris@40 1172 {
Chris@153 1173 //!!! unused
Chris@153 1174
Chris@40 1175 int value = uc;
Chris@40 1176 float input;
Chris@40 1177
Chris@120 1178 //!!! incorrect for normalizing visible area (and also out of date)
Chris@120 1179
Chris@40 1180 switch (m_colourScale) {
Chris@40 1181
Chris@40 1182 default:
Chris@40 1183 case LinearColourScale:
Chris@40 1184 input = float(value - 1) / 255.0 / (m_normalizeColumns ? 1 : 50);
Chris@40 1185 break;
Chris@40 1186
Chris@40 1187 case MeterColourScale:
Chris@40 1188 input = AudioLevel::preview_to_multiplier(value - 1, 255)
Chris@40 1189 / (m_normalizeColumns ? 1.0 : 50.0);
Chris@40 1190 break;
Chris@40 1191
Chris@40 1192 case dBColourScale:
Chris@40 1193 input = float(value - 1) / 255.0;
Chris@40 1194 input = (input * 80.0) - 80.0;
Chris@40 1195 input = powf(10.0, input) / 20.0;
Chris@40 1196 value = int(input);
Chris@40 1197 break;
Chris@40 1198
Chris@119 1199 case OtherColourScale:
Chris@119 1200 input = float(value - 1) / 255.0;
Chris@119 1201 input = (input * 80.0) - 80.0;
Chris@119 1202 input = powf(10.0, input) / 20.0;
Chris@119 1203 value = int(input);
Chris@119 1204 break;
Chris@119 1205
Chris@40 1206 case PhaseColourScale:
Chris@40 1207 input = float(value - 128) * M_PI / 127.0;
Chris@40 1208 break;
Chris@40 1209 }
Chris@40 1210
Chris@40 1211 return input;
Chris@40 1212 }
Chris@40 1213
Chris@40 1214 float
Chris@40 1215 SpectrogramLayer::getEffectiveMinFrequency() const
Chris@40 1216 {
Chris@40 1217 int sr = m_model->getSampleRate();
Chris@107 1218 float minf = float(sr) / m_fftSize;
Chris@40 1219
Chris@40 1220 if (m_minFrequency > 0.0) {
Chris@107 1221 size_t minbin = size_t((double(m_minFrequency) * m_fftSize) / sr + 0.01);
Chris@40 1222 if (minbin < 1) minbin = 1;
Chris@107 1223 minf = minbin * sr / m_fftSize;
Chris@40 1224 }
Chris@40 1225
Chris@40 1226 return minf;
Chris@40 1227 }
Chris@40 1228
Chris@40 1229 float
Chris@40 1230 SpectrogramLayer::getEffectiveMaxFrequency() const
Chris@40 1231 {
Chris@40 1232 int sr = m_model->getSampleRate();
Chris@40 1233 float maxf = float(sr) / 2;
Chris@40 1234
Chris@40 1235 if (m_maxFrequency > 0.0) {
Chris@107 1236 size_t maxbin = size_t((double(m_maxFrequency) * m_fftSize) / sr + 0.1);
Chris@107 1237 if (maxbin > m_fftSize / 2) maxbin = m_fftSize / 2;
Chris@107 1238 maxf = maxbin * sr / m_fftSize;
Chris@40 1239 }
Chris@40 1240
Chris@40 1241 return maxf;
Chris@40 1242 }
Chris@40 1243
Chris@0 1244 bool
Chris@44 1245 SpectrogramLayer::getYBinRange(View *v, int y, float &q0, float &q1) const
Chris@0 1246 {
Chris@44 1247 int h = v->height();
Chris@0 1248 if (y < 0 || y >= h) return false;
Chris@0 1249
Chris@38 1250 int sr = m_model->getSampleRate();
Chris@40 1251 float minf = getEffectiveMinFrequency();
Chris@40 1252 float maxf = getEffectiveMaxFrequency();
Chris@0 1253
Chris@38 1254 bool logarithmic = (m_frequencyScale == LogFrequencyScale);
Chris@38 1255
Chris@130 1256 //!!! wrong for smoothing -- wrong fft size for fft model
Chris@114 1257
Chris@44 1258 q0 = v->getFrequencyForY(y, minf, maxf, logarithmic);
Chris@44 1259 q1 = v->getFrequencyForY(y - 1, minf, maxf, logarithmic);
Chris@38 1260
Chris@38 1261 // Now map these on to actual bins
Chris@38 1262
Chris@107 1263 int b0 = int((q0 * m_fftSize) / sr);
Chris@107 1264 int b1 = int((q1 * m_fftSize) / sr);
Chris@0 1265
Chris@40 1266 //!!! this is supposed to return fractions-of-bins, as it were, hence the floats
Chris@38 1267 q0 = b0;
Chris@38 1268 q1 = b1;
Chris@38 1269
Chris@107 1270 // q0 = (b0 * sr) / m_fftSize;
Chris@107 1271 // q1 = (b1 * sr) / m_fftSize;
Chris@0 1272
Chris@0 1273 return true;
Chris@0 1274 }
Chris@38 1275
Chris@0 1276 bool
Chris@44 1277 SpectrogramLayer::getXBinRange(View *v, int x, float &s0, float &s1) const
Chris@0 1278 {
Chris@21 1279 size_t modelStart = m_model->getStartFrame();
Chris@21 1280 size_t modelEnd = m_model->getEndFrame();
Chris@0 1281
Chris@0 1282 // Each pixel column covers an exact range of sample frames:
Chris@44 1283 int f0 = v->getFrameForX(x) - modelStart;
Chris@44 1284 int f1 = v->getFrameForX(x + 1) - modelStart - 1;
Chris@20 1285
Chris@41 1286 if (f1 < int(modelStart) || f0 > int(modelEnd)) {
Chris@41 1287 return false;
Chris@41 1288 }
Chris@20 1289
Chris@0 1290 // And that range may be drawn from a possibly non-integral
Chris@0 1291 // range of spectrogram windows:
Chris@0 1292
Chris@0 1293 size_t windowIncrement = getWindowIncrement();
Chris@0 1294 s0 = float(f0) / windowIncrement;
Chris@0 1295 s1 = float(f1) / windowIncrement;
Chris@0 1296
Chris@0 1297 return true;
Chris@0 1298 }
Chris@0 1299
Chris@0 1300 bool
Chris@44 1301 SpectrogramLayer::getXBinSourceRange(View *v, int x, RealTime &min, RealTime &max) const
Chris@0 1302 {
Chris@0 1303 float s0 = 0, s1 = 0;
Chris@44 1304 if (!getXBinRange(v, x, s0, s1)) return false;
Chris@0 1305
Chris@0 1306 int s0i = int(s0 + 0.001);
Chris@0 1307 int s1i = int(s1);
Chris@0 1308
Chris@0 1309 int windowIncrement = getWindowIncrement();
Chris@0 1310 int w0 = s0i * windowIncrement - (m_windowSize - windowIncrement)/2;
Chris@0 1311 int w1 = s1i * windowIncrement + windowIncrement +
Chris@0 1312 (m_windowSize - windowIncrement)/2 - 1;
Chris@0 1313
Chris@0 1314 min = RealTime::frame2RealTime(w0, m_model->getSampleRate());
Chris@0 1315 max = RealTime::frame2RealTime(w1, m_model->getSampleRate());
Chris@0 1316 return true;
Chris@0 1317 }
Chris@0 1318
Chris@0 1319 bool
Chris@44 1320 SpectrogramLayer::getYBinSourceRange(View *v, int y, float &freqMin, float &freqMax)
Chris@0 1321 const
Chris@0 1322 {
Chris@0 1323 float q0 = 0, q1 = 0;
Chris@44 1324 if (!getYBinRange(v, y, q0, q1)) return false;
Chris@0 1325
Chris@0 1326 int q0i = int(q0 + 0.001);
Chris@0 1327 int q1i = int(q1);
Chris@0 1328
Chris@0 1329 int sr = m_model->getSampleRate();
Chris@0 1330
Chris@0 1331 for (int q = q0i; q <= q1i; ++q) {
Chris@121 1332 if (q == q0i) freqMin = (sr * q) / m_fftSize;
Chris@121 1333 if (q == q1i) freqMax = (sr * (q+1)) / m_fftSize;
Chris@0 1334 }
Chris@0 1335 return true;
Chris@0 1336 }
Chris@35 1337
Chris@35 1338 bool
Chris@44 1339 SpectrogramLayer::getAdjustedYBinSourceRange(View *v, int x, int y,
Chris@35 1340 float &freqMin, float &freqMax,
Chris@35 1341 float &adjFreqMin, float &adjFreqMax)
Chris@35 1342 const
Chris@35 1343 {
Chris@130 1344 FFTModel *fft = getFFTModel(v);
Chris@114 1345 if (!fft) return false;
Chris@110 1346
Chris@35 1347 float s0 = 0, s1 = 0;
Chris@44 1348 if (!getXBinRange(v, x, s0, s1)) return false;
Chris@35 1349
Chris@35 1350 float q0 = 0, q1 = 0;
Chris@44 1351 if (!getYBinRange(v, y, q0, q1)) return false;
Chris@35 1352
Chris@35 1353 int s0i = int(s0 + 0.001);
Chris@35 1354 int s1i = int(s1);
Chris@35 1355
Chris@35 1356 int q0i = int(q0 + 0.001);
Chris@35 1357 int q1i = int(q1);
Chris@35 1358
Chris@35 1359 int sr = m_model->getSampleRate();
Chris@35 1360
Chris@38 1361 size_t windowSize = m_windowSize;
Chris@38 1362 size_t windowIncrement = getWindowIncrement();
Chris@38 1363
Chris@35 1364 bool haveAdj = false;
Chris@35 1365
Chris@37 1366 bool peaksOnly = (m_binDisplay == PeakBins ||
Chris@37 1367 m_binDisplay == PeakFrequencies);
Chris@37 1368
Chris@35 1369 for (int q = q0i; q <= q1i; ++q) {
Chris@35 1370
Chris@35 1371 for (int s = s0i; s <= s1i; ++s) {
Chris@35 1372
Chris@160 1373 if (!fft->isColumnAvailable(s)) continue;
Chris@117 1374
Chris@35 1375 float binfreq = (sr * q) / m_windowSize;
Chris@35 1376 if (q == q0i) freqMin = binfreq;
Chris@35 1377 if (q == q1i) freqMax = binfreq;
Chris@37 1378
Chris@114 1379 if (peaksOnly && !fft->isLocalPeak(s, q)) continue;
Chris@38 1380
Chris@114 1381 if (!fft->isOverThreshold(s, q, m_threshold)) continue;
Chris@38 1382
Chris@38 1383 float freq = binfreq;
Chris@38 1384 bool steady = false;
Chris@40 1385
Chris@114 1386 if (s < int(fft->getWidth()) - 1) {
Chris@38 1387
Chris@38 1388 freq = calculateFrequency(q,
Chris@38 1389 windowSize,
Chris@38 1390 windowIncrement,
Chris@38 1391 sr,
Chris@114 1392 fft->getPhaseAt(s, q),
Chris@114 1393 fft->getPhaseAt(s+1, q),
Chris@38 1394 steady);
Chris@35 1395
Chris@38 1396 if (!haveAdj || freq < adjFreqMin) adjFreqMin = freq;
Chris@38 1397 if (!haveAdj || freq > adjFreqMax) adjFreqMax = freq;
Chris@35 1398
Chris@35 1399 haveAdj = true;
Chris@35 1400 }
Chris@35 1401 }
Chris@35 1402 }
Chris@35 1403
Chris@35 1404 if (!haveAdj) {
Chris@40 1405 adjFreqMin = adjFreqMax = 0.0;
Chris@35 1406 }
Chris@35 1407
Chris@35 1408 return haveAdj;
Chris@35 1409 }
Chris@0 1410
Chris@0 1411 bool
Chris@44 1412 SpectrogramLayer::getXYBinSourceRange(View *v, int x, int y,
Chris@38 1413 float &min, float &max,
Chris@38 1414 float &phaseMin, float &phaseMax) const
Chris@0 1415 {
Chris@0 1416 float q0 = 0, q1 = 0;
Chris@44 1417 if (!getYBinRange(v, y, q0, q1)) return false;
Chris@0 1418
Chris@0 1419 float s0 = 0, s1 = 0;
Chris@44 1420 if (!getXBinRange(v, x, s0, s1)) return false;
Chris@0 1421
Chris@0 1422 int q0i = int(q0 + 0.001);
Chris@0 1423 int q1i = int(q1);
Chris@0 1424
Chris@0 1425 int s0i = int(s0 + 0.001);
Chris@0 1426 int s1i = int(s1);
Chris@0 1427
Chris@37 1428 bool rv = false;
Chris@37 1429
Chris@122 1430 size_t zp = getZeroPadLevel(v);
Chris@122 1431 q0i *= zp + 1;
Chris@122 1432 q1i *= zp + 1;
Chris@122 1433
Chris@130 1434 FFTModel *fft = getFFTModel(v);
Chris@0 1435
Chris@114 1436 if (fft) {
Chris@114 1437
Chris@114 1438 int cw = fft->getWidth();
Chris@114 1439 int ch = fft->getHeight();
Chris@0 1440
Chris@110 1441 min = 0.0;
Chris@110 1442 max = 0.0;
Chris@110 1443 phaseMin = 0.0;
Chris@110 1444 phaseMax = 0.0;
Chris@110 1445 bool have = false;
Chris@0 1446
Chris@110 1447 for (int q = q0i; q <= q1i; ++q) {
Chris@110 1448 for (int s = s0i; s <= s1i; ++s) {
Chris@110 1449 if (s >= 0 && q >= 0 && s < cw && q < ch) {
Chris@117 1450
Chris@160 1451 if (!fft->isColumnAvailable(s)) continue;
Chris@110 1452
Chris@110 1453 float value;
Chris@38 1454
Chris@114 1455 value = fft->getPhaseAt(s, q);
Chris@110 1456 if (!have || value < phaseMin) { phaseMin = value; }
Chris@110 1457 if (!have || value > phaseMax) { phaseMax = value; }
Chris@91 1458
Chris@114 1459 value = fft->getMagnitudeAt(s, q);
Chris@110 1460 if (!have || value < min) { min = value; }
Chris@110 1461 if (!have || value > max) { max = value; }
Chris@110 1462
Chris@110 1463 have = true;
Chris@110 1464 }
Chris@110 1465 }
Chris@110 1466 }
Chris@110 1467
Chris@110 1468 if (have) {
Chris@110 1469 rv = true;
Chris@110 1470 }
Chris@0 1471 }
Chris@0 1472
Chris@37 1473 return rv;
Chris@0 1474 }
Chris@0 1475
Chris@114 1476 size_t
Chris@114 1477 SpectrogramLayer::getZeroPadLevel(const View *v) const
Chris@114 1478 {
Chris@114 1479 //!!! tidy all this stuff
Chris@114 1480
Chris@114 1481 if (m_binDisplay != AllBins) return 0;
Chris@118 1482 if (!Preferences::getInstance()->getSmoothSpectrogram()) return 0;
Chris@114 1483 if (m_frequencyScale == LogFrequencyScale) return 3;
Chris@114 1484
Chris@114 1485 int sr = m_model->getSampleRate();
Chris@114 1486
Chris@184 1487 size_t maxbin = m_fftSize / 2;
Chris@114 1488 if (m_maxFrequency > 0) {
Chris@184 1489 maxbin = int((double(m_maxFrequency) * m_fftSize) / sr + 0.1);
Chris@184 1490 if (maxbin > m_fftSize / 2) maxbin = m_fftSize / 2;
Chris@114 1491 }
Chris@114 1492
Chris@114 1493 size_t minbin = 1;
Chris@114 1494 if (m_minFrequency > 0) {
Chris@114 1495 minbin = int((double(m_minFrequency) * m_fftSize) / sr + 0.1);
Chris@114 1496 if (minbin < 1) minbin = 1;
Chris@184 1497 if (minbin >= maxbin) minbin = maxbin - 1;
Chris@114 1498 }
Chris@114 1499
Chris@118 1500 float perPixel =
Chris@118 1501 float(v->height()) /
Chris@184 1502 float((maxbin - minbin) / (m_zeroPadLevel + 1));
Chris@118 1503
Chris@118 1504 if (perPixel > 2.8) {
Chris@118 1505 return 3; // 4x oversampling
Chris@118 1506 } else if (perPixel > 1.5) {
Chris@118 1507 return 1; // 2x
Chris@114 1508 } else {
Chris@118 1509 return 0; // 1x
Chris@114 1510 }
Chris@114 1511 }
Chris@114 1512
Chris@114 1513 size_t
Chris@114 1514 SpectrogramLayer::getFFTSize(const View *v) const
Chris@114 1515 {
Chris@114 1516 return m_fftSize * (getZeroPadLevel(v) + 1);
Chris@114 1517 }
Chris@114 1518
Chris@130 1519 FFTModel *
Chris@130 1520 SpectrogramLayer::getFFTModel(const View *v) const
Chris@114 1521 {
Chris@114 1522 if (!m_model) return 0;
Chris@114 1523
Chris@114 1524 size_t fftSize = getFFTSize(v);
Chris@114 1525
Chris@130 1526 if (m_fftModels.find(v) != m_fftModels.end()) {
Chris@184 1527 if (m_fftModels[v].first == 0) {
Chris@184 1528 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@184 1529 std::cerr << "SpectrogramLayer::getFFTModel(" << v << "): Found null model" << std::endl;
Chris@184 1530 #endif
Chris@184 1531 return 0;
Chris@184 1532 }
Chris@184 1533 if (m_fftModels[v].first->getHeight() != fftSize / 2 + 1) {
Chris@184 1534 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@184 1535 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 1536 #endif
Chris@130 1537 delete m_fftModels[v].first;
Chris@130 1538 m_fftModels.erase(v);
Chris@184 1539 } else {
Chris@184 1540 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@187 1541 std::cerr << "SpectrogramLayer::getFFTModel(" << v << "): Found a good model of height " << m_fftModels[v].first->getHeight() << std::endl;
Chris@184 1542 #endif
Chris@184 1543 return m_fftModels[v].first;
Chris@114 1544 }
Chris@114 1545 }
Chris@114 1546
Chris@130 1547 if (m_fftModels.find(v) == m_fftModels.end()) {
Chris@169 1548
Chris@169 1549 FFTModel *model = new FFTModel(m_model,
Chris@169 1550 m_channel,
Chris@169 1551 m_windowType,
Chris@169 1552 m_windowSize,
Chris@169 1553 getWindowIncrement(),
Chris@169 1554 fftSize,
Chris@169 1555 true,
Chris@169 1556 m_candidateFillStartFrame);
Chris@169 1557
Chris@178 1558 if (!model->isOK()) {
Chris@178 1559 QMessageBox::critical
Chris@178 1560 (0, tr("FFT cache failed"),
Chris@178 1561 tr("Failed to create the FFT model for this spectrogram.\n"
Chris@178 1562 "There may be insufficient memory or disc space to continue."));
Chris@178 1563 delete model;
Chris@178 1564 m_fftModels[v] = FFTFillPair(0, 0);
Chris@178 1565 return 0;
Chris@178 1566 }
Chris@178 1567
Chris@193 1568 if (!m_sliceableModel) {
Chris@193 1569 std::cerr << "SpectrogramLayer: emitting sliceableModelReplaced(0, " << model << ")" << std::endl;
Chris@193 1570 ((SpectrogramLayer *)this)->sliceableModelReplaced(0, model);
Chris@193 1571 m_sliceableModel = model;
Chris@193 1572 }
Chris@193 1573
Chris@169 1574 m_fftModels[v] = FFTFillPair(model, 0);
Chris@169 1575
Chris@169 1576 model->resume();
Chris@114 1577
Chris@114 1578 delete m_updateTimer;
Chris@114 1579 m_updateTimer = new QTimer((SpectrogramLayer *)this);
Chris@114 1580 connect(m_updateTimer, SIGNAL(timeout()),
Chris@114 1581 this, SLOT(fillTimerTimedOut()));
Chris@114 1582 m_updateTimer->start(200);
Chris@114 1583 }
Chris@114 1584
Chris@130 1585 return m_fftModels[v].first;
Chris@114 1586 }
Chris@114 1587
Chris@193 1588 const Model *
Chris@193 1589 SpectrogramLayer::getSliceableModel() const
Chris@193 1590 {
Chris@193 1591 if (m_sliceableModel) return m_sliceableModel;
Chris@193 1592 if (m_fftModels.empty()) return 0;
Chris@193 1593 m_sliceableModel = m_fftModels.begin()->second.first;
Chris@193 1594 return m_sliceableModel;
Chris@193 1595 }
Chris@193 1596
Chris@114 1597 void
Chris@130 1598 SpectrogramLayer::invalidateFFTModels()
Chris@114 1599 {
Chris@130 1600 for (ViewFFTMap::iterator i = m_fftModels.begin();
Chris@130 1601 i != m_fftModels.end(); ++i) {
Chris@115 1602 delete i->second.first;
Chris@114 1603 }
Chris@114 1604
Chris@130 1605 m_fftModels.clear();
Chris@193 1606
Chris@193 1607 if (m_sliceableModel) {
Chris@193 1608 std::cerr << "SpectrogramLayer: emitting sliceableModelReplaced(" << m_sliceableModel << ", 0)" << std::endl;
Chris@193 1609 emit sliceableModelReplaced(m_sliceableModel, 0);
Chris@193 1610 m_sliceableModel = 0;
Chris@193 1611 }
Chris@114 1612 }
Chris@114 1613
Chris@0 1614 void
Chris@119 1615 SpectrogramLayer::invalidateMagnitudes()
Chris@119 1616 {
Chris@119 1617 m_viewMags.clear();
Chris@119 1618 for (std::vector<MagnitudeRange>::iterator i = m_columnMags.begin();
Chris@119 1619 i != m_columnMags.end(); ++i) {
Chris@119 1620 *i = MagnitudeRange();
Chris@119 1621 }
Chris@119 1622 }
Chris@119 1623
Chris@119 1624 bool
Chris@119 1625 SpectrogramLayer::updateViewMagnitudes(View *v) const
Chris@119 1626 {
Chris@119 1627 MagnitudeRange mag;
Chris@119 1628
Chris@119 1629 int x0 = 0, x1 = v->width();
Chris@119 1630 float s00 = 0, s01 = 0, s10 = 0, s11 = 0;
Chris@119 1631
Chris@203 1632 if (!getXBinRange(v, x0, s00, s01)) {
Chris@203 1633 s00 = s01 = m_model->getStartFrame() / getWindowIncrement();
Chris@203 1634 }
Chris@203 1635
Chris@203 1636 if (!getXBinRange(v, x1, s10, s11)) {
Chris@203 1637 s10 = s11 = m_model->getEndFrame() / getWindowIncrement();
Chris@203 1638 }
Chris@119 1639
Chris@119 1640 int s0 = int(std::min(s00, s10) + 0.0001);
Chris@203 1641 int s1 = int(std::max(s01, s11) + 0.0001);
Chris@203 1642
Chris@203 1643 // std::cerr << "SpectrogramLayer::updateViewMagnitudes: x0 = " << x0 << ", x1 = " << x1 << ", s00 = " << s00 << ", s11 = " << s11 << " s0 = " << s0 << ", s1 = " << s1 << std::endl;
Chris@119 1644
Chris@119 1645 if (m_columnMags.size() <= s1) {
Chris@119 1646 m_columnMags.resize(s1 + 1);
Chris@119 1647 }
Chris@119 1648
Chris@119 1649 for (int s = s0; s <= s1; ++s) {
Chris@119 1650 if (m_columnMags[s].isSet()) {
Chris@119 1651 mag.sample(m_columnMags[s]);
Chris@119 1652 }
Chris@119 1653 }
Chris@119 1654
Chris@184 1655 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@119 1656 std::cerr << "SpectrogramLayer::updateViewMagnitudes returning from cols "
Chris@119 1657 << s0 << " -> " << s1 << " inclusive" << std::endl;
Chris@184 1658 #endif
Chris@119 1659
Chris@119 1660 if (!mag.isSet()) return false;
Chris@119 1661 if (mag == m_viewMags[v]) return false;
Chris@119 1662 m_viewMags[v] = mag;
Chris@119 1663 return true;
Chris@119 1664 }
Chris@119 1665
Chris@119 1666 void
Chris@44 1667 SpectrogramLayer::paint(View *v, QPainter &paint, QRect rect) const
Chris@0 1668 {
Chris@197 1669 if (m_colourMap == (int)ColourMapper::BlackOnWhite) {
Chris@55 1670 v->setLightBackground(true);
Chris@55 1671 } else {
Chris@55 1672 v->setLightBackground(false);
Chris@55 1673 }
Chris@55 1674
Chris@161 1675 Profiler profiler("SpectrogramLayer::paint", true);
Chris@0 1676 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@95 1677 std::cerr << "SpectrogramLayer::paint(): m_model is " << m_model << ", zoom level is " << v->getZoomLevel() << ", m_updateTimer " << m_updateTimer << std::endl;
Chris@95 1678
Chris@95 1679 std::cerr << "rect is " << rect.x() << "," << rect.y() << " " << rect.width() << "x" << rect.height() << std::endl;
Chris@0 1680 #endif
Chris@95 1681
Chris@133 1682 long startFrame = v->getStartFrame();
Chris@133 1683 if (startFrame < 0) m_candidateFillStartFrame = 0;
Chris@133 1684 else m_candidateFillStartFrame = startFrame;
Chris@44 1685
Chris@0 1686 if (!m_model || !m_model->isOK() || !m_model->isReady()) {
Chris@0 1687 return;
Chris@0 1688 }
Chris@0 1689
Chris@47 1690 if (isLayerDormant(v)) {
Chris@48 1691 std::cerr << "SpectrogramLayer::paint(): Layer is dormant, making it undormant again" << std::endl;
Chris@29 1692 }
Chris@29 1693
Chris@48 1694 // Need to do this even if !isLayerDormant, as that could mean v
Chris@48 1695 // is not in the dormancy map at all -- we need it to be present
Chris@48 1696 // and accountable for when determining whether we need the cache
Chris@48 1697 // in the cache-fill thread above.
Chris@131 1698 //!!! no longer use cache-fill thread
Chris@131 1699 const_cast<SpectrogramLayer *>(this)->Layer::setLayerDormant(v, false);
Chris@48 1700
Chris@114 1701 size_t fftSize = getFFTSize(v);
Chris@130 1702 FFTModel *fft = getFFTModel(v);
Chris@114 1703 if (!fft) {
Chris@130 1704 std::cerr << "ERROR: SpectrogramLayer::paint(): No FFT model, returning" << std::endl;
Chris@0 1705 return;
Chris@0 1706 }
Chris@0 1707
Chris@95 1708 PixmapCache &cache = m_pixmapCaches[v];
Chris@95 1709
Chris@95 1710 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@95 1711 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 1712 #endif
Chris@95 1713
Chris@0 1714 bool stillCacheing = (m_updateTimer != 0);
Chris@0 1715
Chris@0 1716 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@0 1717 std::cerr << "SpectrogramLayer::paint(): Still cacheing = " << stillCacheing << std::endl;
Chris@0 1718 #endif
Chris@0 1719
Chris@44 1720 int zoomLevel = v->getZoomLevel();
Chris@0 1721
Chris@0 1722 int x0 = 0;
Chris@44 1723 int x1 = v->width();
Chris@0 1724 int y0 = 0;
Chris@44 1725 int y1 = v->height();
Chris@0 1726
Chris@0 1727 bool recreateWholePixmapCache = true;
Chris@0 1728
Chris@95 1729 x0 = rect.left();
Chris@95 1730 x1 = rect.right() + 1;
Chris@95 1731 y0 = rect.top();
Chris@95 1732 y1 = rect.bottom() + 1;
Chris@95 1733
Chris@95 1734 if (cache.validArea.width() > 0) {
Chris@95 1735
Chris@95 1736 if (int(cache.zoomLevel) == zoomLevel &&
Chris@95 1737 cache.pixmap.width() == v->width() &&
Chris@95 1738 cache.pixmap.height() == v->height()) {
Chris@95 1739
Chris@95 1740 if (v->getXForFrame(cache.startFrame) ==
Chris@95 1741 v->getXForFrame(startFrame) &&
Chris@95 1742 cache.validArea.x() <= x0 &&
Chris@95 1743 cache.validArea.x() + cache.validArea.width() >= x1) {
Chris@0 1744
Chris@0 1745 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@0 1746 std::cerr << "SpectrogramLayer: pixmap cache good" << std::endl;
Chris@0 1747 #endif
Chris@0 1748
Chris@95 1749 paint.drawPixmap(rect, cache.pixmap, rect);
Chris@121 1750 illuminateLocalFeatures(v, paint);
Chris@0 1751 return;
Chris@0 1752
Chris@0 1753 } else {
Chris@0 1754
Chris@0 1755 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@0 1756 std::cerr << "SpectrogramLayer: pixmap cache partially OK" << std::endl;
Chris@0 1757 #endif
Chris@0 1758
Chris@0 1759 recreateWholePixmapCache = false;
Chris@0 1760
Chris@95 1761 int dx = v->getXForFrame(cache.startFrame) -
Chris@44 1762 v->getXForFrame(startFrame);
Chris@0 1763
Chris@0 1764 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@95 1765 std::cerr << "SpectrogramLayer: dx = " << dx << " (pixmap cache " << cache.pixmap.width() << "x" << cache.pixmap.height() << ")" << std::endl;
Chris@0 1766 #endif
Chris@0 1767
Chris@95 1768 if (dx != 0 &&
Chris@95 1769 dx > -cache.pixmap.width() &&
Chris@95 1770 dx < cache.pixmap.width()) {
Chris@0 1771
Chris@0 1772 #if defined(Q_WS_WIN32) || defined(Q_WS_MAC)
Chris@0 1773 // Copying a pixmap to itself doesn't work
Chris@0 1774 // properly on Windows or Mac (it only works when
Chris@0 1775 // moving in one direction).
Chris@0 1776
Chris@0 1777 //!!! Need a utility function for this
Chris@0 1778
Chris@0 1779 static QPixmap *tmpPixmap = 0;
Chris@0 1780 if (!tmpPixmap ||
Chris@95 1781 tmpPixmap->width() != cache.pixmap.width() ||
Chris@95 1782 tmpPixmap->height() != cache.pixmap.height()) {
Chris@0 1783 delete tmpPixmap;
Chris@95 1784 tmpPixmap = new QPixmap(cache.pixmap.width(),
Chris@95 1785 cache.pixmap.height());
Chris@0 1786 }
Chris@0 1787 QPainter cachePainter;
Chris@0 1788 cachePainter.begin(tmpPixmap);
Chris@95 1789 cachePainter.drawPixmap(0, 0, cache.pixmap);
Chris@0 1790 cachePainter.end();
Chris@95 1791 cachePainter.begin(&cache.pixmap);
Chris@0 1792 cachePainter.drawPixmap(dx, 0, *tmpPixmap);
Chris@0 1793 cachePainter.end();
Chris@0 1794 #else
Chris@95 1795 QPainter cachePainter(&cache.pixmap);
Chris@95 1796 cachePainter.drawPixmap(dx, 0, cache.pixmap);
Chris@0 1797 cachePainter.end();
Chris@0 1798 #endif
Chris@0 1799
Chris@95 1800 int px = cache.validArea.x();
Chris@95 1801 int pw = cache.validArea.width();
Chris@0 1802
Chris@0 1803 if (dx < 0) {
Chris@95 1804 x0 = cache.pixmap.width() + dx;
Chris@95 1805 x1 = cache.pixmap.width();
Chris@95 1806 px += dx;
Chris@95 1807 if (px < 0) {
Chris@95 1808 pw += px;
Chris@95 1809 px = 0;
Chris@95 1810 if (pw < 0) pw = 0;
Chris@95 1811 }
Chris@0 1812 } else {
Chris@0 1813 x0 = 0;
Chris@0 1814 x1 = dx;
Chris@95 1815 px += dx;
Chris@95 1816 if (px + pw > cache.pixmap.width()) {
Chris@95 1817 pw = int(cache.pixmap.width()) - px;
Chris@95 1818 if (pw < 0) pw = 0;
Chris@95 1819 }
Chris@0 1820 }
Chris@95 1821
Chris@95 1822 cache.validArea =
Chris@95 1823 QRect(px, cache.validArea.y(),
Chris@95 1824 pw, cache.validArea.height());
Chris@95 1825
Chris@95 1826 paint.drawPixmap(rect & cache.validArea,
Chris@95 1827 cache.pixmap,
Chris@95 1828 rect & cache.validArea);
Chris@0 1829 }
Chris@0 1830 }
Chris@0 1831 } else {
Chris@0 1832 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@0 1833 std::cerr << "SpectrogramLayer: pixmap cache useless" << std::endl;
Chris@0 1834 #endif
Chris@95 1835 cache.validArea = QRect();
Chris@0 1836 }
Chris@0 1837 }
Chris@95 1838
Chris@92 1839 /*
Chris@0 1840 if (stillCacheing) {
Chris@0 1841 x0 = rect.left();
Chris@0 1842 x1 = rect.right() + 1;
Chris@0 1843 y0 = rect.top();
Chris@0 1844 y1 = rect.bottom() + 1;
Chris@0 1845 }
Chris@92 1846 */
Chris@95 1847
Chris@133 1848 if (updateViewMagnitudes(v)) {
Chris@184 1849 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@133 1850 std::cerr << "SpectrogramLayer: magnitude range changed to [" << m_viewMags[v].getMin() << "->" << m_viewMags[v].getMax() << "]" << std::endl;
Chris@184 1851 #endif
Chris@133 1852 recreateWholePixmapCache = true;
Chris@133 1853 } else {
Chris@184 1854 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@133 1855 std::cerr << "No change in magnitude range [" << m_viewMags[v].getMin() << "->" << m_viewMags[v].getMax() << "]" << std::endl;
Chris@184 1856 #endif
Chris@133 1857 }
Chris@133 1858
Chris@95 1859 if (recreateWholePixmapCache) {
Chris@95 1860 x0 = 0;
Chris@95 1861 x1 = v->width();
Chris@95 1862 }
Chris@95 1863
Chris@188 1864 //!!! This width should really depend on how fast the machine is
Chris@188 1865 //at redrawing the spectrogram. We could fairly easily time that,
Chris@188 1866 //in this function, and adjust accordingly. The following is
Chris@188 1867 //probably about as small as the block width should go.
Chris@96 1868 int paintBlockWidth = (300000 / zoomLevel);
Chris@96 1869 if (paintBlockWidth < 20) paintBlockWidth = 20;
Chris@96 1870
Chris@96 1871 if (cache.validArea.width() > 0) {
Chris@96 1872
Chris@96 1873 int vx0 = 0, vx1 = 0;
Chris@96 1874 vx0 = cache.validArea.x();
Chris@96 1875 vx1 = cache.validArea.x() + cache.validArea.width();
Chris@96 1876
Chris@96 1877 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@96 1878 std::cerr << "x0 " << x0 << ", x1 " << x1 << ", vx0 " << vx0 << ", vx1 " << vx1 << ", paintBlockWidth " << paintBlockWidth << std::endl;
Chris@96 1879 #endif
Chris@96 1880 if (x0 < vx0) {
Chris@96 1881 if (x0 + paintBlockWidth < vx0) {
Chris@96 1882 x0 = vx0 - paintBlockWidth;
Chris@96 1883 } else {
Chris@96 1884 x0 = 0;
Chris@96 1885 }
Chris@96 1886 } else if (x0 > vx1) {
Chris@96 1887 x0 = vx1;
Chris@96 1888 }
Chris@95 1889
Chris@96 1890 if (x1 < vx0) {
Chris@96 1891 x1 = vx0;
Chris@96 1892 } else if (x1 > vx1) {
Chris@96 1893 if (vx1 + paintBlockWidth < x1) {
Chris@96 1894 x1 = vx1 + paintBlockWidth;
Chris@96 1895 } else {
Chris@96 1896 x1 = v->width();
Chris@95 1897 }
Chris@96 1898 }
Chris@95 1899
Chris@96 1900 cache.validArea = QRect
Chris@96 1901 (std::min(vx0, x0), cache.validArea.y(),
Chris@96 1902 std::max(vx1 - std::min(vx0, x0),
Chris@96 1903 x1 - std::min(vx0, x0)),
Chris@96 1904 cache.validArea.height());
Chris@95 1905
Chris@96 1906 } else {
Chris@96 1907 if (x1 > x0 + paintBlockWidth) {
Chris@133 1908 int sfx = x1;
Chris@133 1909 if (startFrame < 0) sfx = v->getXForFrame(0);
Chris@133 1910 if (sfx >= x0 && sfx + paintBlockWidth <= x1) {
Chris@133 1911 x0 = sfx;
Chris@133 1912 x1 = x0 + paintBlockWidth;
Chris@133 1913 } else {
Chris@133 1914 int mid = (x1 + x0) / 2;
Chris@133 1915 x0 = mid - paintBlockWidth/2;
Chris@133 1916 x1 = x0 + paintBlockWidth;
Chris@133 1917 }
Chris@95 1918 }
Chris@96 1919 cache.validArea = QRect(x0, 0, x1 - x0, v->height());
Chris@95 1920 }
Chris@95 1921
Chris@0 1922 int w = x1 - x0;
Chris@0 1923 int h = y1 - y0;
Chris@0 1924
Chris@95 1925 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@95 1926 std::cerr << "x0 " << x0 << ", x1 " << x1 << ", w " << w << ", h " << h << std::endl;
Chris@95 1927 #endif
Chris@95 1928
Chris@95 1929 if (m_drawBuffer.width() < w || m_drawBuffer.height() < h) {
Chris@95 1930 m_drawBuffer = QImage(w, h, QImage::Format_RGB32);
Chris@95 1931 }
Chris@95 1932
Chris@197 1933 m_drawBuffer.fill(m_palette.getColour(0).rgb());
Chris@35 1934
Chris@37 1935 int sr = m_model->getSampleRate();
Chris@122 1936
Chris@122 1937 // Set minFreq and maxFreq to the frequency extents of the possibly
Chris@122 1938 // zero-padded visible bin range, and displayMinFreq and displayMaxFreq
Chris@122 1939 // to the actual scale frequency extents (presumably not zero padded).
Chris@35 1940
Chris@184 1941 size_t maxbin = fftSize / 2;
Chris@35 1942 if (m_maxFrequency > 0) {
Chris@184 1943 maxbin = int((double(m_maxFrequency) * fftSize) / sr + 0.1);
Chris@184 1944 if (maxbin > fftSize / 2) maxbin = fftSize / 2;
Chris@35 1945 }
Chris@111 1946
Chris@40 1947 size_t minbin = 1;
Chris@37 1948 if (m_minFrequency > 0) {
Chris@114 1949 minbin = int((double(m_minFrequency) * fftSize) / sr + 0.1);
Chris@40 1950 if (minbin < 1) minbin = 1;
Chris@184 1951 if (minbin >= maxbin) minbin = maxbin - 1;
Chris@37 1952 }
Chris@37 1953
Chris@114 1954 float minFreq = (float(minbin) * sr) / fftSize;
Chris@184 1955 float maxFreq = (float(maxbin) * sr) / fftSize;
Chris@0 1956
Chris@122 1957 float displayMinFreq = minFreq;
Chris@122 1958 float displayMaxFreq = maxFreq;
Chris@122 1959
Chris@122 1960 if (fftSize != m_fftSize) {
Chris@122 1961 displayMinFreq = getEffectiveMinFrequency();
Chris@122 1962 displayMaxFreq = getEffectiveMaxFrequency();
Chris@122 1963 }
Chris@122 1964
Chris@92 1965 float ymag[h];
Chris@92 1966 float ydiv[h];
Chris@184 1967 float yval[maxbin + 1]; //!!! cache this?
Chris@92 1968
Chris@38 1969 size_t increment = getWindowIncrement();
Chris@40 1970
Chris@40 1971 bool logarithmic = (m_frequencyScale == LogFrequencyScale);
Chris@38 1972
Chris@184 1973 for (size_t q = minbin; q <= maxbin; ++q) {
Chris@114 1974 float f0 = (float(q) * sr) / fftSize;
Chris@122 1975 yval[q] = v->getYForFrequency(f0, displayMinFreq, displayMaxFreq,
Chris@122 1976 logarithmic);
Chris@122 1977 // std::cerr << "min: " << minFreq << ", max: " << maxFreq << ", yval[" << q << "]: " << yval[q] << std::endl;
Chris@92 1978 }
Chris@92 1979
Chris@119 1980 MagnitudeRange overallMag = m_viewMags[v];
Chris@119 1981 bool overallMagChanged = false;
Chris@119 1982
Chris@162 1983 bool fftSuspended = false;
Chris@131 1984
Chris@137 1985 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@137 1986 std::cerr << (float(v->getFrameForX(1) - v->getFrameForX(0)) / increment) << " bins per pixel" << std::endl;
Chris@137 1987 #endif
Chris@137 1988
Chris@35 1989 for (int x = 0; x < w; ++x) {
Chris@35 1990
Chris@35 1991 for (int y = 0; y < h; ++y) {
Chris@134 1992 ymag[y] = 0.f;
Chris@134 1993 ydiv[y] = 0.f;
Chris@35 1994 }
Chris@35 1995
Chris@35 1996 float s0 = 0, s1 = 0;
Chris@35 1997
Chris@44 1998 if (!getXBinRange(v, x0 + x, s0, s1)) {
Chris@95 1999 assert(x <= m_drawBuffer.width());
Chris@35 2000 continue;
Chris@35 2001 }
Chris@35 2002
Chris@35 2003 int s0i = int(s0 + 0.001);
Chris@35 2004 int s1i = int(s1);
Chris@35 2005
Chris@114 2006 if (s1i >= fft->getWidth()) {
Chris@114 2007 if (s0i >= fft->getWidth()) {
Chris@45 2008 continue;
Chris@45 2009 } else {
Chris@45 2010 s1i = s0i;
Chris@45 2011 }
Chris@45 2012 }
Chris@92 2013
Chris@92 2014 for (int s = s0i; s <= s1i; ++s) {
Chris@92 2015
Chris@160 2016 if (!fft->isColumnAvailable(s)) continue;
Chris@162 2017
Chris@162 2018 if (!fftSuspended) {
Chris@162 2019 fft->suspendWrites();
Chris@162 2020 fftSuspended = true;
Chris@162 2021 }
Chris@162 2022
Chris@119 2023 MagnitudeRange mag;
Chris@92 2024
Chris@184 2025 for (size_t q = minbin; q < maxbin; ++q) {
Chris@92 2026
Chris@92 2027 float y0 = yval[q + 1];
Chris@92 2028 float y1 = yval[q];
Chris@92 2029
Chris@40 2030 if (m_binDisplay == PeakBins ||
Chris@40 2031 m_binDisplay == PeakFrequencies) {
Chris@114 2032 if (!fft->isLocalPeak(s, q)) continue;
Chris@40 2033 }
Chris@114 2034
Chris@114 2035 if (m_threshold != 0.f &&
Chris@114 2036 !fft->isOverThreshold(s, q, m_threshold)) {
Chris@114 2037 continue;
Chris@114 2038 }
Chris@40 2039
Chris@35 2040 float sprop = 1.0;
Chris@35 2041 if (s == s0i) sprop *= (s + 1) - s0;
Chris@35 2042 if (s == s1i) sprop *= s1 - s;
Chris@35 2043
Chris@38 2044 if (m_binDisplay == PeakFrequencies &&
Chris@114 2045 s < int(fft->getWidth()) - 1) {
Chris@35 2046
Chris@38 2047 bool steady = false;
Chris@92 2048 float f = calculateFrequency(q,
Chris@38 2049 m_windowSize,
Chris@38 2050 increment,
Chris@38 2051 sr,
Chris@114 2052 fft->getPhaseAt(s, q),
Chris@114 2053 fft->getPhaseAt(s+1, q),
Chris@38 2054 steady);
Chris@40 2055
Chris@44 2056 y0 = y1 = v->getYForFrequency
Chris@122 2057 (f, displayMinFreq, displayMaxFreq, logarithmic);
Chris@35 2058 }
Chris@38 2059
Chris@35 2060 int y0i = int(y0 + 0.001);
Chris@35 2061 int y1i = int(y1);
Chris@35 2062
Chris@92 2063 float value;
Chris@92 2064
Chris@92 2065 if (m_colourScale == PhaseColourScale) {
Chris@114 2066 value = fft->getPhaseAt(s, q);
Chris@92 2067 } else if (m_normalizeColumns) {
Chris@119 2068 value = fft->getNormalizedMagnitudeAt(s, q);
Chris@119 2069 mag.sample(value);
Chris@119 2070 value *= m_gain;
Chris@92 2071 } else {
Chris@119 2072 value = fft->getMagnitudeAt(s, q);
Chris@119 2073 mag.sample(value);
Chris@119 2074 value *= m_gain;
Chris@92 2075 }
Chris@92 2076
Chris@35 2077 for (int y = y0i; y <= y1i; ++y) {
Chris@35 2078
Chris@35 2079 if (y < 0 || y >= h) continue;
Chris@35 2080
Chris@35 2081 float yprop = sprop;
Chris@35 2082 if (y == y0i) yprop *= (y + 1) - y0;
Chris@35 2083 if (y == y1i) yprop *= y1 - y;
Chris@37 2084 ymag[y] += yprop * value;
Chris@35 2085 ydiv[y] += yprop;
Chris@35 2086 }
Chris@35 2087 }
Chris@119 2088
Chris@119 2089 if (mag.isSet()) {
Chris@119 2090
Chris@203 2091
Chris@203 2092 if (s >= m_columnMags.size()) {
Chris@203 2093 std::cerr << "INTERNAL ERROR: " << s << " >= "
Chris@203 2094 << m_columnMags.size() << " at SpectrogramLayer.cpp:2087" << std::endl;
Chris@203 2095 }
Chris@203 2096
Chris@119 2097 m_columnMags[s].sample(mag);
Chris@119 2098
Chris@119 2099 if (overallMag.sample(mag)) {
Chris@119 2100 //!!! scaling would change here
Chris@119 2101 overallMagChanged = true;
Chris@119 2102 std::cerr << "Overall mag changed (again?) at column " << s << ", to [" << overallMag.getMin() << "->" << overallMag.getMax() << "]" << std::endl;
Chris@119 2103 }
Chris@119 2104 }
Chris@35 2105 }
Chris@35 2106
Chris@35 2107 for (int y = 0; y < h; ++y) {
Chris@35 2108
Chris@35 2109 if (ydiv[y] > 0.0) {
Chris@40 2110
Chris@40 2111 unsigned char pixel = 0;
Chris@40 2112
Chris@38 2113 float avg = ymag[y] / ydiv[y];
Chris@138 2114 pixel = getDisplayValue(v, avg);
Chris@40 2115
Chris@95 2116 assert(x <= m_drawBuffer.width());
Chris@197 2117 QColor c = m_palette.getColour(pixel);
Chris@95 2118 m_drawBuffer.setPixel(x, y,
Chris@95 2119 qRgb(c.red(), c.green(), c.blue()));
Chris@35 2120 }
Chris@35 2121 }
Chris@35 2122 }
Chris@35 2123
Chris@119 2124 if (overallMagChanged) {
Chris@119 2125 m_viewMags[v] = overallMag;
Chris@119 2126 std::cerr << "Overall mag is now [" << m_viewMags[v].getMin() << "->" << m_viewMags[v].getMax() << "] - will be updating" << std::endl;
Chris@119 2127 } else {
Chris@119 2128 std::cerr << "Overall mag unchanged at [" << m_viewMags[v].getMin() << "->" << m_viewMags[v].getMax() << "]" << std::endl;
Chris@119 2129 }
Chris@119 2130
Chris@161 2131 Profiler profiler2("SpectrogramLayer::paint: draw image", true);
Chris@137 2132
Chris@95 2133 paint.drawImage(x0, y0, m_drawBuffer, 0, 0, w, h);
Chris@0 2134
Chris@0 2135 if (recreateWholePixmapCache) {
Chris@95 2136 cache.pixmap = QPixmap(v->width(), v->height());
Chris@0 2137 }
Chris@0 2138
Chris@95 2139 QPainter cachePainter(&cache.pixmap);
Chris@95 2140 cachePainter.drawImage(x0, y0, m_drawBuffer, 0, 0, w, h);
Chris@0 2141 cachePainter.end();
Chris@119 2142
Chris@120 2143 if (!m_normalizeVisibleArea || !overallMagChanged) {
Chris@0 2144
Chris@119 2145 cache.startFrame = startFrame;
Chris@119 2146 cache.zoomLevel = zoomLevel;
Chris@119 2147
Chris@119 2148 if (cache.validArea.x() > 0) {
Chris@95 2149 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@158 2150 std::cerr << "SpectrogramLayer::paint() updating left (0, "
Chris@158 2151 << cache.validArea.x() << ")" << std::endl;
Chris@95 2152 #endif
Chris@119 2153 v->update(0, 0, cache.validArea.x(), v->height());
Chris@119 2154 }
Chris@119 2155
Chris@119 2156 if (cache.validArea.x() + cache.validArea.width() <
Chris@119 2157 cache.pixmap.width()) {
Chris@95 2158 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@119 2159 std::cerr << "SpectrogramLayer::paint() updating right ("
Chris@119 2160 << cache.validArea.x() + cache.validArea.width()
Chris@119 2161 << ", "
Chris@119 2162 << cache.pixmap.width() - (cache.validArea.x() +
Chris@119 2163 cache.validArea.width())
Chris@119 2164 << ")" << std::endl;
Chris@95 2165 #endif
Chris@119 2166 v->update(cache.validArea.x() + cache.validArea.width(),
Chris@119 2167 0,
Chris@119 2168 cache.pixmap.width() - (cache.validArea.x() +
Chris@119 2169 cache.validArea.width()),
Chris@119 2170 v->height());
Chris@119 2171 }
Chris@119 2172 } else {
Chris@119 2173 // overallMagChanged
Chris@119 2174 cache.validArea = QRect();
Chris@119 2175 v->update();
Chris@95 2176 }
Chris@0 2177
Chris@121 2178 illuminateLocalFeatures(v, paint);
Chris@120 2179
Chris@0 2180 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@0 2181 std::cerr << "SpectrogramLayer::paint() returning" << std::endl;
Chris@0 2182 #endif
Chris@131 2183
Chris@162 2184 if (fftSuspended) fft->resume();
Chris@0 2185 }
Chris@0 2186
Chris@121 2187 void
Chris@121 2188 SpectrogramLayer::illuminateLocalFeatures(View *v, QPainter &paint) const
Chris@121 2189 {
Chris@121 2190 QPoint localPos;
Chris@121 2191 if (!v->shouldIlluminateLocalFeatures(this, localPos) || !m_model) {
Chris@121 2192 return;
Chris@121 2193 }
Chris@121 2194
Chris@180 2195 // std::cerr << "SpectrogramLayer: illuminateLocalFeatures("
Chris@180 2196 // << localPos.x() << "," << localPos.y() << ")" << std::endl;
Chris@121 2197
Chris@121 2198 float s0, s1;
Chris@121 2199 float f0, f1;
Chris@121 2200
Chris@121 2201 if (getXBinRange(v, localPos.x(), s0, s1) &&
Chris@121 2202 getYBinSourceRange(v, localPos.y(), f0, f1)) {
Chris@121 2203
Chris@121 2204 int s0i = int(s0 + 0.001);
Chris@121 2205 int s1i = int(s1);
Chris@121 2206
Chris@121 2207 int x0 = v->getXForFrame(s0i * getWindowIncrement());
Chris@121 2208 int x1 = v->getXForFrame((s1i + 1) * getWindowIncrement());
Chris@121 2209
Chris@121 2210 int y1 = getYForFrequency(v, f1);
Chris@121 2211 int y0 = getYForFrequency(v, f0);
Chris@121 2212
Chris@180 2213 // std::cerr << "SpectrogramLayer: illuminate "
Chris@180 2214 // << x0 << "," << y1 << " -> " << x1 << "," << y0 << std::endl;
Chris@121 2215
Chris@121 2216 paint.setPen(Qt::white);
Chris@133 2217
Chris@133 2218 //!!! should we be using paintCrosshairs for this?
Chris@133 2219
Chris@121 2220 paint.drawRect(x0, y1, x1 - x0 + 1, y0 - y1 + 1);
Chris@121 2221 }
Chris@121 2222 }
Chris@121 2223
Chris@42 2224 float
Chris@44 2225 SpectrogramLayer::getYForFrequency(View *v, float frequency) const
Chris@42 2226 {
Chris@44 2227 return v->getYForFrequency(frequency,
Chris@44 2228 getEffectiveMinFrequency(),
Chris@44 2229 getEffectiveMaxFrequency(),
Chris@44 2230 m_frequencyScale == LogFrequencyScale);
Chris@42 2231 }
Chris@42 2232
Chris@42 2233 float
Chris@44 2234 SpectrogramLayer::getFrequencyForY(View *v, int y) const
Chris@42 2235 {
Chris@44 2236 return v->getFrequencyForY(y,
Chris@44 2237 getEffectiveMinFrequency(),
Chris@44 2238 getEffectiveMaxFrequency(),
Chris@44 2239 m_frequencyScale == LogFrequencyScale);
Chris@42 2240 }
Chris@42 2241
Chris@0 2242 int
Chris@115 2243 SpectrogramLayer::getCompletion(View *v) const
Chris@0 2244 {
Chris@115 2245 if (m_updateTimer == 0) return 100;
Chris@130 2246 if (m_fftModels.find(v) == m_fftModels.end()) return 100;
Chris@130 2247
Chris@130 2248 size_t completion = m_fftModels[v].first->getCompletion();
Chris@115 2249 std::cerr << "SpectrogramLayer::getCompletion: completion = " << completion << std::endl;
Chris@0 2250 return completion;
Chris@0 2251 }
Chris@0 2252
Chris@28 2253 bool
Chris@101 2254 SpectrogramLayer::getValueExtents(float &min, float &max,
Chris@101 2255 bool &logarithmic, QString &unit) const
Chris@79 2256 {
Chris@133 2257 //!!!
Chris@133 2258 // min = getEffectiveMinFrequency();
Chris@133 2259 // max = getEffectiveMaxFrequency();
Chris@133 2260
Chris@133 2261 if (!m_model) return false;
Chris@133 2262
Chris@133 2263 int sr = m_model->getSampleRate();
Chris@133 2264 min = float(sr) / m_fftSize;
Chris@133 2265 max = float(sr) / 2;
Chris@133 2266
Chris@101 2267 logarithmic = (m_frequencyScale == LogFrequencyScale);
Chris@79 2268 unit = "Hz";
Chris@79 2269 return true;
Chris@79 2270 }
Chris@79 2271
Chris@79 2272 bool
Chris@101 2273 SpectrogramLayer::getDisplayExtents(float &min, float &max) const
Chris@101 2274 {
Chris@101 2275 min = getEffectiveMinFrequency();
Chris@101 2276 max = getEffectiveMaxFrequency();
Chris@187 2277 std::cerr << "SpectrogramLayer::getDisplayExtents: " << min << "->" << max << std::endl;
Chris@101 2278 return true;
Chris@101 2279 }
Chris@101 2280
Chris@101 2281 bool
Chris@120 2282 SpectrogramLayer::setDisplayExtents(float min, float max)
Chris@120 2283 {
Chris@120 2284 if (!m_model) return false;
Chris@187 2285
Chris@187 2286 std::cerr << "SpectrogramLayer::setDisplayExtents: " << min << "->" << max << std::endl;
Chris@187 2287
Chris@120 2288 if (min < 0) min = 0;
Chris@120 2289 if (max > m_model->getSampleRate()/2) max = m_model->getSampleRate()/2;
Chris@120 2290
Chris@120 2291 size_t minf = lrintf(min);
Chris@120 2292 size_t maxf = lrintf(max);
Chris@120 2293
Chris@120 2294 if (m_minFrequency == minf && m_maxFrequency == maxf) return true;
Chris@120 2295
Chris@120 2296 invalidatePixmapCaches();
Chris@120 2297 invalidateMagnitudes();
Chris@120 2298
Chris@120 2299 m_minFrequency = minf;
Chris@120 2300 m_maxFrequency = maxf;
Chris@120 2301
Chris@120 2302 emit layerParametersChanged();
Chris@120 2303
Chris@133 2304 int vs = getCurrentVerticalZoomStep();
Chris@133 2305 if (vs != m_lastEmittedZoomStep) {
Chris@133 2306 emit verticalZoomChanged();
Chris@133 2307 m_lastEmittedZoomStep = vs;
Chris@133 2308 }
Chris@133 2309
Chris@120 2310 return true;
Chris@120 2311 }
Chris@120 2312
Chris@120 2313 bool
Chris@44 2314 SpectrogramLayer::snapToFeatureFrame(View *v, int &frame,
Chris@28 2315 size_t &resolution,
Chris@28 2316 SnapType snap) const
Chris@13 2317 {
Chris@13 2318 resolution = getWindowIncrement();
Chris@28 2319 int left = (frame / resolution) * resolution;
Chris@28 2320 int right = left + resolution;
Chris@28 2321
Chris@28 2322 switch (snap) {
Chris@28 2323 case SnapLeft: frame = left; break;
Chris@28 2324 case SnapRight: frame = right; break;
Chris@28 2325 case SnapNearest:
Chris@28 2326 case SnapNeighbouring:
Chris@28 2327 if (frame - left > right - frame) frame = right;
Chris@28 2328 else frame = left;
Chris@28 2329 break;
Chris@28 2330 }
Chris@28 2331
Chris@28 2332 return true;
Chris@28 2333 }
Chris@13 2334
Chris@77 2335 bool
Chris@77 2336 SpectrogramLayer::getCrosshairExtents(View *v, QPainter &paint,
Chris@77 2337 QPoint cursorPos,
Chris@77 2338 std::vector<QRect> &extents) const
Chris@77 2339 {
Chris@77 2340 QRect vertical(cursorPos.x() - 12, 0, 12, v->height());
Chris@77 2341 extents.push_back(vertical);
Chris@77 2342
Chris@77 2343 QRect horizontal(0, cursorPos.y(), cursorPos.x(), 1);
Chris@77 2344 extents.push_back(horizontal);
Chris@77 2345
Chris@77 2346 return true;
Chris@77 2347 }
Chris@77 2348
Chris@77 2349 void
Chris@77 2350 SpectrogramLayer::paintCrosshairs(View *v, QPainter &paint,
Chris@77 2351 QPoint cursorPos) const
Chris@77 2352 {
Chris@77 2353 paint.save();
Chris@77 2354 paint.setPen(m_crosshairColour);
Chris@77 2355
Chris@77 2356 paint.drawLine(0, cursorPos.y(), cursorPos.x() - 1, cursorPos.y());
Chris@77 2357 paint.drawLine(cursorPos.x(), 0, cursorPos.x(), v->height());
Chris@77 2358
Chris@77 2359 float fundamental = getFrequencyForY(v, cursorPos.y());
Chris@77 2360
Chris@77 2361 int harmonic = 2;
Chris@77 2362
Chris@77 2363 while (harmonic < 100) {
Chris@77 2364
Chris@77 2365 float hy = lrintf(getYForFrequency(v, fundamental * harmonic));
Chris@77 2366 if (hy < 0 || hy > v->height()) break;
Chris@77 2367
Chris@77 2368 int len = 7;
Chris@77 2369
Chris@77 2370 if (harmonic % 2 == 0) {
Chris@77 2371 if (harmonic % 4 == 0) {
Chris@77 2372 len = 12;
Chris@77 2373 } else {
Chris@77 2374 len = 10;
Chris@77 2375 }
Chris@77 2376 }
Chris@77 2377
Chris@77 2378 paint.drawLine(cursorPos.x() - len,
Chris@77 2379 hy,
Chris@77 2380 cursorPos.x(),
Chris@77 2381 hy);
Chris@77 2382
Chris@77 2383 ++harmonic;
Chris@77 2384 }
Chris@77 2385
Chris@77 2386 paint.restore();
Chris@77 2387 }
Chris@77 2388
Chris@25 2389 QString
Chris@44 2390 SpectrogramLayer::getFeatureDescription(View *v, QPoint &pos) const
Chris@25 2391 {
Chris@25 2392 int x = pos.x();
Chris@25 2393 int y = pos.y();
Chris@0 2394
Chris@25 2395 if (!m_model || !m_model->isOK()) return "";
Chris@0 2396
Chris@38 2397 float magMin = 0, magMax = 0;
Chris@38 2398 float phaseMin = 0, phaseMax = 0;
Chris@0 2399 float freqMin = 0, freqMax = 0;
Chris@35 2400 float adjFreqMin = 0, adjFreqMax = 0;
Chris@25 2401 QString pitchMin, pitchMax;
Chris@0 2402 RealTime rtMin, rtMax;
Chris@0 2403
Chris@38 2404 bool haveValues = false;
Chris@0 2405
Chris@44 2406 if (!getXBinSourceRange(v, x, rtMin, rtMax)) {
Chris@38 2407 return "";
Chris@38 2408 }
Chris@44 2409 if (getXYBinSourceRange(v, x, y, magMin, magMax, phaseMin, phaseMax)) {
Chris@38 2410 haveValues = true;
Chris@38 2411 }
Chris@0 2412
Chris@35 2413 QString adjFreqText = "", adjPitchText = "";
Chris@35 2414
Chris@38 2415 if (m_binDisplay == PeakFrequencies) {
Chris@35 2416
Chris@44 2417 if (!getAdjustedYBinSourceRange(v, x, y, freqMin, freqMax,
Chris@38 2418 adjFreqMin, adjFreqMax)) {
Chris@38 2419 return "";
Chris@38 2420 }
Chris@35 2421
Chris@35 2422 if (adjFreqMin != adjFreqMax) {
Chris@65 2423 adjFreqText = tr("Peak Frequency:\t%1 - %2 Hz\n")
Chris@35 2424 .arg(adjFreqMin).arg(adjFreqMax);
Chris@35 2425 } else {
Chris@65 2426 adjFreqText = tr("Peak Frequency:\t%1 Hz\n")
Chris@35 2427 .arg(adjFreqMin);
Chris@38 2428 }
Chris@38 2429
Chris@38 2430 QString pmin = Pitch::getPitchLabelForFrequency(adjFreqMin);
Chris@38 2431 QString pmax = Pitch::getPitchLabelForFrequency(adjFreqMax);
Chris@38 2432
Chris@38 2433 if (pmin != pmax) {
Chris@65 2434 adjPitchText = tr("Peak Pitch:\t%3 - %4\n").arg(pmin).arg(pmax);
Chris@38 2435 } else {
Chris@65 2436 adjPitchText = tr("Peak Pitch:\t%2\n").arg(pmin);
Chris@35 2437 }
Chris@35 2438
Chris@35 2439 } else {
Chris@35 2440
Chris@44 2441 if (!getYBinSourceRange(v, y, freqMin, freqMax)) return "";
Chris@35 2442 }
Chris@35 2443
Chris@25 2444 QString text;
Chris@25 2445
Chris@25 2446 if (rtMin != rtMax) {
Chris@25 2447 text += tr("Time:\t%1 - %2\n")
Chris@25 2448 .arg(rtMin.toText(true).c_str())
Chris@25 2449 .arg(rtMax.toText(true).c_str());
Chris@25 2450 } else {
Chris@25 2451 text += tr("Time:\t%1\n")
Chris@25 2452 .arg(rtMin.toText(true).c_str());
Chris@0 2453 }
Chris@0 2454
Chris@25 2455 if (freqMin != freqMax) {
Chris@65 2456 text += tr("%1Bin Frequency:\t%2 - %3 Hz\n%4Bin Pitch:\t%5 - %6\n")
Chris@65 2457 .arg(adjFreqText)
Chris@25 2458 .arg(freqMin)
Chris@25 2459 .arg(freqMax)
Chris@65 2460 .arg(adjPitchText)
Chris@65 2461 .arg(Pitch::getPitchLabelForFrequency(freqMin))
Chris@65 2462 .arg(Pitch::getPitchLabelForFrequency(freqMax));
Chris@65 2463 } else {
Chris@65 2464 text += tr("%1Bin Frequency:\t%2 Hz\n%3Bin Pitch:\t%4\n")
Chris@35 2465 .arg(adjFreqText)
Chris@25 2466 .arg(freqMin)
Chris@65 2467 .arg(adjPitchText)
Chris@65 2468 .arg(Pitch::getPitchLabelForFrequency(freqMin));
Chris@25 2469 }
Chris@25 2470
Chris@38 2471 if (haveValues) {
Chris@38 2472 float dbMin = AudioLevel::multiplier_to_dB(magMin);
Chris@38 2473 float dbMax = AudioLevel::multiplier_to_dB(magMax);
Chris@43 2474 QString dbMinString;
Chris@43 2475 QString dbMaxString;
Chris@43 2476 if (dbMin == AudioLevel::DB_FLOOR) {
Chris@43 2477 dbMinString = tr("-Inf");
Chris@43 2478 } else {
Chris@43 2479 dbMinString = QString("%1").arg(lrintf(dbMin));
Chris@43 2480 }
Chris@43 2481 if (dbMax == AudioLevel::DB_FLOOR) {
Chris@43 2482 dbMaxString = tr("-Inf");
Chris@43 2483 } else {
Chris@43 2484 dbMaxString = QString("%1").arg(lrintf(dbMax));
Chris@43 2485 }
Chris@25 2486 if (lrintf(dbMin) != lrintf(dbMax)) {
Chris@199 2487 text += tr("dB:\t%1 - %2").arg(dbMinString).arg(dbMaxString);
Chris@25 2488 } else {
Chris@199 2489 text += tr("dB:\t%1").arg(dbMinString);
Chris@25 2490 }
Chris@38 2491 if (phaseMin != phaseMax) {
Chris@38 2492 text += tr("\nPhase:\t%1 - %2").arg(phaseMin).arg(phaseMax);
Chris@38 2493 } else {
Chris@38 2494 text += tr("\nPhase:\t%1").arg(phaseMin);
Chris@38 2495 }
Chris@25 2496 }
Chris@25 2497
Chris@25 2498 return text;
Chris@0 2499 }
Chris@25 2500
Chris@0 2501 int
Chris@40 2502 SpectrogramLayer::getColourScaleWidth(QPainter &paint) const
Chris@40 2503 {
Chris@40 2504 int cw;
Chris@40 2505
Chris@119 2506 cw = paint.fontMetrics().width("-80dB");
Chris@119 2507
Chris@119 2508 /*!!!
Chris@40 2509 switch (m_colourScale) {
Chris@40 2510 default:
Chris@40 2511 case LinearColourScale:
Chris@40 2512 cw = paint.fontMetrics().width(QString("0.00"));
Chris@40 2513 break;
Chris@40 2514
Chris@40 2515 case MeterColourScale:
Chris@40 2516 case dBColourScale:
Chris@119 2517 case OtherColourScale:
Chris@40 2518 cw = std::max(paint.fontMetrics().width(tr("-Inf")),
Chris@40 2519 paint.fontMetrics().width(tr("-90")));
Chris@40 2520 break;
Chris@40 2521
Chris@40 2522 case PhaseColourScale:
Chris@40 2523 cw = paint.fontMetrics().width(QString("-") + QChar(0x3c0));
Chris@40 2524 break;
Chris@40 2525 }
Chris@119 2526 */
Chris@119 2527
Chris@40 2528
Chris@40 2529 return cw;
Chris@40 2530 }
Chris@40 2531
Chris@40 2532 int
Chris@44 2533 SpectrogramLayer::getVerticalScaleWidth(View *v, QPainter &paint) const
Chris@0 2534 {
Chris@0 2535 if (!m_model || !m_model->isOK()) return 0;
Chris@0 2536
Chris@40 2537 int cw = getColourScaleWidth(paint);
Chris@40 2538
Chris@0 2539 int tw = paint.fontMetrics().width(QString("%1")
Chris@0 2540 .arg(m_maxFrequency > 0 ?
Chris@0 2541 m_maxFrequency - 1 :
Chris@0 2542 m_model->getSampleRate() / 2));
Chris@0 2543
Chris@0 2544 int fw = paint.fontMetrics().width(QString("43Hz"));
Chris@0 2545 if (tw < fw) tw = fw;
Chris@40 2546
Chris@40 2547 int tickw = (m_frequencyScale == LogFrequencyScale ? 10 : 4);
Chris@0 2548
Chris@40 2549 return cw + tickw + tw + 13;
Chris@0 2550 }
Chris@0 2551
Chris@0 2552 void
Chris@44 2553 SpectrogramLayer::paintVerticalScale(View *v, QPainter &paint, QRect rect) const
Chris@0 2554 {
Chris@0 2555 if (!m_model || !m_model->isOK()) {
Chris@0 2556 return;
Chris@0 2557 }
Chris@0 2558
Chris@161 2559 Profiler profiler("SpectrogramLayer::paintVerticalScale", true);
Chris@122 2560
Chris@120 2561 //!!! cache this?
Chris@120 2562
Chris@0 2563 int h = rect.height(), w = rect.width();
Chris@0 2564
Chris@40 2565 int tickw = (m_frequencyScale == LogFrequencyScale ? 10 : 4);
Chris@40 2566 int pkw = (m_frequencyScale == LogFrequencyScale ? 10 : 0);
Chris@40 2567
Chris@107 2568 size_t bins = m_fftSize / 2;
Chris@0 2569 int sr = m_model->getSampleRate();
Chris@0 2570
Chris@0 2571 if (m_maxFrequency > 0) {
Chris@107 2572 bins = int((double(m_maxFrequency) * m_fftSize) / sr + 0.1);
Chris@107 2573 if (bins > m_fftSize / 2) bins = m_fftSize / 2;
Chris@0 2574 }
Chris@0 2575
Chris@40 2576 int cw = getColourScaleWidth(paint);
Chris@119 2577 int cbw = paint.fontMetrics().width("dB");
Chris@40 2578
Chris@0 2579 int py = -1;
Chris@0 2580 int textHeight = paint.fontMetrics().height();
Chris@0 2581 int toff = -textHeight + paint.fontMetrics().ascent() + 2;
Chris@0 2582
Chris@119 2583 if (h > textHeight * 3 + 10) {
Chris@119 2584
Chris@119 2585 int topLines = 2;
Chris@119 2586 if (m_colourScale == PhaseColourScale) topLines = 1;
Chris@119 2587
Chris@119 2588 int ch = h - textHeight * (topLines + 1) - 8;
Chris@119 2589 // paint.drawRect(4, textHeight + 4, cw - 1, ch + 1);
Chris@119 2590 paint.drawRect(4 + cw - cbw, textHeight * topLines + 4, cbw - 1, ch + 1);
Chris@40 2591
Chris@40 2592 QString top, bottom;
Chris@119 2593 /*!!!
Chris@40 2594 switch (m_colourScale) {
Chris@40 2595 default:
Chris@40 2596 case LinearColourScale:
Chris@40 2597 top = (m_normalizeColumns ? "1.0" : "0.02");
Chris@40 2598 bottom = (m_normalizeColumns ? "0.0" : "0.00");
Chris@40 2599 break;
Chris@40 2600
Chris@40 2601 case MeterColourScale:
Chris@40 2602 top = (m_normalizeColumns ? QString("0") :
Chris@40 2603 QString("%1").arg(int(AudioLevel::multiplier_to_dB(0.02))));
Chris@40 2604 bottom = QString("%1").
Chris@40 2605 arg(int(AudioLevel::multiplier_to_dB
Chris@40 2606 (AudioLevel::preview_to_multiplier(0, 255))));
Chris@40 2607 break;
Chris@40 2608
Chris@40 2609 case dBColourScale:
Chris@119 2610 case OtherColourScale:
Chris@40 2611 top = "0";
Chris@40 2612 bottom = "-80";
Chris@40 2613 break;
Chris@40 2614
Chris@40 2615 case PhaseColourScale:
Chris@40 2616 top = QChar(0x3c0);
Chris@40 2617 bottom = "-" + top;
Chris@40 2618 break;
Chris@40 2619 }
Chris@119 2620 */
Chris@119 2621 float min = m_viewMags[v].getMin();
Chris@119 2622 float max = m_viewMags[v].getMax();
Chris@119 2623
Chris@119 2624 float dBmin = AudioLevel::multiplier_to_dB(min);
Chris@119 2625 float dBmax = AudioLevel::multiplier_to_dB(max);
Chris@119 2626
Chris@120 2627 if (dBmax < -60.f) dBmax = -60.f;
Chris@120 2628 else top = QString("%1").arg(lrintf(dBmax));
Chris@120 2629
Chris@120 2630 if (dBmin < dBmax - 60.f) dBmin = dBmax - 60.f;
Chris@119 2631 bottom = QString("%1").arg(lrintf(dBmin));
Chris@119 2632
Chris@119 2633 //!!! & phase etc
Chris@119 2634
Chris@119 2635 if (m_colourScale != PhaseColourScale) {
Chris@119 2636 paint.drawText((cw + 6 - paint.fontMetrics().width("dBFS")) / 2,
Chris@119 2637 2 + textHeight + toff, "dBFS");
Chris@119 2638 }
Chris@119 2639
Chris@119 2640 // paint.drawText((cw + 6 - paint.fontMetrics().width(top)) / 2,
Chris@119 2641 paint.drawText(3 + cw - cbw - paint.fontMetrics().width(top),
Chris@119 2642 2 + textHeight * topLines + toff + textHeight/2, top);
Chris@119 2643
Chris@119 2644 paint.drawText(3 + cw - cbw - paint.fontMetrics().width(bottom),
Chris@119 2645 h + toff - 3 - textHeight/2, bottom);
Chris@40 2646
Chris@40 2647 paint.save();
Chris@40 2648 paint.setBrush(Qt::NoBrush);
Chris@119 2649
Chris@119 2650 int lasty = 0;
Chris@119 2651 int lastdb = 0;
Chris@119 2652
Chris@40 2653 for (int i = 0; i < ch; ++i) {
Chris@119 2654
Chris@119 2655 float dBval = dBmin + (((dBmax - dBmin) * i) / (ch - 1));
Chris@119 2656 int idb = int(dBval);
Chris@119 2657
Chris@119 2658 float value = AudioLevel::dB_to_multiplier(dBval);
Chris@119 2659 int colour = getDisplayValue(v, value * m_gain);
Chris@119 2660 /*
Chris@119 2661 float value = min + (((max - min) * i) / (ch - 1));
Chris@119 2662 if (value < m_threshold) value = 0.f;
Chris@119 2663 int colour = getDisplayValue(v, value * m_gain);
Chris@119 2664 */
Chris@119 2665 /*
Chris@119 2666 int colour = (i * 255) / ch + 1;
Chris@119 2667 */
Chris@197 2668 paint.setPen(m_palette.getColour(colour));
Chris@119 2669
Chris@119 2670 int y = textHeight * topLines + 4 + ch - i;
Chris@119 2671
Chris@119 2672 paint.drawLine(5 + cw - cbw, y, cw + 2, y);
Chris@119 2673
Chris@119 2674 // paint.drawLine(5, 4 + textHeight + ch - i,
Chris@119 2675 // cw + 2, 4 + textHeight + ch - i);
Chris@119 2676
Chris@119 2677
Chris@119 2678 if (i == 0) {
Chris@119 2679 lasty = y;
Chris@119 2680 lastdb = idb;
Chris@119 2681 } else if (i < ch - paint.fontMetrics().ascent() &&
Chris@120 2682 idb != lastdb &&
Chris@119 2683 ((abs(y - lasty) > textHeight &&
Chris@119 2684 idb % 10 == 0) ||
Chris@119 2685 (abs(y - lasty) > paint.fontMetrics().ascent() &&
Chris@119 2686 idb % 5 == 0))) {
Chris@119 2687 paint.setPen(Qt::black);
Chris@119 2688 QString text = QString("%1").arg(idb);
Chris@119 2689 paint.drawText(3 + cw - cbw - paint.fontMetrics().width(text),
Chris@119 2690 y + toff + textHeight/2, text);
Chris@119 2691 paint.setPen(Qt::white);
Chris@119 2692 paint.drawLine(5 + cw - cbw, y, 8 + cw - cbw, y);
Chris@119 2693 lasty = y;
Chris@119 2694 lastdb = idb;
Chris@119 2695 }
Chris@40 2696 }
Chris@40 2697 paint.restore();
Chris@40 2698 }
Chris@40 2699
Chris@40 2700 paint.drawLine(cw + 7, 0, cw + 7, h);
Chris@40 2701
Chris@0 2702 int bin = -1;
Chris@0 2703
Chris@44 2704 for (int y = 0; y < v->height(); ++y) {
Chris@0 2705
Chris@0 2706 float q0, q1;
Chris@44 2707 if (!getYBinRange(v, v->height() - y, q0, q1)) continue;
Chris@0 2708
Chris@0 2709 int vy;
Chris@0 2710
Chris@0 2711 if (int(q0) > bin) {
Chris@0 2712 vy = y;
Chris@0 2713 bin = int(q0);
Chris@0 2714 } else {
Chris@0 2715 continue;
Chris@0 2716 }
Chris@0 2717
Chris@107 2718 int freq = (sr * bin) / m_fftSize;
Chris@0 2719
Chris@0 2720 if (py >= 0 && (vy - py) < textHeight - 1) {
Chris@40 2721 if (m_frequencyScale == LinearFrequencyScale) {
Chris@40 2722 paint.drawLine(w - tickw, h - vy, w, h - vy);
Chris@40 2723 }
Chris@0 2724 continue;
Chris@0 2725 }
Chris@0 2726
Chris@0 2727 QString text = QString("%1").arg(freq);
Chris@40 2728 if (bin == 1) text = QString("%1Hz").arg(freq); // bin 0 is DC
Chris@40 2729 paint.drawLine(cw + 7, h - vy, w - pkw - 1, h - vy);
Chris@0 2730
Chris@0 2731 if (h - vy - textHeight >= -2) {
Chris@40 2732 int tx = w - 3 - paint.fontMetrics().width(text) - std::max(tickw, pkw);
Chris@0 2733 paint.drawText(tx, h - vy + toff, text);
Chris@0 2734 }
Chris@0 2735
Chris@0 2736 py = vy;
Chris@0 2737 }
Chris@40 2738
Chris@40 2739 if (m_frequencyScale == LogFrequencyScale) {
Chris@40 2740
Chris@40 2741 paint.drawLine(w - pkw - 1, 0, w - pkw - 1, h);
Chris@40 2742
Chris@110 2743 int sr = m_model->getSampleRate();
Chris@40 2744 float minf = getEffectiveMinFrequency();
Chris@40 2745 float maxf = getEffectiveMaxFrequency();
Chris@40 2746
Chris@122 2747 int py = h, ppy = h;
Chris@40 2748 paint.setBrush(paint.pen().color());
Chris@40 2749
Chris@40 2750 for (int i = 0; i < 128; ++i) {
Chris@40 2751
Chris@40 2752 float f = Pitch::getFrequencyForPitch(i);
Chris@44 2753 int y = lrintf(v->getYForFrequency(f, minf, maxf, true));
Chris@122 2754
Chris@122 2755 if (y < -2) break;
Chris@122 2756 if (y > h + 2) {
Chris@122 2757 continue;
Chris@122 2758 }
Chris@122 2759
Chris@40 2760 int n = (i % 12);
Chris@122 2761
Chris@122 2762 if (n == 1) {
Chris@122 2763 // C# -- fill the C from here
Chris@122 2764 if (ppy - y > 2) {
Chris@122 2765 paint.fillRect(w - pkw,
Chris@122 2766 // y - (py - y) / 2 - (py - y) / 4,
Chris@122 2767 y,
Chris@122 2768 pkw,
Chris@122 2769 (py + ppy) / 2 - y,
Chris@122 2770 // py - y + 1,
Chris@122 2771 Qt::gray);
Chris@122 2772 }
Chris@122 2773 }
Chris@122 2774
Chris@40 2775 if (n == 1 || n == 3 || n == 6 || n == 8 || n == 10) {
Chris@40 2776 // black notes
Chris@40 2777 paint.drawLine(w - pkw, y, w, y);
Chris@41 2778 int rh = ((py - y) / 4) * 2;
Chris@41 2779 if (rh < 2) rh = 2;
Chris@41 2780 paint.drawRect(w - pkw, y - (py-y)/4, pkw/2, rh);
Chris@40 2781 } else if (n == 0 || n == 5) {
Chris@122 2782 // C, F
Chris@40 2783 if (py < h) {
Chris@40 2784 paint.drawLine(w - pkw, (y + py) / 2, w, (y + py) / 2);
Chris@40 2785 }
Chris@40 2786 }
Chris@40 2787
Chris@122 2788 ppy = py;
Chris@40 2789 py = y;
Chris@40 2790 }
Chris@40 2791 }
Chris@0 2792 }
Chris@0 2793
Chris@187 2794 class SpectrogramRangeMapper : public RangeMapper
Chris@187 2795 {
Chris@187 2796 public:
Chris@187 2797 SpectrogramRangeMapper(int sr, int fftsize) :
Chris@187 2798 // m_dist((float(sr) / 2) - (float(sr) / fftsize)),
Chris@187 2799 m_dist(float(sr) / 2),
Chris@187 2800 m_s2(sqrtf(sqrtf(2))) { }
Chris@187 2801 ~SpectrogramRangeMapper() { }
Chris@187 2802
Chris@187 2803 virtual int getPositionForValue(float value) const {
Chris@187 2804
Chris@187 2805 float dist = m_dist;
Chris@187 2806
Chris@187 2807 int n = 0;
Chris@187 2808 int discard = 0;
Chris@187 2809
Chris@187 2810 while (dist > (value + 0.00001) && dist > 0.1f) {
Chris@187 2811 dist /= m_s2;
Chris@187 2812 ++n;
Chris@187 2813 }
Chris@187 2814
Chris@187 2815 return n;
Chris@187 2816 }
Chris@187 2817
Chris@187 2818 virtual float getValueForPosition(int position) const {
Chris@187 2819
Chris@187 2820 // Vertical zoom step 0 shows the entire range from DC ->
Chris@187 2821 // Nyquist frequency. Step 1 shows 2^(1/4) of the range of
Chris@187 2822 // step 0, and so on until the visible range is smaller than
Chris@187 2823 // the frequency step between bins at the current fft size.
Chris@187 2824
Chris@187 2825 float dist = m_dist;
Chris@187 2826
Chris@187 2827 int n = 0;
Chris@187 2828 while (n < position) {
Chris@187 2829 dist /= m_s2;
Chris@187 2830 ++n;
Chris@187 2831 }
Chris@187 2832
Chris@187 2833 return dist;
Chris@187 2834 }
Chris@187 2835
Chris@187 2836 virtual QString getUnit() const { return "Hz"; }
Chris@187 2837
Chris@187 2838 protected:
Chris@187 2839 float m_dist;
Chris@187 2840 float m_s2;
Chris@187 2841 };
Chris@187 2842
Chris@133 2843 int
Chris@133 2844 SpectrogramLayer::getVerticalZoomSteps(int &defaultStep) const
Chris@133 2845 {
Chris@135 2846 if (!m_model) return 0;
Chris@187 2847
Chris@187 2848 int sr = m_model->getSampleRate();
Chris@187 2849
Chris@187 2850 SpectrogramRangeMapper mapper(sr, m_fftSize);
Chris@187 2851
Chris@187 2852 // int maxStep = mapper.getPositionForValue((float(sr) / m_fftSize) + 0.001);
Chris@187 2853 int maxStep = mapper.getPositionForValue(0);
Chris@187 2854 int minStep = mapper.getPositionForValue(float(sr) / 2);
Chris@135 2855
Chris@187 2856 defaultStep = mapper.getPositionForValue(m_initialMaxFrequency) - minStep;
Chris@187 2857
Chris@187 2858 std::cerr << "SpectrogramLayer::getVerticalZoomSteps: " << maxStep - minStep << " (" << maxStep <<"-" << minStep << "), default is " << defaultStep << " (from initial max freq " << m_initialMaxFrequency << ")" << std::endl;
Chris@187 2859
Chris@187 2860 return maxStep - minStep;
Chris@133 2861 }
Chris@133 2862
Chris@133 2863 int
Chris@133 2864 SpectrogramLayer::getCurrentVerticalZoomStep() const
Chris@133 2865 {
Chris@133 2866 if (!m_model) return 0;
Chris@133 2867
Chris@133 2868 float dmin, dmax;
Chris@133 2869 getDisplayExtents(dmin, dmax);
Chris@133 2870
Chris@187 2871 SpectrogramRangeMapper mapper(m_model->getSampleRate(), m_fftSize);
Chris@187 2872 int n = mapper.getPositionForValue(dmax - dmin);
Chris@187 2873 std::cerr << "SpectrogramLayer::getCurrentVerticalZoomStep: " << n << std::endl;
Chris@133 2874 return n;
Chris@133 2875 }
Chris@133 2876
Chris@133 2877 void
Chris@133 2878 SpectrogramLayer::setVerticalZoomStep(int step)
Chris@133 2879 {
Chris@133 2880 //!!! does not do the right thing for log scale
Chris@133 2881
Chris@187 2882 if (!m_model) return;
Chris@187 2883
Chris@133 2884 float dmin, dmax;
Chris@133 2885 getDisplayExtents(dmin, dmax);
Chris@133 2886
Chris@133 2887 int sr = m_model->getSampleRate();
Chris@187 2888 SpectrogramRangeMapper mapper(sr, m_fftSize);
Chris@187 2889 float ddist = mapper.getValueForPosition(step);
Chris@133 2890
Chris@133 2891 float dmid = (dmax + dmin) / 2;
Chris@133 2892 float newmin = dmid - ddist / 2;
Chris@133 2893 float newmax = dmid + ddist / 2;
Chris@187 2894
Chris@187 2895 float mmin, mmax;
Chris@187 2896 mmin = 0;
Chris@187 2897 mmax = float(sr) / 2;
Chris@133 2898
Chris@187 2899 if (newmin < mmin) {
Chris@187 2900 newmax += (mmin - newmin);
Chris@187 2901 newmin = mmin;
Chris@187 2902 }
Chris@187 2903 if (newmax > mmax) {
Chris@187 2904 newmax = mmax;
Chris@187 2905 }
Chris@133 2906
Chris@187 2907 std::cerr << "SpectrogramLayer::setVerticalZoomStep: " << step << ": " << newmin << " -> " << newmax << " (range " << ddist << ")" << std::endl;
Chris@187 2908
Chris@187 2909 setMinFrequency(int(newmin));
Chris@187 2910 setMaxFrequency(int(newmax));
Chris@187 2911 }
Chris@187 2912
Chris@187 2913 RangeMapper *
Chris@187 2914 SpectrogramLayer::getNewVerticalZoomRangeMapper() const
Chris@187 2915 {
Chris@187 2916 if (!m_model) return 0;
Chris@187 2917 return new SpectrogramRangeMapper(m_model->getSampleRate(), m_fftSize);
Chris@133 2918 }
Chris@133 2919
Chris@6 2920 QString
Chris@6 2921 SpectrogramLayer::toXmlString(QString indent, QString extraAttributes) const
Chris@6 2922 {
Chris@6 2923 QString s;
Chris@6 2924
Chris@6 2925 s += QString("channel=\"%1\" "
Chris@6 2926 "windowSize=\"%2\" "
Chris@153 2927 "windowHopLevel=\"%3\" "
Chris@153 2928 "gain=\"%4\" "
Chris@153 2929 "threshold=\"%5\" ")
Chris@6 2930 .arg(m_channel)
Chris@6 2931 .arg(m_windowSize)
Chris@97 2932 .arg(m_windowHopLevel)
Chris@37 2933 .arg(m_gain)
Chris@37 2934 .arg(m_threshold);
Chris@37 2935
Chris@37 2936 s += QString("minFrequency=\"%1\" "
Chris@37 2937 "maxFrequency=\"%2\" "
Chris@37 2938 "colourScale=\"%3\" "
Chris@37 2939 "colourScheme=\"%4\" "
Chris@37 2940 "colourRotation=\"%5\" "
Chris@37 2941 "frequencyScale=\"%6\" "
Chris@37 2942 "binDisplay=\"%7\" "
Chris@153 2943 "normalizeColumns=\"%8\" "
Chris@153 2944 "normalizeVisibleArea=\"%9\"")
Chris@37 2945 .arg(m_minFrequency)
Chris@6 2946 .arg(m_maxFrequency)
Chris@6 2947 .arg(m_colourScale)
Chris@197 2948 .arg(m_colourMap)
Chris@37 2949 .arg(m_colourRotation)
Chris@35 2950 .arg(m_frequencyScale)
Chris@37 2951 .arg(m_binDisplay)
Chris@153 2952 .arg(m_normalizeColumns ? "true" : "false")
Chris@153 2953 .arg(m_normalizeVisibleArea ? "true" : "false");
Chris@6 2954
Chris@6 2955 return Layer::toXmlString(indent, extraAttributes + " " + s);
Chris@6 2956 }
Chris@6 2957
Chris@11 2958 void
Chris@11 2959 SpectrogramLayer::setProperties(const QXmlAttributes &attributes)
Chris@11 2960 {
Chris@11 2961 bool ok = false;
Chris@11 2962
Chris@11 2963 int channel = attributes.value("channel").toInt(&ok);
Chris@11 2964 if (ok) setChannel(channel);
Chris@11 2965
Chris@11 2966 size_t windowSize = attributes.value("windowSize").toUInt(&ok);
Chris@11 2967 if (ok) setWindowSize(windowSize);
Chris@11 2968
Chris@97 2969 size_t windowHopLevel = attributes.value("windowHopLevel").toUInt(&ok);
Chris@97 2970 if (ok) setWindowHopLevel(windowHopLevel);
Chris@97 2971 else {
Chris@97 2972 size_t windowOverlap = attributes.value("windowOverlap").toUInt(&ok);
Chris@97 2973 // a percentage value
Chris@97 2974 if (ok) {
Chris@97 2975 if (windowOverlap == 0) setWindowHopLevel(0);
Chris@97 2976 else if (windowOverlap == 25) setWindowHopLevel(1);
Chris@97 2977 else if (windowOverlap == 50) setWindowHopLevel(2);
Chris@97 2978 else if (windowOverlap == 75) setWindowHopLevel(3);
Chris@97 2979 else if (windowOverlap == 90) setWindowHopLevel(4);
Chris@97 2980 }
Chris@97 2981 }
Chris@11 2982
Chris@11 2983 float gain = attributes.value("gain").toFloat(&ok);
Chris@11 2984 if (ok) setGain(gain);
Chris@11 2985
Chris@37 2986 float threshold = attributes.value("threshold").toFloat(&ok);
Chris@37 2987 if (ok) setThreshold(threshold);
Chris@37 2988
Chris@37 2989 size_t minFrequency = attributes.value("minFrequency").toUInt(&ok);
Chris@187 2990 if (ok) {
Chris@187 2991 std::cerr << "SpectrogramLayer::setProperties: setting min freq to " << minFrequency << std::endl;
Chris@187 2992 setMinFrequency(minFrequency);
Chris@187 2993 }
Chris@37 2994
Chris@11 2995 size_t maxFrequency = attributes.value("maxFrequency").toUInt(&ok);
Chris@187 2996 if (ok) {
Chris@187 2997 std::cerr << "SpectrogramLayer::setProperties: setting max freq to " << maxFrequency << std::endl;
Chris@187 2998 setMaxFrequency(maxFrequency);
Chris@187 2999 }
Chris@11 3000
Chris@11 3001 ColourScale colourScale = (ColourScale)
Chris@11 3002 attributes.value("colourScale").toInt(&ok);
Chris@11 3003 if (ok) setColourScale(colourScale);
Chris@11 3004
Chris@197 3005 int colourMap = attributes.value("colourScheme").toInt(&ok);
Chris@197 3006 if (ok) setColourMap(colourMap);
Chris@11 3007
Chris@37 3008 int colourRotation = attributes.value("colourRotation").toInt(&ok);
Chris@37 3009 if (ok) setColourRotation(colourRotation);
Chris@37 3010
Chris@11 3011 FrequencyScale frequencyScale = (FrequencyScale)
Chris@11 3012 attributes.value("frequencyScale").toInt(&ok);
Chris@11 3013 if (ok) setFrequencyScale(frequencyScale);
Chris@35 3014
Chris@37 3015 BinDisplay binDisplay = (BinDisplay)
Chris@37 3016 attributes.value("binDisplay").toInt(&ok);
Chris@37 3017 if (ok) setBinDisplay(binDisplay);
Chris@36 3018
Chris@36 3019 bool normalizeColumns =
Chris@36 3020 (attributes.value("normalizeColumns").trimmed() == "true");
Chris@36 3021 setNormalizeColumns(normalizeColumns);
Chris@153 3022
Chris@153 3023 bool normalizeVisibleArea =
Chris@153 3024 (attributes.value("normalizeVisibleArea").trimmed() == "true");
Chris@153 3025 setNormalizeVisibleArea(normalizeVisibleArea);
Chris@11 3026 }
Chris@11 3027