annotate layer/SpectrogramLayer.cpp @ 483:18f38f564d7c

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