annotate layer/SpectrumLayer.cpp @ 348:0093f351641c

* add splash screen
author Chris Cannam
date Wed, 23 Jan 2008 11:38:34 +0000
parents 8c3fb13ef11c
children 369a197737c7
rev   line source
Chris@133 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@133 2
Chris@133 3 /*
Chris@133 4 Sonic Visualiser
Chris@133 5 An audio file viewer and annotation editor.
Chris@133 6 Centre for Digital Music, Queen Mary, University of London.
Chris@195 7 This file copyright 2006-2007 QMUL.
Chris@133 8
Chris@133 9 This program is free software; you can redistribute it and/or
Chris@133 10 modify it under the terms of the GNU General Public License as
Chris@133 11 published by the Free Software Foundation; either version 2 of the
Chris@133 12 License, or (at your option) any later version. See the file
Chris@133 13 COPYING included with this distribution for more information.
Chris@133 14 */
Chris@133 15
Chris@133 16 #include "SpectrumLayer.h"
Chris@133 17
Chris@133 18 #include "data/model/FFTModel.h"
Chris@133 19 #include "view/View.h"
Chris@153 20 #include "base/AudioLevel.h"
Chris@153 21 #include "base/Preferences.h"
Chris@167 22 #include "base/RangeMapper.h"
Chris@277 23 #include "base/Pitch.h"
Chris@285 24 #include "base/ColourMapper.h"
Chris@254 25
Chris@254 26 #include <QPainter>
Chris@316 27 #include <QTextStream>
Chris@316 28
Chris@133 29
Chris@133 30 SpectrumLayer::SpectrumLayer() :
Chris@193 31 m_originModel(0),
Chris@153 32 m_channel(-1),
Chris@153 33 m_channelSet(false),
Chris@290 34 m_windowSize(4096),
Chris@153 35 m_windowType(HanningWindow),
Chris@290 36 m_windowHopLevel(3),
Chris@284 37 m_showPeaks(false),
Chris@275 38 m_newFFTNeeded(true)
Chris@133 39 {
Chris@153 40 Preferences *prefs = Preferences::getInstance();
Chris@153 41 connect(prefs, SIGNAL(propertyChanged(PropertyContainer::PropertyName)),
Chris@153 42 this, SLOT(preferenceChanged(PropertyContainer::PropertyName)));
Chris@153 43 setWindowType(prefs->getWindowType());
Chris@195 44
Chris@195 45 setBinScale(LogBins);
Chris@133 46 }
Chris@133 47
Chris@133 48 SpectrumLayer::~SpectrumLayer()
Chris@133 49 {
Chris@193 50 //!!! delete parent's model
Chris@193 51 // for (size_t i = 0; i < m_fft.size(); ++i) delete m_fft[i];
Chris@133 52 }
Chris@133 53
Chris@133 54 void
Chris@133 55 SpectrumLayer::setModel(DenseTimeValueModel *model)
Chris@133 56 {
Chris@345 57 std::cerr << "SpectrumLayer::setModel(" << model << ") from " << m_originModel << std::endl;
Chris@345 58
Chris@193 59 if (m_originModel == model) return;
Chris@193 60 m_originModel = model;
Chris@277 61
Chris@277 62 if (m_sliceableModel) {
Chris@277 63 const Model *oldModel = m_sliceableModel;
Chris@277 64 setSliceableModel(0);
Chris@277 65 // surprised I'm allowed to delete a const pointer -- may be a
Chris@277 66 // source of future compiler rejection?
Chris@277 67 delete oldModel;
Chris@277 68 }
Chris@275 69 //!!! setupFFT();
Chris@153 70 }
Chris@153 71
Chris@153 72 void
Chris@193 73 SpectrumLayer::setupFFT()
Chris@153 74 {
Chris@193 75 FFTModel *oldFFT = dynamic_cast<FFTModel *>
Chris@193 76 (const_cast<DenseThreeDimensionalModel *>(m_sliceableModel));
Chris@153 77
Chris@193 78 if (oldFFT) {
Chris@193 79 setSliceableModel(0);
Chris@193 80 delete oldFFT;
Chris@153 81 }
Chris@153 82
Chris@193 83 FFTModel *newFFT = new FFTModel(m_originModel,
Chris@193 84 m_channel,
Chris@193 85 m_windowType,
Chris@193 86 m_windowSize,
Chris@193 87 getWindowIncrement(),
Chris@193 88 m_windowSize,
Chris@327 89 false,
Chris@327 90 StorageAdviser::Criteria
Chris@327 91 (StorageAdviser::SpeedCritical |
Chris@327 92 StorageAdviser::FrequentLookupLikely));
Chris@153 93
Chris@193 94 setSliceableModel(newFFT);
Chris@193 95
Chris@254 96 m_biasCurve.clear();
Chris@254 97 for (size_t i = 0; i < m_windowSize; ++i) {
Chris@254 98 m_biasCurve.push_back(1.f / (float(m_windowSize)/2.f));
Chris@254 99 }
Chris@254 100
Chris@193 101 newFFT->resume();
Chris@153 102 }
Chris@153 103
Chris@153 104 void
Chris@153 105 SpectrumLayer::setChannel(int channel)
Chris@153 106 {
Chris@153 107 m_channelSet = true;
Chris@153 108
Chris@193 109 FFTModel *fft = dynamic_cast<FFTModel *>
Chris@193 110 (const_cast<DenseThreeDimensionalModel *>(m_sliceableModel));
Chris@193 111
Chris@153 112 if (m_channel == channel) {
Chris@193 113 if (fft) fft->resume();
Chris@153 114 return;
Chris@153 115 }
Chris@153 116
Chris@153 117 m_channel = channel;
Chris@153 118
Chris@153 119 emit layerParametersChanged();
Chris@133 120 }
Chris@133 121
Chris@153 122 Layer::PropertyList
Chris@153 123 SpectrumLayer::getProperties() const
Chris@153 124 {
Chris@193 125 PropertyList list = SliceLayer::getProperties();
Chris@153 126 list.push_back("Window Size");
Chris@153 127 list.push_back("Window Increment");
Chris@284 128 list.push_back("Show Peak Frequencies");
Chris@153 129 return list;
Chris@153 130 }
Chris@153 131
Chris@153 132 QString
Chris@153 133 SpectrumLayer::getPropertyLabel(const PropertyName &name) const
Chris@153 134 {
Chris@153 135 if (name == "Window Size") return tr("Window Size");
Chris@153 136 if (name == "Window Increment") return tr("Window Overlap");
Chris@284 137 if (name == "Show Peak Frequencies") return tr("Show Peak Frequencies");
Chris@193 138 return SliceLayer::getPropertyLabel(name);
Chris@153 139 }
Chris@153 140
Chris@335 141 QString
Chris@335 142 SpectrumLayer::getPropertyIconName(const PropertyName &name) const
Chris@335 143 {
Chris@335 144 if (name == "Show Peak Frequencies") return "show-peaks";
Chris@335 145 return SliceLayer::getPropertyIconName(name);
Chris@335 146 }
Chris@335 147
Chris@153 148 Layer::PropertyType
Chris@153 149 SpectrumLayer::getPropertyType(const PropertyName &name) const
Chris@153 150 {
Chris@193 151 if (name == "Window Size") return ValueProperty;
Chris@193 152 if (name == "Window Increment") return ValueProperty;
Chris@284 153 if (name == "Show Peak Frequencies") return ToggleProperty;
Chris@193 154 return SliceLayer::getPropertyType(name);
Chris@153 155 }
Chris@153 156
Chris@153 157 QString
Chris@153 158 SpectrumLayer::getPropertyGroupName(const PropertyName &name) const
Chris@153 159 {
Chris@153 160 if (name == "Window Size" ||
Chris@153 161 name == "Window Increment") return tr("Window");
Chris@284 162 if (name == "Show Peak Frequencies") return tr("Plot Type");
Chris@193 163 return SliceLayer::getPropertyGroupName(name);
Chris@153 164 }
Chris@153 165
Chris@153 166 int
Chris@153 167 SpectrumLayer::getPropertyRangeAndValue(const PropertyName &name,
Chris@216 168 int *min, int *max, int *deflt) const
Chris@153 169 {
Chris@216 170 int val = 0;
Chris@153 171
Chris@216 172 int garbage0, garbage1, garbage2;
Chris@153 173 if (!min) min = &garbage0;
Chris@153 174 if (!max) max = &garbage1;
Chris@216 175 if (!deflt) deflt = &garbage2;
Chris@153 176
Chris@193 177 if (name == "Window Size") {
Chris@153 178
Chris@153 179 *min = 0;
Chris@254 180 *max = 15;
Chris@216 181 *deflt = 5;
Chris@153 182
Chris@216 183 val = 0;
Chris@153 184 int ws = m_windowSize;
Chris@216 185 while (ws > 32) { ws >>= 1; val ++; }
Chris@153 186
Chris@153 187 } else if (name == "Window Increment") {
Chris@153 188
Chris@153 189 *min = 0;
Chris@153 190 *max = 5;
Chris@216 191 *deflt = 2;
Chris@153 192
Chris@216 193 val = m_windowHopLevel;
Chris@153 194
Chris@284 195 } else if (name == "Show Peak Frequencies") {
Chris@284 196
Chris@284 197 return m_showPeaks ? 1 : 0;
Chris@284 198
Chris@153 199 } else {
Chris@193 200
Chris@216 201 val = SliceLayer::getPropertyRangeAndValue(name, min, max, deflt);
Chris@153 202 }
Chris@153 203
Chris@216 204 return val;
Chris@153 205 }
Chris@153 206
Chris@153 207 QString
Chris@153 208 SpectrumLayer::getPropertyValueLabel(const PropertyName &name,
Chris@153 209 int value) const
Chris@153 210 {
Chris@153 211 if (name == "Window Size") {
Chris@153 212 return QString("%1").arg(32 << value);
Chris@153 213 }
Chris@153 214 if (name == "Window Increment") {
Chris@153 215 switch (value) {
Chris@153 216 default:
Chris@153 217 case 0: return tr("None");
Chris@153 218 case 1: return tr("25 %");
Chris@153 219 case 2: return tr("50 %");
Chris@153 220 case 3: return tr("75 %");
Chris@153 221 case 4: return tr("87.5 %");
Chris@153 222 case 5: return tr("93.75 %");
Chris@153 223 }
Chris@153 224 }
Chris@193 225 return SliceLayer::getPropertyValueLabel(name, value);
Chris@153 226 }
Chris@153 227
Chris@167 228 RangeMapper *
Chris@167 229 SpectrumLayer::getNewPropertyRangeMapper(const PropertyName &name) const
Chris@167 230 {
Chris@193 231 return SliceLayer::getNewPropertyRangeMapper(name);
Chris@167 232 }
Chris@167 233
Chris@133 234 void
Chris@153 235 SpectrumLayer::setProperty(const PropertyName &name, int value)
Chris@133 236 {
Chris@193 237 if (name == "Window Size") {
Chris@153 238 setWindowSize(32 << value);
Chris@153 239 } else if (name == "Window Increment") {
Chris@153 240 setWindowHopLevel(value);
Chris@284 241 } else if (name == "Show Peak Frequencies") {
Chris@284 242 setShowPeaks(value ? true : false);
Chris@193 243 } else {
Chris@193 244 SliceLayer::setProperty(name, value);
Chris@153 245 }
Chris@153 246 }
Chris@153 247
Chris@153 248 void
Chris@153 249 SpectrumLayer::setWindowSize(size_t ws)
Chris@153 250 {
Chris@153 251 if (m_windowSize == ws) return;
Chris@153 252 m_windowSize = ws;
Chris@275 253 m_newFFTNeeded = true;
Chris@153 254 emit layerParametersChanged();
Chris@153 255 }
Chris@153 256
Chris@153 257 void
Chris@153 258 SpectrumLayer::setWindowHopLevel(size_t v)
Chris@153 259 {
Chris@153 260 if (m_windowHopLevel == v) return;
Chris@153 261 m_windowHopLevel = v;
Chris@275 262 m_newFFTNeeded = true;
Chris@153 263 emit layerParametersChanged();
Chris@153 264 }
Chris@153 265
Chris@153 266 void
Chris@153 267 SpectrumLayer::setWindowType(WindowType w)
Chris@153 268 {
Chris@153 269 if (m_windowType == w) return;
Chris@153 270 m_windowType = w;
Chris@275 271 m_newFFTNeeded = true;
Chris@153 272 emit layerParametersChanged();
Chris@153 273 }
Chris@153 274
Chris@153 275 void
Chris@284 276 SpectrumLayer::setShowPeaks(bool show)
Chris@284 277 {
Chris@284 278 if (m_showPeaks == show) return;
Chris@284 279 m_showPeaks = show;
Chris@284 280 emit layerParametersChanged();
Chris@284 281 }
Chris@284 282
Chris@284 283 void
Chris@153 284 SpectrumLayer::preferenceChanged(PropertyContainer::PropertyName name)
Chris@153 285 {
Chris@153 286 if (name == "Window Type") {
Chris@153 287 setWindowType(Preferences::getInstance()->getWindowType());
Chris@153 288 return;
Chris@153 289 }
Chris@153 290 }
Chris@153 291
Chris@133 292 bool
Chris@248 293 SpectrumLayer::getValueExtents(float &, float &, bool &, QString &) const
Chris@133 294 {
Chris@133 295 return false;
Chris@133 296 }
Chris@133 297
Chris@254 298 float
Chris@265 299 SpectrumLayer::getXForBin(int bin, int totalBins, float w) const
Chris@265 300 {
Chris@265 301 if (!m_sliceableModel) return SliceLayer::getXForBin(bin, totalBins, w);
Chris@265 302
Chris@265 303 float sampleRate = m_sliceableModel->getSampleRate();
Chris@265 304 float binfreq = (sampleRate * bin) / (totalBins * 2);
Chris@265 305
Chris@265 306 return getXForFrequency(binfreq, w);
Chris@265 307 }
Chris@265 308
Chris@265 309 int
Chris@265 310 SpectrumLayer::getBinForX(float x, int totalBins, float w) const
Chris@265 311 {
Chris@265 312 if (!m_sliceableModel) return SliceLayer::getBinForX(x, totalBins, w);
Chris@265 313
Chris@265 314 float sampleRate = m_sliceableModel->getSampleRate();
Chris@265 315 float binfreq = getFrequencyForX(x, w);
Chris@265 316
Chris@265 317 return int((binfreq * totalBins * 2) / sampleRate);
Chris@265 318 }
Chris@265 319
Chris@265 320 float
Chris@254 321 SpectrumLayer::getFrequencyForX(float x, float w) const
Chris@254 322 {
Chris@254 323 float freq = 0;
Chris@280 324 if (!m_sliceableModel) return 0;
Chris@254 325
Chris@254 326 int sampleRate = m_sliceableModel->getSampleRate();
Chris@254 327
Chris@254 328 float maxfreq = float(sampleRate) / 2;
Chris@254 329
Chris@254 330 switch (m_binScale) {
Chris@254 331
Chris@254 332 case LinearBins:
Chris@254 333 freq = ((x * maxfreq) / w);
Chris@254 334 break;
Chris@254 335
Chris@254 336 case LogBins:
Chris@254 337 freq = powf(10.f, (x * log10f(maxfreq)) / w);
Chris@254 338 break;
Chris@254 339
Chris@254 340 case InvertedLogBins:
Chris@254 341 freq = maxfreq - powf(10.f, ((w - x) * log10f(maxfreq)) / w);
Chris@254 342 break;
Chris@254 343 }
Chris@254 344
Chris@254 345 return freq;
Chris@254 346 }
Chris@254 347
Chris@254 348 float
Chris@254 349 SpectrumLayer::getXForFrequency(float freq, float w) const
Chris@254 350 {
Chris@254 351 float x = 0;
Chris@280 352 if (!m_sliceableModel) return x;
Chris@254 353
Chris@254 354 int sampleRate = m_sliceableModel->getSampleRate();
Chris@254 355
Chris@254 356 float maxfreq = float(sampleRate) / 2;
Chris@254 357
Chris@254 358 switch (m_binScale) {
Chris@254 359
Chris@254 360 case LinearBins:
Chris@254 361 x = (freq * w) / maxfreq;
Chris@254 362 break;
Chris@254 363
Chris@254 364 case LogBins:
Chris@254 365 x = (log10f(freq) * w) / log10f(maxfreq);
Chris@254 366 break;
Chris@254 367
Chris@254 368 case InvertedLogBins:
Chris@274 369 if (maxfreq == freq) x = w;
Chris@274 370 else x = w - (log10f(maxfreq - freq) * w) / log10f(maxfreq);
Chris@254 371 break;
Chris@254 372 }
Chris@254 373
Chris@254 374 return x;
Chris@254 375 }
Chris@254 376
Chris@260 377 bool
Chris@267 378 SpectrumLayer::getXScaleValue(const View *v, int x,
Chris@260 379 float &value, QString &unit) const
Chris@260 380 {
Chris@267 381 if (m_xorigins.find(v) == m_xorigins.end()) return false;
Chris@267 382 int xorigin = m_xorigins.find(v)->second;
Chris@267 383 value = getFrequencyForX(x - xorigin, v->width() - xorigin - 1);
Chris@260 384 unit = "Hz";
Chris@260 385 return true;
Chris@260 386 }
Chris@260 387
Chris@264 388 bool
Chris@274 389 SpectrumLayer::getYScaleValue(const View *v, int y,
Chris@274 390 float &value, QString &unit) const
Chris@274 391 {
Chris@274 392 value = getValueForY(y, v);
Chris@274 393
Chris@274 394 if (m_energyScale == dBScale || m_energyScale == MeterScale) {
Chris@274 395
Chris@274 396 if (value > 0.f) {
Chris@274 397 value = 10.f * log10f(value);
Chris@284 398 if (value < m_threshold) value = m_threshold;
Chris@284 399 } else value = m_threshold;
Chris@274 400
Chris@274 401 unit = "dBV";
Chris@274 402
Chris@274 403 } else {
Chris@274 404 unit = "V";
Chris@274 405 }
Chris@274 406
Chris@274 407 return true;
Chris@274 408 }
Chris@274 409
Chris@274 410 bool
Chris@274 411 SpectrumLayer::getYScaleDifference(const View *v, int y0, int y1,
Chris@274 412 float &diff, QString &unit) const
Chris@274 413 {
Chris@274 414 bool rv = SliceLayer::getYScaleDifference(v, y0, y1, diff, unit);
Chris@274 415 if (rv && (unit == "dBV")) unit = "dB";
Chris@274 416 return rv;
Chris@274 417 }
Chris@274 418
Chris@274 419
Chris@274 420 bool
Chris@264 421 SpectrumLayer::getCrosshairExtents(View *v, QPainter &paint,
Chris@264 422 QPoint cursorPos,
Chris@264 423 std::vector<QRect> &extents) const
Chris@264 424 {
Chris@264 425 QRect vertical(cursorPos.x(), cursorPos.y(), 1, v->height() - cursorPos.y());
Chris@264 426 extents.push_back(vertical);
Chris@264 427
Chris@264 428 QRect horizontal(0, cursorPos.y(), v->width(), 12);
Chris@264 429 extents.push_back(horizontal);
Chris@264 430
Chris@280 431 int hoffset = 2;
Chris@280 432 if (m_binScale == LogBins) hoffset = 13;
Chris@278 433
Chris@280 434 int sw = getVerticalScaleWidth(v, paint);
Chris@280 435
Chris@280 436 QRect value(sw, cursorPos.y() - paint.fontMetrics().ascent() - 2,
Chris@280 437 paint.fontMetrics().width("0.0000001 V") + 2,
Chris@264 438 paint.fontMetrics().height());
Chris@280 439 extents.push_back(value);
Chris@280 440
Chris@280 441 QRect log(sw, cursorPos.y() + 2,
Chris@280 442 paint.fontMetrics().width("-80.000 dBV") + 2,
Chris@280 443 paint.fontMetrics().height());
Chris@280 444 extents.push_back(log);
Chris@280 445
Chris@280 446 QRect freq(cursorPos.x(),
Chris@280 447 v->height() - paint.fontMetrics().height() - hoffset,
Chris@280 448 paint.fontMetrics().width("123456 Hz") + 2,
Chris@280 449 paint.fontMetrics().height());
Chris@280 450 extents.push_back(freq);
Chris@264 451
Chris@278 452 int w(paint.fontMetrics().width("C#10+50c") + 2);
Chris@278 453 QRect pitch(cursorPos.x() - w,
Chris@278 454 v->height() - paint.fontMetrics().height() - hoffset,
Chris@278 455 w,
Chris@278 456 paint.fontMetrics().height());
Chris@278 457 extents.push_back(pitch);
Chris@278 458
Chris@264 459 return true;
Chris@264 460 }
Chris@264 461
Chris@254 462 void
Chris@254 463 SpectrumLayer::paintCrosshairs(View *v, QPainter &paint,
Chris@254 464 QPoint cursorPos) const
Chris@254 465 {
Chris@280 466 if (!m_sliceableModel) return;
Chris@280 467
Chris@254 468 paint.save();
Chris@282 469 QFont fn = paint.font();
Chris@282 470 if (fn.pointSize() > 8) {
Chris@282 471 fn.setPointSize(fn.pointSize() - 1);
Chris@282 472 paint.setFont(fn);
Chris@282 473 }
Chris@254 474
Chris@254 475 ColourMapper mapper(m_colourMap, 0, 1);
Chris@254 476 paint.setPen(mapper.getContrastingColour());
Chris@254 477
Chris@254 478 int xorigin = m_xorigins[v];
Chris@254 479 int w = v->width() - xorigin - 1;
Chris@254 480
Chris@254 481 paint.drawLine(xorigin, cursorPos.y(), v->width(), cursorPos.y());
Chris@254 482 paint.drawLine(cursorPos.x(), cursorPos.y(), cursorPos.x(), v->height());
Chris@254 483
Chris@254 484 float fundamental = getFrequencyForX(cursorPos.x() - xorigin, w);
Chris@254 485
Chris@280 486 int hoffset = 2;
Chris@280 487 if (m_binScale == LogBins) hoffset = 13;
Chris@278 488
Chris@278 489 v->drawVisibleText(paint,
Chris@278 490 cursorPos.x() + 2,
Chris@278 491 v->height() - 2 - hoffset,
Chris@278 492 QString("%1 Hz").arg(fundamental),
Chris@278 493 View::OutlinedText);
Chris@278 494
Chris@278 495 if (Pitch::isFrequencyInMidiRange(fundamental)) {
Chris@278 496 QString pitchLabel = Pitch::getPitchLabelForFrequency(fundamental);
Chris@278 497 v->drawVisibleText(paint,
Chris@278 498 cursorPos.x() - paint.fontMetrics().width(pitchLabel) - 2,
Chris@278 499 v->height() - 2 - hoffset,
Chris@278 500 pitchLabel,
Chris@278 501 View::OutlinedText);
Chris@278 502 }
Chris@264 503
Chris@280 504 float value = getValueForY(cursorPos.y(), v);
Chris@284 505 float thresh = m_threshold;
Chris@280 506 float db = thresh;
Chris@280 507 if (value > 0.f) db = 10.f * log10f(value);
Chris@280 508 if (db < thresh) db = thresh;
Chris@280 509
Chris@280 510 v->drawVisibleText(paint,
Chris@280 511 xorigin + 2,
Chris@280 512 cursorPos.y() - 2,
Chris@280 513 QString("%1 V").arg(value),
Chris@280 514 View::OutlinedText);
Chris@280 515
Chris@280 516 v->drawVisibleText(paint,
Chris@280 517 xorigin + 2,
Chris@280 518 cursorPos.y() + 2 + paint.fontMetrics().ascent(),
Chris@280 519 QString("%1 dBV").arg(db),
Chris@280 520 View::OutlinedText);
Chris@280 521
Chris@254 522 int harmonic = 2;
Chris@254 523
Chris@254 524 while (harmonic < 100) {
Chris@254 525
Chris@254 526 float hx = lrintf(getXForFrequency(fundamental * harmonic, w));
Chris@254 527 hx += xorigin;
Chris@254 528
Chris@254 529 if (hx < xorigin || hx > v->width()) break;
Chris@254 530
Chris@254 531 int len = 7;
Chris@254 532
Chris@254 533 if (harmonic % 2 == 0) {
Chris@254 534 if (harmonic % 4 == 0) {
Chris@254 535 len = 12;
Chris@254 536 } else {
Chris@254 537 len = 10;
Chris@254 538 }
Chris@254 539 }
Chris@254 540
Chris@254 541 paint.drawLine(int(hx),
Chris@254 542 cursorPos.y(),
Chris@254 543 int(hx),
Chris@254 544 cursorPos.y() + len);
Chris@254 545
Chris@254 546 ++harmonic;
Chris@254 547 }
Chris@254 548
Chris@254 549 paint.restore();
Chris@254 550 }
Chris@254 551
Chris@199 552 QString
Chris@199 553 SpectrumLayer::getFeatureDescription(View *v, QPoint &p) const
Chris@199 554 {
Chris@199 555 if (!m_sliceableModel) return "";
Chris@199 556
Chris@199 557 int minbin = 0, maxbin = 0, range = 0;
Chris@199 558 QString genericDesc = SliceLayer::getFeatureDescription
Chris@199 559 (v, p, false, minbin, maxbin, range);
Chris@199 560
Chris@199 561 if (genericDesc == "") return "";
Chris@199 562
Chris@199 563 float minvalue = 0.f;
Chris@248 564 if (minbin < int(m_values.size())) minvalue = m_values[minbin];
Chris@199 565
Chris@199 566 float maxvalue = minvalue;
Chris@248 567 if (maxbin < int(m_values.size())) maxvalue = m_values[maxbin];
Chris@199 568
Chris@199 569 if (minvalue > maxvalue) std::swap(minvalue, maxvalue);
Chris@199 570
Chris@199 571 QString binstr;
Chris@199 572 QString hzstr;
Chris@199 573 int minfreq = lrintf((minbin * m_sliceableModel->getSampleRate()) /
Chris@199 574 m_windowSize);
Chris@199 575 int maxfreq = lrintf((std::max(maxbin, minbin+1)
Chris@199 576 * m_sliceableModel->getSampleRate()) /
Chris@199 577 m_windowSize);
Chris@199 578
Chris@199 579 if (maxbin != minbin) {
Chris@199 580 binstr = tr("%1 - %2").arg(minbin+1).arg(maxbin+1);
Chris@199 581 } else {
Chris@199 582 binstr = QString("%1").arg(minbin+1);
Chris@199 583 }
Chris@199 584 if (minfreq != maxfreq) {
Chris@199 585 hzstr = tr("%1 - %2 Hz").arg(minfreq).arg(maxfreq);
Chris@199 586 } else {
Chris@199 587 hzstr = tr("%1 Hz").arg(minfreq);
Chris@199 588 }
Chris@199 589
Chris@199 590 QString valuestr;
Chris@199 591 if (maxvalue != minvalue) {
Chris@199 592 valuestr = tr("%1 - %2").arg(minvalue).arg(maxvalue);
Chris@199 593 } else {
Chris@199 594 valuestr = QString("%1").arg(minvalue);
Chris@199 595 }
Chris@199 596
Chris@199 597 QString dbstr;
Chris@199 598 float mindb = AudioLevel::multiplier_to_dB(minvalue);
Chris@199 599 float maxdb = AudioLevel::multiplier_to_dB(maxvalue);
Chris@199 600 QString mindbstr;
Chris@199 601 QString maxdbstr;
Chris@199 602 if (mindb == AudioLevel::DB_FLOOR) {
Chris@199 603 mindbstr = tr("-Inf");
Chris@199 604 } else {
Chris@199 605 mindbstr = QString("%1").arg(lrintf(mindb));
Chris@199 606 }
Chris@199 607 if (maxdb == AudioLevel::DB_FLOOR) {
Chris@199 608 maxdbstr = tr("-Inf");
Chris@199 609 } else {
Chris@199 610 maxdbstr = QString("%1").arg(lrintf(maxdb));
Chris@199 611 }
Chris@199 612 if (lrintf(mindb) != lrintf(maxdb)) {
Chris@199 613 dbstr = tr("%1 - %2").arg(mindbstr).arg(maxdbstr);
Chris@199 614 } else {
Chris@199 615 dbstr = tr("%1").arg(mindbstr);
Chris@199 616 }
Chris@199 617
Chris@199 618 QString description;
Chris@199 619
Chris@248 620 if (range > int(m_sliceableModel->getResolution())) {
Chris@199 621 description = tr("%1\nBin:\t%2 (%3)\n%4 value:\t%5\ndB:\t%6")
Chris@199 622 .arg(genericDesc)
Chris@199 623 .arg(binstr)
Chris@199 624 .arg(hzstr)
Chris@199 625 .arg(m_samplingMode == NearestSample ? tr("First") :
Chris@199 626 m_samplingMode == SampleMean ? tr("Mean") : tr("Peak"))
Chris@199 627 .arg(valuestr)
Chris@199 628 .arg(dbstr);
Chris@199 629 } else {
Chris@199 630 description = tr("%1\nBin:\t%2 (%3)\nValue:\t%4\ndB:\t%5")
Chris@199 631 .arg(genericDesc)
Chris@199 632 .arg(binstr)
Chris@199 633 .arg(hzstr)
Chris@199 634 .arg(valuestr)
Chris@199 635 .arg(dbstr);
Chris@199 636 }
Chris@199 637
Chris@199 638 return description;
Chris@199 639 }
Chris@199 640
Chris@254 641 void
Chris@275 642 SpectrumLayer::paint(View *v, QPainter &paint, QRect rect) const
Chris@275 643 {
Chris@275 644 if (!m_originModel || !m_originModel->isOK() ||
Chris@275 645 !m_originModel->isReady()) return;
Chris@275 646
Chris@275 647 if (m_newFFTNeeded) {
Chris@275 648 const_cast<SpectrumLayer *>(this)->setupFFT(); //ugh
Chris@275 649 m_newFFTNeeded = false;
Chris@275 650 }
Chris@277 651
Chris@277 652 FFTModel *fft = dynamic_cast<FFTModel *>
Chris@277 653 (const_cast<DenseThreeDimensionalModel *>(m_sliceableModel));
Chris@277 654
Chris@280 655 float thresh = (powf(10, -6) / m_gain) * (m_windowSize / 2.f); // -60dB adj
Chris@277 656
Chris@277 657 int xorigin = getVerticalScaleWidth(v, paint) + 1;
Chris@277 658 int w = v->width() - xorigin - 1;
Chris@277 659
Chris@278 660 int pkh = 0;
Chris@287 661 //!!! if (m_binScale == LogBins) {
Chris@287 662 pkh = 10;
Chris@287 663 //!!! }
Chris@278 664
Chris@345 665 paint.save();
Chris@345 666
Chris@284 667 if (fft && m_showPeaks) {
Chris@277 668
Chris@277 669 // draw peak lines
Chris@277 670
Chris@277 671 size_t col = v->getCentreFrame() / fft->getResolution();
Chris@277 672
Chris@277 673 paint.save();
Chris@277 674 paint.setRenderHint(QPainter::Antialiasing, false);
Chris@277 675 paint.setPen(QColor(160, 160, 160)); //!!!
Chris@277 676
Chris@290 677 int peakminbin = 0;
Chris@290 678 int peakmaxbin = fft->getHeight() - 1;
Chris@290 679 float peakmaxfreq = Pitch::getFrequencyForPitch(128);
Chris@290 680 peakmaxbin = ((peakmaxfreq * fft->getHeight() * 2) / fft->getSampleRate());
Chris@290 681
Chris@280 682 FFTModel::PeakSet peaks = fft->getPeakFrequencies
Chris@290 683 (FFTModel::MajorPitchAdaptivePeaks, col, peakminbin, peakmaxbin);
Chris@280 684
Chris@280 685 ColourMapper mapper(ColourMapper::BlackOnWhite, 0, 1);
Chris@277 686
Chris@277 687 BiasCurve curve;
Chris@277 688 getBiasCurve(curve);
Chris@277 689 size_t cs = curve.size();
Chris@280 690
Chris@280 691 std::vector<float> values;
Chris@277 692
Chris@277 693 for (size_t bin = 0; bin < fft->getHeight(); ++bin) {
Chris@280 694 float value = m_sliceableModel->getValueAt(col, bin);
Chris@280 695 if (bin < cs) value *= curve[bin];
Chris@280 696 values.push_back(value);
Chris@280 697 }
Chris@280 698
Chris@280 699 for (FFTModel::PeakSet::iterator i = peaks.begin();
Chris@280 700 i != peaks.end(); ++i) {
Chris@280 701
Chris@280 702 size_t bin = i->first;
Chris@277 703
Chris@280 704 // std::cerr << "bin = " << bin << ", thresh = " << thresh << ", value = " << fft->getMagnitudeAt(col, bin) << std::endl;
Chris@280 705
Chris@277 706 if (!fft->isOverThreshold(col, bin, thresh)) continue;
Chris@277 707
Chris@280 708 float freq = i->second;
Chris@280 709
Chris@277 710 int x = lrintf(getXForFrequency(freq, w));
Chris@277 711
Chris@277 712 float norm = 0.f;
Chris@280 713 float y = getYForValue(values[bin], v, norm); // don't need y, need norm
Chris@277 714
Chris@277 715 paint.setPen(mapper.map(norm));
Chris@278 716 paint.drawLine(xorigin + x, 0, xorigin + x, v->height() - pkh - 1);
Chris@277 717 }
Chris@277 718
Chris@277 719 paint.restore();
Chris@277 720 }
Chris@275 721
Chris@275 722 SliceLayer::paint(v, paint, rect);
Chris@277 723
Chris@278 724 //!!! All of this stuff relating to depicting frequencies
Chris@278 725 //(keyboard, crosshairs etc) should be applicable to any slice
Chris@278 726 //layer whose model has a vertical scale unit of Hz. However, the
Chris@278 727 //dense 3d model at the moment doesn't record its vertical scale
Chris@278 728 //unit -- we need to fix that and hoist this code as appropriate.
Chris@278 729 //Same really goes for any code in SpectrogramLayer that could be
Chris@278 730 //relevant to Colour3DPlotLayer with unit Hz, but that's a bigger
Chris@278 731 //proposition.
Chris@278 732
Chris@287 733 // if (m_binScale == LogBins) {
Chris@277 734
Chris@287 735 // int pkh = 10;
Chris@277 736 int h = v->height();
Chris@277 737
Chris@277 738 // piano keyboard
Chris@277 739 //!!! should be in a new paintHorizontalScale()?
Chris@278 740 // nice to have a piano keyboard class, of course
Chris@277 741
Chris@278 742 paint.drawLine(xorigin, h - pkh - 1, w + xorigin, h - pkh - 1);
Chris@277 743
Chris@277 744 int px = xorigin, ppx = xorigin;
Chris@278 745 paint.setBrush(paint.pen().color());
Chris@277 746
Chris@277 747 for (int i = 0; i < 128; ++i) {
Chris@277 748
Chris@277 749 float f = Pitch::getFrequencyForPitch(i);
Chris@277 750 int x = lrintf(getXForFrequency(f, w));
Chris@278 751
Chris@278 752 x += xorigin;
Chris@277 753
Chris@278 754 if (i == 0) {
Chris@278 755 px = ppx = x;
Chris@278 756 }
Chris@278 757 if (i == 1) {
Chris@278 758 ppx = px - (x - px);
Chris@278 759 }
Chris@278 760
Chris@278 761 if (x < xorigin) {
Chris@278 762 ppx = px;
Chris@278 763 px = x;
Chris@277 764 continue;
Chris@277 765 }
Chris@278 766
Chris@278 767 if (x > w) {
Chris@278 768 break;
Chris@278 769 }
Chris@277 770
Chris@277 771 int n = (i % 12);
Chris@277 772
Chris@277 773 if (n == 1) {
Chris@277 774 // C# -- fill the C from here
Chris@284 775 QColor col = Qt::gray;
Chris@284 776 if (i == 61) { // filling middle C
Chris@284 777 col = Qt::blue;
Chris@284 778 col = col.light(150);
Chris@284 779 }
Chris@277 780 if (x - ppx > 2) {
Chris@278 781 paint.fillRect((px + ppx) / 2 + 1,
Chris@277 782 h - pkh,
Chris@278 783 x - (px + ppx) / 2 - 1,
Chris@277 784 pkh,
Chris@284 785 col);
Chris@277 786 }
Chris@277 787 }
Chris@277 788
Chris@277 789 if (n == 1 || n == 3 || n == 6 || n == 8 || n == 10) {
Chris@277 790 // black notes
Chris@277 791 paint.drawLine(x, h - pkh, x, h);
Chris@278 792 int rw = lrintf(float(x - px) / 4) * 2;
Chris@277 793 if (rw < 2) rw = 2;
Chris@278 794 paint.drawRect(x - rw/2, h - pkh, rw, pkh/2);
Chris@277 795 } else if (n == 0 || n == 5) {
Chris@277 796 // C, F
Chris@277 797 if (px < w) {
Chris@277 798 paint.drawLine((x + px) / 2, h - pkh, (x + px) / 2, h);
Chris@277 799 }
Chris@277 800 }
Chris@277 801
Chris@277 802 ppx = px;
Chris@277 803 px = x;
Chris@277 804 }
Chris@287 805 // }
Chris@345 806
Chris@345 807 paint.restore();
Chris@275 808 }
Chris@275 809
Chris@275 810 void
Chris@254 811 SpectrumLayer::getBiasCurve(BiasCurve &curve) const
Chris@254 812 {
Chris@254 813 curve = m_biasCurve;
Chris@254 814 }
Chris@199 815
Chris@316 816 void
Chris@316 817 SpectrumLayer::toXml(QTextStream &stream,
Chris@316 818 QString indent, QString extraAttributes) const
Chris@220 819 {
Chris@316 820 QString s = QString("windowSize=\"%1\" "
Chris@316 821 "windowHopLevel=\"%2\"")
Chris@220 822 .arg(m_windowSize)
Chris@220 823 .arg(m_windowHopLevel);
Chris@220 824
Chris@316 825 SliceLayer::toXml(stream, indent, extraAttributes + " " + s);
Chris@220 826 }
Chris@220 827
Chris@220 828 void
Chris@220 829 SpectrumLayer::setProperties(const QXmlAttributes &attributes)
Chris@220 830 {
Chris@220 831 SliceLayer::setProperties(attributes);
Chris@220 832
Chris@220 833 bool ok = false;
Chris@220 834
Chris@220 835 size_t windowSize = attributes.value("windowSize").toUInt(&ok);
Chris@220 836 if (ok) setWindowSize(windowSize);
Chris@220 837
Chris@220 838 size_t windowHopLevel = attributes.value("windowHopLevel").toUInt(&ok);
Chris@220 839 if (ok) setWindowHopLevel(windowHopLevel);
Chris@220 840 }
Chris@220 841
Chris@220 842