annotate layer/SpectrogramLayer.cpp @ 114:991de8783cf5

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