annotate layer/SliceLayer.cpp @ 1233:1a7c2ca31579

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