annotate layer/SpectrogramLayer.cpp @ 117:782142eaaa84

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