annotate layer/SpectrogramLayer.cpp @ 1017:9c890b7dfa83 colourschemes

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