annotate layer/SpectrogramLayer.cpp @ 121:7363cacf7de0

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