annotate layer/SpectrogramLayer.cpp @ 109:12340cb6e6cb

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