annotate layer/SliceLayer.cpp @ 316:c0b9eec70639

* Make XmlExportable::toXml the function that is universally overridden (and pure virtual) instead of toXmlString. Tidies up some classes, notably the model classes, significantly. Closes #1794561.
author Chris Cannam
date Thu, 18 Oct 2007 10:15:07 +0000
parents 3101c68a00c1
children 984c1975f1ff
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@193 17 #include "SliceLayer.h"
Chris@133 18
Chris@133 19 #include "view/View.h"
Chris@153 20 #include "base/AudioLevel.h"
Chris@167 21 #include "base/RangeMapper.h"
Chris@195 22 #include "base/RealTime.h"
Chris@285 23 #include "base/ColourMapper.h"
Chris@287 24 #include "base/ColourDatabase.h"
Chris@195 25
Chris@195 26 #include "PaintAssistant.h"
Chris@133 27
Chris@133 28 #include <QPainter>
Chris@133 29 #include <QPainterPath>
Chris@316 30 #include <QTextStream>
Chris@316 31
Chris@133 32
Chris@193 33 SliceLayer::SliceLayer() :
Chris@193 34 m_sliceableModel(0),
Chris@197 35 m_colourMap(0),
Chris@153 36 m_energyScale(dBScale),
Chris@198 37 m_samplingMode(SampleMean),
Chris@195 38 m_plotStyle(PlotSteps),
Chris@193 39 m_binScale(LinearBins),
Chris@153 40 m_normalize(false),
Chris@284 41 m_threshold(0.0),
Chris@284 42 m_initialThreshold(0.0),
Chris@198 43 m_gain(1.0),
Chris@198 44 m_currentf0(0),
Chris@198 45 m_currentf1(0)
Chris@133 46 {
Chris@133 47 }
Chris@133 48
Chris@193 49 SliceLayer::~SliceLayer()
Chris@133 50 {
Chris@193 51
Chris@133 52 }
Chris@133 53
Chris@133 54 void
Chris@193 55 SliceLayer::setSliceableModel(const Model *model)
Chris@133 56 {
Chris@193 57 const DenseThreeDimensionalModel *sliceable =
Chris@193 58 dynamic_cast<const DenseThreeDimensionalModel *>(model);
Chris@193 59
Chris@193 60 if (model && !sliceable) {
Chris@193 61 std::cerr << "WARNING: SliceLayer::setSliceableModel(" << model
Chris@193 62 << "): model is not a DenseThreeDimensionalModel" << std::endl;
Chris@193 63 }
Chris@193 64
Chris@193 65 if (m_sliceableModel == sliceable) return;
Chris@193 66
Chris@193 67 m_sliceableModel = sliceable;
Chris@193 68
Chris@193 69 connect(m_sliceableModel, SIGNAL(modelChanged()),
Chris@193 70 this, SIGNAL(modelChanged()));
Chris@193 71
Chris@193 72 connect(m_sliceableModel, SIGNAL(modelChanged(size_t, size_t)),
Chris@193 73 this, SIGNAL(modelChanged(size_t, size_t)));
Chris@193 74
Chris@193 75 connect(m_sliceableModel, SIGNAL(completionChanged()),
Chris@193 76 this, SIGNAL(modelCompletionChanged()));
Chris@193 77
Chris@193 78 emit modelReplaced();
Chris@153 79 }
Chris@153 80
Chris@153 81 void
Chris@193 82 SliceLayer::sliceableModelReplaced(const Model *orig, const Model *replacement)
Chris@153 83 {
Chris@193 84 std::cerr << "SliceLayer::sliceableModelReplaced(" << orig << ", " << replacement << ")" << std::endl;
Chris@153 85
Chris@193 86 if (orig == m_sliceableModel) {
Chris@193 87 setSliceableModel
Chris@193 88 (dynamic_cast<const DenseThreeDimensionalModel *>(replacement));
Chris@153 89 }
Chris@153 90 }
Chris@153 91
Chris@153 92 void
Chris@193 93 SliceLayer::modelAboutToBeDeleted(Model *m)
Chris@153 94 {
Chris@193 95 std::cerr << "SliceLayer::modelAboutToBeDeleted(" << m << ")" << std::endl;
Chris@153 96
Chris@193 97 if (m == m_sliceableModel) {
Chris@193 98 setSliceableModel(0);
Chris@153 99 }
Chris@133 100 }
Chris@133 101
Chris@198 102 QString
Chris@198 103 SliceLayer::getFeatureDescription(View *v, QPoint &p) const
Chris@198 104 {
Chris@199 105 int minbin, maxbin, range;
Chris@199 106 return getFeatureDescription(v, p, true, minbin, maxbin, range);
Chris@199 107 }
Chris@199 108
Chris@199 109 QString
Chris@199 110 SliceLayer::getFeatureDescription(View *v, QPoint &p,
Chris@199 111 bool includeBinDescription,
Chris@199 112 int &minbin, int &maxbin, int &range) const
Chris@199 113 {
Chris@199 114 minbin = 0;
Chris@199 115 maxbin = 0;
Chris@198 116 if (!m_sliceableModel) return "";
Chris@198 117
Chris@198 118 int xorigin = m_xorigins[v];
Chris@198 119 int w = v->width() - xorigin - 1;
Chris@198 120
Chris@198 121 int mh = m_sliceableModel->getHeight();
Chris@199 122 minbin = getBinForX(p.x() - xorigin, mh, w);
Chris@199 123 maxbin = getBinForX(p.x() - xorigin + 1, mh, w);
Chris@199 124
Chris@199 125 if (minbin >= mh) minbin = mh - 1;
Chris@199 126 if (maxbin >= mh) maxbin = mh - 1;
Chris@199 127 if (minbin < 0) minbin = 0;
Chris@199 128 if (maxbin < 0) maxbin = 0;
Chris@198 129
Chris@198 130 int sampleRate = m_sliceableModel->getSampleRate();
Chris@198 131
Chris@198 132 size_t f0 = m_currentf0;
Chris@198 133 size_t f1 = m_currentf1;
Chris@198 134
Chris@198 135 RealTime rt0 = RealTime::frame2RealTime(f0, sampleRate);
Chris@198 136 RealTime rt1 = RealTime::frame2RealTime(f1, sampleRate);
Chris@198 137
Chris@199 138 range = f1 - f0 + 1;
Chris@198 139
Chris@280 140 QString rtrangestr = QString("%1 s").arg((rt1 - rt0).toText().c_str());
Chris@280 141
Chris@199 142 if (includeBinDescription) {
Chris@198 143
Chris@199 144 float minvalue = 0.f;
Chris@248 145 if (minbin < int(m_values.size())) minvalue = m_values[minbin];
Chris@198 146
Chris@199 147 float maxvalue = minvalue;
Chris@248 148 if (maxbin < int(m_values.size())) maxvalue = m_values[maxbin];
Chris@199 149
Chris@199 150 if (minvalue > maxvalue) std::swap(minvalue, maxvalue);
Chris@199 151
Chris@199 152 QString binstr;
Chris@199 153 if (maxbin != minbin) {
Chris@199 154 binstr = tr("%1 - %2").arg(minbin+1).arg(maxbin+1);
Chris@199 155 } else {
Chris@199 156 binstr = QString("%1").arg(minbin+1);
Chris@199 157 }
Chris@199 158
Chris@199 159 QString valuestr;
Chris@199 160 if (maxvalue != minvalue) {
Chris@199 161 valuestr = tr("%1 - %2").arg(minvalue).arg(maxvalue);
Chris@199 162 } else {
Chris@199 163 valuestr = QString("%1").arg(minvalue);
Chris@199 164 }
Chris@199 165
Chris@280 166 QString description = tr("Time:\t%1 - %2\nRange:\t%3 samples (%4)\nBin:\t%5\n%6 value:\t%7")
Chris@199 167 .arg(QString::fromStdString(rt0.toText(true)))
Chris@199 168 .arg(QString::fromStdString(rt1.toText(true)))
Chris@199 169 .arg(range)
Chris@280 170 .arg(rtrangestr)
Chris@199 171 .arg(binstr)
Chris@199 172 .arg(m_samplingMode == NearestSample ? tr("First") :
Chris@199 173 m_samplingMode == SampleMean ? tr("Mean") : tr("Peak"))
Chris@199 174 .arg(valuestr);
Chris@199 175
Chris@199 176 return description;
Chris@199 177
Chris@199 178 } else {
Chris@199 179
Chris@280 180 QString description = tr("Time:\t%1 - %2\nRange:\t%3 samples (%4)")
Chris@199 181 .arg(QString::fromStdString(rt0.toText(true)))
Chris@199 182 .arg(QString::fromStdString(rt1.toText(true)))
Chris@280 183 .arg(range)
Chris@280 184 .arg(rtrangestr);
Chris@199 185
Chris@199 186 return description;
Chris@199 187 }
Chris@198 188 }
Chris@198 189
Chris@198 190 float
Chris@198 191 SliceLayer::getXForBin(int bin, int count, float w) const
Chris@198 192 {
Chris@198 193 float x = 0;
Chris@198 194
Chris@198 195 switch (m_binScale) {
Chris@198 196
Chris@198 197 case LinearBins:
Chris@198 198 x = (float(w) * bin) / count;
Chris@198 199 break;
Chris@198 200
Chris@198 201 case LogBins:
Chris@198 202 x = (float(w) * log10f(bin + 1)) / log10f(count + 1);
Chris@198 203 break;
Chris@198 204
Chris@198 205 case InvertedLogBins:
Chris@198 206 x = w - (float(w) * log10f(count - bin - 1)) / log10f(count);
Chris@198 207 break;
Chris@198 208 }
Chris@198 209
Chris@198 210 return x;
Chris@198 211 }
Chris@198 212
Chris@198 213 int
Chris@198 214 SliceLayer::getBinForX(float x, int count, float w) const
Chris@198 215 {
Chris@198 216 int bin = 0;
Chris@198 217
Chris@198 218 switch (m_binScale) {
Chris@198 219
Chris@198 220 case LinearBins:
Chris@198 221 bin = int((x * count) / w + 0.0001);
Chris@198 222 break;
Chris@198 223
Chris@198 224 case LogBins:
Chris@198 225 bin = int(powf(10.f, (x * log10f(count + 1)) / w) - 1 + 0.0001);
Chris@198 226 break;
Chris@198 227
Chris@198 228 case InvertedLogBins:
Chris@198 229 bin = count + 1 - int(powf(10.f, (log10f(count) * (w - x)) / float(w)) + 0.0001);
Chris@198 230 break;
Chris@198 231 }
Chris@198 232
Chris@198 233 return bin;
Chris@198 234 }
Chris@198 235
Chris@274 236 float
Chris@274 237 SliceLayer::getYForValue(float value, const View *v, float &norm) const
Chris@274 238 {
Chris@274 239 norm = 0.f;
Chris@274 240
Chris@274 241 if (m_yorigins.find(v) == m_yorigins.end()) return 0;
Chris@274 242
Chris@274 243 value *= m_gain;
Chris@274 244
Chris@274 245 int yorigin = m_yorigins[v];
Chris@274 246 int h = m_heights[v];
Chris@284 247 float thresh = getThresholdDb();
Chris@274 248
Chris@274 249 float y = 0.f;
Chris@274 250
Chris@274 251 if (h <= 0) return y;
Chris@274 252
Chris@274 253 switch (m_energyScale) {
Chris@274 254
Chris@274 255 case dBScale:
Chris@274 256 {
Chris@274 257 float db = thresh;
Chris@274 258 if (value > 0.f) db = 10.f * log10f(value);
Chris@274 259 if (db < thresh) db = thresh;
Chris@274 260 norm = (db - thresh) / -thresh;
Chris@274 261 y = yorigin - (float(h) * norm);
Chris@274 262 break;
Chris@274 263 }
Chris@274 264
Chris@274 265 case MeterScale:
Chris@274 266 y = AudioLevel::multiplier_to_preview(value, h);
Chris@274 267 norm = float(y) / float(h);
Chris@274 268 y = yorigin - y;
Chris@274 269 break;
Chris@274 270
Chris@274 271 default:
Chris@290 272 // std::cerr << "thresh = " << m_threshold << std::endl;
Chris@284 273 norm = (fabsf(value) - m_threshold);
Chris@284 274 if (norm < 0) norm = 0;
Chris@284 275 y = yorigin - (float(h) * norm);
Chris@274 276 break;
Chris@274 277 }
Chris@274 278
Chris@274 279 return y;
Chris@274 280 }
Chris@274 281
Chris@274 282 float
Chris@274 283 SliceLayer::getValueForY(float y, const View *v) const
Chris@274 284 {
Chris@274 285 float value = 0.f;
Chris@274 286
Chris@274 287 if (m_yorigins.find(v) == m_yorigins.end()) return value;
Chris@274 288
Chris@274 289 int yorigin = m_yorigins[v];
Chris@274 290 int h = m_heights[v];
Chris@284 291 float thresh = getThresholdDb();
Chris@274 292
Chris@274 293 if (h <= 0) return value;
Chris@274 294
Chris@274 295 y = yorigin - y;
Chris@274 296
Chris@274 297 switch (m_energyScale) {
Chris@274 298
Chris@274 299 case dBScale:
Chris@274 300 {
Chris@274 301 float db = ((y / h) * -thresh) + thresh;
Chris@274 302 value = powf(10.f, db/10.f);
Chris@274 303 break;
Chris@274 304 }
Chris@274 305
Chris@274 306 case MeterScale:
Chris@274 307 value = AudioLevel::preview_to_multiplier(lrintf(y), h);
Chris@274 308 break;
Chris@274 309
Chris@274 310 default:
Chris@284 311 value = y / h + m_threshold;
Chris@274 312 }
Chris@274 313
Chris@274 314 return value / m_gain;
Chris@274 315 }
Chris@274 316
Chris@133 317 void
Chris@193 318 SliceLayer::paint(View *v, QPainter &paint, QRect rect) const
Chris@133 319 {
Chris@254 320 if (!m_sliceableModel || !m_sliceableModel->isOK() ||
Chris@254 321 !m_sliceableModel->isReady()) return;
Chris@133 322
Chris@195 323 paint.save();
Chris@195 324 paint.setRenderHint(QPainter::Antialiasing, false);
Chris@195 325
Chris@195 326 if (v->getViewManager() && v->getViewManager()->shouldShowScaleGuides()) {
Chris@195 327 if (!m_scalePoints.empty()) {
Chris@195 328 paint.setPen(QColor(240, 240, 240)); //!!! and dark background?
Chris@195 329 for (size_t i = 0; i < m_scalePoints.size(); ++i) {
Chris@195 330 paint.drawLine(0, m_scalePoints[i], rect.width(), m_scalePoints[i]);
Chris@195 331 }
Chris@195 332 }
Chris@195 333 }
Chris@195 334
Chris@287 335 paint.setPen(getBaseQColor());
Chris@195 336
Chris@274 337 int xorigin = getVerticalScaleWidth(v, paint) + 1;
Chris@195 338 int w = v->width() - xorigin - 1;
Chris@198 339
Chris@198 340 m_xorigins[v] = xorigin; // for use in getFeatureDescription
Chris@133 341
Chris@195 342 int yorigin = v->height() - 20 - paint.fontMetrics().height() - 7;
Chris@195 343 int h = yorigin - paint.fontMetrics().height() - 8;
Chris@133 344
Chris@274 345 m_yorigins[v] = yorigin; // for getYForValue etc
Chris@274 346 m_heights[v] = h;
Chris@274 347
Chris@274 348 if (h <= 0) return;
Chris@274 349
Chris@133 350 QPainterPath path;
Chris@133 351
Chris@248 352 size_t mh = m_sliceableModel->getHeight();
Chris@133 353
Chris@193 354 int divisor = 0;
Chris@193 355
Chris@198 356 m_values.clear();
Chris@193 357 for (size_t bin = 0; bin < mh; ++bin) {
Chris@198 358 m_values.push_back(0.f);
Chris@193 359 }
Chris@193 360
Chris@193 361 size_t f0 = v->getCentreFrame();
Chris@193 362 int f0x = v->getXForFrame(f0);
Chris@195 363 f0 = v->getFrameForX(f0x);
Chris@193 364 size_t f1 = v->getFrameForX(f0x + 1);
Chris@195 365 if (f1 > f0) --f1;
Chris@193 366
Chris@284 367 // std::cerr << "centre frame " << v->getCentreFrame() << ", x " << f0x << ", f0 " << f0 << ", f1 " << f1 << std::endl;
Chris@274 368
Chris@274 369 size_t res = m_sliceableModel->getResolution();
Chris@274 370 size_t col0 = f0 / res;
Chris@193 371 size_t col1 = col0;
Chris@274 372 if (m_samplingMode != NearestSample) col1 = f1 / res;
Chris@274 373 f0 = col0 * res;
Chris@274 374 f1 = (col1 + 1) * res - 1;
Chris@274 375
Chris@284 376 // std::cerr << "resolution " << res << ", col0 " << col0 << ", col1 " << col1 << ", f0 " << f0 << ", f1 " << f1 << std::endl;
Chris@193 377
Chris@198 378 m_currentf0 = f0;
Chris@198 379 m_currentf1 = f1;
Chris@198 380
Chris@254 381 BiasCurve curve;
Chris@254 382 getBiasCurve(curve);
Chris@254 383 size_t cs = curve.size();
Chris@254 384
Chris@195 385 for (size_t col = col0; col <= col1; ++col) {
Chris@193 386 for (size_t bin = 0; bin < mh; ++bin) {
Chris@193 387 float value = m_sliceableModel->getValueAt(col, bin);
Chris@254 388 if (bin < cs) value *= curve[bin];
Chris@193 389 if (m_samplingMode == SamplePeak) {
Chris@198 390 if (value > m_values[bin]) m_values[bin] = value;
Chris@193 391 } else {
Chris@198 392 m_values[bin] += value;
Chris@193 393 }
Chris@153 394 }
Chris@193 395 ++divisor;
Chris@193 396 }
Chris@193 397
Chris@193 398 float max = 0.f;
Chris@193 399 for (size_t bin = 0; bin < mh; ++bin) {
Chris@198 400 if (m_samplingMode == SampleMean) m_values[bin] /= divisor;
Chris@198 401 if (m_values[bin] > max) max = m_values[bin];
Chris@193 402 }
Chris@193 403 if (max != 0.f && m_normalize) {
Chris@193 404 for (size_t bin = 0; bin < mh; ++bin) {
Chris@198 405 m_values[bin] /= max;
Chris@193 406 }
Chris@193 407 }
Chris@193 408
Chris@193 409 float py = 0;
Chris@193 410 float nx = xorigin;
Chris@193 411
Chris@197 412 ColourMapper mapper(m_colourMap, 0, 1);
Chris@197 413
Chris@193 414 for (size_t bin = 0; bin < mh; ++bin) {
Chris@193 415
Chris@198 416 float x = nx;
Chris@198 417 nx = xorigin + getXForBin(bin + 1, mh, w);
Chris@193 418
Chris@198 419 float value = m_values[bin];
Chris@197 420 float norm = 0.f;
Chris@274 421 float y = getYForValue(value, v, norm);
Chris@133 422
Chris@193 423 if (m_plotStyle == PlotLines) {
Chris@193 424
Chris@193 425 if (bin == 0) {
Chris@193 426 path.moveTo(x, y);
Chris@193 427 } else {
Chris@193 428 path.lineTo(x, y);
Chris@193 429 }
Chris@193 430
Chris@193 431 } else if (m_plotStyle == PlotSteps) {
Chris@193 432
Chris@193 433 if (bin == 0) {
Chris@193 434 path.moveTo(x, y);
Chris@193 435 } else {
Chris@193 436 path.lineTo(x, y);
Chris@193 437 }
Chris@193 438 path.lineTo(nx, y);
Chris@193 439
Chris@193 440 } else if (m_plotStyle == PlotBlocks) {
Chris@193 441
Chris@193 442 path.moveTo(x, yorigin);
Chris@133 443 path.lineTo(x, y);
Chris@193 444 path.lineTo(nx, y);
Chris@193 445 path.lineTo(nx, yorigin);
Chris@193 446 path.lineTo(x, yorigin);
Chris@197 447
Chris@197 448 } else if (m_plotStyle == PlotFilledBlocks) {
Chris@197 449
Chris@197 450 paint.fillRect(QRectF(x, y, nx - x, yorigin - y), mapper.map(norm));
Chris@133 451 }
Chris@193 452
Chris@193 453 py = y;
Chris@133 454 }
Chris@133 455
Chris@197 456 if (m_plotStyle != PlotFilledBlocks) {
Chris@197 457 paint.drawPath(path);
Chris@197 458 }
Chris@133 459 paint.restore();
Chris@198 460 /*
Chris@197 461 QPoint discard;
Chris@197 462
Chris@197 463 if (v->getViewManager() && v->getViewManager()->shouldShowFrameCount() &&
Chris@197 464 v->shouldIlluminateLocalFeatures(this, discard)) {
Chris@195 465
Chris@195 466 int sampleRate = m_sliceableModel->getSampleRate();
Chris@195 467
Chris@195 468 QString startText = QString("%1 / %2")
Chris@195 469 .arg(QString::fromStdString
Chris@195 470 (RealTime::frame2RealTime
Chris@195 471 (f0, sampleRate).toText(true)))
Chris@195 472 .arg(f0);
Chris@195 473
Chris@195 474 QString endText = QString(" %1 / %2")
Chris@195 475 .arg(QString::fromStdString
Chris@195 476 (RealTime::frame2RealTime
Chris@195 477 (f1, sampleRate).toText(true)))
Chris@195 478 .arg(f1);
Chris@195 479
Chris@195 480 QString durationText = QString("(%1 / %2) ")
Chris@195 481 .arg(QString::fromStdString
Chris@195 482 (RealTime::frame2RealTime
Chris@195 483 (f1 - f0 + 1, sampleRate).toText(true)))
Chris@195 484 .arg(f1 - f0 + 1);
Chris@195 485
Chris@195 486 v->drawVisibleText
Chris@195 487 (paint, xorigin + 5,
Chris@195 488 paint.fontMetrics().ascent() + 5,
Chris@195 489 startText, View::OutlinedText);
Chris@195 490
Chris@195 491 v->drawVisibleText
Chris@195 492 (paint, xorigin + 5,
Chris@195 493 paint.fontMetrics().ascent() + paint.fontMetrics().height() + 10,
Chris@195 494 endText, View::OutlinedText);
Chris@195 495
Chris@195 496 v->drawVisibleText
Chris@195 497 (paint, xorigin + 5,
Chris@195 498 paint.fontMetrics().ascent() + 2*paint.fontMetrics().height() + 15,
Chris@195 499 durationText, View::OutlinedText);
Chris@195 500 }
Chris@195 501 */
Chris@195 502 }
Chris@195 503
Chris@195 504 int
Chris@248 505 SliceLayer::getVerticalScaleWidth(View *, QPainter &paint) const
Chris@195 506 {
Chris@195 507 if (m_energyScale == LinearScale) {
Chris@220 508 return std::max(paint.fontMetrics().width("0.0") + 13,
Chris@220 509 paint.fontMetrics().width("x10-10"));
Chris@195 510 } else {
Chris@195 511 return std::max(paint.fontMetrics().width(tr("0dB")),
Chris@195 512 paint.fontMetrics().width(tr("-Inf"))) + 13;
Chris@195 513 }
Chris@195 514 }
Chris@195 515
Chris@195 516 void
Chris@195 517 SliceLayer::paintVerticalScale(View *v, QPainter &paint, QRect rect) const
Chris@195 518 {
Chris@284 519 float thresh = m_threshold;
Chris@195 520 if (m_energyScale != LinearScale) {
Chris@284 521 thresh = AudioLevel::dB_to_multiplier(getThresholdDb());
Chris@195 522 }
Chris@195 523
Chris@195 524 // int h = (rect.height() * 3) / 4;
Chris@195 525 // int y = (rect.height() / 2) - (h / 2);
Chris@195 526
Chris@195 527 int yorigin = v->height() - 20 - paint.fontMetrics().height() - 6;
Chris@195 528 int h = yorigin - paint.fontMetrics().height() - 8;
Chris@195 529 if (h < 0) return;
Chris@195 530
Chris@195 531 QRect actual(rect.x(), rect.y() + yorigin - h, rect.width(), h);
Chris@195 532
Chris@220 533 int mult = 1;
Chris@220 534
Chris@195 535 PaintAssistant::paintVerticalLevelScale
Chris@195 536 (paint, actual, thresh, 1.0 / m_gain,
Chris@195 537 PaintAssistant::Scale(m_energyScale),
Chris@220 538 mult,
Chris@195 539 const_cast<std::vector<int> *>(&m_scalePoints));
Chris@220 540
Chris@220 541 if (mult != 1 && mult != 0) {
Chris@220 542 int log = lrintf(log10f(mult));
Chris@220 543 QString a = tr("x10");
Chris@220 544 QString b = QString("%1").arg(-log);
Chris@220 545 paint.drawText(3, 8 + paint.fontMetrics().ascent(), a);
Chris@220 546 paint.drawText(3 + paint.fontMetrics().width(a),
Chris@220 547 3 + paint.fontMetrics().ascent(), b);
Chris@220 548 }
Chris@133 549 }
Chris@133 550
Chris@153 551 Layer::PropertyList
Chris@193 552 SliceLayer::getProperties() const
Chris@153 553 {
Chris@287 554 PropertyList list = SingleColourLayer::getProperties();
Chris@193 555 list.push_back("Plot Type");
Chris@198 556 // list.push_back("Sampling Mode");
Chris@153 557 list.push_back("Scale");
Chris@153 558 list.push_back("Normalize");
Chris@284 559 list.push_back("Threshold");
Chris@153 560 list.push_back("Gain");
Chris@193 561 list.push_back("Bin Scale");
Chris@153 562
Chris@153 563 return list;
Chris@153 564 }
Chris@153 565
Chris@153 566 QString
Chris@193 567 SliceLayer::getPropertyLabel(const PropertyName &name) const
Chris@153 568 {
Chris@193 569 if (name == "Plot Type") return tr("Plot Type");
Chris@290 570 if (name == "Scale") return tr("Scale");
Chris@153 571 if (name == "Normalize") return tr("Normalize");
Chris@284 572 if (name == "Threshold") return tr("Threshold");
Chris@153 573 if (name == "Gain") return tr("Gain");
Chris@193 574 if (name == "Sampling Mode") return tr("Sampling Mode");
Chris@193 575 if (name == "Bin Scale") return tr("Plot X Scale");
Chris@287 576 return SingleColourLayer::getPropertyLabel(name);
Chris@153 577 }
Chris@153 578
Chris@153 579 Layer::PropertyType
Chris@193 580 SliceLayer::getPropertyType(const PropertyName &name) const
Chris@153 581 {
Chris@153 582 if (name == "Gain") return RangeProperty;
Chris@153 583 if (name == "Normalize") return ToggleProperty;
Chris@284 584 if (name == "Threshold") return RangeProperty;
Chris@287 585 if (name == "Plot Type") return ValueProperty;
Chris@290 586 if (name == "Scale") return ValueProperty;
Chris@287 587 if (name == "Sampling Mode") return ValueProperty;
Chris@287 588 if (name == "Bin Scale") return ValueProperty;
Chris@287 589 if (name == "Colour" && m_plotStyle == PlotFilledBlocks) return ValueProperty;
Chris@287 590 return SingleColourLayer::getPropertyType(name);
Chris@153 591 }
Chris@153 592
Chris@153 593 QString
Chris@193 594 SliceLayer::getPropertyGroupName(const PropertyName &name) const
Chris@153 595 {
Chris@153 596 if (name == "Scale" ||
Chris@153 597 name == "Normalize" ||
Chris@193 598 name == "Sampling Mode" ||
Chris@284 599 name == "Threshold" ||
Chris@193 600 name == "Gain") return tr("Scale");
Chris@193 601 if (name == "Plot Type" ||
Chris@193 602 name == "Bin Scale") return tr("Plot Type");
Chris@287 603 return SingleColourLayer::getPropertyGroupName(name);
Chris@153 604 }
Chris@153 605
Chris@153 606 int
Chris@193 607 SliceLayer::getPropertyRangeAndValue(const PropertyName &name,
Chris@216 608 int *min, int *max, int *deflt) const
Chris@153 609 {
Chris@216 610 int val = 0;
Chris@153 611
Chris@216 612 int garbage0, garbage1, garbage2;
Chris@153 613 if (!min) min = &garbage0;
Chris@153 614 if (!max) max = &garbage1;
Chris@248 615 if (!deflt) deflt = &garbage2;
Chris@153 616
Chris@153 617 if (name == "Gain") {
Chris@153 618
Chris@153 619 *min = -50;
Chris@153 620 *max = 50;
Chris@216 621 *deflt = 0;
Chris@153 622
Chris@193 623 std::cerr << "gain is " << m_gain << ", mode is " << m_samplingMode << std::endl;
Chris@193 624
Chris@216 625 val = lrint(log10(m_gain) * 20.0);
Chris@216 626 if (val < *min) val = *min;
Chris@216 627 if (val > *max) val = *max;
Chris@153 628
Chris@284 629 } else if (name == "Threshold") {
Chris@284 630
Chris@284 631 *min = -80;
Chris@284 632 *max = 0;
Chris@284 633
Chris@284 634 *deflt = lrintf(AudioLevel::multiplier_to_dB(m_initialThreshold));
Chris@284 635 if (*deflt < *min) *deflt = *min;
Chris@284 636 if (*deflt > *max) *deflt = *max;
Chris@284 637
Chris@284 638 val = lrintf(AudioLevel::multiplier_to_dB(m_threshold));
Chris@284 639 if (val < *min) val = *min;
Chris@284 640 if (val > *max) val = *max;
Chris@284 641
Chris@153 642 } else if (name == "Normalize") {
Chris@153 643
Chris@216 644 val = (m_normalize ? 1 : 0);
Chris@216 645 *deflt = 0;
Chris@153 646
Chris@287 647 } else if (name == "Colour" && m_plotStyle == PlotFilledBlocks) {
Chris@197 648
Chris@287 649 *min = 0;
Chris@287 650 *max = ColourMapper::getColourMapCount() - 1;
Chris@287 651 *deflt = 0;
Chris@287 652
Chris@287 653 val = m_colourMap;
Chris@153 654
Chris@153 655 } else if (name == "Scale") {
Chris@153 656
Chris@153 657 *min = 0;
Chris@153 658 *max = 2;
Chris@216 659 *deflt = (int)dBScale;
Chris@153 660
Chris@216 661 val = (int)m_energyScale;
Chris@153 662
Chris@193 663 } else if (name == "Sampling Mode") {
Chris@153 664
Chris@153 665 *min = 0;
Chris@193 666 *max = 2;
Chris@216 667 *deflt = (int)SampleMean;
Chris@193 668
Chris@216 669 val = (int)m_samplingMode;
Chris@153 670
Chris@193 671 } else if (name == "Plot Type") {
Chris@193 672
Chris@193 673 *min = 0;
Chris@197 674 *max = 3;
Chris@216 675 *deflt = (int)PlotSteps;
Chris@193 676
Chris@216 677 val = (int)m_plotStyle;
Chris@193 678
Chris@193 679 } else if (name == "Bin Scale") {
Chris@193 680
Chris@193 681 *min = 0;
Chris@198 682 *max = 2;
Chris@216 683 *deflt = (int)LinearBins;
Chris@198 684 // *max = 1; // I don't think we really do want to offer inverted log
Chris@193 685
Chris@216 686 val = (int)m_binScale;
Chris@193 687
Chris@153 688 } else {
Chris@287 689 val = SingleColourLayer::getPropertyRangeAndValue(name, min, max, deflt);
Chris@153 690 }
Chris@153 691
Chris@216 692 return val;
Chris@153 693 }
Chris@153 694
Chris@153 695 QString
Chris@193 696 SliceLayer::getPropertyValueLabel(const PropertyName &name,
Chris@153 697 int value) const
Chris@153 698 {
Chris@287 699 if (name == "Colour" && m_plotStyle == PlotFilledBlocks) {
Chris@287 700 return ColourMapper::getColourMapName(value);
Chris@153 701 }
Chris@153 702 if (name == "Scale") {
Chris@153 703 switch (value) {
Chris@153 704 default:
Chris@153 705 case 0: return tr("Linear");
Chris@153 706 case 1: return tr("Meter");
Chris@153 707 case 2: return tr("dB");
Chris@153 708 }
Chris@153 709 }
Chris@193 710 if (name == "Sampling Mode") {
Chris@153 711 switch (value) {
Chris@153 712 default:
Chris@193 713 case 0: return tr("Any");
Chris@193 714 case 1: return tr("Mean");
Chris@193 715 case 2: return tr("Peak");
Chris@193 716 }
Chris@193 717 }
Chris@193 718 if (name == "Plot Type") {
Chris@193 719 switch (value) {
Chris@193 720 default:
Chris@193 721 case 0: return tr("Lines");
Chris@193 722 case 1: return tr("Steps");
Chris@193 723 case 2: return tr("Blocks");
Chris@197 724 case 3: return tr("Colours");
Chris@193 725 }
Chris@193 726 }
Chris@193 727 if (name == "Bin Scale") {
Chris@193 728 switch (value) {
Chris@193 729 default:
Chris@198 730 case 0: return tr("Linear Bins");
Chris@198 731 case 1: return tr("Log Bins");
Chris@198 732 case 2: return tr("Rev Log Bins");
Chris@153 733 }
Chris@153 734 }
Chris@287 735 return SingleColourLayer::getPropertyValueLabel(name, value);
Chris@153 736 }
Chris@153 737
Chris@167 738 RangeMapper *
Chris@193 739 SliceLayer::getNewPropertyRangeMapper(const PropertyName &name) const
Chris@167 740 {
Chris@167 741 if (name == "Gain") {
Chris@167 742 return new LinearRangeMapper(-50, 50, -25, 25, tr("dB"));
Chris@167 743 }
Chris@284 744 if (name == "Threshold") {
Chris@284 745 return new LinearRangeMapper(-80, 0, -80, 0, tr("dB"));
Chris@284 746 }
Chris@287 747 return SingleColourLayer::getNewPropertyRangeMapper(name);
Chris@167 748 }
Chris@167 749
Chris@133 750 void
Chris@193 751 SliceLayer::setProperty(const PropertyName &name, int value)
Chris@133 752 {
Chris@153 753 if (name == "Gain") {
Chris@153 754 setGain(pow(10, float(value)/20.0));
Chris@284 755 } else if (name == "Threshold") {
Chris@284 756 if (value == -80) setThreshold(0.0);
Chris@284 757 else setThreshold(AudioLevel::dB_to_multiplier(value));
Chris@287 758 } else if (name == "Colour" && m_plotStyle == PlotFilledBlocks) {
Chris@287 759 setFillColourMap(value);
Chris@153 760 } else if (name == "Scale") {
Chris@153 761 switch (value) {
Chris@153 762 default:
Chris@153 763 case 0: setEnergyScale(LinearScale); break;
Chris@153 764 case 1: setEnergyScale(MeterScale); break;
Chris@153 765 case 2: setEnergyScale(dBScale); break;
Chris@153 766 }
Chris@193 767 } else if (name == "Plot Type") {
Chris@193 768 setPlotStyle(PlotStyle(value));
Chris@193 769 } else if (name == "Sampling Mode") {
Chris@193 770 switch (value) {
Chris@193 771 default:
Chris@193 772 case 0: setSamplingMode(NearestSample); break;
Chris@193 773 case 1: setSamplingMode(SampleMean); break;
Chris@193 774 case 2: setSamplingMode(SamplePeak); break;
Chris@193 775 }
Chris@193 776 } else if (name == "Bin Scale") {
Chris@193 777 switch (value) {
Chris@193 778 default:
Chris@193 779 case 0: setBinScale(LinearBins); break;
Chris@193 780 case 1: setBinScale(LogBins); break;
Chris@193 781 case 2: setBinScale(InvertedLogBins); break;
Chris@193 782 }
Chris@153 783 } else if (name == "Normalize") {
Chris@153 784 setNormalize(value ? true : false);
Chris@287 785 } else {
Chris@287 786 SingleColourLayer::setProperty(name, value);
Chris@153 787 }
Chris@153 788 }
Chris@153 789
Chris@153 790 void
Chris@197 791 SliceLayer::setFillColourMap(int map)
Chris@197 792 {
Chris@197 793 if (m_colourMap == map) return;
Chris@197 794 m_colourMap = map;
Chris@197 795 emit layerParametersChanged();
Chris@197 796 }
Chris@197 797
Chris@197 798 void
Chris@193 799 SliceLayer::setEnergyScale(EnergyScale scale)
Chris@153 800 {
Chris@153 801 if (m_energyScale == scale) return;
Chris@153 802 m_energyScale = scale;
Chris@153 803 emit layerParametersChanged();
Chris@153 804 }
Chris@153 805
Chris@153 806 void
Chris@193 807 SliceLayer::setSamplingMode(SamplingMode mode)
Chris@153 808 {
Chris@193 809 if (m_samplingMode == mode) return;
Chris@193 810 m_samplingMode = mode;
Chris@153 811 emit layerParametersChanged();
Chris@153 812 }
Chris@153 813
Chris@153 814 void
Chris@193 815 SliceLayer::setPlotStyle(PlotStyle style)
Chris@153 816 {
Chris@193 817 if (m_plotStyle == style) return;
Chris@197 818 bool colourTypeChanged = (style == PlotFilledBlocks ||
Chris@197 819 m_plotStyle == PlotFilledBlocks);
Chris@193 820 m_plotStyle = style;
Chris@197 821 if (colourTypeChanged) {
Chris@197 822 emit layerParameterRangesChanged();
Chris@197 823 }
Chris@153 824 emit layerParametersChanged();
Chris@153 825 }
Chris@153 826
Chris@153 827 void
Chris@193 828 SliceLayer::setBinScale(BinScale scale)
Chris@153 829 {
Chris@193 830 if (m_binScale == scale) return;
Chris@193 831 m_binScale = scale;
Chris@153 832 emit layerParametersChanged();
Chris@153 833 }
Chris@153 834
Chris@153 835 void
Chris@193 836 SliceLayer::setNormalize(bool n)
Chris@153 837 {
Chris@153 838 if (m_normalize == n) return;
Chris@153 839 m_normalize = n;
Chris@153 840 emit layerParametersChanged();
Chris@153 841 }
Chris@153 842
Chris@153 843 void
Chris@284 844 SliceLayer::setThreshold(float thresh)
Chris@284 845 {
Chris@284 846 if (m_threshold == thresh) return;
Chris@284 847 m_threshold = thresh;
Chris@284 848 emit layerParametersChanged();
Chris@284 849 }
Chris@284 850
Chris@284 851 void
Chris@193 852 SliceLayer::setGain(float gain)
Chris@153 853 {
Chris@153 854 if (m_gain == gain) return;
Chris@153 855 m_gain = gain;
Chris@153 856 emit layerParametersChanged();
Chris@153 857 }
Chris@153 858
Chris@284 859 float
Chris@284 860 SliceLayer::getThresholdDb() const
Chris@284 861 {
Chris@284 862 if (m_threshold == 0.0) return -80.f;
Chris@284 863 float db = AudioLevel::multiplier_to_dB(m_threshold);
Chris@284 864 return db;
Chris@284 865 }
Chris@284 866
Chris@287 867 int
Chris@287 868 SliceLayer::getDefaultColourHint(bool darkbg, bool &impose)
Chris@287 869 {
Chris@287 870 impose = false;
Chris@287 871 return ColourDatabase::getInstance()->getColourIndex
Chris@287 872 (QString(darkbg ? "Bright Blue" : "Blue"));
Chris@287 873 }
Chris@287 874
Chris@316 875 void
Chris@316 876 SliceLayer::toXml(QTextStream &stream,
Chris@316 877 QString indent, QString extraAttributes) const
Chris@153 878 {
Chris@153 879 QString s;
Chris@153 880
Chris@287 881 s += QString("colourScheme=\"%1\" "
Chris@287 882 "energyScale=\"%2\" "
Chris@287 883 "samplingMode=\"%3\" "
Chris@287 884 "gain=\"%4\" "
Chris@287 885 "normalize=\"%5\"")
Chris@197 886 .arg(m_colourMap)
Chris@153 887 .arg(m_energyScale)
Chris@193 888 .arg(m_samplingMode)
Chris@153 889 .arg(m_gain)
Chris@153 890 .arg(m_normalize ? "true" : "false");
Chris@153 891
Chris@316 892 SingleColourLayer::toXml(stream, indent, extraAttributes + " " + s);
Chris@153 893 }
Chris@153 894
Chris@153 895 void
Chris@193 896 SliceLayer::setProperties(const QXmlAttributes &attributes)
Chris@153 897 {
Chris@153 898 bool ok = false;
Chris@153 899
Chris@287 900 SingleColourLayer::setProperties(attributes);
Chris@153 901
Chris@153 902 EnergyScale scale = (EnergyScale)
Chris@153 903 attributes.value("energyScale").toInt(&ok);
Chris@153 904 if (ok) setEnergyScale(scale);
Chris@153 905
Chris@193 906 SamplingMode mode = (SamplingMode)
Chris@193 907 attributes.value("samplingMode").toInt(&ok);
Chris@193 908 if (ok) setSamplingMode(mode);
Chris@153 909
Chris@197 910 int colourMap = attributes.value("colourScheme").toInt(&ok);
Chris@197 911 if (ok) setFillColourMap(colourMap);
Chris@197 912
Chris@153 913 float gain = attributes.value("gain").toFloat(&ok);
Chris@153 914 if (ok) setGain(gain);
Chris@153 915
Chris@153 916 bool normalize = (attributes.value("normalize").trimmed() == "true");
Chris@153 917 setNormalize(normalize);
Chris@133 918 }
Chris@133 919
Chris@133 920 bool
Chris@248 921 SliceLayer::getValueExtents(float &, float &, bool &, QString &) const
Chris@133 922 {
Chris@133 923 return false;
Chris@133 924 }
Chris@133 925