1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847
|
/***************************************************************************
TrackPixmap.cpp - buffered pixmap for displaying a kwave track
-------------------
begin : Tue Mar 20 2001
copyright : (C) 2001 by Thomas Eschenbacher
email : Thomas.Eschenbacher@gmx.de
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include <new>
#include <QMutexLocker>
#include <QPainter>
#include <QPolygon>
#include <QTime>
#include "libkwave/SampleReader.h"
#include "libkwave/Track.h"
#include "libgui/TrackPixmap.h"
/**
* This factor determines how many times the order is over than
* the minimum required order. Higher values give less problems with
* aliasing but some amplitude errors. Lower values make less
* amplitude errors but more aliasing problems.
* This should be a good compromise...
*/
#define INTERPOLATION_PRECISION 4
/**
* If the zoom factor is below this margin, an interpolation
* will be used.
*/
#define INTERPOLATION_ZOOM 0.10
//***************************************************************************
Kwave::TrackPixmap::TrackPixmap(Kwave::Track &track)
:QObject(), m_pixmap(), m_track(track), m_offset(0), m_zoom(0.0),
m_vertical_zoom(1.0), m_minmax_mode(false),
m_sample_buffer(), m_min_buffer(), m_max_buffer(),
m_modified(false), m_valid(0), m_lock_buffer(),
m_interpolation_order(0), m_interpolation_alpha(),
m_colors(Kwave::Colors::Normal)
{
// connect all the notification signals of the track
connect(&track,
SIGNAL(sigSamplesInserted(Kwave::Track *, sample_index_t,
sample_index_t)), this,
SLOT(slotSamplesInserted(Kwave::Track *,
sample_index_t, sample_index_t)));
connect(&track, SIGNAL(sigSamplesDeleted(Kwave::Track *, sample_index_t,
sample_index_t)), this, SLOT(slotSamplesDeleted(Kwave::Track *,
sample_index_t, sample_index_t)));
connect(&track, SIGNAL(sigSamplesModified(Kwave::Track *, sample_index_t,
sample_index_t)), this, SLOT(slotSamplesModified(Kwave::Track *,
sample_index_t, sample_index_t)));
connect(&track, SIGNAL(sigSelectionChanged(bool)),
this, SLOT(selectionChanged()));
}
//***************************************************************************
Kwave::TrackPixmap::~TrackPixmap()
{
QMutexLocker lock(&m_lock_buffer);
m_interpolation_alpha.clear();
}
//***************************************************************************
void Kwave::TrackPixmap::setOffset(sample_index_t offset)
{
QMutexLocker lock(&m_lock_buffer);
if (offset == m_offset) return; // no change
const unsigned int buflen = static_cast<unsigned int>(m_valid.size());
if (m_minmax_mode) {
// move content of min and max buffer
// one buffer element = one screen pixel
Q_ASSERT(buflen == m_min_buffer.size());
Q_ASSERT(buflen == m_max_buffer.size());
if ((buflen != m_min_buffer.size()) ||
(buflen != m_max_buffer.size()))
{
qDebug("TrackPixmap::setOffset(): buflen = %u", buflen);
qDebug("TrackPixmap::setOffset(): min_buffer : %u",
m_min_buffer.size());
qDebug("TrackPixmap::setOffset(): max_buffer : %u",
m_max_buffer.size());
}
// check for misaligned offset changes
if (( offset % static_cast<sample_index_t>(ceil(m_zoom))) !=
(m_offset % static_cast<sample_index_t>(ceil(m_zoom)))) {
#ifdef CURRENTLY_UNUSED
// this will become interesting later, with the offset/zoom optimizations
qWarning("TrackPixmap::setOffset(): oh nooo, "\
"offset %u is misaligned by %u sample(s), please fix this!",
offset, offset % pixels2samples(1));
qWarning("TrackPixmap::setOffset(): "\
"now I have to throw away the whole buffer :-((");
qDebug("TrackPixmap::setOffset(%u): "\
"misaligned->invalidating buffer", offset);
#endif
invalidateBuffer();
} else if (offset > m_offset) {
// move left
int diff = samples2pixels(offset - m_offset);
// qDebug("TrackPixmap::setOffset(): moving left (min/max): %u",diff);
Q_ASSERT(diff);
Q_ASSERT(buflen);
if (diff && buflen) {
unsigned int src = Kwave::toUint(diff);
unsigned int dst = 0;
while (src < buflen) {
m_min_buffer[dst] = m_min_buffer[src];
m_max_buffer[dst] = m_max_buffer[src];
m_valid[dst++] = m_valid[src++];
}
while (dst < buflen) m_valid.clearBit(dst++);
}
} else {
// move right
int diff = Kwave::toInt(samples2pixels(m_offset - offset));
// qDebug("TrackPixmap::setOffset(): moving right (min/max): %u",diff);
Q_ASSERT(diff);
Q_ASSERT(buflen);
if (diff && buflen) {
int dst = buflen - 1;
while (dst >= diff) {
int src = dst - diff;
m_min_buffer[dst] = m_min_buffer[src];
m_max_buffer[dst] = m_max_buffer[src];
m_valid[dst--] = m_valid[src--];
}
diff = dst + 1;
while (diff--) m_valid.clearBit(dst--);
}
}
} else {
// move content of sample buffer
// one buffer element = one sample
Q_ASSERT(buflen == m_sample_buffer.size());
if (offset > m_offset) {
// move left
// qDebug("TrackPixmap::setOffset(): moving left (normal)");
unsigned int diff = Kwave::toUint(offset - m_offset);
unsigned int src = Kwave::toUint(diff);
unsigned int dst = 0;
while (src < buflen) {
m_sample_buffer[dst] = m_sample_buffer[src];
m_valid[dst++] = m_valid[src++];
}
while (dst < buflen) m_valid.clearBit(dst++);
} else {
// move right
// qDebug("TrackPixmap::setOffset(): moving right (normal)");
int diff = Kwave::toInt(m_offset - offset);
Q_ASSERT(buflen);
if (buflen) {
int dst = buflen - 1;
while (dst >= diff) {
int src = dst - diff;
m_sample_buffer[dst] = m_sample_buffer[src];
m_valid[dst--] = m_valid[src];
}
diff = dst + 1;
while (diff--) m_valid.clearBit(dst--);
}
}
}
m_offset = offset;
m_modified = true;
}
//***************************************************************************
void Kwave::TrackPixmap::resizeBuffer()
{
bool ok = true;
int buflen;
int oldlen = static_cast<int>(m_valid.size());
int w = width();
Q_ASSERT(w >= 0);
if (m_minmax_mode) {
// one buffer index == one screen pixel
buflen = w;
ok &= m_min_buffer.resize(buflen);
Q_ASSERT(ok);
ok &= m_max_buffer.resize(buflen);
Q_ASSERT(ok);
} else {
// one buffer index == one sample
buflen = Kwave::toInt(pixels2samples(w));
ok &= m_sample_buffer.resize(buflen);
Q_ASSERT(ok);
}
m_valid.resize(buflen);
while (oldlen < buflen) m_valid.clearBit(oldlen++);
}
//***************************************************************************
void Kwave::TrackPixmap::setZoom(double zoom)
{
QMutexLocker lock(&m_lock_buffer);
Q_ASSERT(zoom >= 0.0);
if (qFuzzyCompare(zoom, m_zoom)) return; // no change
// qDebug("TrackPixmap::setZoom(%0.3f)", zoom);
if ((zoom > 1.0) && !m_minmax_mode) {
// switch to min/max mode
// qDebug("TrackPixmap::setZoom(): switch to min/max mode");
invalidateBuffer();
m_minmax_mode = true;
} else if ((zoom <= 1.0) && m_minmax_mode) {
// switch to normal mode
// qDebug("TrackPixmap::setZoom(): switch to normal mode");
invalidateBuffer();
m_minmax_mode = false;
}
// take the new zoom and resize the buffer
m_zoom = zoom;
if (m_minmax_mode) {
// TODO: some clever caching instead of throwing away everything
resizeBuffer();
invalidateBuffer();
} else {
resizeBuffer();
}
m_modified = true;
}
//***************************************************************************
void Kwave::TrackPixmap::resize(int width, int height)
{
QMutexLocker lock(&m_lock_buffer);
int old_width = m_pixmap.width();
int old_height = m_pixmap.height();
if ((old_width == width) && (old_height == height)) return; // no change
m_pixmap = QPixmap(width, height);
if (width != old_width) resizeBuffer();
m_modified = true;
}
//***************************************************************************
void Kwave::TrackPixmap::invalidateBuffer()
{
m_valid.fill(false);
m_modified = true;
}
//***************************************************************************
bool Kwave::TrackPixmap::validateBuffer()
{
int first = 0;
int last = 0;
int buflen = static_cast<int>(m_valid.size());
sample_index_t left = m_offset;
sample_index_t right = (m_track.length()) ? (m_track.length() - 1) : 0;
Kwave::SampleReader *reader = m_track.openReader(
Kwave::SinglePassForward, left, right);
Q_ASSERT(reader);
if (!reader) return false;
if (m_minmax_mode) {
Q_ASSERT(Kwave::toInt(m_min_buffer.size()) == buflen);
Q_ASSERT(Kwave::toInt(m_max_buffer.size()) == buflen);
} else {
Q_ASSERT(Kwave::toInt(m_sample_buffer.size()) == buflen);
}
// work-around for missing extra buffer, delete the whole buffer
// instead. this should not do any harm, in this mode we only
// have few samples and redrawing will be fast
if (m_zoom < INTERPOLATION_ZOOM) invalidateBuffer();
while (first < buflen) {
// find the first invalid index
for (first = last; (first < buflen) && m_valid.testBit(first);
++first)
{}
// break if the first index is out of range
if (first >= buflen) {
delete reader;
return false; // buffer is already ok
}
// find the last invalid index
for (last = first; (last < buflen) && !m_valid[last]; ++last) {
}
if (last >= buflen) last = buflen - 1;
if ((last > first) && (m_valid[last])) --last;
// fill our array(s) with fresh sample data
if (m_minmax_mode) {
// indices are in pixels, convert to samples
// first sample in first pixel
sample_index_t s1 = m_offset +
static_cast<sample_index_t>(floor(first * m_zoom));
// last sample of last pixel
sample_index_t s2 = m_offset +
static_cast<sample_index_t>(floor((last+1) * m_zoom)) - 1;
while (first <= last) {
s2 = m_offset + static_cast<sample_index_t>(
floor((first + 1) * m_zoom));
// get min/max for interval [s1...s2]
sample_t min;
sample_t max;
reader->minMax(s1, s2, min, max);
m_min_buffer[first] = min;
m_max_buffer[first] = max;
m_valid.setBit(first);
// advance to the next position
++first;
s1 = s2 + 1;
}
} else {
// each index is one sample
// -> read directly into the buffer
reader->seek(m_offset + first);
unsigned int count = reader->read(m_sample_buffer,
first, last - first + 1);
while (count) {
m_valid.setBit(first++);
count--;
}
// fill the rest with zeroes
while (first <= last) {
m_valid.setBit(first);
m_sample_buffer[first++] = 0;
}
}
Q_ASSERT(first >= last);
++last;
}
#ifdef DEBUG
for (first = 0; first < m_valid.size(); first++) {
if (!m_valid[first]) qWarning("TrackPixmap::validateBuffer(): "\
"still invalid index: %u", first);
}
#endif /* DEBUG */
delete reader;
return true;
}
//***************************************************************************
void Kwave::TrackPixmap::repaint()
{
QMutexLocker lock(&m_lock_buffer);
int w = width();
int h = height();
if (!w || !h) return; // not valid yet
if (m_track.selected()) {
m_colors = Kwave::Colors::Normal;
} else {
m_colors = Kwave::Colors::Disabled;
}
QPainter p(&m_pixmap);
p.fillRect(0, 0, w, h, m_colors.background);
if (m_zoom > 0) {
// first make the buffer valid
validateBuffer();
// then draw the samples
if (m_minmax_mode) {
drawOverview(p, h >> 1, h, 0, w - 1);
} else {
if (m_zoom < INTERPOLATION_ZOOM) {
drawInterpolatedSignal(p, w, h >> 1, h);
} else {
drawPolyLineSignal(p, w, h >> 1, h);
}
}
// draw the zero-line
int last = (m_track.length() > m_offset) ?
samples2pixels(m_track.length() - 1 - m_offset) : 0;
p.setPen(m_colors.zero);
if (last >= w) {
p.drawLine(0, h >> 1, w - 1, h >> 1);
} else {
p.drawLine(0, h >> 1, last, h >> 1);
p.setPen(m_colors.zero_unused);
p.drawLine(last, h >> 1, w, h >> 1);
}
}
// now we are no longer "modified"
m_modified = false;
}
//***************************************************************************
void Kwave::TrackPixmap::setVerticalZoom(double zoom)
{
QMutexLocker lock(&m_lock_buffer);
if (qFuzzyCompare(zoom, m_vertical_zoom)) return;
m_vertical_zoom = zoom;
m_modified = true;
}
//***************************************************************************
bool Kwave::TrackPixmap::isModified()
{
QMutexLocker lock(&m_lock_buffer);
return m_modified;
}
//***************************************************************************
void Kwave::TrackPixmap::selectionChanged()
{
QMutexLocker lock(&m_lock_buffer);
m_modified = true;
}
//***************************************************************************
void Kwave::TrackPixmap::drawOverview(QPainter &p, int middle, int height,
int first, int last)
{
const Kwave::SampleArray &min_buffer = m_min_buffer;
const Kwave::SampleArray &max_buffer = m_max_buffer;
Q_ASSERT(m_minmax_mode);
Q_ASSERT(width() <= Kwave::toInt(min_buffer.size()));
Q_ASSERT(width() <= Kwave::toInt(max_buffer.size()));
// scale_y: pixels per unit
double scale_y = (m_vertical_zoom * height) / (1 << SAMPLE_BITS);
p.setPen(m_colors.sample);
int last_min = Kwave::toInt(min_buffer[first] * scale_y);
int last_max = Kwave::toInt(max_buffer[first] * scale_y);
for (int i = first; i <= last; i++) {
Q_ASSERT(m_valid[i]);
int max = Kwave::toInt(max_buffer[i] * scale_y);
int min = Kwave::toInt(min_buffer[i] * scale_y);
// make sure there is a connection between this
// section and the one before, avoid gaps
if (min > last_max + 1) min = last_max + 1;
if (max + 1 < last_min) max = last_min - 1;
p.drawLine(i, middle - max, i, middle - min);
last_min = min;
last_max = max;
}
}
//***************************************************************************
void Kwave::TrackPixmap::calculateInterpolation()
{
double f;
double Fg;
int k;
int N;
// qDebug("TrackPixmap::calculateInterpolation()");
// remove all previous coefficents and signal buffer
m_interpolation_alpha.clear();
Q_ASSERT(!qFuzzyIsNull(m_zoom));
if (qFuzzyIsNull(m_zoom)) return;
// offset: index of first visible sample (left) [0...length-1]
// m_zoom: number of samples / pixel
// approximate the 3dB frequency of the low pass as
// Fg = f_g / f_a
// f_a: current "sample rate" of display (pixels) = 1.0
// f_g: signal rate = (m_zoom/2)
Fg = m_zoom / 2;
// N: order of the filter, at least 2 * (1 / m_zoom)
N = samples2pixels(INTERPOLATION_PRECISION);
N |= 0x01; // make N an odd number !
// allocate a buffer for the coefficients
m_interpolation_alpha = QVector<double>(N + 1);
m_interpolation_order = N;
Q_ASSERT(m_interpolation_alpha.count() == (N + 1));
if (m_interpolation_alpha.count() != (N + 1)) return;
// calculate the raw coefficients and
// apply a Hamming window
//
// sin( (2k-N) * Pi * Fg ) 2kPi
// alpha_k = 2 * Fg * ----------------------- * [ 0,54 - 0,46 * cos ---- ]
// (2k - N) * Pi * Fg N
//
f = 0.0; // (store the sum of all coefficients in "f")
for (k = 0; k <= N; ++k) {
m_interpolation_alpha[k] =
sin((2 * k - N) * M_PI * Fg) / ((2 * k - N) * M_PI * Fg);
m_interpolation_alpha[k] *= (0.54 - 0.46 * cos(2 * k * M_PI / N));
f += m_interpolation_alpha[k];
}
// norm the coefficients to 1.0 / m_zoom
f *= m_zoom;
for (k = 0; k <= N; ++k)
m_interpolation_alpha[k] /= f;
}
//***************************************************************************
void Kwave::TrackPixmap::drawInterpolatedSignal(QPainter &p, int width,
int middle, int height)
{
float *sig;
float *sig_buffer;
float scale_y;
int i;
int k;
int N;
int sample;
int x;
int buflen = static_cast<int>(m_valid.size());
const Kwave::SampleArray &sample_buffer = m_sample_buffer;
// qDebug("TrackPixmap::drawInterpolatedSignal()");
Q_ASSERT(m_zoom > 0);
if (m_zoom <= 0) return;
// scale_y: pixels per unit
scale_y = static_cast<float>(m_vertical_zoom * height) /
static_cast<float>((SAMPLE_MAX + 1) << 1);
// N: order of the filter, at least 2 * (1/m_zoom)
N = samples2pixels(INTERPOLATION_PRECISION);
N |= 0x01; // make N an odd number !
// re-calculate the interpolation's filter and buffers
// if the current order has changed
if (m_interpolation_order != N) {
calculateInterpolation();
N = m_interpolation_order;
}
Q_ASSERT(m_interpolation_alpha.count() == (N + 1));
if (m_interpolation_alpha.count() != (N + 1)) return;
// buffer for intermediate resampled data
sig_buffer = new(std::nothrow) float[width + N + 2];
Q_ASSERT(sig_buffer);
if (!sig_buffer) return;
// fill the sample buffer with zeroes
for (i = 0; i < width + N + 2; ++i)
sig_buffer[i] = 0.0;
// resample
x = -1 * samples2pixels(2);
sample = -2; // start some samples left of the window
sig = sig_buffer + (N / 2);
while (x <= width + N / 2) {
if ((x >= -N / 2) && (sample > 0) && (sample < buflen)) {
sig[x] = static_cast<float>(sample_buffer[sample]) * scale_y;
}
sample++;
x = Q_LIKELY(sample >= 0) ?
samples2pixels(sample) :
(-1 * samples2pixels(-1 * sample));
}
// array with sample points
QPolygon points;
// pass the signal data through the filter
for (i = 0; i < width; ++i) {
sig = sig_buffer + (i + N);
float y = 0.0;
for (k = 0; k <= N; ++k, --sig)
y += (*sig) * static_cast<float>(m_interpolation_alpha[k]);
points.append(QPoint(i, middle - Kwave::toInt(y)));
}
// display the filter's interpolated output
p.setPen(m_colors.interpolated);
p.drawPolyline(points);
// display the original samples
sample = 0;
x = samples2pixels(sample);
sig = sig_buffer + (N / 2);
p.setPen(m_colors.sample);
i = 0;
points.clear();
while (x < width) {
if (x >= 0) {
// mark original samples
points.append(QPoint(x, middle - Kwave::toInt(sig[x])));
}
sample++;
x = samples2pixels(sample);
}
p.drawPoints(points);
delete[] sig_buffer;
}
//***************************************************************************
void Kwave::TrackPixmap::drawPolyLineSignal(QPainter &p, int width,
int middle, int height)
{
const Kwave::SampleArray &sample_buffer = m_sample_buffer;
double scale_y;
unsigned int sample;
unsigned int buflen = sample_buffer.size();
// scale_y: pixels per unit
scale_y = (m_vertical_zoom * static_cast<double>(height)) /
static_cast<double>((SAMPLE_MAX + 1) << 1);
// array with sample points
QPolygon points;
// display the original samples
sample = 0;
int x = 0;
int y;
while (x < width) {
// mark original samples
sample_t value = (sample < buflen) ? sample_buffer[sample] : 0;
y = Kwave::toInt(value * scale_y);
points.append(QPoint(x, middle - y));
sample++;
x = samples2pixels(sample);
}
// interpolate the rest of the display if necessary
if (samples2pixels(sample - 1) <= width) {
int x1;
int x2;
double y1;
double y2;
x1 = samples2pixels(sample - 1);
x2 = samples2pixels(sample);
y1 = ((sample) && (sample <= buflen)) ? (scale_y *
static_cast<double>(sample_buffer[sample - 1])) : 0.0;
y2 = (sample < buflen) ? (scale_y *
static_cast<double>(sample_buffer[sample ])) : 0.0;
x = width - 1;
y = Kwave::toInt(static_cast<double>(x - x1) * (y2 - y1) /
static_cast<double>(x2 - x1));
points.append(QPoint(x, middle - y));
}
if (m_zoom >= 1.0) {
// show only poly-line (bright)
p.setPen(Qt::white);
p.drawPolyline(points);
} else {
// show the poly-line (dark)
p.setPen(Qt::darkGray);
p.drawPolyline(points);
// show the original points (bright)
p.setPen(Qt::white);
p.drawPoints(points);
}
}
//***************************************************************************
void Kwave::TrackPixmap::slotSamplesInserted(Kwave::Track *,
sample_index_t offset,
sample_index_t length)
{
{
QMutexLocker lock(&m_lock_buffer);
convertOverlap(offset, length);
if (!length) return; // false alarm
// mark all positions from here to right end as "invalid"
const qsizetype first = offset;
const qsizetype last = m_valid.size();
Q_ASSERT(first < m_valid.size());
Q_ASSERT(last > first);
m_valid.fill(false, first, last);
// repaint of the signal is needed
m_modified = true;
}
// notify our owner about changed data -> screen refresh?
emit sigModified();
}
//***************************************************************************
void Kwave::TrackPixmap::slotSamplesDeleted(Kwave::Track *,
sample_index_t offset,
sample_index_t length)
{
{
QMutexLocker lock(&m_lock_buffer);
convertOverlap(offset, length);
if (!length) return; // false alarm
// mark all positions from here to right end as "invalid"
const qsizetype first = offset;
const qsizetype last = m_valid.size();
Q_ASSERT(first < m_valid.size());
Q_ASSERT(last > first);
m_valid.fill(false, first, last);
// repaint of the signal is needed
m_modified = true;
}
// notify our owner about changed data -> screen refresh?
emit sigModified();
}
//***************************************************************************
void Kwave::TrackPixmap::slotSamplesModified(Kwave::Track *,
sample_index_t offset,
sample_index_t length)
{
{
QMutexLocker lock(&m_lock_buffer);
convertOverlap(offset, length);
if (!length) return; // false alarm
// mark all overlapping positions as "invalid"
const int first = Kwave::toInt(offset);
const int last = Kwave::toInt(offset + length);
Q_ASSERT(first < m_valid.size());
Q_ASSERT(last > first);
m_valid.fill(false, first, last);
// repaint of the signal is needed
m_modified = true;
}
// notify our owner about changed data -> screen refresh?
emit sigModified();
}
//***************************************************************************
void Kwave::TrackPixmap::convertOverlap(sample_index_t &offset,
sample_index_t &length)
{
if (m_zoom <= 0.0) length = 0;
if (!length) return;
if ((offset + length) <= m_offset) {
length = 0;
return; // not yet in view
}
unsigned int buflen = static_cast<unsigned int>(m_valid.size());
if (!buflen) {
offset = 0;
length = 0;
return;
}
// calculate the length
if (m_minmax_mode) {
// attention: round up the length int this mode!
if (offset >= m_offset + static_cast<sample_index_t>(
ceil(buflen * m_zoom)))
{
length = 0; // out of view
return;
} else {
length = static_cast<sample_index_t>(
ceil(static_cast<double>(length) / m_zoom));
}
} else {
if (offset >= m_offset + buflen) {
length = 0; // out of view
return;
}
}
// convert the offset
offset = (offset > m_offset) ? offset - m_offset : 0;
if (m_minmax_mode) {
// attention: round down in this mode!
double ofs_d = static_cast<double>(offset);
sample_index_t ofs = static_cast<sample_index_t>(floor(ofs_d / m_zoom));
// if offset was rounded down, increment length
if (ofs != static_cast<sample_index_t>(ceil(ofs_d / m_zoom)))
length++;
offset = ofs;
}
// limit the offset (maybe something happened when rounding)
if (offset >= buflen) offset = buflen - 1;
// limit the length to the end of the buffer
if (offset + length > buflen) length = buflen - offset;
Q_ASSERT(length);
}
//***************************************************************************
//***************************************************************************
#include "moc_TrackPixmap.cpp"
|