annotate layer/SpectrogramLayer.cpp @ 135:e98130764635

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