annotate layer/SpectrogramLayer.cpp @ 216:34bbbcb3c01f sv1-1.0pre1

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