annotate layer/SpectrogramLayer.cpp @ 131:eaae73b6bd28

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