annotate layer/SpectrogramLayer.cpp @ 1085:179ea8a2f650 spectrogram-minor-refactor

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