annotate layer/WaveformLayer.cpp @ 270:61a704654497

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