annotate layer/SpectrogramLayer.cpp @ 119:508276c923ba

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