annotate layer/SpectrogramLayer.cpp @ 215:d2334a77db73

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