annotate layer/WaveformLayer.cpp @ 1471:f2525e6cbdf1 by-id

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