annotate layer/SpectrogramLayer.cpp @ 985:8053c0dfa919 simple-fft-model

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