annotate layer/SpectrogramLayer.cpp @ 96:095916d7ed4d

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