annotate layer/SpectrogramLayer.cpp @ 1002:dc8092488005 3.0-integration

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