|
|
|
|
@ -45,17 +45,16 @@ Visualizer *myVisualizer; |
|
|
|
|
const int Visualizer::WindowTimeout = 1000/25; /* 25 fps */ |
|
|
|
|
|
|
|
|
|
Visualizer::Visualizer() |
|
|
|
|
: Screen(NC::Window(0, MainStartY, COLS, MainHeight, "", Config.visualizer_color, NC::brNone)) |
|
|
|
|
{ |
|
|
|
|
w = new NC::Window(0, MainStartY, COLS, MainHeight, "", Config.visualizer_color, NC::brNone); |
|
|
|
|
|
|
|
|
|
ResetFD(); |
|
|
|
|
itsSamples = Config.visualizer_in_stereo ? 4096 : 2048; |
|
|
|
|
m_samples = Config.visualizer_in_stereo ? 4096 : 2048; |
|
|
|
|
# ifdef HAVE_FFTW3_H |
|
|
|
|
itsFFTResults = itsSamples/2+1; |
|
|
|
|
itsFreqsMagnitude = new unsigned[itsFFTResults]; |
|
|
|
|
itsInput = static_cast<double *>(fftw_malloc(sizeof(double)*itsSamples)); |
|
|
|
|
itsOutput = static_cast<fftw_complex *>(fftw_malloc(sizeof(fftw_complex)*itsFFTResults)); |
|
|
|
|
itsPlan = fftw_plan_dft_r2c_1d(itsSamples, itsInput, itsOutput, FFTW_ESTIMATE); |
|
|
|
|
m_fftw_results = m_samples/2+1; |
|
|
|
|
m_freq_magnitudes = new unsigned[m_fftw_results]; |
|
|
|
|
m_fftw_input = static_cast<double *>(fftw_malloc(sizeof(double)*m_samples)); |
|
|
|
|
m_fftw_output = static_cast<fftw_complex *>(fftw_malloc(sizeof(fftw_complex)*m_fftw_results)); |
|
|
|
|
m_fftw_plan = fftw_plan_dft_r2c_1d(m_samples, m_fftw_input, m_fftw_output, FFTW_ESTIMATE); |
|
|
|
|
# endif // HAVE_FFTW3_H
|
|
|
|
|
|
|
|
|
|
FindOutputID(); |
|
|
|
|
@ -79,14 +78,14 @@ void Visualizer::switchTo() |
|
|
|
|
Global::myPrevScreen = myScreen; |
|
|
|
|
myScreen = this; |
|
|
|
|
drawHeader(); |
|
|
|
|
w->clear(); |
|
|
|
|
w.clear(); |
|
|
|
|
|
|
|
|
|
SetFD(); |
|
|
|
|
|
|
|
|
|
itsTimer.tv_sec = 0; |
|
|
|
|
itsTimer.tv_usec = 0; |
|
|
|
|
m_timer.tv_sec = 0; |
|
|
|
|
m_timer.tv_usec = 0; |
|
|
|
|
|
|
|
|
|
if (itsFifo >= 0) |
|
|
|
|
if (m_fifo >= 0) |
|
|
|
|
Global::wFooter->setTimeout(WindowTimeout); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -94,8 +93,8 @@ void Visualizer::resize() |
|
|
|
|
{ |
|
|
|
|
size_t x_offset, width; |
|
|
|
|
getWindowResizeParams(x_offset, width); |
|
|
|
|
w->resize(width, MainHeight); |
|
|
|
|
w->moveTo(x_offset, MainStartY); |
|
|
|
|
w.resize(width, MainHeight); |
|
|
|
|
w.moveTo(x_offset, MainStartY); |
|
|
|
|
hasToBeResized = 0; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -106,21 +105,21 @@ std::wstring Visualizer::title() |
|
|
|
|
|
|
|
|
|
void Visualizer::update() |
|
|
|
|
{ |
|
|
|
|
if (itsFifo < 0) |
|
|
|
|
if (m_fifo < 0) |
|
|
|
|
return; |
|
|
|
|
|
|
|
|
|
// PCM in format 44100:16:1 (for mono visualization) and 44100:16:2 (for stereo visualization) is supported
|
|
|
|
|
int16_t buf[itsSamples]; |
|
|
|
|
ssize_t data = read(itsFifo, buf, sizeof(buf)); |
|
|
|
|
int16_t buf[m_samples]; |
|
|
|
|
ssize_t data = read(m_fifo, buf, sizeof(buf)); |
|
|
|
|
if (data < 0) // no data available in fifo
|
|
|
|
|
return; |
|
|
|
|
|
|
|
|
|
if (itsOutputID != -1 && Global::Timer.tv_sec > itsTimer.tv_sec+Config.visualizer_sync_interval) |
|
|
|
|
if (m_output_id != -1 && Global::Timer.tv_sec > m_timer.tv_sec+Config.visualizer_sync_interval) |
|
|
|
|
{ |
|
|
|
|
Mpd.DisableOutput(itsOutputID); |
|
|
|
|
Mpd.DisableOutput(m_output_id); |
|
|
|
|
usleep(50000); |
|
|
|
|
Mpd.EnableOutput(itsOutputID); |
|
|
|
|
itsTimer = Global::Timer; |
|
|
|
|
Mpd.EnableOutput(m_output_id); |
|
|
|
|
m_timer = Global::Timer; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void (Visualizer::*draw)(int16_t *, ssize_t, size_t, size_t); |
|
|
|
|
@ -131,7 +130,7 @@ void Visualizer::update() |
|
|
|
|
# endif // HAVE_FFTW3_H
|
|
|
|
|
draw = &Visualizer::DrawSoundWave; |
|
|
|
|
|
|
|
|
|
w->clear(); |
|
|
|
|
w.clear(); |
|
|
|
|
ssize_t samples_read = data/sizeof(int16_t); |
|
|
|
|
if (Config.visualizer_in_stereo) |
|
|
|
|
{ |
|
|
|
|
@ -147,7 +146,7 @@ void Visualizer::update() |
|
|
|
|
} |
|
|
|
|
else |
|
|
|
|
(this->*draw)(buf, samples_read, 0, MainHeight); |
|
|
|
|
w->refresh(); |
|
|
|
|
w.refresh(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void Visualizer::spacePressed() |
|
|
|
|
@ -160,10 +159,10 @@ void Visualizer::spacePressed() |
|
|
|
|
|
|
|
|
|
void Visualizer::DrawSoundWave(int16_t *buf, ssize_t samples, size_t y_offset, size_t height) |
|
|
|
|
{ |
|
|
|
|
const int samples_per_col = samples/w->getWidth(); |
|
|
|
|
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 size_t win_width = w.getWidth(); |
|
|
|
|
for (size_t i = 0; i < win_width; ++i) |
|
|
|
|
{ |
|
|
|
|
double point_pos = 0; |
|
|
|
|
@ -172,7 +171,7 @@ void Visualizer::DrawSoundWave(int16_t *buf, ssize_t samples, size_t y_offset, s |
|
|
|
|
point_pos /= samples_per_col; |
|
|
|
|
point_pos /= std::numeric_limits<int16_t>::max(); |
|
|
|
|
point_pos *= half_height; |
|
|
|
|
*w << NC::XY(i, y_offset+half_height+point_pos) << Config.visualizer_chars[0]; |
|
|
|
|
w << NC::XY(i, y_offset+half_height+point_pos) << Config.visualizer_chars[0]; |
|
|
|
|
if (i && abs(prev_point_pos-point_pos) > 2) |
|
|
|
|
{ |
|
|
|
|
// if gap is too big. intermediate values are needed
|
|
|
|
|
@ -180,7 +179,7 @@ void Visualizer::DrawSoundWave(int16_t *buf, ssize_t samples, size_t y_offset, s |
|
|
|
|
const int breakpoint = std::max(prev_point_pos, point_pos); |
|
|
|
|
const int half = (prev_point_pos+point_pos)/2; |
|
|
|
|
for (int k = std::min(prev_point_pos, point_pos)+1; k < breakpoint; k += 2) |
|
|
|
|
*w << NC::XY(i-(k < half), y_offset+half_height+k) << Config.visualizer_chars[0]; |
|
|
|
|
w << NC::XY(i-(k < half), y_offset+half_height+k) << Config.visualizer_chars[0]; |
|
|
|
|
} |
|
|
|
|
prev_point_pos = point_pos; |
|
|
|
|
} |
|
|
|
|
@ -189,58 +188,58 @@ void Visualizer::DrawSoundWave(int16_t *buf, ssize_t samples, size_t y_offset, s |
|
|
|
|
#ifdef HAVE_FFTW3_H |
|
|
|
|
void Visualizer::DrawFrequencySpectrum(int16_t *buf, ssize_t samples, size_t y_offset, size_t height) |
|
|
|
|
{ |
|
|
|
|
for (unsigned i = 0, j = 0; i < itsSamples; ++i) |
|
|
|
|
for (unsigned i = 0, j = 0; i < m_samples; ++i) |
|
|
|
|
{ |
|
|
|
|
if (j < samples) |
|
|
|
|
itsInput[i] = buf[j++]; |
|
|
|
|
m_fftw_input[i] = buf[j++]; |
|
|
|
|
else |
|
|
|
|
itsInput[i] = 0; |
|
|
|
|
m_fftw_input[i] = 0; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fftw_execute(itsPlan); |
|
|
|
|
fftw_execute(m_fftw_plan); |
|
|
|
|
|
|
|
|
|
// count magnitude of each frequency and scale it to fit the screen
|
|
|
|
|
for (unsigned i = 0; i < itsFFTResults; ++i) |
|
|
|
|
itsFreqsMagnitude[i] = sqrt(itsOutput[i][0]*itsOutput[i][0] + itsOutput[i][1]*itsOutput[i][1])/1e5*height/5; |
|
|
|
|
for (unsigned i = 0; i < m_fftw_results; ++i) |
|
|
|
|
m_freq_magnitudes[i] = sqrt(m_fftw_output[i][0]*m_fftw_output[i][0] + m_fftw_output[i][1]*m_fftw_output[i][1])/1e5*height/5; |
|
|
|
|
|
|
|
|
|
const size_t win_width = w->getWidth(); |
|
|
|
|
const int freqs_per_col = itsFFTResults/win_width /* cut bandwidth a little to achieve better look */ * 7/10; |
|
|
|
|
const size_t win_width = w.getWidth(); |
|
|
|
|
const int freqs_per_col = m_fftw_results/win_width /* cut bandwidth a little to achieve better look */ * 7/10; |
|
|
|
|
for (size_t i = 0; i < win_width; ++i) |
|
|
|
|
{ |
|
|
|
|
size_t bar_height = 0; |
|
|
|
|
for (int j = 0; j < freqs_per_col; ++j) |
|
|
|
|
bar_height += itsFreqsMagnitude[i*freqs_per_col+j]; |
|
|
|
|
bar_height += m_freq_magnitudes[i*freqs_per_col+j]; |
|
|
|
|
bar_height = std::min(bar_height/freqs_per_col, height); |
|
|
|
|
const size_t start_y = y_offset > 0 ? y_offset : height-bar_height; |
|
|
|
|
const size_t stop_y = std::min(bar_height+start_y, w->getHeight()); |
|
|
|
|
const size_t stop_y = std::min(bar_height+start_y, w.getHeight()); |
|
|
|
|
for (size_t j = start_y; j < stop_y; ++j) |
|
|
|
|
*w << NC::XY(i, j) << Config.visualizer_chars[1]; |
|
|
|
|
w << NC::XY(i, j) << Config.visualizer_chars[1]; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
#endif // HAVE_FFTW3_H
|
|
|
|
|
|
|
|
|
|
void Visualizer::SetFD() |
|
|
|
|
{ |
|
|
|
|
if (itsFifo < 0 && (itsFifo = open(Config.visualizer_fifo_path.c_str(), O_RDONLY | O_NONBLOCK)) < 0) |
|
|
|
|
if (m_fifo < 0 && (m_fifo = open(Config.visualizer_fifo_path.c_str(), O_RDONLY | O_NONBLOCK)) < 0) |
|
|
|
|
Statusbar::msg("Couldn't open \"%s\" for reading PCM data: %s", Config.visualizer_fifo_path.c_str(), strerror(errno)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void Visualizer::ResetFD() |
|
|
|
|
{ |
|
|
|
|
itsFifo = -1; |
|
|
|
|
m_fifo = -1; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void Visualizer::FindOutputID() |
|
|
|
|
{ |
|
|
|
|
itsOutputID = -1; |
|
|
|
|
m_output_id = -1; |
|
|
|
|
if (!Config.visualizer_output_name.empty()) |
|
|
|
|
{ |
|
|
|
|
size_t i = 0; |
|
|
|
|
auto outputs = Mpd.GetOutputs(); |
|
|
|
|
for (auto o = outputs.begin(); o != outputs.end(); ++o, ++i) |
|
|
|
|
if (o->name() == Config.visualizer_output_name) |
|
|
|
|
itsOutputID = i; |
|
|
|
|
if (itsOutputID == -1) |
|
|
|
|
m_output_id = i; |
|
|
|
|
if (m_output_id == -1) |
|
|
|
|
Statusbar::msg("There is no output named \"%s\"", Config.visualizer_output_name.c_str()); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|