annotate layer/SpectrogramLayer.cpp @ 1078:ee01a4062747 spectrogram-minor-refactor

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