annotate layer/SpectrogramLayer.cpp @ 510:683c46d7500b

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