annotate layer/WaveformLayer.cpp @ 1367:f5566f7271fe waverevision

Rework waveform renderer to use smooth paths, aiming to get near-pixel-identical results when zoomed out far enough for a single path not to be relevant
author Chris Cannam
date Wed, 31 Oct 2018 15:06:32 +0000
parents c2a3ac0a6688
children ca9a36a5ab76
rev   line source
Chris@58 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@0 2
Chris@0 3 /*
Chris@59 4 Sonic Visualiser
Chris@59 5 An audio file viewer and annotation editor.
Chris@59 6 Centre for Digital Music, Queen Mary, University of London.
Chris@182 7 This file copyright 2006 Chris Cannam and QMUL.
Chris@0 8
Chris@59 9 This program is free software; you can redistribute it and/or
Chris@59 10 modify it under the terms of the GNU General Public License as
Chris@59 11 published by the Free Software Foundation; either version 2 of the
Chris@59 12 License, or (at your option) any later version. See the file
Chris@59 13 COPYING included with this distribution for more information.
Chris@0 14 */
Chris@0 15
Chris@0 16 #include "WaveformLayer.h"
Chris@0 17
Chris@0 18 #include "base/AudioLevel.h"
Chris@128 19 #include "view/View.h"
Chris@0 20 #include "base/Profiler.h"
Chris@167 21 #include "base/RangeMapper.h"
Chris@1147 22 #include "base/Strings.h"
Chris@1078 23
Chris@376 24 #include "ColourDatabase.h"
Chris@1078 25 #include "PaintAssistant.h"
Chris@1340 26
Chris@1340 27 #include "data/model/WaveformOversampler.h"
Chris@0 28
Chris@0 29 #include <QPainter>
Chris@0 30 #include <QPixmap>
Chris@316 31 #include <QTextStream>
Chris@0 32
Chris@0 33 #include <iostream>
Chris@0 34 #include <cmath>
Chris@0 35
Chris@1352 36 //#define DEBUG_WAVEFORM_PAINT 1
Chris@1338 37 //#define DEBUG_WAVEFORM_PAINT_BY_PIXEL 1
Chris@4 38
Chris@1333 39 using std::vector;
Chris@682 40
Chris@0 41
Chris@44 42 WaveformLayer::WaveformLayer() :
Chris@287 43 SingleColourLayer(),
Chris@0 44 m_model(0),
Chris@0 45 m_gain(1.0f),
Chris@67 46 m_autoNormalize(false),
Chris@0 47 m_showMeans(true),
Chris@0 48 m_channelMode(SeparateChannels),
Chris@0 49 m_channel(-1),
Chris@0 50 m_scale(LinearScale),
Chris@709 51 m_middleLineHeight(0.5),
Chris@0 52 m_aggressive(false),
Chris@0 53 m_cache(0),
Chris@1325 54 m_cacheValid(false)
Chris@0 55 {
Chris@0 56 }
Chris@0 57
Chris@0 58 WaveformLayer::~WaveformLayer()
Chris@0 59 {
Chris@0 60 delete m_cache;
Chris@0 61 }
Chris@0 62
Chris@0 63 void
Chris@0 64 WaveformLayer::setModel(const RangeSummarisableTimeValueModel *model)
Chris@0 65 {
Chris@69 66 bool channelsChanged = false;
Chris@69 67 if (m_channel == -1) {
Chris@69 68 if (!m_model) {
Chris@69 69 if (model) {
Chris@69 70 channelsChanged = true;
Chris@69 71 }
Chris@69 72 } else {
Chris@69 73 if (model &&
Chris@69 74 m_model->getChannelCount() != model->getChannelCount()) {
Chris@69 75 channelsChanged = true;
Chris@69 76 }
Chris@69 77 }
Chris@69 78 }
Chris@69 79
Chris@0 80 m_model = model;
Chris@0 81 m_cacheValid = false;
Chris@0 82 if (!m_model || !m_model->isOK()) return;
Chris@0 83
Chris@320 84 connectSignals(m_model);
Chris@301 85
Chris@0 86 emit modelReplaced();
Chris@69 87
Chris@69 88 if (channelsChanged) emit layerParametersChanged();
Chris@0 89 }
Chris@0 90
Chris@0 91 Layer::PropertyList
Chris@0 92 WaveformLayer::getProperties() const
Chris@0 93 {
Chris@287 94 PropertyList list = SingleColourLayer::getProperties();
Chris@87 95 list.push_back("Scale");
Chris@87 96 list.push_back("Gain");
Chris@87 97 list.push_back("Normalize Visible Area");
Chris@68 98
Chris@68 99 if (m_model && m_model->getChannelCount() > 1 && m_channel == -1) {
Chris@87 100 list.push_back("Channels");
Chris@68 101 }
Chris@68 102
Chris@0 103 return list;
Chris@0 104 }
Chris@0 105
Chris@87 106 QString
Chris@87 107 WaveformLayer::getPropertyLabel(const PropertyName &name) const
Chris@87 108 {
Chris@87 109 if (name == "Scale") return tr("Scale");
Chris@87 110 if (name == "Gain") return tr("Gain");
Chris@87 111 if (name == "Normalize Visible Area") return tr("Normalize Visible Area");
Chris@87 112 if (name == "Channels") return tr("Channels");
Chris@287 113 return SingleColourLayer::getPropertyLabel(name);
Chris@87 114 }
Chris@87 115
Chris@335 116 QString
Chris@335 117 WaveformLayer::getPropertyIconName(const PropertyName &name) const
Chris@335 118 {
Chris@335 119 if (name == "Normalize Visible Area") return "normalise";
Chris@335 120 return "";
Chris@335 121 }
Chris@335 122
Chris@0 123 Layer::PropertyType
Chris@0 124 WaveformLayer::getPropertyType(const PropertyName &name) const
Chris@0 125 {
Chris@87 126 if (name == "Gain") return RangeProperty;
Chris@87 127 if (name == "Normalize Visible Area") return ToggleProperty;
Chris@87 128 if (name == "Channels") return ValueProperty;
Chris@87 129 if (name == "Scale") return ValueProperty;
Chris@287 130 return SingleColourLayer::getPropertyType(name);
Chris@0 131 }
Chris@0 132
Chris@0 133 QString
Chris@0 134 WaveformLayer::getPropertyGroupName(const PropertyName &name) const
Chris@0 135 {
Chris@87 136 if (name == "Gain" ||
Chris@87 137 name == "Normalize Visible Area" ||
Chris@1266 138 name == "Scale") return tr("Scale");
Chris@0 139 return QString();
Chris@0 140 }
Chris@0 141
Chris@0 142 int
Chris@0 143 WaveformLayer::getPropertyRangeAndValue(const PropertyName &name,
Chris@216 144 int *min, int *max, int *deflt) const
Chris@0 145 {
Chris@216 146 int val = 0;
Chris@0 147
Chris@216 148 int garbage0, garbage1, garbage2;
Chris@56 149 if (!min) min = &garbage0;
Chris@56 150 if (!max) max = &garbage1;
Chris@216 151 if (!deflt) deflt = &garbage2;
Chris@10 152
Chris@87 153 if (name == "Gain") {
Chris@0 154
Chris@1266 155 *min = -50;
Chris@1266 156 *max = 50;
Chris@216 157 *deflt = 0;
Chris@0 158
Chris@1266 159 val = int(lrint(log10(m_gain) * 20.0));
Chris@1266 160 if (val < *min) val = *min;
Chris@1266 161 if (val > *max) val = *max;
Chris@0 162
Chris@87 163 } else if (name == "Normalize Visible Area") {
Chris@67 164
Chris@216 165 val = (m_autoNormalize ? 1 : 0);
Chris@216 166 *deflt = 0;
Chris@67 167
Chris@87 168 } else if (name == "Channels") {
Chris@0 169
Chris@67 170 *min = 0;
Chris@67 171 *max = 2;
Chris@216 172 *deflt = 0;
Chris@216 173 if (m_channelMode == MixChannels) val = 1;
Chris@216 174 else if (m_channelMode == MergeChannels) val = 2;
Chris@216 175 else val = 0;
Chris@0 176
Chris@87 177 } else if (name == "Scale") {
Chris@0 178
Chris@1266 179 *min = 0;
Chris@1266 180 *max = 2;
Chris@216 181 *deflt = 0;
Chris@0 182
Chris@1266 183 val = (int)m_scale;
Chris@0 184
Chris@0 185 } else {
Chris@1266 186 val = SingleColourLayer::getPropertyRangeAndValue(name, min, max, deflt);
Chris@0 187 }
Chris@0 188
Chris@216 189 return val;
Chris@0 190 }
Chris@0 191
Chris@0 192 QString
Chris@0 193 WaveformLayer::getPropertyValueLabel(const PropertyName &name,
Chris@1266 194 int value) const
Chris@0 195 {
Chris@87 196 if (name == "Scale") {
Chris@1266 197 switch (value) {
Chris@1266 198 default:
Chris@1266 199 case 0: return tr("Linear");
Chris@1266 200 case 1: return tr("Meter");
Chris@1266 201 case 2: return tr("dB");
Chris@1266 202 }
Chris@0 203 }
Chris@87 204 if (name == "Channels") {
Chris@67 205 switch (value) {
Chris@67 206 default:
Chris@67 207 case 0: return tr("Separate");
Chris@67 208 case 1: return tr("Mean");
Chris@67 209 case 2: return tr("Butterfly");
Chris@67 210 }
Chris@67 211 }
Chris@287 212 return SingleColourLayer::getPropertyValueLabel(name, value);
Chris@0 213 }
Chris@0 214
Chris@167 215 RangeMapper *
Chris@167 216 WaveformLayer::getNewPropertyRangeMapper(const PropertyName &name) const
Chris@167 217 {
Chris@167 218 if (name == "Gain") {
Chris@167 219 return new LinearRangeMapper(-50, 50, -25, 25, tr("dB"));
Chris@167 220 }
Chris@167 221 return 0;
Chris@167 222 }
Chris@167 223
Chris@0 224 void
Chris@0 225 WaveformLayer::setProperty(const PropertyName &name, int value)
Chris@0 226 {
Chris@87 227 if (name == "Gain") {
Chris@1266 228 setGain(float(pow(10, float(value)/20.0)));
Chris@87 229 } else if (name == "Normalize Visible Area") {
Chris@67 230 setAutoNormalize(value ? true : false);
Chris@87 231 } else if (name == "Channels") {
Chris@67 232 if (value == 1) setChannelMode(MixChannels);
Chris@67 233 else if (value == 2) setChannelMode(MergeChannels);
Chris@67 234 else setChannelMode(SeparateChannels);
Chris@87 235 } else if (name == "Scale") {
Chris@1266 236 switch (value) {
Chris@1266 237 default:
Chris@1266 238 case 0: setScale(LinearScale); break;
Chris@1266 239 case 1: setScale(MeterScale); break;
Chris@1266 240 case 2: setScale(dBScale); break;
Chris@1266 241 }
Chris@287 242 } else {
Chris@287 243 SingleColourLayer::setProperty(name, value);
Chris@0 244 }
Chris@0 245 }
Chris@0 246
Chris@0 247 void
Chris@67 248 WaveformLayer::setGain(float gain)
Chris@0 249 {
Chris@0 250 if (m_gain == gain) return;
Chris@0 251 m_gain = gain;
Chris@0 252 m_cacheValid = false;
Chris@0 253 emit layerParametersChanged();
Chris@133 254 emit verticalZoomChanged();
Chris@0 255 }
Chris@0 256
Chris@0 257 void
Chris@67 258 WaveformLayer::setAutoNormalize(bool autoNormalize)
Chris@67 259 {
Chris@67 260 if (m_autoNormalize == autoNormalize) return;
Chris@67 261 m_autoNormalize = autoNormalize;
Chris@67 262 m_cacheValid = false;
Chris@67 263 emit layerParametersChanged();
Chris@67 264 }
Chris@67 265
Chris@67 266 void
Chris@0 267 WaveformLayer::setShowMeans(bool showMeans)
Chris@0 268 {
Chris@0 269 if (m_showMeans == showMeans) return;
Chris@0 270 m_showMeans = showMeans;
Chris@0 271 m_cacheValid = false;
Chris@0 272 emit layerParametersChanged();
Chris@0 273 }
Chris@0 274
Chris@0 275 void
Chris@0 276 WaveformLayer::setChannelMode(ChannelMode channelMode)
Chris@0 277 {
Chris@0 278 if (m_channelMode == channelMode) return;
Chris@0 279 m_channelMode = channelMode;
Chris@0 280 m_cacheValid = false;
Chris@0 281 emit layerParametersChanged();
Chris@0 282 }
Chris@0 283
Chris@0 284 void
Chris@0 285 WaveformLayer::setChannel(int channel)
Chris@0 286 {
Chris@587 287 // SVDEBUG << "WaveformLayer::setChannel(" << channel << ")" << endl;
Chris@0 288
Chris@0 289 if (m_channel == channel) return;
Chris@0 290 m_channel = channel;
Chris@0 291 m_cacheValid = false;
Chris@0 292 emit layerParametersChanged();
Chris@0 293 }
Chris@0 294
Chris@0 295 void
Chris@0 296 WaveformLayer::setScale(Scale scale)
Chris@0 297 {
Chris@0 298 if (m_scale == scale) return;
Chris@0 299 m_scale = scale;
Chris@0 300 m_cacheValid = false;
Chris@0 301 emit layerParametersChanged();
Chris@0 302 }
Chris@0 303
Chris@0 304 void
Chris@908 305 WaveformLayer::setMiddleLineHeight(double height)
Chris@709 306 {
Chris@709 307 if (m_middleLineHeight == height) return;
Chris@709 308 m_middleLineHeight = height;
Chris@709 309 m_cacheValid = false;
Chris@709 310 emit layerParametersChanged();
Chris@709 311 }
Chris@709 312
Chris@709 313 void
Chris@0 314 WaveformLayer::setAggressiveCacheing(bool aggressive)
Chris@0 315 {
Chris@0 316 if (m_aggressive == aggressive) return;
Chris@0 317 m_aggressive = aggressive;
Chris@0 318 m_cacheValid = false;
Chris@0 319 emit layerParametersChanged();
Chris@0 320 }
Chris@0 321
Chris@0 322 int
Chris@918 323 WaveformLayer::getCompletion(LayerGeometryProvider *) const
Chris@0 324 {
Chris@0 325 int completion = 100;
Chris@0 326 if (!m_model || !m_model->isOK()) return completion;
Chris@0 327 if (m_model->isReady(&completion)) return 100;
Chris@0 328 return completion;
Chris@0 329 }
Chris@0 330
Chris@79 331 bool
Chris@908 332 WaveformLayer::getValueExtents(double &min, double &max,
Chris@248 333 bool &, QString &unit) const
Chris@79 334 {
Chris@79 335 if (m_scale == LinearScale) {
Chris@79 336 min = 0.0;
Chris@79 337 max = 1.0;
Chris@79 338 unit = "V";
Chris@79 339 } else if (m_scale == MeterScale) {
Chris@79 340 return false; //!!!
Chris@79 341 } else {
Chris@79 342 min = AudioLevel::multiplier_to_dB(0.0);
Chris@79 343 max = AudioLevel::multiplier_to_dB(1.0);
Chris@79 344 unit = "dB";
Chris@79 345 }
Chris@79 346 return true;
Chris@79 347 }
Chris@79 348
Chris@1367 349 double
Chris@908 350 WaveformLayer::dBscale(double sample, int m) const
Chris@0 351 {
Chris@67 352 if (sample < 0.0) return dBscale(-sample, m);
Chris@908 353 double dB = AudioLevel::multiplier_to_dB(sample);
Chris@0 354 if (dB < -50.0) return 0;
Chris@0 355 if (dB > 0.0) return m;
Chris@1367 356 return ((dB + 50.0) * m) / 50.0;
Chris@0 357 }
Chris@0 358
Chris@805 359 int
Chris@805 360 WaveformLayer::getChannelArrangement(int &min, int &max,
Chris@67 361 bool &merging, bool &mixing)
Chris@0 362 const
Chris@0 363 {
Chris@0 364 if (!m_model || !m_model->isOK()) return 0;
Chris@0 365
Chris@805 366 int channels = m_model->getChannelCount();
Chris@0 367 if (channels == 0) return 0;
Chris@0 368
Chris@805 369 int rawChannels = channels;
Chris@0 370
Chris@0 371 if (m_channel == -1) {
Chris@1266 372 min = 0;
Chris@1266 373 if (m_channelMode == MergeChannels ||
Chris@67 374 m_channelMode == MixChannels) {
Chris@1266 375 max = 0;
Chris@1266 376 channels = 1;
Chris@1266 377 } else {
Chris@1266 378 max = channels - 1;
Chris@1266 379 }
Chris@0 380 } else {
Chris@1266 381 min = m_channel;
Chris@1266 382 max = m_channel;
Chris@1266 383 rawChannels = 1;
Chris@1266 384 channels = 1;
Chris@0 385 }
Chris@0 386
Chris@0 387 merging = (m_channelMode == MergeChannels && rawChannels > 1);
Chris@67 388 mixing = (m_channelMode == MixChannels && rawChannels > 1);
Chris@0 389
Chris@587 390 // SVDEBUG << "WaveformLayer::getChannelArrangement: min " << min << ", max " << max << ", merging " << merging << ", channels " << channels << endl;
Chris@0 391
Chris@0 392 return channels;
Chris@0 393 }
Chris@0 394
Chris@67 395 bool
Chris@918 396 WaveformLayer::isLayerScrollable(const LayerGeometryProvider *) const
Chris@67 397 {
Chris@67 398 return !m_autoNormalize;
Chris@67 399 }
Chris@67 400
Chris@68 401 static float meterdbs[] = { -40, -30, -20, -15, -10,
Chris@68 402 -5, -3, -2, -1, -0.5, 0 };
Chris@68 403
Chris@365 404 bool
Chris@1336 405 WaveformLayer::getSourceFramesForX(LayerGeometryProvider *v,
Chris@1336 406 int x, int modelZoomLevel,
Chris@908 407 sv_frame_t &f0, sv_frame_t &f1) const
Chris@365 408 {
Chris@908 409 sv_frame_t viewFrame = v->getFrameForX(x);
Chris@365 410 if (viewFrame < 0) {
Chris@365 411 f0 = 0;
Chris@365 412 f1 = 0;
Chris@365 413 return false;
Chris@365 414 }
Chris@365 415
Chris@365 416 f0 = viewFrame;
Chris@365 417 f0 = f0 / modelZoomLevel;
Chris@365 418 f0 = f0 * modelZoomLevel;
Chris@365 419
Chris@1341 420 if (v->getZoomLevel().zone == ZoomLevel::PixelsPerFrame) {
Chris@1341 421 f1 = f0 + 1;
Chris@1341 422 } else {
Chris@1341 423 viewFrame = v->getFrameForX(x + 1);
Chris@1341 424 f1 = viewFrame;
Chris@1341 425 f1 = f1 / modelZoomLevel;
Chris@1341 426 f1 = f1 * modelZoomLevel;
Chris@1341 427 }
Chris@365 428
Chris@365 429 return (f0 < m_model->getEndFrame());
Chris@365 430 }
Chris@365 431
Chris@365 432 float
Chris@918 433 WaveformLayer::getNormalizeGain(LayerGeometryProvider *v, int channel) const
Chris@365 434 {
Chris@908 435 sv_frame_t startFrame = v->getStartFrame();
Chris@908 436 sv_frame_t endFrame = v->getEndFrame();
Chris@365 437
Chris@908 438 sv_frame_t modelStart = m_model->getStartFrame();
Chris@908 439 sv_frame_t modelEnd = m_model->getEndFrame();
Chris@365 440
Chris@908 441 sv_frame_t rangeStart, rangeEnd;
Chris@365 442
Chris@365 443 if (startFrame < modelStart) rangeStart = modelStart;
Chris@365 444 else rangeStart = startFrame;
Chris@365 445
Chris@365 446 if (endFrame < 0) rangeEnd = 0;
Chris@365 447 else if (endFrame > modelEnd) rangeEnd = modelEnd;
Chris@365 448 else rangeEnd = endFrame;
Chris@365 449
Chris@365 450 if (rangeEnd < rangeStart) rangeEnd = rangeStart;
Chris@365 451
Chris@365 452 RangeSummarisableTimeValueModel::Range range =
Chris@365 453 m_model->getSummary(channel, rangeStart, rangeEnd - rangeStart);
Chris@365 454
Chris@805 455 int minChannel = 0, maxChannel = 0;
Chris@365 456 bool mergingChannels = false, mixingChannels = false;
Chris@365 457
Chris@859 458 (void)getChannelArrangement(minChannel, maxChannel,
Chris@859 459 mergingChannels, mixingChannels);
Chris@365 460
Chris@365 461 if (mergingChannels || mixingChannels) {
Chris@365 462 RangeSummarisableTimeValueModel::Range otherRange =
Chris@365 463 m_model->getSummary(1, rangeStart, rangeEnd - rangeStart);
Chris@386 464 range.setMax(std::max(range.max(), otherRange.max()));
Chris@386 465 range.setMin(std::min(range.min(), otherRange.min()));
Chris@386 466 range.setAbsmean(std::min(range.absmean(), otherRange.absmean()));
Chris@365 467 }
Chris@365 468
Chris@908 469 return float(1.0 / std::max(fabs(range.max()), fabs(range.min())));
Chris@365 470 }
Chris@365 471
Chris@0 472 void
Chris@916 473 WaveformLayer::paint(LayerGeometryProvider *v, QPainter &viewPainter, QRect rect) const
Chris@0 474 {
Chris@0 475 if (!m_model || !m_model->isOK()) {
Chris@1266 476 return;
Chris@0 477 }
Chris@0 478
Chris@1325 479 ZoomLevel zoomLevel = v->getZoomLevel();
Chris@0 480
Chris@2 481 #ifdef DEBUG_WAVEFORM_PAINT
Chris@0 482 Profiler profiler("WaveformLayer::paint", true);
Chris@1338 483 SVCERR << "WaveformLayer::paint (" << rect.x() << "," << rect.y()
Chris@1266 484 << ") [" << rect.width() << "x" << rect.height() << "]: zoom " << zoomLevel << endl;
Chris@2 485 #endif
Chris@0 486
Chris@805 487 int channels = 0, minChannel = 0, maxChannel = 0;
Chris@67 488 bool mergingChannels = false, mixingChannels = false;
Chris@0 489
Chris@67 490 channels = getChannelArrangement(minChannel, maxChannel,
Chris@67 491 mergingChannels, mixingChannels);
Chris@0 492 if (channels == 0) return;
Chris@0 493
Chris@915 494 int w = v->getPaintWidth();
Chris@915 495 int h = v->getPaintHeight();
Chris@0 496
Chris@0 497 QPainter *paint;
Chris@0 498
Chris@0 499 if (m_aggressive) {
Chris@0 500
Chris@214 501 #ifdef DEBUG_WAVEFORM_PAINT
Chris@1338 502 SVCERR << "WaveformLayer::paint: aggressive is true" << endl;
Chris@214 503 #endif
Chris@214 504
Chris@1325 505 using namespace std::rel_ops;
Chris@1325 506
Chris@1266 507 if (m_cacheValid && (zoomLevel != m_cacheZoomLevel)) {
Chris@1266 508 m_cacheValid = false;
Chris@1266 509 }
Chris@0 510
Chris@1266 511 if (!m_cache || m_cache->width() != w || m_cache->height() != h) {
Chris@214 512 #ifdef DEBUG_WAVEFORM_PAINT
Chris@214 513 if (m_cache) {
Chris@1338 514 SVCERR << "WaveformLayer::paint: cache size " << m_cache->width() << "x" << m_cache->height() << " differs from view size " << w << "x" << h << ": regenerating aggressive cache" << endl;
Chris@214 515 }
Chris@214 516 #endif
Chris@1266 517 delete m_cache;
Chris@1266 518 m_cache = new QPixmap(w, h);
Chris@214 519 m_cacheValid = false;
Chris@1266 520 }
Chris@214 521
Chris@1266 522 if (m_cacheValid) {
Chris@1266 523 viewPainter.drawPixmap(rect, *m_cache, rect);
Chris@1266 524 return;
Chris@1266 525 }
Chris@0 526
Chris@1266 527 paint = new QPainter(m_cache);
Chris@0 528
Chris@1266 529 paint->setPen(Qt::NoPen);
Chris@1266 530 paint->setBrush(getBackgroundQColor(v));
Chris@1266 531 paint->drawRect(rect);
Chris@0 532
Chris@1266 533 paint->setPen(getForegroundQColor(v));
Chris@1266 534 paint->setBrush(Qt::NoBrush);
Chris@0 535
Chris@0 536 } else {
Chris@1266 537 paint = &viewPainter;
Chris@0 538 }
Chris@0 539
Chris@1367 540 paint->setRenderHint(QPainter::Antialiasing, true);
Chris@28 541
Chris@709 542 if (m_middleLineHeight != 0.5) {
Chris@709 543 paint->save();
Chris@908 544 double space = m_middleLineHeight * 2;
Chris@709 545 if (space > 1.0) space = 2.0 - space;
Chris@908 546 double yt = h * (m_middleLineHeight - space/2);
Chris@709 547 paint->translate(QPointF(0, yt));
Chris@709 548 paint->scale(1.0, space);
Chris@709 549 }
Chris@709 550
Chris@0 551 int x0 = 0, x1 = w - 1;
Chris@0 552
Chris@0 553 x0 = rect.left();
Chris@0 554 x1 = rect.right();
Chris@0 555
Chris@1334 556 if (x0 > 0) {
Chris@1334 557 rect.adjust(-1, 0, 0, 0);
Chris@1334 558 x0 = rect.left();
Chris@1334 559 }
Chris@1334 560
Chris@1334 561 if (x1 < w) {
Chris@1334 562 rect.adjust(0, 0, 1, 0);
Chris@1334 563 x1 = rect.right();
Chris@1334 564 }
Chris@28 565
Chris@365 566 // Our zoom level may differ from that at which the underlying
Chris@365 567 // model has its blocks.
Chris@302 568
Chris@365 569 // Each pixel within our visible range must always draw from
Chris@365 570 // exactly the same set of underlying audio frames, no matter what
Chris@365 571 // the range being drawn is. And that set of underlying frames
Chris@365 572 // must remain the same when we scroll one or more pixels left or
Chris@365 573 // right.
Chris@1325 574
Chris@1325 575 int desiredBlockSize = 1;
Chris@1325 576 if (zoomLevel.zone == ZoomLevel::FramesPerPixel) {
Chris@1325 577 desiredBlockSize = zoomLevel.level;
Chris@1325 578 }
Chris@1325 579 int blockSize = m_model->getSummaryBlockSize(desiredBlockSize);
Chris@365 580
Chris@908 581 sv_frame_t frame0;
Chris@908 582 sv_frame_t frame1;
Chris@908 583 sv_frame_t spare;
Chris@365 584
Chris@1325 585 getSourceFramesForX(v, x0, blockSize, frame0, spare);
Chris@1325 586 getSourceFramesForX(v, x1, blockSize, spare, frame1);
Chris@365 587
Chris@4 588 #ifdef DEBUG_WAVEFORM_PAINT
Chris@1338 589 SVCERR << "Painting waveform from " << frame0 << " to " << frame1 << " (" << (x1-x0+1) << " pixels at zoom " << zoomLevel << " and model zoom " << blockSize << ")" << endl;
Chris@4 590 #endif
Chris@0 591
Chris@1338 592 m_effectiveGains.clear();
Chris@805 593 while ((int)m_effectiveGains.size() <= maxChannel) {
Chris@67 594 m_effectiveGains.push_back(m_gain);
Chris@67 595 }
Chris@1337 596 if (m_autoNormalize) {
Chris@1337 597 for (int ch = minChannel; ch <= maxChannel; ++ch) {
Chris@1337 598 m_effectiveGains[ch] = getNormalizeGain(v, ch);
Chris@1337 599 }
Chris@1336 600 }
Chris@67 601
Chris@1338 602 RangeVec ranges;
Chris@1338 603
Chris@1336 604 if (v->getZoomLevel().zone == ZoomLevel::FramesPerPixel) {
Chris@1338 605 getSummaryRanges(minChannel, maxChannel,
Chris@1338 606 mixingChannels || mergingChannels,
Chris@1338 607 frame0, frame1,
Chris@1338 608 blockSize, ranges);
Chris@1338 609 } else {
Chris@1338 610 getOversampledRanges(minChannel, maxChannel,
Chris@1338 611 mixingChannels || mergingChannels,
Chris@1338 612 frame0, frame1,
Chris@1338 613 v->getZoomLevel().level, ranges);
Chris@1338 614 }
Chris@1333 615
Chris@1343 616 if (!ranges.empty()) {
Chris@1343 617 for (int ch = minChannel; ch <= maxChannel; ++ch) {
Chris@1343 618 paintChannel(v, paint, rect, ch, ranges, blockSize,
Chris@1343 619 frame0, frame1);
Chris@1343 620 }
Chris@1333 621 }
Chris@1333 622
Chris@709 623 if (m_middleLineHeight != 0.5) {
Chris@709 624 paint->restore();
Chris@709 625 }
Chris@709 626
Chris@0 627 if (m_aggressive) {
Chris@1332 628 if (m_model->isReady() && rect == v->getPaintRect()) {
Chris@1266 629 m_cacheValid = true;
Chris@1266 630 m_cacheZoomLevel = zoomLevel;
Chris@1266 631 }
Chris@1266 632 paint->end();
Chris@1266 633 delete paint;
Chris@1266 634 viewPainter.drawPixmap(rect, *m_cache, rect);
Chris@0 635 }
Chris@0 636 }
Chris@0 637
Chris@1332 638 void
Chris@1338 639 WaveformLayer::getSummaryRanges(int minChannel, int maxChannel,
Chris@1338 640 bool mixingOrMerging,
Chris@1338 641 sv_frame_t frame0, sv_frame_t frame1,
Chris@1338 642 int blockSize, RangeVec &ranges)
Chris@1338 643 const
Chris@1338 644 {
Chris@1338 645 for (int ch = minChannel; ch <= maxChannel; ++ch) {
Chris@1338 646 ranges.push_back({});
Chris@1338 647 m_model->getSummaries(ch, frame0, frame1 - frame0,
Chris@1338 648 ranges[ch - minChannel], blockSize);
Chris@1338 649 #ifdef DEBUG_WAVEFORM_PAINT
Chris@1338 650 SVCERR << "channel " << ch << ": " << ranges[ch - minChannel].size() << " ranges from " << frame0 << " to " << frame1 << " at zoom level " << blockSize << endl;
Chris@1338 651 #endif
Chris@1338 652 }
Chris@1338 653
Chris@1338 654 if (mixingOrMerging) {
Chris@1338 655 if (minChannel != 0 || maxChannel != 0) {
Chris@1366 656 throw std::logic_error("Internal error: min & max channels should be 0 when merging or mixing all channels");
Chris@1338 657 } else if (m_model->getChannelCount() > 1) {
Chris@1338 658 ranges.push_back({});
Chris@1338 659 m_model->getSummaries
Chris@1338 660 (1, frame0, frame1 - frame0, ranges[1], blockSize);
Chris@1338 661 }
Chris@1338 662 }
Chris@1338 663 }
Chris@1338 664
Chris@1338 665 void
Chris@1338 666 WaveformLayer::getOversampledRanges(int minChannel, int maxChannel,
Chris@1366 667 bool mixingOrMerging,
Chris@1338 668 sv_frame_t frame0, sv_frame_t frame1,
Chris@1338 669 int oversampleBy, RangeVec &ranges)
Chris@1338 670 const
Chris@1338 671 {
Chris@1366 672 if (mixingOrMerging) {
Chris@1366 673 if (minChannel != 0 || maxChannel != 0) {
Chris@1366 674 throw std::logic_error("Internal error: min & max channels should be 0 when merging or mixing all channels");
Chris@1366 675 }
Chris@1366 676 if (m_model->getChannelCount() > 1) {
Chris@1366 677 // call back on self for the individual channels with
Chris@1366 678 // mixingOrMerging false
Chris@1366 679 getOversampledRanges
Chris@1366 680 (0, 1, false, frame0, frame1, oversampleBy, ranges);
Chris@1366 681 return;
Chris@1366 682 }
Chris@1366 683 }
Chris@1366 684
Chris@1338 685 // These frame values, tail length, etc variables are at the model
Chris@1338 686 // sample rate, not the oversampled rate
Chris@1338 687
Chris@1338 688 sv_frame_t tail = 16;
Chris@1338 689 sv_frame_t startFrame = m_model->getStartFrame();
Chris@1338 690 sv_frame_t endFrame = m_model->getEndFrame();
Chris@1338 691
Chris@1338 692 sv_frame_t rf0 = frame0 - tail;
Chris@1338 693 if (rf0 < startFrame) {
Chris@1338 694 rf0 = 0;
Chris@1338 695 }
Chris@1338 696
Chris@1338 697 sv_frame_t rf1 = frame1 + tail;
Chris@1338 698 if (rf1 >= endFrame) {
Chris@1338 699 rf1 = endFrame - 1;
Chris@1338 700 }
Chris@1338 701 if (rf1 <= rf0) {
Chris@1338 702 SVCERR << "WARNING: getOversampledRanges: rf1 (" << rf1 << ") <= rf0 ("
Chris@1338 703 << rf0 << ")" << endl;
Chris@1338 704 return;
Chris@1338 705 }
Chris@1338 706
Chris@1338 707 for (int ch = minChannel; ch <= maxChannel; ++ch) {
Chris@1339 708 floatvec_t oversampled = WaveformOversampler::getOversampledData
Chris@1339 709 (m_model, ch, frame0, frame1 - frame0, oversampleBy);
Chris@1338 710 RangeSummarisableTimeValueModel::RangeBlock rr;
Chris@1339 711 for (float v: oversampled) {
Chris@1338 712 RangeSummarisableTimeValueModel::Range r;
Chris@1339 713 r.sample(v);
Chris@1338 714 rr.push_back(r);
Chris@1338 715 }
Chris@1338 716 ranges.push_back(rr);
Chris@1338 717
Chris@1338 718 #ifdef DEBUG_WAVEFORM_PAINT
Chris@1338 719 SVCERR << "getOversampledRanges: " << frame0 << " -> " << frame1
Chris@1338 720 << " (" << frame1 - frame0 << "-frame range) at ratio "
Chris@1338 721 << oversampleBy << " with tail " << tail
Chris@1338 722 << " -> got " << oversampled.size()
Chris@1338 723 << " oversampled values for channel " << ch
Chris@1338 724 << ", from which returning " << rr.size() << " ranges" << endl;
Chris@1338 725 #endif
Chris@1338 726 }
Chris@1338 727
Chris@1338 728 return;
Chris@1338 729 }
Chris@1338 730
Chris@1338 731 void
Chris@1338 732 WaveformLayer::paintChannel(LayerGeometryProvider *v,
Chris@1338 733 QPainter *paint,
Chris@1338 734 QRect rect, int ch,
Chris@1338 735 const RangeVec &ranges,
Chris@1338 736 int blockSize,
Chris@1352 737 sv_frame_t frame0,
Chris@1352 738 sv_frame_t frame1)
Chris@1332 739 const
Chris@1332 740 {
Chris@1334 741 int x0 = rect.left();
Chris@1334 742 int y0 = rect.top();
Chris@1334 743
Chris@1334 744 int x1 = rect.right();
Chris@1334 745 int y1 = rect.bottom();
Chris@1334 746
Chris@1332 747 int h = v->getPaintHeight();
Chris@1332 748
Chris@1332 749 int channels = 0, minChannel = 0, maxChannel = 0;
Chris@1332 750 bool mergingChannels = false, mixingChannels = false;
Chris@1332 751
Chris@1332 752 channels = getChannelArrangement(minChannel, maxChannel,
Chris@1332 753 mergingChannels, mixingChannels);
Chris@1332 754 if (channels == 0) return;
Chris@1332 755
Chris@1332 756 QColor baseColour = getBaseQColor();
Chris@1332 757 QColor midColour = baseColour;
Chris@1367 758
Chris@1332 759 if (midColour == Qt::black) {
Chris@1332 760 midColour = Qt::gray;
Chris@1332 761 } else if (v->hasLightBackground()) {
Chris@1332 762 midColour = midColour.light(150);
Chris@1332 763 } else {
Chris@1332 764 midColour = midColour.light(50);
Chris@1332 765 }
Chris@1332 766
Chris@1332 767 double gain = m_effectiveGains[ch];
Chris@1332 768
Chris@1332 769 int m = (h / channels) / 2;
Chris@1332 770 int my = m + (((ch - minChannel) * h) / channels);
Chris@1332 771
Chris@1332 772 #ifdef DEBUG_WAVEFORM_PAINT
Chris@1338 773 SVCERR << "ch = " << ch << ", channels = " << channels << ", m = " << m << ", my = " << my << ", h = " << h << endl;
Chris@1332 774 #endif
Chris@1332 775
Chris@1332 776 if (my - m > y1 || my + m < y0) return;
Chris@1332 777
Chris@1332 778 if ((m_scale == dBScale || m_scale == MeterScale) &&
Chris@1332 779 m_channelMode != MergeChannels) {
Chris@1332 780 m = (h / channels);
Chris@1332 781 my = m + (((ch - minChannel) * h) / channels);
Chris@1332 782 }
Chris@1332 783
Chris@1367 784 // Horizontal axis along middle
Chris@1367 785 paint->setPen(QPen(midColour, 0));
Chris@1332 786 paint->drawLine(x0, my, x1, my);
Chris@1332 787
Chris@1335 788 paintChannelScaleGuides(v, paint, rect, ch);
Chris@1332 789
Chris@1333 790 int rangeix = ch - minChannel;
Chris@1338 791
Chris@1338 792 #ifdef DEBUG_WAVEFORM_PAINT
Chris@1338 793 SVCERR << "paint channel " << ch << ": frame0 = " << frame0 << ", frame1 = " << frame1 << ", blockSize = " << blockSize << ", have " << ranges.size() << " range blocks of which ours is index " << rangeix << " with " << ranges[rangeix].size() << " ranges in it" << endl;
Chris@1352 794 #else
Chris@1352 795 (void)frame1; // not actually used
Chris@1338 796 #endif
Chris@1338 797
Chris@1367 798 QPainterPath waveformPath;
Chris@1367 799 QPainterPath meanPath;
Chris@1367 800 QPainterPath clipPath;
Chris@1367 801 vector<QPointF> individualSamplePoints;
Chris@1367 802
Chris@1367 803 bool firstPoint = true;
Chris@1367 804
Chris@1332 805 for (int x = x0; x <= x1; ++x) {
Chris@1332 806
Chris@1332 807 sv_frame_t f0, f1;
Chris@1338 808 sv_frame_t i0, i1;
Chris@1338 809
Chris@1338 810 bool showIndividualSample = false;
Chris@1338 811
Chris@1338 812 if (v->getZoomLevel().zone == ZoomLevel::FramesPerPixel) {
Chris@1338 813 if (!getSourceFramesForX(v, x, blockSize, f0, f1)) {
Chris@1338 814 continue;
Chris@1338 815 }
Chris@1338 816 f1 = f1 - 1;
Chris@1338 817 i0 = (f0 - frame0) / blockSize;
Chris@1338 818 i1 = (f1 - frame0) / blockSize;
Chris@1338 819 } else {
Chris@1338 820 int oversampleBy = v->getZoomLevel().level;
Chris@1338 821 f0 = f1 = v->getFrameForX(x);
Chris@1338 822 int xf0 = v->getXForFrame(f0);
Chris@1338 823 showIndividualSample = (x == xf0);
Chris@1338 824 i0 = i1 = (f0 - frame0) * oversampleBy + (x - xf0);
Chris@1338 825 }
Chris@1332 826
Chris@1332 827 if (f0 < frame0) {
Chris@1338 828 SVCERR << "ERROR: WaveformLayer::paint: pixel " << x << " has f0 = " << f0 << " which is less than range frame0 " << frame0 << " for x0 = " << x0 << endl;
Chris@1332 829 continue;
Chris@1332 830 }
Chris@1332 831
Chris@1338 832 #ifdef DEBUG_WAVEFORM_PAINT_BY_PIXEL
Chris@1338 833 SVCERR << "WaveformLayer::paint: pixel " << x << ": i0 " << i0 << " (f " << f0 << "), i1 " << i1 << " (f " << f1 << ")" << endl;
Chris@1332 834 #endif
Chris@1332 835
Chris@1332 836 if (i1 > i0 + 1) {
Chris@1338 837 SVCERR << "WaveformLayer::paint: ERROR: i1 " << i1 << " > i0 " << i0 << " plus one (zoom = " << v->getZoomLevel() << ", model zoom = " << blockSize << ")" << endl;
Chris@1332 838 }
Chris@1332 839
Chris@1333 840 const auto &r = ranges[rangeix];
Chris@1333 841 RangeSummarisableTimeValueModel::Range range;
Chris@1333 842
Chris@1333 843 if (in_range_for(r, i0)) {
Chris@1332 844
Chris@1333 845 range = r[i0];
Chris@1332 846
Chris@1333 847 if (i1 > i0 && in_range_for(r, i1)) {
Chris@1333 848 range.setMax(std::max(range.max(), r[i1].max()));
Chris@1333 849 range.setMin(std::min(range.min(), r[i1].min()));
Chris@1333 850 range.setAbsmean((range.absmean() + r[i1].absmean()) / 2);
Chris@1332 851 }
Chris@1332 852
Chris@1332 853 } else {
Chris@1332 854 #ifdef DEBUG_WAVEFORM_PAINT
Chris@1338 855 SVCERR << "No (or not enough) ranges for index i0 = " << i0 << " (there are " << r.size() << " range(s))" << endl;
Chris@1332 856 #endif
Chris@1332 857 continue;
Chris@1332 858 }
Chris@1332 859
Chris@1367 860 double rangeBottom = 0, rangeTop = 0, meanBottom = 0, meanTop = 0;
Chris@1332 861
Chris@1333 862 if (mergingChannels && ranges.size() > 1) {
Chris@1332 863
Chris@1333 864 const auto &other = ranges[1];
Chris@1333 865
Chris@1333 866 if (in_range_for(other, i0)) {
Chris@1332 867
Chris@1332 868 range.setMax(fabsf(range.max()));
Chris@1333 869 range.setMin(-fabsf(other[i0].max()));
Chris@1332 870 range.setAbsmean
Chris@1333 871 ((range.absmean() + other[i0].absmean()) / 2);
Chris@1332 872
Chris@1333 873 if (i1 > i0 && in_range_for(other, i1)) {
Chris@1332 874 // let's not concern ourselves about the mean
Chris@1333 875 range.setMin(std::min(range.min(),
Chris@1333 876 -fabsf(other[i1].max())));
Chris@1332 877 }
Chris@1332 878 }
Chris@1332 879
Chris@1333 880 } else if (mixingChannels && ranges.size() > 1) {
Chris@1332 881
Chris@1333 882 const auto &other = ranges[1];
Chris@1333 883
Chris@1333 884 if (in_range_for(other, i0)) {
Chris@1332 885
Chris@1333 886 range.setMax((range.max() + other[i0].max()) / 2);
Chris@1333 887 range.setMin((range.min() + other[i0].min()) / 2);
Chris@1333 888 range.setAbsmean((range.absmean() + other[i0].absmean()) / 2);
Chris@1332 889 }
Chris@1332 890 }
Chris@1332 891
Chris@1332 892 switch (m_scale) {
Chris@1332 893
Chris@1332 894 case LinearScale:
Chris@1367 895 rangeBottom = range.min() * gain * m;
Chris@1367 896 rangeTop = range.max() * gain * m;
Chris@1367 897 meanBottom = range.absmean() * gain * (-m);
Chris@1367 898 meanTop = range.absmean() * gain * m;
Chris@1332 899 break;
Chris@1332 900
Chris@1332 901 case dBScale:
Chris@1332 902 if (!mergingChannels) {
Chris@1367 903 double db0 = dBscale(range.min() * gain, m);
Chris@1367 904 double db1 = dBscale(range.max() * gain, m);
Chris@1367 905 rangeTop = std::max(db0, db1);
Chris@1367 906 meanTop = std::min(db0, db1);
Chris@1332 907 if (mixingChannels) rangeBottom = meanTop;
Chris@1332 908 else rangeBottom = dBscale(range.absmean() * gain, m);
Chris@1367 909 meanBottom = rangeBottom;
Chris@1332 910 } else {
Chris@1367 911 rangeBottom = -dBscale(range.min() * gain, m);
Chris@1367 912 rangeTop = dBscale(range.max() * gain, m);
Chris@1367 913 meanBottom = -dBscale(range.absmean() * gain, m);
Chris@1367 914 meanTop = dBscale(range.absmean() * gain, m);
Chris@1332 915 }
Chris@1332 916 break;
Chris@1332 917
Chris@1332 918 case MeterScale:
Chris@1332 919 if (!mergingChannels) {
Chris@1367 920 double r0 = fabs(AudioLevel::multiplier_to_preview
Chris@1367 921 (range.min() * gain, m));
Chris@1367 922 double r1 = fabs(AudioLevel::multiplier_to_preview
Chris@1367 923 (range.max() * gain, m));
Chris@1367 924 rangeTop = std::max(r0, r1);
Chris@1367 925 meanTop = std::min(r0, r1);
Chris@1332 926 if (mixingChannels) rangeBottom = meanTop;
Chris@1367 927 else rangeBottom = AudioLevel::multiplier_to_preview
Chris@1367 928 (range.absmean() * gain, m);
Chris@1332 929 meanBottom = rangeBottom;
Chris@1332 930 } else {
Chris@1367 931 rangeBottom = -AudioLevel::multiplier_to_preview
Chris@1367 932 (range.min() * gain, m);
Chris@1367 933 rangeTop = AudioLevel::multiplier_to_preview
Chris@1367 934 (range.max() * gain, m);
Chris@1367 935 meanBottom = -AudioLevel::multiplier_to_preview
Chris@1367 936 (range.absmean() * gain, m);
Chris@1367 937 meanTop = AudioLevel::multiplier_to_preview
Chris@1367 938 (range.absmean() * gain, m);
Chris@1332 939 }
Chris@1332 940 break;
Chris@1332 941 }
Chris@1332 942
Chris@1367 943 rangeBottom = my - rangeBottom;
Chris@1367 944 rangeTop = my - rangeTop;
Chris@1367 945 meanBottom = my - meanBottom;
Chris@1367 946 meanTop = my - meanTop;
Chris@1332 947
Chris@1332 948 bool clipped = false;
Chris@1332 949
Chris@1332 950 if (rangeTop < my - m) { rangeTop = my - m; }
Chris@1332 951 if (rangeTop > my + m) { rangeTop = my + m; }
Chris@1332 952 if (rangeBottom < my - m) { rangeBottom = my - m; }
Chris@1332 953 if (rangeBottom > my + m) { rangeBottom = my + m; }
Chris@1332 954
Chris@1367 955 if (range.max() <= -1.0 || range.max() >= 1.0) {
Chris@1367 956 clipped = true;
Chris@1367 957 }
Chris@1332 958
Chris@1367 959 bool drawMean = m_showMeans;
Chris@1332 960
Chris@1367 961 meanTop = meanTop - 0.5;
Chris@1367 962 meanBottom = meanBottom + 0.5;
Chris@1367 963
Chris@1367 964 if (meanTop <= rangeTop + 1.0) {
Chris@1367 965 meanTop = rangeTop + 1.0;
Chris@1332 966 }
Chris@1367 967 if (meanBottom >= rangeBottom - 1.0 && m_scale == LinearScale) {
Chris@1367 968 meanBottom = rangeBottom - 1.0;
Chris@1332 969 }
Chris@1367 970 if (meanTop > meanBottom - 1.0) {
Chris@1367 971 drawMean = false;
Chris@1332 972 }
Chris@1332 973
Chris@1338 974 #ifdef DEBUG_WAVEFORM_PAINT_BY_PIXEL
Chris@1338 975 SVCERR << "range " << rangeBottom << " -> " << rangeTop << ", means " << meanBottom << " -> " << meanTop << ", raw range " << range.min() << " -> " << range.max() << endl;
Chris@1332 976 #endif
Chris@1332 977
Chris@1367 978 double rangeMiddle = (rangeTop + rangeBottom) / 2.0;
Chris@1367 979 bool trivialRange = (fabs(rangeTop - rangeBottom) < 1.0);
Chris@1367 980 double px = x + 0.5;
Chris@1367 981
Chris@1367 982 if (showIndividualSample) {
Chris@1367 983 individualSamplePoints.push_back(QPointF(px, rangeTop));
Chris@1367 984 if (!trivialRange) {
Chris@1367 985 // common e.g. in "butterfly" merging mode
Chris@1367 986 individualSamplePoints.push_back(QPointF(px, rangeBottom));
Chris@1332 987 }
Chris@1332 988 }
Chris@1332 989
Chris@1367 990 if (firstPoint) {
Chris@1367 991 waveformPath = QPainterPath(QPointF(px, rangeMiddle));
Chris@1367 992 firstPoint = false;
Chris@1367 993 } else {
Chris@1367 994 waveformPath.lineTo(QPointF(px, rangeMiddle));
Chris@1367 995 }
Chris@1367 996
Chris@1367 997 if (!trivialRange) {
Chris@1367 998 waveformPath.lineTo(QPointF(px, rangeTop));
Chris@1367 999 waveformPath.lineTo(QPointF(px, rangeBottom));
Chris@1367 1000 waveformPath.lineTo(QPointF(px, rangeMiddle));
Chris@1367 1001 }
Chris@1367 1002
Chris@1332 1003 if (drawMean) {
Chris@1367 1004 meanPath.moveTo(QPointF(px, meanBottom));
Chris@1367 1005 meanPath.lineTo(QPointF(px, meanTop));
Chris@1332 1006 }
Chris@1367 1007
Chris@1367 1008 if (clipped) {
Chris@1367 1009 if (trivialRange) {
Chris@1367 1010 clipPath.moveTo(QPointF(px, rangeMiddle));
Chris@1367 1011 clipPath.lineTo(QPointF(px+1, rangeMiddle));
Chris@1367 1012 } else {
Chris@1367 1013 clipPath.moveTo(QPointF(px, rangeBottom));
Chris@1367 1014 clipPath.lineTo(QPointF(px, rangeTop));
Chris@1367 1015 }
Chris@1367 1016 }
Chris@1367 1017 }
Chris@1367 1018
Chris@1367 1019 double penWidth = 1.0;
Chris@1367 1020
Chris@1367 1021 if (m_model->isReady()) {
Chris@1367 1022 paint->setPen(QPen(baseColour, penWidth));
Chris@1367 1023 } else {
Chris@1367 1024 paint->setPen(QPen(midColour, penWidth));
Chris@1367 1025 }
Chris@1367 1026 paint->drawPath(waveformPath);
Chris@1367 1027
Chris@1367 1028 if (!clipPath.isEmpty()) {
Chris@1367 1029 paint->save();
Chris@1367 1030 paint->setPen(QPen(ColourDatabase::getInstance()->
Chris@1367 1031 getContrastingColour(m_colour), penWidth));
Chris@1367 1032 paint->drawPath(clipPath);
Chris@1367 1033 paint->restore();
Chris@1367 1034 }
Chris@1367 1035
Chris@1367 1036 if (!meanPath.isEmpty()) {
Chris@1367 1037 paint->save();
Chris@1367 1038 paint->setPen(QPen(midColour, penWidth));
Chris@1367 1039 paint->drawPath(meanPath);
Chris@1367 1040 paint->restore();
Chris@1367 1041 }
Chris@1367 1042
Chris@1367 1043 if (!individualSamplePoints.empty()) {
Chris@1367 1044 paint->save();
Chris@1367 1045 paint->setPen(QPen(baseColour, penWidth));
Chris@1367 1046 double sz = ViewManager::scalePixelSize(2.0);
Chris@1367 1047 for (QPointF p: individualSamplePoints) {
Chris@1367 1048 paint->drawRect(QRectF(p.x() - sz/2, p.y() - sz/2, sz, sz));
Chris@1367 1049 }
Chris@1367 1050 paint->restore();
Chris@1332 1051 }
Chris@1332 1052 }
Chris@1332 1053
Chris@1335 1054 void
Chris@1335 1055 WaveformLayer::paintChannelScaleGuides(LayerGeometryProvider *v,
Chris@1335 1056 QPainter *paint,
Chris@1335 1057 QRect rect,
Chris@1335 1058 int ch) const
Chris@1335 1059 {
Chris@1335 1060 int x0 = rect.left();
Chris@1335 1061 int x1 = rect.right();
Chris@1335 1062
Chris@1335 1063 int n = 10;
Chris@1335 1064 int py = -1;
Chris@1335 1065
Chris@1335 1066 double gain = m_effectiveGains[ch];
Chris@1335 1067
Chris@1335 1068 if (v->hasLightBackground() &&
Chris@1335 1069 v->getViewManager() &&
Chris@1335 1070 v->getViewManager()->shouldShowScaleGuides()) {
Chris@1335 1071
Chris@1335 1072 paint->setPen(QColor(240, 240, 240));
Chris@1335 1073
Chris@1335 1074 for (int i = 1; i < n; ++i) {
Chris@1335 1075
Chris@1335 1076 double val = 0.0, nval = 0.0;
Chris@1335 1077
Chris@1335 1078 switch (m_scale) {
Chris@1335 1079
Chris@1335 1080 case LinearScale:
Chris@1335 1081 val = (i * gain) / n;
Chris@1335 1082 if (i > 0) nval = -val;
Chris@1335 1083 break;
Chris@1335 1084
Chris@1335 1085 case MeterScale:
Chris@1335 1086 val = AudioLevel::dB_to_multiplier(meterdbs[i]) * gain;
Chris@1335 1087 break;
Chris@1335 1088
Chris@1335 1089 case dBScale:
Chris@1335 1090 val = AudioLevel::dB_to_multiplier(-(10*n) + i * 10) * gain;
Chris@1335 1091 break;
Chris@1335 1092 }
Chris@1335 1093
Chris@1335 1094 if (val < -1.0 || val > 1.0) continue;
Chris@1335 1095
Chris@1335 1096 int y = getYForValue(v, val, ch);
Chris@1335 1097
Chris@1335 1098 if (py >= 0 && abs(y - py) < 10) continue;
Chris@1335 1099 else py = y;
Chris@1335 1100
Chris@1335 1101 int ny = y;
Chris@1335 1102 if (nval != 0.0) {
Chris@1335 1103 ny = getYForValue(v, nval, ch);
Chris@1335 1104 }
Chris@1335 1105
Chris@1335 1106 paint->drawLine(x0, y, x1, y);
Chris@1335 1107 if (ny != y) {
Chris@1335 1108 paint->drawLine(x0, ny, x1, ny);
Chris@1335 1109 }
Chris@1335 1110 }
Chris@1335 1111 }
Chris@1335 1112 }
Chris@1335 1113
Chris@25 1114 QString
Chris@918 1115 WaveformLayer::getFeatureDescription(LayerGeometryProvider *v, QPoint &pos) const
Chris@25 1116 {
Chris@25 1117 int x = pos.x();
Chris@25 1118
Chris@25 1119 if (!m_model || !m_model->isOK()) return "";
Chris@25 1120
Chris@1325 1121 ZoomLevel zoomLevel = v->getZoomLevel();
Chris@25 1122
Chris@1325 1123 int desiredBlockSize = 1;
Chris@1325 1124 if (zoomLevel.zone == ZoomLevel::FramesPerPixel) {
Chris@1325 1125 desiredBlockSize = zoomLevel.level;
Chris@1325 1126 }
Chris@1325 1127
Chris@1325 1128 int blockSize = m_model->getSummaryBlockSize(desiredBlockSize);
Chris@365 1129
Chris@908 1130 sv_frame_t f0, f1;
Chris@1325 1131 if (!getSourceFramesForX(v, x, blockSize, f0, f1)) return "";
Chris@25 1132
Chris@25 1133 QString text;
Chris@25 1134
Chris@25 1135 RealTime rt0 = RealTime::frame2RealTime(f0, m_model->getSampleRate());
Chris@25 1136 RealTime rt1 = RealTime::frame2RealTime(f1, m_model->getSampleRate());
Chris@25 1137
Chris@25 1138 if (f1 != f0 + 1 && (rt0.sec != rt1.sec || rt0.msec() != rt1.msec())) {
Chris@1266 1139 text += tr("Time:\t%1 - %2")
Chris@1266 1140 .arg(rt0.toText(true).c_str())
Chris@1266 1141 .arg(rt1.toText(true).c_str());
Chris@25 1142 } else {
Chris@1266 1143 text += tr("Time:\t%1")
Chris@1266 1144 .arg(rt0.toText(true).c_str());
Chris@25 1145 }
Chris@25 1146
Chris@805 1147 int channels = 0, minChannel = 0, maxChannel = 0;
Chris@67 1148 bool mergingChannels = false, mixingChannels = false;
Chris@25 1149
Chris@67 1150 channels = getChannelArrangement(minChannel, maxChannel,
Chris@67 1151 mergingChannels, mixingChannels);
Chris@25 1152 if (channels == 0) return "";
Chris@25 1153
Chris@805 1154 for (int ch = minChannel; ch <= maxChannel; ++ch) {
Chris@25 1155
Chris@1266 1156 RangeSummarisableTimeValueModel::RangeBlock ranges;
Chris@302 1157 m_model->getSummaries(ch, f0, f1 - f0, ranges, blockSize);
Chris@25 1158
Chris@1266 1159 if (ranges.empty()) continue;
Chris@1266 1160
Chris@1266 1161 RangeSummarisableTimeValueModel::Range range = ranges[0];
Chris@1266 1162
Chris@1266 1163 QString label = tr("Level:");
Chris@1266 1164 if (minChannel != maxChannel) {
Chris@1266 1165 if (ch == 0) label = tr("Left:");
Chris@1266 1166 else if (ch == 1) label = tr("Right:");
Chris@1266 1167 else label = tr("Channel %1").arg(ch + 1);
Chris@1266 1168 }
Chris@25 1169
Chris@76 1170 bool singleValue = false;
Chris@908 1171 double min, max;
Chris@76 1172
Chris@386 1173 if (fabs(range.min()) < 0.01) {
Chris@386 1174 min = range.min();
Chris@386 1175 max = range.max();
Chris@76 1176 singleValue = (min == max);
Chris@76 1177 } else {
Chris@908 1178 int imin = int(lrint(range.min() * 10000));
Chris@908 1179 int imax = int(lrint(range.max() * 10000));
Chris@76 1180 singleValue = (imin == imax);
Chris@908 1181 min = double(imin)/10000;
Chris@908 1182 max = double(imax)/10000;
Chris@76 1183 }
Chris@76 1184
Chris@1266 1185 int db = int(AudioLevel::multiplier_to_dB(std::max(fabsf(range.min()),
Chris@1266 1186 fabsf(range.max())))
Chris@1266 1187 * 100);
Chris@25 1188
Chris@1266 1189 if (!singleValue) {
Chris@1266 1190 text += tr("\n%1\t%2 - %3 (%4 dB peak)")
Chris@1266 1191 .arg(label).arg(min).arg(max).arg(double(db)/100);
Chris@1266 1192 } else {
Chris@1266 1193 text += tr("\n%1\t%2 (%3 dB peak)")
Chris@1266 1194 .arg(label).arg(min).arg(double(db)/100);
Chris@1266 1195 }
Chris@25 1196 }
Chris@25 1197
Chris@25 1198 return text;
Chris@25 1199 }
Chris@25 1200
Chris@0 1201 int
Chris@918 1202 WaveformLayer::getYForValue(const LayerGeometryProvider *v, double value, int channel) const
Chris@68 1203 {
Chris@805 1204 int channels = 0, minChannel = 0, maxChannel = 0;
Chris@274 1205 bool mergingChannels = false, mixingChannels = false;
Chris@274 1206
Chris@274 1207 channels = getChannelArrangement(minChannel, maxChannel,
Chris@274 1208 mergingChannels, mixingChannels);
Chris@853 1209 if (channels == 0) return 0;
Chris@68 1210 if (maxChannel < minChannel || channel < minChannel) return 0;
Chris@68 1211
Chris@918 1212 int h = v->getPaintHeight();
Chris@68 1213 int m = (h / channels) / 2;
Chris@1266 1214
Chris@68 1215 if ((m_scale == dBScale || m_scale == MeterScale) &&
Chris@68 1216 m_channelMode != MergeChannels) {
Chris@68 1217 m = (h / channels);
Chris@68 1218 }
Chris@68 1219
Chris@274 1220 int my = m + (((channel - minChannel) * h) / channels);
Chris@274 1221
Chris@68 1222 int vy = 0;
Chris@68 1223
Chris@274 1224 switch (m_scale) {
Chris@68 1225
Chris@68 1226 case LinearScale:
Chris@68 1227 vy = int(m * value);
Chris@68 1228 break;
Chris@68 1229
Chris@68 1230 case MeterScale:
Chris@68 1231 vy = AudioLevel::multiplier_to_preview(value, m);
Chris@68 1232 break;
Chris@68 1233
Chris@68 1234 case dBScale:
Chris@1367 1235 vy = int(dBscale(value, m));
Chris@68 1236 break;
Chris@68 1237 }
Chris@68 1238
Chris@1338 1239 // SVCERR << "mergingChannels= " << mergingChannels << ", channel = " << channel << ", value = " << value << ", vy = " << vy << endl;
Chris@324 1240
Chris@68 1241 return my - vy;
Chris@68 1242 }
Chris@68 1243
Chris@908 1244 double
Chris@918 1245 WaveformLayer::getValueForY(const LayerGeometryProvider *v, int y, int &channel) const
Chris@261 1246 {
Chris@805 1247 int channels = 0, minChannel = 0, maxChannel = 0;
Chris@274 1248 bool mergingChannels = false, mixingChannels = false;
Chris@274 1249
Chris@274 1250 channels = getChannelArrangement(minChannel, maxChannel,
Chris@274 1251 mergingChannels, mixingChannels);
Chris@853 1252 if (channels == 0) return 0;
Chris@261 1253 if (maxChannel < minChannel) return 0;
Chris@261 1254
Chris@918 1255 int h = v->getPaintHeight();
Chris@261 1256 int m = (h / channels) / 2;
Chris@261 1257
Chris@261 1258 if ((m_scale == dBScale || m_scale == MeterScale) &&
Chris@261 1259 m_channelMode != MergeChannels) {
Chris@261 1260 m = (h / channels);
Chris@261 1261 }
Chris@274 1262
Chris@274 1263 channel = (y * channels) / h + minChannel;
Chris@261 1264
Chris@261 1265 int my = m + (((channel - minChannel) * h) / channels);
Chris@261 1266
Chris@262 1267 int vy = my - y;
Chris@908 1268 double value = 0;
Chris@908 1269 double thresh = -50.f;
Chris@261 1270
Chris@274 1271 switch (m_scale) {
Chris@261 1272
Chris@261 1273 case LinearScale:
Chris@908 1274 value = double(vy) / m;
Chris@261 1275 break;
Chris@261 1276
Chris@261 1277 case MeterScale:
Chris@261 1278 value = AudioLevel::preview_to_multiplier(vy, m);
Chris@261 1279 break;
Chris@261 1280
Chris@261 1281 case dBScale:
Chris@908 1282 value = (-thresh * double(vy)) / m + thresh;
Chris@274 1283 value = AudioLevel::dB_to_multiplier(value);
Chris@261 1284 break;
Chris@261 1285 }
Chris@261 1286
Chris@274 1287 return value / m_gain;
Chris@261 1288 }
Chris@261 1289
Chris@261 1290 bool
Chris@918 1291 WaveformLayer::getYScaleValue(const LayerGeometryProvider *v, int y,
Chris@908 1292 double &value, QString &unit) const
Chris@261 1293 {
Chris@805 1294 int channel;
Chris@261 1295
Chris@274 1296 value = getValueForY(v, y, channel);
Chris@261 1297
Chris@274 1298 if (m_scale == dBScale || m_scale == MeterScale) {
Chris@261 1299
Chris@908 1300 double thresh = -50.f;
Chris@274 1301
Chris@908 1302 if (value > 0.0) {
Chris@908 1303 value = 10.0 * log10(value);
Chris@274 1304 if (value < thresh) value = thresh;
Chris@274 1305 } else value = thresh;
Chris@274 1306
Chris@274 1307 unit = "dBV";
Chris@274 1308
Chris@274 1309 } else {
Chris@274 1310 unit = "V";
Chris@274 1311 }
Chris@274 1312
Chris@274 1313 return true;
Chris@274 1314 }
Chris@274 1315
Chris@274 1316 bool
Chris@918 1317 WaveformLayer::getYScaleDifference(const LayerGeometryProvider *v, int y0, int y1,
Chris@908 1318 double &diff, QString &unit) const
Chris@274 1319 {
Chris@805 1320 int c0, c1;
Chris@908 1321 double v0 = getValueForY(v, y0, c0);
Chris@908 1322 double v1 = getValueForY(v, y1, c1);
Chris@274 1323
Chris@274 1324 if (c0 != c1) {
Chris@274 1325 // different channels, not comparable
Chris@908 1326 diff = 0.0;
Chris@274 1327 unit = "";
Chris@274 1328 return false;
Chris@274 1329 }
Chris@274 1330
Chris@274 1331 if (m_scale == dBScale || m_scale == MeterScale) {
Chris@274 1332
Chris@908 1333 double thresh = -50.0;
Chris@274 1334
Chris@274 1335 if (v1 == v0) diff = thresh;
Chris@274 1336 else {
Chris@274 1337 if (v1 > v0) diff = v0 / v1;
Chris@274 1338 else diff = v1 / v0;
Chris@274 1339
Chris@908 1340 diff = 10.0 * log10(diff);
Chris@274 1341 if (diff < thresh) diff = thresh;
Chris@274 1342 }
Chris@274 1343
Chris@274 1344 unit = "dBV";
Chris@274 1345
Chris@274 1346 } else {
Chris@908 1347 diff = fabs(v1 - v0);
Chris@274 1348 unit = "V";
Chris@274 1349 }
Chris@274 1350
Chris@261 1351 return true;
Chris@261 1352 }
Chris@261 1353
Chris@68 1354 int
Chris@918 1355 WaveformLayer::getVerticalScaleWidth(LayerGeometryProvider *, bool, QPainter &paint) const
Chris@0 1356 {
Chris@0 1357 if (m_scale == LinearScale) {
Chris@1266 1358 return paint.fontMetrics().width("0.0") + 13;
Chris@0 1359 } else {
Chris@1266 1360 return std::max(paint.fontMetrics().width(tr("0dB")),
Chris@1266 1361 paint.fontMetrics().width(Strings::minus_infinity)) + 13;
Chris@0 1362 }
Chris@0 1363 }
Chris@0 1364
Chris@0 1365 void
Chris@918 1366 WaveformLayer::paintVerticalScale(LayerGeometryProvider *v, bool, QPainter &paint, QRect rect) const
Chris@0 1367 {
Chris@0 1368 if (!m_model || !m_model->isOK()) {
Chris@1266 1369 return;
Chris@0 1370 }
Chris@0 1371
Chris@805 1372 int channels = 0, minChannel = 0, maxChannel = 0;
Chris@67 1373 bool mergingChannels = false, mixingChannels = false;
Chris@0 1374
Chris@67 1375 channels = getChannelArrangement(minChannel, maxChannel,
Chris@67 1376 mergingChannels, mixingChannels);
Chris@0 1377 if (channels == 0) return;
Chris@0 1378
Chris@0 1379 int h = rect.height(), w = rect.width();
Chris@0 1380 int textHeight = paint.fontMetrics().height();
Chris@0 1381 int toff = -textHeight/2 + paint.fontMetrics().ascent() + 1;
Chris@0 1382
Chris@908 1383 double gain = m_gain;
Chris@67 1384
Chris@805 1385 for (int ch = minChannel; ch <= maxChannel; ++ch) {
Chris@0 1386
Chris@1266 1387 int lastLabelledY = -1;
Chris@0 1388
Chris@805 1389 if (ch < (int)m_effectiveGains.size()) gain = m_effectiveGains[ch];
Chris@67 1390
Chris@68 1391 int n = 10;
Chris@0 1392
Chris@1266 1393 for (int i = 0; i <= n; ++i) {
Chris@68 1394
Chris@908 1395 double val = 0.0, nval = 0.0;
Chris@1266 1396 QString text = "";
Chris@0 1397
Chris@68 1398 switch (m_scale) {
Chris@68 1399
Chris@68 1400 case LinearScale:
Chris@68 1401 val = (i * gain) / n;
Chris@1266 1402 text = QString("%1").arg(double(i) / n);
Chris@1266 1403 if (i == 0) text = "0.0";
Chris@68 1404 else {
Chris@68 1405 nval = -val;
Chris@68 1406 if (i == n) text = "1.0";
Chris@68 1407 }
Chris@68 1408 break;
Chris@0 1409
Chris@68 1410 case MeterScale:
Chris@68 1411 val = AudioLevel::dB_to_multiplier(meterdbs[i]) * gain;
Chris@1266 1412 text = QString("%1").arg(meterdbs[i]);
Chris@1266 1413 if (i == n) text = tr("0dB");
Chris@1266 1414 if (i == 0) {
Chris@1147 1415 text = Strings::minus_infinity;
Chris@68 1416 val = 0.0;
Chris@1266 1417 }
Chris@68 1418 break;
Chris@0 1419
Chris@68 1420 case dBScale:
Chris@68 1421 val = AudioLevel::dB_to_multiplier(-(10*n) + i * 10) * gain;
Chris@1266 1422 text = QString("%1").arg(-(10*n) + i * 10);
Chris@1266 1423 if (i == n) text = tr("0dB");
Chris@1266 1424 if (i == 0) {
Chris@1147 1425 text = Strings::minus_infinity;
Chris@68 1426 val = 0.0;
Chris@1266 1427 }
Chris@68 1428 break;
Chris@68 1429 }
Chris@0 1430
Chris@68 1431 if (val < -1.0 || val > 1.0) continue;
Chris@0 1432
Chris@274 1433 int y = getYForValue(v, val, ch);
Chris@0 1434
Chris@68 1435 int ny = y;
Chris@68 1436 if (nval != 0.0) {
Chris@274 1437 ny = getYForValue(v, nval, ch);
Chris@68 1438 }
Chris@0 1439
Chris@68 1440 bool spaceForLabel = (i == 0 ||
Chris@68 1441 abs(y - lastLabelledY) >= textHeight - 1);
Chris@0 1442
Chris@68 1443 if (spaceForLabel) {
Chris@0 1444
Chris@68 1445 int tx = 3;
Chris@68 1446 if (m_scale != LinearScale) {
Chris@68 1447 tx = w - 10 - paint.fontMetrics().width(text);
Chris@68 1448 }
Chris@68 1449
Chris@68 1450 int ty = y;
Chris@68 1451 if (ty < paint.fontMetrics().ascent()) {
Chris@68 1452 ty = paint.fontMetrics().ascent();
Chris@68 1453 } else if (ty > h - paint.fontMetrics().descent()) {
Chris@68 1454 ty = h - paint.fontMetrics().descent();
Chris@68 1455 } else {
Chris@68 1456 ty += toff;
Chris@68 1457 }
Chris@68 1458 paint.drawText(tx, ty, text);
Chris@0 1459
Chris@68 1460 lastLabelledY = ty - toff;
Chris@67 1461
Chris@68 1462 if (ny != y) {
Chris@68 1463 ty = ny;
Chris@68 1464 if (ty < paint.fontMetrics().ascent()) {
Chris@68 1465 ty = paint.fontMetrics().ascent();
Chris@68 1466 } else if (ty > h - paint.fontMetrics().descent()) {
Chris@68 1467 ty = h - paint.fontMetrics().descent();
Chris@68 1468 } else {
Chris@68 1469 ty += toff;
Chris@68 1470 }
Chris@68 1471 paint.drawText(tx, ty, text);
Chris@68 1472 }
Chris@68 1473
Chris@68 1474 paint.drawLine(w - 7, y, w, y);
Chris@68 1475 if (ny != y) paint.drawLine(w - 7, ny, w, ny);
Chris@68 1476
Chris@68 1477 } else {
Chris@68 1478
Chris@68 1479 paint.drawLine(w - 4, y, w, y);
Chris@68 1480 if (ny != y) paint.drawLine(w - 4, ny, w, ny);
Chris@68 1481 }
Chris@1266 1482 }
Chris@0 1483 }
Chris@0 1484 }
Chris@0 1485
Chris@316 1486 void
Chris@316 1487 WaveformLayer::toXml(QTextStream &stream,
Chris@316 1488 QString indent, QString extraAttributes) const
Chris@6 1489 {
Chris@6 1490 QString s;
Chris@6 1491
Chris@285 1492 QString colourName, colourSpec, darkbg;
Chris@285 1493 ColourDatabase::getInstance()->getStringValues
Chris@285 1494 (m_colour, colourName, colourSpec, darkbg);
Chris@285 1495
Chris@6 1496 s += QString("gain=\"%1\" "
Chris@1266 1497 "showMeans=\"%2\" "
Chris@1266 1498 "greyscale=\"%3\" "
Chris@1266 1499 "channelMode=\"%4\" "
Chris@1266 1500 "channel=\"%5\" "
Chris@287 1501 "scale=\"%6\" "
Chris@709 1502 "middleLineHeight=\"%7\" "
Chris@1266 1503 "aggressive=\"%8\" "
Chris@709 1504 "autoNormalize=\"%9\"")
Chris@1266 1505 .arg(m_gain)
Chris@1266 1506 .arg(m_showMeans)
Chris@1367 1507 .arg(true) // Option removed, but effectively always on, so
Chris@1367 1508 // retained in the session file for compatibility
Chris@1266 1509 .arg(m_channelMode)
Chris@1266 1510 .arg(m_channel)
Chris@1266 1511 .arg(m_scale)
Chris@709 1512 .arg(m_middleLineHeight)
Chris@1266 1513 .arg(m_aggressive)
Chris@67 1514 .arg(m_autoNormalize);
Chris@6 1515
Chris@316 1516 SingleColourLayer::toXml(stream, indent, extraAttributes + " " + s);
Chris@6 1517 }
Chris@6 1518
Chris@11 1519 void
Chris@11 1520 WaveformLayer::setProperties(const QXmlAttributes &attributes)
Chris@11 1521 {
Chris@11 1522 bool ok = false;
Chris@11 1523
Chris@287 1524 SingleColourLayer::setProperties(attributes);
Chris@287 1525
Chris@11 1526 float gain = attributes.value("gain").toFloat(&ok);
Chris@11 1527 if (ok) setGain(gain);
Chris@11 1528
Chris@11 1529 bool showMeans = (attributes.value("showMeans") == "1" ||
Chris@1266 1530 attributes.value("showMeans") == "true");
Chris@11 1531 setShowMeans(showMeans);
Chris@11 1532
Chris@11 1533 ChannelMode channelMode = (ChannelMode)
Chris@1266 1534 attributes.value("channelMode").toInt(&ok);
Chris@11 1535 if (ok) setChannelMode(channelMode);
Chris@11 1536
Chris@11 1537 int channel = attributes.value("channel").toInt(&ok);
Chris@11 1538 if (ok) setChannel(channel);
Chris@11 1539
Chris@709 1540 Scale scale = (Scale)attributes.value("scale").toInt(&ok);
Chris@11 1541 if (ok) setScale(scale);
Chris@11 1542
Chris@709 1543 float middleLineHeight = attributes.value("middleLineHeight").toFloat(&ok);
Chris@709 1544 if (ok) setMiddleLineHeight(middleLineHeight);
Chris@709 1545
Chris@11 1546 bool aggressive = (attributes.value("aggressive") == "1" ||
Chris@1266 1547 attributes.value("aggressive") == "true");
Chris@1367 1548 setAggressiveCacheing(aggressive);
Chris@67 1549
Chris@67 1550 bool autoNormalize = (attributes.value("autoNormalize") == "1" ||
Chris@67 1551 attributes.value("autoNormalize") == "true");
Chris@67 1552 setAutoNormalize(autoNormalize);
Chris@11 1553 }
Chris@11 1554
Chris@133 1555 int
Chris@133 1556 WaveformLayer::getVerticalZoomSteps(int &defaultStep) const
Chris@133 1557 {
Chris@133 1558 defaultStep = 50;
Chris@133 1559 return 100;
Chris@133 1560 }
Chris@0 1561
Chris@133 1562 int
Chris@133 1563 WaveformLayer::getCurrentVerticalZoomStep() const
Chris@133 1564 {
Chris@908 1565 int val = int(lrint(log10(m_gain) * 20.0) + 50);
Chris@133 1566 if (val < 0) val = 0;
Chris@133 1567 if (val > 100) val = 100;
Chris@133 1568 return val;
Chris@133 1569 }
Chris@133 1570
Chris@133 1571 void
Chris@133 1572 WaveformLayer::setVerticalZoomStep(int step)
Chris@133 1573 {
Chris@908 1574 setGain(powf(10, float(step - 50) / 20.f));
Chris@133 1575 }
Chris@133 1576