annotate layer/SpectrogramLayer.cpp @ 481:74a7729e3653

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