annotate layer/SpectrogramLayer.cpp @ 85:d31c4f5230d7

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