annotate layer/WaveformLayer.cpp @ 1335:bc44b520405f zoom

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