annotate layer/WaveformLayer.cpp @ 285:9dd432665059

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