annotate layer/SpectrumLayer.cpp @ 265:a3a54632cd4c

* fixes to spectrum harmonic cursor etc
author Chris Cannam
date Fri, 15 Jun 2007 15:48:11 +0000
parents 7e9e533f1863
children 4ed1446ad604
rev   line source
Chris@133 1
Chris@133 2 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@133 3
Chris@133 4 /*
Chris@133 5 Sonic Visualiser
Chris@133 6 An audio file viewer and annotation editor.
Chris@133 7 Centre for Digital Music, Queen Mary, University of London.
Chris@195 8 This file copyright 2006-2007 QMUL.
Chris@133 9
Chris@133 10 This program is free software; you can redistribute it and/or
Chris@133 11 modify it under the terms of the GNU General Public License as
Chris@133 12 published by the Free Software Foundation; either version 2 of the
Chris@133 13 License, or (at your option) any later version. See the file
Chris@133 14 COPYING included with this distribution for more information.
Chris@133 15 */
Chris@133 16
Chris@133 17 #include "SpectrumLayer.h"
Chris@133 18
Chris@133 19 #include "data/model/FFTModel.h"
Chris@133 20 #include "view/View.h"
Chris@153 21 #include "base/AudioLevel.h"
Chris@153 22 #include "base/Preferences.h"
Chris@167 23 #include "base/RangeMapper.h"
Chris@254 24 #include "ColourMapper.h"
Chris@254 25
Chris@254 26 #include <QPainter>
Chris@133 27
Chris@133 28 SpectrumLayer::SpectrumLayer() :
Chris@193 29 m_originModel(0),
Chris@153 30 m_channel(-1),
Chris@153 31 m_channelSet(false),
Chris@153 32 m_windowSize(1024),
Chris@153 33 m_windowType(HanningWindow),
Chris@153 34 m_windowHopLevel(2)
Chris@133 35 {
Chris@153 36 Preferences *prefs = Preferences::getInstance();
Chris@153 37 connect(prefs, SIGNAL(propertyChanged(PropertyContainer::PropertyName)),
Chris@153 38 this, SLOT(preferenceChanged(PropertyContainer::PropertyName)));
Chris@153 39 setWindowType(prefs->getWindowType());
Chris@195 40
Chris@195 41 setBinScale(LogBins);
Chris@133 42 }
Chris@133 43
Chris@133 44 SpectrumLayer::~SpectrumLayer()
Chris@133 45 {
Chris@193 46 //!!! delete parent's model
Chris@193 47 // for (size_t i = 0; i < m_fft.size(); ++i) delete m_fft[i];
Chris@133 48 }
Chris@133 49
Chris@133 50 void
Chris@133 51 SpectrumLayer::setModel(DenseTimeValueModel *model)
Chris@133 52 {
Chris@193 53 if (m_originModel == model) return;
Chris@193 54 m_originModel = model;
Chris@193 55 setupFFT();
Chris@153 56 }
Chris@153 57
Chris@153 58 void
Chris@193 59 SpectrumLayer::setupFFT()
Chris@153 60 {
Chris@193 61 FFTModel *oldFFT = dynamic_cast<FFTModel *>
Chris@193 62 (const_cast<DenseThreeDimensionalModel *>(m_sliceableModel));
Chris@153 63
Chris@193 64 if (oldFFT) {
Chris@193 65 setSliceableModel(0);
Chris@193 66 delete oldFFT;
Chris@153 67 }
Chris@153 68
Chris@193 69 FFTModel *newFFT = new FFTModel(m_originModel,
Chris@193 70 m_channel,
Chris@193 71 m_windowType,
Chris@193 72 m_windowSize,
Chris@193 73 getWindowIncrement(),
Chris@193 74 m_windowSize,
Chris@193 75 true);
Chris@153 76
Chris@193 77 setSliceableModel(newFFT);
Chris@193 78
Chris@254 79 m_biasCurve.clear();
Chris@254 80 for (size_t i = 0; i < m_windowSize; ++i) {
Chris@254 81 m_biasCurve.push_back(1.f / (float(m_windowSize)/2.f));
Chris@254 82 }
Chris@254 83
Chris@193 84 newFFT->resume();
Chris@153 85 }
Chris@153 86
Chris@153 87 void
Chris@153 88 SpectrumLayer::setChannel(int channel)
Chris@153 89 {
Chris@153 90 m_channelSet = true;
Chris@153 91
Chris@193 92 FFTModel *fft = dynamic_cast<FFTModel *>
Chris@193 93 (const_cast<DenseThreeDimensionalModel *>(m_sliceableModel));
Chris@193 94
Chris@153 95 if (m_channel == channel) {
Chris@193 96 if (fft) fft->resume();
Chris@153 97 return;
Chris@153 98 }
Chris@153 99
Chris@153 100 m_channel = channel;
Chris@153 101
Chris@193 102 if (!fft) setupFFT();
Chris@153 103
Chris@153 104 emit layerParametersChanged();
Chris@133 105 }
Chris@133 106
Chris@153 107 Layer::PropertyList
Chris@153 108 SpectrumLayer::getProperties() const
Chris@153 109 {
Chris@193 110 PropertyList list = SliceLayer::getProperties();
Chris@153 111 list.push_back("Window Size");
Chris@153 112 list.push_back("Window Increment");
Chris@153 113 return list;
Chris@153 114 }
Chris@153 115
Chris@153 116 QString
Chris@153 117 SpectrumLayer::getPropertyLabel(const PropertyName &name) const
Chris@153 118 {
Chris@153 119 if (name == "Window Size") return tr("Window Size");
Chris@153 120 if (name == "Window Increment") return tr("Window Overlap");
Chris@193 121 return SliceLayer::getPropertyLabel(name);
Chris@153 122 }
Chris@153 123
Chris@153 124 Layer::PropertyType
Chris@153 125 SpectrumLayer::getPropertyType(const PropertyName &name) const
Chris@153 126 {
Chris@193 127 if (name == "Window Size") return ValueProperty;
Chris@193 128 if (name == "Window Increment") return ValueProperty;
Chris@193 129 return SliceLayer::getPropertyType(name);
Chris@153 130 }
Chris@153 131
Chris@153 132 QString
Chris@153 133 SpectrumLayer::getPropertyGroupName(const PropertyName &name) const
Chris@153 134 {
Chris@153 135 if (name == "Window Size" ||
Chris@153 136 name == "Window Increment") return tr("Window");
Chris@193 137 return SliceLayer::getPropertyGroupName(name);
Chris@153 138 }
Chris@153 139
Chris@153 140 int
Chris@153 141 SpectrumLayer::getPropertyRangeAndValue(const PropertyName &name,
Chris@216 142 int *min, int *max, int *deflt) const
Chris@153 143 {
Chris@216 144 int val = 0;
Chris@153 145
Chris@216 146 int garbage0, garbage1, garbage2;
Chris@153 147 if (!min) min = &garbage0;
Chris@153 148 if (!max) max = &garbage1;
Chris@216 149 if (!deflt) deflt = &garbage2;
Chris@153 150
Chris@193 151 if (name == "Window Size") {
Chris@153 152
Chris@153 153 *min = 0;
Chris@254 154 *max = 15;
Chris@216 155 *deflt = 5;
Chris@153 156
Chris@216 157 val = 0;
Chris@153 158 int ws = m_windowSize;
Chris@216 159 while (ws > 32) { ws >>= 1; val ++; }
Chris@153 160
Chris@153 161 } else if (name == "Window Increment") {
Chris@153 162
Chris@153 163 *min = 0;
Chris@153 164 *max = 5;
Chris@216 165 *deflt = 2;
Chris@153 166
Chris@216 167 val = m_windowHopLevel;
Chris@153 168
Chris@153 169 } else {
Chris@193 170
Chris@216 171 val = SliceLayer::getPropertyRangeAndValue(name, min, max, deflt);
Chris@153 172 }
Chris@153 173
Chris@216 174 return val;
Chris@153 175 }
Chris@153 176
Chris@153 177 QString
Chris@153 178 SpectrumLayer::getPropertyValueLabel(const PropertyName &name,
Chris@153 179 int value) const
Chris@153 180 {
Chris@153 181 if (name == "Window Size") {
Chris@153 182 return QString("%1").arg(32 << value);
Chris@153 183 }
Chris@153 184 if (name == "Window Increment") {
Chris@153 185 switch (value) {
Chris@153 186 default:
Chris@153 187 case 0: return tr("None");
Chris@153 188 case 1: return tr("25 %");
Chris@153 189 case 2: return tr("50 %");
Chris@153 190 case 3: return tr("75 %");
Chris@153 191 case 4: return tr("87.5 %");
Chris@153 192 case 5: return tr("93.75 %");
Chris@153 193 }
Chris@153 194 }
Chris@193 195 return SliceLayer::getPropertyValueLabel(name, value);
Chris@153 196 }
Chris@153 197
Chris@167 198 RangeMapper *
Chris@167 199 SpectrumLayer::getNewPropertyRangeMapper(const PropertyName &name) const
Chris@167 200 {
Chris@193 201 return SliceLayer::getNewPropertyRangeMapper(name);
Chris@167 202 }
Chris@167 203
Chris@133 204 void
Chris@153 205 SpectrumLayer::setProperty(const PropertyName &name, int value)
Chris@133 206 {
Chris@193 207 if (name == "Window Size") {
Chris@153 208 setWindowSize(32 << value);
Chris@153 209 } else if (name == "Window Increment") {
Chris@153 210 setWindowHopLevel(value);
Chris@193 211 } else {
Chris@193 212 SliceLayer::setProperty(name, value);
Chris@153 213 }
Chris@153 214 }
Chris@153 215
Chris@153 216 void
Chris@153 217 SpectrumLayer::setWindowSize(size_t ws)
Chris@153 218 {
Chris@153 219 if (m_windowSize == ws) return;
Chris@153 220 m_windowSize = ws;
Chris@193 221 setupFFT();
Chris@153 222 emit layerParametersChanged();
Chris@153 223 }
Chris@153 224
Chris@153 225 void
Chris@153 226 SpectrumLayer::setWindowHopLevel(size_t v)
Chris@153 227 {
Chris@153 228 if (m_windowHopLevel == v) return;
Chris@153 229 m_windowHopLevel = v;
Chris@193 230 setupFFT();
Chris@153 231 emit layerParametersChanged();
Chris@153 232 }
Chris@153 233
Chris@153 234 void
Chris@153 235 SpectrumLayer::setWindowType(WindowType w)
Chris@153 236 {
Chris@153 237 if (m_windowType == w) return;
Chris@153 238 m_windowType = w;
Chris@193 239 setupFFT();
Chris@153 240 emit layerParametersChanged();
Chris@153 241 }
Chris@153 242
Chris@153 243 void
Chris@153 244 SpectrumLayer::preferenceChanged(PropertyContainer::PropertyName name)
Chris@153 245 {
Chris@153 246 if (name == "Window Type") {
Chris@153 247 setWindowType(Preferences::getInstance()->getWindowType());
Chris@153 248 return;
Chris@153 249 }
Chris@153 250 }
Chris@153 251
Chris@133 252 bool
Chris@248 253 SpectrumLayer::getValueExtents(float &, float &, bool &, QString &) const
Chris@133 254 {
Chris@133 255 return false;
Chris@133 256 }
Chris@133 257
Chris@254 258 float
Chris@265 259 SpectrumLayer::getXForBin(int bin, int totalBins, float w) const
Chris@265 260 {
Chris@265 261 if (!m_sliceableModel) return SliceLayer::getXForBin(bin, totalBins, w);
Chris@265 262
Chris@265 263 float sampleRate = m_sliceableModel->getSampleRate();
Chris@265 264 float binfreq = (sampleRate * bin) / (totalBins * 2);
Chris@265 265
Chris@265 266 return getXForFrequency(binfreq, w);
Chris@265 267 }
Chris@265 268
Chris@265 269 int
Chris@265 270 SpectrumLayer::getBinForX(float x, int totalBins, float w) const
Chris@265 271 {
Chris@265 272 if (!m_sliceableModel) return SliceLayer::getBinForX(x, totalBins, w);
Chris@265 273
Chris@265 274 float sampleRate = m_sliceableModel->getSampleRate();
Chris@265 275 float binfreq = getFrequencyForX(x, w);
Chris@265 276
Chris@265 277 return int((binfreq * totalBins * 2) / sampleRate);
Chris@265 278 }
Chris@265 279
Chris@265 280 float
Chris@254 281 SpectrumLayer::getFrequencyForX(float x, float w) const
Chris@254 282 {
Chris@254 283 float freq = 0;
Chris@254 284
Chris@254 285 int sampleRate = m_sliceableModel->getSampleRate();
Chris@254 286
Chris@254 287 float maxfreq = float(sampleRate) / 2;
Chris@254 288
Chris@254 289 switch (m_binScale) {
Chris@254 290
Chris@254 291 case LinearBins:
Chris@254 292 freq = ((x * maxfreq) / w);
Chris@254 293 break;
Chris@254 294
Chris@254 295 case LogBins:
Chris@254 296 freq = powf(10.f, (x * log10f(maxfreq)) / w);
Chris@254 297 break;
Chris@254 298
Chris@254 299 case InvertedLogBins:
Chris@254 300 freq = maxfreq - powf(10.f, ((w - x) * log10f(maxfreq)) / w);
Chris@254 301 break;
Chris@254 302 }
Chris@254 303
Chris@254 304 return freq;
Chris@254 305 }
Chris@254 306
Chris@254 307 float
Chris@254 308 SpectrumLayer::getXForFrequency(float freq, float w) const
Chris@254 309 {
Chris@254 310 float x = 0;
Chris@254 311
Chris@254 312 int sampleRate = m_sliceableModel->getSampleRate();
Chris@254 313
Chris@254 314 float maxfreq = float(sampleRate) / 2;
Chris@254 315
Chris@254 316 switch (m_binScale) {
Chris@254 317
Chris@254 318 case LinearBins:
Chris@254 319 x = (freq * w) / maxfreq;
Chris@254 320 break;
Chris@254 321
Chris@254 322 case LogBins:
Chris@254 323 x = (log10f(freq) * w) / log10f(maxfreq);
Chris@254 324 break;
Chris@254 325
Chris@254 326 case InvertedLogBins:
Chris@254 327 x = (w - log10f(maxfreq - freq) * w) / log10f(maxfreq);
Chris@254 328 break;
Chris@254 329 }
Chris@254 330
Chris@254 331 return x;
Chris@254 332 }
Chris@254 333
Chris@260 334 bool
Chris@260 335 SpectrumLayer::getXScaleValue(View *v, int x,
Chris@260 336 float &value, QString &unit) const
Chris@260 337 {
Chris@264 338 value = getFrequencyForX(x - m_xorigins[v], v->width() - m_xorigins[v] - 1);
Chris@260 339 unit = "Hz";
Chris@260 340 return true;
Chris@260 341 }
Chris@260 342
Chris@264 343 bool
Chris@264 344 SpectrumLayer::getCrosshairExtents(View *v, QPainter &paint,
Chris@264 345 QPoint cursorPos,
Chris@264 346 std::vector<QRect> &extents) const
Chris@264 347 {
Chris@264 348 QRect vertical(cursorPos.x(), cursorPos.y(), 1, v->height() - cursorPos.y());
Chris@264 349 extents.push_back(vertical);
Chris@264 350
Chris@264 351 QRect horizontal(0, cursorPos.y(), v->width(), 12);
Chris@264 352 extents.push_back(horizontal);
Chris@264 353
Chris@264 354 QRect label(cursorPos.x(), v->height() - paint.fontMetrics().height(),
Chris@264 355 paint.fontMetrics().width("123456 Hz") + 2,
Chris@264 356 paint.fontMetrics().height());
Chris@264 357 extents.push_back(label);
Chris@264 358
Chris@264 359 return true;
Chris@264 360 }
Chris@264 361
Chris@254 362 void
Chris@254 363 SpectrumLayer::paintCrosshairs(View *v, QPainter &paint,
Chris@254 364 QPoint cursorPos) const
Chris@254 365 {
Chris@254 366 paint.save();
Chris@254 367
Chris@254 368 ColourMapper mapper(m_colourMap, 0, 1);
Chris@254 369 paint.setPen(mapper.getContrastingColour());
Chris@254 370
Chris@254 371 int xorigin = m_xorigins[v];
Chris@254 372 int w = v->width() - xorigin - 1;
Chris@254 373
Chris@254 374 paint.drawLine(xorigin, cursorPos.y(), v->width(), cursorPos.y());
Chris@254 375 paint.drawLine(cursorPos.x(), cursorPos.y(), cursorPos.x(), v->height());
Chris@254 376
Chris@254 377 float fundamental = getFrequencyForX(cursorPos.x() - xorigin, w);
Chris@254 378
Chris@264 379 paint.drawText(cursorPos.x() + 2, v->height() - 2,
Chris@264 380 QString("%1 Hz").arg(fundamental));
Chris@264 381
Chris@254 382 int harmonic = 2;
Chris@254 383
Chris@254 384 while (harmonic < 100) {
Chris@254 385
Chris@254 386 float hx = lrintf(getXForFrequency(fundamental * harmonic, w));
Chris@254 387 hx += xorigin;
Chris@254 388
Chris@254 389 if (hx < xorigin || hx > v->width()) break;
Chris@254 390
Chris@254 391 int len = 7;
Chris@254 392
Chris@254 393 if (harmonic % 2 == 0) {
Chris@254 394 if (harmonic % 4 == 0) {
Chris@254 395 len = 12;
Chris@254 396 } else {
Chris@254 397 len = 10;
Chris@254 398 }
Chris@254 399 }
Chris@254 400
Chris@254 401 paint.drawLine(int(hx),
Chris@254 402 cursorPos.y(),
Chris@254 403 int(hx),
Chris@254 404 cursorPos.y() + len);
Chris@254 405
Chris@254 406 ++harmonic;
Chris@254 407 }
Chris@254 408
Chris@254 409 paint.restore();
Chris@254 410 }
Chris@254 411
Chris@199 412 QString
Chris@199 413 SpectrumLayer::getFeatureDescription(View *v, QPoint &p) const
Chris@199 414 {
Chris@199 415 if (!m_sliceableModel) return "";
Chris@199 416
Chris@199 417 int minbin = 0, maxbin = 0, range = 0;
Chris@199 418 QString genericDesc = SliceLayer::getFeatureDescription
Chris@199 419 (v, p, false, minbin, maxbin, range);
Chris@199 420
Chris@199 421 if (genericDesc == "") return "";
Chris@199 422
Chris@199 423 float minvalue = 0.f;
Chris@248 424 if (minbin < int(m_values.size())) minvalue = m_values[minbin];
Chris@199 425
Chris@199 426 float maxvalue = minvalue;
Chris@248 427 if (maxbin < int(m_values.size())) maxvalue = m_values[maxbin];
Chris@199 428
Chris@199 429 if (minvalue > maxvalue) std::swap(minvalue, maxvalue);
Chris@199 430
Chris@199 431 QString binstr;
Chris@199 432 QString hzstr;
Chris@199 433 int minfreq = lrintf((minbin * m_sliceableModel->getSampleRate()) /
Chris@199 434 m_windowSize);
Chris@199 435 int maxfreq = lrintf((std::max(maxbin, minbin+1)
Chris@199 436 * m_sliceableModel->getSampleRate()) /
Chris@199 437 m_windowSize);
Chris@199 438
Chris@199 439 if (maxbin != minbin) {
Chris@199 440 binstr = tr("%1 - %2").arg(minbin+1).arg(maxbin+1);
Chris@199 441 } else {
Chris@199 442 binstr = QString("%1").arg(minbin+1);
Chris@199 443 }
Chris@199 444 if (minfreq != maxfreq) {
Chris@199 445 hzstr = tr("%1 - %2 Hz").arg(minfreq).arg(maxfreq);
Chris@199 446 } else {
Chris@199 447 hzstr = tr("%1 Hz").arg(minfreq);
Chris@199 448 }
Chris@199 449
Chris@199 450 QString valuestr;
Chris@199 451 if (maxvalue != minvalue) {
Chris@199 452 valuestr = tr("%1 - %2").arg(minvalue).arg(maxvalue);
Chris@199 453 } else {
Chris@199 454 valuestr = QString("%1").arg(minvalue);
Chris@199 455 }
Chris@199 456
Chris@199 457 QString dbstr;
Chris@199 458 float mindb = AudioLevel::multiplier_to_dB(minvalue);
Chris@199 459 float maxdb = AudioLevel::multiplier_to_dB(maxvalue);
Chris@199 460 QString mindbstr;
Chris@199 461 QString maxdbstr;
Chris@199 462 if (mindb == AudioLevel::DB_FLOOR) {
Chris@199 463 mindbstr = tr("-Inf");
Chris@199 464 } else {
Chris@199 465 mindbstr = QString("%1").arg(lrintf(mindb));
Chris@199 466 }
Chris@199 467 if (maxdb == AudioLevel::DB_FLOOR) {
Chris@199 468 maxdbstr = tr("-Inf");
Chris@199 469 } else {
Chris@199 470 maxdbstr = QString("%1").arg(lrintf(maxdb));
Chris@199 471 }
Chris@199 472 if (lrintf(mindb) != lrintf(maxdb)) {
Chris@199 473 dbstr = tr("%1 - %2").arg(mindbstr).arg(maxdbstr);
Chris@199 474 } else {
Chris@199 475 dbstr = tr("%1").arg(mindbstr);
Chris@199 476 }
Chris@199 477
Chris@199 478 QString description;
Chris@199 479
Chris@248 480 if (range > int(m_sliceableModel->getResolution())) {
Chris@199 481 description = tr("%1\nBin:\t%2 (%3)\n%4 value:\t%5\ndB:\t%6")
Chris@199 482 .arg(genericDesc)
Chris@199 483 .arg(binstr)
Chris@199 484 .arg(hzstr)
Chris@199 485 .arg(m_samplingMode == NearestSample ? tr("First") :
Chris@199 486 m_samplingMode == SampleMean ? tr("Mean") : tr("Peak"))
Chris@199 487 .arg(valuestr)
Chris@199 488 .arg(dbstr);
Chris@199 489 } else {
Chris@199 490 description = tr("%1\nBin:\t%2 (%3)\nValue:\t%4\ndB:\t%5")
Chris@199 491 .arg(genericDesc)
Chris@199 492 .arg(binstr)
Chris@199 493 .arg(hzstr)
Chris@199 494 .arg(valuestr)
Chris@199 495 .arg(dbstr);
Chris@199 496 }
Chris@199 497
Chris@199 498 return description;
Chris@199 499 }
Chris@199 500
Chris@254 501 void
Chris@254 502 SpectrumLayer::getBiasCurve(BiasCurve &curve) const
Chris@254 503 {
Chris@254 504 curve = m_biasCurve;
Chris@254 505 }
Chris@199 506
Chris@220 507 QString
Chris@220 508 SpectrumLayer::toXmlString(QString indent, QString extraAttributes) const
Chris@220 509 {
Chris@220 510 QString s;
Chris@199 511
Chris@220 512 s += QString("windowSize=\"%1\" "
Chris@220 513 "windowHopLevel=\"%2\"")
Chris@220 514 .arg(m_windowSize)
Chris@220 515 .arg(m_windowHopLevel);
Chris@220 516
Chris@220 517 return SliceLayer::toXmlString(indent, extraAttributes + " " + s);
Chris@220 518 }
Chris@220 519
Chris@220 520 void
Chris@220 521 SpectrumLayer::setProperties(const QXmlAttributes &attributes)
Chris@220 522 {
Chris@220 523 SliceLayer::setProperties(attributes);
Chris@220 524
Chris@220 525 bool ok = false;
Chris@220 526
Chris@220 527 size_t windowSize = attributes.value("windowSize").toUInt(&ok);
Chris@220 528 if (ok) setWindowSize(windowSize);
Chris@220 529
Chris@220 530 size_t windowHopLevel = attributes.value("windowHopLevel").toUInt(&ok);
Chris@220 531 if (ok) setWindowHopLevel(windowHopLevel);
Chris@220 532 }
Chris@220 533
Chris@220 534