annotate layer/WaveformLayer.cpp @ 251:948a3b05798b

* minor usability fixes
author Chris Cannam
date Fri, 18 May 2007 16:47:38 +0000
parents 28c8e8e3c537
children 11021509c4eb
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@67 392 if (sample < 0.0) return dBscale(-sample, m);
Chris@0 393 float dB = AudioLevel::multiplier_to_dB(sample);
Chris@0 394 if (dB < -50.0) return 0;
Chris@0 395 if (dB > 0.0) return m;
Chris@0 396 return int(((dB + 50.0) * m) / 50.0 + 0.1);
Chris@0 397 }
Chris@0 398
Chris@0 399 size_t
Chris@67 400 WaveformLayer::getChannelArrangement(size_t &min, size_t &max,
Chris@67 401 bool &merging, bool &mixing)
Chris@0 402 const
Chris@0 403 {
Chris@0 404 if (!m_model || !m_model->isOK()) return 0;
Chris@0 405
Chris@0 406 size_t channels = m_model->getChannelCount();
Chris@0 407 if (channels == 0) return 0;
Chris@0 408
Chris@0 409 size_t rawChannels = channels;
Chris@0 410
Chris@0 411 if (m_channel == -1) {
Chris@0 412 min = 0;
Chris@67 413 if (m_channelMode == MergeChannels ||
Chris@67 414 m_channelMode == MixChannels) {
Chris@0 415 max = 0;
Chris@0 416 channels = 1;
Chris@0 417 } else {
Chris@0 418 max = channels - 1;
Chris@0 419 }
Chris@0 420 } else {
Chris@0 421 min = m_channel;
Chris@0 422 max = m_channel;
Chris@0 423 rawChannels = 1;
Chris@0 424 channels = 1;
Chris@0 425 }
Chris@0 426
Chris@0 427 merging = (m_channelMode == MergeChannels && rawChannels > 1);
Chris@67 428 mixing = (m_channelMode == MixChannels && rawChannels > 1);
Chris@0 429
Chris@0 430 // std::cerr << "WaveformLayer::getChannelArrangement: min " << min << ", max " << max << ", merging " << merging << ", channels " << channels << std::endl;
Chris@0 431
Chris@0 432 return channels;
Chris@0 433 }
Chris@0 434
Chris@67 435 bool
Chris@67 436 WaveformLayer::isLayerScrollable(const View *) const
Chris@67 437 {
Chris@67 438 return !m_autoNormalize;
Chris@67 439 }
Chris@67 440
Chris@68 441 static float meterdbs[] = { -40, -30, -20, -15, -10,
Chris@68 442 -5, -3, -2, -1, -0.5, 0 };
Chris@68 443
Chris@0 444 void
Chris@44 445 WaveformLayer::paint(View *v, QPainter &viewPainter, QRect rect) const
Chris@0 446 {
Chris@0 447 if (!m_model || !m_model->isOK()) {
Chris@0 448 return;
Chris@0 449 }
Chris@0 450
Chris@44 451 long startFrame = v->getStartFrame();
Chris@44 452 int zoomLevel = v->getZoomLevel();
Chris@0 453
Chris@2 454 #ifdef DEBUG_WAVEFORM_PAINT
Chris@0 455 Profiler profiler("WaveformLayer::paint", true);
Chris@0 456 std::cerr << "WaveformLayer::paint (" << rect.x() << "," << rect.y()
Chris@0 457 << ") [" << rect.width() << "x" << rect.height() << "]: zoom " << zoomLevel << ", start " << startFrame << std::endl;
Chris@2 458 #endif
Chris@0 459
Chris@0 460 size_t channels = 0, minChannel = 0, maxChannel = 0;
Chris@67 461 bool mergingChannels = false, mixingChannels = false;
Chris@0 462
Chris@67 463 channels = getChannelArrangement(minChannel, maxChannel,
Chris@67 464 mergingChannels, mixingChannels);
Chris@0 465 if (channels == 0) return;
Chris@0 466
Chris@44 467 int w = v->width();
Chris@44 468 int h = v->height();
Chris@0 469
Chris@0 470 bool ready = m_model->isReady();
Chris@0 471 QPainter *paint;
Chris@0 472
Chris@0 473 if (m_aggressive) {
Chris@0 474
Chris@214 475 #ifdef DEBUG_WAVEFORM_PAINT
Chris@214 476 std::cerr << "WaveformLayer::paint: aggressive is true" << std::endl;
Chris@214 477 #endif
Chris@214 478
Chris@0 479 if (m_cacheValid && (zoomLevel != m_cacheZoomLevel)) {
Chris@0 480 m_cacheValid = false;
Chris@0 481 }
Chris@0 482
Chris@214 483 if (!m_cache || m_cache->width() != w || m_cache->height() != h) {
Chris@214 484 #ifdef DEBUG_WAVEFORM_PAINT
Chris@214 485 if (m_cache) {
Chris@214 486 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 487 }
Chris@214 488 #endif
Chris@214 489 delete m_cache;
Chris@214 490 m_cache = new QPixmap(w, h);
Chris@214 491 m_cacheValid = false;
Chris@214 492 }
Chris@214 493
Chris@0 494 if (m_cacheValid) {
Chris@0 495 viewPainter.drawPixmap(rect, *m_cache, rect);
Chris@0 496 return;
Chris@0 497 }
Chris@0 498
Chris@0 499 paint = new QPainter(m_cache);
Chris@0 500
Chris@0 501 paint->setPen(Qt::NoPen);
Chris@44 502 paint->setBrush(v->palette().background());
Chris@0 503 paint->drawRect(rect);
Chris@0 504
Chris@0 505 paint->setPen(Qt::black);
Chris@0 506 paint->setBrush(Qt::NoBrush);
Chris@0 507
Chris@0 508 } else {
Chris@0 509 paint = &viewPainter;
Chris@0 510 }
Chris@0 511
Chris@28 512 paint->setRenderHint(QPainter::Antialiasing, false);
Chris@28 513
Chris@0 514 int x0 = 0, x1 = w - 1;
Chris@0 515 int y0 = 0, y1 = h - 1;
Chris@0 516
Chris@0 517 x0 = rect.left();
Chris@0 518 x1 = rect.right();
Chris@0 519 y0 = rect.top();
Chris@0 520 y1 = rect.bottom();
Chris@0 521
Chris@28 522 if (x0 > 0) --x0;
Chris@44 523 if (x1 < v->width()) ++x1;
Chris@28 524
Chris@44 525 long frame0 = v->getFrameForX(x0);
Chris@44 526 long frame1 = v->getFrameForX(x1 + 1);
Chris@0 527
Chris@4 528 #ifdef DEBUG_WAVEFORM_PAINT
Chris@4 529 std::cerr << "Painting waveform from " << frame0 << " to " << frame1 << " (" << (x1-x0+1) << " pixels at zoom " << zoomLevel << ")" << std::endl;
Chris@4 530 #endif
Chris@0 531
Chris@200 532 RangeSummarisableTimeValueModel::RangeBlock *ranges =
Chris@200 533 new RangeSummarisableTimeValueModel::RangeBlock;
Chris@200 534
Chris@200 535 RangeSummarisableTimeValueModel::RangeBlock *otherChannelRanges = 0;
Chris@0 536 RangeSummarisableTimeValueModel::Range range;
Chris@0 537
Chris@0 538 QColor greys[3];
Chris@0 539 if (m_colour == Qt::black) {
Chris@27 540 for (int i = 0; i < 3; ++i) { // 0 lightest, 2 darkest
Chris@0 541 int level = 192 - 64 * i;
Chris@0 542 greys[i] = QColor(level, level, level);
Chris@0 543 }
Chris@0 544 } else {
Chris@44 545 int hue, sat, val;
Chris@44 546 m_colour.getHsv(&hue, &sat, &val);
Chris@27 547 for (int i = 0; i < 3; ++i) { // 0 lightest, 2 darkest
Chris@44 548 if (v->hasLightBackground()) {
Chris@44 549 greys[i] = QColor::fromHsv(hue, sat * (i + 1) / 4, val);
Chris@27 550 } else {
Chris@44 551 greys[i] = QColor::fromHsv(hue, sat * (3 - i) / 4, val);
Chris@27 552 }
Chris@27 553 }
Chris@0 554 }
Chris@0 555
Chris@0 556 QColor midColour = m_colour;
Chris@0 557 if (midColour == Qt::black) {
Chris@0 558 midColour = Qt::gray;
Chris@44 559 } else if (v->hasLightBackground()) {
Chris@0 560 midColour = midColour.light(150);
Chris@0 561 } else {
Chris@0 562 midColour = midColour.light(50);
Chris@0 563 }
Chris@0 564
Chris@67 565 while (m_effectiveGains.size() <= maxChannel) {
Chris@67 566 m_effectiveGains.push_back(m_gain);
Chris@67 567 }
Chris@67 568
Chris@0 569 for (size_t ch = minChannel; ch <= maxChannel; ++ch) {
Chris@0 570
Chris@0 571 int prevRangeBottom = -1, prevRangeTop = -1;
Chris@28 572 QColor prevRangeBottomColour = m_colour, prevRangeTopColour = m_colour;
Chris@0 573
Chris@67 574 m_effectiveGains[ch] = m_gain;
Chris@67 575
Chris@67 576 if (m_autoNormalize) {
Chris@67 577 RangeSummarisableTimeValueModel::Range range =
Chris@67 578 m_model->getRange(ch, startFrame < 0 ? 0 : startFrame,
Chris@67 579 v->getEndFrame());
Chris@67 580 if (mergingChannels || mixingChannels) {
Chris@67 581 RangeSummarisableTimeValueModel::Range otherRange =
Chris@67 582 m_model->getRange(1, startFrame < 0 ? 0 : startFrame,
Chris@67 583 v->getEndFrame());
Chris@67 584 range.max = std::max(range.max, otherRange.max);
Chris@67 585 range.min = std::min(range.min, otherRange.min);
Chris@67 586 range.absmean = std::min(range.absmean, otherRange.absmean);
Chris@67 587 }
Chris@67 588 m_effectiveGains[ch] = 1.0 / std::max(fabsf(range.max),
Chris@67 589 fabsf(range.min));
Chris@67 590 }
Chris@67 591
Chris@67 592 float gain = m_effectiveGains[ch];
Chris@67 593
Chris@68 594 int m = (h / channels) / 2;
Chris@68 595 int my = m + (((ch - minChannel) * h) / channels);
Chris@68 596
Chris@68 597 // std::cerr << "ch = " << ch << ", channels = " << channels << ", m = " << m << ", my = " << my << ", h = " << h << std::endl;
Chris@68 598
Chris@68 599 if (my - m > y1 || my + m < y0) continue;
Chris@68 600
Chris@68 601 if ((m_scale == dBScale || m_scale == MeterScale) &&
Chris@68 602 m_channelMode != MergeChannels) {
Chris@68 603 m = (h / channels);
Chris@68 604 my = m + (((ch - minChannel) * h) / channels);
Chris@68 605 }
Chris@68 606
Chris@68 607 paint->setPen(greys[0]);
Chris@68 608 paint->drawLine(x0, my, x1, my);
Chris@68 609
Chris@68 610 int n = 10;
Chris@68 611 int py = -1;
Chris@68 612
Chris@195 613 if (v->hasLightBackground() &&
Chris@195 614 v->getViewManager() &&
Chris@195 615 v->getViewManager()->shouldShowScaleGuides()) {
Chris@68 616
Chris@68 617 paint->setPen(QColor(240, 240, 240));
Chris@68 618
Chris@68 619 for (int i = 1; i < n; ++i) {
Chris@68 620
Chris@68 621 float val = 0.0, nval = 0.0;
Chris@68 622
Chris@68 623 switch (m_scale) {
Chris@68 624
Chris@68 625 case LinearScale:
Chris@68 626 val = (i * gain) / n;
Chris@68 627 if (i > 0) nval = -val;
Chris@68 628 break;
Chris@68 629
Chris@68 630 case MeterScale:
Chris@68 631 val = AudioLevel::dB_to_multiplier(meterdbs[i]) * gain;
Chris@68 632 break;
Chris@68 633
Chris@68 634 case dBScale:
Chris@68 635 val = AudioLevel::dB_to_multiplier(-(10*n) + i * 10) * gain;
Chris@68 636 break;
Chris@68 637 }
Chris@68 638
Chris@68 639 if (val < -1.0 || val > 1.0) continue;
Chris@68 640
Chris@68 641 int y = getYForValue(v, m_scale, val, ch, minChannel, maxChannel);
Chris@68 642
Chris@68 643 if (py >= 0 && abs(y - py) < 10) continue;
Chris@68 644 else py = y;
Chris@68 645
Chris@68 646 int ny = y;
Chris@68 647 if (nval != 0.0) {
Chris@68 648 ny = getYForValue(v, m_scale, nval, ch, minChannel, maxChannel);
Chris@68 649 }
Chris@68 650
Chris@68 651 paint->drawLine(x0, y, x1, y);
Chris@68 652 if (ny != y) {
Chris@68 653 paint->drawLine(x0, ny, x1, ny);
Chris@68 654 }
Chris@68 655 }
Chris@68 656 }
Chris@68 657
Chris@68 658 if (frame1 <= 0) continue;
Chris@68 659
Chris@68 660 size_t modelZoomLevel = zoomLevel;
Chris@68 661
Chris@200 662 m_model->getRanges
Chris@200 663 (ch, frame0 < 0 ? 0 : frame0, frame1, *ranges, modelZoomLevel);
Chris@68 664
Chris@68 665 if (mergingChannels || mixingChannels) {
Chris@71 666 if (m_model->getChannelCount() > 1) {
Chris@200 667 if (!otherChannelRanges) {
Chris@200 668 otherChannelRanges =
Chris@200 669 new RangeSummarisableTimeValueModel::RangeBlock;
Chris@200 670 }
Chris@200 671 m_model->getRanges
Chris@200 672 (1, frame0 < 0 ? 0 : frame0, frame1, *otherChannelRanges,
Chris@200 673 modelZoomLevel);
Chris@71 674 } else {
Chris@200 675 if (otherChannelRanges != ranges) delete otherChannelRanges;
Chris@71 676 otherChannelRanges = ranges;
Chris@71 677 }
Chris@68 678 }
Chris@68 679
Chris@0 680 for (int x = x0; x <= x1; ++x) {
Chris@0 681
Chris@0 682 range = RangeSummarisableTimeValueModel::Range();
Chris@0 683 size_t index = x - x0;
Chris@0 684 size_t maxIndex = index;
Chris@0 685
Chris@0 686 if (frame0 < 0) {
Chris@0 687 if (index < size_t(-frame0 / zoomLevel)) {
Chris@0 688 continue;
Chris@0 689 } else {
Chris@0 690 index -= -frame0 / zoomLevel;
Chris@0 691 maxIndex = index;
Chris@0 692 }
Chris@0 693 }
Chris@0 694
Chris@0 695 if (int(modelZoomLevel) != zoomLevel) {
Chris@0 696
Chris@0 697 index = size_t((double(index) * zoomLevel) / modelZoomLevel);
Chris@0 698
Chris@0 699 if (int(modelZoomLevel) < zoomLevel) {
Chris@0 700 // Peaks may be missed! The model should avoid
Chris@0 701 // this by rounding zoom levels up rather than
Chris@0 702 // down, but we'd better cope in case it doesn't
Chris@0 703 maxIndex = index;
Chris@0 704 } else {
Chris@0 705 maxIndex = size_t((double(index + 1) * zoomLevel)
Chris@0 706 / modelZoomLevel) - 1;
Chris@0 707 }
Chris@0 708 }
Chris@0 709
Chris@200 710 if (ranges && index < ranges->size()) {
Chris@0 711
Chris@200 712 range = (*ranges)[index];
Chris@0 713
Chris@200 714 if (maxIndex > index && maxIndex < ranges->size()) {
Chris@200 715 range.max = std::max(range.max, (*ranges)[maxIndex].max);
Chris@200 716 range.min = std::min(range.min, (*ranges)[maxIndex].min);
Chris@0 717 range.absmean = (range.absmean +
Chris@200 718 (*ranges)[maxIndex].absmean) / 2;
Chris@0 719 }
Chris@0 720
Chris@0 721 } else {
Chris@0 722 continue;
Chris@0 723 }
Chris@0 724
Chris@0 725 int rangeBottom = 0, rangeTop = 0, meanBottom = 0, meanTop = 0;
Chris@0 726
Chris@0 727 if (mergingChannels) {
Chris@0 728
Chris@200 729 if (otherChannelRanges && index < otherChannelRanges->size()) {
Chris@0 730
Chris@0 731 range.max = fabsf(range.max);
Chris@200 732 range.min = -fabsf((*otherChannelRanges)[index].max);
Chris@0 733 range.absmean = (range.absmean +
Chris@200 734 (*otherChannelRanges)[index].absmean) / 2;
Chris@0 735
Chris@200 736 if (maxIndex > index && maxIndex < otherChannelRanges->size()) {
Chris@0 737 // let's not concern ourselves about the mean
Chris@0 738 range.min = std::min
Chris@0 739 (range.min,
Chris@200 740 -fabsf((*otherChannelRanges)[maxIndex].max));
Chris@0 741 }
Chris@0 742 }
Chris@67 743
Chris@67 744 } else if (mixingChannels) {
Chris@67 745
Chris@200 746 if (otherChannelRanges && index < otherChannelRanges->size()) {
Chris@67 747
Chris@200 748 range.max = (range.max + (*otherChannelRanges)[index].max) / 2;
Chris@200 749 range.min = (range.min + (*otherChannelRanges)[index].min) / 2;
Chris@200 750 range.absmean = (range.absmean + (*otherChannelRanges)[index].absmean) / 2;
Chris@67 751 }
Chris@67 752 }
Chris@0 753
Chris@0 754 int greyLevels = 1;
Chris@0 755 if (m_greyscale && (m_scale == LinearScale)) greyLevels = 4;
Chris@0 756
Chris@0 757 switch (m_scale) {
Chris@0 758
Chris@0 759 case LinearScale:
Chris@67 760 rangeBottom = int( m * greyLevels * range.min * gain);
Chris@67 761 rangeTop = int( m * greyLevels * range.max * gain);
Chris@67 762 meanBottom = int(-m * range.absmean * gain);
Chris@67 763 meanTop = int( m * range.absmean * gain);
Chris@0 764 break;
Chris@0 765
Chris@0 766 case dBScale:
Chris@67 767 if (!mergingChannels) {
Chris@67 768 int db0 = dBscale(range.min * gain, m);
Chris@67 769 int db1 = dBscale(range.max * gain, m);
Chris@67 770 rangeTop = std::max(db0, db1);
Chris@67 771 meanTop = std::min(db0, db1);
Chris@67 772 if (mixingChannels) rangeBottom = meanTop;
Chris@67 773 else rangeBottom = dBscale(range.absmean * gain, m);
Chris@67 774 meanBottom = rangeBottom;
Chris@67 775 } else {
Chris@67 776 rangeBottom = -dBscale(range.min * gain, m * greyLevels);
Chris@67 777 rangeTop = dBscale(range.max * gain, m * greyLevels);
Chris@67 778 meanBottom = -dBscale(range.absmean * gain, m);
Chris@67 779 meanTop = dBscale(range.absmean * gain, m);
Chris@67 780 }
Chris@0 781 break;
Chris@0 782
Chris@0 783 case MeterScale:
Chris@67 784 if (!mergingChannels) {
Chris@67 785 int r0 = abs(AudioLevel::multiplier_to_preview(range.min * gain, m));
Chris@67 786 int r1 = abs(AudioLevel::multiplier_to_preview(range.max * gain, m));
Chris@67 787 rangeTop = std::max(r0, r1);
Chris@67 788 meanTop = std::min(r0, r1);
Chris@67 789 if (mixingChannels) rangeBottom = meanTop;
Chris@67 790 else rangeBottom = AudioLevel::multiplier_to_preview(range.absmean * gain, m);
Chris@67 791 meanBottom = rangeBottom;
Chris@67 792 } else {
Chris@67 793 rangeBottom = AudioLevel::multiplier_to_preview(range.min * gain, m * greyLevels);
Chris@67 794 rangeTop = AudioLevel::multiplier_to_preview(range.max * gain, m * greyLevels);
Chris@67 795 meanBottom = -AudioLevel::multiplier_to_preview(range.absmean * gain, m);
Chris@67 796 meanTop = AudioLevel::multiplier_to_preview(range.absmean * gain, m);
Chris@67 797 }
Chris@67 798 break;
Chris@0 799 }
Chris@0 800
Chris@27 801 rangeBottom = my * greyLevels - rangeBottom;
Chris@27 802 rangeTop = my * greyLevels - rangeTop;
Chris@27 803 meanBottom = my - meanBottom;
Chris@27 804 meanTop = my - meanTop;
Chris@27 805
Chris@27 806 int topFill = (rangeTop % greyLevels);
Chris@27 807 if (topFill > 0) topFill = greyLevels - topFill;
Chris@27 808
Chris@27 809 int bottomFill = (rangeBottom % greyLevels);
Chris@27 810
Chris@0 811 rangeTop = rangeTop / greyLevels;
Chris@0 812 rangeBottom = rangeBottom / greyLevels;
Chris@0 813
Chris@0 814 bool clipped = false;
Chris@27 815
Chris@27 816 if (rangeTop < my - m) { rangeTop = my - m; }
Chris@27 817 if (rangeTop > my + m) { rangeTop = my + m; }
Chris@27 818 if (rangeBottom < my - m) { rangeBottom = my - m; }
Chris@27 819 if (rangeBottom > my + m) { rangeBottom = my + m; }
Chris@27 820
Chris@67 821 if (range.max <= -1.0 ||
Chris@67 822 range.max >= 1.0) clipped = true;
Chris@0 823
Chris@0 824 if (meanBottom > rangeBottom) meanBottom = rangeBottom;
Chris@0 825 if (meanTop < rangeTop) meanTop = rangeTop;
Chris@0 826
Chris@0 827 bool drawMean = m_showMeans;
Chris@0 828 if (meanTop == rangeTop) {
Chris@0 829 if (meanTop < meanBottom) ++meanTop;
Chris@0 830 else drawMean = false;
Chris@0 831 }
Chris@67 832 if (meanBottom == rangeBottom && m_scale == LinearScale) {
Chris@0 833 if (meanBottom > meanTop) --meanBottom;
Chris@0 834 else drawMean = false;
Chris@0 835 }
Chris@0 836
Chris@0 837 if (x != x0 && prevRangeBottom != -1) {
Chris@0 838 if (prevRangeBottom > rangeBottom &&
Chris@0 839 prevRangeTop > rangeBottom) {
Chris@28 840 // paint->setPen(midColour);
Chris@28 841 paint->setPen(m_colour);
Chris@0 842 paint->drawLine(x-1, prevRangeTop, x, rangeBottom);
Chris@28 843 paint->setPen(prevRangeTopColour);
Chris@0 844 paint->drawPoint(x-1, prevRangeTop);
Chris@0 845 } else if (prevRangeBottom < rangeTop &&
Chris@0 846 prevRangeTop < rangeTop) {
Chris@28 847 // paint->setPen(midColour);
Chris@28 848 paint->setPen(m_colour);
Chris@0 849 paint->drawLine(x-1, prevRangeBottom, x, rangeTop);
Chris@28 850 paint->setPen(prevRangeBottomColour);
Chris@0 851 paint->drawPoint(x-1, prevRangeBottom);
Chris@0 852 }
Chris@0 853 }
Chris@0 854
Chris@0 855 if (ready) {
Chris@67 856 if (clipped /*!!! ||
Chris@67 857 range.min * gain <= -1.0 ||
Chris@67 858 range.max * gain >= 1.0 */) {
Chris@0 859 paint->setPen(Qt::red);
Chris@0 860 } else {
Chris@0 861 paint->setPen(m_colour);
Chris@0 862 }
Chris@0 863 } else {
Chris@0 864 paint->setPen(midColour);
Chris@0 865 }
Chris@0 866
Chris@0 867 paint->drawLine(x, rangeBottom, x, rangeTop);
Chris@0 868
Chris@28 869 prevRangeTopColour = m_colour;
Chris@28 870 prevRangeBottomColour = m_colour;
Chris@28 871
Chris@0 872 if (m_greyscale && (m_scale == LinearScale) && ready) {
Chris@0 873 if (!clipped) {
Chris@0 874 if (rangeTop < rangeBottom) {
Chris@0 875 if (topFill > 0 &&
Chris@0 876 (!drawMean || (rangeTop < meanTop - 1))) {
Chris@0 877 paint->setPen(greys[topFill - 1]);
Chris@27 878 paint->drawPoint(x, rangeTop);
Chris@28 879 prevRangeTopColour = greys[topFill - 1];
Chris@0 880 }
Chris@0 881 if (bottomFill > 0 &&
Chris@0 882 (!drawMean || (rangeBottom > meanBottom + 1))) {
Chris@0 883 paint->setPen(greys[bottomFill - 1]);
Chris@27 884 paint->drawPoint(x, rangeBottom);
Chris@28 885 prevRangeBottomColour = greys[bottomFill - 1];
Chris@0 886 }
Chris@0 887 }
Chris@0 888 }
Chris@0 889 }
Chris@0 890
Chris@0 891 if (drawMean) {
Chris@0 892 paint->setPen(midColour);
Chris@0 893 paint->drawLine(x, meanBottom, x, meanTop);
Chris@0 894 }
Chris@0 895
Chris@0 896 prevRangeBottom = rangeBottom;
Chris@0 897 prevRangeTop = rangeTop;
Chris@0 898 }
Chris@0 899 }
Chris@0 900
Chris@0 901 if (m_aggressive) {
Chris@41 902
Chris@44 903 if (ready && rect == v->rect()) {
Chris@0 904 m_cacheValid = true;
Chris@0 905 m_cacheZoomLevel = zoomLevel;
Chris@0 906 }
Chris@0 907 paint->end();
Chris@0 908 delete paint;
Chris@0 909 viewPainter.drawPixmap(rect, *m_cache, rect);
Chris@0 910 }
Chris@200 911
Chris@200 912 if (otherChannelRanges != ranges) delete otherChannelRanges;
Chris@200 913 delete ranges;
Chris@0 914 }
Chris@0 915
Chris@25 916 QString
Chris@44 917 WaveformLayer::getFeatureDescription(View *v, QPoint &pos) const
Chris@25 918 {
Chris@25 919 int x = pos.x();
Chris@25 920
Chris@25 921 if (!m_model || !m_model->isOK()) return "";
Chris@25 922
Chris@44 923 long f0 = v->getFrameForX(x);
Chris@44 924 long f1 = v->getFrameForX(x + 1);
Chris@25 925
Chris@25 926 if (f0 < 0) f0 = 0;
Chris@25 927 if (f1 <= f0) return "";
Chris@25 928
Chris@25 929 QString text;
Chris@25 930
Chris@25 931 RealTime rt0 = RealTime::frame2RealTime(f0, m_model->getSampleRate());
Chris@25 932 RealTime rt1 = RealTime::frame2RealTime(f1, m_model->getSampleRate());
Chris@25 933
Chris@25 934 if (f1 != f0 + 1 && (rt0.sec != rt1.sec || rt0.msec() != rt1.msec())) {
Chris@25 935 text += tr("Time:\t%1 - %2")
Chris@25 936 .arg(rt0.toText(true).c_str())
Chris@25 937 .arg(rt1.toText(true).c_str());
Chris@25 938 } else {
Chris@25 939 text += tr("Time:\t%1")
Chris@25 940 .arg(rt0.toText(true).c_str());
Chris@25 941 }
Chris@25 942
Chris@25 943 size_t channels = 0, minChannel = 0, maxChannel = 0;
Chris@67 944 bool mergingChannels = false, mixingChannels = false;
Chris@25 945
Chris@67 946 channels = getChannelArrangement(minChannel, maxChannel,
Chris@67 947 mergingChannels, mixingChannels);
Chris@25 948 if (channels == 0) return "";
Chris@25 949
Chris@25 950 for (size_t ch = minChannel; ch <= maxChannel; ++ch) {
Chris@25 951
Chris@44 952 size_t blockSize = v->getZoomLevel();
Chris@200 953 RangeSummarisableTimeValueModel::RangeBlock ranges;
Chris@200 954 m_model->getRanges(ch, f0, f1, ranges, blockSize);
Chris@25 955
Chris@25 956 if (ranges.empty()) continue;
Chris@25 957
Chris@25 958 RangeSummarisableTimeValueModel::Range range = ranges[0];
Chris@25 959
Chris@25 960 QString label = tr("Level:");
Chris@25 961 if (minChannel != maxChannel) {
Chris@25 962 if (ch == 0) label = tr("Left:");
Chris@25 963 else if (ch == 1) label = tr("Right:");
Chris@25 964 else label = tr("Channel %1").arg(ch + 1);
Chris@25 965 }
Chris@25 966
Chris@76 967 bool singleValue = false;
Chris@76 968 float min, max;
Chris@76 969
Chris@76 970 if (fabs(range.min) < 0.01) {
Chris@76 971 min = range.min;
Chris@76 972 max = range.max;
Chris@76 973 singleValue = (min == max);
Chris@76 974 } else {
Chris@76 975 int imin = int(range.min * 1000);
Chris@76 976 int imax = int(range.max * 1000);
Chris@76 977 singleValue = (imin == imax);
Chris@76 978 min = float(imin)/1000;
Chris@76 979 max = float(imax)/1000;
Chris@76 980 }
Chris@76 981
Chris@25 982 int db = int(AudioLevel::multiplier_to_dB(std::max(fabsf(range.min),
Chris@25 983 fabsf(range.max)))
Chris@25 984 * 100);
Chris@25 985
Chris@76 986 if (!singleValue) {
Chris@25 987 text += tr("\n%1\t%2 - %3 (%4 dB peak)")
Chris@76 988 .arg(label).arg(min).arg(max).arg(float(db)/100);
Chris@25 989 } else {
Chris@25 990 text += tr("\n%1\t%2 (%3 dB peak)")
Chris@76 991 .arg(label).arg(min).arg(float(db)/100);
Chris@25 992 }
Chris@25 993 }
Chris@25 994
Chris@25 995 return text;
Chris@25 996 }
Chris@25 997
Chris@0 998 int
Chris@68 999 WaveformLayer::getYForValue(View *v, Scale scale, float value, size_t channel,
Chris@68 1000 size_t minChannel, size_t maxChannel) const
Chris@68 1001 {
Chris@68 1002 if (maxChannel < minChannel || channel < minChannel) return 0;
Chris@68 1003
Chris@68 1004 int h = v->height();
Chris@68 1005
Chris@68 1006 int channels = maxChannel - minChannel + 1;
Chris@68 1007 int m = (h / channels) / 2;
Chris@68 1008 int my = m + (((channel - minChannel) * h) / channels);
Chris@68 1009
Chris@68 1010 if ((m_scale == dBScale || m_scale == MeterScale) &&
Chris@68 1011 m_channelMode != MergeChannels) {
Chris@68 1012 m = (h / channels);
Chris@68 1013 my = m + (((channel - minChannel) * h) / channels);
Chris@68 1014 }
Chris@68 1015
Chris@68 1016 int vy = 0;
Chris@68 1017
Chris@68 1018 switch (scale) {
Chris@68 1019
Chris@68 1020 case LinearScale:
Chris@68 1021 vy = int(m * value);
Chris@68 1022 break;
Chris@68 1023
Chris@68 1024 case MeterScale:
Chris@68 1025 vy = AudioLevel::multiplier_to_preview(value, m);
Chris@68 1026 break;
Chris@68 1027
Chris@68 1028 case dBScale:
Chris@68 1029 vy = dBscale(value, m);
Chris@68 1030 break;
Chris@68 1031 }
Chris@68 1032
Chris@68 1033 return my - vy;
Chris@68 1034 }
Chris@68 1035
Chris@68 1036 int
Chris@248 1037 WaveformLayer::getVerticalScaleWidth(View *, QPainter &paint) const
Chris@0 1038 {
Chris@0 1039 if (m_scale == LinearScale) {
Chris@0 1040 return paint.fontMetrics().width("0.0") + 13;
Chris@0 1041 } else {
Chris@0 1042 return std::max(paint.fontMetrics().width(tr("0dB")),
Chris@0 1043 paint.fontMetrics().width(tr("-Inf"))) + 13;
Chris@0 1044 }
Chris@0 1045 }
Chris@0 1046
Chris@0 1047 void
Chris@44 1048 WaveformLayer::paintVerticalScale(View *v, QPainter &paint, QRect rect) const
Chris@0 1049 {
Chris@0 1050 if (!m_model || !m_model->isOK()) {
Chris@0 1051 return;
Chris@0 1052 }
Chris@0 1053
Chris@0 1054 size_t channels = 0, minChannel = 0, maxChannel = 0;
Chris@67 1055 bool mergingChannels = false, mixingChannels = false;
Chris@0 1056
Chris@67 1057 channels = getChannelArrangement(minChannel, maxChannel,
Chris@67 1058 mergingChannels, mixingChannels);
Chris@0 1059 if (channels == 0) return;
Chris@0 1060
Chris@0 1061 int h = rect.height(), w = rect.width();
Chris@0 1062 int textHeight = paint.fontMetrics().height();
Chris@0 1063 int toff = -textHeight/2 + paint.fontMetrics().ascent() + 1;
Chris@0 1064
Chris@67 1065 float gain = m_gain;
Chris@67 1066
Chris@0 1067 for (size_t ch = minChannel; ch <= maxChannel; ++ch) {
Chris@0 1068
Chris@68 1069 int lastLabelledY = -1;
Chris@0 1070
Chris@67 1071 if (ch < m_effectiveGains.size()) gain = m_effectiveGains[ch];
Chris@67 1072
Chris@68 1073 int n = 10;
Chris@0 1074
Chris@68 1075 for (int i = 0; i <= n; ++i) {
Chris@68 1076
Chris@68 1077 float val = 0.0, nval = 0.0;
Chris@0 1078 QString text = "";
Chris@0 1079
Chris@68 1080 switch (m_scale) {
Chris@68 1081
Chris@68 1082 case LinearScale:
Chris@68 1083 val = (i * gain) / n;
Chris@68 1084 text = QString("%1").arg(float(i) / n);
Chris@68 1085 if (i == 0) text = "0.0";
Chris@68 1086 else {
Chris@68 1087 nval = -val;
Chris@68 1088 if (i == n) text = "1.0";
Chris@68 1089 }
Chris@68 1090 break;
Chris@0 1091
Chris@68 1092 case MeterScale:
Chris@68 1093 val = AudioLevel::dB_to_multiplier(meterdbs[i]) * gain;
Chris@68 1094 text = QString("%1").arg(meterdbs[i]);
Chris@68 1095 if (i == n) text = tr("0dB");
Chris@68 1096 if (i == 0) {
Chris@68 1097 text = tr("-Inf");
Chris@68 1098 val = 0.0;
Chris@68 1099 }
Chris@68 1100 break;
Chris@0 1101
Chris@68 1102 case dBScale:
Chris@68 1103 val = AudioLevel::dB_to_multiplier(-(10*n) + i * 10) * gain;
Chris@68 1104 text = QString("%1").arg(-(10*n) + i * 10);
Chris@68 1105 if (i == n) text = tr("0dB");
Chris@68 1106 if (i == 0) {
Chris@68 1107 text = tr("-Inf");
Chris@68 1108 val = 0.0;
Chris@68 1109 }
Chris@68 1110 break;
Chris@68 1111 }
Chris@0 1112
Chris@68 1113 if (val < -1.0 || val > 1.0) continue;
Chris@0 1114
Chris@68 1115 int y = getYForValue(v, m_scale, val, ch, minChannel, maxChannel);
Chris@0 1116
Chris@68 1117 int ny = y;
Chris@68 1118 if (nval != 0.0) {
Chris@68 1119 ny = getYForValue(v, m_scale, nval, ch, minChannel, maxChannel);
Chris@68 1120 }
Chris@0 1121
Chris@68 1122 bool spaceForLabel = (i == 0 ||
Chris@68 1123 abs(y - lastLabelledY) >= textHeight - 1);
Chris@0 1124
Chris@68 1125 if (spaceForLabel) {
Chris@0 1126
Chris@68 1127 int tx = 3;
Chris@68 1128 if (m_scale != LinearScale) {
Chris@68 1129 tx = w - 10 - paint.fontMetrics().width(text);
Chris@68 1130 }
Chris@68 1131
Chris@68 1132 int ty = y;
Chris@68 1133 if (ty < paint.fontMetrics().ascent()) {
Chris@68 1134 ty = paint.fontMetrics().ascent();
Chris@68 1135 } else if (ty > h - paint.fontMetrics().descent()) {
Chris@68 1136 ty = h - paint.fontMetrics().descent();
Chris@68 1137 } else {
Chris@68 1138 ty += toff;
Chris@68 1139 }
Chris@68 1140 paint.drawText(tx, ty, text);
Chris@0 1141
Chris@68 1142 lastLabelledY = ty - toff;
Chris@67 1143
Chris@68 1144 if (ny != y) {
Chris@68 1145 ty = ny;
Chris@68 1146 if (ty < paint.fontMetrics().ascent()) {
Chris@68 1147 ty = paint.fontMetrics().ascent();
Chris@68 1148 } else if (ty > h - paint.fontMetrics().descent()) {
Chris@68 1149 ty = h - paint.fontMetrics().descent();
Chris@68 1150 } else {
Chris@68 1151 ty += toff;
Chris@68 1152 }
Chris@68 1153 paint.drawText(tx, ty, text);
Chris@68 1154 }
Chris@68 1155
Chris@68 1156 paint.drawLine(w - 7, y, w, y);
Chris@68 1157 if (ny != y) paint.drawLine(w - 7, ny, w, ny);
Chris@68 1158
Chris@68 1159 } else {
Chris@68 1160
Chris@68 1161 paint.drawLine(w - 4, y, w, y);
Chris@68 1162 if (ny != y) paint.drawLine(w - 4, ny, w, ny);
Chris@68 1163 }
Chris@0 1164 }
Chris@0 1165 }
Chris@0 1166 }
Chris@0 1167
Chris@6 1168 QString
Chris@6 1169 WaveformLayer::toXmlString(QString indent, QString extraAttributes) const
Chris@6 1170 {
Chris@6 1171 QString s;
Chris@6 1172
Chris@6 1173 s += QString("gain=\"%1\" "
Chris@6 1174 "colour=\"%2\" "
Chris@6 1175 "showMeans=\"%3\" "
Chris@6 1176 "greyscale=\"%4\" "
Chris@6 1177 "channelMode=\"%5\" "
Chris@6 1178 "channel=\"%6\" "
Chris@6 1179 "scale=\"%7\" "
Chris@67 1180 "aggressive=\"%8\" "
Chris@67 1181 "autoNormalize=\"%9\"")
Chris@6 1182 .arg(m_gain)
Chris@6 1183 .arg(encodeColour(m_colour))
Chris@6 1184 .arg(m_showMeans)
Chris@6 1185 .arg(m_greyscale)
Chris@6 1186 .arg(m_channelMode)
Chris@6 1187 .arg(m_channel)
Chris@6 1188 .arg(m_scale)
Chris@67 1189 .arg(m_aggressive)
Chris@67 1190 .arg(m_autoNormalize);
Chris@6 1191
Chris@6 1192 return Layer::toXmlString(indent, extraAttributes + " " + s);
Chris@6 1193 }
Chris@6 1194
Chris@11 1195 void
Chris@11 1196 WaveformLayer::setProperties(const QXmlAttributes &attributes)
Chris@11 1197 {
Chris@11 1198 bool ok = false;
Chris@11 1199
Chris@11 1200 float gain = attributes.value("gain").toFloat(&ok);
Chris@11 1201 if (ok) setGain(gain);
Chris@11 1202
Chris@11 1203 QString colourSpec = attributes.value("colour");
Chris@11 1204 if (colourSpec != "") {
Chris@11 1205 QColor colour(colourSpec);
Chris@11 1206 if (colour.isValid()) {
Chris@11 1207 setBaseColour(QColor(colourSpec));
Chris@11 1208 }
Chris@11 1209 }
Chris@11 1210
Chris@11 1211 bool showMeans = (attributes.value("showMeans") == "1" ||
Chris@11 1212 attributes.value("showMeans") == "true");
Chris@11 1213 setShowMeans(showMeans);
Chris@11 1214
Chris@11 1215 bool greyscale = (attributes.value("greyscale") == "1" ||
Chris@11 1216 attributes.value("greyscale") == "true");
Chris@11 1217 setUseGreyscale(greyscale);
Chris@11 1218
Chris@11 1219 ChannelMode channelMode = (ChannelMode)
Chris@11 1220 attributes.value("channelMode").toInt(&ok);
Chris@11 1221 if (ok) setChannelMode(channelMode);
Chris@11 1222
Chris@11 1223 int channel = attributes.value("channel").toInt(&ok);
Chris@11 1224 if (ok) setChannel(channel);
Chris@11 1225
Chris@11 1226 Scale scale = (Scale)
Chris@11 1227 attributes.value("scale").toInt(&ok);
Chris@11 1228 if (ok) setScale(scale);
Chris@11 1229
Chris@11 1230 bool aggressive = (attributes.value("aggressive") == "1" ||
Chris@11 1231 attributes.value("aggressive") == "true");
Chris@11 1232 setUseGreyscale(aggressive);
Chris@67 1233
Chris@67 1234 bool autoNormalize = (attributes.value("autoNormalize") == "1" ||
Chris@67 1235 attributes.value("autoNormalize") == "true");
Chris@67 1236 setAutoNormalize(autoNormalize);
Chris@11 1237 }
Chris@11 1238
Chris@133 1239 int
Chris@133 1240 WaveformLayer::getVerticalZoomSteps(int &defaultStep) const
Chris@133 1241 {
Chris@133 1242 defaultStep = 50;
Chris@133 1243 return 100;
Chris@133 1244 }
Chris@0 1245
Chris@133 1246 int
Chris@133 1247 WaveformLayer::getCurrentVerticalZoomStep() const
Chris@133 1248 {
Chris@133 1249 int val = lrint(log10(m_gain) * 20.0) + 50;
Chris@133 1250 if (val < 0) val = 0;
Chris@133 1251 if (val > 100) val = 100;
Chris@133 1252 return val;
Chris@133 1253 }
Chris@133 1254
Chris@133 1255 void
Chris@133 1256 WaveformLayer::setVerticalZoomStep(int step)
Chris@133 1257 {
Chris@133 1258 setGain(pow(10, float(step - 50) / 20.0));
Chris@133 1259 }
Chris@133 1260