annotate layer/SpectrogramLayer.cpp @ 170:bdba24db0446

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