annotate layer/WaveformLayer.cpp @ 1338:6d091348e4e9 zoom

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