annotate layer/SpectrogramLayer.cpp @ 1063:a0f234acd6e7 spectrogram-minor-refactor

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