2019-02-04 21:34:12 +01:00
|
|
|
/*
|
|
|
|
Strawberry Music Player
|
|
|
|
This file was part of Clementine.
|
|
|
|
Copyright 2004, Max Howell <max.howell@methylblue.com>
|
|
|
|
Copyright 2009-2010, David Sansome <davidsansome@gmail.com>
|
|
|
|
Copyright 2010, 2014, John Maguire <john.maguire@gmail.com>
|
|
|
|
Copyright 2014-2015, Mark Furneaux <mark@furneaux.ca>
|
|
|
|
Copyright 2014, Krzysztof Sobiecki <sobkas@gmail.com>
|
|
|
|
|
|
|
|
Strawberry 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 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Strawberry is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Strawberry. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "boomanalyzer.h"
|
|
|
|
|
|
|
|
#include <cmath>
|
|
|
|
|
|
|
|
#include <QWidget>
|
|
|
|
#include <QPixmap>
|
|
|
|
#include <QPainter>
|
2020-02-08 03:40:30 +01:00
|
|
|
#include <QPalette>
|
2019-02-04 21:34:12 +01:00
|
|
|
#include <QColor>
|
|
|
|
|
2020-02-08 03:40:30 +01:00
|
|
|
#include "engine/engine_fwd.h"
|
|
|
|
#include "engine/enginebase.h"
|
|
|
|
#include "fht.h"
|
|
|
|
#include "analyzerbase.h"
|
|
|
|
|
2019-02-04 21:34:12 +01:00
|
|
|
using Analyzer::Scope;
|
|
|
|
|
2021-03-21 18:53:02 +01:00
|
|
|
const int BoomAnalyzer::kColumnWidth = 4;
|
|
|
|
const int BoomAnalyzer::kMaxBandCount = 256;
|
|
|
|
const int BoomAnalyzer::kMinBandCount = 32;
|
2019-02-04 21:34:12 +01:00
|
|
|
|
2021-06-12 20:53:23 +02:00
|
|
|
const char *BoomAnalyzer::kName = QT_TRANSLATE_NOOP("AnalyzerContainer", "Boom analyzer");
|
2019-02-04 21:34:12 +01:00
|
|
|
|
2021-06-12 20:53:23 +02:00
|
|
|
BoomAnalyzer::BoomAnalyzer(QWidget *parent)
|
2019-02-04 21:34:12 +01:00
|
|
|
: Analyzer::Base(parent, 9),
|
|
|
|
bands_(0),
|
|
|
|
scope_(kMinBandCount),
|
|
|
|
fg_(palette().color(QPalette::Highlight)),
|
2021-07-11 07:40:57 +02:00
|
|
|
K_barHeight_(1.271), // 1.471
|
|
|
|
F_peakSpeed_(1.103), // 1.122
|
2019-02-04 21:34:12 +01:00
|
|
|
F_(1.0),
|
|
|
|
bar_height_(kMaxBandCount, 0),
|
|
|
|
peak_height_(kMaxBandCount, 0),
|
|
|
|
peak_speed_(kMaxBandCount, 0.01),
|
|
|
|
barPixmap_(kColumnWidth, 50) {
|
|
|
|
|
|
|
|
setMinimumWidth(kMinBandCount * (kColumnWidth + 1) - 1);
|
|
|
|
setMaximumWidth(kMaxBandCount * (kColumnWidth + 1) - 1);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-06-12 20:53:23 +02:00
|
|
|
void BoomAnalyzer::changeK_barHeight(const int newValue) {
|
2019-02-04 21:34:12 +01:00
|
|
|
K_barHeight_ = static_cast<double>(newValue) / 1000;
|
|
|
|
}
|
|
|
|
|
2021-06-12 20:53:23 +02:00
|
|
|
void BoomAnalyzer::changeF_peakSpeed(const int newValue) {
|
2019-02-04 21:34:12 +01:00
|
|
|
F_peakSpeed_ = static_cast<double>(newValue) / 1000;
|
|
|
|
}
|
|
|
|
|
2021-06-12 20:53:23 +02:00
|
|
|
void BoomAnalyzer::resizeEvent(QResizeEvent *e) {
|
2019-02-04 21:34:12 +01:00
|
|
|
|
|
|
|
QWidget::resizeEvent(e);
|
|
|
|
|
2021-10-30 02:21:29 +02:00
|
|
|
const int HEIGHT = height() - 2;
|
2019-02-04 21:34:12 +01:00
|
|
|
const double h = 1.2 / HEIGHT;
|
|
|
|
|
2021-03-21 18:53:02 +01:00
|
|
|
bands_ = qMin(static_cast<int>(static_cast<double>(width() + 1) / (kColumnWidth + 1)) + 1, kMaxBandCount);
|
2019-02-04 21:34:12 +01:00
|
|
|
scope_.resize(bands_);
|
|
|
|
|
2021-10-11 22:28:28 +02:00
|
|
|
F_ = static_cast<double>(HEIGHT) / (log10(256) * static_cast<double>(1.1) /*<- max. amplitude*/);
|
2019-02-04 21:34:12 +01:00
|
|
|
|
|
|
|
barPixmap_ = QPixmap(kColumnWidth - 2, HEIGHT);
|
|
|
|
canvas_ = QPixmap(size());
|
2020-07-18 04:12:50 +02:00
|
|
|
canvas_.fill(palette().color(QPalette::Window));
|
2019-02-04 21:34:12 +01:00
|
|
|
|
|
|
|
QPainter p(&barPixmap_);
|
2021-10-30 02:21:29 +02:00
|
|
|
for (int y = 0; y < HEIGHT; ++y) {
|
2019-02-04 21:34:12 +01:00
|
|
|
const double F = static_cast<double>(y) * h;
|
|
|
|
|
|
|
|
p.setPen(QColor(qMax(0, 255 - static_cast<int>(229.0 * F)),
|
|
|
|
qMax(0, 255 - static_cast<int>(229.0 * F)),
|
|
|
|
qMax(0, 255 - static_cast<int>(191.0 * F))));
|
|
|
|
p.drawLine(0, y, kColumnWidth - 2, y);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-06-12 20:53:23 +02:00
|
|
|
void BoomAnalyzer::transform(Scope &s) {
|
2019-02-04 21:34:12 +01:00
|
|
|
|
|
|
|
fht_->spectrum(s.data());
|
2022-02-06 04:19:45 +01:00
|
|
|
fht_->scale(s.data(), 1.0F / 50);
|
2019-02-04 21:34:12 +01:00
|
|
|
|
2021-03-21 18:53:02 +01:00
|
|
|
s.resize(scope_.size() <= static_cast<quint64>(kMaxBandCount) / 2 ? kMaxBandCount / 2 : scope_.size());
|
2019-02-04 21:34:12 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-06-12 20:53:23 +02:00
|
|
|
void BoomAnalyzer::analyze(QPainter &p, const Scope &scope, const bool new_frame) {
|
2019-02-04 21:34:12 +01:00
|
|
|
|
|
|
|
if (!new_frame || engine_->state() == Engine::Paused) {
|
|
|
|
p.drawPixmap(0, 0, canvas_);
|
|
|
|
return;
|
|
|
|
}
|
2021-03-26 21:30:13 +01:00
|
|
|
double h = 0.0;
|
2019-02-04 21:34:12 +01:00
|
|
|
const uint MAX_HEIGHT = height() - 1;
|
|
|
|
|
|
|
|
QPainter canvas_painter(&canvas_);
|
2020-07-18 04:12:50 +02:00
|
|
|
canvas_.fill(palette().color(QPalette::Window));
|
2019-02-04 21:34:12 +01:00
|
|
|
|
|
|
|
Analyzer::interpolate(scope, scope_);
|
|
|
|
|
2021-03-26 21:30:13 +01:00
|
|
|
for (int i = 0, x = 0, y = 0; i < bands_; ++i, x += kColumnWidth + 1) {
|
2019-02-04 21:34:12 +01:00
|
|
|
h = log10(scope_[i] * 256.0) * F_;
|
|
|
|
|
|
|
|
if (h > MAX_HEIGHT) h = MAX_HEIGHT;
|
|
|
|
|
|
|
|
if (h > bar_height_[i]) {
|
|
|
|
bar_height_[i] = h;
|
|
|
|
|
|
|
|
if (h > peak_height_[i]) {
|
|
|
|
peak_height_[i] = h;
|
|
|
|
peak_speed_[i] = 0.01;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
goto peak_handling;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (bar_height_[i] > 0.0) {
|
|
|
|
bar_height_[i] -= K_barHeight_; // 1.4
|
|
|
|
if (bar_height_[i] < 0.0) bar_height_[i] = 0.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
peak_handling:
|
|
|
|
|
|
|
|
if (peak_height_[i] > 0.0) {
|
|
|
|
peak_height_[i] -= peak_speed_[i];
|
|
|
|
peak_speed_[i] *= F_peakSpeed_; // 1.12
|
|
|
|
|
|
|
|
if (peak_height_[i] < bar_height_[i]) peak_height_[i] = bar_height_[i];
|
|
|
|
if (peak_height_[i] < 0.0) peak_height_[i] = 0.0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-21 18:53:02 +01:00
|
|
|
y = height() - static_cast<int>(bar_height_[i]);
|
2019-02-04 21:34:12 +01:00
|
|
|
canvas_painter.drawPixmap(x + 1, y, barPixmap_, 0, y, -1, -1);
|
|
|
|
canvas_painter.setPen(fg_);
|
2021-08-23 21:21:08 +02:00
|
|
|
if (bar_height_[i] > 0) {
|
2019-02-04 21:34:12 +01:00
|
|
|
canvas_painter.drawRect(x, y, kColumnWidth - 1, height() - y - 1);
|
2021-08-23 21:21:08 +02:00
|
|
|
}
|
2019-02-04 21:34:12 +01:00
|
|
|
|
2021-03-21 18:53:02 +01:00
|
|
|
y = height() - static_cast<int>(peak_height_[i]);
|
2019-02-04 21:34:12 +01:00
|
|
|
canvas_painter.setPen(palette().color(QPalette::Midlight));
|
|
|
|
canvas_painter.drawLine(x, y, x + kColumnWidth - 1, y);
|
|
|
|
}
|
|
|
|
|
|
|
|
p.drawPixmap(0, 0, canvas_);
|
|
|
|
|
|
|
|
}
|