annotate layer/SpectrogramLayer.cpp @ 1038:6d4fa45a271d spectrogram-minor-refactor

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