visualizer: add filled wave visualizer

This commit is contained in:
Darby Payne
2014-10-20 23:09:55 -07:00
committed by Andrzej Rybczak
parent 9bab03e4b2
commit 1f2daaa08c
7 changed files with 119 additions and 46 deletions

View File

@@ -39,4 +39,6 @@ enum class Design { Classic, Alternative };
std::ostream &operator<<(std::ostream &os, Design ui); std::ostream &operator<<(std::ostream &os, Design ui);
std::istream &operator>>(std::istream &is, Design &ui); std::istream &operator>>(std::istream &is, Design &ui);
enum class VisualizerType { Wave, WaveFilled, Spectrum };
#endif // NCMPCPP_ENUMS_H #endif // NCMPCPP_ENUMS_H

View File

@@ -231,13 +231,8 @@ bool Configuration::read(const std::string &config_path)
return boost::posix_time::seconds(v); return boost::posix_time::seconds(v);
})); }));
p.add("visualizer_type", option_parser::worker([this](std::string &&v) { p.add("visualizer_type", option_parser::worker([this](std::string &&v) {
if (v == "wave") visualizer_type = stringToVisualizerType( v );
visualizer_use_wave = true; }, defaults_to(visualizer_type, VisualizerType::Wave)
else if (v == "spectrum")
visualizer_use_wave = false;
else
throw std::runtime_error("invalid argument: " + v);
}, defaults_to(visualizer_use_wave, true)
)); ));
p.add("visualizer_look", assign_default<std::string>( p.add("visualizer_look", assign_default<std::string>(
visualizer_chars, "●▮", [](std::string &&s) { visualizer_chars, "●▮", [](std::string &&s) {
@@ -649,3 +644,5 @@ bool Configuration::read(const std::string &config_path)
std::ifstream f(config_path); std::ifstream f(config_path);
return p.run(f); return p.run(f);
} }
/* vim: set tabstop=4 softtabstop=4 shiftwidth=4 noexpandtab : */

View File

@@ -109,7 +109,9 @@ struct Configuration
NC::Color statusbar_color; NC::Color statusbar_color;
NC::Color alternative_ui_separator_color; NC::Color alternative_ui_separator_color;
NC::Color active_column_color; NC::Color active_column_color;
std::vector<NC::Color> visualizer_colors; std::vector<NC::Color> visualizer_colors;
VisualizerType visualizer_type;
NC::Border window_border; NC::Border window_border;
NC::Border active_window_border; NC::Border active_window_border;
@@ -151,7 +153,6 @@ struct Configuration
bool ask_before_clearing_playlists; bool ask_before_clearing_playlists;
bool mouse_support; bool mouse_support;
bool mouse_list_scroll_whole_page; bool mouse_list_scroll_whole_page;
bool visualizer_use_wave;
bool visualizer_in_stereo; bool visualizer_in_stereo;
bool data_fetching_delay; bool data_fetching_delay;
bool media_library_sort_by_mtime; bool media_library_sort_by_mtime;

View File

@@ -43,6 +43,18 @@ NC::Color stringToColor(const std::string &color)
return result; return result;
} }
VisualizerType stringToVisualizerType(const std::string &visualizerType)
{
VisualizerType result = VisualizerType::Wave;
if (visualizerType == "wave")
result = VisualizerType::Wave;
else if (visualizerType == "spectrum")
result = VisualizerType::Spectrum;
else if (visualizerType == "wave_filled")
result = VisualizerType::WaveFilled;
return result;
}
NC::Border stringToBorder(const std::string &border) NC::Border stringToBorder(const std::string &border)
{ {
NC::Border result = NC::Border::None; NC::Border result = NC::Border::None;

View File

@@ -24,8 +24,10 @@
#include "mpdpp.h" #include "mpdpp.h"
#include "mutable_song.h" #include "mutable_song.h"
#include "window.h" #include "window.h"
#include "enums.h"
NC::Color stringToColor(const std::string &color); NC::Color stringToColor(const std::string &color);
VisualizerType stringToVisualizerType(const std::string &visualizerType);
NC::Border stringToBorder(const std::string &border); NC::Border stringToBorder(const std::string &border);
std::string tagTypeToString(mpd_tag_type tag); std::string tagTypeToString(mpd_tag_type tag);

View File

@@ -37,6 +37,7 @@
#include "title.h" #include "title.h"
#include "screen_switcher.h" #include "screen_switcher.h"
#include "status.h" #include "status.h"
#include "enums.h"
using Global::MainStartY; using Global::MainStartY;
using Global::MainHeight; using Global::MainHeight;
@@ -109,10 +110,13 @@ void Visualizer::update()
void (Visualizer::*draw)(int16_t *, ssize_t, size_t, size_t); void (Visualizer::*draw)(int16_t *, ssize_t, size_t, size_t);
# ifdef HAVE_FFTW3_H # ifdef HAVE_FFTW3_H
if (!Config.visualizer_use_wave) if (Config.visualizer_type == VisualizerType::Spectrum)
draw = &Visualizer::DrawFrequencySpectrum; draw = &Visualizer::DrawFrequencySpectrum;
else else
# endif // HAVE_FFTW3_H # endif // HAVE_FFTW3_H
if (Config.visualizer_type == VisualizerType::WaveFilled)
draw = &Visualizer::DrawSoundWaveFill;
else
draw = &Visualizer::DrawSoundWave; draw = &Visualizer::DrawSoundWave;
const ssize_t samples_read = data/sizeof(int16_t); const ssize_t samples_read = data/sizeof(int16_t);
@@ -137,7 +141,7 @@ void Visualizer::update()
} }
size_t half_height = MainHeight/2; size_t half_height = MainHeight/2;
(this->*draw)(buf_left, samples_read/2, 0, half_height); (this->*draw)(buf_left, samples_read/2, 0, half_height);
(this->*draw)(buf_right, samples_read/2, half_height+(draw == &Visualizer::DrawSoundWave ? 1 : 0), half_height+(draw != &Visualizer::DrawSoundWave ? 1 : 0)); (this->*draw)(buf_right, samples_read/2, half_height+(draw == &Visualizer::DrawFrequencySpectrum ? 0 : 1), half_height+(draw != &Visualizer::DrawFrequencySpectrum ? 0 : 1));
} }
else else
(this->*draw)(buf, samples_read, 0, MainHeight); (this->*draw)(buf, samples_read, 0, MainHeight);
@@ -154,12 +158,63 @@ int Visualizer::windowTimeout()
void Visualizer::spacePressed() void Visualizer::spacePressed()
{ {
std::string visualizerName;
if (Config.visualizer_type == VisualizerType::Wave)
{
Config.visualizer_type = VisualizerType::WaveFilled;
visualizerName = "sound wave filled";
}
# ifdef HAVE_FFTW3_H # ifdef HAVE_FFTW3_H
Config.visualizer_use_wave = !Config.visualizer_use_wave; else if (Config.visualizer_type == VisualizerType::WaveFilled)
Statusbar::printf("Visualization type: %1%", {
Config.visualizer_use_wave ? "sound wave" : "frequency spectrum" Config.visualizer_type = VisualizerType::Spectrum;
); visualizerName = "frequency spectrum";
}
# endif // HAVE_FFTW3_H # endif // HAVE_FFTW3_H
else
{
Config.visualizer_type = VisualizerType::Wave;
visualizerName = "sound wave";
}
Statusbar::printf("Visualization type: %1%", visualizerName.c_str());
}
NC::Color Visualizer::toColor( int number, int max )
{
const int colorMapSize = Config.visualizer_colors.size();
const int normalizedNumber = ( ( number * colorMapSize ) / max ) % colorMapSize;
return Config.visualizer_colors[normalizedNumber];
}
void Visualizer::DrawSoundWaveFill(int16_t *buf, ssize_t samples, size_t y_offset, size_t height)
{
const int samples_per_col = samples/w.getWidth();
const int half_height = height/2;
double prev_point_pos = 0;
const size_t win_width = w.getWidth();
const bool left = y_offset > 0;
int x = 0;
for (size_t i = 0; i < win_width; ++i)
{
double point_pos = 0;
for (int j = 0; j < samples_per_col; ++j)
point_pos += buf[i*samples_per_col+j];
point_pos /= samples_per_col;
point_pos /= std::numeric_limits<int16_t>::max();
point_pos *= half_height;
for (int k = 0; k < point_pos * 2; k += 1)
{
x = left ? height + k : height - k;
if ( x > 0 && x < w.getHeight() && (i-(k < half_height + point_pos)) > 0 && (i-(k < half_height + point_pos)) < w.getWidth() )
{
w << toColor( k, height )
<< NC::XY(i-(k < half_height + point_pos), x)
<< Config.visualizer_chars[1]
<< NC::Color::End;
}
}
}
} }
void Visualizer::DrawSoundWave(int16_t *buf, ssize_t samples, size_t y_offset, size_t height) void Visualizer::DrawSoundWave(int16_t *buf, ssize_t samples, size_t y_offset, size_t height)
@@ -282,3 +337,4 @@ void Visualizer::FindOutputID()
#endif // ENABLE_VISUALIZER #endif // ENABLE_VISUALIZER
/* vim: set tabstop=4 softtabstop=4 shiftwidth=4 noexpandtab : */

View File

@@ -64,7 +64,9 @@ protected:
virtual bool isLockable() OVERRIDE { return true; } virtual bool isLockable() OVERRIDE { return true; }
private: private:
NC::Color toColor(int, int);
void DrawSoundWave(int16_t *, ssize_t, size_t, size_t); void DrawSoundWave(int16_t *, ssize_t, size_t, size_t);
void DrawSoundWaveFill(int16_t *, ssize_t, size_t, size_t);
# ifdef HAVE_FFTW3_H # ifdef HAVE_FFTW3_H
void DrawFrequencySpectrum(int16_t *, ssize_t, size_t, size_t); void DrawFrequencySpectrum(int16_t *, ssize_t, size_t, size_t);
# endif // HAVE_FFTW3_H # endif // HAVE_FFTW3_H
@@ -89,3 +91,4 @@ extern Visualizer *myVisualizer;
#endif // NCMPCPP_VISUALIZER_H #endif // NCMPCPP_VISUALIZER_H
/* vim: set tabstop=4 softtabstop=4 shiftwidth=4 noexpandtab : */