annotate layer/SpectrogramLayer.cpp @ 482:7f1ed4bfea1e

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