annotate layer/SpectrogramLayer.cpp @ 320:984c1975f1ff

* Some tidying up to handling of alignment; add alignment status label to pane; ensure alignment when dragging with mouse as well as when playing
author Chris Cannam
date Thu, 25 Oct 2007 14:32:23 +0000
parents c0b9eec70639
children 4f4f38a11cd2
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@253 25 #include "base/LogRange.h"
Chris@283 26 #include "base/CommandHistory.h"
Chris@285 27 #include "base/ColourMapper.h"
Chris@283 28 #include "ImageRegionFinder.h"
Chris@0 29
Chris@0 30 #include <QPainter>
Chris@0 31 #include <QImage>
Chris@0 32 #include <QPixmap>
Chris@0 33 #include <QRect>
Chris@0 34 #include <QTimer>
Chris@92 35 #include <QApplication>
Chris@178 36 #include <QMessageBox>
Chris@283 37 #include <QMouseEvent>
Chris@316 38 #include <QTextStream>
Chris@0 39
Chris@0 40 #include <iostream>
Chris@0 41
Chris@0 42 #include <cassert>
Chris@0 43 #include <cmath>
Chris@0 44
Chris@174 45 //#define DEBUG_SPECTROGRAM_REPAINT 1
Chris@0 46
Chris@44 47 SpectrogramLayer::SpectrogramLayer(Configuration config) :
Chris@0 48 m_model(0),
Chris@0 49 m_channel(0),
Chris@0 50 m_windowSize(1024),
Chris@0 51 m_windowType(HanningWindow),
Chris@97 52 m_windowHopLevel(2),
Chris@109 53 m_zeroPadLevel(0),
Chris@107 54 m_fftSize(1024),
Chris@0 55 m_gain(1.0),
Chris@215 56 m_initialGain(1.0),
Chris@37 57 m_threshold(0.0),
Chris@215 58 m_initialThreshold(0.0),
Chris@9 59 m_colourRotation(0),
Chris@215 60 m_initialRotation(0),
Chris@119 61 m_minFrequency(10),
Chris@0 62 m_maxFrequency(8000),
Chris@135 63 m_initialMaxFrequency(8000),
Chris@0 64 m_colourScale(dBColourScale),
Chris@197 65 m_colourMap(0),
Chris@0 66 m_frequencyScale(LinearFrequencyScale),
Chris@37 67 m_binDisplay(AllBins),
Chris@36 68 m_normalizeColumns(false),
Chris@120 69 m_normalizeVisibleArea(false),
Chris@133 70 m_lastEmittedZoomStep(-1),
Chris@215 71 m_lastPaintBlockWidth(0),
Chris@0 72 m_updateTimer(0),
Chris@44 73 m_candidateFillStartFrame(0),
Chris@193 74 m_exiting(false),
Chris@193 75 m_sliceableModel(0)
Chris@0 76 {
Chris@215 77 if (config == FullRangeDb) {
Chris@215 78 m_initialMaxFrequency = 0;
Chris@215 79 setMaxFrequency(0);
Chris@215 80 } else if (config == MelodicRange) {
Chris@0 81 setWindowSize(8192);
Chris@97 82 setWindowHopLevel(4);
Chris@215 83 m_initialMaxFrequency = 1500;
Chris@215 84 setMaxFrequency(1500);
Chris@215 85 setMinFrequency(40);
Chris@0 86 setColourScale(LinearColourScale);
Chris@215 87 setColourMap(ColourMapper::Sunset);
Chris@215 88 setFrequencyScale(LogFrequencyScale);
Chris@224 89 // setGain(20);
Chris@37 90 } else if (config == MelodicPeaks) {
Chris@37 91 setWindowSize(4096);
Chris@97 92 setWindowHopLevel(5);
Chris@135 93 m_initialMaxFrequency = 2000;
Chris@40 94 setMaxFrequency(2000);
Chris@37 95 setMinFrequency(40);
Chris@37 96 setFrequencyScale(LogFrequencyScale);
Chris@215 97 setColourScale(LinearColourScale);
Chris@37 98 setBinDisplay(PeakFrequencies);
Chris@37 99 setNormalizeColumns(true);
Chris@0 100 }
Chris@110 101
Chris@122 102 Preferences *prefs = Preferences::getInstance();
Chris@122 103 connect(prefs, SIGNAL(propertyChanged(PropertyContainer::PropertyName)),
Chris@122 104 this, SLOT(preferenceChanged(PropertyContainer::PropertyName)));
Chris@122 105 setWindowType(prefs->getWindowType());
Chris@122 106
Chris@197 107 initialisePalette();
Chris@0 108 }
Chris@0 109
Chris@0 110 SpectrogramLayer::~SpectrogramLayer()
Chris@0 111 {
Chris@0 112 delete m_updateTimer;
Chris@0 113 m_updateTimer = 0;
Chris@0 114
Chris@130 115 invalidateFFTModels();
Chris@0 116 }
Chris@0 117
Chris@0 118 void
Chris@0 119 SpectrogramLayer::setModel(const DenseTimeValueModel *model)
Chris@0 120 {
Chris@101 121 // std::cerr << "SpectrogramLayer(" << this << "): setModel(" << model << ")" << std::endl;
Chris@34 122
Chris@110 123 if (model == m_model) return;
Chris@110 124
Chris@0 125 m_model = model;
Chris@130 126 invalidateFFTModels();
Chris@0 127
Chris@0 128 if (!m_model || !m_model->isOK()) return;
Chris@0 129
Chris@320 130 connectSignals(m_model);
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@221 579 if (name == "Spectrogram Smoothing") {
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@248 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@248 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@130 963 const FFTModel *model = i->second.first;
Chris@115 964 size_t lastFill = i->second.second;
Chris@115 965
Chris@130 966 if (model) {
Chris@130 967
Chris@130 968 size_t fill = model->getFillExtent();
Chris@115 969
Chris@0 970 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@130 971 std::cerr << "SpectrogramLayer::fillTimerTimedOut: extent for " << model << ": " << fill << ", last " << lastFill << ", total " << m_model->getEndFrame() << std::endl;
Chris@0 972 #endif
Chris@115 973
Chris@115 974 if (fill >= lastFill) {
Chris@115 975 if (fill >= m_model->getEndFrame() && lastFill > 0) {
Chris@0 976 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@115 977 std::cerr << "complete!" << std::endl;
Chris@0 978 #endif
Chris@115 979 invalidatePixmapCaches();
Chris@184 980 i->second.second = -1;
Chris@115 981 emit modelChanged();
Chris@115 982
Chris@115 983 } else if (fill > lastFill) {
Chris@0 984 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@115 985 std::cerr << "SpectrogramLayer: emitting modelChanged("
Chris@115 986 << lastFill << "," << fill << ")" << std::endl;
Chris@0 987 #endif
Chris@115 988 invalidatePixmapCaches(lastFill, fill);
Chris@184 989 i->second.second = fill;
Chris@115 990 emit modelChanged(lastFill, fill);
Chris@115 991 }
Chris@115 992 } else {
Chris@0 993 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@115 994 std::cerr << "SpectrogramLayer: going backwards, emitting modelChanged("
Chris@115 995 << m_model->getStartFrame() << "," << m_model->getEndFrame() << ")" << std::endl;
Chris@0 996 #endif
Chris@115 997 invalidatePixmapCaches();
Chris@184 998 i->second.second = fill;
Chris@115 999 emit modelChanged(m_model->getStartFrame(), m_model->getEndFrame());
Chris@115 1000 }
Chris@115 1001
Chris@115 1002 if (i->second.second >= 0) {
Chris@115 1003 allDone = false;
Chris@115 1004 }
Chris@115 1005 }
Chris@0 1006 }
Chris@115 1007
Chris@115 1008 if (allDone) {
Chris@115 1009 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@115 1010 std::cerr << "SpectrogramLayer: all complete!" << std::endl;
Chris@115 1011 #endif
Chris@115 1012 delete m_updateTimer;
Chris@115 1013 m_updateTimer = 0;
Chris@115 1014 }
Chris@0 1015 }
Chris@0 1016
Chris@224 1017 bool
Chris@224 1018 SpectrogramLayer::hasLightBackground() const
Chris@224 1019 {
Chris@287 1020 return ColourMapper(m_colourMap, 1.f, 255.f).hasLightBackground();
Chris@224 1021 }
Chris@224 1022
Chris@0 1023 void
Chris@197 1024 SpectrogramLayer::initialisePalette()
Chris@0 1025 {
Chris@10 1026 int formerRotation = m_colourRotation;
Chris@10 1027
Chris@197 1028 if (m_colourMap == (int)ColourMapper::BlackOnWhite) {
Chris@197 1029 m_palette.setColour(NO_VALUE, Qt::white);
Chris@38 1030 } else {
Chris@197 1031 m_palette.setColour(NO_VALUE, Qt::black);
Chris@38 1032 }
Chris@0 1033
Chris@197 1034 ColourMapper mapper(m_colourMap, 1.f, 255.f);
Chris@196 1035
Chris@0 1036 for (int pixel = 1; pixel < 256; ++pixel) {
Chris@197 1037 m_palette.setColour(pixel, mapper.map(pixel));
Chris@0 1038 }
Chris@9 1039
Chris@196 1040 m_crosshairColour = mapper.getContrastingColour();
Chris@196 1041
Chris@9 1042 m_colourRotation = 0;
Chris@197 1043 rotatePalette(m_colourRotation - formerRotation);
Chris@10 1044 m_colourRotation = formerRotation;
Chris@9 1045 }
Chris@9 1046
Chris@9 1047 void
Chris@197 1048 SpectrogramLayer::rotatePalette(int distance)
Chris@9 1049 {
Chris@31 1050 QColor newPixels[256];
Chris@9 1051
Chris@197 1052 newPixels[NO_VALUE] = m_palette.getColour(NO_VALUE);
Chris@9 1053
Chris@9 1054 for (int pixel = 1; pixel < 256; ++pixel) {
Chris@9 1055 int target = pixel + distance;
Chris@9 1056 while (target < 1) target += 255;
Chris@9 1057 while (target > 255) target -= 255;
Chris@197 1058 newPixels[target] = m_palette.getColour(pixel);
Chris@9 1059 }
Chris@9 1060
Chris@9 1061 for (int pixel = 0; pixel < 256; ++pixel) {
Chris@197 1062 m_palette.setColour(pixel, newPixels[pixel]);
Chris@9 1063 }
Chris@0 1064 }
Chris@0 1065
Chris@38 1066 unsigned char
Chris@119 1067 SpectrogramLayer::getDisplayValue(View *v, float input) const
Chris@38 1068 {
Chris@38 1069 int value;
Chris@37 1070
Chris@120 1071 float min = 0.f;
Chris@120 1072 float max = 1.f;
Chris@120 1073
Chris@120 1074 if (m_normalizeVisibleArea) {
Chris@120 1075 min = m_viewMags[v].getMin();
Chris@120 1076 max = m_viewMags[v].getMax();
Chris@120 1077 } else if (!m_normalizeColumns) {
Chris@224 1078 if (m_colourScale == LinearColourScale //||
Chris@224 1079 // m_colourScale == MeterColourScale) {
Chris@224 1080 ) {
Chris@224 1081 max = 0.1f;
Chris@120 1082 }
Chris@120 1083 }
Chris@120 1084
Chris@119 1085 float thresh = -80.f;
Chris@119 1086
Chris@119 1087 if (max == 0.f) max = 1.f;
Chris@119 1088 if (max == min) min = max - 0.0001f;
Chris@119 1089
Chris@40 1090 switch (m_colourScale) {
Chris@40 1091
Chris@40 1092 default:
Chris@40 1093 case LinearColourScale:
Chris@119 1094 value = int(((input - min) / (max - min)) * 255.f) + 1;
Chris@40 1095 break;
Chris@40 1096
Chris@40 1097 case MeterColourScale:
Chris@210 1098 value = AudioLevel::multiplier_to_preview
Chris@210 1099 ((input - min) / (max - min), 254) + 1;
Chris@40 1100 break;
Chris@119 1101
Chris@210 1102 case dBSquaredColourScale:
Chris@215 1103 input = ((input - min) * (input - min)) / ((max - min) * (max - min));
Chris@133 1104 if (input > 0.f) {
Chris@133 1105 input = 10.f * log10f(input);
Chris@133 1106 } else {
Chris@133 1107 input = thresh;
Chris@133 1108 }
Chris@119 1109 if (min > 0.f) {
Chris@119 1110 thresh = 10.f * log10f(min * min);
Chris@119 1111 if (thresh < -80.f) thresh = -80.f;
Chris@119 1112 }
Chris@119 1113 input = (input - thresh) / (-thresh);
Chris@119 1114 if (input < 0.f) input = 0.f;
Chris@119 1115 if (input > 1.f) input = 1.f;
Chris@119 1116 value = int(input * 255.f) + 1;
Chris@119 1117 break;
Chris@40 1118
Chris@215 1119 case dBColourScale:
Chris@215 1120 //!!! experiment with normalizing the visible area this way.
Chris@215 1121 //In any case, we need to have some indication of what the dB
Chris@215 1122 //scale is relative to.
Chris@215 1123 input = (input - min) / (max - min);
Chris@215 1124 if (input > 0.f) {
Chris@215 1125 input = 10.f * log10f(input);
Chris@215 1126 } else {
Chris@215 1127 input = thresh;
Chris@215 1128 }
Chris@215 1129 if (min > 0.f) {
Chris@215 1130 thresh = 10.f * log10f(min);
Chris@215 1131 if (thresh < -80.f) thresh = -80.f;
Chris@215 1132 }
Chris@215 1133 input = (input - thresh) / (-thresh);
Chris@215 1134 if (input < 0.f) input = 0.f;
Chris@215 1135 if (input > 1.f) input = 1.f;
Chris@215 1136 value = int(input * 255.f) + 1;
Chris@215 1137 break;
Chris@215 1138
Chris@40 1139 case PhaseColourScale:
Chris@40 1140 value = int((input * 127.0 / M_PI) + 128);
Chris@40 1141 break;
Chris@0 1142 }
Chris@210 1143
Chris@38 1144 if (value > UCHAR_MAX) value = UCHAR_MAX;
Chris@38 1145 if (value < 0) value = 0;
Chris@38 1146 return value;
Chris@0 1147 }
Chris@0 1148
Chris@40 1149 float
Chris@40 1150 SpectrogramLayer::getInputForDisplayValue(unsigned char uc) const
Chris@40 1151 {
Chris@153 1152 //!!! unused
Chris@153 1153
Chris@40 1154 int value = uc;
Chris@40 1155 float input;
Chris@40 1156
Chris@120 1157 //!!! incorrect for normalizing visible area (and also out of date)
Chris@120 1158
Chris@40 1159 switch (m_colourScale) {
Chris@40 1160
Chris@40 1161 default:
Chris@40 1162 case LinearColourScale:
Chris@40 1163 input = float(value - 1) / 255.0 / (m_normalizeColumns ? 1 : 50);
Chris@40 1164 break;
Chris@40 1165
Chris@40 1166 case MeterColourScale:
Chris@40 1167 input = AudioLevel::preview_to_multiplier(value - 1, 255)
Chris@40 1168 / (m_normalizeColumns ? 1.0 : 50.0);
Chris@40 1169 break;
Chris@40 1170
Chris@215 1171 case dBSquaredColourScale:
Chris@40 1172 input = float(value - 1) / 255.0;
Chris@40 1173 input = (input * 80.0) - 80.0;
Chris@40 1174 input = powf(10.0, input) / 20.0;
Chris@40 1175 value = int(input);
Chris@40 1176 break;
Chris@40 1177
Chris@215 1178 case dBColourScale:
Chris@119 1179 input = float(value - 1) / 255.0;
Chris@119 1180 input = (input * 80.0) - 80.0;
Chris@119 1181 input = powf(10.0, input) / 20.0;
Chris@119 1182 value = int(input);
Chris@119 1183 break;
Chris@119 1184
Chris@40 1185 case PhaseColourScale:
Chris@40 1186 input = float(value - 128) * M_PI / 127.0;
Chris@40 1187 break;
Chris@40 1188 }
Chris@40 1189
Chris@40 1190 return input;
Chris@40 1191 }
Chris@40 1192
Chris@40 1193 float
Chris@40 1194 SpectrogramLayer::getEffectiveMinFrequency() const
Chris@40 1195 {
Chris@40 1196 int sr = m_model->getSampleRate();
Chris@107 1197 float minf = float(sr) / m_fftSize;
Chris@40 1198
Chris@40 1199 if (m_minFrequency > 0.0) {
Chris@107 1200 size_t minbin = size_t((double(m_minFrequency) * m_fftSize) / sr + 0.01);
Chris@40 1201 if (minbin < 1) minbin = 1;
Chris@107 1202 minf = minbin * sr / m_fftSize;
Chris@40 1203 }
Chris@40 1204
Chris@40 1205 return minf;
Chris@40 1206 }
Chris@40 1207
Chris@40 1208 float
Chris@40 1209 SpectrogramLayer::getEffectiveMaxFrequency() const
Chris@40 1210 {
Chris@40 1211 int sr = m_model->getSampleRate();
Chris@40 1212 float maxf = float(sr) / 2;
Chris@40 1213
Chris@40 1214 if (m_maxFrequency > 0.0) {
Chris@107 1215 size_t maxbin = size_t((double(m_maxFrequency) * m_fftSize) / sr + 0.1);
Chris@107 1216 if (maxbin > m_fftSize / 2) maxbin = m_fftSize / 2;
Chris@107 1217 maxf = maxbin * sr / m_fftSize;
Chris@40 1218 }
Chris@40 1219
Chris@40 1220 return maxf;
Chris@40 1221 }
Chris@40 1222
Chris@0 1223 bool
Chris@44 1224 SpectrogramLayer::getYBinRange(View *v, int y, float &q0, float &q1) const
Chris@0 1225 {
Chris@44 1226 int h = v->height();
Chris@0 1227 if (y < 0 || y >= h) return false;
Chris@0 1228
Chris@38 1229 int sr = m_model->getSampleRate();
Chris@40 1230 float minf = getEffectiveMinFrequency();
Chris@40 1231 float maxf = getEffectiveMaxFrequency();
Chris@0 1232
Chris@38 1233 bool logarithmic = (m_frequencyScale == LogFrequencyScale);
Chris@38 1234
Chris@130 1235 //!!! wrong for smoothing -- wrong fft size for fft model
Chris@114 1236
Chris@44 1237 q0 = v->getFrequencyForY(y, minf, maxf, logarithmic);
Chris@44 1238 q1 = v->getFrequencyForY(y - 1, minf, maxf, logarithmic);
Chris@38 1239
Chris@38 1240 // Now map these on to actual bins
Chris@38 1241
Chris@107 1242 int b0 = int((q0 * m_fftSize) / sr);
Chris@107 1243 int b1 = int((q1 * m_fftSize) / sr);
Chris@0 1244
Chris@40 1245 //!!! this is supposed to return fractions-of-bins, as it were, hence the floats
Chris@38 1246 q0 = b0;
Chris@38 1247 q1 = b1;
Chris@38 1248
Chris@107 1249 // q0 = (b0 * sr) / m_fftSize;
Chris@107 1250 // q1 = (b1 * sr) / m_fftSize;
Chris@0 1251
Chris@0 1252 return true;
Chris@0 1253 }
Chris@38 1254
Chris@0 1255 bool
Chris@44 1256 SpectrogramLayer::getXBinRange(View *v, int x, float &s0, float &s1) const
Chris@0 1257 {
Chris@21 1258 size_t modelStart = m_model->getStartFrame();
Chris@21 1259 size_t modelEnd = m_model->getEndFrame();
Chris@0 1260
Chris@0 1261 // Each pixel column covers an exact range of sample frames:
Chris@44 1262 int f0 = v->getFrameForX(x) - modelStart;
Chris@44 1263 int f1 = v->getFrameForX(x + 1) - modelStart - 1;
Chris@20 1264
Chris@41 1265 if (f1 < int(modelStart) || f0 > int(modelEnd)) {
Chris@41 1266 return false;
Chris@41 1267 }
Chris@20 1268
Chris@0 1269 // And that range may be drawn from a possibly non-integral
Chris@0 1270 // range of spectrogram windows:
Chris@0 1271
Chris@0 1272 size_t windowIncrement = getWindowIncrement();
Chris@0 1273 s0 = float(f0) / windowIncrement;
Chris@0 1274 s1 = float(f1) / windowIncrement;
Chris@0 1275
Chris@0 1276 return true;
Chris@0 1277 }
Chris@0 1278
Chris@0 1279 bool
Chris@44 1280 SpectrogramLayer::getXBinSourceRange(View *v, int x, RealTime &min, RealTime &max) const
Chris@0 1281 {
Chris@0 1282 float s0 = 0, s1 = 0;
Chris@44 1283 if (!getXBinRange(v, x, s0, s1)) return false;
Chris@0 1284
Chris@0 1285 int s0i = int(s0 + 0.001);
Chris@0 1286 int s1i = int(s1);
Chris@0 1287
Chris@0 1288 int windowIncrement = getWindowIncrement();
Chris@0 1289 int w0 = s0i * windowIncrement - (m_windowSize - windowIncrement)/2;
Chris@0 1290 int w1 = s1i * windowIncrement + windowIncrement +
Chris@0 1291 (m_windowSize - windowIncrement)/2 - 1;
Chris@0 1292
Chris@0 1293 min = RealTime::frame2RealTime(w0, m_model->getSampleRate());
Chris@0 1294 max = RealTime::frame2RealTime(w1, m_model->getSampleRate());
Chris@0 1295 return true;
Chris@0 1296 }
Chris@0 1297
Chris@0 1298 bool
Chris@44 1299 SpectrogramLayer::getYBinSourceRange(View *v, int y, float &freqMin, float &freqMax)
Chris@0 1300 const
Chris@0 1301 {
Chris@0 1302 float q0 = 0, q1 = 0;
Chris@44 1303 if (!getYBinRange(v, y, q0, q1)) return false;
Chris@0 1304
Chris@0 1305 int q0i = int(q0 + 0.001);
Chris@0 1306 int q1i = int(q1);
Chris@0 1307
Chris@0 1308 int sr = m_model->getSampleRate();
Chris@0 1309
Chris@0 1310 for (int q = q0i; q <= q1i; ++q) {
Chris@121 1311 if (q == q0i) freqMin = (sr * q) / m_fftSize;
Chris@121 1312 if (q == q1i) freqMax = (sr * (q+1)) / m_fftSize;
Chris@0 1313 }
Chris@0 1314 return true;
Chris@0 1315 }
Chris@35 1316
Chris@35 1317 bool
Chris@44 1318 SpectrogramLayer::getAdjustedYBinSourceRange(View *v, int x, int y,
Chris@35 1319 float &freqMin, float &freqMax,
Chris@35 1320 float &adjFreqMin, float &adjFreqMax)
Chris@35 1321 const
Chris@35 1322 {
Chris@277 1323 if (!m_model || !m_model->isOK() || !m_model->isReady()) {
Chris@277 1324 return false;
Chris@277 1325 }
Chris@277 1326
Chris@130 1327 FFTModel *fft = getFFTModel(v);
Chris@114 1328 if (!fft) return false;
Chris@110 1329
Chris@35 1330 float s0 = 0, s1 = 0;
Chris@44 1331 if (!getXBinRange(v, x, s0, s1)) return false;
Chris@35 1332
Chris@35 1333 float q0 = 0, q1 = 0;
Chris@44 1334 if (!getYBinRange(v, y, q0, q1)) return false;
Chris@35 1335
Chris@35 1336 int s0i = int(s0 + 0.001);
Chris@35 1337 int s1i = int(s1);
Chris@35 1338
Chris@35 1339 int q0i = int(q0 + 0.001);
Chris@35 1340 int q1i = int(q1);
Chris@35 1341
Chris@35 1342 int sr = m_model->getSampleRate();
Chris@35 1343
Chris@38 1344 size_t windowSize = m_windowSize;
Chris@38 1345 size_t windowIncrement = getWindowIncrement();
Chris@38 1346
Chris@35 1347 bool haveAdj = false;
Chris@35 1348
Chris@37 1349 bool peaksOnly = (m_binDisplay == PeakBins ||
Chris@37 1350 m_binDisplay == PeakFrequencies);
Chris@37 1351
Chris@35 1352 for (int q = q0i; q <= q1i; ++q) {
Chris@35 1353
Chris@35 1354 for (int s = s0i; s <= s1i; ++s) {
Chris@35 1355
Chris@160 1356 if (!fft->isColumnAvailable(s)) continue;
Chris@117 1357
Chris@35 1358 float binfreq = (sr * q) / m_windowSize;
Chris@35 1359 if (q == q0i) freqMin = binfreq;
Chris@35 1360 if (q == q1i) freqMax = binfreq;
Chris@37 1361
Chris@114 1362 if (peaksOnly && !fft->isLocalPeak(s, q)) continue;
Chris@38 1363
Chris@253 1364 if (!fft->isOverThreshold(s, q, m_threshold * (m_fftSize/2))) continue;
Chris@38 1365
Chris@38 1366 float freq = binfreq;
Chris@38 1367 bool steady = false;
Chris@40 1368
Chris@114 1369 if (s < int(fft->getWidth()) - 1) {
Chris@38 1370
Chris@277 1371 fft->estimateStableFrequency(s, q, freq);
Chris@35 1372
Chris@38 1373 if (!haveAdj || freq < adjFreqMin) adjFreqMin = freq;
Chris@38 1374 if (!haveAdj || freq > adjFreqMax) adjFreqMax = freq;
Chris@35 1375
Chris@35 1376 haveAdj = true;
Chris@35 1377 }
Chris@35 1378 }
Chris@35 1379 }
Chris@35 1380
Chris@35 1381 if (!haveAdj) {
Chris@40 1382 adjFreqMin = adjFreqMax = 0.0;
Chris@35 1383 }
Chris@35 1384
Chris@35 1385 return haveAdj;
Chris@35 1386 }
Chris@0 1387
Chris@0 1388 bool
Chris@44 1389 SpectrogramLayer::getXYBinSourceRange(View *v, int x, int y,
Chris@38 1390 float &min, float &max,
Chris@38 1391 float &phaseMin, float &phaseMax) const
Chris@0 1392 {
Chris@277 1393 if (!m_model || !m_model->isOK() || !m_model->isReady()) {
Chris@277 1394 return false;
Chris@277 1395 }
Chris@277 1396
Chris@0 1397 float q0 = 0, q1 = 0;
Chris@44 1398 if (!getYBinRange(v, y, q0, q1)) return false;
Chris@0 1399
Chris@0 1400 float s0 = 0, s1 = 0;
Chris@44 1401 if (!getXBinRange(v, x, s0, s1)) return false;
Chris@0 1402
Chris@0 1403 int q0i = int(q0 + 0.001);
Chris@0 1404 int q1i = int(q1);
Chris@0 1405
Chris@0 1406 int s0i = int(s0 + 0.001);
Chris@0 1407 int s1i = int(s1);
Chris@0 1408
Chris@37 1409 bool rv = false;
Chris@37 1410
Chris@122 1411 size_t zp = getZeroPadLevel(v);
Chris@122 1412 q0i *= zp + 1;
Chris@122 1413 q1i *= zp + 1;
Chris@122 1414
Chris@130 1415 FFTModel *fft = getFFTModel(v);
Chris@0 1416
Chris@114 1417 if (fft) {
Chris@114 1418
Chris@114 1419 int cw = fft->getWidth();
Chris@114 1420 int ch = fft->getHeight();
Chris@0 1421
Chris@110 1422 min = 0.0;
Chris@110 1423 max = 0.0;
Chris@110 1424 phaseMin = 0.0;
Chris@110 1425 phaseMax = 0.0;
Chris@110 1426 bool have = false;
Chris@0 1427
Chris@110 1428 for (int q = q0i; q <= q1i; ++q) {
Chris@110 1429 for (int s = s0i; s <= s1i; ++s) {
Chris@110 1430 if (s >= 0 && q >= 0 && s < cw && q < ch) {
Chris@117 1431
Chris@160 1432 if (!fft->isColumnAvailable(s)) continue;
Chris@110 1433
Chris@110 1434 float value;
Chris@38 1435
Chris@114 1436 value = fft->getPhaseAt(s, q);
Chris@110 1437 if (!have || value < phaseMin) { phaseMin = value; }
Chris@110 1438 if (!have || value > phaseMax) { phaseMax = value; }
Chris@91 1439
Chris@252 1440 value = fft->getMagnitudeAt(s, q) / (m_fftSize/2);
Chris@110 1441 if (!have || value < min) { min = value; }
Chris@110 1442 if (!have || value > max) { max = value; }
Chris@110 1443
Chris@110 1444 have = true;
Chris@110 1445 }
Chris@110 1446 }
Chris@110 1447 }
Chris@110 1448
Chris@110 1449 if (have) {
Chris@110 1450 rv = true;
Chris@110 1451 }
Chris@0 1452 }
Chris@0 1453
Chris@37 1454 return rv;
Chris@0 1455 }
Chris@0 1456
Chris@114 1457 size_t
Chris@114 1458 SpectrogramLayer::getZeroPadLevel(const View *v) const
Chris@114 1459 {
Chris@114 1460 //!!! tidy all this stuff
Chris@114 1461
Chris@114 1462 if (m_binDisplay != AllBins) return 0;
Chris@221 1463
Chris@221 1464 Preferences::SpectrogramSmoothing smoothing =
Chris@221 1465 Preferences::getInstance()->getSpectrogramSmoothing();
Chris@221 1466
Chris@221 1467 if (smoothing == Preferences::NoSpectrogramSmoothing ||
Chris@221 1468 smoothing == Preferences::SpectrogramInterpolated) return 0;
Chris@221 1469
Chris@114 1470 if (m_frequencyScale == LogFrequencyScale) return 3;
Chris@114 1471
Chris@114 1472 int sr = m_model->getSampleRate();
Chris@114 1473
Chris@184 1474 size_t maxbin = m_fftSize / 2;
Chris@114 1475 if (m_maxFrequency > 0) {
Chris@184 1476 maxbin = int((double(m_maxFrequency) * m_fftSize) / sr + 0.1);
Chris@184 1477 if (maxbin > m_fftSize / 2) maxbin = m_fftSize / 2;
Chris@114 1478 }
Chris@114 1479
Chris@114 1480 size_t minbin = 1;
Chris@114 1481 if (m_minFrequency > 0) {
Chris@114 1482 minbin = int((double(m_minFrequency) * m_fftSize) / sr + 0.1);
Chris@114 1483 if (minbin < 1) minbin = 1;
Chris@184 1484 if (minbin >= maxbin) minbin = maxbin - 1;
Chris@114 1485 }
Chris@114 1486
Chris@118 1487 float perPixel =
Chris@118 1488 float(v->height()) /
Chris@184 1489 float((maxbin - minbin) / (m_zeroPadLevel + 1));
Chris@118 1490
Chris@118 1491 if (perPixel > 2.8) {
Chris@118 1492 return 3; // 4x oversampling
Chris@118 1493 } else if (perPixel > 1.5) {
Chris@118 1494 return 1; // 2x
Chris@114 1495 } else {
Chris@118 1496 return 0; // 1x
Chris@114 1497 }
Chris@114 1498 }
Chris@114 1499
Chris@114 1500 size_t
Chris@114 1501 SpectrogramLayer::getFFTSize(const View *v) const
Chris@114 1502 {
Chris@114 1503 return m_fftSize * (getZeroPadLevel(v) + 1);
Chris@114 1504 }
Chris@114 1505
Chris@130 1506 FFTModel *
Chris@130 1507 SpectrogramLayer::getFFTModel(const View *v) const
Chris@114 1508 {
Chris@114 1509 if (!m_model) return 0;
Chris@114 1510
Chris@114 1511 size_t fftSize = getFFTSize(v);
Chris@114 1512
Chris@130 1513 if (m_fftModels.find(v) != m_fftModels.end()) {
Chris@184 1514 if (m_fftModels[v].first == 0) {
Chris@184 1515 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@184 1516 std::cerr << "SpectrogramLayer::getFFTModel(" << v << "): Found null model" << std::endl;
Chris@184 1517 #endif
Chris@184 1518 return 0;
Chris@184 1519 }
Chris@184 1520 if (m_fftModels[v].first->getHeight() != fftSize / 2 + 1) {
Chris@184 1521 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@184 1522 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 1523 #endif
Chris@130 1524 delete m_fftModels[v].first;
Chris@130 1525 m_fftModels.erase(v);
Chris@184 1526 } else {
Chris@184 1527 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@187 1528 std::cerr << "SpectrogramLayer::getFFTModel(" << v << "): Found a good model of height " << m_fftModels[v].first->getHeight() << std::endl;
Chris@184 1529 #endif
Chris@184 1530 return m_fftModels[v].first;
Chris@114 1531 }
Chris@114 1532 }
Chris@114 1533
Chris@130 1534 if (m_fftModels.find(v) == m_fftModels.end()) {
Chris@169 1535
Chris@169 1536 FFTModel *model = new FFTModel(m_model,
Chris@169 1537 m_channel,
Chris@169 1538 m_windowType,
Chris@169 1539 m_windowSize,
Chris@169 1540 getWindowIncrement(),
Chris@169 1541 fftSize,
Chris@169 1542 true,
Chris@169 1543 m_candidateFillStartFrame);
Chris@169 1544
Chris@178 1545 if (!model->isOK()) {
Chris@178 1546 QMessageBox::critical
Chris@178 1547 (0, tr("FFT cache failed"),
Chris@178 1548 tr("Failed to create the FFT model for this spectrogram.\n"
Chris@178 1549 "There may be insufficient memory or disc space to continue."));
Chris@178 1550 delete model;
Chris@178 1551 m_fftModels[v] = FFTFillPair(0, 0);
Chris@178 1552 return 0;
Chris@178 1553 }
Chris@178 1554
Chris@193 1555 if (!m_sliceableModel) {
Chris@248 1556 #ifdef DEBUG_SPECTROGRAM
Chris@193 1557 std::cerr << "SpectrogramLayer: emitting sliceableModelReplaced(0, " << model << ")" << std::endl;
Chris@248 1558 #endif
Chris@193 1559 ((SpectrogramLayer *)this)->sliceableModelReplaced(0, model);
Chris@193 1560 m_sliceableModel = model;
Chris@193 1561 }
Chris@193 1562
Chris@169 1563 m_fftModels[v] = FFTFillPair(model, 0);
Chris@169 1564
Chris@169 1565 model->resume();
Chris@114 1566
Chris@114 1567 delete m_updateTimer;
Chris@114 1568 m_updateTimer = new QTimer((SpectrogramLayer *)this);
Chris@114 1569 connect(m_updateTimer, SIGNAL(timeout()),
Chris@114 1570 this, SLOT(fillTimerTimedOut()));
Chris@114 1571 m_updateTimer->start(200);
Chris@114 1572 }
Chris@114 1573
Chris@130 1574 return m_fftModels[v].first;
Chris@114 1575 }
Chris@114 1576
Chris@193 1577 const Model *
Chris@193 1578 SpectrogramLayer::getSliceableModel() const
Chris@193 1579 {
Chris@193 1580 if (m_sliceableModel) return m_sliceableModel;
Chris@193 1581 if (m_fftModels.empty()) return 0;
Chris@193 1582 m_sliceableModel = m_fftModels.begin()->second.first;
Chris@193 1583 return m_sliceableModel;
Chris@193 1584 }
Chris@193 1585
Chris@114 1586 void
Chris@130 1587 SpectrogramLayer::invalidateFFTModels()
Chris@114 1588 {
Chris@130 1589 for (ViewFFTMap::iterator i = m_fftModels.begin();
Chris@130 1590 i != m_fftModels.end(); ++i) {
Chris@115 1591 delete i->second.first;
Chris@114 1592 }
Chris@114 1593
Chris@130 1594 m_fftModels.clear();
Chris@193 1595
Chris@193 1596 if (m_sliceableModel) {
Chris@193 1597 std::cerr << "SpectrogramLayer: emitting sliceableModelReplaced(" << m_sliceableModel << ", 0)" << std::endl;
Chris@193 1598 emit sliceableModelReplaced(m_sliceableModel, 0);
Chris@193 1599 m_sliceableModel = 0;
Chris@193 1600 }
Chris@114 1601 }
Chris@114 1602
Chris@0 1603 void
Chris@119 1604 SpectrogramLayer::invalidateMagnitudes()
Chris@119 1605 {
Chris@119 1606 m_viewMags.clear();
Chris@119 1607 for (std::vector<MagnitudeRange>::iterator i = m_columnMags.begin();
Chris@119 1608 i != m_columnMags.end(); ++i) {
Chris@119 1609 *i = MagnitudeRange();
Chris@119 1610 }
Chris@119 1611 }
Chris@119 1612
Chris@119 1613 bool
Chris@119 1614 SpectrogramLayer::updateViewMagnitudes(View *v) const
Chris@119 1615 {
Chris@119 1616 MagnitudeRange mag;
Chris@119 1617
Chris@119 1618 int x0 = 0, x1 = v->width();
Chris@119 1619 float s00 = 0, s01 = 0, s10 = 0, s11 = 0;
Chris@119 1620
Chris@203 1621 if (!getXBinRange(v, x0, s00, s01)) {
Chris@203 1622 s00 = s01 = m_model->getStartFrame() / getWindowIncrement();
Chris@203 1623 }
Chris@203 1624
Chris@203 1625 if (!getXBinRange(v, x1, s10, s11)) {
Chris@203 1626 s10 = s11 = m_model->getEndFrame() / getWindowIncrement();
Chris@203 1627 }
Chris@119 1628
Chris@119 1629 int s0 = int(std::min(s00, s10) + 0.0001);
Chris@203 1630 int s1 = int(std::max(s01, s11) + 0.0001);
Chris@203 1631
Chris@203 1632 // std::cerr << "SpectrogramLayer::updateViewMagnitudes: x0 = " << x0 << ", x1 = " << x1 << ", s00 = " << s00 << ", s11 = " << s11 << " s0 = " << s0 << ", s1 = " << s1 << std::endl;
Chris@119 1633
Chris@248 1634 if (int(m_columnMags.size()) <= s1) {
Chris@119 1635 m_columnMags.resize(s1 + 1);
Chris@119 1636 }
Chris@119 1637
Chris@119 1638 for (int s = s0; s <= s1; ++s) {
Chris@119 1639 if (m_columnMags[s].isSet()) {
Chris@119 1640 mag.sample(m_columnMags[s]);
Chris@119 1641 }
Chris@119 1642 }
Chris@119 1643
Chris@184 1644 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@119 1645 std::cerr << "SpectrogramLayer::updateViewMagnitudes returning from cols "
Chris@119 1646 << s0 << " -> " << s1 << " inclusive" << std::endl;
Chris@184 1647 #endif
Chris@119 1648
Chris@119 1649 if (!mag.isSet()) return false;
Chris@119 1650 if (mag == m_viewMags[v]) return false;
Chris@119 1651 m_viewMags[v] = mag;
Chris@119 1652 return true;
Chris@119 1653 }
Chris@119 1654
Chris@119 1655 void
Chris@44 1656 SpectrogramLayer::paint(View *v, QPainter &paint, QRect rect) const
Chris@0 1657 {
Chris@253 1658 // What a lovely, old-fashioned function this is.
Chris@253 1659 // It's practically FORTRAN 77 in its clarity and linearity.
Chris@253 1660
Chris@161 1661 Profiler profiler("SpectrogramLayer::paint", true);
Chris@0 1662 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@95 1663 std::cerr << "SpectrogramLayer::paint(): m_model is " << m_model << ", zoom level is " << v->getZoomLevel() << ", m_updateTimer " << m_updateTimer << std::endl;
Chris@95 1664
Chris@95 1665 std::cerr << "rect is " << rect.x() << "," << rect.y() << " " << rect.width() << "x" << rect.height() << std::endl;
Chris@0 1666 #endif
Chris@95 1667
Chris@133 1668 long startFrame = v->getStartFrame();
Chris@133 1669 if (startFrame < 0) m_candidateFillStartFrame = 0;
Chris@133 1670 else m_candidateFillStartFrame = startFrame;
Chris@44 1671
Chris@0 1672 if (!m_model || !m_model->isOK() || !m_model->isReady()) {
Chris@0 1673 return;
Chris@0 1674 }
Chris@0 1675
Chris@47 1676 if (isLayerDormant(v)) {
Chris@48 1677 std::cerr << "SpectrogramLayer::paint(): Layer is dormant, making it undormant again" << std::endl;
Chris@29 1678 }
Chris@29 1679
Chris@48 1680 // Need to do this even if !isLayerDormant, as that could mean v
Chris@48 1681 // is not in the dormancy map at all -- we need it to be present
Chris@48 1682 // and accountable for when determining whether we need the cache
Chris@48 1683 // in the cache-fill thread above.
Chris@131 1684 //!!! no longer use cache-fill thread
Chris@131 1685 const_cast<SpectrogramLayer *>(this)->Layer::setLayerDormant(v, false);
Chris@48 1686
Chris@114 1687 size_t fftSize = getFFTSize(v);
Chris@130 1688 FFTModel *fft = getFFTModel(v);
Chris@114 1689 if (!fft) {
Chris@130 1690 std::cerr << "ERROR: SpectrogramLayer::paint(): No FFT model, returning" << std::endl;
Chris@0 1691 return;
Chris@0 1692 }
Chris@0 1693
Chris@95 1694 PixmapCache &cache = m_pixmapCaches[v];
Chris@95 1695
Chris@95 1696 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@95 1697 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 1698 #endif
Chris@95 1699
Chris@248 1700 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@0 1701 bool stillCacheing = (m_updateTimer != 0);
Chris@0 1702 std::cerr << "SpectrogramLayer::paint(): Still cacheing = " << stillCacheing << std::endl;
Chris@0 1703 #endif
Chris@0 1704
Chris@44 1705 int zoomLevel = v->getZoomLevel();
Chris@0 1706
Chris@0 1707 int x0 = 0;
Chris@44 1708 int x1 = v->width();
Chris@0 1709
Chris@0 1710 bool recreateWholePixmapCache = true;
Chris@0 1711
Chris@95 1712 x0 = rect.left();
Chris@95 1713 x1 = rect.right() + 1;
Chris@95 1714
Chris@95 1715 if (cache.validArea.width() > 0) {
Chris@95 1716
Chris@95 1717 if (int(cache.zoomLevel) == zoomLevel &&
Chris@95 1718 cache.pixmap.width() == v->width() &&
Chris@95 1719 cache.pixmap.height() == v->height()) {
Chris@95 1720
Chris@95 1721 if (v->getXForFrame(cache.startFrame) ==
Chris@95 1722 v->getXForFrame(startFrame) &&
Chris@95 1723 cache.validArea.x() <= x0 &&
Chris@95 1724 cache.validArea.x() + cache.validArea.width() >= x1) {
Chris@0 1725
Chris@0 1726 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@0 1727 std::cerr << "SpectrogramLayer: pixmap cache good" << std::endl;
Chris@0 1728 #endif
Chris@0 1729
Chris@95 1730 paint.drawPixmap(rect, cache.pixmap, rect);
Chris@121 1731 illuminateLocalFeatures(v, paint);
Chris@0 1732 return;
Chris@0 1733
Chris@0 1734 } else {
Chris@0 1735
Chris@0 1736 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@0 1737 std::cerr << "SpectrogramLayer: pixmap cache partially OK" << std::endl;
Chris@0 1738 #endif
Chris@0 1739
Chris@0 1740 recreateWholePixmapCache = false;
Chris@0 1741
Chris@95 1742 int dx = v->getXForFrame(cache.startFrame) -
Chris@44 1743 v->getXForFrame(startFrame);
Chris@0 1744
Chris@0 1745 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@95 1746 std::cerr << "SpectrogramLayer: dx = " << dx << " (pixmap cache " << cache.pixmap.width() << "x" << cache.pixmap.height() << ")" << std::endl;
Chris@0 1747 #endif
Chris@0 1748
Chris@95 1749 if (dx != 0 &&
Chris@95 1750 dx > -cache.pixmap.width() &&
Chris@95 1751 dx < cache.pixmap.width()) {
Chris@0 1752
Chris@0 1753 #if defined(Q_WS_WIN32) || defined(Q_WS_MAC)
Chris@0 1754 // Copying a pixmap to itself doesn't work
Chris@0 1755 // properly on Windows or Mac (it only works when
Chris@0 1756 // moving in one direction).
Chris@0 1757
Chris@0 1758 //!!! Need a utility function for this
Chris@0 1759
Chris@0 1760 static QPixmap *tmpPixmap = 0;
Chris@0 1761 if (!tmpPixmap ||
Chris@95 1762 tmpPixmap->width() != cache.pixmap.width() ||
Chris@95 1763 tmpPixmap->height() != cache.pixmap.height()) {
Chris@0 1764 delete tmpPixmap;
Chris@95 1765 tmpPixmap = new QPixmap(cache.pixmap.width(),
Chris@95 1766 cache.pixmap.height());
Chris@0 1767 }
Chris@0 1768 QPainter cachePainter;
Chris@0 1769 cachePainter.begin(tmpPixmap);
Chris@95 1770 cachePainter.drawPixmap(0, 0, cache.pixmap);
Chris@0 1771 cachePainter.end();
Chris@95 1772 cachePainter.begin(&cache.pixmap);
Chris@0 1773 cachePainter.drawPixmap(dx, 0, *tmpPixmap);
Chris@0 1774 cachePainter.end();
Chris@0 1775 #else
Chris@95 1776 QPainter cachePainter(&cache.pixmap);
Chris@95 1777 cachePainter.drawPixmap(dx, 0, cache.pixmap);
Chris@0 1778 cachePainter.end();
Chris@0 1779 #endif
Chris@0 1780
Chris@95 1781 int px = cache.validArea.x();
Chris@95 1782 int pw = cache.validArea.width();
Chris@0 1783
Chris@0 1784 if (dx < 0) {
Chris@95 1785 x0 = cache.pixmap.width() + dx;
Chris@95 1786 x1 = cache.pixmap.width();
Chris@95 1787 px += dx;
Chris@95 1788 if (px < 0) {
Chris@95 1789 pw += px;
Chris@95 1790 px = 0;
Chris@95 1791 if (pw < 0) pw = 0;
Chris@95 1792 }
Chris@0 1793 } else {
Chris@0 1794 x0 = 0;
Chris@0 1795 x1 = dx;
Chris@95 1796 px += dx;
Chris@95 1797 if (px + pw > cache.pixmap.width()) {
Chris@95 1798 pw = int(cache.pixmap.width()) - px;
Chris@95 1799 if (pw < 0) pw = 0;
Chris@95 1800 }
Chris@0 1801 }
Chris@95 1802
Chris@95 1803 cache.validArea =
Chris@95 1804 QRect(px, cache.validArea.y(),
Chris@95 1805 pw, cache.validArea.height());
Chris@95 1806
Chris@95 1807 paint.drawPixmap(rect & cache.validArea,
Chris@95 1808 cache.pixmap,
Chris@95 1809 rect & cache.validArea);
Chris@0 1810 }
Chris@0 1811 }
Chris@0 1812 } else {
Chris@0 1813 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@0 1814 std::cerr << "SpectrogramLayer: pixmap cache useless" << std::endl;
Chris@224 1815 if (int(cache.zoomLevel) != zoomLevel) {
Chris@224 1816 std::cerr << "(cache zoomLevel " << cache.zoomLevel
Chris@224 1817 << " != " << zoomLevel << ")" << std::endl;
Chris@224 1818 }
Chris@224 1819 if (cache.pixmap.width() != v->width()) {
Chris@224 1820 std::cerr << "(cache width " << cache.pixmap.width()
Chris@224 1821 << " != " << v->width();
Chris@224 1822 }
Chris@224 1823 if (cache.pixmap.height() != v->height()) {
Chris@224 1824 std::cerr << "(cache height " << cache.pixmap.height()
Chris@224 1825 << " != " << v->height();
Chris@224 1826 }
Chris@0 1827 #endif
Chris@95 1828 cache.validArea = QRect();
Chris@0 1829 }
Chris@0 1830 }
Chris@95 1831
Chris@133 1832 if (updateViewMagnitudes(v)) {
Chris@184 1833 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@133 1834 std::cerr << "SpectrogramLayer: magnitude range changed to [" << m_viewMags[v].getMin() << "->" << m_viewMags[v].getMax() << "]" << std::endl;
Chris@184 1835 #endif
Chris@133 1836 recreateWholePixmapCache = true;
Chris@133 1837 } else {
Chris@184 1838 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@133 1839 std::cerr << "No change in magnitude range [" << m_viewMags[v].getMin() << "->" << m_viewMags[v].getMax() << "]" << std::endl;
Chris@184 1840 #endif
Chris@133 1841 }
Chris@133 1842
Chris@95 1843 if (recreateWholePixmapCache) {
Chris@95 1844 x0 = 0;
Chris@95 1845 x1 = v->width();
Chris@95 1846 }
Chris@95 1847
Chris@215 1848 struct timeval tv;
Chris@215 1849 (void)gettimeofday(&tv, 0);
Chris@215 1850 RealTime mainPaintStart = RealTime::fromTimeval(tv);
Chris@215 1851
Chris@215 1852 int paintBlockWidth = m_lastPaintBlockWidth;
Chris@215 1853
Chris@215 1854 if (paintBlockWidth == 0) {
Chris@215 1855 paintBlockWidth = (300000 / zoomLevel);
Chris@215 1856 } else {
Chris@215 1857 RealTime lastTime = m_lastPaintTime;
Chris@215 1858 while (lastTime > RealTime::fromMilliseconds(200) &&
Chris@215 1859 paintBlockWidth > 50) {
Chris@215 1860 paintBlockWidth /= 2;
Chris@215 1861 lastTime = lastTime / 2;
Chris@215 1862 }
Chris@215 1863 while (lastTime < RealTime::fromMilliseconds(90) &&
Chris@215 1864 paintBlockWidth < 1500) {
Chris@215 1865 paintBlockWidth *= 2;
Chris@215 1866 lastTime = lastTime * 2;
Chris@215 1867 }
Chris@215 1868 }
Chris@215 1869
Chris@96 1870 if (paintBlockWidth < 20) paintBlockWidth = 20;
Chris@96 1871
Chris@224 1872 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@215 1873 std::cerr << "[" << this << "]: last paint width: " << m_lastPaintBlockWidth << ", last paint time: " << m_lastPaintTime << ", new paint width: " << paintBlockWidth << std::endl;
Chris@224 1874 #endif
Chris@224 1875
Chris@224 1876 // We always paint the full height when refreshing the cache.
Chris@224 1877 // Smaller heights can be used when painting direct from cache
Chris@224 1878 // (further up in this function), but we want to ensure the cache
Chris@224 1879 // is coherent without having to worry about vertical matching of
Chris@224 1880 // required and valid areas as well as horizontal.
Chris@224 1881
Chris@224 1882 int h = v->height();
Chris@215 1883
Chris@96 1884 if (cache.validArea.width() > 0) {
Chris@96 1885
Chris@96 1886 int vx0 = 0, vx1 = 0;
Chris@96 1887 vx0 = cache.validArea.x();
Chris@96 1888 vx1 = cache.validArea.x() + cache.validArea.width();
Chris@96 1889
Chris@96 1890 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@96 1891 std::cerr << "x0 " << x0 << ", x1 " << x1 << ", vx0 " << vx0 << ", vx1 " << vx1 << ", paintBlockWidth " << paintBlockWidth << std::endl;
Chris@96 1892 #endif
Chris@96 1893 if (x0 < vx0) {
Chris@96 1894 if (x0 + paintBlockWidth < vx0) {
Chris@96 1895 x0 = vx0 - paintBlockWidth;
Chris@96 1896 } else {
Chris@96 1897 x0 = 0;
Chris@96 1898 }
Chris@96 1899 } else if (x0 > vx1) {
Chris@96 1900 x0 = vx1;
Chris@96 1901 }
Chris@95 1902
Chris@96 1903 if (x1 < vx0) {
Chris@96 1904 x1 = vx0;
Chris@96 1905 } else if (x1 > vx1) {
Chris@96 1906 if (vx1 + paintBlockWidth < x1) {
Chris@96 1907 x1 = vx1 + paintBlockWidth;
Chris@96 1908 } else {
Chris@96 1909 x1 = v->width();
Chris@95 1910 }
Chris@96 1911 }
Chris@95 1912
Chris@96 1913 cache.validArea = QRect
Chris@96 1914 (std::min(vx0, x0), cache.validArea.y(),
Chris@96 1915 std::max(vx1 - std::min(vx0, x0),
Chris@96 1916 x1 - std::min(vx0, x0)),
Chris@96 1917 cache.validArea.height());
Chris@95 1918
Chris@96 1919 } else {
Chris@96 1920 if (x1 > x0 + paintBlockWidth) {
Chris@133 1921 int sfx = x1;
Chris@133 1922 if (startFrame < 0) sfx = v->getXForFrame(0);
Chris@133 1923 if (sfx >= x0 && sfx + paintBlockWidth <= x1) {
Chris@133 1924 x0 = sfx;
Chris@133 1925 x1 = x0 + paintBlockWidth;
Chris@133 1926 } else {
Chris@133 1927 int mid = (x1 + x0) / 2;
Chris@133 1928 x0 = mid - paintBlockWidth/2;
Chris@133 1929 x1 = x0 + paintBlockWidth;
Chris@133 1930 }
Chris@95 1931 }
Chris@224 1932 cache.validArea = QRect(x0, 0, x1 - x0, h);
Chris@95 1933 }
Chris@95 1934
Chris@0 1935 int w = x1 - x0;
Chris@0 1936
Chris@95 1937 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@95 1938 std::cerr << "x0 " << x0 << ", x1 " << x1 << ", w " << w << ", h " << h << std::endl;
Chris@95 1939 #endif
Chris@95 1940
Chris@95 1941 if (m_drawBuffer.width() < w || m_drawBuffer.height() < h) {
Chris@95 1942 m_drawBuffer = QImage(w, h, QImage::Format_RGB32);
Chris@95 1943 }
Chris@95 1944
Chris@197 1945 m_drawBuffer.fill(m_palette.getColour(0).rgb());
Chris@35 1946
Chris@37 1947 int sr = m_model->getSampleRate();
Chris@122 1948
Chris@122 1949 // Set minFreq and maxFreq to the frequency extents of the possibly
Chris@122 1950 // zero-padded visible bin range, and displayMinFreq and displayMaxFreq
Chris@122 1951 // to the actual scale frequency extents (presumably not zero padded).
Chris@253 1952
Chris@253 1953 // If we are zero padding, we want to use the zero-padded
Chris@253 1954 // equivalents of the bins that we would be using if not zero
Chris@253 1955 // padded, to avoid spaces at the top and bottom of the display.
Chris@253 1956
Chris@253 1957 // Note fftSize is the actual zero-padded fft size, m_fftSize the
Chris@253 1958 // nominal fft size.
Chris@35 1959
Chris@253 1960 size_t maxbin = m_fftSize / 2;
Chris@35 1961 if (m_maxFrequency > 0) {
Chris@253 1962 maxbin = int((double(m_maxFrequency) * m_fftSize) / sr + 0.001);
Chris@253 1963 if (maxbin > m_fftSize / 2) maxbin = m_fftSize / 2;
Chris@35 1964 }
Chris@111 1965
Chris@40 1966 size_t minbin = 1;
Chris@37 1967 if (m_minFrequency > 0) {
Chris@253 1968 minbin = int((double(m_minFrequency) * m_fftSize) / sr + 0.001);
Chris@253 1969 // std::cerr << "m_minFrequency = " << m_minFrequency << " -> minbin = " << minbin << std::endl;
Chris@40 1970 if (minbin < 1) minbin = 1;
Chris@184 1971 if (minbin >= maxbin) minbin = maxbin - 1;
Chris@37 1972 }
Chris@37 1973
Chris@253 1974 int zpl = getZeroPadLevel(v) + 1;
Chris@253 1975 minbin = minbin * zpl;
Chris@253 1976 maxbin = (maxbin + 1) * zpl - 1;
Chris@253 1977
Chris@114 1978 float minFreq = (float(minbin) * sr) / fftSize;
Chris@184 1979 float maxFreq = (float(maxbin) * sr) / fftSize;
Chris@0 1980
Chris@122 1981 float displayMinFreq = minFreq;
Chris@122 1982 float displayMaxFreq = maxFreq;
Chris@122 1983
Chris@122 1984 if (fftSize != m_fftSize) {
Chris@122 1985 displayMinFreq = getEffectiveMinFrequency();
Chris@122 1986 displayMaxFreq = getEffectiveMaxFrequency();
Chris@122 1987 }
Chris@122 1988
Chris@253 1989 // std::cerr << "(giving actual minFreq " << minFreq << " and display minFreq " << displayMinFreq << ")" << std::endl;
Chris@253 1990
Chris@92 1991 float ymag[h];
Chris@92 1992 float ydiv[h];
Chris@184 1993 float yval[maxbin + 1]; //!!! cache this?
Chris@92 1994
Chris@38 1995 size_t increment = getWindowIncrement();
Chris@40 1996
Chris@40 1997 bool logarithmic = (m_frequencyScale == LogFrequencyScale);
Chris@38 1998
Chris@184 1999 for (size_t q = minbin; q <= maxbin; ++q) {
Chris@114 2000 float f0 = (float(q) * sr) / fftSize;
Chris@122 2001 yval[q] = v->getYForFrequency(f0, displayMinFreq, displayMaxFreq,
Chris@122 2002 logarithmic);
Chris@122 2003 // std::cerr << "min: " << minFreq << ", max: " << maxFreq << ", yval[" << q << "]: " << yval[q] << std::endl;
Chris@92 2004 }
Chris@92 2005
Chris@119 2006 MagnitudeRange overallMag = m_viewMags[v];
Chris@119 2007 bool overallMagChanged = false;
Chris@119 2008
Chris@162 2009 bool fftSuspended = false;
Chris@131 2010
Chris@221 2011 bool interpolate = false;
Chris@221 2012 Preferences::SpectrogramSmoothing smoothing =
Chris@221 2013 Preferences::getInstance()->getSpectrogramSmoothing();
Chris@221 2014 if (smoothing == Preferences::SpectrogramInterpolated ||
Chris@221 2015 smoothing == Preferences::SpectrogramZeroPaddedAndInterpolated) {
Chris@222 2016 if (m_binDisplay != PeakBins &&
Chris@222 2017 m_binDisplay != PeakFrequencies) {
Chris@222 2018 interpolate = true;
Chris@222 2019 }
Chris@221 2020 }
Chris@221 2021
Chris@137 2022 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@224 2023 std::cerr << ((float(v->getFrameForX(1) - v->getFrameForX(0))) / increment) << " bin(s) per pixel" << std::endl;
Chris@137 2024 #endif
Chris@137 2025
Chris@224 2026 bool runOutOfData = false;
Chris@224 2027
Chris@35 2028 for (int x = 0; x < w; ++x) {
Chris@35 2029
Chris@224 2030 if (runOutOfData) break;
Chris@224 2031
Chris@35 2032 for (int y = 0; y < h; ++y) {
Chris@134 2033 ymag[y] = 0.f;
Chris@134 2034 ydiv[y] = 0.f;
Chris@35 2035 }
Chris@35 2036
Chris@35 2037 float s0 = 0, s1 = 0;
Chris@35 2038
Chris@44 2039 if (!getXBinRange(v, x0 + x, s0, s1)) {
Chris@95 2040 assert(x <= m_drawBuffer.width());
Chris@35 2041 continue;
Chris@35 2042 }
Chris@35 2043
Chris@35 2044 int s0i = int(s0 + 0.001);
Chris@35 2045 int s1i = int(s1);
Chris@35 2046
Chris@248 2047 if (s1i >= int(fft->getWidth())) {
Chris@248 2048 if (s0i >= int(fft->getWidth())) {
Chris@45 2049 continue;
Chris@45 2050 } else {
Chris@45 2051 s1i = s0i;
Chris@45 2052 }
Chris@45 2053 }
Chris@92 2054
Chris@92 2055 for (int s = s0i; s <= s1i; ++s) {
Chris@92 2056
Chris@224 2057 if (!fft->isColumnAvailable(s)) {
Chris@224 2058 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@224 2059 std::cerr << "Met unavailable column at col " << s << std::endl;
Chris@224 2060 #endif
Chris@224 2061 // continue;
Chris@224 2062 runOutOfData = true;
Chris@224 2063 break;
Chris@224 2064 }
Chris@162 2065
Chris@162 2066 if (!fftSuspended) {
Chris@162 2067 fft->suspendWrites();
Chris@162 2068 fftSuspended = true;
Chris@162 2069 }
Chris@162 2070
Chris@119 2071 MagnitudeRange mag;
Chris@92 2072
Chris@280 2073 FFTModel::PeakSet peaks;
Chris@280 2074 if (m_binDisplay == PeakFrequencies &&
Chris@280 2075 s < int(fft->getWidth()) - 1) {
Chris@280 2076 peaks = fft->getPeakFrequencies(FFTModel::AllPeaks,
Chris@280 2077 s,
Chris@280 2078 minbin, maxbin - 1);
Chris@280 2079 }
Chris@280 2080
Chris@184 2081 for (size_t q = minbin; q < maxbin; ++q) {
Chris@92 2082
Chris@92 2083 float y0 = yval[q + 1];
Chris@92 2084 float y1 = yval[q];
Chris@92 2085
Chris@280 2086 if (m_binDisplay == PeakBins) {
Chris@114 2087 if (!fft->isLocalPeak(s, q)) continue;
Chris@40 2088 }
Chris@280 2089 if (m_binDisplay == PeakFrequencies) {
Chris@280 2090 if (peaks.find(q) == peaks.end()) continue;
Chris@280 2091 }
Chris@114 2092
Chris@114 2093 if (m_threshold != 0.f &&
Chris@253 2094 !fft->isOverThreshold(s, q, m_threshold * (m_fftSize/2))) {
Chris@114 2095 continue;
Chris@114 2096 }
Chris@40 2097
Chris@35 2098 float sprop = 1.0;
Chris@35 2099 if (s == s0i) sprop *= (s + 1) - s0;
Chris@35 2100 if (s == s1i) sprop *= s1 - s;
Chris@35 2101
Chris@280 2102 if (m_binDisplay == PeakFrequencies) {
Chris@44 2103 y0 = y1 = v->getYForFrequency
Chris@280 2104 (peaks[q], displayMinFreq, displayMaxFreq, logarithmic);
Chris@35 2105 }
Chris@38 2106
Chris@35 2107 int y0i = int(y0 + 0.001);
Chris@35 2108 int y1i = int(y1);
Chris@35 2109
Chris@92 2110 float value;
Chris@92 2111
Chris@92 2112 if (m_colourScale == PhaseColourScale) {
Chris@114 2113 value = fft->getPhaseAt(s, q);
Chris@92 2114 } else if (m_normalizeColumns) {
Chris@119 2115 value = fft->getNormalizedMagnitudeAt(s, q);
Chris@119 2116 mag.sample(value);
Chris@119 2117 value *= m_gain;
Chris@92 2118 } else {
Chris@252 2119 value = fft->getMagnitudeAt(s, q) / (m_fftSize/2);
Chris@119 2120 mag.sample(value);
Chris@119 2121 value *= m_gain;
Chris@92 2122 }
Chris@92 2123
Chris@221 2124 if (interpolate) {
Chris@221 2125
Chris@221 2126 int ypi = y0i;
Chris@221 2127 if (q < maxbin - 1) ypi = int(yval[q + 2]);
Chris@221 2128
Chris@221 2129 for (int y = ypi; y <= y1i; ++y) {
Chris@221 2130
Chris@221 2131 if (y < 0 || y >= h) continue;
Chris@221 2132
Chris@221 2133 float yprop = sprop;
Chris@221 2134 float iprop = yprop;
Chris@221 2135
Chris@221 2136 if (ypi < y0i && y <= y0i) {
Chris@221 2137
Chris@221 2138 float half = float(y0i - ypi) / 2;
Chris@221 2139 float dist = y - (ypi + half);
Chris@221 2140
Chris@221 2141 if (dist >= 0) {
Chris@221 2142 iprop = (iprop * dist) / half;
Chris@221 2143 ymag[y] += iprop * value;
Chris@221 2144 }
Chris@221 2145 } else {
Chris@221 2146 if (y1i > y0i) {
Chris@221 2147
Chris@221 2148 float half = float(y1i - y0i) / 2;
Chris@221 2149 float dist = y - (y0i + half);
Chris@221 2150
Chris@221 2151 if (dist >= 0) {
Chris@221 2152 iprop = (iprop * (half - dist)) / half;
Chris@221 2153 }
Chris@221 2154 }
Chris@221 2155
Chris@221 2156 ymag[y] += iprop * value;
Chris@221 2157 ydiv[y] += yprop;
Chris@221 2158 }
Chris@221 2159 }
Chris@221 2160
Chris@221 2161 } else {
Chris@221 2162
Chris@221 2163 for (int y = y0i; y <= y1i; ++y) {
Chris@221 2164
Chris@221 2165 if (y < 0 || y >= h) continue;
Chris@221 2166
Chris@221 2167 float yprop = sprop;
Chris@221 2168 if (y == y0i) yprop *= (y + 1) - y0;
Chris@221 2169 if (y == y1i) yprop *= y1 - y;
Chris@221 2170
Chris@221 2171 for (int y = y0i; y <= y1i; ++y) {
Chris@35 2172
Chris@221 2173 if (y < 0 || y >= h) continue;
Chris@221 2174
Chris@221 2175 float yprop = sprop;
Chris@221 2176 if (y == y0i) yprop *= (y + 1) - y0;
Chris@221 2177 if (y == y1i) yprop *= y1 - y;
Chris@221 2178 ymag[y] += yprop * value;
Chris@221 2179 ydiv[y] += yprop;
Chris@221 2180 }
Chris@221 2181 }
Chris@221 2182 }
Chris@35 2183 }
Chris@119 2184
Chris@119 2185 if (mag.isSet()) {
Chris@119 2186
Chris@248 2187 if (s >= int(m_columnMags.size())) {
Chris@203 2188 std::cerr << "INTERNAL ERROR: " << s << " >= "
Chris@203 2189 << m_columnMags.size() << " at SpectrogramLayer.cpp:2087" << std::endl;
Chris@203 2190 }
Chris@203 2191
Chris@119 2192 m_columnMags[s].sample(mag);
Chris@119 2193
Chris@119 2194 if (overallMag.sample(mag)) {
Chris@119 2195 //!!! scaling would change here
Chris@119 2196 overallMagChanged = true;
Chris@209 2197 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@119 2198 std::cerr << "Overall mag changed (again?) at column " << s << ", to [" << overallMag.getMin() << "->" << overallMag.getMax() << "]" << std::endl;
Chris@209 2199 #endif
Chris@119 2200 }
Chris@119 2201 }
Chris@35 2202 }
Chris@35 2203
Chris@35 2204 for (int y = 0; y < h; ++y) {
Chris@35 2205
Chris@35 2206 if (ydiv[y] > 0.0) {
Chris@40 2207
Chris@40 2208 unsigned char pixel = 0;
Chris@40 2209
Chris@38 2210 float avg = ymag[y] / ydiv[y];
Chris@138 2211 pixel = getDisplayValue(v, avg);
Chris@40 2212
Chris@95 2213 assert(x <= m_drawBuffer.width());
Chris@197 2214 QColor c = m_palette.getColour(pixel);
Chris@95 2215 m_drawBuffer.setPixel(x, y,
Chris@95 2216 qRgb(c.red(), c.green(), c.blue()));
Chris@35 2217 }
Chris@35 2218 }
Chris@35 2219 }
Chris@35 2220
Chris@119 2221 if (overallMagChanged) {
Chris@119 2222 m_viewMags[v] = overallMag;
Chris@209 2223 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@119 2224 std::cerr << "Overall mag is now [" << m_viewMags[v].getMin() << "->" << m_viewMags[v].getMax() << "] - will be updating" << std::endl;
Chris@209 2225 #endif
Chris@119 2226 } else {
Chris@209 2227 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@119 2228 std::cerr << "Overall mag unchanged at [" << m_viewMags[v].getMin() << "->" << m_viewMags[v].getMax() << "]" << std::endl;
Chris@209 2229 #endif
Chris@119 2230 }
Chris@119 2231
Chris@161 2232 Profiler profiler2("SpectrogramLayer::paint: draw image", true);
Chris@137 2233
Chris@224 2234 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@224 2235 std::cerr << "Painting " << w << "x" << rect.height()
Chris@224 2236 << " from draw buffer at " << 0 << "," << rect.y()
Chris@224 2237 << " to window at " << x0 << "," << rect.y() << std::endl;
Chris@224 2238 #endif
Chris@224 2239
Chris@224 2240 paint.drawImage(x0, rect.y(), m_drawBuffer, 0, rect.y(), w, rect.height());
Chris@0 2241
Chris@0 2242 if (recreateWholePixmapCache) {
Chris@224 2243 cache.pixmap = QPixmap(v->width(), h);
Chris@0 2244 }
Chris@0 2245
Chris@224 2246 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@224 2247 std::cerr << "Painting " << w << "x" << h
Chris@224 2248 << " from draw buffer at " << 0 << "," << 0
Chris@224 2249 << " to cache at " << x0 << "," << 0 << std::endl;
Chris@224 2250 #endif
Chris@224 2251
Chris@95 2252 QPainter cachePainter(&cache.pixmap);
Chris@224 2253 cachePainter.drawImage(x0, 0, m_drawBuffer, 0, 0, w, h);
Chris@0 2254 cachePainter.end();
Chris@119 2255
Chris@120 2256 if (!m_normalizeVisibleArea || !overallMagChanged) {
Chris@0 2257
Chris@119 2258 cache.startFrame = startFrame;
Chris@119 2259 cache.zoomLevel = zoomLevel;
Chris@119 2260
Chris@119 2261 if (cache.validArea.x() > 0) {
Chris@95 2262 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@158 2263 std::cerr << "SpectrogramLayer::paint() updating left (0, "
Chris@158 2264 << cache.validArea.x() << ")" << std::endl;
Chris@95 2265 #endif
Chris@224 2266 v->update(0, 0, cache.validArea.x(), h);
Chris@119 2267 }
Chris@119 2268
Chris@119 2269 if (cache.validArea.x() + cache.validArea.width() <
Chris@119 2270 cache.pixmap.width()) {
Chris@95 2271 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@119 2272 std::cerr << "SpectrogramLayer::paint() updating right ("
Chris@119 2273 << cache.validArea.x() + cache.validArea.width()
Chris@119 2274 << ", "
Chris@119 2275 << cache.pixmap.width() - (cache.validArea.x() +
Chris@119 2276 cache.validArea.width())
Chris@119 2277 << ")" << std::endl;
Chris@95 2278 #endif
Chris@119 2279 v->update(cache.validArea.x() + cache.validArea.width(),
Chris@119 2280 0,
Chris@119 2281 cache.pixmap.width() - (cache.validArea.x() +
Chris@119 2282 cache.validArea.width()),
Chris@224 2283 h);
Chris@119 2284 }
Chris@119 2285 } else {
Chris@119 2286 // overallMagChanged
Chris@119 2287 cache.validArea = QRect();
Chris@119 2288 v->update();
Chris@95 2289 }
Chris@0 2290
Chris@121 2291 illuminateLocalFeatures(v, paint);
Chris@120 2292
Chris@0 2293 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@0 2294 std::cerr << "SpectrogramLayer::paint() returning" << std::endl;
Chris@0 2295 #endif
Chris@131 2296
Chris@215 2297 m_lastPaintBlockWidth = paintBlockWidth;
Chris@215 2298 (void)gettimeofday(&tv, 0);
Chris@215 2299 m_lastPaintTime = RealTime::fromTimeval(tv) - mainPaintStart;
Chris@215 2300
Chris@162 2301 if (fftSuspended) fft->resume();
Chris@0 2302 }
Chris@0 2303
Chris@121 2304 void
Chris@121 2305 SpectrogramLayer::illuminateLocalFeatures(View *v, QPainter &paint) const
Chris@121 2306 {
Chris@121 2307 QPoint localPos;
Chris@121 2308 if (!v->shouldIlluminateLocalFeatures(this, localPos) || !m_model) {
Chris@121 2309 return;
Chris@121 2310 }
Chris@121 2311
Chris@180 2312 // std::cerr << "SpectrogramLayer: illuminateLocalFeatures("
Chris@180 2313 // << localPos.x() << "," << localPos.y() << ")" << std::endl;
Chris@121 2314
Chris@121 2315 float s0, s1;
Chris@121 2316 float f0, f1;
Chris@121 2317
Chris@121 2318 if (getXBinRange(v, localPos.x(), s0, s1) &&
Chris@121 2319 getYBinSourceRange(v, localPos.y(), f0, f1)) {
Chris@121 2320
Chris@121 2321 int s0i = int(s0 + 0.001);
Chris@121 2322 int s1i = int(s1);
Chris@121 2323
Chris@121 2324 int x0 = v->getXForFrame(s0i * getWindowIncrement());
Chris@121 2325 int x1 = v->getXForFrame((s1i + 1) * getWindowIncrement());
Chris@121 2326
Chris@248 2327 int y1 = int(getYForFrequency(v, f1));
Chris@248 2328 int y0 = int(getYForFrequency(v, f0));
Chris@121 2329
Chris@180 2330 // std::cerr << "SpectrogramLayer: illuminate "
Chris@180 2331 // << x0 << "," << y1 << " -> " << x1 << "," << y0 << std::endl;
Chris@121 2332
Chris@287 2333 paint.setPen(v->getForeground());
Chris@133 2334
Chris@133 2335 //!!! should we be using paintCrosshairs for this?
Chris@133 2336
Chris@121 2337 paint.drawRect(x0, y1, x1 - x0 + 1, y0 - y1 + 1);
Chris@121 2338 }
Chris@121 2339 }
Chris@121 2340
Chris@42 2341 float
Chris@267 2342 SpectrogramLayer::getYForFrequency(const View *v, float frequency) const
Chris@42 2343 {
Chris@44 2344 return v->getYForFrequency(frequency,
Chris@44 2345 getEffectiveMinFrequency(),
Chris@44 2346 getEffectiveMaxFrequency(),
Chris@44 2347 m_frequencyScale == LogFrequencyScale);
Chris@42 2348 }
Chris@42 2349
Chris@42 2350 float
Chris@267 2351 SpectrogramLayer::getFrequencyForY(const View *v, int y) const
Chris@42 2352 {
Chris@44 2353 return v->getFrequencyForY(y,
Chris@44 2354 getEffectiveMinFrequency(),
Chris@44 2355 getEffectiveMaxFrequency(),
Chris@44 2356 m_frequencyScale == LogFrequencyScale);
Chris@42 2357 }
Chris@42 2358
Chris@0 2359 int
Chris@115 2360 SpectrogramLayer::getCompletion(View *v) const
Chris@0 2361 {
Chris@115 2362 if (m_updateTimer == 0) return 100;
Chris@130 2363 if (m_fftModels.find(v) == m_fftModels.end()) return 100;
Chris@130 2364
Chris@130 2365 size_t completion = m_fftModels[v].first->getCompletion();
Chris@224 2366 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@115 2367 std::cerr << "SpectrogramLayer::getCompletion: completion = " << completion << std::endl;
Chris@224 2368 #endif
Chris@0 2369 return completion;
Chris@0 2370 }
Chris@0 2371
Chris@28 2372 bool
Chris@101 2373 SpectrogramLayer::getValueExtents(float &min, float &max,
Chris@101 2374 bool &logarithmic, QString &unit) const
Chris@79 2375 {
Chris@133 2376 if (!m_model) return false;
Chris@133 2377
Chris@133 2378 int sr = m_model->getSampleRate();
Chris@133 2379 min = float(sr) / m_fftSize;
Chris@133 2380 max = float(sr) / 2;
Chris@133 2381
Chris@101 2382 logarithmic = (m_frequencyScale == LogFrequencyScale);
Chris@79 2383 unit = "Hz";
Chris@79 2384 return true;
Chris@79 2385 }
Chris@79 2386
Chris@79 2387 bool
Chris@101 2388 SpectrogramLayer::getDisplayExtents(float &min, float &max) const
Chris@101 2389 {
Chris@101 2390 min = getEffectiveMinFrequency();
Chris@101 2391 max = getEffectiveMaxFrequency();
Chris@253 2392
Chris@248 2393 // std::cerr << "SpectrogramLayer::getDisplayExtents: " << min << "->" << max << std::endl;
Chris@101 2394 return true;
Chris@101 2395 }
Chris@101 2396
Chris@101 2397 bool
Chris@120 2398 SpectrogramLayer::setDisplayExtents(float min, float max)
Chris@120 2399 {
Chris@120 2400 if (!m_model) return false;
Chris@187 2401
Chris@253 2402 // std::cerr << "SpectrogramLayer::setDisplayExtents: " << min << "->" << max << std::endl;
Chris@187 2403
Chris@120 2404 if (min < 0) min = 0;
Chris@120 2405 if (max > m_model->getSampleRate()/2) max = m_model->getSampleRate()/2;
Chris@120 2406
Chris@120 2407 size_t minf = lrintf(min);
Chris@120 2408 size_t maxf = lrintf(max);
Chris@120 2409
Chris@120 2410 if (m_minFrequency == minf && m_maxFrequency == maxf) return true;
Chris@120 2411
Chris@120 2412 invalidatePixmapCaches();
Chris@120 2413 invalidateMagnitudes();
Chris@120 2414
Chris@120 2415 m_minFrequency = minf;
Chris@120 2416 m_maxFrequency = maxf;
Chris@120 2417
Chris@120 2418 emit layerParametersChanged();
Chris@120 2419
Chris@133 2420 int vs = getCurrentVerticalZoomStep();
Chris@133 2421 if (vs != m_lastEmittedZoomStep) {
Chris@133 2422 emit verticalZoomChanged();
Chris@133 2423 m_lastEmittedZoomStep = vs;
Chris@133 2424 }
Chris@133 2425
Chris@120 2426 return true;
Chris@120 2427 }
Chris@120 2428
Chris@120 2429 bool
Chris@267 2430 SpectrogramLayer::getYScaleValue(const View *v, int y,
Chris@261 2431 float &value, QString &unit) const
Chris@261 2432 {
Chris@261 2433 value = getFrequencyForY(v, y);
Chris@261 2434 unit = "Hz";
Chris@261 2435 return true;
Chris@261 2436 }
Chris@261 2437
Chris@261 2438 bool
Chris@248 2439 SpectrogramLayer::snapToFeatureFrame(View *, int &frame,
Chris@28 2440 size_t &resolution,
Chris@28 2441 SnapType snap) const
Chris@13 2442 {
Chris@13 2443 resolution = getWindowIncrement();
Chris@28 2444 int left = (frame / resolution) * resolution;
Chris@28 2445 int right = left + resolution;
Chris@28 2446
Chris@28 2447 switch (snap) {
Chris@28 2448 case SnapLeft: frame = left; break;
Chris@28 2449 case SnapRight: frame = right; break;
Chris@28 2450 case SnapNearest:
Chris@28 2451 case SnapNeighbouring:
Chris@28 2452 if (frame - left > right - frame) frame = right;
Chris@28 2453 else frame = left;
Chris@28 2454 break;
Chris@28 2455 }
Chris@28 2456
Chris@28 2457 return true;
Chris@28 2458 }
Chris@13 2459
Chris@283 2460 void
Chris@283 2461 SpectrogramLayer::measureDoubleClick(View *v, QMouseEvent *e)
Chris@283 2462 {
Chris@283 2463 PixmapCache &cache = m_pixmapCaches[v];
Chris@283 2464
Chris@283 2465 std::cerr << "cache width: " << cache.pixmap.width() << ", height: "
Chris@283 2466 << cache.pixmap.height() << std::endl;
Chris@283 2467
Chris@283 2468 QImage image = cache.pixmap.toImage();
Chris@283 2469
Chris@283 2470 ImageRegionFinder finder;
Chris@283 2471 QRect rect = finder.findRegionExtents(&image, e->pos());
Chris@283 2472 if (rect.isValid()) {
Chris@283 2473 MeasureRect mr;
Chris@283 2474 setMeasureRectFromPixrect(v, mr, rect);
Chris@283 2475 CommandHistory::getInstance()->addCommand
Chris@283 2476 (new AddMeasurementRectCommand(this, mr));
Chris@283 2477 }
Chris@283 2478 }
Chris@283 2479
Chris@77 2480 bool
Chris@264 2481 SpectrogramLayer::getCrosshairExtents(View *v, QPainter &paint,
Chris@77 2482 QPoint cursorPos,
Chris@77 2483 std::vector<QRect> &extents) const
Chris@77 2484 {
Chris@77 2485 QRect vertical(cursorPos.x() - 12, 0, 12, v->height());
Chris@77 2486 extents.push_back(vertical);
Chris@77 2487
Chris@77 2488 QRect horizontal(0, cursorPos.y(), cursorPos.x(), 1);
Chris@77 2489 extents.push_back(horizontal);
Chris@77 2490
Chris@264 2491 int sw = getVerticalScaleWidth(v, paint);
Chris@264 2492
Chris@280 2493 QRect freq(sw, cursorPos.y() - paint.fontMetrics().ascent() - 2,
Chris@280 2494 paint.fontMetrics().width("123456 Hz") + 2,
Chris@280 2495 paint.fontMetrics().height());
Chris@280 2496 extents.push_back(freq);
Chris@264 2497
Chris@279 2498 QRect pitch(sw, cursorPos.y() + 2,
Chris@279 2499 paint.fontMetrics().width("C#10+50c") + 2,
Chris@279 2500 paint.fontMetrics().height());
Chris@279 2501 extents.push_back(pitch);
Chris@279 2502
Chris@280 2503 QRect rt(cursorPos.x(),
Chris@280 2504 v->height() - paint.fontMetrics().height() - 2,
Chris@280 2505 paint.fontMetrics().width("1234.567 s"),
Chris@280 2506 paint.fontMetrics().height());
Chris@280 2507 extents.push_back(rt);
Chris@280 2508
Chris@280 2509 int w(paint.fontMetrics().width("1234567890") + 2);
Chris@280 2510 QRect frame(cursorPos.x() - w - 2,
Chris@280 2511 v->height() - paint.fontMetrics().height() - 2,
Chris@280 2512 w,
Chris@280 2513 paint.fontMetrics().height());
Chris@280 2514 extents.push_back(frame);
Chris@280 2515
Chris@77 2516 return true;
Chris@77 2517 }
Chris@77 2518
Chris@77 2519 void
Chris@77 2520 SpectrogramLayer::paintCrosshairs(View *v, QPainter &paint,
Chris@77 2521 QPoint cursorPos) const
Chris@77 2522 {
Chris@77 2523 paint.save();
Chris@283 2524
Chris@283 2525 int sw = getVerticalScaleWidth(v, paint);
Chris@283 2526
Chris@282 2527 QFont fn = paint.font();
Chris@282 2528 if (fn.pointSize() > 8) {
Chris@282 2529 fn.setPointSize(fn.pointSize() - 1);
Chris@282 2530 paint.setFont(fn);
Chris@282 2531 }
Chris@77 2532 paint.setPen(m_crosshairColour);
Chris@77 2533
Chris@77 2534 paint.drawLine(0, cursorPos.y(), cursorPos.x() - 1, cursorPos.y());
Chris@77 2535 paint.drawLine(cursorPos.x(), 0, cursorPos.x(), v->height());
Chris@77 2536
Chris@77 2537 float fundamental = getFrequencyForY(v, cursorPos.y());
Chris@77 2538
Chris@278 2539 v->drawVisibleText(paint,
Chris@278 2540 sw + 2,
Chris@278 2541 cursorPos.y() - 2,
Chris@278 2542 QString("%1 Hz").arg(fundamental),
Chris@278 2543 View::OutlinedText);
Chris@278 2544
Chris@279 2545 if (Pitch::isFrequencyInMidiRange(fundamental)) {
Chris@279 2546 QString pitchLabel = Pitch::getPitchLabelForFrequency(fundamental);
Chris@279 2547 v->drawVisibleText(paint,
Chris@279 2548 sw + 2,
Chris@279 2549 cursorPos.y() + paint.fontMetrics().ascent() + 2,
Chris@279 2550 pitchLabel,
Chris@279 2551 View::OutlinedText);
Chris@279 2552 }
Chris@279 2553
Chris@280 2554 long frame = v->getFrameForX(cursorPos.x());
Chris@279 2555 RealTime rt = RealTime::frame2RealTime(frame, m_model->getSampleRate());
Chris@280 2556 QString rtLabel = QString("%1 s").arg(rt.toText(true).c_str());
Chris@280 2557 QString frameLabel = QString("%1").arg(frame);
Chris@280 2558 v->drawVisibleText(paint,
Chris@280 2559 cursorPos.x() - paint.fontMetrics().width(frameLabel) - 2,
Chris@280 2560 v->height() - 2,
Chris@280 2561 frameLabel,
Chris@280 2562 View::OutlinedText);
Chris@280 2563 v->drawVisibleText(paint,
Chris@280 2564 cursorPos.x() + 2,
Chris@280 2565 v->height() - 2,
Chris@280 2566 rtLabel,
Chris@280 2567 View::OutlinedText);
Chris@264 2568
Chris@77 2569 int harmonic = 2;
Chris@77 2570
Chris@77 2571 while (harmonic < 100) {
Chris@77 2572
Chris@77 2573 float hy = lrintf(getYForFrequency(v, fundamental * harmonic));
Chris@77 2574 if (hy < 0 || hy > v->height()) break;
Chris@77 2575
Chris@77 2576 int len = 7;
Chris@77 2577
Chris@77 2578 if (harmonic % 2 == 0) {
Chris@77 2579 if (harmonic % 4 == 0) {
Chris@77 2580 len = 12;
Chris@77 2581 } else {
Chris@77 2582 len = 10;
Chris@77 2583 }
Chris@77 2584 }
Chris@77 2585
Chris@77 2586 paint.drawLine(cursorPos.x() - len,
Chris@248 2587 int(hy),
Chris@77 2588 cursorPos.x(),
Chris@248 2589 int(hy));
Chris@77 2590
Chris@77 2591 ++harmonic;
Chris@77 2592 }
Chris@77 2593
Chris@77 2594 paint.restore();
Chris@77 2595 }
Chris@77 2596
Chris@25 2597 QString
Chris@44 2598 SpectrogramLayer::getFeatureDescription(View *v, QPoint &pos) const
Chris@25 2599 {
Chris@25 2600 int x = pos.x();
Chris@25 2601 int y = pos.y();
Chris@0 2602
Chris@25 2603 if (!m_model || !m_model->isOK()) return "";
Chris@0 2604
Chris@38 2605 float magMin = 0, magMax = 0;
Chris@38 2606 float phaseMin = 0, phaseMax = 0;
Chris@0 2607 float freqMin = 0, freqMax = 0;
Chris@35 2608 float adjFreqMin = 0, adjFreqMax = 0;
Chris@25 2609 QString pitchMin, pitchMax;
Chris@0 2610 RealTime rtMin, rtMax;
Chris@0 2611
Chris@38 2612 bool haveValues = false;
Chris@0 2613
Chris@44 2614 if (!getXBinSourceRange(v, x, rtMin, rtMax)) {
Chris@38 2615 return "";
Chris@38 2616 }
Chris@44 2617 if (getXYBinSourceRange(v, x, y, magMin, magMax, phaseMin, phaseMax)) {
Chris@38 2618 haveValues = true;
Chris@38 2619 }
Chris@0 2620
Chris@35 2621 QString adjFreqText = "", adjPitchText = "";
Chris@35 2622
Chris@38 2623 if (m_binDisplay == PeakFrequencies) {
Chris@35 2624
Chris@44 2625 if (!getAdjustedYBinSourceRange(v, x, y, freqMin, freqMax,
Chris@38 2626 adjFreqMin, adjFreqMax)) {
Chris@38 2627 return "";
Chris@38 2628 }
Chris@35 2629
Chris@35 2630 if (adjFreqMin != adjFreqMax) {
Chris@65 2631 adjFreqText = tr("Peak Frequency:\t%1 - %2 Hz\n")
Chris@35 2632 .arg(adjFreqMin).arg(adjFreqMax);
Chris@35 2633 } else {
Chris@65 2634 adjFreqText = tr("Peak Frequency:\t%1 Hz\n")
Chris@35 2635 .arg(adjFreqMin);
Chris@38 2636 }
Chris@38 2637
Chris@38 2638 QString pmin = Pitch::getPitchLabelForFrequency(adjFreqMin);
Chris@38 2639 QString pmax = Pitch::getPitchLabelForFrequency(adjFreqMax);
Chris@38 2640
Chris@38 2641 if (pmin != pmax) {
Chris@65 2642 adjPitchText = tr("Peak Pitch:\t%3 - %4\n").arg(pmin).arg(pmax);
Chris@38 2643 } else {
Chris@65 2644 adjPitchText = tr("Peak Pitch:\t%2\n").arg(pmin);
Chris@35 2645 }
Chris@35 2646
Chris@35 2647 } else {
Chris@35 2648
Chris@44 2649 if (!getYBinSourceRange(v, y, freqMin, freqMax)) return "";
Chris@35 2650 }
Chris@35 2651
Chris@25 2652 QString text;
Chris@25 2653
Chris@25 2654 if (rtMin != rtMax) {
Chris@25 2655 text += tr("Time:\t%1 - %2\n")
Chris@25 2656 .arg(rtMin.toText(true).c_str())
Chris@25 2657 .arg(rtMax.toText(true).c_str());
Chris@25 2658 } else {
Chris@25 2659 text += tr("Time:\t%1\n")
Chris@25 2660 .arg(rtMin.toText(true).c_str());
Chris@0 2661 }
Chris@0 2662
Chris@25 2663 if (freqMin != freqMax) {
Chris@65 2664 text += tr("%1Bin Frequency:\t%2 - %3 Hz\n%4Bin Pitch:\t%5 - %6\n")
Chris@65 2665 .arg(adjFreqText)
Chris@25 2666 .arg(freqMin)
Chris@25 2667 .arg(freqMax)
Chris@65 2668 .arg(adjPitchText)
Chris@65 2669 .arg(Pitch::getPitchLabelForFrequency(freqMin))
Chris@65 2670 .arg(Pitch::getPitchLabelForFrequency(freqMax));
Chris@65 2671 } else {
Chris@65 2672 text += tr("%1Bin Frequency:\t%2 Hz\n%3Bin Pitch:\t%4\n")
Chris@35 2673 .arg(adjFreqText)
Chris@25 2674 .arg(freqMin)
Chris@65 2675 .arg(adjPitchText)
Chris@65 2676 .arg(Pitch::getPitchLabelForFrequency(freqMin));
Chris@25 2677 }
Chris@25 2678
Chris@38 2679 if (haveValues) {
Chris@38 2680 float dbMin = AudioLevel::multiplier_to_dB(magMin);
Chris@38 2681 float dbMax = AudioLevel::multiplier_to_dB(magMax);
Chris@43 2682 QString dbMinString;
Chris@43 2683 QString dbMaxString;
Chris@43 2684 if (dbMin == AudioLevel::DB_FLOOR) {
Chris@43 2685 dbMinString = tr("-Inf");
Chris@43 2686 } else {
Chris@43 2687 dbMinString = QString("%1").arg(lrintf(dbMin));
Chris@43 2688 }
Chris@43 2689 if (dbMax == AudioLevel::DB_FLOOR) {
Chris@43 2690 dbMaxString = tr("-Inf");
Chris@43 2691 } else {
Chris@43 2692 dbMaxString = QString("%1").arg(lrintf(dbMax));
Chris@43 2693 }
Chris@25 2694 if (lrintf(dbMin) != lrintf(dbMax)) {
Chris@199 2695 text += tr("dB:\t%1 - %2").arg(dbMinString).arg(dbMaxString);
Chris@25 2696 } else {
Chris@199 2697 text += tr("dB:\t%1").arg(dbMinString);
Chris@25 2698 }
Chris@38 2699 if (phaseMin != phaseMax) {
Chris@38 2700 text += tr("\nPhase:\t%1 - %2").arg(phaseMin).arg(phaseMax);
Chris@38 2701 } else {
Chris@38 2702 text += tr("\nPhase:\t%1").arg(phaseMin);
Chris@38 2703 }
Chris@25 2704 }
Chris@25 2705
Chris@25 2706 return text;
Chris@0 2707 }
Chris@25 2708
Chris@0 2709 int
Chris@40 2710 SpectrogramLayer::getColourScaleWidth(QPainter &paint) const
Chris@40 2711 {
Chris@40 2712 int cw;
Chris@40 2713
Chris@119 2714 cw = paint.fontMetrics().width("-80dB");
Chris@119 2715
Chris@40 2716 return cw;
Chris@40 2717 }
Chris@40 2718
Chris@40 2719 int
Chris@248 2720 SpectrogramLayer::getVerticalScaleWidth(View *, QPainter &paint) const
Chris@0 2721 {
Chris@0 2722 if (!m_model || !m_model->isOK()) return 0;
Chris@0 2723
Chris@40 2724 int cw = getColourScaleWidth(paint);
Chris@40 2725
Chris@0 2726 int tw = paint.fontMetrics().width(QString("%1")
Chris@0 2727 .arg(m_maxFrequency > 0 ?
Chris@0 2728 m_maxFrequency - 1 :
Chris@0 2729 m_model->getSampleRate() / 2));
Chris@0 2730
Chris@234 2731 int fw = paint.fontMetrics().width(tr("43Hz"));
Chris@0 2732 if (tw < fw) tw = fw;
Chris@40 2733
Chris@40 2734 int tickw = (m_frequencyScale == LogFrequencyScale ? 10 : 4);
Chris@0 2735
Chris@40 2736 return cw + tickw + tw + 13;
Chris@0 2737 }
Chris@0 2738
Chris@0 2739 void
Chris@44 2740 SpectrogramLayer::paintVerticalScale(View *v, QPainter &paint, QRect rect) const
Chris@0 2741 {
Chris@0 2742 if (!m_model || !m_model->isOK()) {
Chris@0 2743 return;
Chris@0 2744 }
Chris@0 2745
Chris@161 2746 Profiler profiler("SpectrogramLayer::paintVerticalScale", true);
Chris@122 2747
Chris@120 2748 //!!! cache this?
Chris@120 2749
Chris@0 2750 int h = rect.height(), w = rect.width();
Chris@0 2751
Chris@40 2752 int tickw = (m_frequencyScale == LogFrequencyScale ? 10 : 4);
Chris@40 2753 int pkw = (m_frequencyScale == LogFrequencyScale ? 10 : 0);
Chris@40 2754
Chris@107 2755 size_t bins = m_fftSize / 2;
Chris@0 2756 int sr = m_model->getSampleRate();
Chris@0 2757
Chris@0 2758 if (m_maxFrequency > 0) {
Chris@107 2759 bins = int((double(m_maxFrequency) * m_fftSize) / sr + 0.1);
Chris@107 2760 if (bins > m_fftSize / 2) bins = m_fftSize / 2;
Chris@0 2761 }
Chris@0 2762
Chris@40 2763 int cw = getColourScaleWidth(paint);
Chris@119 2764 int cbw = paint.fontMetrics().width("dB");
Chris@40 2765
Chris@0 2766 int py = -1;
Chris@0 2767 int textHeight = paint.fontMetrics().height();
Chris@0 2768 int toff = -textHeight + paint.fontMetrics().ascent() + 2;
Chris@0 2769
Chris@119 2770 if (h > textHeight * 3 + 10) {
Chris@119 2771
Chris@119 2772 int topLines = 2;
Chris@119 2773 if (m_colourScale == PhaseColourScale) topLines = 1;
Chris@119 2774
Chris@119 2775 int ch = h - textHeight * (topLines + 1) - 8;
Chris@119 2776 // paint.drawRect(4, textHeight + 4, cw - 1, ch + 1);
Chris@119 2777 paint.drawRect(4 + cw - cbw, textHeight * topLines + 4, cbw - 1, ch + 1);
Chris@40 2778
Chris@40 2779 QString top, bottom;
Chris@119 2780 float min = m_viewMags[v].getMin();
Chris@119 2781 float max = m_viewMags[v].getMax();
Chris@119 2782
Chris@119 2783 float dBmin = AudioLevel::multiplier_to_dB(min);
Chris@119 2784 float dBmax = AudioLevel::multiplier_to_dB(max);
Chris@119 2785
Chris@120 2786 if (dBmax < -60.f) dBmax = -60.f;
Chris@120 2787 else top = QString("%1").arg(lrintf(dBmax));
Chris@120 2788
Chris@120 2789 if (dBmin < dBmax - 60.f) dBmin = dBmax - 60.f;
Chris@119 2790 bottom = QString("%1").arg(lrintf(dBmin));
Chris@119 2791
Chris@119 2792 //!!! & phase etc
Chris@119 2793
Chris@119 2794 if (m_colourScale != PhaseColourScale) {
Chris@119 2795 paint.drawText((cw + 6 - paint.fontMetrics().width("dBFS")) / 2,
Chris@119 2796 2 + textHeight + toff, "dBFS");
Chris@119 2797 }
Chris@119 2798
Chris@119 2799 // paint.drawText((cw + 6 - paint.fontMetrics().width(top)) / 2,
Chris@119 2800 paint.drawText(3 + cw - cbw - paint.fontMetrics().width(top),
Chris@119 2801 2 + textHeight * topLines + toff + textHeight/2, top);
Chris@119 2802
Chris@119 2803 paint.drawText(3 + cw - cbw - paint.fontMetrics().width(bottom),
Chris@119 2804 h + toff - 3 - textHeight/2, bottom);
Chris@40 2805
Chris@40 2806 paint.save();
Chris@40 2807 paint.setBrush(Qt::NoBrush);
Chris@119 2808
Chris@119 2809 int lasty = 0;
Chris@119 2810 int lastdb = 0;
Chris@119 2811
Chris@40 2812 for (int i = 0; i < ch; ++i) {
Chris@119 2813
Chris@119 2814 float dBval = dBmin + (((dBmax - dBmin) * i) / (ch - 1));
Chris@119 2815 int idb = int(dBval);
Chris@119 2816
Chris@119 2817 float value = AudioLevel::dB_to_multiplier(dBval);
Chris@119 2818 int colour = getDisplayValue(v, value * m_gain);
Chris@210 2819
Chris@197 2820 paint.setPen(m_palette.getColour(colour));
Chris@119 2821
Chris@119 2822 int y = textHeight * topLines + 4 + ch - i;
Chris@119 2823
Chris@119 2824 paint.drawLine(5 + cw - cbw, y, cw + 2, y);
Chris@119 2825
Chris@119 2826 if (i == 0) {
Chris@119 2827 lasty = y;
Chris@119 2828 lastdb = idb;
Chris@119 2829 } else if (i < ch - paint.fontMetrics().ascent() &&
Chris@120 2830 idb != lastdb &&
Chris@119 2831 ((abs(y - lasty) > textHeight &&
Chris@119 2832 idb % 10 == 0) ||
Chris@119 2833 (abs(y - lasty) > paint.fontMetrics().ascent() &&
Chris@119 2834 idb % 5 == 0))) {
Chris@287 2835 paint.setPen(v->getBackground());
Chris@119 2836 QString text = QString("%1").arg(idb);
Chris@119 2837 paint.drawText(3 + cw - cbw - paint.fontMetrics().width(text),
Chris@119 2838 y + toff + textHeight/2, text);
Chris@287 2839 paint.setPen(v->getForeground());
Chris@119 2840 paint.drawLine(5 + cw - cbw, y, 8 + cw - cbw, y);
Chris@119 2841 lasty = y;
Chris@119 2842 lastdb = idb;
Chris@119 2843 }
Chris@40 2844 }
Chris@40 2845 paint.restore();
Chris@40 2846 }
Chris@40 2847
Chris@40 2848 paint.drawLine(cw + 7, 0, cw + 7, h);
Chris@40 2849
Chris@0 2850 int bin = -1;
Chris@0 2851
Chris@44 2852 for (int y = 0; y < v->height(); ++y) {
Chris@0 2853
Chris@0 2854 float q0, q1;
Chris@44 2855 if (!getYBinRange(v, v->height() - y, q0, q1)) continue;
Chris@0 2856
Chris@0 2857 int vy;
Chris@0 2858
Chris@0 2859 if (int(q0) > bin) {
Chris@0 2860 vy = y;
Chris@0 2861 bin = int(q0);
Chris@0 2862 } else {
Chris@0 2863 continue;
Chris@0 2864 }
Chris@0 2865
Chris@107 2866 int freq = (sr * bin) / m_fftSize;
Chris@0 2867
Chris@0 2868 if (py >= 0 && (vy - py) < textHeight - 1) {
Chris@40 2869 if (m_frequencyScale == LinearFrequencyScale) {
Chris@40 2870 paint.drawLine(w - tickw, h - vy, w, h - vy);
Chris@40 2871 }
Chris@0 2872 continue;
Chris@0 2873 }
Chris@0 2874
Chris@0 2875 QString text = QString("%1").arg(freq);
Chris@234 2876 if (bin == 1) text = tr("%1Hz").arg(freq); // bin 0 is DC
Chris@40 2877 paint.drawLine(cw + 7, h - vy, w - pkw - 1, h - vy);
Chris@0 2878
Chris@0 2879 if (h - vy - textHeight >= -2) {
Chris@40 2880 int tx = w - 3 - paint.fontMetrics().width(text) - std::max(tickw, pkw);
Chris@0 2881 paint.drawText(tx, h - vy + toff, text);
Chris@0 2882 }
Chris@0 2883
Chris@0 2884 py = vy;
Chris@0 2885 }
Chris@40 2886
Chris@40 2887 if (m_frequencyScale == LogFrequencyScale) {
Chris@40 2888
Chris@277 2889 // piano keyboard
Chris@277 2890
Chris@40 2891 paint.drawLine(w - pkw - 1, 0, w - pkw - 1, h);
Chris@40 2892
Chris@40 2893 float minf = getEffectiveMinFrequency();
Chris@40 2894 float maxf = getEffectiveMaxFrequency();
Chris@40 2895
Chris@122 2896 int py = h, ppy = h;
Chris@40 2897 paint.setBrush(paint.pen().color());
Chris@40 2898
Chris@40 2899 for (int i = 0; i < 128; ++i) {
Chris@40 2900
Chris@40 2901 float f = Pitch::getFrequencyForPitch(i);
Chris@44 2902 int y = lrintf(v->getYForFrequency(f, minf, maxf, true));
Chris@122 2903
Chris@122 2904 if (y < -2) break;
Chris@122 2905 if (y > h + 2) {
Chris@122 2906 continue;
Chris@122 2907 }
Chris@122 2908
Chris@40 2909 int n = (i % 12);
Chris@122 2910
Chris@122 2911 if (n == 1) {
Chris@122 2912 // C# -- fill the C from here
Chris@284 2913 QColor col = Qt::gray;
Chris@284 2914 if (i == 61) { // filling middle C
Chris@284 2915 col = Qt::blue;
Chris@284 2916 col = col.light(150);
Chris@284 2917 }
Chris@122 2918 if (ppy - y > 2) {
Chris@122 2919 paint.fillRect(w - pkw,
Chris@122 2920 y,
Chris@122 2921 pkw,
Chris@122 2922 (py + ppy) / 2 - y,
Chris@284 2923 col);
Chris@122 2924 }
Chris@122 2925 }
Chris@122 2926
Chris@40 2927 if (n == 1 || n == 3 || n == 6 || n == 8 || n == 10) {
Chris@40 2928 // black notes
Chris@40 2929 paint.drawLine(w - pkw, y, w, y);
Chris@41 2930 int rh = ((py - y) / 4) * 2;
Chris@41 2931 if (rh < 2) rh = 2;
Chris@41 2932 paint.drawRect(w - pkw, y - (py-y)/4, pkw/2, rh);
Chris@40 2933 } else if (n == 0 || n == 5) {
Chris@122 2934 // C, F
Chris@40 2935 if (py < h) {
Chris@40 2936 paint.drawLine(w - pkw, (y + py) / 2, w, (y + py) / 2);
Chris@40 2937 }
Chris@40 2938 }
Chris@40 2939
Chris@122 2940 ppy = py;
Chris@40 2941 py = y;
Chris@40 2942 }
Chris@40 2943 }
Chris@0 2944 }
Chris@0 2945
Chris@187 2946 class SpectrogramRangeMapper : public RangeMapper
Chris@187 2947 {
Chris@187 2948 public:
Chris@248 2949 SpectrogramRangeMapper(int sr, int /* fftsize */) :
Chris@187 2950 m_dist(float(sr) / 2),
Chris@187 2951 m_s2(sqrtf(sqrtf(2))) { }
Chris@187 2952 ~SpectrogramRangeMapper() { }
Chris@187 2953
Chris@187 2954 virtual int getPositionForValue(float value) const {
Chris@187 2955
Chris@187 2956 float dist = m_dist;
Chris@187 2957
Chris@187 2958 int n = 0;
Chris@187 2959
Chris@187 2960 while (dist > (value + 0.00001) && dist > 0.1f) {
Chris@187 2961 dist /= m_s2;
Chris@187 2962 ++n;
Chris@187 2963 }
Chris@187 2964
Chris@187 2965 return n;
Chris@187 2966 }
Chris@187 2967
Chris@187 2968 virtual float getValueForPosition(int position) const {
Chris@187 2969
Chris@187 2970 // Vertical zoom step 0 shows the entire range from DC ->
Chris@187 2971 // Nyquist frequency. Step 1 shows 2^(1/4) of the range of
Chris@187 2972 // step 0, and so on until the visible range is smaller than
Chris@187 2973 // the frequency step between bins at the current fft size.
Chris@187 2974
Chris@187 2975 float dist = m_dist;
Chris@187 2976
Chris@187 2977 int n = 0;
Chris@187 2978 while (n < position) {
Chris@187 2979 dist /= m_s2;
Chris@187 2980 ++n;
Chris@187 2981 }
Chris@187 2982
Chris@187 2983 return dist;
Chris@187 2984 }
Chris@187 2985
Chris@187 2986 virtual QString getUnit() const { return "Hz"; }
Chris@187 2987
Chris@187 2988 protected:
Chris@187 2989 float m_dist;
Chris@187 2990 float m_s2;
Chris@187 2991 };
Chris@187 2992
Chris@133 2993 int
Chris@133 2994 SpectrogramLayer::getVerticalZoomSteps(int &defaultStep) const
Chris@133 2995 {
Chris@135 2996 if (!m_model) return 0;
Chris@187 2997
Chris@187 2998 int sr = m_model->getSampleRate();
Chris@187 2999
Chris@187 3000 SpectrogramRangeMapper mapper(sr, m_fftSize);
Chris@187 3001
Chris@187 3002 // int maxStep = mapper.getPositionForValue((float(sr) / m_fftSize) + 0.001);
Chris@187 3003 int maxStep = mapper.getPositionForValue(0);
Chris@187 3004 int minStep = mapper.getPositionForValue(float(sr) / 2);
Chris@250 3005
Chris@250 3006 size_t initialMax = m_initialMaxFrequency;
Chris@250 3007 if (initialMax == 0) initialMax = sr / 2;
Chris@250 3008
Chris@250 3009 defaultStep = mapper.getPositionForValue(initialMax) - minStep;
Chris@250 3010
Chris@250 3011 // std::cerr << "SpectrogramLayer::getVerticalZoomSteps: " << maxStep - minStep << " (" << maxStep <<"-" << minStep << "), default is " << defaultStep << " (from initial max freq " << initialMax << ")" << std::endl;
Chris@187 3012
Chris@187 3013 return maxStep - minStep;
Chris@133 3014 }
Chris@133 3015
Chris@133 3016 int
Chris@133 3017 SpectrogramLayer::getCurrentVerticalZoomStep() const
Chris@133 3018 {
Chris@133 3019 if (!m_model) return 0;
Chris@133 3020
Chris@133 3021 float dmin, dmax;
Chris@133 3022 getDisplayExtents(dmin, dmax);
Chris@133 3023
Chris@187 3024 SpectrogramRangeMapper mapper(m_model->getSampleRate(), m_fftSize);
Chris@187 3025 int n = mapper.getPositionForValue(dmax - dmin);
Chris@248 3026 // std::cerr << "SpectrogramLayer::getCurrentVerticalZoomStep: " << n << std::endl;
Chris@133 3027 return n;
Chris@133 3028 }
Chris@133 3029
Chris@133 3030 void
Chris@133 3031 SpectrogramLayer::setVerticalZoomStep(int step)
Chris@133 3032 {
Chris@187 3033 if (!m_model) return;
Chris@187 3034
Chris@253 3035 float dmin = m_minFrequency, dmax = m_maxFrequency;
Chris@253 3036 // getDisplayExtents(dmin, dmax);
Chris@253 3037
Chris@253 3038 // std::cerr << "current range " << dmin << " -> " << dmax << ", range " << dmax-dmin << ", mid " << (dmax + dmin)/2 << std::endl;
Chris@133 3039
Chris@133 3040 int sr = m_model->getSampleRate();
Chris@187 3041 SpectrogramRangeMapper mapper(sr, m_fftSize);
Chris@253 3042 float newdist = mapper.getValueForPosition(step);
Chris@253 3043
Chris@253 3044 float newmin, newmax;
Chris@253 3045
Chris@253 3046 if (m_frequencyScale == LogFrequencyScale) {
Chris@253 3047
Chris@253 3048 // need to pick newmin and newmax such that
Chris@253 3049 //
Chris@253 3050 // (log(newmin) + log(newmax)) / 2 == logmid
Chris@253 3051 // and
Chris@253 3052 // newmax - newmin = newdist
Chris@253 3053 //
Chris@253 3054 // so log(newmax - newdist) + log(newmax) == 2logmid
Chris@253 3055 // log(newmax(newmax - newdist)) == 2logmid
Chris@253 3056 // newmax.newmax - newmax.newdist == exp(2logmid)
Chris@253 3057 // newmax^2 + (-newdist)newmax + -exp(2logmid) == 0
Chris@253 3058 // quadratic with a = 1, b = -newdist, c = -exp(2logmid), all known
Chris@253 3059 //
Chris@253 3060 // positive root
Chris@253 3061 // newmax = (newdist + sqrt(newdist^2 + 4exp(2logmid))) / 2
Chris@253 3062 //
Chris@253 3063 // but logmid = (log(dmin) + log(dmax)) / 2
Chris@253 3064 // so exp(2logmid) = exp(log(dmin) + log(dmax))
Chris@253 3065 // = exp(log(dmin.dmax))
Chris@253 3066 // = dmin.dmax
Chris@253 3067 // so newmax = (newdist + sqrtf(newdist^2 + 4dmin.dmax)) / 2
Chris@253 3068
Chris@253 3069 newmax = (newdist + sqrtf(newdist*newdist + 4*dmin*dmax)) / 2;
Chris@253 3070 newmin = newmax - newdist;
Chris@253 3071
Chris@253 3072 // std::cerr << "newmin = " << newmin << ", newmax = " << newmax << std::endl;
Chris@253 3073
Chris@253 3074 } else {
Chris@253 3075 float dmid = (dmax + dmin) / 2;
Chris@253 3076 newmin = dmid - newdist / 2;
Chris@253 3077 newmax = dmid + newdist / 2;
Chris@253 3078 }
Chris@187 3079
Chris@187 3080 float mmin, mmax;
Chris@187 3081 mmin = 0;
Chris@187 3082 mmax = float(sr) / 2;
Chris@133 3083
Chris@187 3084 if (newmin < mmin) {
Chris@187 3085 newmax += (mmin - newmin);
Chris@187 3086 newmin = mmin;
Chris@187 3087 }
Chris@187 3088 if (newmax > mmax) {
Chris@187 3089 newmax = mmax;
Chris@187 3090 }
Chris@133 3091
Chris@253 3092 // std::cerr << "SpectrogramLayer::setVerticalZoomStep: " << step << ": " << newmin << " -> " << newmax << " (range " << newdist << ")" << std::endl;
Chris@253 3093
Chris@253 3094 setMinFrequency(lrintf(newmin));
Chris@253 3095 setMaxFrequency(lrintf(newmax));
Chris@187 3096 }
Chris@187 3097
Chris@187 3098 RangeMapper *
Chris@187 3099 SpectrogramLayer::getNewVerticalZoomRangeMapper() const
Chris@187 3100 {
Chris@187 3101 if (!m_model) return 0;
Chris@187 3102 return new SpectrogramRangeMapper(m_model->getSampleRate(), m_fftSize);
Chris@133 3103 }
Chris@133 3104
Chris@273 3105 void
Chris@273 3106 SpectrogramLayer::updateMeasureRectYCoords(View *v, const MeasureRect &r) const
Chris@273 3107 {
Chris@273 3108 int y0 = 0;
Chris@273 3109 if (r.startY > 0.0) y0 = getYForFrequency(v, r.startY);
Chris@273 3110
Chris@273 3111 int y1 = y0;
Chris@273 3112 if (r.endY > 0.0) y1 = getYForFrequency(v, r.endY);
Chris@273 3113
Chris@273 3114 // std::cerr << "SpectrogramLayer::updateMeasureRectYCoords: start " << r.startY << " -> " << y0 << ", end " << r.endY << " -> " << y1 << std::endl;
Chris@273 3115
Chris@273 3116 r.pixrect = QRect(r.pixrect.x(), y0, r.pixrect.width(), y1 - y0);
Chris@273 3117 }
Chris@273 3118
Chris@273 3119 void
Chris@273 3120 SpectrogramLayer::setMeasureRectYCoord(View *v, MeasureRect &r, bool start, int y) const
Chris@273 3121 {
Chris@273 3122 if (start) {
Chris@273 3123 r.startY = getFrequencyForY(v, y);
Chris@273 3124 r.endY = r.startY;
Chris@273 3125 } else {
Chris@273 3126 r.endY = getFrequencyForY(v, y);
Chris@273 3127 }
Chris@273 3128 // std::cerr << "SpectrogramLayer::setMeasureRectYCoord: start " << r.startY << " <- " << y << ", end " << r.endY << " <- " << y << std::endl;
Chris@273 3129
Chris@273 3130 }
Chris@273 3131
Chris@316 3132 void
Chris@316 3133 SpectrogramLayer::toXml(QTextStream &stream,
Chris@316 3134 QString indent, QString extraAttributes) const
Chris@6 3135 {
Chris@6 3136 QString s;
Chris@6 3137
Chris@6 3138 s += QString("channel=\"%1\" "
Chris@6 3139 "windowSize=\"%2\" "
Chris@153 3140 "windowHopLevel=\"%3\" "
Chris@153 3141 "gain=\"%4\" "
Chris@153 3142 "threshold=\"%5\" ")
Chris@6 3143 .arg(m_channel)
Chris@6 3144 .arg(m_windowSize)
Chris@97 3145 .arg(m_windowHopLevel)
Chris@37 3146 .arg(m_gain)
Chris@37 3147 .arg(m_threshold);
Chris@37 3148
Chris@37 3149 s += QString("minFrequency=\"%1\" "
Chris@37 3150 "maxFrequency=\"%2\" "
Chris@37 3151 "colourScale=\"%3\" "
Chris@37 3152 "colourScheme=\"%4\" "
Chris@37 3153 "colourRotation=\"%5\" "
Chris@37 3154 "frequencyScale=\"%6\" "
Chris@37 3155 "binDisplay=\"%7\" "
Chris@153 3156 "normalizeColumns=\"%8\" "
Chris@153 3157 "normalizeVisibleArea=\"%9\"")
Chris@37 3158 .arg(m_minFrequency)
Chris@6 3159 .arg(m_maxFrequency)
Chris@6 3160 .arg(m_colourScale)
Chris@197 3161 .arg(m_colourMap)
Chris@37 3162 .arg(m_colourRotation)
Chris@35 3163 .arg(m_frequencyScale)
Chris@37 3164 .arg(m_binDisplay)
Chris@153 3165 .arg(m_normalizeColumns ? "true" : "false")
Chris@153 3166 .arg(m_normalizeVisibleArea ? "true" : "false");
Chris@6 3167
Chris@316 3168 Layer::toXml(stream, indent, extraAttributes + " " + s);
Chris@6 3169 }
Chris@6 3170
Chris@11 3171 void
Chris@11 3172 SpectrogramLayer::setProperties(const QXmlAttributes &attributes)
Chris@11 3173 {
Chris@11 3174 bool ok = false;
Chris@11 3175
Chris@11 3176 int channel = attributes.value("channel").toInt(&ok);
Chris@11 3177 if (ok) setChannel(channel);
Chris@11 3178
Chris@11 3179 size_t windowSize = attributes.value("windowSize").toUInt(&ok);
Chris@11 3180 if (ok) setWindowSize(windowSize);
Chris@11 3181
Chris@97 3182 size_t windowHopLevel = attributes.value("windowHopLevel").toUInt(&ok);
Chris@97 3183 if (ok) setWindowHopLevel(windowHopLevel);
Chris@97 3184 else {
Chris@97 3185 size_t windowOverlap = attributes.value("windowOverlap").toUInt(&ok);
Chris@97 3186 // a percentage value
Chris@97 3187 if (ok) {
Chris@97 3188 if (windowOverlap == 0) setWindowHopLevel(0);
Chris@97 3189 else if (windowOverlap == 25) setWindowHopLevel(1);
Chris@97 3190 else if (windowOverlap == 50) setWindowHopLevel(2);
Chris@97 3191 else if (windowOverlap == 75) setWindowHopLevel(3);
Chris@97 3192 else if (windowOverlap == 90) setWindowHopLevel(4);
Chris@97 3193 }
Chris@97 3194 }
Chris@11 3195
Chris@11 3196 float gain = attributes.value("gain").toFloat(&ok);
Chris@11 3197 if (ok) setGain(gain);
Chris@11 3198
Chris@37 3199 float threshold = attributes.value("threshold").toFloat(&ok);
Chris@37 3200 if (ok) setThreshold(threshold);
Chris@37 3201
Chris@37 3202 size_t minFrequency = attributes.value("minFrequency").toUInt(&ok);
Chris@187 3203 if (ok) {
Chris@187 3204 std::cerr << "SpectrogramLayer::setProperties: setting min freq to " << minFrequency << std::endl;
Chris@187 3205 setMinFrequency(minFrequency);
Chris@187 3206 }
Chris@37 3207
Chris@11 3208 size_t maxFrequency = attributes.value("maxFrequency").toUInt(&ok);
Chris@187 3209 if (ok) {
Chris@187 3210 std::cerr << "SpectrogramLayer::setProperties: setting max freq to " << maxFrequency << std::endl;
Chris@187 3211 setMaxFrequency(maxFrequency);
Chris@187 3212 }
Chris@11 3213
Chris@11 3214 ColourScale colourScale = (ColourScale)
Chris@11 3215 attributes.value("colourScale").toInt(&ok);
Chris@11 3216 if (ok) setColourScale(colourScale);
Chris@11 3217
Chris@197 3218 int colourMap = attributes.value("colourScheme").toInt(&ok);
Chris@197 3219 if (ok) setColourMap(colourMap);
Chris@11 3220
Chris@37 3221 int colourRotation = attributes.value("colourRotation").toInt(&ok);
Chris@37 3222 if (ok) setColourRotation(colourRotation);
Chris@37 3223
Chris@11 3224 FrequencyScale frequencyScale = (FrequencyScale)
Chris@11 3225 attributes.value("frequencyScale").toInt(&ok);
Chris@11 3226 if (ok) setFrequencyScale(frequencyScale);
Chris@35 3227
Chris@37 3228 BinDisplay binDisplay = (BinDisplay)
Chris@37 3229 attributes.value("binDisplay").toInt(&ok);
Chris@37 3230 if (ok) setBinDisplay(binDisplay);
Chris@36 3231
Chris@36 3232 bool normalizeColumns =
Chris@36 3233 (attributes.value("normalizeColumns").trimmed() == "true");
Chris@36 3234 setNormalizeColumns(normalizeColumns);
Chris@153 3235
Chris@153 3236 bool normalizeVisibleArea =
Chris@153 3237 (attributes.value("normalizeVisibleArea").trimmed() == "true");
Chris@153 3238 setNormalizeVisibleArea(normalizeVisibleArea);
Chris@11 3239 }
Chris@11 3240