visualizer: simplify DrawSoundWaveFill

This commit is contained in:
Andrzej Rybczak
2014-10-26 20:18:51 +01:00
parent dfc55789d8
commit 459aa706c0

View File

@@ -105,7 +105,8 @@ void Visualizer::update()
if (m_fifo < 0) if (m_fifo < 0)
return; return;
// PCM in format 44100:16:1 (for mono visualization) and 44100:16:2 (for stereo visualization) is supported // PCM in format 44100:16:1 (for mono visualization) and
// 44100:16:2 (for stereo visualization) is supported.
int16_t buf[m_samples]; int16_t buf[m_samples];
ssize_t data = read(m_fifo, buf, sizeof(buf)); ssize_t data = read(m_fifo, buf, sizeof(buf));
if (data < 0) // no data available in fifo if (data < 0) // no data available in fifo
@@ -224,7 +225,7 @@ void Visualizer::DrawSoundWaveStereo(int16_t *buf_left, int16_t *buf_right, ssiz
void Visualizer::DrawSoundWaveFillStereo(int16_t *buf_left, int16_t *buf_right, ssize_t samples, size_t height) void Visualizer::DrawSoundWaveFillStereo(int16_t *buf_left, int16_t *buf_right, ssize_t samples, size_t height)
{ {
DrawSoundWaveFill(buf_left, samples, 0, height); DrawSoundWaveFill(buf_left, samples, 0, height);
DrawSoundWaveFill(buf_right, samples, height + 1, height); DrawSoundWaveFill(buf_right, samples, height, w.getHeight() - height);
} }
// DrawSoundEllipseStereo: This visualizer only works in stereo. The colors form concentric // DrawSoundEllipseStereo: This visualizer only works in stereo. The colors form concentric
@@ -263,29 +264,34 @@ void Visualizer::DrawSoundEllipseStereo(int16_t *buf_left, int16_t *buf_right, s
// is dedicated to the right channel, the bottom the left channel. // is dedicated to the right channel, the bottom the left channel.
void Visualizer::DrawSoundWaveFill(int16_t *buf, ssize_t samples, size_t y_offset, size_t height) void Visualizer::DrawSoundWaveFill(int16_t *buf, ssize_t samples, size_t y_offset, size_t height)
{ {
const int samples_per_col = samples/w.getWidth(); // if right channel is drawn, bars descend from the top to the bottom
const int half_height = height/2; const bool flipped = y_offset > 0;
const size_t win_width = w.getWidth(); const size_t win_width = w.getWidth();
const bool left = y_offset > 0; const int samples_per_column = samples/win_width;
int x = 0;
for (size_t i = 0; i < win_width; ++i) // too little samples
if (samples_per_column == 0)
return;
int32_t point_y;
for (size_t x = 0; x < win_width; ++x)
{ {
double point_pos = 0; point_y = 0;
for (int j = 0; j < samples_per_col; ++j) // calculate mean from the relevant points
point_pos += buf[i*samples_per_col+j]; for (int j = 0; j < samples_per_column; ++j)
point_pos /= samples_per_col; point_y += buf[x*samples_per_column+j];
point_pos /= std::numeric_limits<int16_t>::max(); point_y /= samples_per_column;
point_pos *= half_height; // normalize it to fit the screen
for (int k = 0; k < point_pos * 2; k += 1) point_y = std::abs(point_y);
point_y *= height / 32768.0;
for (int32_t j = 0; j < point_y; ++j)
{ {
x = left ? height + k : height - k; size_t y = flipped ? y_offset+j : y_offset+height-j-1;
if ( x > 0 && x < w.getHeight() && (i-(k < half_height + point_pos)) > 0 && (i-(k < half_height + point_pos)) < w.getWidth() ) w << NC::XY(x, y)
{ << toColor(j, height)
w << toColor( k, height ) << Config.visualizer_chars[1]
<< NC::XY(i-(k < half_height + point_pos), x) << NC::Color::End;
<< Config.visualizer_chars[1]
<< NC::Color::End;
}
} }
} }
} }