annotate layer/SpectrogramLayer.cpp @ 34:c43f2c4f66f2

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