annotate layer/WaveformLayer.cpp @ 843:db0759e4c6c3

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