| 1 |
/* |
| 2 |
* Copyright (C) 2019-2021 Rerrah |
| 3 |
* |
| 4 |
* Permission is hereby granted, free of charge, to any person |
| 5 |
* obtaining a copy of this software and associated documentation |
| 6 |
* files (the "Software"), to deal in the Software without |
| 7 |
* restriction, including without limitation the rights to use, |
| 8 |
* copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 |
* copies of the Software, and to permit persons to whom the |
| 10 |
* Software is furnished to do so, subject to the following |
| 11 |
* conditions: |
| 12 |
* |
| 13 |
* The above copyright notice and this permission notice shall be |
| 14 |
* included in all copies or substantial portions of the Software. |
| 15 |
* |
| 16 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 17 |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
| 18 |
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 19 |
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 20 |
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 21 |
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 22 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 23 |
* OTHER DEALINGS IN THE SOFTWARE. |
| 24 |
*/ |
| 25 |
|
| 26 |
#include "gui/wave_visual.hpp" |
| 27 |
#include "gui/color_palette.hpp" |
| 28 |
#include <limits> |
| 29 |
#include <QPainter> |
| 30 |
//Xcode 8.3: "no member names 'abs' in namespace 'std' |
| 31 |
#include <cstdlib> |
| 32 |
|
| 33 |
WaveVisual::WaveVisual(QWidget *parent) |
| 34 |
: QWidget(parent) |
| 35 |
{ |
| 36 |
setAttribute(Qt::WA_OpaquePaintEvent); |
| 37 |
} |
| 38 |
|
| 39 |
void WaveVisual::setColorPalette(std::shared_ptr<ColorPalette> palette) |
| 40 |
{ |
| 41 |
palette_ = palette; |
| 42 |
} |
| 43 |
|
| 44 |
void WaveVisual::setStereoSamples(const int16_t *buffer, size_t frames) |
| 45 |
{ |
| 46 |
// identify the highest of the 2 input signals |
| 47 |
int32_t sum = 0; |
| 48 |
for (size_t i = 0; i < frames; ++i) { |
| 49 |
size_t p = i << 1; |
| 50 |
sum += std::abs(buffer[p]); |
| 51 |
sum -= std::abs(buffer[p + 1]); |
| 52 |
} |
| 53 |
|
| 54 |
// use this signal as display data |
| 55 |
samples_.resize(frames); |
| 56 |
int16_t *samples = samples_.data(); |
| 57 |
for (size_t i = 0; i < frames; ++i) |
| 58 |
samples[i] = buffer[(i << 1) + (sum < 0)]; |
| 59 |
|
| 60 |
repaint(); |
| 61 |
} |
| 62 |
|
| 63 |
void WaveVisual::paintEvent(QPaintEvent*) |
| 64 |
{ |
| 65 |
QPainter painter(this); |
| 66 |
|
| 67 |
if (!palette_) |
| 68 |
return; |
| 69 |
|
| 70 |
int w = width(); |
| 71 |
int h = height(); |
| 72 |
int centerh = h >> 1; |
| 73 |
painter.fillRect(0, 0, w, h, palette_->wavBackColor); |
| 74 |
|
| 75 |
const int16_t *samples = samples_.data(); |
| 76 |
size_t frames = samples_.size(); |
| 77 |
if (frames <= 0) |
| 78 |
return; |
| 79 |
|
| 80 |
painter.setPen(palette_->wavDrawColor); |
| 81 |
|
| 82 |
int lastY = centerh; |
| 83 |
const int16_t range = std::numeric_limits<int16_t>::max() >> 1; |
| 84 |
for (int x = 0; x < w; ++x) { |
| 85 |
size_t index = (size_t)(x * ((double)frames / w)); |
| 86 |
|
| 87 |
int16_t sample = samples[index]; |
| 88 |
int y = centerh - (centerh * sample / range); |
| 89 |
painter.drawPoint(x, y); |
| 90 |
|
| 91 |
// draw intermediate points |
| 92 |
int y1 = lastY, y2 = y; |
| 93 |
int yM = (y1 + y2) >> 1; |
| 94 |
while (y1 != y2) { |
| 95 |
bool b = (y1 > yM) ^ (y1 > y2); |
| 96 |
painter.drawPoint(x - !b, y1); |
| 97 |
y1 += (y1 < y2) ? 1 : -1; |
| 98 |
} |
| 99 |
|
| 100 |
lastY = y; |
| 101 |
} |
| 102 |
} |