annotate layer/SpectrogramLayer.cpp @ 25:dcdb21b62dbb

* Refactor sparse models. Previously the 1D and time-value models duplicated a lot of code; now there is a base class (SparseModel) templated on the stored point type, and the subclasses define point types with the necessary characteristics. * Add NoteModel, a new SparseModel subclass. * Reorganise local feature description display. Instead of asking the layer to draw its own, just query it for a textual description and draw that in Pane. Greatly simplifies this part of the layer code. * Add local feature descriptions to colour 3D plot and waveform layers. * Add pitch in MIDI-pitch-and-cents to spectrogram layer. * Give AudioGenerator its own mutex to shorten lock times in CallbackPlaySource. * Minor adjustments to layers menu &c
author Chris Cannam
date Thu, 02 Feb 2006 16:10:19 +0000
parents 6b794a2af3d9
children 202d1dca67d2
rev   line source
Chris@0 1 /* -*- c-basic-offset: 4 -*- vi:set ts=8 sts=4 sw=4: */
Chris@0 2
Chris@0 3 /*
Chris@0 4 A waveform viewer and audio annotation editor.
Chris@5 5 Chris Cannam, Queen Mary University of London, 2005-2006
Chris@0 6
Chris@0 7 This is experimental software. Not for distribution.
Chris@0 8 */
Chris@0 9
Chris@0 10 #include "SpectrogramLayer.h"
Chris@0 11
Chris@0 12 #include "base/View.h"
Chris@0 13 #include "base/Profiler.h"
Chris@0 14 #include "base/AudioLevel.h"
Chris@0 15 #include "base/Window.h"
Chris@24 16 #include "base/Pitch.h"
Chris@0 17
Chris@0 18 #include <QPainter>
Chris@0 19 #include <QImage>
Chris@0 20 #include <QPixmap>
Chris@0 21 #include <QRect>
Chris@0 22 #include <QTimer>
Chris@0 23
Chris@0 24 #include <iostream>
Chris@0 25
Chris@0 26 #include <cassert>
Chris@0 27 #include <cmath>
Chris@0 28
Chris@0 29 //#define DEBUG_SPECTROGRAM_REPAINT 1
Chris@0 30
Chris@0 31
Chris@0 32 SpectrogramLayer::SpectrogramLayer(View *w, Configuration config) :
Chris@0 33 Layer(w),
Chris@0 34 m_model(0),
Chris@0 35 m_channel(0),
Chris@0 36 m_windowSize(1024),
Chris@0 37 m_windowType(HanningWindow),
Chris@0 38 m_windowOverlap(50),
Chris@0 39 m_gain(1.0),
Chris@9 40 m_colourRotation(0),
Chris@0 41 m_maxFrequency(8000),
Chris@0 42 m_colourScale(dBColourScale),
Chris@0 43 m_colourScheme(DefaultColours),
Chris@0 44 m_frequencyScale(LinearFrequencyScale),
Chris@0 45 m_cache(0),
Chris@0 46 m_cacheInvalid(true),
Chris@0 47 m_pixmapCache(0),
Chris@0 48 m_pixmapCacheInvalid(true),
Chris@0 49 m_fillThread(0),
Chris@0 50 m_updateTimer(0),
Chris@0 51 m_lastFillExtent(0),
Chris@0 52 m_exiting(false)
Chris@0 53 {
Chris@0 54 if (config == MelodicRange) {
Chris@0 55 setWindowSize(8192);
Chris@0 56 setWindowOverlap(90);
Chris@0 57 setWindowType(ParzenWindow);
Chris@0 58 setMaxFrequency(1000);
Chris@0 59 setColourScale(LinearColourScale);
Chris@0 60 }
Chris@0 61
Chris@0 62 if (m_view) m_view->setLightBackground(false);
Chris@0 63 m_view->addLayer(this);
Chris@0 64 }
Chris@0 65
Chris@0 66 SpectrogramLayer::~SpectrogramLayer()
Chris@0 67 {
Chris@0 68 delete m_updateTimer;
Chris@0 69 m_updateTimer = 0;
Chris@0 70
Chris@0 71 m_exiting = true;
Chris@0 72 m_condition.wakeAll();
Chris@0 73 if (m_fillThread) m_fillThread->wait();
Chris@0 74 delete m_fillThread;
Chris@0 75
Chris@0 76 delete m_cache;
Chris@0 77 }
Chris@0 78
Chris@0 79 void
Chris@0 80 SpectrogramLayer::setModel(const DenseTimeValueModel *model)
Chris@0 81 {
Chris@0 82 m_mutex.lock();
Chris@0 83 m_model = model;
Chris@0 84 m_mutex.unlock();
Chris@0 85
Chris@0 86 if (!m_model || !m_model->isOK()) return;
Chris@0 87
Chris@0 88 connect(m_model, SIGNAL(modelChanged()), this, SIGNAL(modelChanged()));
Chris@0 89 connect(m_model, SIGNAL(modelChanged(size_t, size_t)),
Chris@0 90 this, SIGNAL(modelChanged(size_t, size_t)));
Chris@0 91
Chris@0 92 connect(m_model, SIGNAL(completionChanged()),
Chris@0 93 this, SIGNAL(modelCompletionChanged()));
Chris@0 94
Chris@0 95 connect(m_model, SIGNAL(modelChanged()), this, SLOT(cacheInvalid()));
Chris@0 96 connect(m_model, SIGNAL(modelChanged(size_t, size_t)),
Chris@0 97 this, SLOT(cacheInvalid(size_t, size_t)));
Chris@0 98
Chris@0 99 emit modelReplaced();
Chris@0 100 fillCache();
Chris@0 101 }
Chris@0 102
Chris@0 103 Layer::PropertyList
Chris@0 104 SpectrogramLayer::getProperties() const
Chris@0 105 {
Chris@0 106 PropertyList list;
Chris@0 107 list.push_back(tr("Colour"));
Chris@0 108 list.push_back(tr("Colour Scale"));
Chris@0 109 list.push_back(tr("Window Type"));
Chris@0 110 list.push_back(tr("Window Size"));
Chris@0 111 list.push_back(tr("Window Overlap"));
Chris@0 112 list.push_back(tr("Gain"));
Chris@9 113 list.push_back(tr("Colour Rotation"));
Chris@0 114 list.push_back(tr("Max Frequency"));
Chris@0 115 list.push_back(tr("Frequency Scale"));
Chris@0 116 return list;
Chris@0 117 }
Chris@0 118
Chris@0 119 Layer::PropertyType
Chris@0 120 SpectrogramLayer::getPropertyType(const PropertyName &name) const
Chris@0 121 {
Chris@0 122 if (name == tr("Gain")) return RangeProperty;
Chris@9 123 if (name == tr("Colour Rotation")) return RangeProperty;
Chris@0 124 return ValueProperty;
Chris@0 125 }
Chris@0 126
Chris@0 127 QString
Chris@0 128 SpectrogramLayer::getPropertyGroupName(const PropertyName &name) const
Chris@0 129 {
Chris@0 130 if (name == tr("Window Size") ||
Chris@0 131 name == tr("Window Overlap")) return tr("Window");
Chris@0 132 if (name == tr("Gain") ||
Chris@9 133 name == tr("Colour Rotation") ||
Chris@0 134 name == tr("Colour Scale")) return tr("Scale");
Chris@0 135 if (name == tr("Max Frequency") ||
Chris@0 136 name == tr("Frequency Scale")) return tr("Frequency");
Chris@0 137 return QString();
Chris@0 138 }
Chris@0 139
Chris@0 140 int
Chris@0 141 SpectrogramLayer::getPropertyRangeAndValue(const PropertyName &name,
Chris@0 142 int *min, int *max) const
Chris@0 143 {
Chris@0 144 int deft = 0;
Chris@0 145
Chris@10 146 int throwaway;
Chris@10 147 if (!min) min = &throwaway;
Chris@10 148 if (!max) max = &throwaway;
Chris@10 149
Chris@0 150 if (name == tr("Gain")) {
Chris@0 151
Chris@0 152 *min = -50;
Chris@0 153 *max = 50;
Chris@0 154
Chris@0 155 deft = lrint(log10(m_gain) * 20.0);
Chris@0 156 if (deft < *min) deft = *min;
Chris@0 157 if (deft > *max) deft = *max;
Chris@0 158
Chris@9 159 } else if (name == tr("Colour Rotation")) {
Chris@9 160
Chris@9 161 *min = 0;
Chris@9 162 *max = 256;
Chris@9 163
Chris@9 164 deft = m_colourRotation;
Chris@9 165
Chris@0 166 } else if (name == tr("Colour Scale")) {
Chris@0 167
Chris@0 168 *min = 0;
Chris@0 169 *max = 3;
Chris@0 170
Chris@0 171 deft = (int)m_colourScale;
Chris@0 172
Chris@0 173 } else if (name == tr("Colour")) {
Chris@0 174
Chris@0 175 *min = 0;
Chris@0 176 *max = 5;
Chris@0 177
Chris@0 178 deft = (int)m_colourScheme;
Chris@0 179
Chris@0 180 } else if (name == tr("Window Type")) {
Chris@0 181
Chris@0 182 *min = 0;
Chris@0 183 *max = 6;
Chris@0 184
Chris@0 185 deft = (int)m_windowType;
Chris@0 186
Chris@0 187 } else if (name == tr("Window Size")) {
Chris@0 188
Chris@0 189 *min = 0;
Chris@0 190 *max = 10;
Chris@0 191
Chris@0 192 deft = 0;
Chris@0 193 int ws = m_windowSize;
Chris@0 194 while (ws > 32) { ws >>= 1; deft ++; }
Chris@0 195
Chris@0 196 } else if (name == tr("Window Overlap")) {
Chris@0 197
Chris@0 198 *min = 0;
Chris@0 199 *max = 4;
Chris@0 200
Chris@0 201 deft = m_windowOverlap / 25;
Chris@0 202 if (m_windowOverlap == 90) deft = 4;
Chris@0 203
Chris@0 204 } else if (name == tr("Max Frequency")) {
Chris@0 205
Chris@0 206 *min = 0;
Chris@0 207 *max = 9;
Chris@0 208
Chris@0 209 switch (m_maxFrequency) {
Chris@0 210 case 500: deft = 0; break;
Chris@0 211 case 1000: deft = 1; break;
Chris@0 212 case 1500: deft = 2; break;
Chris@0 213 case 2000: deft = 3; break;
Chris@0 214 case 4000: deft = 4; break;
Chris@0 215 case 6000: deft = 5; break;
Chris@0 216 case 8000: deft = 6; break;
Chris@0 217 case 12000: deft = 7; break;
Chris@0 218 case 16000: deft = 8; break;
Chris@0 219 default: deft = 9; break;
Chris@0 220 }
Chris@0 221
Chris@0 222 } else if (name == tr("Frequency Scale")) {
Chris@0 223
Chris@0 224 *min = 0;
Chris@0 225 *max = 1;
Chris@0 226 deft = (int)m_frequencyScale;
Chris@0 227
Chris@0 228 } else {
Chris@0 229 deft = Layer::getPropertyRangeAndValue(name, min, max);
Chris@0 230 }
Chris@0 231
Chris@0 232 return deft;
Chris@0 233 }
Chris@0 234
Chris@0 235 QString
Chris@0 236 SpectrogramLayer::getPropertyValueLabel(const PropertyName &name,
Chris@9 237 int value) const
Chris@0 238 {
Chris@0 239 if (name == tr("Colour")) {
Chris@0 240 switch (value) {
Chris@0 241 default:
Chris@0 242 case 0: return tr("Default");
Chris@0 243 case 1: return tr("White on Black");
Chris@0 244 case 2: return tr("Black on White");
Chris@0 245 case 3: return tr("Red on Blue");
Chris@0 246 case 4: return tr("Yellow on Black");
Chris@0 247 case 5: return tr("Red on Black");
Chris@0 248 }
Chris@0 249 }
Chris@0 250 if (name == tr("Colour Scale")) {
Chris@0 251 switch (value) {
Chris@0 252 default:
Chris@0 253 case 0: return tr("Level Linear");
Chris@0 254 case 1: return tr("Level Meter");
Chris@0 255 case 2: return tr("Level dB");
Chris@0 256 case 3: return tr("Phase");
Chris@0 257 }
Chris@0 258 }
Chris@0 259 if (name == tr("Window Type")) {
Chris@0 260 switch ((WindowType)value) {
Chris@0 261 default:
Chris@0 262 case RectangularWindow: return tr("Rectangular");
Chris@0 263 case BartlettWindow: return tr("Bartlett");
Chris@0 264 case HammingWindow: return tr("Hamming");
Chris@0 265 case HanningWindow: return tr("Hanning");
Chris@0 266 case BlackmanWindow: return tr("Blackman");
Chris@0 267 case GaussianWindow: return tr("Gaussian");
Chris@0 268 case ParzenWindow: return tr("Parzen");
Chris@0 269 }
Chris@0 270 }
Chris@0 271 if (name == tr("Window Size")) {
Chris@0 272 return QString("%1").arg(32 << value);
Chris@0 273 }
Chris@0 274 if (name == tr("Window Overlap")) {
Chris@0 275 switch (value) {
Chris@0 276 default:
Chris@0 277 case 0: return tr("None");
Chris@0 278 case 1: return tr("25 %");
Chris@0 279 case 2: return tr("50 %");
Chris@0 280 case 3: return tr("75 %");
Chris@0 281 case 4: return tr("90 %");
Chris@0 282 }
Chris@0 283 }
Chris@0 284 if (name == tr("Max Frequency")) {
Chris@0 285 switch (value) {
Chris@0 286 default:
Chris@0 287 case 0: return tr("500 Hz");
Chris@0 288 case 1: return tr("1 KHz");
Chris@0 289 case 2: return tr("1.5 KHz");
Chris@0 290 case 3: return tr("2 KHz");
Chris@0 291 case 4: return tr("4 KHz");
Chris@0 292 case 5: return tr("6 KHz");
Chris@0 293 case 6: return tr("8 KHz");
Chris@0 294 case 7: return tr("12 KHz");
Chris@0 295 case 8: return tr("16 KHz");
Chris@0 296 case 9: return tr("All");
Chris@0 297 }
Chris@0 298 }
Chris@0 299 if (name == tr("Frequency Scale")) {
Chris@0 300 switch (value) {
Chris@0 301 default:
Chris@0 302 case 0: return tr("Linear");
Chris@0 303 case 1: return tr("Log");
Chris@0 304 }
Chris@0 305 }
Chris@0 306 return tr("<unknown>");
Chris@0 307 }
Chris@0 308
Chris@0 309 void
Chris@0 310 SpectrogramLayer::setProperty(const PropertyName &name, int value)
Chris@0 311 {
Chris@0 312 if (name == tr("Gain")) {
Chris@0 313 setGain(pow(10, float(value)/20.0));
Chris@9 314 } else if (name == tr("Colour Rotation")) {
Chris@9 315 setColourRotation(value);
Chris@0 316 } else if (name == tr("Colour")) {
Chris@0 317 if (m_view) m_view->setLightBackground(value == 2);
Chris@0 318 switch (value) {
Chris@0 319 default:
Chris@0 320 case 0: setColourScheme(DefaultColours); break;
Chris@0 321 case 1: setColourScheme(WhiteOnBlack); break;
Chris@0 322 case 2: setColourScheme(BlackOnWhite); break;
Chris@0 323 case 3: setColourScheme(RedOnBlue); break;
Chris@0 324 case 4: setColourScheme(YellowOnBlack); break;
Chris@0 325 case 5: setColourScheme(RedOnBlack); break;
Chris@0 326 }
Chris@0 327 } else if (name == tr("Window Type")) {
Chris@0 328 setWindowType(WindowType(value));
Chris@0 329 } else if (name == tr("Window Size")) {
Chris@0 330 setWindowSize(32 << value);
Chris@0 331 } else if (name == tr("Window Overlap")) {
Chris@0 332 if (value == 4) setWindowOverlap(90);
Chris@0 333 else setWindowOverlap(25 * value);
Chris@0 334 } else if (name == tr("Max Frequency")) {
Chris@0 335 switch (value) {
Chris@0 336 case 0: setMaxFrequency(500); break;
Chris@0 337 case 1: setMaxFrequency(1000); break;
Chris@0 338 case 2: setMaxFrequency(1500); break;
Chris@0 339 case 3: setMaxFrequency(2000); break;
Chris@0 340 case 4: setMaxFrequency(4000); break;
Chris@0 341 case 5: setMaxFrequency(6000); break;
Chris@0 342 case 6: setMaxFrequency(8000); break;
Chris@0 343 case 7: setMaxFrequency(12000); break;
Chris@0 344 case 8: setMaxFrequency(16000); break;
Chris@0 345 default:
Chris@0 346 case 9: setMaxFrequency(0); break;
Chris@0 347 }
Chris@0 348 } else if (name == tr("Colour Scale")) {
Chris@0 349 switch (value) {
Chris@0 350 default:
Chris@0 351 case 0: setColourScale(LinearColourScale); break;
Chris@0 352 case 1: setColourScale(MeterColourScale); break;
Chris@0 353 case 2: setColourScale(dBColourScale); break;
Chris@0 354 case 3: setColourScale(PhaseColourScale); break;
Chris@0 355 }
Chris@0 356 } else if (name == tr("Frequency Scale")) {
Chris@0 357 switch (value) {
Chris@0 358 default:
Chris@0 359 case 0: setFrequencyScale(LinearFrequencyScale); break;
Chris@0 360 case 1: setFrequencyScale(LogFrequencyScale); break;
Chris@0 361 }
Chris@0 362 }
Chris@0 363 }
Chris@0 364
Chris@0 365 void
Chris@0 366 SpectrogramLayer::setChannel(int ch)
Chris@0 367 {
Chris@0 368 if (m_channel == ch) return;
Chris@0 369
Chris@0 370 m_mutex.lock();
Chris@0 371 m_cacheInvalid = true;
Chris@0 372 m_pixmapCacheInvalid = true;
Chris@0 373
Chris@0 374 m_channel = ch;
Chris@9 375
Chris@9 376 m_mutex.unlock();
Chris@9 377
Chris@0 378 emit layerParametersChanged();
Chris@9 379
Chris@0 380 fillCache();
Chris@0 381 }
Chris@0 382
Chris@0 383 int
Chris@0 384 SpectrogramLayer::getChannel() const
Chris@0 385 {
Chris@0 386 return m_channel;
Chris@0 387 }
Chris@0 388
Chris@0 389 void
Chris@0 390 SpectrogramLayer::setWindowSize(size_t ws)
Chris@0 391 {
Chris@0 392 if (m_windowSize == ws) return;
Chris@0 393
Chris@0 394 m_mutex.lock();
Chris@0 395 m_cacheInvalid = true;
Chris@0 396 m_pixmapCacheInvalid = true;
Chris@0 397
Chris@0 398 m_windowSize = ws;
Chris@0 399
Chris@0 400 m_mutex.unlock();
Chris@9 401
Chris@9 402 emit layerParametersChanged();
Chris@9 403
Chris@0 404 fillCache();
Chris@0 405 }
Chris@0 406
Chris@0 407 size_t
Chris@0 408 SpectrogramLayer::getWindowSize() const
Chris@0 409 {
Chris@0 410 return m_windowSize;
Chris@0 411 }
Chris@0 412
Chris@0 413 void
Chris@0 414 SpectrogramLayer::setWindowOverlap(size_t wi)
Chris@0 415 {
Chris@0 416 if (m_windowOverlap == wi) return;
Chris@0 417
Chris@0 418 m_mutex.lock();
Chris@0 419 m_cacheInvalid = true;
Chris@0 420 m_pixmapCacheInvalid = true;
Chris@0 421
Chris@0 422 m_windowOverlap = wi;
Chris@0 423
Chris@0 424 m_mutex.unlock();
Chris@9 425
Chris@9 426 emit layerParametersChanged();
Chris@9 427
Chris@0 428 fillCache();
Chris@0 429 }
Chris@0 430
Chris@0 431 size_t
Chris@0 432 SpectrogramLayer::getWindowOverlap() const
Chris@0 433 {
Chris@0 434 return m_windowOverlap;
Chris@0 435 }
Chris@0 436
Chris@0 437 void
Chris@0 438 SpectrogramLayer::setWindowType(WindowType w)
Chris@0 439 {
Chris@0 440 if (m_windowType == w) return;
Chris@0 441
Chris@0 442 m_mutex.lock();
Chris@0 443 m_cacheInvalid = true;
Chris@0 444 m_pixmapCacheInvalid = true;
Chris@0 445
Chris@0 446 m_windowType = w;
Chris@0 447
Chris@0 448 m_mutex.unlock();
Chris@9 449
Chris@9 450 emit layerParametersChanged();
Chris@9 451
Chris@0 452 fillCache();
Chris@0 453 }
Chris@0 454
Chris@0 455 WindowType
Chris@0 456 SpectrogramLayer::getWindowType() const
Chris@0 457 {
Chris@0 458 return m_windowType;
Chris@0 459 }
Chris@0 460
Chris@0 461 void
Chris@0 462 SpectrogramLayer::setGain(float gain)
Chris@0 463 {
Chris@0 464 if (m_gain == gain) return; //!!! inadequate for floats!
Chris@0 465
Chris@0 466 m_mutex.lock();
Chris@0 467 m_cacheInvalid = true;
Chris@0 468 m_pixmapCacheInvalid = true;
Chris@0 469
Chris@0 470 m_gain = gain;
Chris@0 471
Chris@0 472 m_mutex.unlock();
Chris@9 473
Chris@9 474 emit layerParametersChanged();
Chris@9 475
Chris@0 476 fillCache();
Chris@0 477 }
Chris@0 478
Chris@0 479 float
Chris@0 480 SpectrogramLayer::getGain() const
Chris@0 481 {
Chris@0 482 return m_gain;
Chris@0 483 }
Chris@0 484
Chris@0 485 void
Chris@0 486 SpectrogramLayer::setMaxFrequency(size_t mf)
Chris@0 487 {
Chris@0 488 if (m_maxFrequency == mf) return;
Chris@0 489
Chris@0 490 m_mutex.lock();
Chris@1 491 // don't need to invalidate main cache here
Chris@0 492 m_pixmapCacheInvalid = true;
Chris@0 493
Chris@0 494 m_maxFrequency = mf;
Chris@0 495
Chris@0 496 m_mutex.unlock();
Chris@9 497
Chris@9 498 emit layerParametersChanged();
Chris@0 499 }
Chris@0 500
Chris@0 501 size_t
Chris@0 502 SpectrogramLayer::getMaxFrequency() const
Chris@0 503 {
Chris@0 504 return m_maxFrequency;
Chris@0 505 }
Chris@0 506
Chris@0 507 void
Chris@9 508 SpectrogramLayer::setColourRotation(int r)
Chris@9 509 {
Chris@9 510 m_mutex.lock();
Chris@9 511 // don't need to invalidate main cache here
Chris@9 512 m_pixmapCacheInvalid = true;
Chris@9 513
Chris@9 514 if (r < 0) r = 0;
Chris@9 515 if (r > 256) r = 256;
Chris@9 516 int distance = r - m_colourRotation;
Chris@9 517
Chris@9 518 if (distance != 0) {
Chris@9 519 rotateCacheColourmap(-distance);
Chris@9 520 m_colourRotation = r;
Chris@9 521 }
Chris@9 522
Chris@9 523 m_mutex.unlock();
Chris@9 524
Chris@9 525 emit layerParametersChanged();
Chris@9 526 }
Chris@9 527
Chris@9 528 void
Chris@0 529 SpectrogramLayer::setColourScale(ColourScale colourScale)
Chris@0 530 {
Chris@0 531 if (m_colourScale == colourScale) return;
Chris@0 532
Chris@0 533 m_mutex.lock();
Chris@0 534 m_cacheInvalid = true;
Chris@0 535 m_pixmapCacheInvalid = true;
Chris@0 536
Chris@0 537 m_colourScale = colourScale;
Chris@0 538
Chris@0 539 m_mutex.unlock();
Chris@0 540 fillCache();
Chris@9 541
Chris@9 542 emit layerParametersChanged();
Chris@0 543 }
Chris@0 544
Chris@0 545 SpectrogramLayer::ColourScale
Chris@0 546 SpectrogramLayer::getColourScale() const
Chris@0 547 {
Chris@0 548 return m_colourScale;
Chris@0 549 }
Chris@0 550
Chris@0 551 void
Chris@0 552 SpectrogramLayer::setColourScheme(ColourScheme scheme)
Chris@0 553 {
Chris@0 554 if (m_colourScheme == scheme) return;
Chris@0 555
Chris@0 556 m_mutex.lock();
Chris@0 557 // don't need to invalidate main cache here
Chris@0 558 m_pixmapCacheInvalid = true;
Chris@0 559
Chris@0 560 m_colourScheme = scheme;
Chris@0 561 setCacheColourmap();
Chris@9 562
Chris@9 563 m_mutex.unlock();
Chris@9 564
Chris@0 565 emit layerParametersChanged();
Chris@0 566 }
Chris@0 567
Chris@0 568 SpectrogramLayer::ColourScheme
Chris@0 569 SpectrogramLayer::getColourScheme() const
Chris@0 570 {
Chris@0 571 return m_colourScheme;
Chris@0 572 }
Chris@0 573
Chris@0 574 void
Chris@0 575 SpectrogramLayer::setFrequencyScale(FrequencyScale frequencyScale)
Chris@0 576 {
Chris@0 577 if (m_frequencyScale == frequencyScale) return;
Chris@0 578
Chris@0 579 m_mutex.lock();
Chris@0 580 // don't need to invalidate main cache here
Chris@0 581 m_pixmapCacheInvalid = true;
Chris@0 582
Chris@0 583 m_frequencyScale = frequencyScale;
Chris@0 584
Chris@0 585 m_mutex.unlock();
Chris@9 586
Chris@9 587 emit layerParametersChanged();
Chris@0 588 }
Chris@0 589
Chris@0 590 SpectrogramLayer::FrequencyScale
Chris@0 591 SpectrogramLayer::getFrequencyScale() const
Chris@0 592 {
Chris@0 593 return m_frequencyScale;
Chris@0 594 }
Chris@0 595
Chris@0 596 void
Chris@0 597 SpectrogramLayer::cacheInvalid()
Chris@0 598 {
Chris@0 599 m_cacheInvalid = true;
Chris@0 600 m_pixmapCacheInvalid = true;
Chris@0 601 m_cachedInitialVisibleArea = false;
Chris@0 602 fillCache();
Chris@0 603 }
Chris@0 604
Chris@0 605 void
Chris@0 606 SpectrogramLayer::cacheInvalid(size_t, size_t)
Chris@0 607 {
Chris@0 608 // for now (or forever?)
Chris@0 609 cacheInvalid();
Chris@0 610 }
Chris@0 611
Chris@0 612 void
Chris@0 613 SpectrogramLayer::fillCache()
Chris@0 614 {
Chris@0 615 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@0 616 std::cerr << "SpectrogramLayer::fillCache" << std::endl;
Chris@0 617 #endif
Chris@0 618 QMutexLocker locker(&m_mutex);
Chris@0 619
Chris@0 620 m_lastFillExtent = 0;
Chris@0 621
Chris@0 622 delete m_updateTimer;
Chris@0 623 m_updateTimer = new QTimer(this);
Chris@0 624 connect(m_updateTimer, SIGNAL(timeout()), this, SLOT(fillTimerTimedOut()));
Chris@0 625 m_updateTimer->start(200);
Chris@0 626
Chris@0 627 if (!m_fillThread) {
Chris@0 628 std::cerr << "SpectrogramLayer::fillCache creating thread" << std::endl;
Chris@0 629 m_fillThread = new CacheFillThread(*this);
Chris@0 630 m_fillThread->start();
Chris@0 631 }
Chris@0 632
Chris@0 633 m_condition.wakeAll();
Chris@0 634 }
Chris@0 635
Chris@0 636 void
Chris@0 637 SpectrogramLayer::fillTimerTimedOut()
Chris@0 638 {
Chris@0 639 if (m_fillThread && m_model) {
Chris@0 640 size_t fillExtent = m_fillThread->getFillExtent();
Chris@0 641 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@0 642 std::cerr << "SpectrogramLayer::fillTimerTimedOut: extent " << fillExtent << ", last " << m_lastFillExtent << ", total " << m_model->getEndFrame() << std::endl;
Chris@0 643 #endif
Chris@0 644 if (fillExtent >= m_lastFillExtent) {
Chris@0 645 if (fillExtent >= m_model->getEndFrame() && m_lastFillExtent > 0) {
Chris@0 646 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@0 647 std::cerr << "complete!" << std::endl;
Chris@0 648 #endif
Chris@0 649 emit modelChanged();
Chris@0 650 m_pixmapCacheInvalid = true;
Chris@0 651 delete m_updateTimer;
Chris@0 652 m_updateTimer = 0;
Chris@0 653 m_lastFillExtent = 0;
Chris@0 654 } else if (fillExtent > m_lastFillExtent) {
Chris@0 655 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@0 656 std::cerr << "SpectrogramLayer: emitting modelChanged("
Chris@0 657 << m_lastFillExtent << "," << fillExtent << ")" << std::endl;
Chris@0 658 #endif
Chris@0 659 emit modelChanged(m_lastFillExtent, fillExtent);
Chris@0 660 m_pixmapCacheInvalid = true;
Chris@0 661 m_lastFillExtent = fillExtent;
Chris@0 662 }
Chris@0 663 } else {
Chris@0 664 if (m_view) {
Chris@0 665 size_t sf = 0;
Chris@0 666 if (m_view->getStartFrame() > 0) sf = m_view->getStartFrame();
Chris@0 667 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@0 668 std::cerr << "SpectrogramLayer: going backwards, emitting modelChanged("
Chris@0 669 << sf << "," << m_view->getEndFrame() << ")" << std::endl;
Chris@0 670 #endif
Chris@0 671 emit modelChanged(sf, m_view->getEndFrame());
Chris@0 672 m_pixmapCacheInvalid = true;
Chris@0 673 }
Chris@0 674 m_lastFillExtent = fillExtent;
Chris@0 675 }
Chris@0 676 }
Chris@0 677 }
Chris@0 678
Chris@0 679 void
Chris@0 680 SpectrogramLayer::setCacheColourmap()
Chris@0 681 {
Chris@0 682 if (m_cacheInvalid || !m_cache) return;
Chris@0 683
Chris@10 684 int formerRotation = m_colourRotation;
Chris@10 685
Chris@0 686 m_cache->setNumColors(256);
Chris@0 687
Chris@0 688 m_cache->setColor(0, qRgb(255, 255, 255));
Chris@0 689
Chris@0 690 for (int pixel = 1; pixel < 256; ++pixel) {
Chris@0 691
Chris@0 692 QColor colour;
Chris@0 693 int hue, px;
Chris@0 694
Chris@0 695 switch (m_colourScheme) {
Chris@0 696
Chris@0 697 default:
Chris@0 698 case DefaultColours:
Chris@0 699 hue = 256 - pixel;
Chris@0 700 colour = QColor::fromHsv(hue, pixel/2 + 128, pixel);
Chris@0 701 break;
Chris@0 702
Chris@0 703 case WhiteOnBlack:
Chris@0 704 colour = QColor(pixel, pixel, pixel);
Chris@0 705 break;
Chris@0 706
Chris@0 707 case BlackOnWhite:
Chris@0 708 colour = QColor(256-pixel, 256-pixel, 256-pixel);
Chris@0 709 break;
Chris@0 710
Chris@0 711 case RedOnBlue:
Chris@0 712 colour = QColor(pixel > 128 ? (pixel - 128) * 2 : 0, 0,
Chris@0 713 pixel < 128 ? pixel : (256 - pixel));
Chris@0 714 break;
Chris@0 715
Chris@0 716 case YellowOnBlack:
Chris@0 717 px = 256 - pixel;
Chris@0 718 colour = QColor(px < 64 ? 255 - px/2 :
Chris@0 719 px < 128 ? 224 - (px - 64) :
Chris@0 720 px < 192 ? 160 - (px - 128) * 3 / 2 :
Chris@0 721 256 - px,
Chris@0 722 pixel,
Chris@0 723 pixel / 4);
Chris@0 724 break;
Chris@0 725
Chris@0 726 case RedOnBlack:
Chris@0 727 colour = QColor::fromHsv(10, pixel, pixel);
Chris@0 728 break;
Chris@0 729 }
Chris@0 730
Chris@0 731 m_cache->setColor
Chris@0 732 (pixel, qRgb(colour.red(), colour.green(), colour.blue()));
Chris@0 733 }
Chris@9 734
Chris@9 735 m_colourRotation = 0;
Chris@10 736 rotateCacheColourmap(m_colourRotation - formerRotation);
Chris@10 737 m_colourRotation = formerRotation;
Chris@9 738 }
Chris@9 739
Chris@9 740 void
Chris@9 741 SpectrogramLayer::rotateCacheColourmap(int distance)
Chris@9 742 {
Chris@10 743 if (!m_cache) return;
Chris@10 744
Chris@9 745 QRgb newPixels[256];
Chris@9 746
Chris@9 747 newPixels[0] = m_cache->color(0);
Chris@9 748
Chris@9 749 for (int pixel = 1; pixel < 256; ++pixel) {
Chris@9 750 int target = pixel + distance;
Chris@9 751 while (target < 1) target += 255;
Chris@9 752 while (target > 255) target -= 255;
Chris@9 753 newPixels[target] = m_cache->color(pixel);
Chris@9 754 }
Chris@9 755
Chris@9 756 for (int pixel = 0; pixel < 256; ++pixel) {
Chris@9 757 m_cache->setColor(pixel, newPixels[pixel]);
Chris@9 758 }
Chris@0 759 }
Chris@0 760
Chris@0 761 bool
Chris@0 762 SpectrogramLayer::fillCacheColumn(int column, double *input,
Chris@0 763 fftw_complex *output,
Chris@0 764 fftw_plan plan,
Chris@9 765 size_t windowSize,
Chris@9 766 size_t increment,
Chris@0 767 const Window<double> &windower,
Chris@0 768 bool lock) const
Chris@0 769 {
Chris@0 770 int startFrame = increment * column;
Chris@9 771 int endFrame = startFrame + windowSize;
Chris@0 772
Chris@9 773 startFrame -= int(windowSize - increment) / 2;
Chris@9 774 endFrame -= int(windowSize - increment) / 2;
Chris@0 775 size_t pfx = 0;
Chris@0 776
Chris@0 777 if (startFrame < 0) {
Chris@0 778 pfx = size_t(-startFrame);
Chris@0 779 for (size_t i = 0; i < pfx; ++i) {
Chris@0 780 input[i] = 0.0;
Chris@0 781 }
Chris@0 782 }
Chris@0 783
Chris@0 784 size_t got = m_model->getValues(m_channel, startFrame + pfx,
Chris@0 785 endFrame, input + pfx);
Chris@9 786 while (got + pfx < windowSize) {
Chris@0 787 input[got + pfx] = 0.0;
Chris@0 788 ++got;
Chris@0 789 }
Chris@0 790
Chris@0 791 if (m_gain != 1.0) {
Chris@9 792 for (size_t i = 0; i < windowSize; ++i) {
Chris@0 793 input[i] *= m_gain;
Chris@0 794 }
Chris@0 795 }
Chris@0 796
Chris@0 797 windower.cut(input);
Chris@0 798
Chris@0 799 fftw_execute(plan);
Chris@0 800
Chris@0 801 if (lock) m_mutex.lock();
Chris@0 802 bool interrupted = false;
Chris@0 803
Chris@9 804 for (size_t i = 0; i < windowSize / 2; ++i) {
Chris@0 805
Chris@0 806 int value = 0;
Chris@0 807
Chris@0 808 if (m_colourScale == PhaseColourScale) {
Chris@0 809
Chris@0 810 double phase = atan2(-output[i][1], output[i][0]);
Chris@0 811 value = int((phase * 128 / M_PI) + 128);
Chris@0 812
Chris@0 813 } else {
Chris@1 814
Chris@0 815 double mag = sqrt(output[i][0] * output[i][0] +
Chris@0 816 output[i][1] * output[i][1]);
Chris@9 817 mag /= windowSize / 2;
Chris@0 818
Chris@0 819 switch (m_colourScale) {
Chris@0 820
Chris@0 821 default:
Chris@0 822 case LinearColourScale:
Chris@0 823 value = int(mag * 50 * 256);
Chris@0 824 break;
Chris@0 825
Chris@0 826 case MeterColourScale:
Chris@0 827 value = AudioLevel::multiplier_to_preview(mag * 50, 256);
Chris@0 828 break;
Chris@0 829
Chris@0 830 case dBColourScale:
Chris@0 831 mag = 20.0 * log10(mag);
Chris@0 832 mag = (mag + 80.0) / 80.0;
Chris@0 833 if (mag < 0.0) mag = 0.0;
Chris@0 834 if (mag > 1.0) mag = 1.0;
Chris@0 835 value = int(mag * 256);
Chris@0 836 }
Chris@0 837 }
Chris@0 838
Chris@0 839 if (value > 254) value = 254;
Chris@0 840 if (value < 0) value = 0;
Chris@0 841
Chris@0 842 if (m_cacheInvalid || m_exiting) {
Chris@0 843 interrupted = true;
Chris@0 844 break;
Chris@0 845 }
Chris@0 846
Chris@1 847 if (column < m_cache->width() && (int)i < m_cache->height()) {
Chris@0 848 m_cache->setPixel(column, i, value + 1); // 0 is "unset"
Chris@0 849 }
Chris@0 850 }
Chris@0 851
Chris@0 852 if (lock) m_mutex.unlock();
Chris@0 853 return !interrupted;
Chris@0 854 }
Chris@0 855
Chris@0 856 void
Chris@0 857 SpectrogramLayer::CacheFillThread::run()
Chris@0 858 {
Chris@0 859 // std::cerr << "SpectrogramLayer::CacheFillThread::run" << std::endl;
Chris@0 860
Chris@0 861 m_layer.m_mutex.lock();
Chris@0 862
Chris@0 863 while (!m_layer.m_exiting) {
Chris@0 864
Chris@0 865 bool interrupted = false;
Chris@0 866
Chris@0 867 // std::cerr << "SpectrogramLayer::CacheFillThread::run in loop" << std::endl;
Chris@0 868
Chris@1 869 if (m_layer.m_model && m_layer.m_cacheInvalid) {
Chris@0 870
Chris@0 871 // std::cerr << "SpectrogramLayer::CacheFillThread::run: something to do" << std::endl;
Chris@0 872
Chris@0 873 while (!m_layer.m_model->isReady()) {
Chris@0 874 m_layer.m_condition.wait(&m_layer.m_mutex, 100);
Chris@0 875 }
Chris@0 876
Chris@0 877 m_layer.m_cachedInitialVisibleArea = false;
Chris@0 878 m_layer.m_cacheInvalid = false;
Chris@0 879 m_fillExtent = 0;
Chris@0 880 m_fillCompletion = 0;
Chris@0 881
Chris@0 882 std::cerr << "SpectrogramLayer::CacheFillThread::run: model is ready" << std::endl;
Chris@0 883
Chris@0 884 size_t start = m_layer.m_model->getStartFrame();
Chris@0 885 size_t end = m_layer.m_model->getEndFrame();
Chris@9 886
Chris@9 887 WindowType windowType = m_layer.m_windowType;
Chris@0 888 size_t windowSize = m_layer.m_windowSize;
Chris@0 889 size_t windowIncrement = m_layer.getWindowIncrement();
Chris@0 890
Chris@0 891 size_t visibleStart = start;
Chris@0 892 size_t visibleEnd = end;
Chris@0 893
Chris@0 894 if (m_layer.m_view) {
Chris@0 895 if (m_layer.m_view->getStartFrame() < 0) {
Chris@0 896 visibleStart = 0;
Chris@0 897 } else {
Chris@0 898 visibleStart = m_layer.m_view->getStartFrame();
Chris@0 899 visibleStart = (visibleStart / windowIncrement) *
Chris@0 900 windowIncrement;
Chris@0 901 }
Chris@0 902 visibleEnd = m_layer.m_view->getEndFrame();
Chris@0 903 }
Chris@0 904
Chris@0 905 delete m_layer.m_cache;
Chris@9 906 size_t width = (end - start) / windowIncrement + 1;
Chris@9 907 size_t height = windowSize / 2;
Chris@9 908 m_layer.m_cache = new QImage(width, height,
Chris@9 909 QImage::Format_Indexed8);
Chris@9 910
Chris@9 911 // If we're using JACK in mlock mode, this will be locked
Chris@9 912 // and we ought to unlock to avoid memory exhaustion.
Chris@9 913 // Shame it doesn't appear to be possible to allocate
Chris@9 914 // unlocked in the first place.
Chris@9 915 //!!! hm, I don't think this is working.
Chris@9 916 MUNLOCK((void *)m_layer.m_cache, width * height);
Chris@0 917
Chris@0 918 m_layer.setCacheColourmap();
Chris@10 919
Chris@0 920 m_layer.m_cache->fill(0);
Chris@0 921 m_layer.m_mutex.unlock();
Chris@0 922
Chris@0 923 double *input = (double *)
Chris@0 924 fftw_malloc(windowSize * sizeof(double));
Chris@0 925
Chris@0 926 fftw_complex *output = (fftw_complex *)
Chris@0 927 fftw_malloc(windowSize * sizeof(fftw_complex));
Chris@0 928
Chris@0 929 fftw_plan plan = fftw_plan_dft_r2c_1d(windowSize, input,
Chris@1 930 output, FFTW_ESTIMATE);
Chris@0 931
Chris@9 932 Window<double> windower(windowType, windowSize);
Chris@0 933
Chris@0 934 if (!plan) {
Chris@1 935 std::cerr << "WARNING: fftw_plan_dft_r2c_1d(" << windowSize << ") failed!" << std::endl;
Chris@0 936 fftw_free(input);
Chris@0 937 fftw_free(output);
Chris@0 938 m_layer.m_mutex.lock();
Chris@0 939 continue;
Chris@0 940 }
Chris@0 941
Chris@0 942 int counter = 0;
Chris@0 943 int updateAt = (end / windowIncrement) / 20;
Chris@0 944 if (updateAt < 100) updateAt = 100;
Chris@0 945
Chris@0 946 bool doVisibleFirst = (visibleStart != start && visibleEnd != end);
Chris@0 947
Chris@0 948 if (doVisibleFirst) {
Chris@0 949
Chris@0 950 m_layer.m_mutex.lock();
Chris@0 951
Chris@0 952 for (size_t f = visibleStart; f < visibleEnd; f += windowIncrement) {
Chris@0 953
Chris@0 954 m_layer.fillCacheColumn(int((f - start) / windowIncrement),
Chris@9 955 input, output, plan,
Chris@9 956 windowSize, windowIncrement,
Chris@9 957 windower, false);
Chris@0 958
Chris@0 959 m_layer.m_mutex.unlock();
Chris@0 960 m_layer.m_mutex.lock();
Chris@0 961
Chris@0 962 if (m_layer.m_cacheInvalid || m_layer.m_exiting) {
Chris@0 963 interrupted = true;
Chris@0 964 m_fillExtent = 0;
Chris@0 965 break;
Chris@0 966 }
Chris@0 967
Chris@0 968 if (++counter == updateAt) {
Chris@0 969 if (f < end) m_fillExtent = f;
Chris@0 970 m_fillCompletion = size_t(100 * fabsf(float(f - visibleStart) /
Chris@0 971 float(end - start)));
Chris@0 972 counter = 0;
Chris@0 973 }
Chris@0 974 }
Chris@0 975
Chris@0 976 m_layer.m_mutex.unlock();
Chris@0 977 }
Chris@0 978
Chris@0 979 m_layer.m_cachedInitialVisibleArea = true;
Chris@0 980
Chris@0 981 if (!interrupted && doVisibleFirst) {
Chris@0 982
Chris@0 983 for (size_t f = visibleEnd; f < end; f += windowIncrement) {
Chris@0 984
Chris@0 985 if (!m_layer.fillCacheColumn(int((f - start) / windowIncrement),
Chris@9 986 input, output, plan,
Chris@9 987 windowSize, windowIncrement,
Chris@9 988 windower, true)) {
Chris@0 989 interrupted = true;
Chris@0 990 m_fillExtent = 0;
Chris@0 991 break;
Chris@0 992 }
Chris@0 993
Chris@0 994
Chris@0 995 if (++counter == updateAt) {
Chris@0 996 if (f < end) m_fillExtent = f;
Chris@0 997 m_fillCompletion = size_t(100 * fabsf(float(f - visibleStart) /
Chris@0 998 float(end - start)));
Chris@0 999 counter = 0;
Chris@0 1000 }
Chris@0 1001 }
Chris@0 1002 }
Chris@0 1003
Chris@0 1004 if (!interrupted) {
Chris@0 1005
Chris@0 1006 size_t remainingEnd = end;
Chris@0 1007 if (doVisibleFirst) {
Chris@0 1008 remainingEnd = visibleStart;
Chris@0 1009 if (remainingEnd > start) --remainingEnd;
Chris@0 1010 else remainingEnd = start;
Chris@0 1011 }
Chris@0 1012 size_t baseCompletion = m_fillCompletion;
Chris@0 1013
Chris@0 1014 for (size_t f = start; f < remainingEnd; f += windowIncrement) {
Chris@0 1015
Chris@0 1016 if (!m_layer.fillCacheColumn(int((f - start) / windowIncrement),
Chris@9 1017 input, output, plan,
Chris@9 1018 windowSize, windowIncrement,
Chris@9 1019 windower, true)) {
Chris@0 1020 interrupted = true;
Chris@0 1021 m_fillExtent = 0;
Chris@0 1022 break;
Chris@0 1023 }
Chris@0 1024
Chris@0 1025 if (++counter == updateAt) {
Chris@0 1026 m_fillExtent = f;
Chris@0 1027 m_fillCompletion = baseCompletion +
Chris@0 1028 size_t(100 * fabsf(float(f - start) /
Chris@0 1029 float(end - start)));
Chris@0 1030 counter = 0;
Chris@0 1031 }
Chris@0 1032 }
Chris@0 1033 }
Chris@0 1034
Chris@0 1035 fftw_destroy_plan(plan);
Chris@0 1036 fftw_free(output);
Chris@0 1037 fftw_free(input);
Chris@0 1038
Chris@0 1039 if (!interrupted) {
Chris@0 1040 m_fillExtent = end;
Chris@0 1041 m_fillCompletion = 100;
Chris@0 1042 }
Chris@0 1043
Chris@0 1044 m_layer.m_mutex.lock();
Chris@0 1045 }
Chris@0 1046
Chris@0 1047 if (!interrupted) m_layer.m_condition.wait(&m_layer.m_mutex, 2000);
Chris@0 1048 }
Chris@0 1049 }
Chris@0 1050
Chris@0 1051 bool
Chris@0 1052 SpectrogramLayer::getYBinRange(int y, float &q0, float &q1) const
Chris@0 1053 {
Chris@0 1054 int h = m_view->height();
Chris@0 1055 if (y < 0 || y >= h) return false;
Chris@0 1056
Chris@0 1057 // Each pixel in a column is drawn from a possibly non-
Chris@0 1058 // integral set of frequency bins.
Chris@0 1059
Chris@0 1060 if (m_frequencyScale == LinearFrequencyScale) {
Chris@0 1061
Chris@0 1062 size_t bins = m_windowSize / 2;
Chris@0 1063
Chris@0 1064 if (m_maxFrequency > 0) {
Chris@0 1065 int sr = m_model->getSampleRate();
Chris@0 1066 bins = int((double(m_maxFrequency) * m_windowSize) / sr + 0.1);
Chris@0 1067 if (bins > m_windowSize / 2) bins = m_windowSize / 2;
Chris@0 1068 }
Chris@0 1069
Chris@0 1070 q0 = float(h - y - 1) * bins / h;
Chris@0 1071 q1 = float(h - y) * bins / h;
Chris@0 1072
Chris@0 1073 } else {
Chris@0 1074
Chris@0 1075 // This is all most ad-hoc. I'm not at my brightest.
Chris@0 1076
Chris@0 1077 int sr = m_model->getSampleRate();
Chris@0 1078
Chris@0 1079 float maxf = m_maxFrequency;
Chris@0 1080 if (maxf == 0.0) maxf = float(sr) / 2;
Chris@0 1081
Chris@0 1082 float minf = float(sr) / m_windowSize;
Chris@0 1083
Chris@0 1084 float maxlogf = log10f(maxf);
Chris@0 1085 float minlogf = log10f(minf);
Chris@0 1086
Chris@0 1087 float logf0 = minlogf + ((maxlogf - minlogf) * (h - y - 1)) / h;
Chris@0 1088 float logf1 = minlogf + ((maxlogf - minlogf) * (h - y)) / h;
Chris@0 1089
Chris@0 1090 float f0 = pow(10.f, logf0);
Chris@0 1091 float f1 = pow(10.f, logf1);
Chris@0 1092
Chris@0 1093 q0 = ((f0 * m_windowSize) / sr) - 1;
Chris@0 1094 q1 = ((f1 * m_windowSize) / sr) - 1;
Chris@0 1095
Chris@0 1096 // std::cout << "y=" << y << " h=" << h << " maxf=" << maxf << " maxlogf="
Chris@0 1097 // << maxlogf << " logf0=" << logf0 << " f0=" << f0 << " q0="
Chris@0 1098 // << q0 << std::endl;
Chris@0 1099 }
Chris@0 1100
Chris@0 1101 return true;
Chris@0 1102 }
Chris@0 1103
Chris@0 1104 bool
Chris@20 1105 SpectrogramLayer::getXBinRange(int x, float &s0, float &s1) const
Chris@0 1106 {
Chris@21 1107 size_t modelStart = m_model->getStartFrame();
Chris@21 1108 size_t modelEnd = m_model->getEndFrame();
Chris@0 1109
Chris@0 1110 // Each pixel column covers an exact range of sample frames:
Chris@20 1111 int f0 = getFrameForX(x) - modelStart;
Chris@20 1112 int f1 = getFrameForX(x + 1) - modelStart - 1;
Chris@20 1113
Chris@0 1114 if (f1 < int(modelStart) || f0 > int(modelEnd)) return false;
Chris@20 1115
Chris@0 1116 // And that range may be drawn from a possibly non-integral
Chris@0 1117 // range of spectrogram windows:
Chris@0 1118
Chris@0 1119 size_t windowIncrement = getWindowIncrement();
Chris@0 1120 s0 = float(f0) / windowIncrement;
Chris@0 1121 s1 = float(f1) / windowIncrement;
Chris@0 1122
Chris@0 1123 return true;
Chris@0 1124 }
Chris@0 1125
Chris@0 1126 bool
Chris@0 1127 SpectrogramLayer::getXBinSourceRange(int x, RealTime &min, RealTime &max) const
Chris@0 1128 {
Chris@0 1129 float s0 = 0, s1 = 0;
Chris@0 1130 if (!getXBinRange(x, s0, s1)) return false;
Chris@0 1131
Chris@0 1132 int s0i = int(s0 + 0.001);
Chris@0 1133 int s1i = int(s1);
Chris@0 1134
Chris@0 1135 int windowIncrement = getWindowIncrement();
Chris@0 1136 int w0 = s0i * windowIncrement - (m_windowSize - windowIncrement)/2;
Chris@0 1137 int w1 = s1i * windowIncrement + windowIncrement +
Chris@0 1138 (m_windowSize - windowIncrement)/2 - 1;
Chris@0 1139
Chris@0 1140 min = RealTime::frame2RealTime(w0, m_model->getSampleRate());
Chris@0 1141 max = RealTime::frame2RealTime(w1, m_model->getSampleRate());
Chris@0 1142 return true;
Chris@0 1143 }
Chris@0 1144
Chris@0 1145 bool
Chris@0 1146 SpectrogramLayer::getYBinSourceRange(int y, float &freqMin, float &freqMax)
Chris@0 1147 const
Chris@0 1148 {
Chris@0 1149 float q0 = 0, q1 = 0;
Chris@0 1150 if (!getYBinRange(y, q0, q1)) return false;
Chris@0 1151
Chris@0 1152 int q0i = int(q0 + 0.001);
Chris@0 1153 int q1i = int(q1);
Chris@0 1154
Chris@0 1155 int sr = m_model->getSampleRate();
Chris@0 1156
Chris@0 1157 for (int q = q0i; q <= q1i; ++q) {
Chris@0 1158 int binfreq = (sr * (q + 1)) / m_windowSize;
Chris@0 1159 if (q == q0i) freqMin = binfreq;
Chris@0 1160 if (q == q1i) freqMax = binfreq;
Chris@0 1161 }
Chris@0 1162 return true;
Chris@0 1163 }
Chris@0 1164
Chris@0 1165 bool
Chris@0 1166 SpectrogramLayer::getXYBinSourceRange(int x, int y, float &dbMin, float &dbMax) const
Chris@0 1167 {
Chris@0 1168 float q0 = 0, q1 = 0;
Chris@0 1169 if (!getYBinRange(y, q0, q1)) return false;
Chris@0 1170
Chris@0 1171 float s0 = 0, s1 = 0;
Chris@0 1172 if (!getXBinRange(x, s0, s1)) return false;
Chris@0 1173
Chris@0 1174 int q0i = int(q0 + 0.001);
Chris@0 1175 int q1i = int(q1);
Chris@0 1176
Chris@0 1177 int s0i = int(s0 + 0.001);
Chris@0 1178 int s1i = int(s1);
Chris@0 1179
Chris@0 1180 if (m_mutex.tryLock()) {
Chris@0 1181 if (m_cache && !m_cacheInvalid) {
Chris@0 1182
Chris@0 1183 int cw = m_cache->width();
Chris@0 1184 int ch = m_cache->height();
Chris@0 1185
Chris@0 1186 int min = -1, max = -1;
Chris@0 1187
Chris@0 1188 for (int q = q0i; q <= q1i; ++q) {
Chris@0 1189 for (int s = s0i; s <= s1i; ++s) {
Chris@0 1190 if (s >= 0 && q >= 0 && s < cw && q < ch) {
Chris@0 1191 int value = m_cache->scanLine(q)[s];
Chris@0 1192 if (min == -1 || value < min) min = value;
Chris@0 1193 if (max == -1 || value > max) max = value;
Chris@0 1194 }
Chris@0 1195 }
Chris@0 1196 }
Chris@0 1197
Chris@0 1198 if (min < 0) return false;
Chris@0 1199
Chris@0 1200 dbMin = (float(min) / 256.0) * 80.0 - 80.0;
Chris@0 1201 dbMax = (float(max + 1) / 256.0) * 80.0 - 80.1;
Chris@0 1202
Chris@0 1203 m_mutex.unlock();
Chris@0 1204 return true;
Chris@0 1205 }
Chris@0 1206
Chris@0 1207 m_mutex.unlock();
Chris@0 1208 }
Chris@0 1209
Chris@0 1210 return false;
Chris@0 1211 }
Chris@0 1212
Chris@0 1213 void
Chris@0 1214 SpectrogramLayer::paint(QPainter &paint, QRect rect) const
Chris@0 1215 {
Chris@0 1216 // Profiler profiler("SpectrogramLayer::paint", true);
Chris@0 1217 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@0 1218 std::cerr << "SpectrogramLayer::paint(): m_model is " << m_model << ", zoom level is " << m_view->getZoomLevel() << ", m_updateTimer " << m_updateTimer << ", pixmap cache invalid " << m_pixmapCacheInvalid << std::endl;
Chris@0 1219 #endif
Chris@0 1220
Chris@0 1221 if (!m_model || !m_model->isOK() || !m_model->isReady()) {
Chris@0 1222 return;
Chris@0 1223 }
Chris@0 1224
Chris@0 1225 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@0 1226 std::cerr << "SpectrogramLayer::paint(): About to lock" << std::endl;
Chris@0 1227 #endif
Chris@0 1228
Chris@0 1229 /*
Chris@0 1230 if (m_cachedInitialVisibleArea) {
Chris@0 1231 if (!m_mutex.tryLock()) {
Chris@0 1232 m_view->update();
Chris@0 1233 return;
Chris@0 1234 }
Chris@0 1235 } else {
Chris@0 1236 */
Chris@0 1237 m_mutex.lock();
Chris@0 1238 // }
Chris@0 1239
Chris@0 1240 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@0 1241 std::cerr << "SpectrogramLayer::paint(): locked" << std::endl;
Chris@0 1242 #endif
Chris@0 1243
Chris@0 1244 if (m_cacheInvalid) { // lock the mutex before checking this
Chris@0 1245 m_mutex.unlock();
Chris@0 1246 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@0 1247 std::cerr << "SpectrogramLayer::paint(): Cache invalid, returning" << std::endl;
Chris@0 1248 #endif
Chris@0 1249 return;
Chris@0 1250 }
Chris@0 1251
Chris@0 1252 bool stillCacheing = (m_updateTimer != 0);
Chris@0 1253
Chris@0 1254 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@0 1255 std::cerr << "SpectrogramLayer::paint(): Still cacheing = " << stillCacheing << std::endl;
Chris@0 1256 #endif
Chris@0 1257
Chris@0 1258 long startFrame = m_view->getStartFrame();
Chris@0 1259 int zoomLevel = m_view->getZoomLevel();
Chris@0 1260
Chris@0 1261 int x0 = 0;
Chris@0 1262 int x1 = m_view->width();
Chris@0 1263 int y0 = 0;
Chris@0 1264 int y1 = m_view->height();
Chris@0 1265
Chris@0 1266 bool recreateWholePixmapCache = true;
Chris@0 1267
Chris@0 1268 if (!m_pixmapCacheInvalid) {
Chris@0 1269
Chris@0 1270 //!!! This cache may have been obsoleted entirely by the
Chris@0 1271 //scrolling cache in View. Perhaps experiment with
Chris@0 1272 //removing it and see if it makes things even quicker (or else
Chris@0 1273 //make it optional)
Chris@0 1274
Chris@0 1275 if (int(m_pixmapCacheZoomLevel) == zoomLevel &&
Chris@0 1276 m_pixmapCache->width() == m_view->width() &&
Chris@0 1277 m_pixmapCache->height() == m_view->height()) {
Chris@0 1278
Chris@20 1279 if (getXForFrame(m_pixmapCacheStartFrame) ==
Chris@20 1280 getXForFrame(startFrame)) {
Chris@0 1281
Chris@0 1282 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@0 1283 std::cerr << "SpectrogramLayer: pixmap cache good" << std::endl;
Chris@0 1284 #endif
Chris@0 1285
Chris@0 1286 m_mutex.unlock();
Chris@0 1287 paint.drawPixmap(rect, *m_pixmapCache, rect);
Chris@0 1288 return;
Chris@0 1289
Chris@0 1290 } else {
Chris@0 1291
Chris@0 1292 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@0 1293 std::cerr << "SpectrogramLayer: pixmap cache partially OK" << std::endl;
Chris@0 1294 #endif
Chris@0 1295
Chris@0 1296 recreateWholePixmapCache = false;
Chris@0 1297
Chris@20 1298 int dx = getXForFrame(m_pixmapCacheStartFrame) -
Chris@20 1299 getXForFrame(startFrame);
Chris@0 1300
Chris@0 1301 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@0 1302 std::cerr << "SpectrogramLayer: dx = " << dx << " (pixmap cache " << m_pixmapCache->width() << "x" << m_pixmapCache->height() << ")" << std::endl;
Chris@0 1303 #endif
Chris@0 1304
Chris@0 1305 if (dx > -m_pixmapCache->width() && dx < m_pixmapCache->width()) {
Chris@0 1306
Chris@0 1307 #if defined(Q_WS_WIN32) || defined(Q_WS_MAC)
Chris@0 1308 // Copying a pixmap to itself doesn't work
Chris@0 1309 // properly on Windows or Mac (it only works when
Chris@0 1310 // moving in one direction).
Chris@0 1311
Chris@0 1312 //!!! Need a utility function for this
Chris@0 1313
Chris@0 1314 static QPixmap *tmpPixmap = 0;
Chris@0 1315 if (!tmpPixmap ||
Chris@0 1316 tmpPixmap->width() != m_pixmapCache->width() ||
Chris@0 1317 tmpPixmap->height() != m_pixmapCache->height()) {
Chris@0 1318 delete tmpPixmap;
Chris@0 1319 tmpPixmap = new QPixmap(m_pixmapCache->width(),
Chris@0 1320 m_pixmapCache->height());
Chris@0 1321 }
Chris@0 1322 QPainter cachePainter;
Chris@0 1323 cachePainter.begin(tmpPixmap);
Chris@0 1324 cachePainter.drawPixmap(0, 0, *m_pixmapCache);
Chris@0 1325 cachePainter.end();
Chris@0 1326 cachePainter.begin(m_pixmapCache);
Chris@0 1327 cachePainter.drawPixmap(dx, 0, *tmpPixmap);
Chris@0 1328 cachePainter.end();
Chris@0 1329 #else
Chris@0 1330 QPainter cachePainter(m_pixmapCache);
Chris@0 1331 cachePainter.drawPixmap(dx, 0, *m_pixmapCache);
Chris@0 1332 cachePainter.end();
Chris@0 1333 #endif
Chris@0 1334
Chris@0 1335 paint.drawPixmap(rect, *m_pixmapCache, rect);
Chris@0 1336
Chris@0 1337 if (dx < 0) {
Chris@0 1338 x0 = m_pixmapCache->width() + dx;
Chris@0 1339 x1 = m_pixmapCache->width();
Chris@0 1340 } else {
Chris@0 1341 x0 = 0;
Chris@0 1342 x1 = dx;
Chris@0 1343 }
Chris@0 1344 }
Chris@0 1345 }
Chris@0 1346 } else {
Chris@0 1347 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@0 1348 std::cerr << "SpectrogramLayer: pixmap cache useless" << std::endl;
Chris@0 1349 #endif
Chris@0 1350 }
Chris@0 1351 }
Chris@0 1352
Chris@0 1353 if (stillCacheing) {
Chris@0 1354 x0 = rect.left();
Chris@0 1355 x1 = rect.right() + 1;
Chris@0 1356 y0 = rect.top();
Chris@0 1357 y1 = rect.bottom() + 1;
Chris@0 1358 }
Chris@0 1359
Chris@0 1360 int w = x1 - x0;
Chris@0 1361 int h = y1 - y0;
Chris@0 1362
Chris@0 1363 // std::cerr << "x0 " << x0 << ", x1 " << x1 << ", w " << w << ", h " << h << std::endl;
Chris@0 1364
Chris@0 1365 QImage scaled(w, h, QImage::Format_RGB32);
Chris@0 1366
Chris@0 1367 m_mutex.unlock();
Chris@0 1368
Chris@0 1369 for (int y = 0; y < h; ++y) {
Chris@0 1370
Chris@0 1371 m_mutex.lock();
Chris@0 1372 if (m_cacheInvalid) {
Chris@0 1373 m_mutex.unlock();
Chris@0 1374 break;
Chris@0 1375 }
Chris@0 1376
Chris@0 1377 int cw = m_cache->width();
Chris@0 1378 int ch = m_cache->height();
Chris@0 1379
Chris@0 1380 float q0 = 0, q1 = 0;
Chris@0 1381
Chris@0 1382 if (!getYBinRange(y0 + y, q0, q1)) {
Chris@0 1383 for (int x = 0; x < w; ++x) {
Chris@0 1384 assert(x <= scaled.width());
Chris@0 1385 scaled.setPixel(x, y, qRgb(0, 0, 0));
Chris@0 1386 }
Chris@0 1387 m_mutex.unlock();
Chris@0 1388 continue;
Chris@0 1389 }
Chris@0 1390
Chris@0 1391 int q0i = int(q0 + 0.001);
Chris@0 1392 int q1i = int(q1);
Chris@0 1393
Chris@0 1394 for (int x = 0; x < w; ++x) {
Chris@0 1395
Chris@0 1396 float s0 = 0, s1 = 0;
Chris@0 1397
Chris@20 1398 if (!getXBinRange(x0 + x, s0, s1)) {
Chris@0 1399 assert(x <= scaled.width());
Chris@0 1400 scaled.setPixel(x, y, qRgb(0, 0, 0));
Chris@0 1401 continue;
Chris@0 1402 }
Chris@0 1403
Chris@0 1404 int s0i = int(s0 + 0.001);
Chris@0 1405 int s1i = int(s1);
Chris@0 1406
Chris@0 1407 float total = 0, divisor = 0;
Chris@0 1408
Chris@0 1409 for (int s = s0i; s <= s1i; ++s) {
Chris@0 1410
Chris@0 1411 float sprop = 1.0;
Chris@0 1412 if (s == s0i) sprop *= (s + 1) - s0;
Chris@0 1413 if (s == s1i) sprop *= s1 - s;
Chris@0 1414
Chris@0 1415 for (int q = q0i; q <= q1i; ++q) {
Chris@0 1416
Chris@0 1417 float qprop = sprop;
Chris@0 1418 if (q == q0i) qprop *= (q + 1) - q0;
Chris@0 1419 if (q == q1i) qprop *= q1 - q;
Chris@0 1420
Chris@0 1421 if (s >= 0 && q >= 0 && s < cw && q < ch) {
Chris@0 1422 total += qprop * m_cache->scanLine(q)[s];
Chris@0 1423 divisor += qprop;
Chris@0 1424 }
Chris@0 1425 }
Chris@0 1426 }
Chris@0 1427
Chris@0 1428 if (divisor > 0.0) {
Chris@10 1429 /*
Chris@0 1430 int pixel = int(total / divisor);
Chris@0 1431 if (pixel > 255) pixel = 255;
Chris@0 1432 if (pixel < 1) pixel = 1;
Chris@0 1433 assert(x <= scaled.width());
Chris@0 1434 scaled.setPixel(x, y, m_cache->color(pixel));
Chris@10 1435 */
Chris@9 1436 float pixel = total / divisor;
Chris@9 1437 float lq = pixel - int(pixel);
Chris@9 1438 float hq = int(pixel) + 1 - pixel;
Chris@9 1439 int pixNum = int(pixel);
Chris@9 1440 QRgb low = m_cache->color(pixNum > 255 ? 255 : pixNum);
Chris@9 1441 QRgb high = m_cache->color(pixNum > 254 ? 255 : pixNum + 1);
Chris@9 1442 QRgb mixed = qRgb
Chris@9 1443 (qRed(low) * lq + qRed(high) * hq + 0.01,
Chris@9 1444 qGreen(low) * lq + qGreen(high) * hq + 0.01,
Chris@9 1445 qBlue(low) * lq + qBlue(high) * hq + 0.01);
Chris@9 1446 scaled.setPixel(x, y, mixed);
Chris@10 1447
Chris@0 1448 } else {
Chris@0 1449 assert(x <= scaled.width());
Chris@0 1450 scaled.setPixel(x, y, qRgb(0, 0, 0));
Chris@0 1451 }
Chris@0 1452 }
Chris@0 1453
Chris@0 1454 m_mutex.unlock();
Chris@0 1455 }
Chris@0 1456
Chris@0 1457 paint.drawImage(x0, y0, scaled);
Chris@0 1458
Chris@0 1459 if (recreateWholePixmapCache) {
Chris@0 1460 delete m_pixmapCache;
Chris@0 1461 m_pixmapCache = new QPixmap(w, h);
Chris@0 1462 }
Chris@0 1463
Chris@0 1464 QPainter cachePainter(m_pixmapCache);
Chris@0 1465 cachePainter.drawImage(x0, y0, scaled);
Chris@0 1466 cachePainter.end();
Chris@0 1467
Chris@0 1468 m_pixmapCacheInvalid = false;
Chris@0 1469 m_pixmapCacheStartFrame = startFrame;
Chris@0 1470 m_pixmapCacheZoomLevel = zoomLevel;
Chris@0 1471
Chris@0 1472 #ifdef DEBUG_SPECTROGRAM_REPAINT
Chris@0 1473 std::cerr << "SpectrogramLayer::paint() returning" << std::endl;
Chris@0 1474 #endif
Chris@0 1475 }
Chris@0 1476
Chris@0 1477 int
Chris@0 1478 SpectrogramLayer::getCompletion() const
Chris@0 1479 {
Chris@0 1480 if (m_updateTimer == 0) return 100;
Chris@0 1481 size_t completion = m_fillThread->getFillCompletion();
Chris@0 1482 // std::cerr << "SpectrogramLayer::getCompletion: completion = " << completion << std::endl;
Chris@0 1483 return completion;
Chris@0 1484 }
Chris@0 1485
Chris@13 1486 int
Chris@13 1487 SpectrogramLayer::getNearestFeatureFrame(int frame,
Chris@13 1488 size_t &resolution,
Chris@13 1489 bool snapRight) const
Chris@13 1490 {
Chris@13 1491 resolution = getWindowIncrement();
Chris@13 1492 int snapFrame = (frame / resolution) * resolution;
Chris@13 1493 if (snapRight) snapFrame += resolution;
Chris@13 1494 return snapFrame;
Chris@13 1495 }
Chris@13 1496
Chris@25 1497 QString
Chris@25 1498 SpectrogramLayer::getFeatureDescription(QPoint &pos) const
Chris@25 1499 {
Chris@25 1500 int x = pos.x();
Chris@25 1501 int y = pos.y();
Chris@0 1502
Chris@25 1503 if (!m_model || !m_model->isOK()) return "";
Chris@0 1504
Chris@0 1505 float dbMin = 0, dbMax = 0;
Chris@0 1506 float freqMin = 0, freqMax = 0;
Chris@25 1507 QString pitchMin, pitchMax;
Chris@0 1508 RealTime rtMin, rtMax;
Chris@0 1509
Chris@25 1510 bool haveDb = false;
Chris@0 1511
Chris@25 1512 if (!getXBinSourceRange(x, rtMin, rtMax)) return "";
Chris@25 1513 if (!getYBinSourceRange(y, freqMin, freqMax)) return "";
Chris@25 1514 if (getXYBinSourceRange(x, y, dbMin, dbMax)) haveDb = true;
Chris@0 1515
Chris@25 1516 //!!! want to actually do a one-off FFT to recalculate the dB value!
Chris@25 1517
Chris@25 1518 QString text;
Chris@25 1519
Chris@25 1520 if (rtMin != rtMax) {
Chris@25 1521 text += tr("Time:\t%1 - %2\n")
Chris@25 1522 .arg(rtMin.toText(true).c_str())
Chris@25 1523 .arg(rtMax.toText(true).c_str());
Chris@25 1524 } else {
Chris@25 1525 text += tr("Time:\t%1\n")
Chris@25 1526 .arg(rtMin.toText(true).c_str());
Chris@0 1527 }
Chris@0 1528
Chris@25 1529 if (freqMin != freqMax) {
Chris@25 1530 text += tr("Frequency:\t%1 - %2 Hz\nPitch:\t%3 - %4\n")
Chris@25 1531 .arg(freqMin)
Chris@25 1532 .arg(freqMax)
Chris@25 1533 .arg(Pitch::getPitchLabelForFrequency(freqMin))
Chris@25 1534 .arg(Pitch::getPitchLabelForFrequency(freqMax));
Chris@25 1535 } else {
Chris@25 1536 text += tr("Frequency:\t%1 Hz\nPitch:\t%2\n")
Chris@25 1537 .arg(freqMin)
Chris@25 1538 .arg(Pitch::getPitchLabelForFrequency(freqMin));
Chris@25 1539 }
Chris@25 1540
Chris@25 1541 if (haveDb) {
Chris@25 1542 if (lrintf(dbMin) != lrintf(dbMax)) {
Chris@25 1543 text += tr("dB:\t%1 - %2").arg(lrintf(dbMin)).arg(lrintf(dbMax));
Chris@25 1544 } else {
Chris@25 1545 text += tr("dB:\t%1").arg(lrintf(dbMin));
Chris@25 1546 }
Chris@25 1547 }
Chris@25 1548
Chris@25 1549 return text;
Chris@0 1550 }
Chris@25 1551
Chris@0 1552 int
Chris@0 1553 SpectrogramLayer::getVerticalScaleWidth(QPainter &paint) const
Chris@0 1554 {
Chris@0 1555 if (!m_model || !m_model->isOK()) return 0;
Chris@0 1556
Chris@0 1557 int tw = paint.fontMetrics().width(QString("%1")
Chris@0 1558 .arg(m_maxFrequency > 0 ?
Chris@0 1559 m_maxFrequency - 1 :
Chris@0 1560 m_model->getSampleRate() / 2));
Chris@0 1561
Chris@0 1562 int fw = paint.fontMetrics().width(QString("43Hz"));
Chris@0 1563 if (tw < fw) tw = fw;
Chris@0 1564
Chris@0 1565 return tw + 13;
Chris@0 1566 }
Chris@0 1567
Chris@0 1568 void
Chris@0 1569 SpectrogramLayer::paintVerticalScale(QPainter &paint, QRect rect) const
Chris@0 1570 {
Chris@0 1571 if (!m_model || !m_model->isOK()) {
Chris@0 1572 return;
Chris@0 1573 }
Chris@0 1574
Chris@0 1575 int h = rect.height(), w = rect.width();
Chris@0 1576
Chris@0 1577 size_t bins = m_windowSize / 2;
Chris@0 1578 int sr = m_model->getSampleRate();
Chris@0 1579
Chris@0 1580 if (m_maxFrequency > 0) {
Chris@0 1581 bins = int((double(m_maxFrequency) * m_windowSize) / sr + 0.1);
Chris@0 1582 if (bins > m_windowSize / 2) bins = m_windowSize / 2;
Chris@0 1583 }
Chris@0 1584
Chris@0 1585 int py = -1;
Chris@0 1586 int textHeight = paint.fontMetrics().height();
Chris@0 1587 int toff = -textHeight + paint.fontMetrics().ascent() + 2;
Chris@0 1588
Chris@0 1589 int bin = -1;
Chris@0 1590
Chris@0 1591 for (int y = 0; y < m_view->height(); ++y) {
Chris@0 1592
Chris@0 1593 float q0, q1;
Chris@0 1594 if (!getYBinRange(m_view->height() - y, q0, q1)) continue;
Chris@0 1595
Chris@0 1596 int vy;
Chris@0 1597
Chris@0 1598 if (int(q0) > bin) {
Chris@0 1599 vy = y;
Chris@0 1600 bin = int(q0);
Chris@0 1601 } else {
Chris@0 1602 continue;
Chris@0 1603 }
Chris@0 1604
Chris@0 1605 int freq = (sr * (bin + 1)) / m_windowSize;
Chris@0 1606
Chris@0 1607 if (py >= 0 && (vy - py) < textHeight - 1) {
Chris@0 1608 paint.drawLine(w - 4, h - vy, w, h - vy);
Chris@0 1609 continue;
Chris@0 1610 }
Chris@0 1611
Chris@0 1612 QString text = QString("%1").arg(freq);
Chris@0 1613 if (bin == 0) text = QString("%1Hz").arg(freq);
Chris@0 1614 paint.drawLine(0, h - vy, w, h - vy);
Chris@0 1615
Chris@0 1616 if (h - vy - textHeight >= -2) {
Chris@0 1617 int tx = w - 10 - paint.fontMetrics().width(text);
Chris@0 1618 paint.drawText(tx, h - vy + toff, text);
Chris@0 1619 }
Chris@0 1620
Chris@0 1621 py = vy;
Chris@0 1622 }
Chris@0 1623 }
Chris@0 1624
Chris@6 1625 QString
Chris@6 1626 SpectrogramLayer::toXmlString(QString indent, QString extraAttributes) const
Chris@6 1627 {
Chris@6 1628 QString s;
Chris@6 1629
Chris@6 1630 s += QString("channel=\"%1\" "
Chris@6 1631 "windowSize=\"%2\" "
Chris@6 1632 "windowType=\"%3\" "
Chris@6 1633 "windowOverlap=\"%4\" "
Chris@6 1634 "gain=\"%5\" "
Chris@6 1635 "maxFrequency=\"%6\" "
Chris@6 1636 "colourScale=\"%7\" "
Chris@6 1637 "colourScheme=\"%8\" "
Chris@6 1638 "frequencyScale=\"%9\"")
Chris@6 1639 .arg(m_channel)
Chris@6 1640 .arg(m_windowSize)
Chris@6 1641 .arg(m_windowType)
Chris@6 1642 .arg(m_windowOverlap)
Chris@6 1643 .arg(m_gain)
Chris@6 1644 .arg(m_maxFrequency)
Chris@6 1645 .arg(m_colourScale)
Chris@6 1646 .arg(m_colourScheme)
Chris@6 1647 .arg(m_frequencyScale);
Chris@6 1648
Chris@6 1649 return Layer::toXmlString(indent, extraAttributes + " " + s);
Chris@6 1650 }
Chris@6 1651
Chris@11 1652 void
Chris@11 1653 SpectrogramLayer::setProperties(const QXmlAttributes &attributes)
Chris@11 1654 {
Chris@11 1655 bool ok = false;
Chris@11 1656
Chris@11 1657 int channel = attributes.value("channel").toInt(&ok);
Chris@11 1658 if (ok) setChannel(channel);
Chris@11 1659
Chris@11 1660 size_t windowSize = attributes.value("windowSize").toUInt(&ok);
Chris@11 1661 if (ok) setWindowSize(windowSize);
Chris@11 1662
Chris@11 1663 WindowType windowType = (WindowType)
Chris@11 1664 attributes.value("windowType").toInt(&ok);
Chris@11 1665 if (ok) setWindowType(windowType);
Chris@11 1666
Chris@11 1667 size_t windowOverlap = attributes.value("windowOverlap").toUInt(&ok);
Chris@11 1668 if (ok) setWindowOverlap(windowOverlap);
Chris@11 1669
Chris@11 1670 float gain = attributes.value("gain").toFloat(&ok);
Chris@11 1671 if (ok) setGain(gain);
Chris@11 1672
Chris@11 1673 size_t maxFrequency = attributes.value("maxFrequency").toUInt(&ok);
Chris@11 1674 if (ok) setMaxFrequency(maxFrequency);
Chris@11 1675
Chris@11 1676 ColourScale colourScale = (ColourScale)
Chris@11 1677 attributes.value("colourScale").toInt(&ok);
Chris@11 1678 if (ok) setColourScale(colourScale);
Chris@11 1679
Chris@11 1680 ColourScheme colourScheme = (ColourScheme)
Chris@11 1681 attributes.value("colourScheme").toInt(&ok);
Chris@11 1682 if (ok) setColourScheme(colourScheme);
Chris@11 1683
Chris@11 1684 FrequencyScale frequencyScale = (FrequencyScale)
Chris@11 1685 attributes.value("frequencyScale").toInt(&ok);
Chris@11 1686 if (ok) setFrequencyScale(frequencyScale);
Chris@11 1687 }
Chris@11 1688
Chris@11 1689
Chris@0 1690 #ifdef INCLUDE_MOCFILES
Chris@0 1691 #include "SpectrogramLayer.moc.cpp"
Chris@0 1692 #endif
Chris@0 1693