annotate layer/SpectrogramLayer.cpp @ 104:1348818e7be7

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