annotate layer/SpectrogramLayer.cpp @ 1053:38a53c7b81f6 3.0-integration

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