annotate layer/WaveformLayer.cpp @ 63:fb02fe13ff47

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