annotate layer/WaveformLayer.cpp @ 67:c4fff27cd651

* Add auto-normalize option to waveform layer * Various fixes to display of dB/metered levels in waveform layer. Still need to fix to ensure they don't waste half the display * Add mix channels option to waveform layer * Use multiple transforms menus, one per transform type -- not sure about this * Give centroid plugin two outputs, for log and linear frequency weightings * Show scale units from plugin in time-value display
author Chris Cannam
date Wed, 29 Mar 2006 12:35:17 +0000
parents 705f05ab42e3
children 193b569a975f
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@59 7 This file copyright 2006 Chris Cannam.
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@0 19 #include "base/View.h"
Chris@0 20 #include "base/Profiler.h"
Chris@0 21
Chris@0 22 #include <QPainter>
Chris@0 23 #include <QPixmap>
Chris@0 24
Chris@0 25 #include <iostream>
Chris@0 26 #include <cmath>
Chris@0 27
Chris@4 28 //#define DEBUG_WAVEFORM_PAINT 1
Chris@4 29
Chris@0 30 using std::cerr;
Chris@0 31 using std::endl;
Chris@0 32
Chris@44 33 WaveformLayer::WaveformLayer() :
Chris@44 34 Layer(),
Chris@0 35 m_model(0),
Chris@0 36 m_gain(1.0f),
Chris@67 37 m_autoNormalize(false),
Chris@0 38 m_colour(Qt::black),
Chris@0 39 m_showMeans(true),
Chris@0 40 m_greyscale(true),
Chris@0 41 m_channelMode(SeparateChannels),
Chris@0 42 m_channel(-1),
Chris@0 43 m_scale(LinearScale),
Chris@0 44 m_aggressive(false),
Chris@0 45 m_cache(0),
Chris@0 46 m_cacheValid(false)
Chris@0 47 {
Chris@44 48
Chris@0 49 }
Chris@0 50
Chris@0 51 WaveformLayer::~WaveformLayer()
Chris@0 52 {
Chris@0 53 delete m_cache;
Chris@0 54 }
Chris@0 55
Chris@0 56 void
Chris@0 57 WaveformLayer::setModel(const RangeSummarisableTimeValueModel *model)
Chris@0 58 {
Chris@0 59 m_model = model;
Chris@0 60 m_cacheValid = false;
Chris@0 61 if (!m_model || !m_model->isOK()) return;
Chris@0 62
Chris@0 63 connect(m_model, SIGNAL(modelChanged()), this, SIGNAL(modelChanged()));
Chris@0 64 connect(m_model, SIGNAL(modelChanged(size_t, size_t)),
Chris@0 65 this, SIGNAL(modelChanged(size_t, size_t)));
Chris@0 66
Chris@0 67 connect(m_model, SIGNAL(completionChanged()),
Chris@0 68 this, SIGNAL(modelCompletionChanged()));
Chris@0 69
Chris@0 70 emit modelReplaced();
Chris@0 71 }
Chris@0 72
Chris@0 73 Layer::PropertyList
Chris@0 74 WaveformLayer::getProperties() const
Chris@0 75 {
Chris@0 76 PropertyList list;
Chris@0 77 list.push_back(tr("Colour"));
Chris@0 78 list.push_back(tr("Scale"));
Chris@0 79 list.push_back(tr("Gain"));
Chris@67 80 list.push_back(tr("Normalize Visible Area"));
Chris@67 81 list.push_back(tr("Channels"));
Chris@0 82 return list;
Chris@0 83 }
Chris@0 84
Chris@0 85 Layer::PropertyType
Chris@0 86 WaveformLayer::getPropertyType(const PropertyName &name) const
Chris@0 87 {
Chris@0 88 if (name == tr("Gain")) return RangeProperty;
Chris@67 89 if (name == tr("Normalize Visible Area")) return ToggleProperty;
Chris@0 90 if (name == tr("Colour")) return ValueProperty;
Chris@67 91 if (name == tr("Channels")) return ValueProperty;
Chris@0 92 if (name == tr("Scale")) return ValueProperty;
Chris@0 93 return InvalidProperty;
Chris@0 94 }
Chris@0 95
Chris@0 96 QString
Chris@0 97 WaveformLayer::getPropertyGroupName(const PropertyName &name) const
Chris@0 98 {
Chris@0 99 if (name == tr("Gain") ||
Chris@67 100 name == tr("Normalize Visible Area") ||
Chris@0 101 name == tr("Scale")) return tr("Scale");
Chris@0 102 return QString();
Chris@0 103 }
Chris@0 104
Chris@0 105 int
Chris@0 106 WaveformLayer::getPropertyRangeAndValue(const PropertyName &name,
Chris@67 107 int *min, int *max) const
Chris@0 108 {
Chris@0 109 int deft = 0;
Chris@0 110
Chris@56 111 int garbage0, garbage1;
Chris@56 112 if (!min) min = &garbage0;
Chris@56 113 if (!max) max = &garbage1;
Chris@10 114
Chris@0 115 if (name == tr("Gain")) {
Chris@0 116
Chris@0 117 *min = -50;
Chris@0 118 *max = 50;
Chris@0 119
Chris@34 120 deft = lrint(log10(m_gain) * 20.0);
Chris@0 121 if (deft < *min) deft = *min;
Chris@0 122 if (deft > *max) deft = *max;
Chris@0 123
Chris@67 124 } else if (name == tr("Normalize Visible Area")) {
Chris@67 125
Chris@67 126 deft = (m_autoNormalize ? 1 : 0);
Chris@67 127
Chris@0 128 } else if (name == tr("Colour")) {
Chris@0 129
Chris@0 130 *min = 0;
Chris@0 131 *max = 5;
Chris@0 132
Chris@0 133 if (m_colour == Qt::black) deft = 0;
Chris@0 134 else if (m_colour == Qt::darkRed) deft = 1;
Chris@0 135 else if (m_colour == Qt::darkBlue) deft = 2;
Chris@0 136 else if (m_colour == Qt::darkGreen) deft = 3;
Chris@0 137 else if (m_colour == QColor(200, 50, 255)) deft = 4;
Chris@0 138 else if (m_colour == QColor(255, 150, 50)) deft = 5;
Chris@0 139
Chris@67 140 } else if (name == tr("Channels")) {
Chris@0 141
Chris@67 142 *min = 0;
Chris@67 143 *max = 2;
Chris@67 144 if (m_channelMode == MixChannels) deft = 1;
Chris@67 145 else if (m_channelMode == MergeChannels) deft = 2;
Chris@67 146 else deft = 0;
Chris@0 147
Chris@0 148 } else if (name == tr("Scale")) {
Chris@0 149
Chris@0 150 *min = 0;
Chris@0 151 *max = 2;
Chris@0 152
Chris@0 153 deft = (int)m_scale;
Chris@0 154
Chris@0 155 } else {
Chris@0 156 deft = Layer::getPropertyRangeAndValue(name, min, max);
Chris@0 157 }
Chris@0 158
Chris@0 159 return deft;
Chris@0 160 }
Chris@0 161
Chris@0 162 QString
Chris@0 163 WaveformLayer::getPropertyValueLabel(const PropertyName &name,
Chris@0 164 int value) const
Chris@0 165 {
Chris@0 166 if (name == tr("Colour")) {
Chris@0 167 switch (value) {
Chris@0 168 default:
Chris@0 169 case 0: return tr("Black");
Chris@0 170 case 1: return tr("Red");
Chris@0 171 case 2: return tr("Blue");
Chris@0 172 case 3: return tr("Green");
Chris@0 173 case 4: return tr("Purple");
Chris@0 174 case 5: return tr("Orange");
Chris@0 175 }
Chris@0 176 }
Chris@0 177 if (name == tr("Scale")) {
Chris@0 178 switch (value) {
Chris@0 179 default:
Chris@0 180 case 0: return tr("Linear");
Chris@0 181 case 1: return tr("Meter");
Chris@0 182 case 2: return tr("dB");
Chris@0 183 }
Chris@0 184 }
Chris@67 185 if (name == tr("Channels")) {
Chris@67 186 switch (value) {
Chris@67 187 default:
Chris@67 188 case 0: return tr("Separate");
Chris@67 189 case 1: return tr("Mean");
Chris@67 190 case 2: return tr("Butterfly");
Chris@67 191 }
Chris@67 192 }
Chris@0 193 return tr("<unknown>");
Chris@0 194 }
Chris@0 195
Chris@0 196 void
Chris@0 197 WaveformLayer::setProperty(const PropertyName &name, int value)
Chris@0 198 {
Chris@0 199 if (name == tr("Gain")) {
Chris@0 200 setGain(pow(10, float(value)/20.0));
Chris@67 201 } else if (name == tr("Normalize Visible Area")) {
Chris@67 202 setAutoNormalize(value ? true : false);
Chris@0 203 } else if (name == tr("Colour")) {
Chris@0 204 switch (value) {
Chris@0 205 default:
Chris@0 206 case 0: setBaseColour(Qt::black); break;
Chris@0 207 case 1: setBaseColour(Qt::darkRed); break;
Chris@0 208 case 2: setBaseColour(Qt::darkBlue); break;
Chris@0 209 case 3: setBaseColour(Qt::darkGreen); break;
Chris@0 210 case 4: setBaseColour(QColor(200, 50, 255)); break;
Chris@0 211 case 5: setBaseColour(QColor(255, 150, 50)); break;
Chris@0 212 }
Chris@67 213 } else if (name == tr("Channels")) {
Chris@67 214 if (value == 1) setChannelMode(MixChannels);
Chris@67 215 else if (value == 2) setChannelMode(MergeChannels);
Chris@67 216 else setChannelMode(SeparateChannels);
Chris@0 217 } else if (name == tr("Scale")) {
Chris@0 218 switch (value) {
Chris@0 219 default:
Chris@0 220 case 0: setScale(LinearScale); break;
Chris@0 221 case 1: setScale(MeterScale); break;
Chris@0 222 case 2: setScale(dBScale); break;
Chris@0 223 }
Chris@0 224 }
Chris@0 225 }
Chris@0 226
Chris@0 227 void
Chris@67 228 WaveformLayer::setGain(float gain)
Chris@0 229 {
Chris@0 230 if (m_gain == gain) return;
Chris@0 231 m_gain = gain;
Chris@0 232 m_cacheValid = false;
Chris@0 233 emit layerParametersChanged();
Chris@0 234 }
Chris@0 235
Chris@0 236 void
Chris@67 237 WaveformLayer::setAutoNormalize(bool autoNormalize)
Chris@67 238 {
Chris@67 239 if (m_autoNormalize == autoNormalize) return;
Chris@67 240 m_autoNormalize = autoNormalize;
Chris@67 241 m_cacheValid = false;
Chris@67 242 emit layerParametersChanged();
Chris@67 243 }
Chris@67 244
Chris@67 245 void
Chris@0 246 WaveformLayer::setBaseColour(QColor colour)
Chris@0 247 {
Chris@0 248 if (m_colour == colour) return;
Chris@0 249 m_colour = colour;
Chris@0 250 m_cacheValid = false;
Chris@0 251 emit layerParametersChanged();
Chris@0 252 }
Chris@0 253
Chris@0 254 void
Chris@0 255 WaveformLayer::setShowMeans(bool showMeans)
Chris@0 256 {
Chris@0 257 if (m_showMeans == showMeans) return;
Chris@0 258 m_showMeans = showMeans;
Chris@0 259 m_cacheValid = false;
Chris@0 260 emit layerParametersChanged();
Chris@0 261 }
Chris@0 262
Chris@0 263 void
Chris@0 264 WaveformLayer::setUseGreyscale(bool useGreyscale)
Chris@0 265 {
Chris@0 266 if (m_greyscale == useGreyscale) return;
Chris@0 267 m_greyscale = useGreyscale;
Chris@0 268 m_cacheValid = false;
Chris@0 269 emit layerParametersChanged();
Chris@0 270 }
Chris@0 271
Chris@0 272 void
Chris@0 273 WaveformLayer::setChannelMode(ChannelMode channelMode)
Chris@0 274 {
Chris@0 275 if (m_channelMode == channelMode) return;
Chris@0 276 m_channelMode = channelMode;
Chris@0 277 m_cacheValid = false;
Chris@0 278 emit layerParametersChanged();
Chris@0 279 }
Chris@0 280
Chris@0 281 void
Chris@0 282 WaveformLayer::setChannel(int channel)
Chris@0 283 {
Chris@0 284 std::cerr << "WaveformLayer::setChannel(" << channel << ")" << std::endl;
Chris@0 285
Chris@0 286 if (m_channel == channel) return;
Chris@0 287 m_channel = channel;
Chris@0 288 m_cacheValid = false;
Chris@0 289 emit layerParametersChanged();
Chris@0 290 }
Chris@0 291
Chris@0 292 void
Chris@0 293 WaveformLayer::setScale(Scale scale)
Chris@0 294 {
Chris@0 295 if (m_scale == scale) return;
Chris@0 296 m_scale = scale;
Chris@0 297 m_cacheValid = false;
Chris@0 298 emit layerParametersChanged();
Chris@0 299 }
Chris@0 300
Chris@0 301 void
Chris@0 302 WaveformLayer::setAggressiveCacheing(bool aggressive)
Chris@0 303 {
Chris@0 304 if (m_aggressive == aggressive) return;
Chris@0 305 m_aggressive = aggressive;
Chris@0 306 m_cacheValid = false;
Chris@0 307 emit layerParametersChanged();
Chris@0 308 }
Chris@0 309
Chris@0 310 int
Chris@0 311 WaveformLayer::getCompletion() const
Chris@0 312 {
Chris@0 313 int completion = 100;
Chris@0 314 if (!m_model || !m_model->isOK()) return completion;
Chris@0 315 if (m_model->isReady(&completion)) return 100;
Chris@0 316 return completion;
Chris@0 317 }
Chris@0 318
Chris@0 319 int
Chris@0 320 WaveformLayer::dBscale(float sample, int m) const
Chris@0 321 {
Chris@67 322 //!!! if (sample < 0.0) return -dBscale(-sample, m);
Chris@67 323 if (sample < 0.0) return dBscale(-sample, m);
Chris@0 324 float dB = AudioLevel::multiplier_to_dB(sample);
Chris@0 325 if (dB < -50.0) return 0;
Chris@0 326 if (dB > 0.0) return m;
Chris@0 327 return int(((dB + 50.0) * m) / 50.0 + 0.1);
Chris@0 328 }
Chris@0 329
Chris@0 330 size_t
Chris@67 331 WaveformLayer::getChannelArrangement(size_t &min, size_t &max,
Chris@67 332 bool &merging, bool &mixing)
Chris@0 333 const
Chris@0 334 {
Chris@0 335 if (!m_model || !m_model->isOK()) return 0;
Chris@0 336
Chris@0 337 size_t channels = m_model->getChannelCount();
Chris@0 338 if (channels == 0) return 0;
Chris@0 339
Chris@0 340 size_t rawChannels = channels;
Chris@0 341
Chris@0 342 if (m_channel == -1) {
Chris@0 343 min = 0;
Chris@67 344 if (m_channelMode == MergeChannels ||
Chris@67 345 m_channelMode == MixChannels) {
Chris@0 346 max = 0;
Chris@0 347 channels = 1;
Chris@0 348 } else {
Chris@0 349 max = channels - 1;
Chris@0 350 }
Chris@0 351 } else {
Chris@0 352 min = m_channel;
Chris@0 353 max = m_channel;
Chris@0 354 rawChannels = 1;
Chris@0 355 channels = 1;
Chris@0 356 }
Chris@0 357
Chris@0 358 merging = (m_channelMode == MergeChannels && rawChannels > 1);
Chris@67 359 mixing = (m_channelMode == MixChannels && rawChannels > 1);
Chris@0 360
Chris@0 361 // std::cerr << "WaveformLayer::getChannelArrangement: min " << min << ", max " << max << ", merging " << merging << ", channels " << channels << std::endl;
Chris@0 362
Chris@0 363 return channels;
Chris@0 364 }
Chris@0 365
Chris@67 366 bool
Chris@67 367 WaveformLayer::isLayerScrollable(const View *) const
Chris@67 368 {
Chris@67 369 return !m_autoNormalize;
Chris@67 370 }
Chris@67 371
Chris@0 372 void
Chris@44 373 WaveformLayer::paint(View *v, QPainter &viewPainter, QRect rect) const
Chris@0 374 {
Chris@0 375 if (!m_model || !m_model->isOK()) {
Chris@0 376 return;
Chris@0 377 }
Chris@0 378
Chris@44 379 long startFrame = v->getStartFrame();
Chris@44 380 int zoomLevel = v->getZoomLevel();
Chris@0 381
Chris@2 382 #ifdef DEBUG_WAVEFORM_PAINT
Chris@0 383 Profiler profiler("WaveformLayer::paint", true);
Chris@0 384 std::cerr << "WaveformLayer::paint (" << rect.x() << "," << rect.y()
Chris@0 385 << ") [" << rect.width() << "x" << rect.height() << "]: zoom " << zoomLevel << ", start " << startFrame << std::endl;
Chris@2 386 #endif
Chris@0 387
Chris@0 388 size_t channels = 0, minChannel = 0, maxChannel = 0;
Chris@67 389 bool mergingChannels = false, mixingChannels = false;
Chris@0 390
Chris@67 391 channels = getChannelArrangement(minChannel, maxChannel,
Chris@67 392 mergingChannels, mixingChannels);
Chris@0 393 if (channels == 0) return;
Chris@0 394
Chris@44 395 int w = v->width();
Chris@44 396 int h = v->height();
Chris@0 397
Chris@0 398 bool ready = m_model->isReady();
Chris@0 399 QPainter *paint;
Chris@0 400
Chris@0 401 if (m_aggressive) {
Chris@0 402
Chris@0 403 if (m_cacheValid && (zoomLevel != m_cacheZoomLevel)) {
Chris@0 404 m_cacheValid = false;
Chris@0 405 }
Chris@0 406
Chris@0 407 if (m_cacheValid) {
Chris@0 408 viewPainter.drawPixmap(rect, *m_cache, rect);
Chris@0 409 return;
Chris@0 410 }
Chris@0 411
Chris@0 412 if (!m_cache || m_cache->width() != w || m_cache->height() != h) {
Chris@0 413 delete m_cache;
Chris@0 414 m_cache = new QPixmap(w, h);
Chris@0 415 }
Chris@0 416
Chris@0 417 paint = new QPainter(m_cache);
Chris@0 418
Chris@0 419 paint->setPen(Qt::NoPen);
Chris@44 420 paint->setBrush(v->palette().background());
Chris@0 421 paint->drawRect(rect);
Chris@0 422
Chris@0 423 paint->setPen(Qt::black);
Chris@0 424 paint->setBrush(Qt::NoBrush);
Chris@0 425
Chris@0 426 } else {
Chris@0 427 paint = &viewPainter;
Chris@0 428 }
Chris@0 429
Chris@28 430 paint->setRenderHint(QPainter::Antialiasing, false);
Chris@28 431
Chris@0 432 int x0 = 0, x1 = w - 1;
Chris@0 433 int y0 = 0, y1 = h - 1;
Chris@0 434
Chris@0 435 x0 = rect.left();
Chris@0 436 x1 = rect.right();
Chris@0 437 y0 = rect.top();
Chris@0 438 y1 = rect.bottom();
Chris@0 439
Chris@28 440 if (x0 > 0) --x0;
Chris@44 441 if (x1 < v->width()) ++x1;
Chris@28 442
Chris@44 443 long frame0 = v->getFrameForX(x0);
Chris@44 444 long frame1 = v->getFrameForX(x1 + 1);
Chris@0 445
Chris@4 446 #ifdef DEBUG_WAVEFORM_PAINT
Chris@4 447 std::cerr << "Painting waveform from " << frame0 << " to " << frame1 << " (" << (x1-x0+1) << " pixels at zoom " << zoomLevel << ")" << std::endl;
Chris@4 448 #endif
Chris@0 449
Chris@0 450 RangeSummarisableTimeValueModel::RangeBlock ranges;
Chris@0 451 RangeSummarisableTimeValueModel::RangeBlock otherChannelRanges;
Chris@0 452 RangeSummarisableTimeValueModel::Range range;
Chris@0 453
Chris@0 454 QColor greys[3];
Chris@0 455 if (m_colour == Qt::black) {
Chris@27 456 for (int i = 0; i < 3; ++i) { // 0 lightest, 2 darkest
Chris@0 457 int level = 192 - 64 * i;
Chris@0 458 greys[i] = QColor(level, level, level);
Chris@0 459 }
Chris@0 460 } else {
Chris@44 461 int hue, sat, val;
Chris@44 462 m_colour.getHsv(&hue, &sat, &val);
Chris@27 463 for (int i = 0; i < 3; ++i) { // 0 lightest, 2 darkest
Chris@44 464 if (v->hasLightBackground()) {
Chris@44 465 greys[i] = QColor::fromHsv(hue, sat * (i + 1) / 4, val);
Chris@27 466 } else {
Chris@44 467 greys[i] = QColor::fromHsv(hue, sat * (3 - i) / 4, val);
Chris@27 468 }
Chris@27 469 }
Chris@0 470 }
Chris@0 471
Chris@0 472 QColor midColour = m_colour;
Chris@0 473 if (midColour == Qt::black) {
Chris@0 474 midColour = Qt::gray;
Chris@44 475 } else if (v->hasLightBackground()) {
Chris@0 476 midColour = midColour.light(150);
Chris@0 477 } else {
Chris@0 478 midColour = midColour.light(50);
Chris@0 479 }
Chris@0 480
Chris@67 481 while (m_effectiveGains.size() <= maxChannel) {
Chris@67 482 m_effectiveGains.push_back(m_gain);
Chris@67 483 }
Chris@67 484
Chris@0 485 for (size_t ch = minChannel; ch <= maxChannel; ++ch) {
Chris@0 486
Chris@0 487 int prevRangeBottom = -1, prevRangeTop = -1;
Chris@28 488 QColor prevRangeBottomColour = m_colour, prevRangeTopColour = m_colour;
Chris@0 489
Chris@0 490 int m = (h / channels) / 2;
Chris@0 491 int my = m + (((ch - minChannel) * h) / channels);
Chris@0 492
Chris@0 493 // std::cerr << "ch = " << ch << ", channels = " << channels << ", m = " << m << ", my = " << my << ", h = " << h << std::endl;
Chris@0 494
Chris@0 495 if (my - m > y1 || my + m < y0) continue;
Chris@0 496
Chris@0 497 paint->setPen(greys[0]);
Chris@0 498 paint->drawLine(x0, my, x1, my);
Chris@0 499
Chris@0 500 if (frame1 <= 0) continue;
Chris@0 501
Chris@0 502 size_t modelZoomLevel = zoomLevel;
Chris@0 503
Chris@0 504 ranges = m_model->getRanges
Chris@0 505 (ch, frame0 < 0 ? 0 : frame0, frame1, modelZoomLevel);
Chris@0 506
Chris@67 507 if (mergingChannels || mixingChannels) {
Chris@0 508 otherChannelRanges = m_model->getRanges
Chris@0 509 (1, frame0 < 0 ? 0 : frame0, frame1, modelZoomLevel);
Chris@0 510 }
Chris@0 511
Chris@67 512 m_effectiveGains[ch] = m_gain;
Chris@67 513
Chris@67 514 if (m_autoNormalize) {
Chris@67 515 RangeSummarisableTimeValueModel::Range range =
Chris@67 516 m_model->getRange(ch, startFrame < 0 ? 0 : startFrame,
Chris@67 517 v->getEndFrame());
Chris@67 518 if (mergingChannels || mixingChannels) {
Chris@67 519 RangeSummarisableTimeValueModel::Range otherRange =
Chris@67 520 m_model->getRange(1, startFrame < 0 ? 0 : startFrame,
Chris@67 521 v->getEndFrame());
Chris@67 522 range.max = std::max(range.max, otherRange.max);
Chris@67 523 range.min = std::min(range.min, otherRange.min);
Chris@67 524 range.absmean = std::min(range.absmean, otherRange.absmean);
Chris@67 525 }
Chris@67 526 m_effectiveGains[ch] = 1.0 / std::max(fabsf(range.max),
Chris@67 527 fabsf(range.min));
Chris@67 528 }
Chris@67 529
Chris@67 530 float gain = m_effectiveGains[ch];
Chris@67 531
Chris@0 532 for (int x = x0; x <= x1; ++x) {
Chris@0 533
Chris@0 534 range = RangeSummarisableTimeValueModel::Range();
Chris@0 535 size_t index = x - x0;
Chris@0 536 size_t maxIndex = index;
Chris@0 537
Chris@0 538 if (frame0 < 0) {
Chris@0 539 if (index < size_t(-frame0 / zoomLevel)) {
Chris@0 540 continue;
Chris@0 541 } else {
Chris@0 542 index -= -frame0 / zoomLevel;
Chris@0 543 maxIndex = index;
Chris@0 544 }
Chris@0 545 }
Chris@0 546
Chris@0 547 if (int(modelZoomLevel) != zoomLevel) {
Chris@0 548
Chris@0 549 index = size_t((double(index) * zoomLevel) / modelZoomLevel);
Chris@0 550
Chris@0 551 if (int(modelZoomLevel) < zoomLevel) {
Chris@0 552 // Peaks may be missed! The model should avoid
Chris@0 553 // this by rounding zoom levels up rather than
Chris@0 554 // down, but we'd better cope in case it doesn't
Chris@0 555 maxIndex = index;
Chris@0 556 } else {
Chris@0 557 maxIndex = size_t((double(index + 1) * zoomLevel)
Chris@0 558 / modelZoomLevel) - 1;
Chris@0 559 }
Chris@0 560 }
Chris@0 561
Chris@0 562 if (index < ranges.size()) {
Chris@0 563
Chris@0 564 range = ranges[index];
Chris@0 565
Chris@0 566 if (maxIndex > index && maxIndex < ranges.size()) {
Chris@0 567 range.max = std::max(range.max, ranges[maxIndex].max);
Chris@0 568 range.min = std::min(range.min, ranges[maxIndex].min);
Chris@0 569 range.absmean = (range.absmean +
Chris@0 570 ranges[maxIndex].absmean) / 2;
Chris@0 571 }
Chris@0 572
Chris@0 573 } else {
Chris@0 574 continue;
Chris@0 575 }
Chris@0 576
Chris@0 577 int rangeBottom = 0, rangeTop = 0, meanBottom = 0, meanTop = 0;
Chris@0 578
Chris@0 579 if (mergingChannels) {
Chris@0 580
Chris@0 581 if (index < otherChannelRanges.size()) {
Chris@0 582
Chris@0 583 range.max = fabsf(range.max);
Chris@0 584 range.min = -fabsf(otherChannelRanges[index].max);
Chris@0 585 range.absmean = (range.absmean +
Chris@0 586 otherChannelRanges[index].absmean) / 2;
Chris@0 587
Chris@0 588 if (maxIndex > index && maxIndex < ranges.size()) {
Chris@0 589 // let's not concern ourselves about the mean
Chris@0 590 range.min = std::min
Chris@0 591 (range.min,
Chris@0 592 -fabsf(otherChannelRanges[maxIndex].max));
Chris@0 593 }
Chris@0 594 }
Chris@67 595
Chris@67 596 } else if (mixingChannels) {
Chris@67 597
Chris@67 598 if (index < otherChannelRanges.size()) {
Chris@67 599
Chris@67 600 range.max = (range.max + otherChannelRanges[index].max) / 2;
Chris@67 601 range.min = (range.min + otherChannelRanges[index].min) / 2;
Chris@67 602 range.absmean = (range.absmean + otherChannelRanges[index].absmean) / 2;
Chris@67 603 }
Chris@67 604 }
Chris@0 605
Chris@0 606 int greyLevels = 1;
Chris@0 607 if (m_greyscale && (m_scale == LinearScale)) greyLevels = 4;
Chris@0 608
Chris@0 609 switch (m_scale) {
Chris@0 610
Chris@0 611 case LinearScale:
Chris@67 612 rangeBottom = int( m * greyLevels * range.min * gain);
Chris@67 613 rangeTop = int( m * greyLevels * range.max * gain);
Chris@67 614 meanBottom = int(-m * range.absmean * gain);
Chris@67 615 meanTop = int( m * range.absmean * gain);
Chris@0 616 break;
Chris@0 617
Chris@0 618 case dBScale:
Chris@67 619 if (!mergingChannels) {
Chris@67 620 int db0 = dBscale(range.min * gain, m);
Chris@67 621 int db1 = dBscale(range.max * gain, m);
Chris@67 622 rangeTop = std::max(db0, db1);
Chris@67 623 meanTop = std::min(db0, db1);
Chris@67 624 if (mixingChannels) rangeBottom = meanTop;
Chris@67 625 else rangeBottom = dBscale(range.absmean * gain, m);
Chris@67 626 meanBottom = rangeBottom;
Chris@67 627 } else {
Chris@67 628 rangeBottom = -dBscale(range.min * gain, m * greyLevels);
Chris@67 629 rangeTop = dBscale(range.max * gain, m * greyLevels);
Chris@67 630 meanBottom = -dBscale(range.absmean * gain, m);
Chris@67 631 meanTop = dBscale(range.absmean * gain, m);
Chris@67 632 }
Chris@0 633 break;
Chris@0 634
Chris@0 635 case MeterScale:
Chris@67 636 if (!mergingChannels) {
Chris@67 637 int r0 = abs(AudioLevel::multiplier_to_preview(range.min * gain, m));
Chris@67 638 int r1 = abs(AudioLevel::multiplier_to_preview(range.max * gain, m));
Chris@67 639 rangeTop = std::max(r0, r1);
Chris@67 640 meanTop = std::min(r0, r1);
Chris@67 641 if (mixingChannels) rangeBottom = meanTop;
Chris@67 642 else rangeBottom = AudioLevel::multiplier_to_preview(range.absmean * gain, m);
Chris@67 643 meanBottom = rangeBottom;
Chris@67 644 } else {
Chris@67 645 rangeBottom = AudioLevel::multiplier_to_preview(range.min * gain, m * greyLevels);
Chris@67 646 rangeTop = AudioLevel::multiplier_to_preview(range.max * gain, m * greyLevels);
Chris@67 647 meanBottom = -AudioLevel::multiplier_to_preview(range.absmean * gain, m);
Chris@67 648 meanTop = AudioLevel::multiplier_to_preview(range.absmean * gain, m);
Chris@67 649 }
Chris@67 650 break;
Chris@0 651 }
Chris@0 652
Chris@27 653 rangeBottom = my * greyLevels - rangeBottom;
Chris@27 654 rangeTop = my * greyLevels - rangeTop;
Chris@27 655 meanBottom = my - meanBottom;
Chris@27 656 meanTop = my - meanTop;
Chris@27 657
Chris@27 658 int topFill = (rangeTop % greyLevels);
Chris@27 659 if (topFill > 0) topFill = greyLevels - topFill;
Chris@27 660
Chris@27 661 int bottomFill = (rangeBottom % greyLevels);
Chris@27 662
Chris@0 663 rangeTop = rangeTop / greyLevels;
Chris@0 664 rangeBottom = rangeBottom / greyLevels;
Chris@0 665
Chris@0 666 bool clipped = false;
Chris@27 667
Chris@27 668 if (rangeTop < my - m) { rangeTop = my - m; }
Chris@27 669 if (rangeTop > my + m) { rangeTop = my + m; }
Chris@27 670 if (rangeBottom < my - m) { rangeBottom = my - m; }
Chris@27 671 if (rangeBottom > my + m) { rangeBottom = my + m; }
Chris@27 672
Chris@67 673 if (range.max <= -1.0 ||
Chris@67 674 range.max >= 1.0) clipped = true;
Chris@0 675
Chris@0 676 if (meanBottom > rangeBottom) meanBottom = rangeBottom;
Chris@0 677 if (meanTop < rangeTop) meanTop = rangeTop;
Chris@0 678
Chris@0 679 bool drawMean = m_showMeans;
Chris@0 680 if (meanTop == rangeTop) {
Chris@0 681 if (meanTop < meanBottom) ++meanTop;
Chris@0 682 else drawMean = false;
Chris@0 683 }
Chris@67 684 if (meanBottom == rangeBottom && m_scale == LinearScale) {
Chris@0 685 if (meanBottom > meanTop) --meanBottom;
Chris@0 686 else drawMean = false;
Chris@0 687 }
Chris@0 688
Chris@0 689 if (x != x0 && prevRangeBottom != -1) {
Chris@0 690 if (prevRangeBottom > rangeBottom &&
Chris@0 691 prevRangeTop > rangeBottom) {
Chris@28 692 // paint->setPen(midColour);
Chris@28 693 paint->setPen(m_colour);
Chris@0 694 paint->drawLine(x-1, prevRangeTop, x, rangeBottom);
Chris@28 695 paint->setPen(prevRangeTopColour);
Chris@0 696 paint->drawPoint(x-1, prevRangeTop);
Chris@0 697 } else if (prevRangeBottom < rangeTop &&
Chris@0 698 prevRangeTop < rangeTop) {
Chris@28 699 // paint->setPen(midColour);
Chris@28 700 paint->setPen(m_colour);
Chris@0 701 paint->drawLine(x-1, prevRangeBottom, x, rangeTop);
Chris@28 702 paint->setPen(prevRangeBottomColour);
Chris@0 703 paint->drawPoint(x-1, prevRangeBottom);
Chris@0 704 }
Chris@0 705 }
Chris@0 706
Chris@0 707 if (ready) {
Chris@67 708 if (clipped /*!!! ||
Chris@67 709 range.min * gain <= -1.0 ||
Chris@67 710 range.max * gain >= 1.0 */) {
Chris@0 711 paint->setPen(Qt::red);
Chris@0 712 } else {
Chris@0 713 paint->setPen(m_colour);
Chris@0 714 }
Chris@0 715 } else {
Chris@0 716 paint->setPen(midColour);
Chris@0 717 }
Chris@0 718
Chris@0 719 paint->drawLine(x, rangeBottom, x, rangeTop);
Chris@0 720
Chris@28 721 prevRangeTopColour = m_colour;
Chris@28 722 prevRangeBottomColour = m_colour;
Chris@28 723
Chris@0 724 if (m_greyscale && (m_scale == LinearScale) && ready) {
Chris@0 725 if (!clipped) {
Chris@0 726 if (rangeTop < rangeBottom) {
Chris@0 727 if (topFill > 0 &&
Chris@0 728 (!drawMean || (rangeTop < meanTop - 1))) {
Chris@0 729 paint->setPen(greys[topFill - 1]);
Chris@27 730 paint->drawPoint(x, rangeTop);
Chris@28 731 prevRangeTopColour = greys[topFill - 1];
Chris@0 732 }
Chris@0 733 if (bottomFill > 0 &&
Chris@0 734 (!drawMean || (rangeBottom > meanBottom + 1))) {
Chris@0 735 paint->setPen(greys[bottomFill - 1]);
Chris@27 736 paint->drawPoint(x, rangeBottom);
Chris@28 737 prevRangeBottomColour = greys[bottomFill - 1];
Chris@0 738 }
Chris@0 739 }
Chris@0 740 }
Chris@0 741 }
Chris@0 742
Chris@0 743 if (drawMean) {
Chris@0 744 paint->setPen(midColour);
Chris@0 745 paint->drawLine(x, meanBottom, x, meanTop);
Chris@0 746 }
Chris@0 747
Chris@0 748 prevRangeBottom = rangeBottom;
Chris@0 749 prevRangeTop = rangeTop;
Chris@0 750 }
Chris@0 751 }
Chris@0 752
Chris@0 753 if (m_aggressive) {
Chris@41 754
Chris@44 755 if (ready && rect == v->rect()) {
Chris@0 756 m_cacheValid = true;
Chris@0 757 m_cacheZoomLevel = zoomLevel;
Chris@0 758 }
Chris@0 759 paint->end();
Chris@0 760 delete paint;
Chris@0 761 viewPainter.drawPixmap(rect, *m_cache, rect);
Chris@0 762 }
Chris@0 763 }
Chris@0 764
Chris@25 765 QString
Chris@44 766 WaveformLayer::getFeatureDescription(View *v, QPoint &pos) const
Chris@25 767 {
Chris@25 768 int x = pos.x();
Chris@25 769
Chris@25 770 if (!m_model || !m_model->isOK()) return "";
Chris@25 771
Chris@44 772 long f0 = v->getFrameForX(x);
Chris@44 773 long f1 = v->getFrameForX(x + 1);
Chris@25 774
Chris@25 775 if (f0 < 0) f0 = 0;
Chris@25 776 if (f1 <= f0) return "";
Chris@25 777
Chris@25 778 QString text;
Chris@25 779
Chris@25 780 RealTime rt0 = RealTime::frame2RealTime(f0, m_model->getSampleRate());
Chris@25 781 RealTime rt1 = RealTime::frame2RealTime(f1, m_model->getSampleRate());
Chris@25 782
Chris@25 783 if (f1 != f0 + 1 && (rt0.sec != rt1.sec || rt0.msec() != rt1.msec())) {
Chris@25 784 text += tr("Time:\t%1 - %2")
Chris@25 785 .arg(rt0.toText(true).c_str())
Chris@25 786 .arg(rt1.toText(true).c_str());
Chris@25 787 } else {
Chris@25 788 text += tr("Time:\t%1")
Chris@25 789 .arg(rt0.toText(true).c_str());
Chris@25 790 }
Chris@25 791
Chris@25 792 size_t channels = 0, minChannel = 0, maxChannel = 0;
Chris@67 793 bool mergingChannels = false, mixingChannels = false;
Chris@25 794
Chris@67 795 channels = getChannelArrangement(minChannel, maxChannel,
Chris@67 796 mergingChannels, mixingChannels);
Chris@25 797 if (channels == 0) return "";
Chris@25 798
Chris@25 799 for (size_t ch = minChannel; ch <= maxChannel; ++ch) {
Chris@25 800
Chris@44 801 size_t blockSize = v->getZoomLevel();
Chris@25 802 RangeSummarisableTimeValueModel::RangeBlock ranges =
Chris@25 803 m_model->getRanges(ch, f0, f1, blockSize);
Chris@25 804
Chris@25 805 if (ranges.empty()) continue;
Chris@25 806
Chris@25 807 RangeSummarisableTimeValueModel::Range range = ranges[0];
Chris@25 808
Chris@25 809 QString label = tr("Level:");
Chris@25 810 if (minChannel != maxChannel) {
Chris@25 811 if (ch == 0) label = tr("Left:");
Chris@25 812 else if (ch == 1) label = tr("Right:");
Chris@25 813 else label = tr("Channel %1").arg(ch + 1);
Chris@25 814 }
Chris@25 815
Chris@25 816 int min = int(range.min * 1000);
Chris@25 817 int max = int(range.max * 1000);
Chris@25 818 int db = int(AudioLevel::multiplier_to_dB(std::max(fabsf(range.min),
Chris@25 819 fabsf(range.max)))
Chris@25 820 * 100);
Chris@25 821
Chris@25 822 if (min != max) {
Chris@25 823 text += tr("\n%1\t%2 - %3 (%4 dB peak)")
Chris@25 824 .arg(label).arg(float(min)/1000).arg(float(max)/1000).arg(float(db)/100);
Chris@25 825 } else {
Chris@25 826 text += tr("\n%1\t%2 (%3 dB peak)")
Chris@25 827 .arg(label).arg(float(min)/1000).arg(float(db)/100);
Chris@25 828 }
Chris@25 829 }
Chris@25 830
Chris@25 831 return text;
Chris@25 832 }
Chris@25 833
Chris@0 834 int
Chris@44 835 WaveformLayer::getVerticalScaleWidth(View *v, QPainter &paint) const
Chris@0 836 {
Chris@0 837 if (m_scale == LinearScale) {
Chris@0 838 return paint.fontMetrics().width("0.0") + 13;
Chris@0 839 } else {
Chris@0 840 return std::max(paint.fontMetrics().width(tr("0dB")),
Chris@0 841 paint.fontMetrics().width(tr("-Inf"))) + 13;
Chris@0 842 }
Chris@0 843 }
Chris@0 844
Chris@0 845 void
Chris@44 846 WaveformLayer::paintVerticalScale(View *v, QPainter &paint, QRect rect) const
Chris@0 847 {
Chris@0 848 if (!m_model || !m_model->isOK()) {
Chris@0 849 return;
Chris@0 850 }
Chris@0 851
Chris@0 852 size_t channels = 0, minChannel = 0, maxChannel = 0;
Chris@67 853 bool mergingChannels = false, mixingChannels = false;
Chris@0 854
Chris@67 855 channels = getChannelArrangement(minChannel, maxChannel,
Chris@67 856 mergingChannels, mixingChannels);
Chris@0 857 if (channels == 0) return;
Chris@0 858
Chris@0 859 int h = rect.height(), w = rect.width();
Chris@0 860 int textHeight = paint.fontMetrics().height();
Chris@0 861 int toff = -textHeight/2 + paint.fontMetrics().ascent() + 1;
Chris@0 862
Chris@67 863 float gain = m_gain;
Chris@67 864
Chris@0 865 for (size_t ch = minChannel; ch <= maxChannel; ++ch) {
Chris@0 866
Chris@0 867 int m = (h / channels) / 2;
Chris@0 868 int my = m + (((ch - minChannel) * h) / channels);
Chris@0 869 int py = -1;
Chris@0 870
Chris@67 871 if (ch < m_effectiveGains.size()) gain = m_effectiveGains[ch];
Chris@67 872
Chris@0 873 for (int i = 0; i <= 10; ++i) {
Chris@0 874
Chris@0 875 int vy = 0;
Chris@0 876 QString text = "";
Chris@0 877
Chris@0 878 if (m_scale == LinearScale) {
Chris@0 879
Chris@67 880 vy = int((m * i * gain) / 10);
Chris@0 881
Chris@0 882 text = QString("%1").arg(float(i) / 10.0);
Chris@0 883 if (i == 0) text = "0.0";
Chris@0 884 if (i == 10) text = "1.0";
Chris@0 885
Chris@0 886 } else {
Chris@0 887
Chris@0 888 int db;
Chris@0 889 bool minvalue = false;
Chris@0 890
Chris@0 891 if (m_scale == MeterScale) {
Chris@0 892 static int dbs[] = { -50, -40, -30, -20, -15,
Chris@0 893 -10, -5, -3, -2, -1, 0 };
Chris@0 894 db = dbs[i];
Chris@0 895 if (db == -50) minvalue = true;
Chris@0 896 vy = AudioLevel::multiplier_to_preview
Chris@67 897 (AudioLevel::dB_to_multiplier(db) * gain, m);
Chris@0 898 } else {
Chris@0 899 db = -100 + i * 10;
Chris@0 900 if (db == -100) minvalue = true;
Chris@0 901 vy = dBscale
Chris@67 902 (AudioLevel::dB_to_multiplier(db) * gain, m);
Chris@0 903 }
Chris@0 904
Chris@0 905 text = QString("%1").arg(db);
Chris@0 906 if (db == 0) text = tr("0dB");
Chris@0 907 if (minvalue) {
Chris@0 908 text = tr("-Inf");
Chris@0 909 vy = 0;
Chris@0 910 }
Chris@0 911 }
Chris@0 912
Chris@0 913 if (vy < 0) vy = -vy;
Chris@67 914 // if (vy >= m - 1) continue;
Chris@0 915
Chris@0 916 if (py >= 0 && (vy - py) < textHeight - 1) {
Chris@0 917 paint.drawLine(w - 4, my - vy, w, my - vy);
Chris@0 918 if (vy > 0) paint.drawLine(w - 4, my + vy, w, my + vy);
Chris@0 919 continue;
Chris@0 920 }
Chris@0 921
Chris@0 922 paint.drawLine(w - 7, my - vy, w, my - vy);
Chris@0 923 if (vy > 0) paint.drawLine(w - 7, my + vy, w, my + vy);
Chris@0 924
Chris@0 925 int tx = 3;
Chris@0 926 if (m_scale != LinearScale) {
Chris@0 927 tx = w - 10 - paint.fontMetrics().width(text);
Chris@0 928 }
Chris@0 929
Chris@67 930 if (vy >= m - 1) continue;
Chris@67 931
Chris@0 932 paint.drawText(tx, my - vy + toff, text);
Chris@0 933 if (vy > 0) paint.drawText(tx, my + vy + toff, text);
Chris@0 934
Chris@0 935 py = vy;
Chris@0 936 }
Chris@0 937 }
Chris@0 938 }
Chris@0 939
Chris@6 940 QString
Chris@6 941 WaveformLayer::toXmlString(QString indent, QString extraAttributes) const
Chris@6 942 {
Chris@6 943 QString s;
Chris@6 944
Chris@6 945 s += QString("gain=\"%1\" "
Chris@6 946 "colour=\"%2\" "
Chris@6 947 "showMeans=\"%3\" "
Chris@6 948 "greyscale=\"%4\" "
Chris@6 949 "channelMode=\"%5\" "
Chris@6 950 "channel=\"%6\" "
Chris@6 951 "scale=\"%7\" "
Chris@67 952 "aggressive=\"%8\" "
Chris@67 953 "autoNormalize=\"%9\"")
Chris@6 954 .arg(m_gain)
Chris@6 955 .arg(encodeColour(m_colour))
Chris@6 956 .arg(m_showMeans)
Chris@6 957 .arg(m_greyscale)
Chris@6 958 .arg(m_channelMode)
Chris@6 959 .arg(m_channel)
Chris@6 960 .arg(m_scale)
Chris@67 961 .arg(m_aggressive)
Chris@67 962 .arg(m_autoNormalize);
Chris@6 963
Chris@6 964 return Layer::toXmlString(indent, extraAttributes + " " + s);
Chris@6 965 }
Chris@6 966
Chris@11 967 void
Chris@11 968 WaveformLayer::setProperties(const QXmlAttributes &attributes)
Chris@11 969 {
Chris@11 970 bool ok = false;
Chris@11 971
Chris@11 972 float gain = attributes.value("gain").toFloat(&ok);
Chris@11 973 if (ok) setGain(gain);
Chris@11 974
Chris@11 975 QString colourSpec = attributes.value("colour");
Chris@11 976 if (colourSpec != "") {
Chris@11 977 QColor colour(colourSpec);
Chris@11 978 if (colour.isValid()) {
Chris@11 979 setBaseColour(QColor(colourSpec));
Chris@11 980 }
Chris@11 981 }
Chris@11 982
Chris@11 983 bool showMeans = (attributes.value("showMeans") == "1" ||
Chris@11 984 attributes.value("showMeans") == "true");
Chris@11 985 setShowMeans(showMeans);
Chris@11 986
Chris@11 987 bool greyscale = (attributes.value("greyscale") == "1" ||
Chris@11 988 attributes.value("greyscale") == "true");
Chris@11 989 setUseGreyscale(greyscale);
Chris@11 990
Chris@11 991 ChannelMode channelMode = (ChannelMode)
Chris@11 992 attributes.value("channelMode").toInt(&ok);
Chris@11 993 if (ok) setChannelMode(channelMode);
Chris@11 994
Chris@11 995 int channel = attributes.value("channel").toInt(&ok);
Chris@11 996 if (ok) setChannel(channel);
Chris@11 997
Chris@11 998 Scale scale = (Scale)
Chris@11 999 attributes.value("scale").toInt(&ok);
Chris@11 1000 if (ok) setScale(scale);
Chris@11 1001
Chris@11 1002 bool aggressive = (attributes.value("aggressive") == "1" ||
Chris@11 1003 attributes.value("aggressive") == "true");
Chris@11 1004 setUseGreyscale(aggressive);
Chris@67 1005
Chris@67 1006 bool autoNormalize = (attributes.value("autoNormalize") == "1" ||
Chris@67 1007 attributes.value("autoNormalize") == "true");
Chris@67 1008 setAutoNormalize(autoNormalize);
Chris@11 1009 }
Chris@11 1010
Chris@0 1011 #ifdef INCLUDE_MOCFILES
Chris@0 1012 #include "WaveformLayer.moc.cpp"
Chris@0 1013 #endif
Chris@0 1014