annotate layer/SpectrogramLayer.cpp @ 390:0384cf63e91c

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