annotate layer/SpectrogramLayer.cpp @ 69:6dad2724f3aa

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