annotate layer/SpectrogramLayer.cpp @ 112:43b1ca9647e1

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