annotate layer/SpectrogramLayer.cpp @ 134:13949a6970ab

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