annotate layer/SpectrogramLayer.cpp @ 196:22c99c8aa1e0

* Add separate colour mapping unit; use it in spectrogram (colour 3d plot to follow) * Add another colour scheme resembling that of a noted commercial application
author Chris Cannam
date Wed, 31 Jan 2007 12:13:47 +0000
parents 57c2350a8c40
children 6b023411087b
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@182 7 This file copyright 2006 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@196 25 #include "ColourMapper.h"
Chris@0 26
Chris@0 27 #include <QPainter>
Chris@0 28 #include <QImage>
Chris@0 29 #include <QPixmap>
Chris@0 30 #include <QRect>
Chris@0 31 #include <QTimer>
Chris@92 32 #include <QApplication>
Chris@178 33 #include <QMessageBox>
Chris@0 34
Chris@0 35 #include <iostream>
Chris@0 36
Chris@0 37 #include <cassert>
Chris@0 38 #include <cmath>
Chris@0 39
Chris@174 40 //#define DEBUG_SPECTROGRAM_REPAINT 1
Chris@0 41
Chris@44 42 SpectrogramLayer::SpectrogramLayer(Configuration config) :
Chris@0 43 m_model(0),
Chris@0 44 m_channel(0),
Chris@0 45 m_windowSize(1024),
Chris@0 46 m_windowType(HanningWindow),
Chris@97 47 m_windowHopLevel(2),
Chris@109 48 m_zeroPadLevel(0),
Chris@107 49 m_fftSize(1024),
Chris@0 50 m_gain(1.0),
Chris@37 51 m_threshold(0.0),
Chris@9 52 m_colourRotation(0),
Chris@119 53 m_minFrequency(10),
Chris@0 54 m_maxFrequency(8000),
Chris@135 55 m_initialMaxFrequency(8000),
Chris@0 56 m_colourScale(dBColourScale),
Chris@196 57 m_colourScheme(0),
Chris@0 58 m_frequencyScale(LinearFrequencyScale),
Chris@37 59 m_binDisplay(AllBins),
Chris@36 60 m_normalizeColumns(false),
Chris@120 61 m_normalizeVisibleArea(false),
Chris@133 62 m_lastEmittedZoomStep(-1),
Chris@0 63 m_updateTimer(0),
Chris@44 64 m_candidateFillStartFrame(0),
Chris@193 65 m_exiting(false),
Chris@193 66 m_sliceableModel(0)
Chris@0 67 {
Chris@0 68 if (config == MelodicRange) {
Chris@0 69 setWindowSize(8192);
Chris@97 70 setWindowHopLevel(4);
Chris@109 71 // setWindowType(ParzenWindow);
Chris@135 72 m_initialMaxFrequency = 1000;
Chris@0 73 setMaxFrequency(1000);
Chris@0 74 setColourScale(LinearColourScale);
Chris@37 75 } else if (config == MelodicPeaks) {
Chris@37 76 setWindowSize(4096);
Chris@97 77 setWindowHopLevel(5);
Chris@109 78 // setWindowType(BlackmanWindow);
Chris@135 79 m_initialMaxFrequency = 2000;
Chris@40 80 setMaxFrequency(2000);
Chris@37 81 setMinFrequency(40);
Chris@37 82 setFrequencyScale(LogFrequencyScale);
Chris@41 83 setColourScale(MeterColourScale);
Chris@37 84 setBinDisplay(PeakFrequencies);
Chris@37 85 setNormalizeColumns(true);
Chris@0 86 }
Chris@110 87
Chris@122 88 Preferences *prefs = Preferences::getInstance();
Chris@122 89 connect(prefs, SIGNAL(propertyChanged(PropertyContainer::PropertyName)),
Chris@122 90 this, SLOT(preferenceChanged(PropertyContainer::PropertyName)));
Chris@122 91 setWindowType(prefs->getWindowType());
Chris@122 92
Chris@110 93 setColourmap();
Chris@0 94 }
Chris@0 95
Chris@0 96 SpectrogramLayer::~SpectrogramLayer()
Chris@0 97 {
Chris@0 98 delete m_updateTimer;
Chris@0 99 m_updateTimer = 0;
Chris@0 100
Chris@130 101 invalidateFFTModels();
Chris@0 102 }
Chris@0 103
Chris@0 104 void
Chris@0 105 SpectrogramLayer::setModel(const DenseTimeValueModel *model)
Chris@0 106 {
Chris@101 107 // std::cerr << "SpectrogramLayer(" << this << "): setModel(" << model << ")" << std::endl;
Chris@34 108
Chris@110 109 if (model == m_model) return;
Chris@110 110
Chris@0 111 m_model = model;
Chris@130 112 invalidateFFTModels();
Chris@0 113
Chris@0 114 if (!m_model || !m_model->isOK()) return;
Chris@0 115
Chris@0 116 connect(m_model, SIGNAL(modelChanged()), this, SIGNAL(modelChanged()));
Chris@0 117 connect(m_model, SIGNAL(modelChanged(size_t, size_t)),
Chris@0 118 this, SIGNAL(modelChanged(size_t, size_t)));
Chris@0 119
Chris@0 120 connect(m_model, SIGNAL(completionChanged()),
Chris@0 121 this, SIGNAL(modelCompletionChanged()));
Chris@0 122
Chris@0 123 connect(m_model, SIGNAL(modelChanged()), this, SLOT(cacheInvalid()));
Chris@0 124 connect(m_model, SIGNAL(modelChanged(size_t, size_t)),
Chris@0 125 this, SLOT(cacheInvalid(size_t, size_t)));
Chris@0 126
Chris@0 127 emit modelReplaced();
Chris@110 128 }
Chris@115 129
Chris@0 130 Layer::PropertyList
Chris@0 131 SpectrogramLayer::getProperties() const
Chris@0 132 {
Chris@0 133 PropertyList list;
Chris@87 134 list.push_back("Colour");
Chris@87 135 list.push_back("Colour Scale");
Chris@87 136 list.push_back("Window Size");
Chris@97 137 list.push_back("Window Increment");
Chris@87 138 list.push_back("Normalize Columns");
Chris@120 139 list.push_back("Normalize Visible Area");
Chris@87 140 list.push_back("Bin Display");
Chris@87 141 list.push_back("Threshold");
Chris@87 142 list.push_back("Gain");
Chris@87 143 list.push_back("Colour Rotation");
Chris@153 144 // list.push_back("Min Frequency");
Chris@153 145 // list.push_back("Max Frequency");
Chris@87 146 list.push_back("Frequency Scale");
Chris@153 147 //// list.push_back("Zero Padding");
Chris@0 148 return list;
Chris@0 149 }
Chris@0 150
Chris@87 151 QString
Chris@87 152 SpectrogramLayer::getPropertyLabel(const PropertyName &name) const
Chris@87 153 {
Chris@87 154 if (name == "Colour") return tr("Colour");
Chris@87 155 if (name == "Colour Scale") return tr("Colour Scale");
Chris@87 156 if (name == "Window Size") return tr("Window Size");
Chris@112 157 if (name == "Window Increment") return tr("Window Overlap");
Chris@87 158 if (name == "Normalize Columns") return tr("Normalize Columns");
Chris@120 159 if (name == "Normalize Visible Area") return tr("Normalize Visible Area");
Chris@87 160 if (name == "Bin Display") return tr("Bin Display");
Chris@87 161 if (name == "Threshold") return tr("Threshold");
Chris@87 162 if (name == "Gain") return tr("Gain");
Chris@87 163 if (name == "Colour Rotation") return tr("Colour Rotation");
Chris@87 164 if (name == "Min Frequency") return tr("Min Frequency");
Chris@87 165 if (name == "Max Frequency") return tr("Max Frequency");
Chris@87 166 if (name == "Frequency Scale") return tr("Frequency Scale");
Chris@109 167 if (name == "Zero Padding") return tr("Smoothing");
Chris@87 168 return "";
Chris@87 169 }
Chris@87 170
Chris@0 171 Layer::PropertyType
Chris@0 172 SpectrogramLayer::getPropertyType(const PropertyName &name) const
Chris@0 173 {
Chris@87 174 if (name == "Gain") return RangeProperty;
Chris@87 175 if (name == "Colour Rotation") return RangeProperty;
Chris@87 176 if (name == "Normalize Columns") return ToggleProperty;
Chris@120 177 if (name == "Normalize Visible Area") return ToggleProperty;
Chris@87 178 if (name == "Threshold") return RangeProperty;
Chris@109 179 if (name == "Zero Padding") return ToggleProperty;
Chris@0 180 return ValueProperty;
Chris@0 181 }
Chris@0 182
Chris@0 183 QString
Chris@0 184 SpectrogramLayer::getPropertyGroupName(const PropertyName &name) const
Chris@0 185 {
Chris@153 186 if (name == "Bin Display" ||
Chris@153 187 name == "Frequency Scale") return tr("Bins");
Chris@87 188 if (name == "Window Size" ||
Chris@109 189 name == "Window Increment" ||
Chris@109 190 name == "Zero Padding") return tr("Window");
Chris@87 191 if (name == "Colour" ||
Chris@153 192 // name == "Gain" ||
Chris@87 193 name == "Threshold" ||
Chris@87 194 name == "Colour Rotation") return tr("Colour");
Chris@87 195 if (name == "Normalize Columns" ||
Chris@120 196 name == "Normalize Visible Area" ||
Chris@153 197 // name == "Bin Display" ||
Chris@153 198 name == "Gain" ||
Chris@87 199 name == "Colour Scale") return tr("Scale");
Chris@153 200 // if (name == "Max Frequency" ||
Chris@153 201 // name == "Min Frequency" ||
Chris@153 202 // name == "Frequency Scale" ||
Chris@153 203 // name == "Frequency Adjustment") return tr("Range");
Chris@0 204 return QString();
Chris@0 205 }
Chris@0 206
Chris@0 207 int
Chris@0 208 SpectrogramLayer::getPropertyRangeAndValue(const PropertyName &name,
Chris@55 209 int *min, int *max) const
Chris@0 210 {
Chris@0 211 int deft = 0;
Chris@0 212
Chris@55 213 int garbage0, garbage1;
Chris@55 214 if (!min) min = &garbage0;
Chris@55 215 if (!max) max = &garbage1;
Chris@10 216
Chris@87 217 if (name == "Gain") {
Chris@0 218
Chris@0 219 *min = -50;
Chris@0 220 *max = 50;
Chris@0 221
Chris@0 222 deft = lrint(log10(m_gain) * 20.0);
Chris@0 223 if (deft < *min) deft = *min;
Chris@0 224 if (deft > *max) deft = *max;
Chris@0 225
Chris@87 226 } else if (name == "Threshold") {
Chris@37 227
Chris@37 228 *min = -50;
Chris@37 229 *max = 0;
Chris@37 230
Chris@37 231 deft = lrintf(AudioLevel::multiplier_to_dB(m_threshold));
Chris@37 232 if (deft < *min) deft = *min;
Chris@37 233 if (deft > *max) deft = *max;
Chris@37 234
Chris@87 235 } else if (name == "Colour Rotation") {
Chris@9 236
Chris@9 237 *min = 0;
Chris@9 238 *max = 256;
Chris@9 239
Chris@9 240 deft = m_colourRotation;
Chris@9 241
Chris@87 242 } else if (name == "Colour Scale") {
Chris@0 243
Chris@0 244 *min = 0;
Chris@176 245 *max = 4;
Chris@0 246
Chris@0 247 deft = (int)m_colourScale;
Chris@0 248
Chris@87 249 } else if (name == "Colour") {
Chris@0 250
Chris@0 251 *min = 0;
Chris@196 252 *max = ColourMapper::getColourMapCount() - 1;
Chris@196 253
Chris@196 254 deft = m_colourScheme;
Chris@0 255
Chris@87 256 } else if (name == "Window Size") {
Chris@0 257
Chris@0 258 *min = 0;
Chris@0 259 *max = 10;
Chris@0 260
Chris@0 261 deft = 0;
Chris@0 262 int ws = m_windowSize;
Chris@0 263 while (ws > 32) { ws >>= 1; deft ++; }
Chris@0 264
Chris@97 265 } else if (name == "Window Increment") {
Chris@0 266
Chris@0 267 *min = 0;
Chris@97 268 *max = 5;
Chris@0 269
Chris@97 270 deft = m_windowHopLevel;
Chris@0 271
Chris@109 272 } else if (name == "Zero Padding") {
Chris@109 273
Chris@109 274 *min = 0;
Chris@109 275 *max = 1;
Chris@109 276
Chris@109 277 deft = m_zeroPadLevel > 0 ? 1 : 0;
Chris@109 278
Chris@87 279 } else if (name == "Min Frequency") {
Chris@37 280
Chris@37 281 *min = 0;
Chris@37 282 *max = 9;
Chris@37 283
Chris@37 284 switch (m_minFrequency) {
Chris@37 285 case 0: default: deft = 0; break;
Chris@37 286 case 10: deft = 1; break;
Chris@37 287 case 20: deft = 2; break;
Chris@37 288 case 40: deft = 3; break;
Chris@37 289 case 100: deft = 4; break;
Chris@37 290 case 250: deft = 5; break;
Chris@37 291 case 500: deft = 6; break;
Chris@37 292 case 1000: deft = 7; break;
Chris@37 293 case 4000: deft = 8; break;
Chris@37 294 case 10000: deft = 9; break;
Chris@37 295 }
Chris@37 296
Chris@87 297 } else if (name == "Max Frequency") {
Chris@0 298
Chris@0 299 *min = 0;
Chris@0 300 *max = 9;
Chris@0 301
Chris@0 302 switch (m_maxFrequency) {
Chris@0 303 case 500: deft = 0; break;
Chris@0 304 case 1000: deft = 1; break;
Chris@0 305 case 1500: deft = 2; break;
Chris@0 306 case 2000: deft = 3; break;
Chris@0 307 case 4000: deft = 4; break;
Chris@0 308 case 6000: deft = 5; break;
Chris@0 309 case 8000: deft = 6; break;
Chris@0 310 case 12000: deft = 7; break;
Chris@0 311 case 16000: deft = 8; break;
Chris@0 312 default: deft = 9; break;
Chris@0 313 }
Chris@0 314
Chris@87 315 } else if (name == "Frequency Scale") {
Chris@0 316
Chris@0 317 *min = 0;
Chris@0 318 *max = 1;
Chris@0 319 deft = (int)m_frequencyScale;
Chris@0 320
Chris@87 321 } else if (name == "Bin Display") {
Chris@35 322
Chris@35 323 *min = 0;
Chris@35 324 *max = 2;
Chris@37 325 deft = (int)m_binDisplay;
Chris@35 326
Chris@87 327 } else if (name == "Normalize Columns") {
Chris@36 328
Chris@36 329 deft = (m_normalizeColumns ? 1 : 0);
Chris@36 330
Chris@120 331 } else if (name == "Normalize Visible Area") {
Chris@120 332
Chris@120 333 deft = (m_normalizeVisibleArea ? 1 : 0);
Chris@120 334
Chris@0 335 } else {
Chris@0 336 deft = Layer::getPropertyRangeAndValue(name, min, max);
Chris@0 337 }
Chris@0 338
Chris@0 339 return deft;
Chris@0 340 }
Chris@0 341
Chris@0 342 QString
Chris@0 343 SpectrogramLayer::getPropertyValueLabel(const PropertyName &name,
Chris@9 344 int value) const
Chris@0 345 {
Chris@87 346 if (name == "Colour") {
Chris@196 347 return ColourMapper::getColourMapName(value);
Chris@0 348 }
Chris@87 349 if (name == "Colour Scale") {
Chris@0 350 switch (value) {
Chris@0 351 default:
Chris@37 352 case 0: return tr("Linear");
Chris@37 353 case 1: return tr("Meter");
Chris@37 354 case 2: return tr("dB");
Chris@176 355 case 3: return tr("dB^2");
Chris@119 356 case 4: return tr("Phase");
Chris@0 357 }
Chris@0 358 }
Chris@87 359 if (name == "Window Size") {
Chris@0 360 return QString("%1").arg(32 << value);
Chris@0 361 }
Chris@97 362 if (name == "Window Increment") {
Chris@0 363 switch (value) {
Chris@0 364 default:
Chris@112 365 case 0: return tr("None");
Chris@112 366 case 1: return tr("25 %");
Chris@112 367 case 2: return tr("50 %");
Chris@112 368 case 3: return tr("75 %");
Chris@112 369 case 4: return tr("87.5 %");
Chris@112 370 case 5: return tr("93.75 %");
Chris@0 371 }
Chris@0 372 }
Chris@109 373 if (name == "Zero Padding") {
Chris@109 374 if (value == 0) return tr("None");
Chris@109 375 return QString("%1x").arg(value + 1);
Chris@109 376 }
Chris@87 377 if (name == "Min Frequency") {
Chris@37 378 switch (value) {
Chris@37 379 default:
Chris@38 380 case 0: return tr("No min");
Chris@37 381 case 1: return tr("10 Hz");
Chris@37 382 case 2: return tr("20 Hz");
Chris@37 383 case 3: return tr("40 Hz");
Chris@37 384 case 4: return tr("100 Hz");
Chris@37 385 case 5: return tr("250 Hz");
Chris@37 386 case 6: return tr("500 Hz");
Chris@37 387 case 7: return tr("1 KHz");
Chris@37 388 case 8: return tr("4 KHz");
Chris@37 389 case 9: return tr("10 KHz");
Chris@37 390 }
Chris@37 391 }
Chris@87 392 if (name == "Max Frequency") {
Chris@0 393 switch (value) {
Chris@0 394 default:
Chris@0 395 case 0: return tr("500 Hz");
Chris@0 396 case 1: return tr("1 KHz");
Chris@0 397 case 2: return tr("1.5 KHz");
Chris@0 398 case 3: return tr("2 KHz");
Chris@0 399 case 4: return tr("4 KHz");
Chris@0 400 case 5: return tr("6 KHz");
Chris@0 401 case 6: return tr("8 KHz");
Chris@0 402 case 7: return tr("12 KHz");
Chris@0 403 case 8: return tr("16 KHz");
Chris@38 404 case 9: return tr("No max");
Chris@0 405 }
Chris@0 406 }
Chris@87 407 if (name == "Frequency Scale") {
Chris@0 408 switch (value) {
Chris@0 409 default:
Chris@0 410 case 0: return tr("Linear");
Chris@0 411 case 1: return tr("Log");
Chris@0 412 }
Chris@0 413 }
Chris@87 414 if (name == "Bin Display") {
Chris@35 415 switch (value) {
Chris@35 416 default:
Chris@37 417 case 0: return tr("All Bins");
Chris@37 418 case 1: return tr("Peak Bins");
Chris@37 419 case 2: return tr("Frequencies");
Chris@35 420 }
Chris@35 421 }
Chris@0 422 return tr("<unknown>");
Chris@0 423 }
Chris@0 424
Chris@167 425 RangeMapper *
Chris@167 426 SpectrogramLayer::getNewPropertyRangeMapper(const PropertyName &name) const
Chris@167 427 {
Chris@167 428 if (name == "Gain") {
Chris@167 429 return new LinearRangeMapper(-50, 50, -25, 25, tr("dB"));
Chris@167 430 }
Chris@167 431 if (name == "Threshold") {
Chris@167 432 return new LinearRangeMapper(-50, 0, -50, 0, tr("dB"));
Chris@167 433 }
Chris@167 434 return 0;
Chris@167 435 }
Chris@167 436
Chris@0 437 void
Chris@0 438 SpectrogramLayer::setProperty(const PropertyName &name, int value)
Chris@0 439 {
Chris@87 440 if (name == "Gain") {
Chris@0 441 setGain(pow(10, float(value)/20.0));
Chris@87 442 } else if (name == "Threshold") {
Chris@37 443 if (value == -50) setThreshold(0.0);
Chris@37 444 else setThreshold(AudioLevel::dB_to_multiplier(value));
Chris@87 445 } else if (name == "Colour Rotation") {
Chris@9 446 setColourRotation(value);
Chris@87 447 } else if (name == "Colour") {
Chris@196 448 setColourScheme(value);
Chris@87 449 } else if (name == "Window Size") {
Chris@0 450 setWindowSize(32 << value);
Chris@97 451 } else if (name == "Window Increment") {
Chris@97 452 setWindowHopLevel(value);
Chris@109 453 } else if (name == "Zero Padding") {
Chris@109 454 setZeroPadLevel(value > 0.1 ? 3 : 0);
Chris@87 455 } else if (name == "Min Frequency") {
Chris@37 456 switch (value) {
Chris@37 457 default:
Chris@37 458 case 0: setMinFrequency(0); break;
Chris@37 459 case 1: setMinFrequency(10); break;
Chris@37 460 case 2: setMinFrequency(20); break;
Chris@37 461 case 3: setMinFrequency(40); break;
Chris@37 462 case 4: setMinFrequency(100); break;
Chris@37 463 case 5: setMinFrequency(250); break;
Chris@37 464 case 6: setMinFrequency(500); break;
Chris@37 465 case 7: setMinFrequency(1000); break;
Chris@37 466 case 8: setMinFrequency(4000); break;
Chris@37 467 case 9: setMinFrequency(10000); break;
Chris@37 468 }
Chris@133 469 int vs = getCurrentVerticalZoomStep();
Chris@133 470 if (vs != m_lastEmittedZoomStep) {
Chris@133 471 emit verticalZoomChanged();
Chris@133 472 m_lastEmittedZoomStep = vs;
Chris@133 473 }
Chris@87 474 } else if (name == "Max Frequency") {
Chris@0 475 switch (value) {
Chris@0 476 case 0: setMaxFrequency(500); break;
Chris@0 477 case 1: setMaxFrequency(1000); break;
Chris@0 478 case 2: setMaxFrequency(1500); break;
Chris@0 479 case 3: setMaxFrequency(2000); break;
Chris@0 480 case 4: setMaxFrequency(4000); break;
Chris@0 481 case 5: setMaxFrequency(6000); break;
Chris@0 482 case 6: setMaxFrequency(8000); break;
Chris@0 483 case 7: setMaxFrequency(12000); break;
Chris@0 484 case 8: setMaxFrequency(16000); break;
Chris@0 485 default:
Chris@0 486 case 9: setMaxFrequency(0); break;
Chris@0 487 }
Chris@133 488 int vs = getCurrentVerticalZoomStep();
Chris@133 489 if (vs != m_lastEmittedZoomStep) {
Chris@133 490 emit verticalZoomChanged();
Chris@133 491 m_lastEmittedZoomStep = vs;
Chris@133 492 }
Chris@87 493 } else if (name == "Colour Scale") {
Chris@0 494 switch (value) {
Chris@0 495 default:
Chris@0 496 case 0: setColourScale(LinearColourScale); break;
Chris@0 497 case 1: setColourScale(MeterColourScale); break;
Chris@0 498 case 2: setColourScale(dBColourScale); break;
Chris@119 499 case 3: setColourScale(OtherColourScale); break;
Chris@119 500 case 4: setColourScale(PhaseColourScale); break;
Chris@0 501 }
Chris@87 502 } else if (name == "Frequency Scale") {
Chris@0 503 switch (value) {
Chris@0 504 default:
Chris@0 505 case 0: setFrequencyScale(LinearFrequencyScale); break;
Chris@0 506 case 1: setFrequencyScale(LogFrequencyScale); break;
Chris@0 507 }
Chris@87 508 } else if (name == "Bin Display") {
Chris@35 509 switch (value) {
Chris@35 510 default:
Chris@37 511 case 0: setBinDisplay(AllBins); break;
Chris@37 512 case 1: setBinDisplay(PeakBins); break;
Chris@37 513 case 2: setBinDisplay(PeakFrequencies); break;
Chris@35 514 }
Chris@82 515 } else if (name == "Normalize Columns") {
Chris@36 516 setNormalizeColumns(value ? true : false);
Chris@120 517 } else if (name == "Normalize Visible Area") {
Chris@120 518 setNormalizeVisibleArea(value ? true : false);
Chris@0 519 }
Chris@0 520 }
Chris@0 521
Chris@0 522 void
Chris@95 523 SpectrogramLayer::invalidatePixmapCaches()
Chris@95 524 {
Chris@95 525 for (ViewPixmapCache::iterator i = m_pixmapCaches.begin();
Chris@95 526 i != m_pixmapCaches.end(); ++i) {
Chris@95 527 i->second.validArea = QRect();
Chris@95 528 }
Chris@95 529 }
Chris@95 530
Chris@95 531 void
Chris@95 532 SpectrogramLayer::invalidatePixmapCaches(size_t startFrame, size_t endFrame)
Chris@95 533 {
Chris@95 534 for (ViewPixmapCache::iterator i = m_pixmapCaches.begin();
Chris@95 535 i != m_pixmapCaches.end(); ++i) {
Chris@131 536
Chris@95 537 //!!! when are views removed from the map? on setLayerDormant?
Chris@95 538 const View *v = i->first;
Chris@95 539
Chris@115 540 if (startFrame < v->getEndFrame() && int(endFrame) >= v->getStartFrame()) {
Chris@95 541 i->second.validArea = QRect();
Chris@95 542 }
Chris@95 543 }
Chris@95 544 }
Chris@95 545
Chris@95 546 void
Chris@122 547 SpectrogramLayer::preferenceChanged(PropertyContainer::PropertyName name)
Chris@122 548 {
Chris@122 549 std::cerr << "SpectrogramLayer::preferenceChanged(" << name.toStdString() << ")" << std::endl;
Chris@122 550
Chris@122 551 if (name == "Window Type") {
Chris@122 552 setWindowType(Preferences::getInstance()->getWindowType());
Chris@122 553 return;
Chris@122 554 }
Chris@122 555 if (name == "Smooth Spectrogram") {
Chris@122 556 invalidatePixmapCaches();
Chris@122 557 invalidateMagnitudes();
Chris@122 558 emit layerParametersChanged();
Chris@122 559 }
Chris@122 560 if (name == "Tuning Frequency") {
Chris@122 561 emit layerParametersChanged();
Chris@122 562 }
Chris@122 563 }
Chris@122 564
Chris@122 565 void
Chris@0 566 SpectrogramLayer::setChannel(int ch)
Chris@0 567 {
Chris@0 568 if (m_channel == ch) return;
Chris@0 569
Chris@95 570 invalidatePixmapCaches();
Chris@0 571 m_channel = ch;
Chris@130 572 invalidateFFTModels();
Chris@9 573
Chris@0 574 emit layerParametersChanged();
Chris@0 575 }
Chris@0 576
Chris@0 577 int
Chris@0 578 SpectrogramLayer::getChannel() const
Chris@0 579 {
Chris@0 580 return m_channel;
Chris@0 581 }
Chris@0 582
Chris@0 583 void
Chris@0 584 SpectrogramLayer::setWindowSize(size_t ws)
Chris@0 585 {
Chris@0 586 if (m_windowSize == ws) return;
Chris@0 587
Chris@95 588 invalidatePixmapCaches();
Chris@0 589
Chris@0 590 m_windowSize = ws;
Chris@109 591 m_fftSize = ws * (m_zeroPadLevel + 1);
Chris@0 592
Chris@130 593 invalidateFFTModels();
Chris@9 594
Chris@9 595 emit layerParametersChanged();
Chris@0 596 }
Chris@0 597
Chris@0 598 size_t
Chris@0 599 SpectrogramLayer::getWindowSize() const
Chris@0 600 {
Chris@0 601 return m_windowSize;
Chris@0 602 }
Chris@0 603
Chris@0 604 void
Chris@97 605 SpectrogramLayer::setWindowHopLevel(size_t v)
Chris@0 606 {
Chris@97 607 if (m_windowHopLevel == v) return;
Chris@0 608
Chris@95 609 invalidatePixmapCaches();
Chris@0 610
Chris@97 611 m_windowHopLevel = v;
Chris@0 612
Chris@130 613 invalidateFFTModels();
Chris@9 614
Chris@9 615 emit layerParametersChanged();
Chris@9 616
Chris@110 617 // fillCache();
Chris@0 618 }
Chris@0 619
Chris@0 620 size_t
Chris@97 621 SpectrogramLayer::getWindowHopLevel() const
Chris@0 622 {
Chris@97 623 return m_windowHopLevel;
Chris@0 624 }
Chris@0 625
Chris@0 626 void
Chris@109 627 SpectrogramLayer::setZeroPadLevel(size_t v)
Chris@109 628 {
Chris@109 629 if (m_zeroPadLevel == v) return;
Chris@109 630
Chris@109 631 invalidatePixmapCaches();
Chris@109 632
Chris@109 633 m_zeroPadLevel = v;
Chris@109 634 m_fftSize = m_windowSize * (v + 1);
Chris@110 635
Chris@130 636 invalidateFFTModels();
Chris@109 637
Chris@109 638 emit layerParametersChanged();
Chris@109 639 }
Chris@109 640
Chris@109 641 size_t
Chris@109 642 SpectrogramLayer::getZeroPadLevel() const
Chris@109 643 {
Chris@109 644 return m_zeroPadLevel;
Chris@109 645 }
Chris@109 646
Chris@109 647 void
Chris@0 648 SpectrogramLayer::setWindowType(WindowType w)
Chris@0 649 {
Chris@0 650 if (m_windowType == w) return;
Chris@0 651
Chris@95 652 invalidatePixmapCaches();
Chris@0 653
Chris@0 654 m_windowType = w;
Chris@110 655
Chris@130 656 invalidateFFTModels();
Chris@9 657
Chris@9 658 emit layerParametersChanged();
Chris@0 659 }
Chris@0 660
Chris@0 661 WindowType
Chris@0 662 SpectrogramLayer::getWindowType() const
Chris@0 663 {
Chris@0 664 return m_windowType;
Chris@0 665 }
Chris@0 666
Chris@0 667 void
Chris@0 668 SpectrogramLayer::setGain(float gain)
Chris@0 669 {
Chris@101 670 // std::cerr << "SpectrogramLayer::setGain(" << gain << ") (my gain is now "
Chris@101 671 // << m_gain << ")" << std::endl;
Chris@55 672
Chris@40 673 if (m_gain == gain) return;
Chris@0 674
Chris@95 675 invalidatePixmapCaches();
Chris@0 676
Chris@0 677 m_gain = gain;
Chris@0 678
Chris@9 679 emit layerParametersChanged();
Chris@0 680 }
Chris@0 681
Chris@0 682 float
Chris@0 683 SpectrogramLayer::getGain() const
Chris@0 684 {
Chris@0 685 return m_gain;
Chris@0 686 }
Chris@0 687
Chris@0 688 void
Chris@37 689 SpectrogramLayer::setThreshold(float threshold)
Chris@37 690 {
Chris@40 691 if (m_threshold == threshold) return;
Chris@37 692
Chris@95 693 invalidatePixmapCaches();
Chris@37 694
Chris@37 695 m_threshold = threshold;
Chris@37 696
Chris@37 697 emit layerParametersChanged();
Chris@37 698 }
Chris@37 699
Chris@37 700 float
Chris@37 701 SpectrogramLayer::getThreshold() const
Chris@37 702 {
Chris@37 703 return m_threshold;
Chris@37 704 }
Chris@37 705
Chris@37 706 void
Chris@37 707 SpectrogramLayer::setMinFrequency(size_t mf)
Chris@37 708 {
Chris@37 709 if (m_minFrequency == mf) return;
Chris@37 710
Chris@187 711 std::cerr << "SpectrogramLayer::setMinFrequency: " << mf << std::endl;
Chris@187 712
Chris@95 713 invalidatePixmapCaches();
Chris@119 714 invalidateMagnitudes();
Chris@37 715
Chris@37 716 m_minFrequency = mf;
Chris@37 717
Chris@37 718 emit layerParametersChanged();
Chris@37 719 }
Chris@37 720
Chris@37 721 size_t
Chris@37 722 SpectrogramLayer::getMinFrequency() const
Chris@37 723 {
Chris@37 724 return m_minFrequency;
Chris@37 725 }
Chris@37 726
Chris@37 727 void
Chris@0 728 SpectrogramLayer::setMaxFrequency(size_t mf)
Chris@0 729 {
Chris@0 730 if (m_maxFrequency == mf) return;
Chris@0 731
Chris@187 732 std::cerr << "SpectrogramLayer::setMaxFrequency: " << mf << std::endl;
Chris@187 733
Chris@95 734 invalidatePixmapCaches();
Chris@119 735 invalidateMagnitudes();
Chris@0 736
Chris@0 737 m_maxFrequency = mf;
Chris@0 738
Chris@9 739 emit layerParametersChanged();
Chris@0 740 }
Chris@0 741
Chris@0 742 size_t
Chris@0 743 SpectrogramLayer::getMaxFrequency() const
Chris@0 744 {
Chris@0 745 return m_maxFrequency;
Chris@0 746 }
Chris@0 747
Chris@0 748 void
Chris@9 749 SpectrogramLayer::setColourRotation(int r)
Chris@9 750 {
Chris@95 751 invalidatePixmapCaches();
Chris@9 752
Chris@9 753 if (r < 0) r = 0;
Chris@9 754 if (r > 256) r = 256;
Chris@9 755 int distance = r - m_colourRotation;
Chris@9 756
Chris@9 757 if (distance != 0) {
Chris@90 758 rotateColourmap(-distance);
Chris@9 759 m_colourRotation = r;
Chris@9 760 }
Chris@9 761
Chris@9 762 emit layerParametersChanged();
Chris@9 763 }
Chris@9 764
Chris@9 765 void
Chris@0 766 SpectrogramLayer::setColourScale(ColourScale colourScale)
Chris@0 767 {
Chris@0 768 if (m_colourScale == colourScale) return;
Chris@0 769
Chris@95 770 invalidatePixmapCaches();
Chris@0 771
Chris@0 772 m_colourScale = colourScale;
Chris@0 773
Chris@9 774 emit layerParametersChanged();
Chris@0 775 }
Chris@0 776
Chris@0 777 SpectrogramLayer::ColourScale
Chris@0 778 SpectrogramLayer::getColourScale() const
Chris@0 779 {
Chris@0 780 return m_colourScale;
Chris@0 781 }
Chris@0 782
Chris@0 783 void
Chris@196 784 SpectrogramLayer::setColourScheme(int scheme)
Chris@0 785 {
Chris@0 786 if (m_colourScheme == scheme) return;
Chris@0 787
Chris@95 788 invalidatePixmapCaches();
Chris@0 789
Chris@0 790 m_colourScheme = scheme;
Chris@90 791 setColourmap();
Chris@9 792
Chris@0 793 emit layerParametersChanged();
Chris@0 794 }
Chris@0 795
Chris@196 796 int
Chris@0 797 SpectrogramLayer::getColourScheme() const
Chris@0 798 {
Chris@0 799 return m_colourScheme;
Chris@0 800 }
Chris@0 801
Chris@0 802 void
Chris@0 803 SpectrogramLayer::setFrequencyScale(FrequencyScale frequencyScale)
Chris@0 804 {
Chris@0 805 if (m_frequencyScale == frequencyScale) return;
Chris@0 806
Chris@95 807 invalidatePixmapCaches();
Chris@0 808 m_frequencyScale = frequencyScale;
Chris@9 809
Chris@9 810 emit layerParametersChanged();
Chris@0 811 }
Chris@0 812
Chris@0 813 SpectrogramLayer::FrequencyScale
Chris@0 814 SpectrogramLayer::getFrequencyScale() const
Chris@0 815 {
Chris@0 816 return m_frequencyScale;
Chris@0 817 }
Chris@0 818
Chris@0 819 void
Chris@37 820 SpectrogramLayer::setBinDisplay(BinDisplay binDisplay)
Chris@35 821 {
Chris@37 822 if (m_binDisplay == binDisplay) return;
Chris@35 823
Chris@95 824 invalidatePixmapCaches();
Chris@37 825 m_binDisplay = binDisplay;
Chris@35 826
Chris@35 827 emit layerParametersChanged();
Chris@35 828 }
Chris@35 829
Chris@37 830 SpectrogramLayer::BinDisplay
Chris@37 831 SpectrogramLayer::getBinDisplay() const
Chris@35 832 {
Chris@37 833 return m_binDisplay;
Chris@35 834 }
Chris@35 835
Chris@35 836 void
Chris@36 837 SpectrogramLayer::setNormalizeColumns(bool n)
Chris@36 838 {
Chris@36 839 if (m_normalizeColumns == n) return;
Chris@36 840
Chris@95 841 invalidatePixmapCaches();
Chris@119 842 invalidateMagnitudes();
Chris@36 843 m_normalizeColumns = n;
Chris@36 844
Chris@36 845 emit layerParametersChanged();
Chris@36 846 }
Chris@36 847
Chris@36 848 bool
Chris@36 849 SpectrogramLayer::getNormalizeColumns() const
Chris@36 850 {
Chris@36 851 return m_normalizeColumns;
Chris@36 852 }
Chris@36 853
Chris@36 854 void
Chris@120 855 SpectrogramLayer::setNormalizeVisibleArea(bool n)
Chris@120 856 {
Chris@120 857 if (m_normalizeVisibleArea == n) return;
Chris@120 858
Chris@120 859 invalidatePixmapCaches();
Chris@120 860 invalidateMagnitudes();
Chris@120 861 m_normalizeVisibleArea = n;
Chris@120 862
Chris@120 863 emit layerParametersChanged();
Chris@120 864 }
Chris@120 865
Chris@120 866 bool
Chris@120 867 SpectrogramLayer::getNormalizeVisibleArea() const
Chris@120 868 {
Chris@120 869 return m_normalizeVisibleArea;
Chris@120 870 }
Chris@120 871
Chris@120 872 void
Chris@47 873 SpectrogramLayer::setLayerDormant(const View *v, bool dormant)
Chris@29 874 {
Chris@33 875 if (dormant) {
Chris@33 876
Chris@131 877 if (isLayerDormant(v)) {
Chris@131 878 return;
Chris@131 879 }
Chris@131 880
Chris@131 881 Layer::setLayerDormant(v, true);
Chris@33 882
Chris@95 883 invalidatePixmapCaches();
Chris@95 884 m_pixmapCaches.erase(v);
Chris@114 885
Chris@130 886 if (m_fftModels.find(v) != m_fftModels.end()) {
Chris@193 887
Chris@193 888 if (m_sliceableModel == m_fftModels[v].first) {
Chris@193 889 bool replaced = false;
Chris@193 890 for (ViewFFTMap::iterator i = m_fftModels.begin();
Chris@193 891 i != m_fftModels.end(); ++i) {
Chris@193 892 if (i->second.first != m_sliceableModel) {
Chris@193 893 emit sliceableModelReplaced(m_sliceableModel, i->second.first);
Chris@193 894 replaced = true;
Chris@193 895 break;
Chris@193 896 }
Chris@193 897 }
Chris@193 898 if (!replaced) emit sliceableModelReplaced(m_sliceableModel, 0);
Chris@193 899 }
Chris@193 900
Chris@130 901 delete m_fftModels[v].first;
Chris@130 902 m_fftModels.erase(v);
Chris@114 903 }
Chris@33 904
Chris@33 905 } else {
Chris@33 906
Chris@131 907 Layer::setLayerDormant(v, false);
Chris@33 908 }
Chris@29 909 }
Chris@29 910
Chris@29 911 void
Chris@0 912 SpectrogramLayer::cacheInvalid()
Chris@0 913 {
Chris@95 914 invalidatePixmapCaches();
Chris@119 915 invalidateMagnitudes();
Chris@0 916 }
Chris@0 917
Chris@0 918 void
Chris@0 919 SpectrogramLayer::cacheInvalid(size_t, size_t)
Chris@0 920 {
Chris@0 921 // for now (or forever?)
Chris@0 922 cacheInvalid();
Chris@0 923 }
Chris@0 924
Chris@0 925 void
Chris@0 926 SpectrogramLayer::fillTimerTimedOut()
Chris@0 927 {
Chris@115 928 if (!m_model) return;
Chris@115 929
Chris@115 930 bool allDone = true;
Chris@115 931
Chris@184 932 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@184 933 std::cerr << "SpectrogramLayer::fillTimerTimedOut: have " << m_fftModels.size() << " FFT models associated with views" << std::endl;
Chris@184 934 #endif
Chris@184 935
Chris@130 936 for (ViewFFTMap::iterator i = m_fftModels.begin();
Chris@130 937 i != m_fftModels.end(); ++i) {
Chris@115 938
Chris@115 939 const View *v = i->first;
Chris@130 940 const FFTModel *model = i->second.first;
Chris@115 941 size_t lastFill = i->second.second;
Chris@115 942
Chris@130 943 if (model) {
Chris@130 944
Chris@130 945 size_t fill = model->getFillExtent();
Chris@115 946
Chris@0 947 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@130 948 std::cerr << "SpectrogramLayer::fillTimerTimedOut: extent for " << model << ": " << fill << ", last " << lastFill << ", total " << m_model->getEndFrame() << std::endl;
Chris@0 949 #endif
Chris@115 950
Chris@115 951 if (fill >= lastFill) {
Chris@115 952 if (fill >= m_model->getEndFrame() && lastFill > 0) {
Chris@0 953 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@115 954 std::cerr << "complete!" << std::endl;
Chris@0 955 #endif
Chris@115 956 invalidatePixmapCaches();
Chris@184 957 i->second.second = -1;
Chris@115 958 emit modelChanged();
Chris@115 959
Chris@115 960 } else if (fill > lastFill) {
Chris@0 961 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@115 962 std::cerr << "SpectrogramLayer: emitting modelChanged("
Chris@115 963 << lastFill << "," << fill << ")" << std::endl;
Chris@0 964 #endif
Chris@115 965 invalidatePixmapCaches(lastFill, fill);
Chris@184 966 i->second.second = fill;
Chris@115 967 emit modelChanged(lastFill, fill);
Chris@115 968 }
Chris@115 969 } else {
Chris@0 970 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@115 971 std::cerr << "SpectrogramLayer: going backwards, emitting modelChanged("
Chris@115 972 << m_model->getStartFrame() << "," << m_model->getEndFrame() << ")" << std::endl;
Chris@0 973 #endif
Chris@115 974 invalidatePixmapCaches();
Chris@184 975 i->second.second = fill;
Chris@115 976 emit modelChanged(m_model->getStartFrame(), m_model->getEndFrame());
Chris@115 977 }
Chris@115 978
Chris@115 979 if (i->second.second >= 0) {
Chris@115 980 allDone = false;
Chris@115 981 }
Chris@115 982 }
Chris@0 983 }
Chris@115 984
Chris@115 985 if (allDone) {
Chris@115 986 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@115 987 std::cerr << "SpectrogramLayer: all complete!" << std::endl;
Chris@115 988 #endif
Chris@115 989 delete m_updateTimer;
Chris@115 990 m_updateTimer = 0;
Chris@115 991 }
Chris@0 992 }
Chris@0 993
Chris@0 994 void
Chris@90 995 SpectrogramLayer::setColourmap()
Chris@0 996 {
Chris@10 997 int formerRotation = m_colourRotation;
Chris@10 998
Chris@196 999 if (m_colourScheme == (int)ColourMapper::BlackOnWhite) {
Chris@86 1000 m_colourMap.setColour(NO_VALUE, Qt::white);
Chris@38 1001 } else {
Chris@86 1002 m_colourMap.setColour(NO_VALUE, Qt::black);
Chris@38 1003 }
Chris@0 1004
Chris@196 1005 ColourMapper mapper(m_colourScheme, 1.f, 256.f);
Chris@196 1006
Chris@0 1007 for (int pixel = 1; pixel < 256; ++pixel) {
Chris@0 1008
Chris@196 1009 m_colourMap.setColour(pixel, mapper.map(pixel));
Chris@0 1010 }
Chris@9 1011
Chris@196 1012 m_crosshairColour = mapper.getContrastingColour();
Chris@196 1013
Chris@9 1014 m_colourRotation = 0;
Chris@90 1015 rotateColourmap(m_colourRotation - formerRotation);
Chris@10 1016 m_colourRotation = formerRotation;
Chris@9 1017 }
Chris@9 1018
Chris@9 1019 void
Chris@90 1020 SpectrogramLayer::rotateColourmap(int distance)
Chris@9 1021 {
Chris@31 1022 QColor newPixels[256];
Chris@9 1023
Chris@86 1024 newPixels[NO_VALUE] = m_colourMap.getColour(NO_VALUE);
Chris@9 1025
Chris@9 1026 for (int pixel = 1; pixel < 256; ++pixel) {
Chris@9 1027 int target = pixel + distance;
Chris@9 1028 while (target < 1) target += 255;
Chris@9 1029 while (target > 255) target -= 255;
Chris@86 1030 newPixels[target] = m_colourMap.getColour(pixel);
Chris@9 1031 }
Chris@9 1032
Chris@9 1033 for (int pixel = 0; pixel < 256; ++pixel) {
Chris@86 1034 m_colourMap.setColour(pixel, newPixels[pixel]);
Chris@9 1035 }
Chris@0 1036 }
Chris@0 1037
Chris@38 1038 float
Chris@38 1039 SpectrogramLayer::calculateFrequency(size_t bin,
Chris@38 1040 size_t windowSize,
Chris@38 1041 size_t windowIncrement,
Chris@38 1042 size_t sampleRate,
Chris@38 1043 float oldPhase,
Chris@38 1044 float newPhase,
Chris@38 1045 bool &steadyState)
Chris@38 1046 {
Chris@38 1047 // At frequency f, phase shift of 2pi (one cycle) happens in 1/f sec.
Chris@38 1048 // At hopsize h and sample rate sr, one hop happens in h/sr sec.
Chris@38 1049 // At window size w, for bin b, f is b*sr/w.
Chris@38 1050 // thus 2pi phase shift happens in w/(b*sr) sec.
Chris@38 1051 // We need to know what phase shift we expect from h/sr sec.
Chris@38 1052 // -> 2pi * ((h/sr) / (w/(b*sr)))
Chris@38 1053 // = 2pi * ((h * b * sr) / (w * sr))
Chris@38 1054 // = 2pi * (h * b) / w.
Chris@38 1055
Chris@38 1056 float frequency = (float(bin) * sampleRate) / windowSize;
Chris@38 1057
Chris@38 1058 float expectedPhase =
Chris@38 1059 oldPhase + (2.0 * M_PI * bin * windowIncrement) / windowSize;
Chris@38 1060
Chris@104 1061 float phaseError = princargf(newPhase - expectedPhase);
Chris@38 1062
Chris@142 1063 if (fabsf(phaseError) < (1.1f * (windowIncrement * M_PI) / windowSize)) {
Chris@38 1064
Chris@38 1065 // The new frequency estimate based on the phase error
Chris@38 1066 // resulting from assuming the "native" frequency of this bin
Chris@38 1067
Chris@38 1068 float newFrequency =
Chris@38 1069 (sampleRate * (expectedPhase + phaseError - oldPhase)) /
Chris@38 1070 (2 * M_PI * windowIncrement);
Chris@38 1071
Chris@38 1072 steadyState = true;
Chris@38 1073 return newFrequency;
Chris@38 1074 }
Chris@38 1075
Chris@38 1076 steadyState = false;
Chris@38 1077 return frequency;
Chris@38 1078 }
Chris@38 1079
Chris@38 1080 unsigned char
Chris@119 1081 SpectrogramLayer::getDisplayValue(View *v, float input) const
Chris@38 1082 {
Chris@38 1083 int value;
Chris@37 1084
Chris@120 1085 float min = 0.f;
Chris@120 1086 float max = 1.f;
Chris@120 1087
Chris@120 1088 if (m_normalizeVisibleArea) {
Chris@120 1089 min = m_viewMags[v].getMin();
Chris@120 1090 max = m_viewMags[v].getMax();
Chris@120 1091 } else if (!m_normalizeColumns) {
Chris@120 1092 if (m_colourScale == LinearColourScale ||
Chris@120 1093 m_colourScale == MeterColourScale) {
Chris@120 1094 max = 0.1f;
Chris@120 1095 }
Chris@120 1096 }
Chris@120 1097
Chris@119 1098 float thresh = -80.f;
Chris@119 1099
Chris@119 1100 if (max == 0.f) max = 1.f;
Chris@119 1101 if (max == min) min = max - 0.0001f;
Chris@119 1102
Chris@40 1103 switch (m_colourScale) {
Chris@40 1104
Chris@40 1105 default:
Chris@40 1106 case LinearColourScale:
Chris@119 1107 // value = int
Chris@119 1108 // (input * (m_normalizeColumns ? 1.0 : 50.0) * 255.0) + 1;
Chris@119 1109 value = int(((input - min) / (max - min)) * 255.f) + 1;
Chris@40 1110 break;
Chris@40 1111
Chris@40 1112 case MeterColourScale:
Chris@119 1113 // value = AudioLevel::multiplier_to_preview
Chris@119 1114 // (input * (m_normalizeColumns ? 1.0 : 50.0), 255) + 1;
Chris@119 1115 value = AudioLevel::multiplier_to_preview((input - min) / (max - min), 255) + 1;
Chris@40 1116 break;
Chris@40 1117
Chris@40 1118 case dBColourScale:
Chris@119 1119 //!!! experiment with normalizing the visible area this way.
Chris@119 1120 //In any case, we need to have some indication of what the dB
Chris@119 1121 //scale is relative to.
Chris@133 1122 input = input / max;
Chris@133 1123 if (input > 0.f) {
Chris@133 1124 input = 10.f * log10f(input);
Chris@133 1125 } else {
Chris@133 1126 input = thresh;
Chris@133 1127 }
Chris@119 1128 if (min > 0.f) {
Chris@119 1129 thresh = 10.f * log10f(min);
Chris@119 1130 if (thresh < -80.f) thresh = -80.f;
Chris@119 1131 }
Chris@119 1132 input = (input - thresh) / (-thresh);
Chris@119 1133 if (input < 0.f) input = 0.f;
Chris@119 1134 if (input > 1.f) input = 1.f;
Chris@119 1135 value = int(input * 255.f) + 1;
Chris@119 1136 break;
Chris@119 1137
Chris@119 1138 case OtherColourScale:
Chris@119 1139 //!!! the "Other" scale is just where our current experiments go
Chris@119 1140 //!!! power rather than v
Chris@133 1141 input = (input * input) / (max * max);
Chris@133 1142 if (input > 0.f) {
Chris@133 1143 input = 10.f * log10f(input);
Chris@133 1144 } else {
Chris@133 1145 input = thresh;
Chris@133 1146 }
Chris@119 1147 if (min > 0.f) {
Chris@119 1148 thresh = 10.f * log10f(min * min);
Chris@119 1149 if (thresh < -80.f) thresh = -80.f;
Chris@119 1150 }
Chris@119 1151 input = (input - thresh) / (-thresh);
Chris@119 1152 if (input < 0.f) input = 0.f;
Chris@119 1153 if (input > 1.f) input = 1.f;
Chris@119 1154 value = int(input * 255.f) + 1;
Chris@119 1155 break;
Chris@119 1156
Chris@119 1157 /*!!!
Chris@119 1158 input = 10.f * log10f(input * input);
Chris@119 1159 input = 1.f / (1.f + expf(- (input + 20.f) / 10.f));
Chris@119 1160
Chris@119 1161 if (input < 0.f) input = 0.f;
Chris@119 1162 if (input > 1.f) input = 1.f;
Chris@119 1163 value = int(input * 255.f) + 1;
Chris@119 1164 */
Chris@40 1165 break;
Chris@40 1166
Chris@40 1167 case PhaseColourScale:
Chris@40 1168 value = int((input * 127.0 / M_PI) + 128);
Chris@40 1169 break;
Chris@0 1170 }
Chris@38 1171
Chris@38 1172 if (value > UCHAR_MAX) value = UCHAR_MAX;
Chris@38 1173 if (value < 0) value = 0;
Chris@38 1174 return value;
Chris@0 1175 }
Chris@0 1176
Chris@40 1177 float
Chris@40 1178 SpectrogramLayer::getInputForDisplayValue(unsigned char uc) const
Chris@40 1179 {
Chris@153 1180 //!!! unused
Chris@153 1181
Chris@40 1182 int value = uc;
Chris@40 1183 float input;
Chris@40 1184
Chris@120 1185 //!!! incorrect for normalizing visible area (and also out of date)
Chris@120 1186
Chris@40 1187 switch (m_colourScale) {
Chris@40 1188
Chris@40 1189 default:
Chris@40 1190 case LinearColourScale:
Chris@40 1191 input = float(value - 1) / 255.0 / (m_normalizeColumns ? 1 : 50);
Chris@40 1192 break;
Chris@40 1193
Chris@40 1194 case MeterColourScale:
Chris@40 1195 input = AudioLevel::preview_to_multiplier(value - 1, 255)
Chris@40 1196 / (m_normalizeColumns ? 1.0 : 50.0);
Chris@40 1197 break;
Chris@40 1198
Chris@40 1199 case dBColourScale:
Chris@40 1200 input = float(value - 1) / 255.0;
Chris@40 1201 input = (input * 80.0) - 80.0;
Chris@40 1202 input = powf(10.0, input) / 20.0;
Chris@40 1203 value = int(input);
Chris@40 1204 break;
Chris@40 1205
Chris@119 1206 case OtherColourScale:
Chris@119 1207 input = float(value - 1) / 255.0;
Chris@119 1208 input = (input * 80.0) - 80.0;
Chris@119 1209 input = powf(10.0, input) / 20.0;
Chris@119 1210 value = int(input);
Chris@119 1211 break;
Chris@119 1212
Chris@40 1213 case PhaseColourScale:
Chris@40 1214 input = float(value - 128) * M_PI / 127.0;
Chris@40 1215 break;
Chris@40 1216 }
Chris@40 1217
Chris@40 1218 return input;
Chris@40 1219 }
Chris@40 1220
Chris@40 1221 float
Chris@40 1222 SpectrogramLayer::getEffectiveMinFrequency() const
Chris@40 1223 {
Chris@40 1224 int sr = m_model->getSampleRate();
Chris@107 1225 float minf = float(sr) / m_fftSize;
Chris@40 1226
Chris@40 1227 if (m_minFrequency > 0.0) {
Chris@107 1228 size_t minbin = size_t((double(m_minFrequency) * m_fftSize) / sr + 0.01);
Chris@40 1229 if (minbin < 1) minbin = 1;
Chris@107 1230 minf = minbin * sr / m_fftSize;
Chris@40 1231 }
Chris@40 1232
Chris@40 1233 return minf;
Chris@40 1234 }
Chris@40 1235
Chris@40 1236 float
Chris@40 1237 SpectrogramLayer::getEffectiveMaxFrequency() const
Chris@40 1238 {
Chris@40 1239 int sr = m_model->getSampleRate();
Chris@40 1240 float maxf = float(sr) / 2;
Chris@40 1241
Chris@40 1242 if (m_maxFrequency > 0.0) {
Chris@107 1243 size_t maxbin = size_t((double(m_maxFrequency) * m_fftSize) / sr + 0.1);
Chris@107 1244 if (maxbin > m_fftSize / 2) maxbin = m_fftSize / 2;
Chris@107 1245 maxf = maxbin * sr / m_fftSize;
Chris@40 1246 }
Chris@40 1247
Chris@40 1248 return maxf;
Chris@40 1249 }
Chris@40 1250
Chris@0 1251 bool
Chris@44 1252 SpectrogramLayer::getYBinRange(View *v, int y, float &q0, float &q1) const
Chris@0 1253 {
Chris@44 1254 int h = v->height();
Chris@0 1255 if (y < 0 || y >= h) return false;
Chris@0 1256
Chris@38 1257 int sr = m_model->getSampleRate();
Chris@40 1258 float minf = getEffectiveMinFrequency();
Chris@40 1259 float maxf = getEffectiveMaxFrequency();
Chris@0 1260
Chris@38 1261 bool logarithmic = (m_frequencyScale == LogFrequencyScale);
Chris@38 1262
Chris@130 1263 //!!! wrong for smoothing -- wrong fft size for fft model
Chris@114 1264
Chris@44 1265 q0 = v->getFrequencyForY(y, minf, maxf, logarithmic);
Chris@44 1266 q1 = v->getFrequencyForY(y - 1, minf, maxf, logarithmic);
Chris@38 1267
Chris@38 1268 // Now map these on to actual bins
Chris@38 1269
Chris@107 1270 int b0 = int((q0 * m_fftSize) / sr);
Chris@107 1271 int b1 = int((q1 * m_fftSize) / sr);
Chris@0 1272
Chris@40 1273 //!!! this is supposed to return fractions-of-bins, as it were, hence the floats
Chris@38 1274 q0 = b0;
Chris@38 1275 q1 = b1;
Chris@38 1276
Chris@107 1277 // q0 = (b0 * sr) / m_fftSize;
Chris@107 1278 // q1 = (b1 * sr) / m_fftSize;
Chris@0 1279
Chris@0 1280 return true;
Chris@0 1281 }
Chris@38 1282
Chris@0 1283 bool
Chris@44 1284 SpectrogramLayer::getXBinRange(View *v, int x, float &s0, float &s1) const
Chris@0 1285 {
Chris@21 1286 size_t modelStart = m_model->getStartFrame();
Chris@21 1287 size_t modelEnd = m_model->getEndFrame();
Chris@0 1288
Chris@0 1289 // Each pixel column covers an exact range of sample frames:
Chris@44 1290 int f0 = v->getFrameForX(x) - modelStart;
Chris@44 1291 int f1 = v->getFrameForX(x + 1) - modelStart - 1;
Chris@20 1292
Chris@41 1293 if (f1 < int(modelStart) || f0 > int(modelEnd)) {
Chris@41 1294 return false;
Chris@41 1295 }
Chris@20 1296
Chris@0 1297 // And that range may be drawn from a possibly non-integral
Chris@0 1298 // range of spectrogram windows:
Chris@0 1299
Chris@0 1300 size_t windowIncrement = getWindowIncrement();
Chris@0 1301 s0 = float(f0) / windowIncrement;
Chris@0 1302 s1 = float(f1) / windowIncrement;
Chris@0 1303
Chris@0 1304 return true;
Chris@0 1305 }
Chris@0 1306
Chris@0 1307 bool
Chris@44 1308 SpectrogramLayer::getXBinSourceRange(View *v, int x, RealTime &min, RealTime &max) const
Chris@0 1309 {
Chris@0 1310 float s0 = 0, s1 = 0;
Chris@44 1311 if (!getXBinRange(v, x, s0, s1)) return false;
Chris@0 1312
Chris@0 1313 int s0i = int(s0 + 0.001);
Chris@0 1314 int s1i = int(s1);
Chris@0 1315
Chris@0 1316 int windowIncrement = getWindowIncrement();
Chris@0 1317 int w0 = s0i * windowIncrement - (m_windowSize - windowIncrement)/2;
Chris@0 1318 int w1 = s1i * windowIncrement + windowIncrement +
Chris@0 1319 (m_windowSize - windowIncrement)/2 - 1;
Chris@0 1320
Chris@0 1321 min = RealTime::frame2RealTime(w0, m_model->getSampleRate());
Chris@0 1322 max = RealTime::frame2RealTime(w1, m_model->getSampleRate());
Chris@0 1323 return true;
Chris@0 1324 }
Chris@0 1325
Chris@0 1326 bool
Chris@44 1327 SpectrogramLayer::getYBinSourceRange(View *v, int y, float &freqMin, float &freqMax)
Chris@0 1328 const
Chris@0 1329 {
Chris@0 1330 float q0 = 0, q1 = 0;
Chris@44 1331 if (!getYBinRange(v, y, q0, q1)) return false;
Chris@0 1332
Chris@0 1333 int q0i = int(q0 + 0.001);
Chris@0 1334 int q1i = int(q1);
Chris@0 1335
Chris@0 1336 int sr = m_model->getSampleRate();
Chris@0 1337
Chris@0 1338 for (int q = q0i; q <= q1i; ++q) {
Chris@121 1339 if (q == q0i) freqMin = (sr * q) / m_fftSize;
Chris@121 1340 if (q == q1i) freqMax = (sr * (q+1)) / m_fftSize;
Chris@0 1341 }
Chris@0 1342 return true;
Chris@0 1343 }
Chris@35 1344
Chris@35 1345 bool
Chris@44 1346 SpectrogramLayer::getAdjustedYBinSourceRange(View *v, int x, int y,
Chris@35 1347 float &freqMin, float &freqMax,
Chris@35 1348 float &adjFreqMin, float &adjFreqMax)
Chris@35 1349 const
Chris@35 1350 {
Chris@130 1351 FFTModel *fft = getFFTModel(v);
Chris@114 1352 if (!fft) return false;
Chris@110 1353
Chris@35 1354 float s0 = 0, s1 = 0;
Chris@44 1355 if (!getXBinRange(v, x, s0, s1)) return false;
Chris@35 1356
Chris@35 1357 float q0 = 0, q1 = 0;
Chris@44 1358 if (!getYBinRange(v, y, q0, q1)) return false;
Chris@35 1359
Chris@35 1360 int s0i = int(s0 + 0.001);
Chris@35 1361 int s1i = int(s1);
Chris@35 1362
Chris@35 1363 int q0i = int(q0 + 0.001);
Chris@35 1364 int q1i = int(q1);
Chris@35 1365
Chris@35 1366 int sr = m_model->getSampleRate();
Chris@35 1367
Chris@38 1368 size_t windowSize = m_windowSize;
Chris@38 1369 size_t windowIncrement = getWindowIncrement();
Chris@38 1370
Chris@35 1371 bool haveAdj = false;
Chris@35 1372
Chris@37 1373 bool peaksOnly = (m_binDisplay == PeakBins ||
Chris@37 1374 m_binDisplay == PeakFrequencies);
Chris@37 1375
Chris@35 1376 for (int q = q0i; q <= q1i; ++q) {
Chris@35 1377
Chris@35 1378 for (int s = s0i; s <= s1i; ++s) {
Chris@35 1379
Chris@160 1380 if (!fft->isColumnAvailable(s)) continue;
Chris@117 1381
Chris@35 1382 float binfreq = (sr * q) / m_windowSize;
Chris@35 1383 if (q == q0i) freqMin = binfreq;
Chris@35 1384 if (q == q1i) freqMax = binfreq;
Chris@37 1385
Chris@114 1386 if (peaksOnly && !fft->isLocalPeak(s, q)) continue;
Chris@38 1387
Chris@114 1388 if (!fft->isOverThreshold(s, q, m_threshold)) continue;
Chris@38 1389
Chris@38 1390 float freq = binfreq;
Chris@38 1391 bool steady = false;
Chris@40 1392
Chris@114 1393 if (s < int(fft->getWidth()) - 1) {
Chris@38 1394
Chris@38 1395 freq = calculateFrequency(q,
Chris@38 1396 windowSize,
Chris@38 1397 windowIncrement,
Chris@38 1398 sr,
Chris@114 1399 fft->getPhaseAt(s, q),
Chris@114 1400 fft->getPhaseAt(s+1, q),
Chris@38 1401 steady);
Chris@35 1402
Chris@38 1403 if (!haveAdj || freq < adjFreqMin) adjFreqMin = freq;
Chris@38 1404 if (!haveAdj || freq > adjFreqMax) adjFreqMax = freq;
Chris@35 1405
Chris@35 1406 haveAdj = true;
Chris@35 1407 }
Chris@35 1408 }
Chris@35 1409 }
Chris@35 1410
Chris@35 1411 if (!haveAdj) {
Chris@40 1412 adjFreqMin = adjFreqMax = 0.0;
Chris@35 1413 }
Chris@35 1414
Chris@35 1415 return haveAdj;
Chris@35 1416 }
Chris@0 1417
Chris@0 1418 bool
Chris@44 1419 SpectrogramLayer::getXYBinSourceRange(View *v, int x, int y,
Chris@38 1420 float &min, float &max,
Chris@38 1421 float &phaseMin, float &phaseMax) const
Chris@0 1422 {
Chris@0 1423 float q0 = 0, q1 = 0;
Chris@44 1424 if (!getYBinRange(v, y, q0, q1)) return false;
Chris@0 1425
Chris@0 1426 float s0 = 0, s1 = 0;
Chris@44 1427 if (!getXBinRange(v, x, s0, s1)) return false;
Chris@0 1428
Chris@0 1429 int q0i = int(q0 + 0.001);
Chris@0 1430 int q1i = int(q1);
Chris@0 1431
Chris@0 1432 int s0i = int(s0 + 0.001);
Chris@0 1433 int s1i = int(s1);
Chris@0 1434
Chris@37 1435 bool rv = false;
Chris@37 1436
Chris@122 1437 size_t zp = getZeroPadLevel(v);
Chris@122 1438 q0i *= zp + 1;
Chris@122 1439 q1i *= zp + 1;
Chris@122 1440
Chris@130 1441 FFTModel *fft = getFFTModel(v);
Chris@0 1442
Chris@114 1443 if (fft) {
Chris@114 1444
Chris@114 1445 int cw = fft->getWidth();
Chris@114 1446 int ch = fft->getHeight();
Chris@0 1447
Chris@110 1448 min = 0.0;
Chris@110 1449 max = 0.0;
Chris@110 1450 phaseMin = 0.0;
Chris@110 1451 phaseMax = 0.0;
Chris@110 1452 bool have = false;
Chris@0 1453
Chris@110 1454 for (int q = q0i; q <= q1i; ++q) {
Chris@110 1455 for (int s = s0i; s <= s1i; ++s) {
Chris@110 1456 if (s >= 0 && q >= 0 && s < cw && q < ch) {
Chris@117 1457
Chris@160 1458 if (!fft->isColumnAvailable(s)) continue;
Chris@110 1459
Chris@110 1460 float value;
Chris@38 1461
Chris@114 1462 value = fft->getPhaseAt(s, q);
Chris@110 1463 if (!have || value < phaseMin) { phaseMin = value; }
Chris@110 1464 if (!have || value > phaseMax) { phaseMax = value; }
Chris@91 1465
Chris@114 1466 value = fft->getMagnitudeAt(s, q);
Chris@110 1467 if (!have || value < min) { min = value; }
Chris@110 1468 if (!have || value > max) { max = value; }
Chris@110 1469
Chris@110 1470 have = true;
Chris@110 1471 }
Chris@110 1472 }
Chris@110 1473 }
Chris@110 1474
Chris@110 1475 if (have) {
Chris@110 1476 rv = true;
Chris@110 1477 }
Chris@0 1478 }
Chris@0 1479
Chris@37 1480 return rv;
Chris@0 1481 }
Chris@0 1482
Chris@114 1483 size_t
Chris@114 1484 SpectrogramLayer::getZeroPadLevel(const View *v) const
Chris@114 1485 {
Chris@114 1486 //!!! tidy all this stuff
Chris@114 1487
Chris@114 1488 if (m_binDisplay != AllBins) return 0;
Chris@118 1489 if (!Preferences::getInstance()->getSmoothSpectrogram()) return 0;
Chris@114 1490 if (m_frequencyScale == LogFrequencyScale) return 3;
Chris@114 1491
Chris@114 1492 int sr = m_model->getSampleRate();
Chris@114 1493
Chris@184 1494 size_t maxbin = m_fftSize / 2;
Chris@114 1495 if (m_maxFrequency > 0) {
Chris@184 1496 maxbin = int((double(m_maxFrequency) * m_fftSize) / sr + 0.1);
Chris@184 1497 if (maxbin > m_fftSize / 2) maxbin = m_fftSize / 2;
Chris@114 1498 }
Chris@114 1499
Chris@114 1500 size_t minbin = 1;
Chris@114 1501 if (m_minFrequency > 0) {
Chris@114 1502 minbin = int((double(m_minFrequency) * m_fftSize) / sr + 0.1);
Chris@114 1503 if (minbin < 1) minbin = 1;
Chris@184 1504 if (minbin >= maxbin) minbin = maxbin - 1;
Chris@114 1505 }
Chris@114 1506
Chris@118 1507 float perPixel =
Chris@118 1508 float(v->height()) /
Chris@184 1509 float((maxbin - minbin) / (m_zeroPadLevel + 1));
Chris@118 1510
Chris@118 1511 if (perPixel > 2.8) {
Chris@118 1512 return 3; // 4x oversampling
Chris@118 1513 } else if (perPixel > 1.5) {
Chris@118 1514 return 1; // 2x
Chris@114 1515 } else {
Chris@118 1516 return 0; // 1x
Chris@114 1517 }
Chris@114 1518 }
Chris@114 1519
Chris@114 1520 size_t
Chris@114 1521 SpectrogramLayer::getFFTSize(const View *v) const
Chris@114 1522 {
Chris@114 1523 return m_fftSize * (getZeroPadLevel(v) + 1);
Chris@114 1524 }
Chris@114 1525
Chris@130 1526 FFTModel *
Chris@130 1527 SpectrogramLayer::getFFTModel(const View *v) const
Chris@114 1528 {
Chris@114 1529 if (!m_model) return 0;
Chris@114 1530
Chris@114 1531 size_t fftSize = getFFTSize(v);
Chris@114 1532
Chris@130 1533 if (m_fftModels.find(v) != m_fftModels.end()) {
Chris@184 1534 if (m_fftModels[v].first == 0) {
Chris@184 1535 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@184 1536 std::cerr << "SpectrogramLayer::getFFTModel(" << v << "): Found null model" << std::endl;
Chris@184 1537 #endif
Chris@184 1538 return 0;
Chris@184 1539 }
Chris@184 1540 if (m_fftModels[v].first->getHeight() != fftSize / 2 + 1) {
Chris@184 1541 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@184 1542 std::cerr << "SpectrogramLayer::getFFTModel(" << v << "): Found a model with the wrong height (" << m_fftModels[v].first->getHeight() << ", wanted " << (fftSize / 2 + 1) << ")" << std::endl;
Chris@184 1543 #endif
Chris@130 1544 delete m_fftModels[v].first;
Chris@130 1545 m_fftModels.erase(v);
Chris@184 1546 } else {
Chris@184 1547 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@187 1548 std::cerr << "SpectrogramLayer::getFFTModel(" << v << "): Found a good model of height " << m_fftModels[v].first->getHeight() << std::endl;
Chris@184 1549 #endif
Chris@184 1550 return m_fftModels[v].first;
Chris@114 1551 }
Chris@114 1552 }
Chris@114 1553
Chris@130 1554 if (m_fftModels.find(v) == m_fftModels.end()) {
Chris@169 1555
Chris@169 1556 FFTModel *model = new FFTModel(m_model,
Chris@169 1557 m_channel,
Chris@169 1558 m_windowType,
Chris@169 1559 m_windowSize,
Chris@169 1560 getWindowIncrement(),
Chris@169 1561 fftSize,
Chris@169 1562 true,
Chris@169 1563 m_candidateFillStartFrame);
Chris@169 1564
Chris@178 1565 if (!model->isOK()) {
Chris@178 1566 QMessageBox::critical
Chris@178 1567 (0, tr("FFT cache failed"),
Chris@178 1568 tr("Failed to create the FFT model for this spectrogram.\n"
Chris@178 1569 "There may be insufficient memory or disc space to continue."));
Chris@178 1570 delete model;
Chris@178 1571 m_fftModels[v] = FFTFillPair(0, 0);
Chris@178 1572 return 0;
Chris@178 1573 }
Chris@178 1574
Chris@193 1575 if (!m_sliceableModel) {
Chris@193 1576 std::cerr << "SpectrogramLayer: emitting sliceableModelReplaced(0, " << model << ")" << std::endl;
Chris@193 1577 ((SpectrogramLayer *)this)->sliceableModelReplaced(0, model);
Chris@193 1578 m_sliceableModel = model;
Chris@193 1579 }
Chris@193 1580
Chris@169 1581 m_fftModels[v] = FFTFillPair(model, 0);
Chris@169 1582
Chris@169 1583 model->resume();
Chris@114 1584
Chris@114 1585 delete m_updateTimer;
Chris@114 1586 m_updateTimer = new QTimer((SpectrogramLayer *)this);
Chris@114 1587 connect(m_updateTimer, SIGNAL(timeout()),
Chris@114 1588 this, SLOT(fillTimerTimedOut()));
Chris@114 1589 m_updateTimer->start(200);
Chris@114 1590 }
Chris@114 1591
Chris@130 1592 return m_fftModels[v].first;
Chris@114 1593 }
Chris@114 1594
Chris@193 1595 const Model *
Chris@193 1596 SpectrogramLayer::getSliceableModel() const
Chris@193 1597 {
Chris@193 1598 if (m_sliceableModel) return m_sliceableModel;
Chris@193 1599 if (m_fftModels.empty()) return 0;
Chris@193 1600 m_sliceableModel = m_fftModels.begin()->second.first;
Chris@193 1601 return m_sliceableModel;
Chris@193 1602 }
Chris@193 1603
Chris@114 1604 void
Chris@130 1605 SpectrogramLayer::invalidateFFTModels()
Chris@114 1606 {
Chris@130 1607 for (ViewFFTMap::iterator i = m_fftModels.begin();
Chris@130 1608 i != m_fftModels.end(); ++i) {
Chris@115 1609 delete i->second.first;
Chris@114 1610 }
Chris@114 1611
Chris@130 1612 m_fftModels.clear();
Chris@193 1613
Chris@193 1614 if (m_sliceableModel) {
Chris@193 1615 std::cerr << "SpectrogramLayer: emitting sliceableModelReplaced(" << m_sliceableModel << ", 0)" << std::endl;
Chris@193 1616 emit sliceableModelReplaced(m_sliceableModel, 0);
Chris@193 1617 m_sliceableModel = 0;
Chris@193 1618 }
Chris@114 1619 }
Chris@114 1620
Chris@0 1621 void
Chris@119 1622 SpectrogramLayer::invalidateMagnitudes()
Chris@119 1623 {
Chris@119 1624 m_viewMags.clear();
Chris@119 1625 for (std::vector<MagnitudeRange>::iterator i = m_columnMags.begin();
Chris@119 1626 i != m_columnMags.end(); ++i) {
Chris@119 1627 *i = MagnitudeRange();
Chris@119 1628 }
Chris@119 1629 }
Chris@119 1630
Chris@119 1631 bool
Chris@119 1632 SpectrogramLayer::updateViewMagnitudes(View *v) const
Chris@119 1633 {
Chris@119 1634 MagnitudeRange mag;
Chris@119 1635
Chris@119 1636 int x0 = 0, x1 = v->width();
Chris@119 1637 float s00 = 0, s01 = 0, s10 = 0, s11 = 0;
Chris@119 1638
Chris@119 1639 getXBinRange(v, x0, s00, s01);
Chris@119 1640 getXBinRange(v, x1, s10, s11);
Chris@119 1641
Chris@119 1642 int s0 = int(std::min(s00, s10) + 0.0001);
Chris@119 1643 int s1 = int(std::max(s01, s11));
Chris@119 1644
Chris@119 1645 if (m_columnMags.size() <= s1) {
Chris@119 1646 m_columnMags.resize(s1 + 1);
Chris@119 1647 }
Chris@119 1648
Chris@119 1649 for (int s = s0; s <= s1; ++s) {
Chris@119 1650 if (m_columnMags[s].isSet()) {
Chris@119 1651 mag.sample(m_columnMags[s]);
Chris@119 1652 }
Chris@119 1653 }
Chris@119 1654
Chris@184 1655 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@119 1656 std::cerr << "SpectrogramLayer::updateViewMagnitudes returning from cols "
Chris@119 1657 << s0 << " -> " << s1 << " inclusive" << std::endl;
Chris@184 1658 #endif
Chris@119 1659
Chris@119 1660 if (!mag.isSet()) return false;
Chris@119 1661 if (mag == m_viewMags[v]) return false;
Chris@119 1662 m_viewMags[v] = mag;
Chris@119 1663 return true;
Chris@119 1664 }
Chris@119 1665
Chris@119 1666 void
Chris@44 1667 SpectrogramLayer::paint(View *v, QPainter &paint, QRect rect) const
Chris@0 1668 {
Chris@196 1669 if (m_colourScheme == (int)ColourMapper::BlackOnWhite) {
Chris@55 1670 v->setLightBackground(true);
Chris@55 1671 } else {
Chris@55 1672 v->setLightBackground(false);
Chris@55 1673 }
Chris@55 1674
Chris@161 1675 Profiler profiler("SpectrogramLayer::paint", true);
Chris@0 1676 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@95 1677 std::cerr << "SpectrogramLayer::paint(): m_model is " << m_model << ", zoom level is " << v->getZoomLevel() << ", m_updateTimer " << m_updateTimer << std::endl;
Chris@95 1678
Chris@95 1679 std::cerr << "rect is " << rect.x() << "," << rect.y() << " " << rect.width() << "x" << rect.height() << std::endl;
Chris@0 1680 #endif
Chris@95 1681
Chris@133 1682 long startFrame = v->getStartFrame();
Chris@133 1683 if (startFrame < 0) m_candidateFillStartFrame = 0;
Chris@133 1684 else m_candidateFillStartFrame = startFrame;
Chris@44 1685
Chris@0 1686 if (!m_model || !m_model->isOK() || !m_model->isReady()) {
Chris@0 1687 return;
Chris@0 1688 }
Chris@0 1689
Chris@47 1690 if (isLayerDormant(v)) {
Chris@48 1691 std::cerr << "SpectrogramLayer::paint(): Layer is dormant, making it undormant again" << std::endl;
Chris@29 1692 }
Chris@29 1693
Chris@48 1694 // Need to do this even if !isLayerDormant, as that could mean v
Chris@48 1695 // is not in the dormancy map at all -- we need it to be present
Chris@48 1696 // and accountable for when determining whether we need the cache
Chris@48 1697 // in the cache-fill thread above.
Chris@131 1698 //!!! no longer use cache-fill thread
Chris@131 1699 const_cast<SpectrogramLayer *>(this)->Layer::setLayerDormant(v, false);
Chris@48 1700
Chris@114 1701 size_t fftSize = getFFTSize(v);
Chris@130 1702 FFTModel *fft = getFFTModel(v);
Chris@114 1703 if (!fft) {
Chris@130 1704 std::cerr << "ERROR: SpectrogramLayer::paint(): No FFT model, returning" << std::endl;
Chris@0 1705 return;
Chris@0 1706 }
Chris@0 1707
Chris@95 1708 PixmapCache &cache = m_pixmapCaches[v];
Chris@95 1709
Chris@95 1710 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@95 1711 std::cerr << "SpectrogramLayer::paint(): pixmap cache valid area " << cache.validArea.x() << ", " << cache.validArea.y() << ", " << cache.validArea.width() << "x" << cache.validArea.height() << std::endl;
Chris@95 1712 #endif
Chris@95 1713
Chris@0 1714 bool stillCacheing = (m_updateTimer != 0);
Chris@0 1715
Chris@0 1716 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@0 1717 std::cerr << "SpectrogramLayer::paint(): Still cacheing = " << stillCacheing << std::endl;
Chris@0 1718 #endif
Chris@0 1719
Chris@44 1720 int zoomLevel = v->getZoomLevel();
Chris@0 1721
Chris@0 1722 int x0 = 0;
Chris@44 1723 int x1 = v->width();
Chris@0 1724 int y0 = 0;
Chris@44 1725 int y1 = v->height();
Chris@0 1726
Chris@0 1727 bool recreateWholePixmapCache = true;
Chris@0 1728
Chris@95 1729 x0 = rect.left();
Chris@95 1730 x1 = rect.right() + 1;
Chris@95 1731 y0 = rect.top();
Chris@95 1732 y1 = rect.bottom() + 1;
Chris@95 1733
Chris@95 1734 if (cache.validArea.width() > 0) {
Chris@95 1735
Chris@95 1736 if (int(cache.zoomLevel) == zoomLevel &&
Chris@95 1737 cache.pixmap.width() == v->width() &&
Chris@95 1738 cache.pixmap.height() == v->height()) {
Chris@95 1739
Chris@95 1740 if (v->getXForFrame(cache.startFrame) ==
Chris@95 1741 v->getXForFrame(startFrame) &&
Chris@95 1742 cache.validArea.x() <= x0 &&
Chris@95 1743 cache.validArea.x() + cache.validArea.width() >= x1) {
Chris@0 1744
Chris@0 1745 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@0 1746 std::cerr << "SpectrogramLayer: pixmap cache good" << std::endl;
Chris@0 1747 #endif
Chris@0 1748
Chris@95 1749 paint.drawPixmap(rect, cache.pixmap, rect);
Chris@121 1750 illuminateLocalFeatures(v, paint);
Chris@0 1751 return;
Chris@0 1752
Chris@0 1753 } else {
Chris@0 1754
Chris@0 1755 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@0 1756 std::cerr << "SpectrogramLayer: pixmap cache partially OK" << std::endl;
Chris@0 1757 #endif
Chris@0 1758
Chris@0 1759 recreateWholePixmapCache = false;
Chris@0 1760
Chris@95 1761 int dx = v->getXForFrame(cache.startFrame) -
Chris@44 1762 v->getXForFrame(startFrame);
Chris@0 1763
Chris@0 1764 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@95 1765 std::cerr << "SpectrogramLayer: dx = " << dx << " (pixmap cache " << cache.pixmap.width() << "x" << cache.pixmap.height() << ")" << std::endl;
Chris@0 1766 #endif
Chris@0 1767
Chris@95 1768 if (dx != 0 &&
Chris@95 1769 dx > -cache.pixmap.width() &&
Chris@95 1770 dx < cache.pixmap.width()) {
Chris@0 1771
Chris@0 1772 #if defined(Q_WS_WIN32) || defined(Q_WS_MAC)
Chris@0 1773 // Copying a pixmap to itself doesn't work
Chris@0 1774 // properly on Windows or Mac (it only works when
Chris@0 1775 // moving in one direction).
Chris@0 1776
Chris@0 1777 //!!! Need a utility function for this
Chris@0 1778
Chris@0 1779 static QPixmap *tmpPixmap = 0;
Chris@0 1780 if (!tmpPixmap ||
Chris@95 1781 tmpPixmap->width() != cache.pixmap.width() ||
Chris@95 1782 tmpPixmap->height() != cache.pixmap.height()) {
Chris@0 1783 delete tmpPixmap;
Chris@95 1784 tmpPixmap = new QPixmap(cache.pixmap.width(),
Chris@95 1785 cache.pixmap.height());
Chris@0 1786 }
Chris@0 1787 QPainter cachePainter;
Chris@0 1788 cachePainter.begin(tmpPixmap);
Chris@95 1789 cachePainter.drawPixmap(0, 0, cache.pixmap);
Chris@0 1790 cachePainter.end();
Chris@95 1791 cachePainter.begin(&cache.pixmap);
Chris@0 1792 cachePainter.drawPixmap(dx, 0, *tmpPixmap);
Chris@0 1793 cachePainter.end();
Chris@0 1794 #else
Chris@95 1795 QPainter cachePainter(&cache.pixmap);
Chris@95 1796 cachePainter.drawPixmap(dx, 0, cache.pixmap);
Chris@0 1797 cachePainter.end();
Chris@0 1798 #endif
Chris@0 1799
Chris@95 1800 int px = cache.validArea.x();
Chris@95 1801 int pw = cache.validArea.width();
Chris@0 1802
Chris@0 1803 if (dx < 0) {
Chris@95 1804 x0 = cache.pixmap.width() + dx;
Chris@95 1805 x1 = cache.pixmap.width();
Chris@95 1806 px += dx;
Chris@95 1807 if (px < 0) {
Chris@95 1808 pw += px;
Chris@95 1809 px = 0;
Chris@95 1810 if (pw < 0) pw = 0;
Chris@95 1811 }
Chris@0 1812 } else {
Chris@0 1813 x0 = 0;
Chris@0 1814 x1 = dx;
Chris@95 1815 px += dx;
Chris@95 1816 if (px + pw > cache.pixmap.width()) {
Chris@95 1817 pw = int(cache.pixmap.width()) - px;
Chris@95 1818 if (pw < 0) pw = 0;
Chris@95 1819 }
Chris@0 1820 }
Chris@95 1821
Chris@95 1822 cache.validArea =
Chris@95 1823 QRect(px, cache.validArea.y(),
Chris@95 1824 pw, cache.validArea.height());
Chris@95 1825
Chris@95 1826 paint.drawPixmap(rect & cache.validArea,
Chris@95 1827 cache.pixmap,
Chris@95 1828 rect & cache.validArea);
Chris@0 1829 }
Chris@0 1830 }
Chris@0 1831 } else {
Chris@0 1832 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@0 1833 std::cerr << "SpectrogramLayer: pixmap cache useless" << std::endl;
Chris@0 1834 #endif
Chris@95 1835 cache.validArea = QRect();
Chris@0 1836 }
Chris@0 1837 }
Chris@95 1838
Chris@92 1839 /*
Chris@0 1840 if (stillCacheing) {
Chris@0 1841 x0 = rect.left();
Chris@0 1842 x1 = rect.right() + 1;
Chris@0 1843 y0 = rect.top();
Chris@0 1844 y1 = rect.bottom() + 1;
Chris@0 1845 }
Chris@92 1846 */
Chris@95 1847
Chris@133 1848 if (updateViewMagnitudes(v)) {
Chris@184 1849 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@133 1850 std::cerr << "SpectrogramLayer: magnitude range changed to [" << m_viewMags[v].getMin() << "->" << m_viewMags[v].getMax() << "]" << std::endl;
Chris@184 1851 #endif
Chris@133 1852 recreateWholePixmapCache = true;
Chris@133 1853 } else {
Chris@184 1854 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@133 1855 std::cerr << "No change in magnitude range [" << m_viewMags[v].getMin() << "->" << m_viewMags[v].getMax() << "]" << std::endl;
Chris@184 1856 #endif
Chris@133 1857 }
Chris@133 1858
Chris@95 1859 if (recreateWholePixmapCache) {
Chris@95 1860 x0 = 0;
Chris@95 1861 x1 = v->width();
Chris@95 1862 }
Chris@95 1863
Chris@188 1864 //!!! This width should really depend on how fast the machine is
Chris@188 1865 //at redrawing the spectrogram. We could fairly easily time that,
Chris@188 1866 //in this function, and adjust accordingly. The following is
Chris@188 1867 //probably about as small as the block width should go.
Chris@96 1868 int paintBlockWidth = (300000 / zoomLevel);
Chris@96 1869 if (paintBlockWidth < 20) paintBlockWidth = 20;
Chris@96 1870
Chris@96 1871 if (cache.validArea.width() > 0) {
Chris@96 1872
Chris@96 1873 int vx0 = 0, vx1 = 0;
Chris@96 1874 vx0 = cache.validArea.x();
Chris@96 1875 vx1 = cache.validArea.x() + cache.validArea.width();
Chris@96 1876
Chris@96 1877 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@96 1878 std::cerr << "x0 " << x0 << ", x1 " << x1 << ", vx0 " << vx0 << ", vx1 " << vx1 << ", paintBlockWidth " << paintBlockWidth << std::endl;
Chris@96 1879 #endif
Chris@96 1880 if (x0 < vx0) {
Chris@96 1881 if (x0 + paintBlockWidth < vx0) {
Chris@96 1882 x0 = vx0 - paintBlockWidth;
Chris@96 1883 } else {
Chris@96 1884 x0 = 0;
Chris@96 1885 }
Chris@96 1886 } else if (x0 > vx1) {
Chris@96 1887 x0 = vx1;
Chris@96 1888 }
Chris@95 1889
Chris@96 1890 if (x1 < vx0) {
Chris@96 1891 x1 = vx0;
Chris@96 1892 } else if (x1 > vx1) {
Chris@96 1893 if (vx1 + paintBlockWidth < x1) {
Chris@96 1894 x1 = vx1 + paintBlockWidth;
Chris@96 1895 } else {
Chris@96 1896 x1 = v->width();
Chris@95 1897 }
Chris@96 1898 }
Chris@95 1899
Chris@96 1900 cache.validArea = QRect
Chris@96 1901 (std::min(vx0, x0), cache.validArea.y(),
Chris@96 1902 std::max(vx1 - std::min(vx0, x0),
Chris@96 1903 x1 - std::min(vx0, x0)),
Chris@96 1904 cache.validArea.height());
Chris@95 1905
Chris@96 1906 } else {
Chris@96 1907 if (x1 > x0 + paintBlockWidth) {
Chris@133 1908 int sfx = x1;
Chris@133 1909 if (startFrame < 0) sfx = v->getXForFrame(0);
Chris@133 1910 if (sfx >= x0 && sfx + paintBlockWidth <= x1) {
Chris@133 1911 x0 = sfx;
Chris@133 1912 x1 = x0 + paintBlockWidth;
Chris@133 1913 } else {
Chris@133 1914 int mid = (x1 + x0) / 2;
Chris@133 1915 x0 = mid - paintBlockWidth/2;
Chris@133 1916 x1 = x0 + paintBlockWidth;
Chris@133 1917 }
Chris@95 1918 }
Chris@96 1919 cache.validArea = QRect(x0, 0, x1 - x0, v->height());
Chris@95 1920 }
Chris@95 1921
Chris@0 1922 int w = x1 - x0;
Chris@0 1923 int h = y1 - y0;
Chris@0 1924
Chris@95 1925 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@95 1926 std::cerr << "x0 " << x0 << ", x1 " << x1 << ", w " << w << ", h " << h << std::endl;
Chris@95 1927 #endif
Chris@95 1928
Chris@95 1929 if (m_drawBuffer.width() < w || m_drawBuffer.height() < h) {
Chris@95 1930 m_drawBuffer = QImage(w, h, QImage::Format_RGB32);
Chris@95 1931 }
Chris@95 1932
Chris@97 1933 m_drawBuffer.fill(m_colourMap.getColour(0).rgb());
Chris@35 1934
Chris@37 1935 int sr = m_model->getSampleRate();
Chris@122 1936
Chris@122 1937 // Set minFreq and maxFreq to the frequency extents of the possibly
Chris@122 1938 // zero-padded visible bin range, and displayMinFreq and displayMaxFreq
Chris@122 1939 // to the actual scale frequency extents (presumably not zero padded).
Chris@35 1940
Chris@184 1941 size_t maxbin = fftSize / 2;
Chris@35 1942 if (m_maxFrequency > 0) {
Chris@184 1943 maxbin = int((double(m_maxFrequency) * fftSize) / sr + 0.1);
Chris@184 1944 if (maxbin > fftSize / 2) maxbin = fftSize / 2;
Chris@35 1945 }
Chris@111 1946
Chris@40 1947 size_t minbin = 1;
Chris@37 1948 if (m_minFrequency > 0) {
Chris@114 1949 minbin = int((double(m_minFrequency) * fftSize) / sr + 0.1);
Chris@40 1950 if (minbin < 1) minbin = 1;
Chris@184 1951 if (minbin >= maxbin) minbin = maxbin - 1;
Chris@37 1952 }
Chris@37 1953
Chris@114 1954 float minFreq = (float(minbin) * sr) / fftSize;
Chris@184 1955 float maxFreq = (float(maxbin) * sr) / fftSize;
Chris@0 1956
Chris@122 1957 float displayMinFreq = minFreq;
Chris@122 1958 float displayMaxFreq = maxFreq;
Chris@122 1959
Chris@122 1960 if (fftSize != m_fftSize) {
Chris@122 1961 displayMinFreq = getEffectiveMinFrequency();
Chris@122 1962 displayMaxFreq = getEffectiveMaxFrequency();
Chris@122 1963 }
Chris@122 1964
Chris@92 1965 float ymag[h];
Chris@92 1966 float ydiv[h];
Chris@184 1967 float yval[maxbin + 1]; //!!! cache this?
Chris@92 1968
Chris@38 1969 size_t increment = getWindowIncrement();
Chris@40 1970
Chris@40 1971 bool logarithmic = (m_frequencyScale == LogFrequencyScale);
Chris@38 1972
Chris@184 1973 for (size_t q = minbin; q <= maxbin; ++q) {
Chris@114 1974 float f0 = (float(q) * sr) / fftSize;
Chris@122 1975 yval[q] = v->getYForFrequency(f0, displayMinFreq, displayMaxFreq,
Chris@122 1976 logarithmic);
Chris@122 1977 // std::cerr << "min: " << minFreq << ", max: " << maxFreq << ", yval[" << q << "]: " << yval[q] << std::endl;
Chris@92 1978 }
Chris@92 1979
Chris@119 1980 MagnitudeRange overallMag = m_viewMags[v];
Chris@119 1981 bool overallMagChanged = false;
Chris@119 1982
Chris@162 1983 bool fftSuspended = false;
Chris@131 1984
Chris@137 1985 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@137 1986 std::cerr << (float(v->getFrameForX(1) - v->getFrameForX(0)) / increment) << " bins per pixel" << std::endl;
Chris@137 1987 #endif
Chris@137 1988
Chris@35 1989 for (int x = 0; x < w; ++x) {
Chris@35 1990
Chris@35 1991 for (int y = 0; y < h; ++y) {
Chris@134 1992 ymag[y] = 0.f;
Chris@134 1993 ydiv[y] = 0.f;
Chris@35 1994 }
Chris@35 1995
Chris@35 1996 float s0 = 0, s1 = 0;
Chris@35 1997
Chris@44 1998 if (!getXBinRange(v, x0 + x, s0, s1)) {
Chris@95 1999 assert(x <= m_drawBuffer.width());
Chris@35 2000 continue;
Chris@35 2001 }
Chris@35 2002
Chris@35 2003 int s0i = int(s0 + 0.001);
Chris@35 2004 int s1i = int(s1);
Chris@35 2005
Chris@114 2006 if (s1i >= fft->getWidth()) {
Chris@114 2007 if (s0i >= fft->getWidth()) {
Chris@45 2008 continue;
Chris@45 2009 } else {
Chris@45 2010 s1i = s0i;
Chris@45 2011 }
Chris@45 2012 }
Chris@92 2013
Chris@92 2014 for (int s = s0i; s <= s1i; ++s) {
Chris@92 2015
Chris@160 2016 if (!fft->isColumnAvailable(s)) continue;
Chris@162 2017
Chris@162 2018 if (!fftSuspended) {
Chris@162 2019 fft->suspendWrites();
Chris@162 2020 fftSuspended = true;
Chris@162 2021 }
Chris@162 2022
Chris@119 2023 MagnitudeRange mag;
Chris@92 2024
Chris@184 2025 for (size_t q = minbin; q < maxbin; ++q) {
Chris@92 2026
Chris@92 2027 float y0 = yval[q + 1];
Chris@92 2028 float y1 = yval[q];
Chris@92 2029
Chris@40 2030 if (m_binDisplay == PeakBins ||
Chris@40 2031 m_binDisplay == PeakFrequencies) {
Chris@114 2032 if (!fft->isLocalPeak(s, q)) continue;
Chris@40 2033 }
Chris@114 2034
Chris@114 2035 if (m_threshold != 0.f &&
Chris@114 2036 !fft->isOverThreshold(s, q, m_threshold)) {
Chris@114 2037 continue;
Chris@114 2038 }
Chris@40 2039
Chris@35 2040 float sprop = 1.0;
Chris@35 2041 if (s == s0i) sprop *= (s + 1) - s0;
Chris@35 2042 if (s == s1i) sprop *= s1 - s;
Chris@35 2043
Chris@38 2044 if (m_binDisplay == PeakFrequencies &&
Chris@114 2045 s < int(fft->getWidth()) - 1) {
Chris@35 2046
Chris@38 2047 bool steady = false;
Chris@92 2048 float f = calculateFrequency(q,
Chris@38 2049 m_windowSize,
Chris@38 2050 increment,
Chris@38 2051 sr,
Chris@114 2052 fft->getPhaseAt(s, q),
Chris@114 2053 fft->getPhaseAt(s+1, q),
Chris@38 2054 steady);
Chris@40 2055
Chris@44 2056 y0 = y1 = v->getYForFrequency
Chris@122 2057 (f, displayMinFreq, displayMaxFreq, logarithmic);
Chris@35 2058 }
Chris@38 2059
Chris@35 2060 int y0i = int(y0 + 0.001);
Chris@35 2061 int y1i = int(y1);
Chris@35 2062
Chris@92 2063 float value;
Chris@92 2064
Chris@92 2065 if (m_colourScale == PhaseColourScale) {
Chris@114 2066 value = fft->getPhaseAt(s, q);
Chris@92 2067 } else if (m_normalizeColumns) {
Chris@119 2068 value = fft->getNormalizedMagnitudeAt(s, q);
Chris@119 2069 mag.sample(value);
Chris@119 2070 value *= m_gain;
Chris@92 2071 } else {
Chris@119 2072 value = fft->getMagnitudeAt(s, q);
Chris@119 2073 mag.sample(value);
Chris@119 2074 value *= m_gain;
Chris@92 2075 }
Chris@92 2076
Chris@35 2077 for (int y = y0i; y <= y1i; ++y) {
Chris@35 2078
Chris@35 2079 if (y < 0 || y >= h) continue;
Chris@35 2080
Chris@35 2081 float yprop = sprop;
Chris@35 2082 if (y == y0i) yprop *= (y + 1) - y0;
Chris@35 2083 if (y == y1i) yprop *= y1 - y;
Chris@37 2084 ymag[y] += yprop * value;
Chris@35 2085 ydiv[y] += yprop;
Chris@35 2086 }
Chris@35 2087 }
Chris@119 2088
Chris@119 2089 if (mag.isSet()) {
Chris@119 2090
Chris@119 2091 m_columnMags[s].sample(mag);
Chris@119 2092
Chris@119 2093 if (overallMag.sample(mag)) {
Chris@119 2094 //!!! scaling would change here
Chris@119 2095 overallMagChanged = true;
Chris@119 2096 std::cerr << "Overall mag changed (again?) at column " << s << ", to [" << overallMag.getMin() << "->" << overallMag.getMax() << "]" << std::endl;
Chris@119 2097 }
Chris@119 2098 }
Chris@35 2099 }
Chris@35 2100
Chris@35 2101 for (int y = 0; y < h; ++y) {
Chris@35 2102
Chris@35 2103 if (ydiv[y] > 0.0) {
Chris@40 2104
Chris@40 2105 unsigned char pixel = 0;
Chris@40 2106
Chris@38 2107 float avg = ymag[y] / ydiv[y];
Chris@138 2108 pixel = getDisplayValue(v, avg);
Chris@40 2109
Chris@95 2110 assert(x <= m_drawBuffer.width());
Chris@86 2111 QColor c = m_colourMap.getColour(pixel);
Chris@95 2112 m_drawBuffer.setPixel(x, y,
Chris@95 2113 qRgb(c.red(), c.green(), c.blue()));
Chris@35 2114 }
Chris@35 2115 }
Chris@35 2116 }
Chris@35 2117
Chris@119 2118 if (overallMagChanged) {
Chris@119 2119 m_viewMags[v] = overallMag;
Chris@119 2120 std::cerr << "Overall mag is now [" << m_viewMags[v].getMin() << "->" << m_viewMags[v].getMax() << "] - will be updating" << std::endl;
Chris@119 2121 } else {
Chris@119 2122 std::cerr << "Overall mag unchanged at [" << m_viewMags[v].getMin() << "->" << m_viewMags[v].getMax() << "]" << std::endl;
Chris@119 2123 }
Chris@119 2124
Chris@161 2125 Profiler profiler2("SpectrogramLayer::paint: draw image", true);
Chris@137 2126
Chris@95 2127 paint.drawImage(x0, y0, m_drawBuffer, 0, 0, w, h);
Chris@0 2128
Chris@0 2129 if (recreateWholePixmapCache) {
Chris@95 2130 cache.pixmap = QPixmap(v->width(), v->height());
Chris@0 2131 }
Chris@0 2132
Chris@95 2133 QPainter cachePainter(&cache.pixmap);
Chris@95 2134 cachePainter.drawImage(x0, y0, m_drawBuffer, 0, 0, w, h);
Chris@0 2135 cachePainter.end();
Chris@119 2136
Chris@120 2137 if (!m_normalizeVisibleArea || !overallMagChanged) {
Chris@0 2138
Chris@119 2139 cache.startFrame = startFrame;
Chris@119 2140 cache.zoomLevel = zoomLevel;
Chris@119 2141
Chris@119 2142 if (cache.validArea.x() > 0) {
Chris@95 2143 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@158 2144 std::cerr << "SpectrogramLayer::paint() updating left (0, "
Chris@158 2145 << cache.validArea.x() << ")" << std::endl;
Chris@95 2146 #endif
Chris@119 2147 v->update(0, 0, cache.validArea.x(), v->height());
Chris@119 2148 }
Chris@119 2149
Chris@119 2150 if (cache.validArea.x() + cache.validArea.width() <
Chris@119 2151 cache.pixmap.width()) {
Chris@95 2152 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@119 2153 std::cerr << "SpectrogramLayer::paint() updating right ("
Chris@119 2154 << cache.validArea.x() + cache.validArea.width()
Chris@119 2155 << ", "
Chris@119 2156 << cache.pixmap.width() - (cache.validArea.x() +
Chris@119 2157 cache.validArea.width())
Chris@119 2158 << ")" << std::endl;
Chris@95 2159 #endif
Chris@119 2160 v->update(cache.validArea.x() + cache.validArea.width(),
Chris@119 2161 0,
Chris@119 2162 cache.pixmap.width() - (cache.validArea.x() +
Chris@119 2163 cache.validArea.width()),
Chris@119 2164 v->height());
Chris@119 2165 }
Chris@119 2166 } else {
Chris@119 2167 // overallMagChanged
Chris@119 2168 cache.validArea = QRect();
Chris@119 2169 v->update();
Chris@95 2170 }
Chris@0 2171
Chris@121 2172 illuminateLocalFeatures(v, paint);
Chris@120 2173
Chris@0 2174 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@0 2175 std::cerr << "SpectrogramLayer::paint() returning" << std::endl;
Chris@0 2176 #endif
Chris@131 2177
Chris@162 2178 if (fftSuspended) fft->resume();
Chris@0 2179 }
Chris@0 2180
Chris@121 2181 void
Chris@121 2182 SpectrogramLayer::illuminateLocalFeatures(View *v, QPainter &paint) const
Chris@121 2183 {
Chris@121 2184 QPoint localPos;
Chris@121 2185 if (!v->shouldIlluminateLocalFeatures(this, localPos) || !m_model) {
Chris@121 2186 return;
Chris@121 2187 }
Chris@121 2188
Chris@180 2189 // std::cerr << "SpectrogramLayer: illuminateLocalFeatures("
Chris@180 2190 // << localPos.x() << "," << localPos.y() << ")" << std::endl;
Chris@121 2191
Chris@121 2192 float s0, s1;
Chris@121 2193 float f0, f1;
Chris@121 2194
Chris@121 2195 if (getXBinRange(v, localPos.x(), s0, s1) &&
Chris@121 2196 getYBinSourceRange(v, localPos.y(), f0, f1)) {
Chris@121 2197
Chris@121 2198 int s0i = int(s0 + 0.001);
Chris@121 2199 int s1i = int(s1);
Chris@121 2200
Chris@121 2201 int x0 = v->getXForFrame(s0i * getWindowIncrement());
Chris@121 2202 int x1 = v->getXForFrame((s1i + 1) * getWindowIncrement());
Chris@121 2203
Chris@121 2204 int y1 = getYForFrequency(v, f1);
Chris@121 2205 int y0 = getYForFrequency(v, f0);
Chris@121 2206
Chris@180 2207 // std::cerr << "SpectrogramLayer: illuminate "
Chris@180 2208 // << x0 << "," << y1 << " -> " << x1 << "," << y0 << std::endl;
Chris@121 2209
Chris@121 2210 paint.setPen(Qt::white);
Chris@133 2211
Chris@133 2212 //!!! should we be using paintCrosshairs for this?
Chris@133 2213
Chris@121 2214 paint.drawRect(x0, y1, x1 - x0 + 1, y0 - y1 + 1);
Chris@121 2215 }
Chris@121 2216 }
Chris@121 2217
Chris@42 2218 float
Chris@44 2219 SpectrogramLayer::getYForFrequency(View *v, float frequency) const
Chris@42 2220 {
Chris@44 2221 return v->getYForFrequency(frequency,
Chris@44 2222 getEffectiveMinFrequency(),
Chris@44 2223 getEffectiveMaxFrequency(),
Chris@44 2224 m_frequencyScale == LogFrequencyScale);
Chris@42 2225 }
Chris@42 2226
Chris@42 2227 float
Chris@44 2228 SpectrogramLayer::getFrequencyForY(View *v, int y) const
Chris@42 2229 {
Chris@44 2230 return v->getFrequencyForY(y,
Chris@44 2231 getEffectiveMinFrequency(),
Chris@44 2232 getEffectiveMaxFrequency(),
Chris@44 2233 m_frequencyScale == LogFrequencyScale);
Chris@42 2234 }
Chris@42 2235
Chris@0 2236 int
Chris@115 2237 SpectrogramLayer::getCompletion(View *v) const
Chris@0 2238 {
Chris@115 2239 if (m_updateTimer == 0) return 100;
Chris@130 2240 if (m_fftModels.find(v) == m_fftModels.end()) return 100;
Chris@130 2241
Chris@130 2242 size_t completion = m_fftModels[v].first->getCompletion();
Chris@115 2243 std::cerr << "SpectrogramLayer::getCompletion: completion = " << completion << std::endl;
Chris@0 2244 return completion;
Chris@0 2245 }
Chris@0 2246
Chris@28 2247 bool
Chris@101 2248 SpectrogramLayer::getValueExtents(float &min, float &max,
Chris@101 2249 bool &logarithmic, QString &unit) const
Chris@79 2250 {
Chris@133 2251 //!!!
Chris@133 2252 // min = getEffectiveMinFrequency();
Chris@133 2253 // max = getEffectiveMaxFrequency();
Chris@133 2254
Chris@133 2255 if (!m_model) return false;
Chris@133 2256
Chris@133 2257 int sr = m_model->getSampleRate();
Chris@133 2258 min = float(sr) / m_fftSize;
Chris@133 2259 max = float(sr) / 2;
Chris@133 2260
Chris@101 2261 logarithmic = (m_frequencyScale == LogFrequencyScale);
Chris@79 2262 unit = "Hz";
Chris@79 2263 return true;
Chris@79 2264 }
Chris@79 2265
Chris@79 2266 bool
Chris@101 2267 SpectrogramLayer::getDisplayExtents(float &min, float &max) const
Chris@101 2268 {
Chris@101 2269 min = getEffectiveMinFrequency();
Chris@101 2270 max = getEffectiveMaxFrequency();
Chris@187 2271 std::cerr << "SpectrogramLayer::getDisplayExtents: " << min << "->" << max << std::endl;
Chris@101 2272 return true;
Chris@101 2273 }
Chris@101 2274
Chris@101 2275 bool
Chris@120 2276 SpectrogramLayer::setDisplayExtents(float min, float max)
Chris@120 2277 {
Chris@120 2278 if (!m_model) return false;
Chris@187 2279
Chris@187 2280 std::cerr << "SpectrogramLayer::setDisplayExtents: " << min << "->" << max << std::endl;
Chris@187 2281
Chris@120 2282 if (min < 0) min = 0;
Chris@120 2283 if (max > m_model->getSampleRate()/2) max = m_model->getSampleRate()/2;
Chris@120 2284
Chris@120 2285 size_t minf = lrintf(min);
Chris@120 2286 size_t maxf = lrintf(max);
Chris@120 2287
Chris@120 2288 if (m_minFrequency == minf && m_maxFrequency == maxf) return true;
Chris@120 2289
Chris@120 2290 invalidatePixmapCaches();
Chris@120 2291 invalidateMagnitudes();
Chris@120 2292
Chris@120 2293 m_minFrequency = minf;
Chris@120 2294 m_maxFrequency = maxf;
Chris@120 2295
Chris@120 2296 emit layerParametersChanged();
Chris@120 2297
Chris@133 2298 int vs = getCurrentVerticalZoomStep();
Chris@133 2299 if (vs != m_lastEmittedZoomStep) {
Chris@133 2300 emit verticalZoomChanged();
Chris@133 2301 m_lastEmittedZoomStep = vs;
Chris@133 2302 }
Chris@133 2303
Chris@120 2304 return true;
Chris@120 2305 }
Chris@120 2306
Chris@120 2307 bool
Chris@44 2308 SpectrogramLayer::snapToFeatureFrame(View *v, int &frame,
Chris@28 2309 size_t &resolution,
Chris@28 2310 SnapType snap) const
Chris@13 2311 {
Chris@13 2312 resolution = getWindowIncrement();
Chris@28 2313 int left = (frame / resolution) * resolution;
Chris@28 2314 int right = left + resolution;
Chris@28 2315
Chris@28 2316 switch (snap) {
Chris@28 2317 case SnapLeft: frame = left; break;
Chris@28 2318 case SnapRight: frame = right; break;
Chris@28 2319 case SnapNearest:
Chris@28 2320 case SnapNeighbouring:
Chris@28 2321 if (frame - left > right - frame) frame = right;
Chris@28 2322 else frame = left;
Chris@28 2323 break;
Chris@28 2324 }
Chris@28 2325
Chris@28 2326 return true;
Chris@28 2327 }
Chris@13 2328
Chris@77 2329 bool
Chris@77 2330 SpectrogramLayer::getCrosshairExtents(View *v, QPainter &paint,
Chris@77 2331 QPoint cursorPos,
Chris@77 2332 std::vector<QRect> &extents) const
Chris@77 2333 {
Chris@77 2334 QRect vertical(cursorPos.x() - 12, 0, 12, v->height());
Chris@77 2335 extents.push_back(vertical);
Chris@77 2336
Chris@77 2337 QRect horizontal(0, cursorPos.y(), cursorPos.x(), 1);
Chris@77 2338 extents.push_back(horizontal);
Chris@77 2339
Chris@77 2340 return true;
Chris@77 2341 }
Chris@77 2342
Chris@77 2343 void
Chris@77 2344 SpectrogramLayer::paintCrosshairs(View *v, QPainter &paint,
Chris@77 2345 QPoint cursorPos) const
Chris@77 2346 {
Chris@77 2347 paint.save();
Chris@77 2348 paint.setPen(m_crosshairColour);
Chris@77 2349
Chris@77 2350 paint.drawLine(0, cursorPos.y(), cursorPos.x() - 1, cursorPos.y());
Chris@77 2351 paint.drawLine(cursorPos.x(), 0, cursorPos.x(), v->height());
Chris@77 2352
Chris@77 2353 float fundamental = getFrequencyForY(v, cursorPos.y());
Chris@77 2354
Chris@77 2355 int harmonic = 2;
Chris@77 2356
Chris@77 2357 while (harmonic < 100) {
Chris@77 2358
Chris@77 2359 float hy = lrintf(getYForFrequency(v, fundamental * harmonic));
Chris@77 2360 if (hy < 0 || hy > v->height()) break;
Chris@77 2361
Chris@77 2362 int len = 7;
Chris@77 2363
Chris@77 2364 if (harmonic % 2 == 0) {
Chris@77 2365 if (harmonic % 4 == 0) {
Chris@77 2366 len = 12;
Chris@77 2367 } else {
Chris@77 2368 len = 10;
Chris@77 2369 }
Chris@77 2370 }
Chris@77 2371
Chris@77 2372 paint.drawLine(cursorPos.x() - len,
Chris@77 2373 hy,
Chris@77 2374 cursorPos.x(),
Chris@77 2375 hy);
Chris@77 2376
Chris@77 2377 ++harmonic;
Chris@77 2378 }
Chris@77 2379
Chris@77 2380 paint.restore();
Chris@77 2381 }
Chris@77 2382
Chris@25 2383 QString
Chris@44 2384 SpectrogramLayer::getFeatureDescription(View *v, QPoint &pos) const
Chris@25 2385 {
Chris@25 2386 int x = pos.x();
Chris@25 2387 int y = pos.y();
Chris@0 2388
Chris@25 2389 if (!m_model || !m_model->isOK()) return "";
Chris@0 2390
Chris@38 2391 float magMin = 0, magMax = 0;
Chris@38 2392 float phaseMin = 0, phaseMax = 0;
Chris@0 2393 float freqMin = 0, freqMax = 0;
Chris@35 2394 float adjFreqMin = 0, adjFreqMax = 0;
Chris@25 2395 QString pitchMin, pitchMax;
Chris@0 2396 RealTime rtMin, rtMax;
Chris@0 2397
Chris@38 2398 bool haveValues = false;
Chris@0 2399
Chris@44 2400 if (!getXBinSourceRange(v, x, rtMin, rtMax)) {
Chris@38 2401 return "";
Chris@38 2402 }
Chris@44 2403 if (getXYBinSourceRange(v, x, y, magMin, magMax, phaseMin, phaseMax)) {
Chris@38 2404 haveValues = true;
Chris@38 2405 }
Chris@0 2406
Chris@35 2407 QString adjFreqText = "", adjPitchText = "";
Chris@35 2408
Chris@38 2409 if (m_binDisplay == PeakFrequencies) {
Chris@35 2410
Chris@44 2411 if (!getAdjustedYBinSourceRange(v, x, y, freqMin, freqMax,
Chris@38 2412 adjFreqMin, adjFreqMax)) {
Chris@38 2413 return "";
Chris@38 2414 }
Chris@35 2415
Chris@35 2416 if (adjFreqMin != adjFreqMax) {
Chris@65 2417 adjFreqText = tr("Peak Frequency:\t%1 - %2 Hz\n")
Chris@35 2418 .arg(adjFreqMin).arg(adjFreqMax);
Chris@35 2419 } else {
Chris@65 2420 adjFreqText = tr("Peak Frequency:\t%1 Hz\n")
Chris@35 2421 .arg(adjFreqMin);
Chris@38 2422 }
Chris@38 2423
Chris@38 2424 QString pmin = Pitch::getPitchLabelForFrequency(adjFreqMin);
Chris@38 2425 QString pmax = Pitch::getPitchLabelForFrequency(adjFreqMax);
Chris@38 2426
Chris@38 2427 if (pmin != pmax) {
Chris@65 2428 adjPitchText = tr("Peak Pitch:\t%3 - %4\n").arg(pmin).arg(pmax);
Chris@38 2429 } else {
Chris@65 2430 adjPitchText = tr("Peak Pitch:\t%2\n").arg(pmin);
Chris@35 2431 }
Chris@35 2432
Chris@35 2433 } else {
Chris@35 2434
Chris@44 2435 if (!getYBinSourceRange(v, y, freqMin, freqMax)) return "";
Chris@35 2436 }
Chris@35 2437
Chris@25 2438 QString text;
Chris@25 2439
Chris@25 2440 if (rtMin != rtMax) {
Chris@25 2441 text += tr("Time:\t%1 - %2\n")
Chris@25 2442 .arg(rtMin.toText(true).c_str())
Chris@25 2443 .arg(rtMax.toText(true).c_str());
Chris@25 2444 } else {
Chris@25 2445 text += tr("Time:\t%1\n")
Chris@25 2446 .arg(rtMin.toText(true).c_str());
Chris@0 2447 }
Chris@0 2448
Chris@25 2449 if (freqMin != freqMax) {
Chris@65 2450 text += tr("%1Bin Frequency:\t%2 - %3 Hz\n%4Bin Pitch:\t%5 - %6\n")
Chris@65 2451 .arg(adjFreqText)
Chris@25 2452 .arg(freqMin)
Chris@25 2453 .arg(freqMax)
Chris@65 2454 .arg(adjPitchText)
Chris@65 2455 .arg(Pitch::getPitchLabelForFrequency(freqMin))
Chris@65 2456 .arg(Pitch::getPitchLabelForFrequency(freqMax));
Chris@65 2457 } else {
Chris@65 2458 text += tr("%1Bin Frequency:\t%2 Hz\n%3Bin Pitch:\t%4\n")
Chris@35 2459 .arg(adjFreqText)
Chris@25 2460 .arg(freqMin)
Chris@65 2461 .arg(adjPitchText)
Chris@65 2462 .arg(Pitch::getPitchLabelForFrequency(freqMin));
Chris@25 2463 }
Chris@25 2464
Chris@38 2465 if (haveValues) {
Chris@38 2466 float dbMin = AudioLevel::multiplier_to_dB(magMin);
Chris@38 2467 float dbMax = AudioLevel::multiplier_to_dB(magMax);
Chris@43 2468 QString dbMinString;
Chris@43 2469 QString dbMaxString;
Chris@43 2470 if (dbMin == AudioLevel::DB_FLOOR) {
Chris@43 2471 dbMinString = tr("-Inf");
Chris@43 2472 } else {
Chris@43 2473 dbMinString = QString("%1").arg(lrintf(dbMin));
Chris@43 2474 }
Chris@43 2475 if (dbMax == AudioLevel::DB_FLOOR) {
Chris@43 2476 dbMaxString = tr("-Inf");
Chris@43 2477 } else {
Chris@43 2478 dbMaxString = QString("%1").arg(lrintf(dbMax));
Chris@43 2479 }
Chris@25 2480 if (lrintf(dbMin) != lrintf(dbMax)) {
Chris@25 2481 text += tr("dB:\t%1 - %2").arg(lrintf(dbMin)).arg(lrintf(dbMax));
Chris@25 2482 } else {
Chris@25 2483 text += tr("dB:\t%1").arg(lrintf(dbMin));
Chris@25 2484 }
Chris@38 2485 if (phaseMin != phaseMax) {
Chris@38 2486 text += tr("\nPhase:\t%1 - %2").arg(phaseMin).arg(phaseMax);
Chris@38 2487 } else {
Chris@38 2488 text += tr("\nPhase:\t%1").arg(phaseMin);
Chris@38 2489 }
Chris@25 2490 }
Chris@25 2491
Chris@25 2492 return text;
Chris@0 2493 }
Chris@25 2494
Chris@0 2495 int
Chris@40 2496 SpectrogramLayer::getColourScaleWidth(QPainter &paint) const
Chris@40 2497 {
Chris@40 2498 int cw;
Chris@40 2499
Chris@119 2500 cw = paint.fontMetrics().width("-80dB");
Chris@119 2501
Chris@119 2502 /*!!!
Chris@40 2503 switch (m_colourScale) {
Chris@40 2504 default:
Chris@40 2505 case LinearColourScale:
Chris@40 2506 cw = paint.fontMetrics().width(QString("0.00"));
Chris@40 2507 break;
Chris@40 2508
Chris@40 2509 case MeterColourScale:
Chris@40 2510 case dBColourScale:
Chris@119 2511 case OtherColourScale:
Chris@40 2512 cw = std::max(paint.fontMetrics().width(tr("-Inf")),
Chris@40 2513 paint.fontMetrics().width(tr("-90")));
Chris@40 2514 break;
Chris@40 2515
Chris@40 2516 case PhaseColourScale:
Chris@40 2517 cw = paint.fontMetrics().width(QString("-") + QChar(0x3c0));
Chris@40 2518 break;
Chris@40 2519 }
Chris@119 2520 */
Chris@119 2521
Chris@40 2522
Chris@40 2523 return cw;
Chris@40 2524 }
Chris@40 2525
Chris@40 2526 int
Chris@44 2527 SpectrogramLayer::getVerticalScaleWidth(View *v, QPainter &paint) const
Chris@0 2528 {
Chris@0 2529 if (!m_model || !m_model->isOK()) return 0;
Chris@0 2530
Chris@40 2531 int cw = getColourScaleWidth(paint);
Chris@40 2532
Chris@0 2533 int tw = paint.fontMetrics().width(QString("%1")
Chris@0 2534 .arg(m_maxFrequency > 0 ?
Chris@0 2535 m_maxFrequency - 1 :
Chris@0 2536 m_model->getSampleRate() / 2));
Chris@0 2537
Chris@0 2538 int fw = paint.fontMetrics().width(QString("43Hz"));
Chris@0 2539 if (tw < fw) tw = fw;
Chris@40 2540
Chris@40 2541 int tickw = (m_frequencyScale == LogFrequencyScale ? 10 : 4);
Chris@0 2542
Chris@40 2543 return cw + tickw + tw + 13;
Chris@0 2544 }
Chris@0 2545
Chris@0 2546 void
Chris@44 2547 SpectrogramLayer::paintVerticalScale(View *v, QPainter &paint, QRect rect) const
Chris@0 2548 {
Chris@0 2549 if (!m_model || !m_model->isOK()) {
Chris@0 2550 return;
Chris@0 2551 }
Chris@0 2552
Chris@161 2553 Profiler profiler("SpectrogramLayer::paintVerticalScale", true);
Chris@122 2554
Chris@120 2555 //!!! cache this?
Chris@120 2556
Chris@0 2557 int h = rect.height(), w = rect.width();
Chris@0 2558
Chris@40 2559 int tickw = (m_frequencyScale == LogFrequencyScale ? 10 : 4);
Chris@40 2560 int pkw = (m_frequencyScale == LogFrequencyScale ? 10 : 0);
Chris@40 2561
Chris@107 2562 size_t bins = m_fftSize / 2;
Chris@0 2563 int sr = m_model->getSampleRate();
Chris@0 2564
Chris@0 2565 if (m_maxFrequency > 0) {
Chris@107 2566 bins = int((double(m_maxFrequency) * m_fftSize) / sr + 0.1);
Chris@107 2567 if (bins > m_fftSize / 2) bins = m_fftSize / 2;
Chris@0 2568 }
Chris@0 2569
Chris@40 2570 int cw = getColourScaleWidth(paint);
Chris@119 2571 int cbw = paint.fontMetrics().width("dB");
Chris@40 2572
Chris@0 2573 int py = -1;
Chris@0 2574 int textHeight = paint.fontMetrics().height();
Chris@0 2575 int toff = -textHeight + paint.fontMetrics().ascent() + 2;
Chris@0 2576
Chris@119 2577 if (h > textHeight * 3 + 10) {
Chris@119 2578
Chris@119 2579 int topLines = 2;
Chris@119 2580 if (m_colourScale == PhaseColourScale) topLines = 1;
Chris@119 2581
Chris@119 2582 int ch = h - textHeight * (topLines + 1) - 8;
Chris@119 2583 // paint.drawRect(4, textHeight + 4, cw - 1, ch + 1);
Chris@119 2584 paint.drawRect(4 + cw - cbw, textHeight * topLines + 4, cbw - 1, ch + 1);
Chris@40 2585
Chris@40 2586 QString top, bottom;
Chris@119 2587 /*!!!
Chris@40 2588 switch (m_colourScale) {
Chris@40 2589 default:
Chris@40 2590 case LinearColourScale:
Chris@40 2591 top = (m_normalizeColumns ? "1.0" : "0.02");
Chris@40 2592 bottom = (m_normalizeColumns ? "0.0" : "0.00");
Chris@40 2593 break;
Chris@40 2594
Chris@40 2595 case MeterColourScale:
Chris@40 2596 top = (m_normalizeColumns ? QString("0") :
Chris@40 2597 QString("%1").arg(int(AudioLevel::multiplier_to_dB(0.02))));
Chris@40 2598 bottom = QString("%1").
Chris@40 2599 arg(int(AudioLevel::multiplier_to_dB
Chris@40 2600 (AudioLevel::preview_to_multiplier(0, 255))));
Chris@40 2601 break;
Chris@40 2602
Chris@40 2603 case dBColourScale:
Chris@119 2604 case OtherColourScale:
Chris@40 2605 top = "0";
Chris@40 2606 bottom = "-80";
Chris@40 2607 break;
Chris@40 2608
Chris@40 2609 case PhaseColourScale:
Chris@40 2610 top = QChar(0x3c0);
Chris@40 2611 bottom = "-" + top;
Chris@40 2612 break;
Chris@40 2613 }
Chris@119 2614 */
Chris@119 2615 float min = m_viewMags[v].getMin();
Chris@119 2616 float max = m_viewMags[v].getMax();
Chris@119 2617
Chris@119 2618 float dBmin = AudioLevel::multiplier_to_dB(min);
Chris@119 2619 float dBmax = AudioLevel::multiplier_to_dB(max);
Chris@119 2620
Chris@120 2621 if (dBmax < -60.f) dBmax = -60.f;
Chris@120 2622 else top = QString("%1").arg(lrintf(dBmax));
Chris@120 2623
Chris@120 2624 if (dBmin < dBmax - 60.f) dBmin = dBmax - 60.f;
Chris@119 2625 bottom = QString("%1").arg(lrintf(dBmin));
Chris@119 2626
Chris@119 2627 //!!! & phase etc
Chris@119 2628
Chris@119 2629 if (m_colourScale != PhaseColourScale) {
Chris@119 2630 paint.drawText((cw + 6 - paint.fontMetrics().width("dBFS")) / 2,
Chris@119 2631 2 + textHeight + toff, "dBFS");
Chris@119 2632 }
Chris@119 2633
Chris@119 2634 // paint.drawText((cw + 6 - paint.fontMetrics().width(top)) / 2,
Chris@119 2635 paint.drawText(3 + cw - cbw - paint.fontMetrics().width(top),
Chris@119 2636 2 + textHeight * topLines + toff + textHeight/2, top);
Chris@119 2637
Chris@119 2638 paint.drawText(3 + cw - cbw - paint.fontMetrics().width(bottom),
Chris@119 2639 h + toff - 3 - textHeight/2, bottom);
Chris@40 2640
Chris@40 2641 paint.save();
Chris@40 2642 paint.setBrush(Qt::NoBrush);
Chris@119 2643
Chris@119 2644 int lasty = 0;
Chris@119 2645 int lastdb = 0;
Chris@119 2646
Chris@40 2647 for (int i = 0; i < ch; ++i) {
Chris@119 2648
Chris@119 2649 float dBval = dBmin + (((dBmax - dBmin) * i) / (ch - 1));
Chris@119 2650 int idb = int(dBval);
Chris@119 2651
Chris@119 2652 float value = AudioLevel::dB_to_multiplier(dBval);
Chris@119 2653 int colour = getDisplayValue(v, value * m_gain);
Chris@119 2654 /*
Chris@119 2655 float value = min + (((max - min) * i) / (ch - 1));
Chris@119 2656 if (value < m_threshold) value = 0.f;
Chris@119 2657 int colour = getDisplayValue(v, value * m_gain);
Chris@119 2658 */
Chris@119 2659 /*
Chris@119 2660 int colour = (i * 255) / ch + 1;
Chris@119 2661 */
Chris@119 2662 paint.setPen(m_colourMap.getColour(colour));
Chris@119 2663
Chris@119 2664 int y = textHeight * topLines + 4 + ch - i;
Chris@119 2665
Chris@119 2666 paint.drawLine(5 + cw - cbw, y, cw + 2, y);
Chris@119 2667
Chris@119 2668 // paint.drawLine(5, 4 + textHeight + ch - i,
Chris@119 2669 // cw + 2, 4 + textHeight + ch - i);
Chris@119 2670
Chris@119 2671
Chris@119 2672 if (i == 0) {
Chris@119 2673 lasty = y;
Chris@119 2674 lastdb = idb;
Chris@119 2675 } else if (i < ch - paint.fontMetrics().ascent() &&
Chris@120 2676 idb != lastdb &&
Chris@119 2677 ((abs(y - lasty) > textHeight &&
Chris@119 2678 idb % 10 == 0) ||
Chris@119 2679 (abs(y - lasty) > paint.fontMetrics().ascent() &&
Chris@119 2680 idb % 5 == 0))) {
Chris@119 2681 paint.setPen(Qt::black);
Chris@119 2682 QString text = QString("%1").arg(idb);
Chris@119 2683 paint.drawText(3 + cw - cbw - paint.fontMetrics().width(text),
Chris@119 2684 y + toff + textHeight/2, text);
Chris@119 2685 paint.setPen(Qt::white);
Chris@119 2686 paint.drawLine(5 + cw - cbw, y, 8 + cw - cbw, y);
Chris@119 2687 lasty = y;
Chris@119 2688 lastdb = idb;
Chris@119 2689 }
Chris@40 2690 }
Chris@40 2691 paint.restore();
Chris@40 2692 }
Chris@40 2693
Chris@40 2694 paint.drawLine(cw + 7, 0, cw + 7, h);
Chris@40 2695
Chris@0 2696 int bin = -1;
Chris@0 2697
Chris@44 2698 for (int y = 0; y < v->height(); ++y) {
Chris@0 2699
Chris@0 2700 float q0, q1;
Chris@44 2701 if (!getYBinRange(v, v->height() - y, q0, q1)) continue;
Chris@0 2702
Chris@0 2703 int vy;
Chris@0 2704
Chris@0 2705 if (int(q0) > bin) {
Chris@0 2706 vy = y;
Chris@0 2707 bin = int(q0);
Chris@0 2708 } else {
Chris@0 2709 continue;
Chris@0 2710 }
Chris@0 2711
Chris@107 2712 int freq = (sr * bin) / m_fftSize;
Chris@0 2713
Chris@0 2714 if (py >= 0 && (vy - py) < textHeight - 1) {
Chris@40 2715 if (m_frequencyScale == LinearFrequencyScale) {
Chris@40 2716 paint.drawLine(w - tickw, h - vy, w, h - vy);
Chris@40 2717 }
Chris@0 2718 continue;
Chris@0 2719 }
Chris@0 2720
Chris@0 2721 QString text = QString("%1").arg(freq);
Chris@40 2722 if (bin == 1) text = QString("%1Hz").arg(freq); // bin 0 is DC
Chris@40 2723 paint.drawLine(cw + 7, h - vy, w - pkw - 1, h - vy);
Chris@0 2724
Chris@0 2725 if (h - vy - textHeight >= -2) {
Chris@40 2726 int tx = w - 3 - paint.fontMetrics().width(text) - std::max(tickw, pkw);
Chris@0 2727 paint.drawText(tx, h - vy + toff, text);
Chris@0 2728 }
Chris@0 2729
Chris@0 2730 py = vy;
Chris@0 2731 }
Chris@40 2732
Chris@40 2733 if (m_frequencyScale == LogFrequencyScale) {
Chris@40 2734
Chris@40 2735 paint.drawLine(w - pkw - 1, 0, w - pkw - 1, h);
Chris@40 2736
Chris@110 2737 int sr = m_model->getSampleRate();
Chris@40 2738 float minf = getEffectiveMinFrequency();
Chris@40 2739 float maxf = getEffectiveMaxFrequency();
Chris@40 2740
Chris@122 2741 int py = h, ppy = h;
Chris@40 2742 paint.setBrush(paint.pen().color());
Chris@40 2743
Chris@40 2744 for (int i = 0; i < 128; ++i) {
Chris@40 2745
Chris@40 2746 float f = Pitch::getFrequencyForPitch(i);
Chris@44 2747 int y = lrintf(v->getYForFrequency(f, minf, maxf, true));
Chris@122 2748
Chris@122 2749 if (y < -2) break;
Chris@122 2750 if (y > h + 2) {
Chris@122 2751 continue;
Chris@122 2752 }
Chris@122 2753
Chris@40 2754 int n = (i % 12);
Chris@122 2755
Chris@122 2756 if (n == 1) {
Chris@122 2757 // C# -- fill the C from here
Chris@122 2758 if (ppy - y > 2) {
Chris@122 2759 paint.fillRect(w - pkw,
Chris@122 2760 // y - (py - y) / 2 - (py - y) / 4,
Chris@122 2761 y,
Chris@122 2762 pkw,
Chris@122 2763 (py + ppy) / 2 - y,
Chris@122 2764 // py - y + 1,
Chris@122 2765 Qt::gray);
Chris@122 2766 }
Chris@122 2767 }
Chris@122 2768
Chris@40 2769 if (n == 1 || n == 3 || n == 6 || n == 8 || n == 10) {
Chris@40 2770 // black notes
Chris@40 2771 paint.drawLine(w - pkw, y, w, y);
Chris@41 2772 int rh = ((py - y) / 4) * 2;
Chris@41 2773 if (rh < 2) rh = 2;
Chris@41 2774 paint.drawRect(w - pkw, y - (py-y)/4, pkw/2, rh);
Chris@40 2775 } else if (n == 0 || n == 5) {
Chris@122 2776 // C, F
Chris@40 2777 if (py < h) {
Chris@40 2778 paint.drawLine(w - pkw, (y + py) / 2, w, (y + py) / 2);
Chris@40 2779 }
Chris@40 2780 }
Chris@40 2781
Chris@122 2782 ppy = py;
Chris@40 2783 py = y;
Chris@40 2784 }
Chris@40 2785 }
Chris@0 2786 }
Chris@0 2787
Chris@187 2788 class SpectrogramRangeMapper : public RangeMapper
Chris@187 2789 {
Chris@187 2790 public:
Chris@187 2791 SpectrogramRangeMapper(int sr, int fftsize) :
Chris@187 2792 // m_dist((float(sr) / 2) - (float(sr) / fftsize)),
Chris@187 2793 m_dist(float(sr) / 2),
Chris@187 2794 m_s2(sqrtf(sqrtf(2))) { }
Chris@187 2795 ~SpectrogramRangeMapper() { }
Chris@187 2796
Chris@187 2797 virtual int getPositionForValue(float value) const {
Chris@187 2798
Chris@187 2799 float dist = m_dist;
Chris@187 2800
Chris@187 2801 int n = 0;
Chris@187 2802 int discard = 0;
Chris@187 2803
Chris@187 2804 while (dist > (value + 0.00001) && dist > 0.1f) {
Chris@187 2805 dist /= m_s2;
Chris@187 2806 ++n;
Chris@187 2807 }
Chris@187 2808
Chris@187 2809 return n;
Chris@187 2810 }
Chris@187 2811
Chris@187 2812 virtual float getValueForPosition(int position) const {
Chris@187 2813
Chris@187 2814 // Vertical zoom step 0 shows the entire range from DC ->
Chris@187 2815 // Nyquist frequency. Step 1 shows 2^(1/4) of the range of
Chris@187 2816 // step 0, and so on until the visible range is smaller than
Chris@187 2817 // the frequency step between bins at the current fft size.
Chris@187 2818
Chris@187 2819 float dist = m_dist;
Chris@187 2820
Chris@187 2821 int n = 0;
Chris@187 2822 while (n < position) {
Chris@187 2823 dist /= m_s2;
Chris@187 2824 ++n;
Chris@187 2825 }
Chris@187 2826
Chris@187 2827 return dist;
Chris@187 2828 }
Chris@187 2829
Chris@187 2830 virtual QString getUnit() const { return "Hz"; }
Chris@187 2831
Chris@187 2832 protected:
Chris@187 2833 float m_dist;
Chris@187 2834 float m_s2;
Chris@187 2835 };
Chris@187 2836
Chris@133 2837 int
Chris@133 2838 SpectrogramLayer::getVerticalZoomSteps(int &defaultStep) const
Chris@133 2839 {
Chris@135 2840 if (!m_model) return 0;
Chris@187 2841
Chris@187 2842 int sr = m_model->getSampleRate();
Chris@187 2843
Chris@187 2844 SpectrogramRangeMapper mapper(sr, m_fftSize);
Chris@187 2845
Chris@187 2846 // int maxStep = mapper.getPositionForValue((float(sr) / m_fftSize) + 0.001);
Chris@187 2847 int maxStep = mapper.getPositionForValue(0);
Chris@187 2848 int minStep = mapper.getPositionForValue(float(sr) / 2);
Chris@135 2849
Chris@187 2850 defaultStep = mapper.getPositionForValue(m_initialMaxFrequency) - minStep;
Chris@187 2851
Chris@187 2852 std::cerr << "SpectrogramLayer::getVerticalZoomSteps: " << maxStep - minStep << " (" << maxStep <<"-" << minStep << "), default is " << defaultStep << " (from initial max freq " << m_initialMaxFrequency << ")" << std::endl;
Chris@187 2853
Chris@187 2854 return maxStep - minStep;
Chris@133 2855 }
Chris@133 2856
Chris@133 2857 int
Chris@133 2858 SpectrogramLayer::getCurrentVerticalZoomStep() const
Chris@133 2859 {
Chris@133 2860 if (!m_model) return 0;
Chris@133 2861
Chris@133 2862 float dmin, dmax;
Chris@133 2863 getDisplayExtents(dmin, dmax);
Chris@133 2864
Chris@187 2865 SpectrogramRangeMapper mapper(m_model->getSampleRate(), m_fftSize);
Chris@187 2866 int n = mapper.getPositionForValue(dmax - dmin);
Chris@187 2867 std::cerr << "SpectrogramLayer::getCurrentVerticalZoomStep: " << n << std::endl;
Chris@133 2868 return n;
Chris@133 2869 }
Chris@133 2870
Chris@133 2871 void
Chris@133 2872 SpectrogramLayer::setVerticalZoomStep(int step)
Chris@133 2873 {
Chris@133 2874 //!!! does not do the right thing for log scale
Chris@133 2875
Chris@187 2876 if (!m_model) return;
Chris@187 2877
Chris@133 2878 float dmin, dmax;
Chris@133 2879 getDisplayExtents(dmin, dmax);
Chris@133 2880
Chris@133 2881 int sr = m_model->getSampleRate();
Chris@187 2882 SpectrogramRangeMapper mapper(sr, m_fftSize);
Chris@187 2883 float ddist = mapper.getValueForPosition(step);
Chris@133 2884
Chris@133 2885 float dmid = (dmax + dmin) / 2;
Chris@133 2886 float newmin = dmid - ddist / 2;
Chris@133 2887 float newmax = dmid + ddist / 2;
Chris@187 2888
Chris@187 2889 float mmin, mmax;
Chris@187 2890 mmin = 0;
Chris@187 2891 mmax = float(sr) / 2;
Chris@133 2892
Chris@187 2893 if (newmin < mmin) {
Chris@187 2894 newmax += (mmin - newmin);
Chris@187 2895 newmin = mmin;
Chris@187 2896 }
Chris@187 2897 if (newmax > mmax) {
Chris@187 2898 newmax = mmax;
Chris@187 2899 }
Chris@133 2900
Chris@187 2901 std::cerr << "SpectrogramLayer::setVerticalZoomStep: " << step << ": " << newmin << " -> " << newmax << " (range " << ddist << ")" << std::endl;
Chris@187 2902
Chris@187 2903 setMinFrequency(int(newmin));
Chris@187 2904 setMaxFrequency(int(newmax));
Chris@187 2905 }
Chris@187 2906
Chris@187 2907 RangeMapper *
Chris@187 2908 SpectrogramLayer::getNewVerticalZoomRangeMapper() const
Chris@187 2909 {
Chris@187 2910 if (!m_model) return 0;
Chris@187 2911 return new SpectrogramRangeMapper(m_model->getSampleRate(), m_fftSize);
Chris@133 2912 }
Chris@133 2913
Chris@6 2914 QString
Chris@6 2915 SpectrogramLayer::toXmlString(QString indent, QString extraAttributes) const
Chris@6 2916 {
Chris@6 2917 QString s;
Chris@6 2918
Chris@6 2919 s += QString("channel=\"%1\" "
Chris@6 2920 "windowSize=\"%2\" "
Chris@153 2921 "windowHopLevel=\"%3\" "
Chris@153 2922 "gain=\"%4\" "
Chris@153 2923 "threshold=\"%5\" ")
Chris@6 2924 .arg(m_channel)
Chris@6 2925 .arg(m_windowSize)
Chris@97 2926 .arg(m_windowHopLevel)
Chris@37 2927 .arg(m_gain)
Chris@37 2928 .arg(m_threshold);
Chris@37 2929
Chris@37 2930 s += QString("minFrequency=\"%1\" "
Chris@37 2931 "maxFrequency=\"%2\" "
Chris@37 2932 "colourScale=\"%3\" "
Chris@37 2933 "colourScheme=\"%4\" "
Chris@37 2934 "colourRotation=\"%5\" "
Chris@37 2935 "frequencyScale=\"%6\" "
Chris@37 2936 "binDisplay=\"%7\" "
Chris@153 2937 "normalizeColumns=\"%8\" "
Chris@153 2938 "normalizeVisibleArea=\"%9\"")
Chris@37 2939 .arg(m_minFrequency)
Chris@6 2940 .arg(m_maxFrequency)
Chris@6 2941 .arg(m_colourScale)
Chris@6 2942 .arg(m_colourScheme)
Chris@37 2943 .arg(m_colourRotation)
Chris@35 2944 .arg(m_frequencyScale)
Chris@37 2945 .arg(m_binDisplay)
Chris@153 2946 .arg(m_normalizeColumns ? "true" : "false")
Chris@153 2947 .arg(m_normalizeVisibleArea ? "true" : "false");
Chris@6 2948
Chris@6 2949 return Layer::toXmlString(indent, extraAttributes + " " + s);
Chris@6 2950 }
Chris@6 2951
Chris@11 2952 void
Chris@11 2953 SpectrogramLayer::setProperties(const QXmlAttributes &attributes)
Chris@11 2954 {
Chris@11 2955 bool ok = false;
Chris@11 2956
Chris@11 2957 int channel = attributes.value("channel").toInt(&ok);
Chris@11 2958 if (ok) setChannel(channel);
Chris@11 2959
Chris@11 2960 size_t windowSize = attributes.value("windowSize").toUInt(&ok);
Chris@11 2961 if (ok) setWindowSize(windowSize);
Chris@11 2962
Chris@97 2963 size_t windowHopLevel = attributes.value("windowHopLevel").toUInt(&ok);
Chris@97 2964 if (ok) setWindowHopLevel(windowHopLevel);
Chris@97 2965 else {
Chris@97 2966 size_t windowOverlap = attributes.value("windowOverlap").toUInt(&ok);
Chris@97 2967 // a percentage value
Chris@97 2968 if (ok) {
Chris@97 2969 if (windowOverlap == 0) setWindowHopLevel(0);
Chris@97 2970 else if (windowOverlap == 25) setWindowHopLevel(1);
Chris@97 2971 else if (windowOverlap == 50) setWindowHopLevel(2);
Chris@97 2972 else if (windowOverlap == 75) setWindowHopLevel(3);
Chris@97 2973 else if (windowOverlap == 90) setWindowHopLevel(4);
Chris@97 2974 }
Chris@97 2975 }
Chris@11 2976
Chris@11 2977 float gain = attributes.value("gain").toFloat(&ok);
Chris@11 2978 if (ok) setGain(gain);
Chris@11 2979
Chris@37 2980 float threshold = attributes.value("threshold").toFloat(&ok);
Chris@37 2981 if (ok) setThreshold(threshold);
Chris@37 2982
Chris@37 2983 size_t minFrequency = attributes.value("minFrequency").toUInt(&ok);
Chris@187 2984 if (ok) {
Chris@187 2985 std::cerr << "SpectrogramLayer::setProperties: setting min freq to " << minFrequency << std::endl;
Chris@187 2986 setMinFrequency(minFrequency);
Chris@187 2987 }
Chris@37 2988
Chris@11 2989 size_t maxFrequency = attributes.value("maxFrequency").toUInt(&ok);
Chris@187 2990 if (ok) {
Chris@187 2991 std::cerr << "SpectrogramLayer::setProperties: setting max freq to " << maxFrequency << std::endl;
Chris@187 2992 setMaxFrequency(maxFrequency);
Chris@187 2993 }
Chris@11 2994
Chris@11 2995 ColourScale colourScale = (ColourScale)
Chris@11 2996 attributes.value("colourScale").toInt(&ok);
Chris@11 2997 if (ok) setColourScale(colourScale);
Chris@11 2998
Chris@196 2999 int colourScheme = attributes.value("colourScheme").toInt(&ok);
Chris@11 3000 if (ok) setColourScheme(colourScheme);
Chris@11 3001
Chris@37 3002 int colourRotation = attributes.value("colourRotation").toInt(&ok);
Chris@37 3003 if (ok) setColourRotation(colourRotation);
Chris@37 3004
Chris@11 3005 FrequencyScale frequencyScale = (FrequencyScale)
Chris@11 3006 attributes.value("frequencyScale").toInt(&ok);
Chris@11 3007 if (ok) setFrequencyScale(frequencyScale);
Chris@35 3008
Chris@37 3009 BinDisplay binDisplay = (BinDisplay)
Chris@37 3010 attributes.value("binDisplay").toInt(&ok);
Chris@37 3011 if (ok) setBinDisplay(binDisplay);
Chris@36 3012
Chris@36 3013 bool normalizeColumns =
Chris@36 3014 (attributes.value("normalizeColumns").trimmed() == "true");
Chris@36 3015 setNormalizeColumns(normalizeColumns);
Chris@153 3016
Chris@153 3017 bool normalizeVisibleArea =
Chris@153 3018 (attributes.value("normalizeVisibleArea").trimmed() == "true");
Chris@153 3019 setNormalizeVisibleArea(normalizeVisibleArea);
Chris@11 3020 }
Chris@11 3021