annotate layer/SpectrogramLayer.cpp @ 1009:96cf499fad62 tony-2.0-integration

Fix incorrect reload of old Tony v1.0 files
author Chris Cannam
date Tue, 20 Oct 2015 12:55:09 +0100
parents 2f8340c22e8a
children fccee028a522
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@1007 1890 paintBlockWidth > 100) {
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@1007 1901 if (paintBlockWidth < 50) paintBlockWidth = 50;
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@1008 2404 float max = fft->getNormalizedMagnitudesAt(sx, values, minbin, maxbin - minbin + 1);
Chris@719 2405 if (max > 0.f) {
Chris@719 2406 for (int i = minbin; i <= maxbin; ++i) {
Chris@1008 2407 values[i - minbin] = float(values[i - minbin] *
Chris@1008 2408 log10f(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@1008 2571 float max = fft->getNormalizedMagnitudesAt(sx, autoarray, minbin, maxbin - minbin + 1);
Chris@988 2572 float scale = log10f(max + 1.f);
Chris@998 2573 // cout << "sx = " << sx << ", max = " << max << ", log10(max) = " << log10(max) << ", scale = " << scale << endl;
Chris@719 2574 for (int i = minbin; i <= maxbin; ++i) {
Chris@862 2575 autoarray[i - minbin] *= scale;
Chris@719 2576 }
Chris@487 2577 } else {
Chris@490 2578 fft->getMagnitudesAt(sx, autoarray, minbin, maxbin - minbin + 1);
Chris@487 2579 }
Chris@484 2580 } else {
Chris@485 2581 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@985 2582 cerr << "Retrieving column " << sx << " from peaks cache" << endl;
Chris@485 2583 #endif
Chris@484 2584 c = sourceModel->getColumn(sx);
Chris@862 2585 if (m_normalization == NormalizeColumns ||
Chris@862 2586 m_normalization == NormalizeHybrid) {
Chris@487 2587 for (int y = 0; y < h; ++y) {
Chris@487 2588 if (c[y] > columnMax) columnMax = c[y];
Chris@487 2589 }
Chris@487 2590 }
Chris@490 2591 values = c.constData() + minbin;
Chris@484 2592 }
Chris@483 2593 psx = sx;
Chris@483 2594 }
Chris@483 2595
Chris@483 2596 for (int y = 0; y < h; ++y) {
Chris@480 2597
Chris@905 2598 double sy0 = binfory[y];
Chris@905 2599 double sy1 = sy0 + 1;
Chris@481 2600 if (y+1 < h) sy1 = binfory[y+1];
Chris@490 2601
Chris@907 2602 double value = 0.0;
Chris@907 2603
Chris@907 2604 if (interpolate && fabs(sy1 - sy0) < 1.0) {
Chris@490 2605
Chris@905 2606 double centre = (sy0 + sy1) / 2;
Chris@907 2607 double dist = (centre - 0.5) - rint(centre - 0.5);
Chris@490 2608 int bin = int(centre);
Chris@490 2609 int other = (dist < 0 ? (bin-1) : (bin+1));
Chris@490 2610 if (bin < minbin) bin = minbin;
Chris@490 2611 if (bin > maxbin) bin = maxbin;
Chris@490 2612 if (other < minbin || other > maxbin) other = bin;
Chris@907 2613 double prop = 1.0 - fabs(dist);
Chris@905 2614
Chris@905 2615 double v0 = values[bin - minbin];
Chris@905 2616 double v1 = values[other - minbin];
Chris@490 2617 if (m_binDisplay == PeakBins) {
Chris@490 2618 if (bin == minbin || bin == maxbin ||
Chris@490 2619 v0 < values[bin-minbin-1] ||
Chris@907 2620 v0 < values[bin-minbin+1]) v0 = 0.0;
Chris@490 2621 if (other == minbin || other == maxbin ||
Chris@490 2622 v1 < values[other-minbin-1] ||
Chris@907 2623 v1 < values[other-minbin+1]) v1 = 0.0;
Chris@489 2624 }
Chris@907 2625 if (v0 == 0.0 && v1 == 0.0) continue;
Chris@907 2626 value = prop * v0 + (1.0 - prop) * v1;
Chris@484 2627
Chris@488 2628 if (m_colourScale != PhaseColourScale) {
Chris@862 2629 if (m_normalization != NormalizeColumns &&
Chris@862 2630 m_normalization != NormalizeHybrid) {
Chris@907 2631 value /= (m_fftSize/2.0);
Chris@488 2632 }
Chris@907 2633 mag.sample(float(value));
Chris@488 2634 value *= m_gain;
Chris@488 2635 }
Chris@488 2636
Chris@907 2637 peaks[y] = float(value);
Chris@490 2638
Chris@490 2639 } else {
Chris@490 2640
Chris@490 2641 int by0 = int(sy0 + 0.0001);
Chris@490 2642 int by1 = int(sy1 + 0.0001);
Chris@490 2643 if (by1 < by0 + 1) by1 = by0 + 1;
Chris@490 2644
Chris@490 2645 for (int bin = by0; bin < by1; ++bin) {
Chris@490 2646
Chris@490 2647 value = values[bin - minbin];
Chris@490 2648 if (m_binDisplay == PeakBins) {
Chris@490 2649 if (bin == minbin || bin == maxbin ||
Chris@490 2650 value < values[bin-minbin-1] ||
Chris@490 2651 value < values[bin-minbin+1]) continue;
Chris@480 2652 }
Chris@490 2653
Chris@490 2654 if (m_colourScale != PhaseColourScale) {
Chris@862 2655 if (m_normalization != NormalizeColumns &&
Chris@862 2656 m_normalization != NormalizeHybrid) {
Chris@907 2657 value /= (m_fftSize/2.0);
Chris@490 2658 }
Chris@907 2659 mag.sample(float(value));
Chris@490 2660 value *= m_gain;
Chris@490 2661 }
Chris@490 2662
Chris@907 2663 if (value > peaks[y]) {
Chris@907 2664 peaks[y] = float(value); //!!! not right for phase!
Chris@907 2665 }
Chris@480 2666 }
Chris@480 2667 }
Chris@483 2668 }
Chris@488 2669
Chris@488 2670 if (mag.isSet()) {
Chris@488 2671 if (sx >= int(m_columnMags.size())) {
Chris@540 2672 #ifdef DEBUG_SPECTROGRAM
Chris@682 2673 cerr << "INTERNAL ERROR: " << sx << " >= "
Chris@488 2674 << m_columnMags.size()
Chris@488 2675 << " at SpectrogramLayer.cpp::paintDrawBuffer"
Chris@682 2676 << endl;
Chris@540 2677 #endif
Chris@490 2678 } else {
Chris@490 2679 m_columnMags[sx].sample(mag);
Chris@491 2680 if (overallMag.sample(mag)) overallMagChanged = true;
Chris@488 2681 }
Chris@488 2682 }
Chris@483 2683 }
Chris@483 2684
Chris@483 2685 for (int y = 0; y < h; ++y) {
Chris@483 2686
Chris@905 2687 double peak = peaks[y];
Chris@483 2688
Chris@488 2689 if (m_colourScale != PhaseColourScale &&
Chris@862 2690 (m_normalization == NormalizeColumns ||
Chris@862 2691 m_normalization == NormalizeHybrid) &&
Chris@488 2692 columnMax > 0.f) {
Chris@488 2693 peak /= columnMax;
Chris@862 2694 if (m_normalization == NormalizeHybrid) {
Chris@862 2695 peak *= log10(columnMax + 1.f);
Chris@719 2696 }
Chris@480 2697 }
Chris@483 2698
Chris@483 2699 unsigned char peakpix = getDisplayValue(v, peak);
Chris@480 2700
Chris@480 2701 m_drawBuffer.setPixel(x, h-y-1, peakpix);
Chris@480 2702 }
Chris@480 2703 }
Chris@480 2704
Chris@480 2705 return true;
Chris@480 2706 }
Chris@477 2707
Chris@121 2708 void
Chris@918 2709 SpectrogramLayer::illuminateLocalFeatures(LayerGeometryProvider *v, QPainter &paint) const
Chris@121 2710 {
Chris@382 2711 Profiler profiler("SpectrogramLayer::illuminateLocalFeatures");
Chris@382 2712
Chris@121 2713 QPoint localPos;
Chris@121 2714 if (!v->shouldIlluminateLocalFeatures(this, localPos) || !m_model) {
Chris@121 2715 return;
Chris@121 2716 }
Chris@121 2717
Chris@682 2718 // cerr << "SpectrogramLayer: illuminateLocalFeatures("
Chris@682 2719 // << localPos.x() << "," << localPos.y() << ")" << endl;
Chris@121 2720
Chris@905 2721 double s0, s1;
Chris@905 2722 double f0, f1;
Chris@121 2723
Chris@121 2724 if (getXBinRange(v, localPos.x(), s0, s1) &&
Chris@121 2725 getYBinSourceRange(v, localPos.y(), f0, f1)) {
Chris@121 2726
Chris@121 2727 int s0i = int(s0 + 0.001);
Chris@121 2728 int s1i = int(s1);
Chris@121 2729
Chris@121 2730 int x0 = v->getXForFrame(s0i * getWindowIncrement());
Chris@121 2731 int x1 = v->getXForFrame((s1i + 1) * getWindowIncrement());
Chris@121 2732
Chris@248 2733 int y1 = int(getYForFrequency(v, f1));
Chris@248 2734 int y0 = int(getYForFrequency(v, f0));
Chris@121 2735
Chris@682 2736 // cerr << "SpectrogramLayer: illuminate "
Chris@682 2737 // << x0 << "," << y1 << " -> " << x1 << "," << y0 << endl;
Chris@121 2738
Chris@287 2739 paint.setPen(v->getForeground());
Chris@133 2740
Chris@133 2741 //!!! should we be using paintCrosshairs for this?
Chris@133 2742
Chris@121 2743 paint.drawRect(x0, y1, x1 - x0 + 1, y0 - y1 + 1);
Chris@121 2744 }
Chris@121 2745 }
Chris@121 2746
Chris@905 2747 double
Chris@918 2748 SpectrogramLayer::getYForFrequency(const LayerGeometryProvider *v, double frequency) const
Chris@42 2749 {
Chris@44 2750 return v->getYForFrequency(frequency,
Chris@44 2751 getEffectiveMinFrequency(),
Chris@44 2752 getEffectiveMaxFrequency(),
Chris@44 2753 m_frequencyScale == LogFrequencyScale);
Chris@42 2754 }
Chris@42 2755
Chris@905 2756 double
Chris@918 2757 SpectrogramLayer::getFrequencyForY(const LayerGeometryProvider *v, int y) const
Chris@42 2758 {
Chris@44 2759 return v->getFrequencyForY(y,
Chris@44 2760 getEffectiveMinFrequency(),
Chris@44 2761 getEffectiveMaxFrequency(),
Chris@44 2762 m_frequencyScale == LogFrequencyScale);
Chris@42 2763 }
Chris@42 2764
Chris@0 2765 int
Chris@918 2766 SpectrogramLayer::getCompletion(LayerGeometryProvider *v) const
Chris@0 2767 {
Chris@920 2768 const View *view = v->getView();
Chris@920 2769
Chris@920 2770 if (m_fftModels.find(view) == m_fftModels.end()) return 100;
Chris@920 2771
Chris@988 2772 int completion = m_fftModels[view]->getCompletion();
Chris@224 2773 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@985 2774 cerr << "SpectrogramLayer::getCompletion: completion = " << completion << endl;
Chris@224 2775 #endif
Chris@0 2776 return completion;
Chris@0 2777 }
Chris@0 2778
Chris@583 2779 QString
Chris@918 2780 SpectrogramLayer::getError(LayerGeometryProvider *v) const
Chris@583 2781 {
Chris@920 2782 const View *view = v->getView();
Chris@920 2783 if (m_fftModels.find(view) == m_fftModels.end()) return "";
Chris@988 2784 return m_fftModels[view]->getError();
Chris@583 2785 }
Chris@583 2786
Chris@28 2787 bool
Chris@905 2788 SpectrogramLayer::getValueExtents(double &min, double &max,
Chris@101 2789 bool &logarithmic, QString &unit) const
Chris@79 2790 {
Chris@133 2791 if (!m_model) return false;
Chris@133 2792
Chris@907 2793 sv_samplerate_t sr = m_model->getSampleRate();
Chris@905 2794 min = double(sr) / m_fftSize;
Chris@905 2795 max = double(sr) / 2;
Chris@133 2796
Chris@101 2797 logarithmic = (m_frequencyScale == LogFrequencyScale);
Chris@79 2798 unit = "Hz";
Chris@79 2799 return true;
Chris@79 2800 }
Chris@79 2801
Chris@79 2802 bool
Chris@905 2803 SpectrogramLayer::getDisplayExtents(double &min, double &max) const
Chris@101 2804 {
Chris@101 2805 min = getEffectiveMinFrequency();
Chris@101 2806 max = getEffectiveMaxFrequency();
Chris@253 2807
Chris@587 2808 // SVDEBUG << "SpectrogramLayer::getDisplayExtents: " << min << "->" << max << endl;
Chris@101 2809 return true;
Chris@101 2810 }
Chris@101 2811
Chris@101 2812 bool
Chris@905 2813 SpectrogramLayer::setDisplayExtents(double min, double max)
Chris@120 2814 {
Chris@120 2815 if (!m_model) return false;
Chris@187 2816
Chris@587 2817 // SVDEBUG << "SpectrogramLayer::setDisplayExtents: " << min << "->" << max << endl;
Chris@187 2818
Chris@120 2819 if (min < 0) min = 0;
Chris@907 2820 if (max > m_model->getSampleRate()/2.0) max = m_model->getSampleRate()/2.0;
Chris@120 2821
Chris@907 2822 int minf = int(lrint(min));
Chris@907 2823 int maxf = int(lrint(max));
Chris@120 2824
Chris@120 2825 if (m_minFrequency == minf && m_maxFrequency == maxf) return true;
Chris@120 2826
Chris@478 2827 invalidateImageCaches();
Chris@120 2828 invalidateMagnitudes();
Chris@120 2829
Chris@120 2830 m_minFrequency = minf;
Chris@120 2831 m_maxFrequency = maxf;
Chris@120 2832
Chris@120 2833 emit layerParametersChanged();
Chris@120 2834
Chris@133 2835 int vs = getCurrentVerticalZoomStep();
Chris@133 2836 if (vs != m_lastEmittedZoomStep) {
Chris@133 2837 emit verticalZoomChanged();
Chris@133 2838 m_lastEmittedZoomStep = vs;
Chris@133 2839 }
Chris@133 2840
Chris@120 2841 return true;
Chris@120 2842 }
Chris@120 2843
Chris@120 2844 bool
Chris@918 2845 SpectrogramLayer::getYScaleValue(const LayerGeometryProvider *v, int y,
Chris@905 2846 double &value, QString &unit) const
Chris@261 2847 {
Chris@261 2848 value = getFrequencyForY(v, y);
Chris@261 2849 unit = "Hz";
Chris@261 2850 return true;
Chris@261 2851 }
Chris@261 2852
Chris@261 2853 bool
Chris@918 2854 SpectrogramLayer::snapToFeatureFrame(LayerGeometryProvider *,
Chris@907 2855 sv_frame_t &frame,
Chris@805 2856 int &resolution,
Chris@28 2857 SnapType snap) const
Chris@13 2858 {
Chris@13 2859 resolution = getWindowIncrement();
Chris@907 2860 sv_frame_t left = (frame / resolution) * resolution;
Chris@907 2861 sv_frame_t right = left + resolution;
Chris@28 2862
Chris@28 2863 switch (snap) {
Chris@28 2864 case SnapLeft: frame = left; break;
Chris@28 2865 case SnapRight: frame = right; break;
Chris@28 2866 case SnapNearest:
Chris@28 2867 case SnapNeighbouring:
Chris@28 2868 if (frame - left > right - frame) frame = right;
Chris@28 2869 else frame = left;
Chris@28 2870 break;
Chris@28 2871 }
Chris@28 2872
Chris@28 2873 return true;
Chris@28 2874 }
Chris@13 2875
Chris@283 2876 void
Chris@918 2877 SpectrogramLayer::measureDoubleClick(LayerGeometryProvider *v, QMouseEvent *e)
Chris@283 2878 {
Chris@920 2879 const View *view = v->getView();
Chris@920 2880 ImageCache &cache = m_imageCaches[view];
Chris@478 2881
Chris@682 2882 cerr << "cache width: " << cache.image.width() << ", height: "
Chris@920 2883 << cache.image.height() << endl;
Chris@478 2884
Chris@478 2885 QImage image = cache.image;
Chris@283 2886
Chris@283 2887 ImageRegionFinder finder;
Chris@283 2888 QRect rect = finder.findRegionExtents(&image, e->pos());
Chris@283 2889 if (rect.isValid()) {
Chris@283 2890 MeasureRect mr;
Chris@283 2891 setMeasureRectFromPixrect(v, mr, rect);
Chris@283 2892 CommandHistory::getInstance()->addCommand
Chris@283 2893 (new AddMeasurementRectCommand(this, mr));
Chris@283 2894 }
Chris@283 2895 }
Chris@283 2896
Chris@77 2897 bool
Chris@918 2898 SpectrogramLayer::getCrosshairExtents(LayerGeometryProvider *v, QPainter &paint,
Chris@77 2899 QPoint cursorPos,
Chris@77 2900 std::vector<QRect> &extents) const
Chris@77 2901 {
Chris@918 2902 QRect vertical(cursorPos.x() - 12, 0, 12, v->getPaintHeight());
Chris@77 2903 extents.push_back(vertical);
Chris@77 2904
Chris@77 2905 QRect horizontal(0, cursorPos.y(), cursorPos.x(), 1);
Chris@77 2906 extents.push_back(horizontal);
Chris@77 2907
Chris@608 2908 int sw = getVerticalScaleWidth(v, m_haveDetailedScale, paint);
Chris@264 2909
Chris@280 2910 QRect freq(sw, cursorPos.y() - paint.fontMetrics().ascent() - 2,
Chris@280 2911 paint.fontMetrics().width("123456 Hz") + 2,
Chris@280 2912 paint.fontMetrics().height());
Chris@280 2913 extents.push_back(freq);
Chris@264 2914
Chris@279 2915 QRect pitch(sw, cursorPos.y() + 2,
Chris@279 2916 paint.fontMetrics().width("C#10+50c") + 2,
Chris@279 2917 paint.fontMetrics().height());
Chris@279 2918 extents.push_back(pitch);
Chris@279 2919
Chris@280 2920 QRect rt(cursorPos.x(),
Chris@918 2921 v->getPaintHeight() - paint.fontMetrics().height() - 2,
Chris@280 2922 paint.fontMetrics().width("1234.567 s"),
Chris@280 2923 paint.fontMetrics().height());
Chris@280 2924 extents.push_back(rt);
Chris@280 2925
Chris@280 2926 int w(paint.fontMetrics().width("1234567890") + 2);
Chris@280 2927 QRect frame(cursorPos.x() - w - 2,
Chris@918 2928 v->getPaintHeight() - paint.fontMetrics().height() - 2,
Chris@280 2929 w,
Chris@280 2930 paint.fontMetrics().height());
Chris@280 2931 extents.push_back(frame);
Chris@280 2932
Chris@77 2933 return true;
Chris@77 2934 }
Chris@77 2935
Chris@77 2936 void
Chris@918 2937 SpectrogramLayer::paintCrosshairs(LayerGeometryProvider *v, QPainter &paint,
Chris@77 2938 QPoint cursorPos) const
Chris@77 2939 {
Chris@77 2940 paint.save();
Chris@283 2941
Chris@608 2942 int sw = getVerticalScaleWidth(v, m_haveDetailedScale, paint);
Chris@283 2943
Chris@282 2944 QFont fn = paint.font();
Chris@282 2945 if (fn.pointSize() > 8) {
Chris@282 2946 fn.setPointSize(fn.pointSize() - 1);
Chris@282 2947 paint.setFont(fn);
Chris@282 2948 }
Chris@77 2949 paint.setPen(m_crosshairColour);
Chris@77 2950
Chris@77 2951 paint.drawLine(0, cursorPos.y(), cursorPos.x() - 1, cursorPos.y());
Chris@918 2952 paint.drawLine(cursorPos.x(), 0, cursorPos.x(), v->getPaintHeight());
Chris@77 2953
Chris@905 2954 double fundamental = getFrequencyForY(v, cursorPos.y());
Chris@77 2955
Chris@278 2956 v->drawVisibleText(paint,
Chris@278 2957 sw + 2,
Chris@278 2958 cursorPos.y() - 2,
Chris@278 2959 QString("%1 Hz").arg(fundamental),
Chris@278 2960 View::OutlinedText);
Chris@278 2961
Chris@279 2962 if (Pitch::isFrequencyInMidiRange(fundamental)) {
Chris@279 2963 QString pitchLabel = Pitch::getPitchLabelForFrequency(fundamental);
Chris@279 2964 v->drawVisibleText(paint,
Chris@279 2965 sw + 2,
Chris@279 2966 cursorPos.y() + paint.fontMetrics().ascent() + 2,
Chris@279 2967 pitchLabel,
Chris@279 2968 View::OutlinedText);
Chris@279 2969 }
Chris@279 2970
Chris@907 2971 sv_frame_t frame = v->getFrameForX(cursorPos.x());
Chris@279 2972 RealTime rt = RealTime::frame2RealTime(frame, m_model->getSampleRate());
Chris@280 2973 QString rtLabel = QString("%1 s").arg(rt.toText(true).c_str());
Chris@280 2974 QString frameLabel = QString("%1").arg(frame);
Chris@280 2975 v->drawVisibleText(paint,
Chris@280 2976 cursorPos.x() - paint.fontMetrics().width(frameLabel) - 2,
Chris@918 2977 v->getPaintHeight() - 2,
Chris@280 2978 frameLabel,
Chris@280 2979 View::OutlinedText);
Chris@280 2980 v->drawVisibleText(paint,
Chris@280 2981 cursorPos.x() + 2,
Chris@918 2982 v->getPaintHeight() - 2,
Chris@280 2983 rtLabel,
Chris@280 2984 View::OutlinedText);
Chris@264 2985
Chris@77 2986 int harmonic = 2;
Chris@77 2987
Chris@77 2988 while (harmonic < 100) {
Chris@77 2989
Chris@907 2990 int hy = int(lrint(getYForFrequency(v, fundamental * harmonic)));
Chris@918 2991 if (hy < 0 || hy > v->getPaintHeight()) break;
Chris@77 2992
Chris@77 2993 int len = 7;
Chris@77 2994
Chris@77 2995 if (harmonic % 2 == 0) {
Chris@77 2996 if (harmonic % 4 == 0) {
Chris@77 2997 len = 12;
Chris@77 2998 } else {
Chris@77 2999 len = 10;
Chris@77 3000 }
Chris@77 3001 }
Chris@77 3002
Chris@77 3003 paint.drawLine(cursorPos.x() - len,
Chris@907 3004 hy,
Chris@77 3005 cursorPos.x(),
Chris@907 3006 hy);
Chris@77 3007
Chris@77 3008 ++harmonic;
Chris@77 3009 }
Chris@77 3010
Chris@77 3011 paint.restore();
Chris@77 3012 }
Chris@77 3013
Chris@25 3014 QString
Chris@918 3015 SpectrogramLayer::getFeatureDescription(LayerGeometryProvider *v, QPoint &pos) const
Chris@25 3016 {
Chris@25 3017 int x = pos.x();
Chris@25 3018 int y = pos.y();
Chris@0 3019
Chris@25 3020 if (!m_model || !m_model->isOK()) return "";
Chris@0 3021
Chris@905 3022 double magMin = 0, magMax = 0;
Chris@905 3023 double phaseMin = 0, phaseMax = 0;
Chris@905 3024 double freqMin = 0, freqMax = 0;
Chris@905 3025 double adjFreqMin = 0, adjFreqMax = 0;
Chris@25 3026 QString pitchMin, pitchMax;
Chris@0 3027 RealTime rtMin, rtMax;
Chris@0 3028
Chris@38 3029 bool haveValues = false;
Chris@0 3030
Chris@44 3031 if (!getXBinSourceRange(v, x, rtMin, rtMax)) {
Chris@38 3032 return "";
Chris@38 3033 }
Chris@44 3034 if (getXYBinSourceRange(v, x, y, magMin, magMax, phaseMin, phaseMax)) {
Chris@38 3035 haveValues = true;
Chris@38 3036 }
Chris@0 3037
Chris@35 3038 QString adjFreqText = "", adjPitchText = "";
Chris@35 3039
Chris@38 3040 if (m_binDisplay == PeakFrequencies) {
Chris@35 3041
Chris@44 3042 if (!getAdjustedYBinSourceRange(v, x, y, freqMin, freqMax,
Chris@38 3043 adjFreqMin, adjFreqMax)) {
Chris@38 3044 return "";
Chris@38 3045 }
Chris@35 3046
Chris@35 3047 if (adjFreqMin != adjFreqMax) {
Chris@65 3048 adjFreqText = tr("Peak Frequency:\t%1 - %2 Hz\n")
Chris@35 3049 .arg(adjFreqMin).arg(adjFreqMax);
Chris@35 3050 } else {
Chris@65 3051 adjFreqText = tr("Peak Frequency:\t%1 Hz\n")
Chris@35 3052 .arg(adjFreqMin);
Chris@38 3053 }
Chris@38 3054
Chris@38 3055 QString pmin = Pitch::getPitchLabelForFrequency(adjFreqMin);
Chris@38 3056 QString pmax = Pitch::getPitchLabelForFrequency(adjFreqMax);
Chris@38 3057
Chris@38 3058 if (pmin != pmax) {
Chris@65 3059 adjPitchText = tr("Peak Pitch:\t%3 - %4\n").arg(pmin).arg(pmax);
Chris@38 3060 } else {
Chris@65 3061 adjPitchText = tr("Peak Pitch:\t%2\n").arg(pmin);
Chris@35 3062 }
Chris@35 3063
Chris@35 3064 } else {
Chris@35 3065
Chris@44 3066 if (!getYBinSourceRange(v, y, freqMin, freqMax)) return "";
Chris@35 3067 }
Chris@35 3068
Chris@25 3069 QString text;
Chris@25 3070
Chris@25 3071 if (rtMin != rtMax) {
Chris@25 3072 text += tr("Time:\t%1 - %2\n")
Chris@25 3073 .arg(rtMin.toText(true).c_str())
Chris@25 3074 .arg(rtMax.toText(true).c_str());
Chris@25 3075 } else {
Chris@25 3076 text += tr("Time:\t%1\n")
Chris@25 3077 .arg(rtMin.toText(true).c_str());
Chris@0 3078 }
Chris@0 3079
Chris@25 3080 if (freqMin != freqMax) {
Chris@65 3081 text += tr("%1Bin Frequency:\t%2 - %3 Hz\n%4Bin Pitch:\t%5 - %6\n")
Chris@65 3082 .arg(adjFreqText)
Chris@25 3083 .arg(freqMin)
Chris@25 3084 .arg(freqMax)
Chris@65 3085 .arg(adjPitchText)
Chris@65 3086 .arg(Pitch::getPitchLabelForFrequency(freqMin))
Chris@65 3087 .arg(Pitch::getPitchLabelForFrequency(freqMax));
Chris@65 3088 } else {
Chris@65 3089 text += tr("%1Bin Frequency:\t%2 Hz\n%3Bin Pitch:\t%4\n")
Chris@35 3090 .arg(adjFreqText)
Chris@25 3091 .arg(freqMin)
Chris@65 3092 .arg(adjPitchText)
Chris@65 3093 .arg(Pitch::getPitchLabelForFrequency(freqMin));
Chris@25 3094 }
Chris@25 3095
Chris@38 3096 if (haveValues) {
Chris@905 3097 double dbMin = AudioLevel::multiplier_to_dB(magMin);
Chris@905 3098 double dbMax = AudioLevel::multiplier_to_dB(magMax);
Chris@43 3099 QString dbMinString;
Chris@43 3100 QString dbMaxString;
Chris@43 3101 if (dbMin == AudioLevel::DB_FLOOR) {
Chris@43 3102 dbMinString = tr("-Inf");
Chris@43 3103 } else {
Chris@907 3104 dbMinString = QString("%1").arg(lrint(dbMin));
Chris@43 3105 }
Chris@43 3106 if (dbMax == AudioLevel::DB_FLOOR) {
Chris@43 3107 dbMaxString = tr("-Inf");
Chris@43 3108 } else {
Chris@907 3109 dbMaxString = QString("%1").arg(lrint(dbMax));
Chris@43 3110 }
Chris@907 3111 if (lrint(dbMin) != lrint(dbMax)) {
Chris@199 3112 text += tr("dB:\t%1 - %2").arg(dbMinString).arg(dbMaxString);
Chris@25 3113 } else {
Chris@199 3114 text += tr("dB:\t%1").arg(dbMinString);
Chris@25 3115 }
Chris@38 3116 if (phaseMin != phaseMax) {
Chris@38 3117 text += tr("\nPhase:\t%1 - %2").arg(phaseMin).arg(phaseMax);
Chris@38 3118 } else {
Chris@38 3119 text += tr("\nPhase:\t%1").arg(phaseMin);
Chris@38 3120 }
Chris@25 3121 }
Chris@25 3122
Chris@25 3123 return text;
Chris@0 3124 }
Chris@25 3125
Chris@0 3126 int
Chris@40 3127 SpectrogramLayer::getColourScaleWidth(QPainter &paint) const
Chris@40 3128 {
Chris@40 3129 int cw;
Chris@40 3130
Chris@119 3131 cw = paint.fontMetrics().width("-80dB");
Chris@119 3132
Chris@40 3133 return cw;
Chris@40 3134 }
Chris@40 3135
Chris@40 3136 int
Chris@918 3137 SpectrogramLayer::getVerticalScaleWidth(LayerGeometryProvider *, bool detailed, QPainter &paint) const
Chris@0 3138 {
Chris@0 3139 if (!m_model || !m_model->isOK()) return 0;
Chris@0 3140
Chris@607 3141 int cw = 0;
Chris@607 3142 if (detailed) cw = getColourScaleWidth(paint);
Chris@40 3143
Chris@0 3144 int tw = paint.fontMetrics().width(QString("%1")
Chris@0 3145 .arg(m_maxFrequency > 0 ?
Chris@0 3146 m_maxFrequency - 1 :
Chris@0 3147 m_model->getSampleRate() / 2));
Chris@0 3148
Chris@234 3149 int fw = paint.fontMetrics().width(tr("43Hz"));
Chris@0 3150 if (tw < fw) tw = fw;
Chris@40 3151
Chris@40 3152 int tickw = (m_frequencyScale == LogFrequencyScale ? 10 : 4);
Chris@0 3153
Chris@40 3154 return cw + tickw + tw + 13;
Chris@0 3155 }
Chris@0 3156
Chris@0 3157 void
Chris@918 3158 SpectrogramLayer::paintVerticalScale(LayerGeometryProvider *v, bool detailed, QPainter &paint, QRect rect) const
Chris@0 3159 {
Chris@0 3160 if (!m_model || !m_model->isOK()) {
Chris@0 3161 return;
Chris@0 3162 }
Chris@0 3163
Chris@382 3164 Profiler profiler("SpectrogramLayer::paintVerticalScale");
Chris@122 3165
Chris@120 3166 //!!! cache this?
Chris@120 3167
Chris@0 3168 int h = rect.height(), w = rect.width();
Chris@0 3169
Chris@40 3170 int tickw = (m_frequencyScale == LogFrequencyScale ? 10 : 4);
Chris@40 3171 int pkw = (m_frequencyScale == LogFrequencyScale ? 10 : 0);
Chris@40 3172
Chris@805 3173 int bins = m_fftSize / 2;
Chris@907 3174 sv_samplerate_t sr = m_model->getSampleRate();
Chris@0 3175
Chris@0 3176 if (m_maxFrequency > 0) {
Chris@107 3177 bins = int((double(m_maxFrequency) * m_fftSize) / sr + 0.1);
Chris@107 3178 if (bins > m_fftSize / 2) bins = m_fftSize / 2;
Chris@0 3179 }
Chris@0 3180
Chris@607 3181 int cw = 0;
Chris@607 3182
Chris@607 3183 if (detailed) cw = getColourScaleWidth(paint);
Chris@119 3184 int cbw = paint.fontMetrics().width("dB");
Chris@40 3185
Chris@0 3186 int py = -1;
Chris@0 3187 int textHeight = paint.fontMetrics().height();
Chris@0 3188 int toff = -textHeight + paint.fontMetrics().ascent() + 2;
Chris@0 3189
Chris@607 3190 if (detailed && (h > textHeight * 3 + 10)) {
Chris@119 3191
Chris@119 3192 int topLines = 2;
Chris@119 3193 if (m_colourScale == PhaseColourScale) topLines = 1;
Chris@119 3194
Chris@119 3195 int ch = h - textHeight * (topLines + 1) - 8;
Chris@119 3196 // paint.drawRect(4, textHeight + 4, cw - 1, ch + 1);
Chris@119 3197 paint.drawRect(4 + cw - cbw, textHeight * topLines + 4, cbw - 1, ch + 1);
Chris@40 3198
Chris@40 3199 QString top, bottom;
Chris@905 3200 double min = m_viewMags[v].getMin();
Chris@905 3201 double max = m_viewMags[v].getMax();
Chris@905 3202
Chris@905 3203 double dBmin = AudioLevel::multiplier_to_dB(min);
Chris@905 3204 double dBmax = AudioLevel::multiplier_to_dB(max);
Chris@119 3205
Chris@120 3206 if (dBmax < -60.f) dBmax = -60.f;
Chris@907 3207 else top = QString("%1").arg(lrint(dBmax));
Chris@120 3208
Chris@120 3209 if (dBmin < dBmax - 60.f) dBmin = dBmax - 60.f;
Chris@907 3210 bottom = QString("%1").arg(lrint(dBmin));
Chris@119 3211
Chris@119 3212 //!!! & phase etc
Chris@119 3213
Chris@119 3214 if (m_colourScale != PhaseColourScale) {
Chris@119 3215 paint.drawText((cw + 6 - paint.fontMetrics().width("dBFS")) / 2,
Chris@119 3216 2 + textHeight + toff, "dBFS");
Chris@119 3217 }
Chris@119 3218
Chris@119 3219 // paint.drawText((cw + 6 - paint.fontMetrics().width(top)) / 2,
Chris@119 3220 paint.drawText(3 + cw - cbw - paint.fontMetrics().width(top),
Chris@119 3221 2 + textHeight * topLines + toff + textHeight/2, top);
Chris@119 3222
Chris@119 3223 paint.drawText(3 + cw - cbw - paint.fontMetrics().width(bottom),
Chris@119 3224 h + toff - 3 - textHeight/2, bottom);
Chris@40 3225
Chris@40 3226 paint.save();
Chris@40 3227 paint.setBrush(Qt::NoBrush);
Chris@119 3228
Chris@119 3229 int lasty = 0;
Chris@119 3230 int lastdb = 0;
Chris@119 3231
Chris@40 3232 for (int i = 0; i < ch; ++i) {
Chris@119 3233
Chris@905 3234 double dBval = dBmin + (((dBmax - dBmin) * i) / (ch - 1));
Chris@119 3235 int idb = int(dBval);
Chris@119 3236
Chris@905 3237 double value = AudioLevel::dB_to_multiplier(dBval);
Chris@119 3238 int colour = getDisplayValue(v, value * m_gain);
Chris@210 3239
Chris@907 3240 paint.setPen(m_palette.getColour((unsigned char)colour));
Chris@119 3241
Chris@119 3242 int y = textHeight * topLines + 4 + ch - i;
Chris@119 3243
Chris@119 3244 paint.drawLine(5 + cw - cbw, y, cw + 2, y);
Chris@119 3245
Chris@119 3246 if (i == 0) {
Chris@119 3247 lasty = y;
Chris@119 3248 lastdb = idb;
Chris@119 3249 } else if (i < ch - paint.fontMetrics().ascent() &&
Chris@120 3250 idb != lastdb &&
Chris@119 3251 ((abs(y - lasty) > textHeight &&
Chris@119 3252 idb % 10 == 0) ||
Chris@119 3253 (abs(y - lasty) > paint.fontMetrics().ascent() &&
Chris@119 3254 idb % 5 == 0))) {
Chris@287 3255 paint.setPen(v->getBackground());
Chris@119 3256 QString text = QString("%1").arg(idb);
Chris@119 3257 paint.drawText(3 + cw - cbw - paint.fontMetrics().width(text),
Chris@119 3258 y + toff + textHeight/2, text);
Chris@287 3259 paint.setPen(v->getForeground());
Chris@119 3260 paint.drawLine(5 + cw - cbw, y, 8 + cw - cbw, y);
Chris@119 3261 lasty = y;
Chris@119 3262 lastdb = idb;
Chris@119 3263 }
Chris@40 3264 }
Chris@40 3265 paint.restore();
Chris@40 3266 }
Chris@40 3267
Chris@40 3268 paint.drawLine(cw + 7, 0, cw + 7, h);
Chris@40 3269
Chris@0 3270 int bin = -1;
Chris@0 3271
Chris@918 3272 for (int y = 0; y < v->getPaintHeight(); ++y) {
Chris@0 3273
Chris@905 3274 double q0, q1;
Chris@918 3275 if (!getYBinRange(v, v->getPaintHeight() - y, q0, q1)) continue;
Chris@0 3276
Chris@0 3277 int vy;
Chris@0 3278
Chris@0 3279 if (int(q0) > bin) {
Chris@0 3280 vy = y;
Chris@0 3281 bin = int(q0);
Chris@0 3282 } else {
Chris@0 3283 continue;
Chris@0 3284 }
Chris@0 3285
Chris@907 3286 int freq = int((sr * bin) / m_fftSize);
Chris@0 3287
Chris@0 3288 if (py >= 0 && (vy - py) < textHeight - 1) {
Chris@40 3289 if (m_frequencyScale == LinearFrequencyScale) {
Chris@40 3290 paint.drawLine(w - tickw, h - vy, w, h - vy);
Chris@40 3291 }
Chris@0 3292 continue;
Chris@0 3293 }
Chris@0 3294
Chris@0 3295 QString text = QString("%1").arg(freq);
Chris@234 3296 if (bin == 1) text = tr("%1Hz").arg(freq); // bin 0 is DC
Chris@40 3297 paint.drawLine(cw + 7, h - vy, w - pkw - 1, h - vy);
Chris@0 3298
Chris@0 3299 if (h - vy - textHeight >= -2) {
Chris@40 3300 int tx = w - 3 - paint.fontMetrics().width(text) - std::max(tickw, pkw);
Chris@0 3301 paint.drawText(tx, h - vy + toff, text);
Chris@0 3302 }
Chris@0 3303
Chris@0 3304 py = vy;
Chris@0 3305 }
Chris@40 3306
Chris@40 3307 if (m_frequencyScale == LogFrequencyScale) {
Chris@40 3308
Chris@277 3309 // piano keyboard
Chris@277 3310
Chris@690 3311 PianoScale().paintPianoVertical
Chris@690 3312 (v, paint, QRect(w - pkw - 1, 0, pkw, h),
Chris@690 3313 getEffectiveMinFrequency(), getEffectiveMaxFrequency());
Chris@40 3314 }
Chris@608 3315
Chris@608 3316 m_haveDetailedScale = detailed;
Chris@0 3317 }
Chris@0 3318
Chris@187 3319 class SpectrogramRangeMapper : public RangeMapper
Chris@187 3320 {
Chris@187 3321 public:
Chris@901 3322 SpectrogramRangeMapper(sv_samplerate_t sr, int /* fftsize */) :
Chris@901 3323 m_dist(sr / 2),
Chris@901 3324 m_s2(sqrt(sqrt(2))) { }
Chris@187 3325 ~SpectrogramRangeMapper() { }
Chris@187 3326
Chris@901 3327 virtual int getPositionForValue(double value) const {
Chris@901 3328
Chris@901 3329 double dist = m_dist;
Chris@187 3330
Chris@187 3331 int n = 0;
Chris@187 3332
Chris@901 3333 while (dist > (value + 0.00001) && dist > 0.1) {
Chris@187 3334 dist /= m_s2;
Chris@187 3335 ++n;
Chris@187 3336 }
Chris@187 3337
Chris@187 3338 return n;
Chris@187 3339 }
Chris@724 3340
Chris@901 3341 virtual int getPositionForValueUnclamped(double value) const {
Chris@724 3342 // We don't really support this
Chris@724 3343 return getPositionForValue(value);
Chris@724 3344 }
Chris@187 3345
Chris@901 3346 virtual double getValueForPosition(int position) const {
Chris@187 3347
Chris@187 3348 // Vertical zoom step 0 shows the entire range from DC ->
Chris@187 3349 // Nyquist frequency. Step 1 shows 2^(1/4) of the range of
Chris@187 3350 // step 0, and so on until the visible range is smaller than
Chris@187 3351 // the frequency step between bins at the current fft size.
Chris@187 3352
Chris@901 3353 double dist = m_dist;
Chris@187 3354
Chris@187 3355 int n = 0;
Chris@187 3356 while (n < position) {
Chris@187 3357 dist /= m_s2;
Chris@187 3358 ++n;
Chris@187 3359 }
Chris@187 3360
Chris@187 3361 return dist;
Chris@187 3362 }
Chris@187 3363
Chris@901 3364 virtual double getValueForPositionUnclamped(int position) const {
Chris@724 3365 // We don't really support this
Chris@724 3366 return getValueForPosition(position);
Chris@724 3367 }
Chris@724 3368
Chris@187 3369 virtual QString getUnit() const { return "Hz"; }
Chris@187 3370
Chris@187 3371 protected:
Chris@901 3372 double m_dist;
Chris@901 3373 double m_s2;
Chris@187 3374 };
Chris@187 3375
Chris@133 3376 int
Chris@133 3377 SpectrogramLayer::getVerticalZoomSteps(int &defaultStep) const
Chris@133 3378 {
Chris@135 3379 if (!m_model) return 0;
Chris@187 3380
Chris@907 3381 sv_samplerate_t sr = m_model->getSampleRate();
Chris@187 3382
Chris@187 3383 SpectrogramRangeMapper mapper(sr, m_fftSize);
Chris@187 3384
Chris@905 3385 // int maxStep = mapper.getPositionForValue((double(sr) / m_fftSize) + 0.001);
Chris@187 3386 int maxStep = mapper.getPositionForValue(0);
Chris@905 3387 int minStep = mapper.getPositionForValue(double(sr) / 2);
Chris@250 3388
Chris@805 3389 int initialMax = m_initialMaxFrequency;
Chris@907 3390 if (initialMax == 0) initialMax = int(sr / 2);
Chris@250 3391
Chris@250 3392 defaultStep = mapper.getPositionForValue(initialMax) - minStep;
Chris@250 3393
Chris@587 3394 // SVDEBUG << "SpectrogramLayer::getVerticalZoomSteps: " << maxStep - minStep << " (" << maxStep <<"-" << minStep << "), default is " << defaultStep << " (from initial max freq " << initialMax << ")" << endl;
Chris@187 3395
Chris@187 3396 return maxStep - minStep;
Chris@133 3397 }
Chris@133 3398
Chris@133 3399 int
Chris@133 3400 SpectrogramLayer::getCurrentVerticalZoomStep() const
Chris@133 3401 {
Chris@133 3402 if (!m_model) return 0;
Chris@133 3403
Chris@905 3404 double dmin, dmax;
Chris@133 3405 getDisplayExtents(dmin, dmax);
Chris@133 3406
Chris@187 3407 SpectrogramRangeMapper mapper(m_model->getSampleRate(), m_fftSize);
Chris@187 3408 int n = mapper.getPositionForValue(dmax - dmin);
Chris@587 3409 // SVDEBUG << "SpectrogramLayer::getCurrentVerticalZoomStep: " << n << endl;
Chris@133 3410 return n;
Chris@133 3411 }
Chris@133 3412
Chris@133 3413 void
Chris@133 3414 SpectrogramLayer::setVerticalZoomStep(int step)
Chris@133 3415 {
Chris@187 3416 if (!m_model) return;
Chris@187 3417
Chris@905 3418 double dmin = m_minFrequency, dmax = m_maxFrequency;
Chris@253 3419 // getDisplayExtents(dmin, dmax);
Chris@253 3420
Chris@682 3421 // cerr << "current range " << dmin << " -> " << dmax << ", range " << dmax-dmin << ", mid " << (dmax + dmin)/2 << endl;
Chris@133 3422
Chris@907 3423 sv_samplerate_t sr = m_model->getSampleRate();
Chris@187 3424 SpectrogramRangeMapper mapper(sr, m_fftSize);
Chris@905 3425 double newdist = mapper.getValueForPosition(step);
Chris@905 3426
Chris@905 3427 double newmin, newmax;
Chris@253 3428
Chris@253 3429 if (m_frequencyScale == LogFrequencyScale) {
Chris@253 3430
Chris@253 3431 // need to pick newmin and newmax such that
Chris@253 3432 //
Chris@253 3433 // (log(newmin) + log(newmax)) / 2 == logmid
Chris@253 3434 // and
Chris@253 3435 // newmax - newmin = newdist
Chris@253 3436 //
Chris@253 3437 // so log(newmax - newdist) + log(newmax) == 2logmid
Chris@253 3438 // log(newmax(newmax - newdist)) == 2logmid
Chris@253 3439 // newmax.newmax - newmax.newdist == exp(2logmid)
Chris@253 3440 // newmax^2 + (-newdist)newmax + -exp(2logmid) == 0
Chris@253 3441 // quadratic with a = 1, b = -newdist, c = -exp(2logmid), all known
Chris@253 3442 //
Chris@253 3443 // positive root
Chris@253 3444 // newmax = (newdist + sqrt(newdist^2 + 4exp(2logmid))) / 2
Chris@253 3445 //
Chris@253 3446 // but logmid = (log(dmin) + log(dmax)) / 2
Chris@253 3447 // so exp(2logmid) = exp(log(dmin) + log(dmax))
Chris@253 3448 // = exp(log(dmin.dmax))
Chris@253 3449 // = dmin.dmax
Chris@253 3450 // so newmax = (newdist + sqrtf(newdist^2 + 4dmin.dmax)) / 2
Chris@253 3451
Chris@907 3452 newmax = (newdist + sqrt(newdist*newdist + 4*dmin*dmax)) / 2;
Chris@253 3453 newmin = newmax - newdist;
Chris@253 3454
Chris@682 3455 // cerr << "newmin = " << newmin << ", newmax = " << newmax << endl;
Chris@253 3456
Chris@253 3457 } else {
Chris@905 3458 double dmid = (dmax + dmin) / 2;
Chris@253 3459 newmin = dmid - newdist / 2;
Chris@253 3460 newmax = dmid + newdist / 2;
Chris@253 3461 }
Chris@187 3462
Chris@905 3463 double mmin, mmax;
Chris@187 3464 mmin = 0;
Chris@905 3465 mmax = double(sr) / 2;
Chris@133 3466
Chris@187 3467 if (newmin < mmin) {
Chris@187 3468 newmax += (mmin - newmin);
Chris@187 3469 newmin = mmin;
Chris@187 3470 }
Chris@187 3471 if (newmax > mmax) {
Chris@187 3472 newmax = mmax;
Chris@187 3473 }
Chris@133 3474
Chris@587 3475 // SVDEBUG << "SpectrogramLayer::setVerticalZoomStep: " << step << ": " << newmin << " -> " << newmax << " (range " << newdist << ")" << endl;
Chris@253 3476
Chris@907 3477 setMinFrequency(int(lrint(newmin)));
Chris@907 3478 setMaxFrequency(int(lrint(newmax)));
Chris@187 3479 }
Chris@187 3480
Chris@187 3481 RangeMapper *
Chris@187 3482 SpectrogramLayer::getNewVerticalZoomRangeMapper() const
Chris@187 3483 {
Chris@187 3484 if (!m_model) return 0;
Chris@187 3485 return new SpectrogramRangeMapper(m_model->getSampleRate(), m_fftSize);
Chris@133 3486 }
Chris@133 3487
Chris@273 3488 void
Chris@918 3489 SpectrogramLayer::updateMeasureRectYCoords(LayerGeometryProvider *v, const MeasureRect &r) const
Chris@273 3490 {
Chris@273 3491 int y0 = 0;
Chris@907 3492 if (r.startY > 0.0) y0 = int(getYForFrequency(v, r.startY));
Chris@273 3493
Chris@273 3494 int y1 = y0;
Chris@907 3495 if (r.endY > 0.0) y1 = int(getYForFrequency(v, r.endY));
Chris@273 3496
Chris@587 3497 // SVDEBUG << "SpectrogramLayer::updateMeasureRectYCoords: start " << r.startY << " -> " << y0 << ", end " << r.endY << " -> " << y1 << endl;
Chris@273 3498
Chris@273 3499 r.pixrect = QRect(r.pixrect.x(), y0, r.pixrect.width(), y1 - y0);
Chris@273 3500 }
Chris@273 3501
Chris@273 3502 void
Chris@918 3503 SpectrogramLayer::setMeasureRectYCoord(LayerGeometryProvider *v, MeasureRect &r, bool start, int y) const
Chris@273 3504 {
Chris@273 3505 if (start) {
Chris@273 3506 r.startY = getFrequencyForY(v, y);
Chris@273 3507 r.endY = r.startY;
Chris@273 3508 } else {
Chris@273 3509 r.endY = getFrequencyForY(v, y);
Chris@273 3510 }
Chris@587 3511 // SVDEBUG << "SpectrogramLayer::setMeasureRectYCoord: start " << r.startY << " <- " << y << ", end " << r.endY << " <- " << y << endl;
Chris@273 3512
Chris@273 3513 }
Chris@273 3514
Chris@316 3515 void
Chris@316 3516 SpectrogramLayer::toXml(QTextStream &stream,
Chris@316 3517 QString indent, QString extraAttributes) const
Chris@6 3518 {
Chris@6 3519 QString s;
Chris@6 3520
Chris@6 3521 s += QString("channel=\"%1\" "
Chris@6 3522 "windowSize=\"%2\" "
Chris@153 3523 "windowHopLevel=\"%3\" "
Chris@153 3524 "gain=\"%4\" "
Chris@153 3525 "threshold=\"%5\" ")
Chris@6 3526 .arg(m_channel)
Chris@6 3527 .arg(m_windowSize)
Chris@97 3528 .arg(m_windowHopLevel)
Chris@37 3529 .arg(m_gain)
Chris@37 3530 .arg(m_threshold);
Chris@37 3531
Chris@37 3532 s += QString("minFrequency=\"%1\" "
Chris@37 3533 "maxFrequency=\"%2\" "
Chris@37 3534 "colourScale=\"%3\" "
Chris@37 3535 "colourScheme=\"%4\" "
Chris@37 3536 "colourRotation=\"%5\" "
Chris@37 3537 "frequencyScale=\"%6\" "
Chris@761 3538 "binDisplay=\"%7\" ")
Chris@37 3539 .arg(m_minFrequency)
Chris@6 3540 .arg(m_maxFrequency)
Chris@6 3541 .arg(m_colourScale)
Chris@197 3542 .arg(m_colourMap)
Chris@37 3543 .arg(m_colourRotation)
Chris@35 3544 .arg(m_frequencyScale)
Chris@761 3545 .arg(m_binDisplay);
Chris@761 3546
Chris@1009 3547 // New-style normalization attributes, allowing for more types of
Chris@1009 3548 // normalization in future: write out the column normalization
Chris@1009 3549 // type separately, and then whether we are normalizing visible
Chris@1009 3550 // area as well afterwards
Chris@1009 3551
Chris@1009 3552 s += QString("columnNormalization=\"%1\" ")
Chris@1009 3553 .arg(m_normalization == NormalizeColumns ? "peak" :
Chris@1009 3554 m_normalization == NormalizeHybrid ? "hybrid" : "none");
Chris@1009 3555
Chris@1009 3556 // Old-style normalization attribute. We *don't* write out
Chris@1009 3557 // normalizeHybrid here because the only release that would accept
Chris@1009 3558 // it (Tony v1.0) has a totally different scale factor for
Chris@1009 3559 // it. We'll just have to accept that session files from Tony
Chris@1009 3560 // v2.0+ will look odd in Tony v1.0
Chris@1009 3561
Chris@1009 3562 s += QString("normalizeColumns=\"%1\" ")
Chris@1009 3563 .arg(m_normalization == NormalizeColumns ? "true" : "false");
Chris@1009 3564
Chris@1009 3565 // And this applies to both old- and new-style attributes
Chris@1009 3566
Chris@1009 3567 s += QString("normalizeVisibleArea=\"%1\" ")
Chris@1009 3568 .arg(m_normalization == NormalizeVisibleArea ? "true" : "false");
Chris@1009 3569
Chris@316 3570 Layer::toXml(stream, indent, extraAttributes + " " + s);
Chris@6 3571 }
Chris@6 3572
Chris@11 3573 void
Chris@11 3574 SpectrogramLayer::setProperties(const QXmlAttributes &attributes)
Chris@11 3575 {
Chris@11 3576 bool ok = false;
Chris@11 3577
Chris@11 3578 int channel = attributes.value("channel").toInt(&ok);
Chris@11 3579 if (ok) setChannel(channel);
Chris@11 3580
Chris@805 3581 int windowSize = attributes.value("windowSize").toUInt(&ok);
Chris@11 3582 if (ok) setWindowSize(windowSize);
Chris@11 3583
Chris@805 3584 int windowHopLevel = attributes.value("windowHopLevel").toUInt(&ok);
Chris@97 3585 if (ok) setWindowHopLevel(windowHopLevel);
Chris@97 3586 else {
Chris@805 3587 int windowOverlap = attributes.value("windowOverlap").toUInt(&ok);
Chris@97 3588 // a percentage value
Chris@97 3589 if (ok) {
Chris@97 3590 if (windowOverlap == 0) setWindowHopLevel(0);
Chris@97 3591 else if (windowOverlap == 25) setWindowHopLevel(1);
Chris@97 3592 else if (windowOverlap == 50) setWindowHopLevel(2);
Chris@97 3593 else if (windowOverlap == 75) setWindowHopLevel(3);
Chris@97 3594 else if (windowOverlap == 90) setWindowHopLevel(4);
Chris@97 3595 }
Chris@97 3596 }
Chris@11 3597
Chris@11 3598 float gain = attributes.value("gain").toFloat(&ok);
Chris@11 3599 if (ok) setGain(gain);
Chris@11 3600
Chris@37 3601 float threshold = attributes.value("threshold").toFloat(&ok);
Chris@37 3602 if (ok) setThreshold(threshold);
Chris@37 3603
Chris@805 3604 int minFrequency = attributes.value("minFrequency").toUInt(&ok);
Chris@187 3605 if (ok) {
Chris@587 3606 SVDEBUG << "SpectrogramLayer::setProperties: setting min freq to " << minFrequency << endl;
Chris@187 3607 setMinFrequency(minFrequency);
Chris@187 3608 }
Chris@37 3609
Chris@805 3610 int maxFrequency = attributes.value("maxFrequency").toUInt(&ok);
Chris@187 3611 if (ok) {
Chris@587 3612 SVDEBUG << "SpectrogramLayer::setProperties: setting max freq to " << maxFrequency << endl;
Chris@187 3613 setMaxFrequency(maxFrequency);
Chris@187 3614 }
Chris@11 3615
Chris@11 3616 ColourScale colourScale = (ColourScale)
Chris@11 3617 attributes.value("colourScale").toInt(&ok);
Chris@11 3618 if (ok) setColourScale(colourScale);
Chris@11 3619
Chris@197 3620 int colourMap = attributes.value("colourScheme").toInt(&ok);
Chris@197 3621 if (ok) setColourMap(colourMap);
Chris@11 3622
Chris@37 3623 int colourRotation = attributes.value("colourRotation").toInt(&ok);
Chris@37 3624 if (ok) setColourRotation(colourRotation);
Chris@37 3625
Chris@11 3626 FrequencyScale frequencyScale = (FrequencyScale)
Chris@11 3627 attributes.value("frequencyScale").toInt(&ok);
Chris@11 3628 if (ok) setFrequencyScale(frequencyScale);
Chris@35 3629
Chris@37 3630 BinDisplay binDisplay = (BinDisplay)
Chris@37 3631 attributes.value("binDisplay").toInt(&ok);
Chris@37 3632 if (ok) setBinDisplay(binDisplay);
Chris@36 3633
Chris@1009 3634 bool haveNewStyleNormalization = false;
Chris@1009 3635
Chris@1009 3636 QString columnNormalization = attributes.value("columnNormalization");
Chris@1009 3637
Chris@1009 3638 if (columnNormalization != "") {
Chris@1009 3639
Chris@1009 3640 haveNewStyleNormalization = true;
Chris@1009 3641
Chris@1009 3642 if (columnNormalization == "peak") {
Chris@1009 3643 setNormalization(NormalizeColumns);
Chris@1009 3644 } else if (columnNormalization == "hybrid") {
Chris@1009 3645 setNormalization(NormalizeHybrid);
Chris@1009 3646 } else if (columnNormalization == "none") {
Chris@1009 3647 // do nothing
Chris@1009 3648 } else {
Chris@1009 3649 cerr << "NOTE: Unknown or unsupported columnNormalization attribute \""
Chris@1009 3650 << columnNormalization << "\"" << endl;
Chris@1009 3651 }
Chris@1009 3652 }
Chris@1009 3653
Chris@1009 3654 if (!haveNewStyleNormalization) {
Chris@1009 3655
Chris@1009 3656 bool normalizeColumns =
Chris@1009 3657 (attributes.value("normalizeColumns").trimmed() == "true");
Chris@1009 3658 if (normalizeColumns) {
Chris@1009 3659 setNormalization(NormalizeColumns);
Chris@1009 3660 }
Chris@1009 3661
Chris@1009 3662 bool normalizeHybrid =
Chris@1009 3663 (attributes.value("normalizeHybrid").trimmed() == "true");
Chris@1009 3664 if (normalizeHybrid) {
Chris@1009 3665 setNormalization(NormalizeHybrid);
Chris@1009 3666 }
Chris@862 3667 }
Chris@153 3668
Chris@153 3669 bool normalizeVisibleArea =
Chris@153 3670 (attributes.value("normalizeVisibleArea").trimmed() == "true");
Chris@862 3671 if (normalizeVisibleArea) {
Chris@862 3672 setNormalization(NormalizeVisibleArea);
Chris@862 3673 }
Chris@761 3674
Chris@1009 3675 if (!haveNewStyleNormalization && m_normalization == NormalizeHybrid) {
Chris@1009 3676 // Tony v1.0 is (and hopefully will remain!) the only released
Chris@1009 3677 // SV-a-like to use old-style attributes when saving sessions
Chris@1009 3678 // that ask for hybrid normalization. It saves them with the
Chris@1009 3679 // wrong gain factor, so hack in a fix for that here -- this
Chris@1009 3680 // gives us backward but not forward compatibility.
Chris@1009 3681 setGain(m_gain / float(m_fftSize / 2));
Chris@862 3682 }
Chris@11 3683 }
Chris@11 3684