Reduce CPU usage of the frequency spectrum visualizer

This commit is contained in:
Andrzej Rybczak
2020-12-22 19:32:55 +01:00
parent def7ea42f6
commit d402df8eeb
5 changed files with 44 additions and 19 deletions

View File

@@ -7,6 +7,7 @@
* Draw a separator between albums with the same name, but a different artist. * Draw a separator between albums with the same name, but a different artist.
* Suppress output of all external commands. * Suppress output of all external commands.
* Consider mouse support when pausing and unpausing curses interface. * Consider mouse support when pausing and unpausing curses interface.
* Reduce CPU usage of the frequency spectrum visualizer.
# ncmpcpp-0.9 (2020-12-20) # ncmpcpp-0.9 (2020-12-20)
* Fix various Mopidy specific bugs. * Fix various Mopidy specific bugs.

View File

@@ -26,6 +26,30 @@ if test "$clock" = "yes"; then
AC_DEFINE([ENABLE_CLOCK], [1], [enables clock screen]) AC_DEFINE([ENABLE_CLOCK], [1], [enables clock screen])
fi fi
# -ftree-vectorize
AC_MSG_CHECKING([whether compiler supports -ftree-vectorize])
old_CXXFLAGS="$CXXFLAGS"
CXXFLAGS="-ftree-vectorize"
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[ ]])],
AC_MSG_RESULT([yes])
tree_vectorize="-ftree-vectorize",
AC_MSG_RESULT([no])
tree_vectorize=""
)
CXXFLAGS="$old_CXXFLAGS $tree_vectorize"
# -ffast-math
AC_MSG_CHECKING([whether compiler supports -ffast-math])
old_CXXFLAGS="$CXXFLAGS"
CXXFLAGS="-ffast-math"
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[ ]])],
AC_MSG_RESULT([yes])
fast_math="-ffast-math",
AC_MSG_RESULT([no])
fast_math=""
)
CXXFLAGS="$old_CXXFLAGS $fast_math"
# -std=c++14 # -std=c++14
AC_MSG_CHECKING([whether compiler supports -std=c++14]) AC_MSG_CHECKING([whether compiler supports -std=c++14])
old_CXXFLAGS="$CXXFLAGS" old_CXXFLAGS="$CXXFLAGS"

View File

@@ -127,7 +127,7 @@
## visualizer look at a larger slice of time, which results in less jumpy ## visualizer look at a larger slice of time, which results in less jumpy
## visualizer output. ## visualizer output.
# #
#visualizer_spectrum_dft_size = 3 #visualizer_spectrum_dft_size = 2
# #
#visualizer_spectrum_gain = 10 #visualizer_spectrum_gain = 10
# #

View File

@@ -77,8 +77,8 @@ Visualizer::Visualizer()
, m_sample_consumption_rate_dn_ctr(0) , m_sample_consumption_rate_dn_ctr(0)
# ifdef HAVE_FFTW3_H # ifdef HAVE_FFTW3_H
, ,
DFT_NONZERO_SIZE(1 << Config.visualizer_spectrum_dft_size), DFT_NONZERO_SIZE(2048 * (2*Config.visualizer_spectrum_dft_size + 4)),
DFT_TOTAL_SIZE(1 << (Config.visualizer_spectrum_dft_size + 2)), DFT_TOTAL_SIZE(1 << 15),
DYNAMIC_RANGE(100-Config.visualizer_spectrum_gain), DYNAMIC_RANGE(100-Config.visualizer_spectrum_gain),
HZ_MIN(Config.visualizer_spectrum_hz_min), HZ_MIN(Config.visualizer_spectrum_hz_min),
HZ_MAX(Config.visualizer_spectrum_hz_max), HZ_MAX(Config.visualizer_spectrum_hz_max),
@@ -206,7 +206,7 @@ void Visualizer::update()
} }
else if (m_sample_consumption_rate > 0) else if (m_sample_consumption_rate > 0)
{ {
if (++m_sample_consumption_rate_dn_ctr > 2) if (++m_sample_consumption_rate_dn_ctr > 4)
{ {
m_sample_consumption_rate_dn_ctr = 0; m_sample_consumption_rate_dn_ctr = 0;
--m_sample_consumption_rate; --m_sample_consumption_rate;
@@ -489,7 +489,7 @@ void Visualizer::DrawFrequencySpectrum(const int16_t *buf, ssize_t samples, size
for (size_t x = 0; x < win_width; ++x) for (size_t x = 0; x < win_width; ++x)
{ {
const size_t &i = m_bar_heights[h_idx].first; const size_t &i = m_bar_heights[h_idx].first;
const double &bar_height = m_bar_heights[h_idx].second; const double bar_height = m_bar_heights[h_idx].second;
double h = 0; double h = 0;
if (x == i) { if (x == i) {
@@ -547,33 +547,33 @@ void Visualizer::DrawFrequencySpectrumStereo(const int16_t *buf_left, const int1
double Visualizer::Interpolate(size_t x, size_t h_idx) double Visualizer::Interpolate(size_t x, size_t h_idx)
{ {
const double &x_next = m_bar_heights[h_idx].first; const double x_next = m_bar_heights[h_idx].first;
const double &h_next = m_bar_heights[h_idx].second; const double h_next = m_bar_heights[h_idx].second;
double dh = 0; double dh = 0;
if (h_idx == 0) { if (h_idx == 0) {
// no data points on left, linear extrap // no data points on left, linear extrap
if (h_idx < m_bar_heights.size()-1) { if (h_idx < m_bar_heights.size()-1) {
const double &x_next2 = m_bar_heights[h_idx+1].first; const double x_next2 = m_bar_heights[h_idx+1].first;
const double &h_next2 = m_bar_heights[h_idx+1].second; const double h_next2 = m_bar_heights[h_idx+1].second;
dh = (h_next2 - h_next) / (x_next2 - x_next); dh = (h_next2 - h_next) / (x_next2 - x_next);
} }
return h_next - dh*(x_next-x); return h_next - dh*(x_next-x);
} else if (h_idx == 1) { } else if (h_idx == 1) {
// one data point on left, linear interp // one data point on left, linear interp
const double &x_prev = m_bar_heights[h_idx-1].first; const double x_prev = m_bar_heights[h_idx-1].first;
const double &h_prev = m_bar_heights[h_idx-1].second; const double h_prev = m_bar_heights[h_idx-1].second;
dh = (h_next - h_prev) / (x_next - x_prev); dh = (h_next - h_prev) / (x_next - x_prev);
return h_next - dh*(x_next-x); return h_next - dh*(x_next-x);
} else if (h_idx < m_bar_heights.size()-1) { } else if (h_idx < m_bar_heights.size()-1) {
// two data points on both sides, cubic interp // two data points on both sides, cubic interp
// see https://en.wikipedia.org/wiki/Cubic_Hermite_spline#Interpolation_on_an_arbitrary_interval // see https://en.wikipedia.org/wiki/Cubic_Hermite_spline#Interpolation_on_an_arbitrary_interval
const double &x_prev2 = m_bar_heights[h_idx-2].first; const double x_prev2 = m_bar_heights[h_idx-2].first;
const double &h_prev2 = m_bar_heights[h_idx-2].second; const double h_prev2 = m_bar_heights[h_idx-2].second;
const double &x_prev = m_bar_heights[h_idx-1].first; const double x_prev = m_bar_heights[h_idx-1].first;
const double &h_prev = m_bar_heights[h_idx-1].second; const double h_prev = m_bar_heights[h_idx-1].second;
const double &x_next2 = m_bar_heights[h_idx+1].first; const double x_next2 = m_bar_heights[h_idx+1].first;
const double &h_next2 = m_bar_heights[h_idx+1].second; const double h_next2 = m_bar_heights[h_idx+1].second;
const double m0 = (h_prev - h_prev2) / (x_prev - x_prev2); const double m0 = (h_prev - h_prev2) / (x_prev - x_prev2);
const double m1 = (h_next2 - h_next) / (x_next2 - x_next); const double m1 = (h_next2 - h_next) / (x_next2 - x_next);

View File

@@ -272,10 +272,10 @@ bool Configuration::read(const std::vector<std::string> &config_paths, bool igno
p.add("visualizer_autoscale", &visualizer_autoscale, "no", yes_no); p.add("visualizer_autoscale", &visualizer_autoscale, "no", yes_no);
p.add("visualizer_spectrum_smooth_look", &visualizer_spectrum_smooth_look, "yes", yes_no); p.add("visualizer_spectrum_smooth_look", &visualizer_spectrum_smooth_look, "yes", yes_no);
p.add("visualizer_spectrum_dft_size", &visualizer_spectrum_dft_size, p.add("visualizer_spectrum_dft_size", &visualizer_spectrum_dft_size,
"3", [](std::string v) { "2", [](std::string v) {
auto result = verbose_lexical_cast<size_t>(v); auto result = verbose_lexical_cast<size_t>(v);
boundsCheck<size_t>(result, 1, 5); boundsCheck<size_t>(result, 1, 5);
return result + 11; return result;
}); });
p.add("visualizer_spectrum_gain", &visualizer_spectrum_gain, p.add("visualizer_spectrum_gain", &visualizer_spectrum_gain,
"10", [](std::string v) { "10", [](std::string v) {