annotate layer/WaveformLayer.cpp @ 1542:10fe8124dc17

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