annotate layer/SpectrogramLayer.cpp @ 91:ed01c1261b55

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