1 /***************************************************************************
2  *   Copyright (C) 2008-2021 by Andrzej Rybczak                            *
3  *   andrzej@rybczak.net                                                   *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.              *
19  ***************************************************************************/
20 
21 #include "screens/visualizer.h"
22 
23 #ifdef ENABLE_VISUALIZER
24 
25 #include <algorithm>
26 #include <boost/date_time/posix_time/posix_time.hpp>
27 #include <boost/math/constants/constants.hpp>
28 #include <cerrno>
29 #include <cmath>
30 #include <cstring>
31 #include <fstream>
32 #include <limits>
33 #include <fcntl.h>
34 #include <netdb.h>
35 #include <cassert>
36 #include <sys/types.h>
37 #include <sys/socket.h>
38 #include <netinet/in.h>
39 
40 #include "global.h"
41 #include "settings.h"
42 #include "status.h"
43 #include "statusbar.h"
44 #include "title.h"
45 #include "screens/screen_switcher.h"
46 #include "status.h"
47 #include "enums.h"
48 #include "utility/wide_string.h"
49 
50 using Samples = std::vector<int16_t>;
51 
52 using Global::MainStartY;
53 using Global::MainHeight;
54 
55 Visualizer *myVisualizer;
56 
57 namespace {
58 
59 // toColor: a scaling function for coloring. For numbers 0 to max this function
60 // returns a coloring from the lowest color to the highest, and colors will not
61 // loop from 0 to max.
toColor(size_t number,size_t max,bool wrap)62 const NC::FormattedColor &toColor(size_t number, size_t max, bool wrap)
63 {
64 	const auto colors_size = Config.visualizer_colors.size();
65 	const auto index = (number * colors_size) / max;
66 	return Config.visualizer_colors[
67 		wrap ? index % colors_size : std::min(index, colors_size-1)
68 	];
69 }
70 
71 }
72 
Visualizer()73 Visualizer::Visualizer()
74 : Screen(NC::Window(0, MainStartY, COLS, MainHeight, "", NC::Color::Default, NC::Border()))
75 , m_output_id(-1)
76 , m_reset_output(false)
77 , m_source_fd(-1)
78 , m_sample_consumption_rate(5)
79 , m_sample_consumption_rate_up_ctr(0)
80 , m_sample_consumption_rate_dn_ctr(0)
81 #	ifdef HAVE_FFTW3_H
82 	,
83   DFT_NONZERO_SIZE(2048 * (2*Config.visualizer_spectrum_dft_size + 4)),
84   DFT_TOTAL_SIZE(1 << 15),
85   DYNAMIC_RANGE(100-Config.visualizer_spectrum_gain),
86   HZ_MIN(Config.visualizer_spectrum_hz_min),
87   HZ_MAX(Config.visualizer_spectrum_hz_max),
88   GAIN(Config.visualizer_spectrum_gain),
89   SMOOTH_CHARS(ToWString("▁▂▃▄▅▆▇█"))
90 #endif
91 {
92 	InitDataSource();
93 	InitVisualization();
94 #	ifdef HAVE_FFTW3_H
95 	m_fftw_results = DFT_TOTAL_SIZE/2+1;
96 	m_freq_magnitudes.resize(m_fftw_results);
97 	m_fftw_input = static_cast<double *>(fftw_malloc(sizeof(double)*DFT_TOTAL_SIZE));
98 	memset(m_fftw_input, 0, sizeof(double)*DFT_TOTAL_SIZE);
99 	m_fftw_output = static_cast<fftw_complex *>(fftw_malloc(sizeof(fftw_complex)*m_fftw_results));
100 	m_fftw_plan = fftw_plan_dft_r2c_1d(DFT_TOTAL_SIZE, m_fftw_input, m_fftw_output, FFTW_ESTIMATE);
101 	m_dft_logspace.reserve(500);
102 	m_bar_heights.reserve(100);
103 #	endif // HAVE_FFTW3_H
104 }
105 
switchTo()106 void Visualizer::switchTo()
107 {
108 	SwitchTo::execute(this);
109 	Clear();
110 	m_reset_output = true;
111 	drawHeader();
112 #	ifdef HAVE_FFTW3_H
113 	GenLogspace();
114 	m_bar_heights.reserve(w.getWidth());
115 #	endif // HAVE_FFTW3_H
116 }
117 
resize()118 void Visualizer::resize()
119 {
120 	size_t x_offset, width;
121 	getWindowResizeParams(x_offset, width);
122 	w.resize(width, MainHeight);
123 	w.moveTo(x_offset, MainStartY);
124 	hasToBeResized = 0;
125 	InitVisualization();
126 #	ifdef HAVE_FFTW3_H
127 	GenLogspace();
128 	m_bar_heights.reserve(w.getWidth());
129 #	endif // HAVE_FFTW3_H
130 }
131 
title()132 std::wstring Visualizer::title()
133 {
134 	return L"Music visualizer";
135 }
136 
update()137 void Visualizer::update()
138 {
139 	if (m_source_fd < 0)
140 		return;
141 
142 	// Disable and enable FIFO to get rid of the difference between audio and
143 	// visualization.
144 	if (m_reset_output && m_output_id != -1)
145 	{
146 		Mpd.DisableOutput(m_output_id);
147 		usleep(50000);
148 		Mpd.EnableOutput(m_output_id);
149 		m_reset_output = false;
150 	}
151 
152 	// PCM in format 44100:16:1 (for mono visualization) and
153 	// 44100:16:2 (for stereo visualization) is supported.
154 	ssize_t bytes_read = read(m_source_fd, m_incoming_samples.data(),
155 	                          sizeof(int16_t) * m_incoming_samples.size());
156 	if (bytes_read > 0)
157 	{
158 		const auto begin = m_incoming_samples.begin();
159 		const auto end = m_incoming_samples.begin() + bytes_read/sizeof(int16_t);
160 
161 		if (Config.visualizer_autoscale)
162 		{
163 			m_auto_scale_multiplier += 1.0/Config.visualizer_fps;
164 			for (auto sample = begin; sample != end; ++sample)
165 			{
166 				double scale = std::numeric_limits<int16_t>::min();
167 				scale /= *sample;
168 				scale = fabs(scale);
169 				if (scale < m_auto_scale_multiplier)
170 					m_auto_scale_multiplier = scale;
171 			}
172 			for (auto sample = begin; sample != end; ++sample)
173 			{
174 				int32_t tmp = *sample;
175 				if (m_auto_scale_multiplier <= 50.0) // limit the auto scale
176 					tmp *= m_auto_scale_multiplier;
177 				if (tmp < std::numeric_limits<int16_t>::min())
178 					*sample = std::numeric_limits<int16_t>::min();
179 				else if (tmp > std::numeric_limits<int16_t>::max())
180 					*sample = std::numeric_limits<int16_t>::max();
181 				else
182 					*sample = tmp;
183 			}
184 		}
185 		m_buffered_samples.put(begin, end);
186 	}
187 
188 	size_t requested_samples =
189 		44100.0 / Config.visualizer_fps * pow(1.1, m_sample_consumption_rate);
190 	if (Config.visualizer_in_stereo)
191 		requested_samples *= 2;
192 
193 	//Statusbar::printf("Samples: %1%, %2%, %3%", m_buffered_samples.size(),
194 	//                  requested_samples, m_sample_consumption_rate);
195 
196 	size_t new_samples = m_buffered_samples.get(requested_samples, m_rendered_samples);
197 	if (new_samples == 0)
198 		return;
199 
200 	// A crude way to adjust the amount of samples consumed from the buffer
201 	// depending on how fast the rendering is.
202 	if (m_buffered_samples.size() > 0)
203 	{
204 		if (++m_sample_consumption_rate_up_ctr > 8)
205 		{
206 			m_sample_consumption_rate_up_ctr = 0;
207 			++m_sample_consumption_rate;
208 		}
209 	}
210 	else if (m_sample_consumption_rate > 0)
211 	{
212 		if (++m_sample_consumption_rate_dn_ctr > 4)
213 		{
214 			m_sample_consumption_rate_dn_ctr = 0;
215 			--m_sample_consumption_rate;
216 		}
217 		m_sample_consumption_rate_up_ctr = 0;
218 	}
219 
220 	w.clear();
221 	if (Config.visualizer_in_stereo)
222 	{
223 		auto chan_samples = m_rendered_samples.size()/2;
224 		int16_t buf_left[chan_samples], buf_right[chan_samples];
225 		for (size_t i = 0, j = 0; i < m_rendered_samples.size(); i += 2, ++j)
226 		{
227 			buf_left[j] = m_rendered_samples[i];
228 			buf_right[j] = m_rendered_samples[i+1];
229 		}
230 		size_t half_height = w.getHeight()/2;
231 
232 		(this->*drawStereo)(buf_left, buf_right, chan_samples, half_height);
233 	}
234 	else
235 	{
236 		(this->*draw)(m_rendered_samples.data(), m_rendered_samples.size(), 0, w.getHeight());
237 	}
238 	w.refresh();
239 }
240 
windowTimeout()241 int Visualizer::windowTimeout()
242 {
243 	if (m_source_fd >= 0 && Status::State::player() == MPD::psPlay)
244 		return 1000/Config.visualizer_fps;
245 	else
246 		return Screen<WindowType>::windowTimeout();
247 }
248 
249 /**********************************************************************/
250 
DrawSoundWave(const int16_t * buf,ssize_t samples,size_t y_offset,size_t height)251 void Visualizer::DrawSoundWave(const int16_t *buf, ssize_t samples, size_t y_offset, size_t height)
252 {
253 	const size_t half_height = height/2;
254 	const size_t base_y = y_offset+half_height;
255 	const size_t win_width = w.getWidth();
256 	const int samples_per_column = samples/win_width;
257 
258 	// too little samples
259 	if (samples_per_column == 0)
260 		return;
261 
262 	auto draw_point = [&](size_t x, int32_t y) {
263 		auto c = toColor(std::abs(y), half_height, false);
264 		w << NC::XY(x, base_y+y)
265 		  << c
266 		  << Config.visualizer_chars[0]
267 		  << NC::FormattedColor::End<>(c);
268 	};
269 
270 	int32_t point_y, prev_point_y = 0;
271 	for (size_t x = 0; x < win_width; ++x)
272 	{
273 		point_y = 0;
274 		// calculate mean from the relevant points
275 		for (int j = 0; j < samples_per_column; ++j)
276 			point_y += buf[x*samples_per_column+j];
277 		point_y /= samples_per_column;
278 		// normalize it to fit the screen
279 		point_y *= height / 65536.0;
280 
281 		draw_point(x, point_y);
282 
283 		// if the gap between two consecutive points is too big,
284 		// intermediate values are needed for the wave to be watchable.
285 		if (x > 0 && std::abs(prev_point_y-point_y) > 1)
286 		{
287 			const int32_t half = (prev_point_y+point_y)/2;
288 			if (prev_point_y < point_y)
289 			{
290 				for (auto y = prev_point_y; y < point_y; ++y)
291 					draw_point(x-(y < half), y);
292 			}
293 			else
294 			{
295 				for (auto y = prev_point_y; y > point_y; --y)
296 					draw_point(x-(y > half), y);
297 			}
298 		}
299 		prev_point_y = point_y;
300 	}
301 }
302 
DrawSoundWaveStereo(const int16_t * buf_left,const int16_t * buf_right,ssize_t samples,size_t height)303 void Visualizer::DrawSoundWaveStereo(const int16_t *buf_left, const int16_t *buf_right, ssize_t samples, size_t height)
304 {
305 	DrawSoundWave(buf_left, samples, 0, height);
306 	DrawSoundWave(buf_right, samples, height, w.getHeight() - height);
307 }
308 
309 /**********************************************************************/
310 
311 // DrawSoundWaveFill: This visualizer is very similar to DrawSoundWave, but
312 // instead of a single line the entire height is filled. In stereo mode, the top
313 // half of the screen is dedicated to the right channel, the bottom the left
314 // channel.
DrawSoundWaveFill(const int16_t * buf,ssize_t samples,size_t y_offset,size_t height)315 void Visualizer::DrawSoundWaveFill(const int16_t *buf, ssize_t samples, size_t y_offset, size_t height)
316 {
317 	// if right channel is drawn, bars descend from the top to the bottom
318 	const bool flipped = y_offset > 0;
319 	const size_t win_width = w.getWidth();
320 	const int samples_per_column = samples/win_width;
321 
322 	// too little samples
323 	if (samples_per_column == 0)
324 		return;
325 
326 	int32_t point_y;
327 	for (size_t x = 0; x < win_width; ++x)
328 	{
329 		point_y = 0;
330 		// calculate mean from the relevant points
331 		for (int j = 0; j < samples_per_column; ++j)
332 			point_y += buf[x*samples_per_column+j];
333 		point_y /= samples_per_column;
334 		// normalize it to fit the screen
335 		point_y = std::abs(point_y);
336 		point_y *= height / 32768.0;
337 
338 		for (int32_t j = 0; j < point_y; ++j)
339 		{
340 			auto c = toColor(j, height, false);
341 			size_t y = flipped ? y_offset+j : y_offset+height-j-1;
342 			w << NC::XY(x, y)
343 			  << c
344 			  << Config.visualizer_chars[1]
345 			  << NC::FormattedColor::End<>(c);
346 		}
347 	}
348 }
349 
DrawSoundWaveFillStereo(const int16_t * buf_left,const int16_t * buf_right,ssize_t samples,size_t height)350 void Visualizer::DrawSoundWaveFillStereo(const int16_t *buf_left, const int16_t *buf_right, ssize_t samples, size_t height)
351 {
352 	DrawSoundWaveFill(buf_left, samples, 0, height);
353 	DrawSoundWaveFill(buf_right, samples, height, w.getHeight() - height);
354 }
355 
356 /**********************************************************************/
357 
358 // Draws the sound wave as an ellipse with origin in the center of the screen.
DrawSoundEllipse(const int16_t * buf,ssize_t samples,size_t,size_t height)359 void Visualizer::DrawSoundEllipse(const int16_t *buf, ssize_t samples, size_t, size_t height)
360 {
361 	const size_t half_width = w.getWidth()/2;
362 	const size_t half_height = height/2;
363 
364 	// Make it so that the loop goes around the ellipse exactly once.
365 	const double deg_multiplier = 2*boost::math::constants::pi<double>()/samples;
366 
367 	int32_t x, y;
368 	double radius, max_radius;
369 	for (ssize_t i = 0; i < samples; ++i)
370 	{
371 		x = half_width * std::cos(i*deg_multiplier);
372 		y = half_height * std::sin(i*deg_multiplier);
373 		max_radius = sqrt(x*x + y*y);
374 
375 		// Calculate the distance of the sample from the center, where 0 is the
376 		// center of the ellipse and 1 is its border.
377 		radius = std::abs(buf[i]);
378 		radius /= 32768.0;
379 
380 		// Appropriately scale the position.
381 		x *= radius;
382 		y *= radius;
383 
384 		auto c = toColor(sqrt(x*x + y*y), max_radius, false);
385 		w << NC::XY(half_width + x, half_height + y)
386 		  << c
387 		  << Config.visualizer_chars[0]
388 		  << NC::FormattedColor::End<>(c);
389 	}
390 }
391 
392 // DrawSoundEllipseStereo: This visualizer only works in stereo. The colors form
393 // concentric rings originating from the center (width/2, height/2). For any
394 // given point, the width is scaled with the left channel and height is scaled
395 // with the right channel. For example, if a song is entirely in the right
396 // channel, then it would just be a vertical line.
397 //
398 // Since every font/terminal is different, the visualizer is never a perfect
399 // circle. This visualizer assume the font height is twice the length of the
400 // font's width. If the font is skinner or wider than this, instead of a circle
401 // it will be an ellipse.
DrawSoundEllipseStereo(const int16_t * buf_left,const int16_t * buf_right,ssize_t samples,size_t half_height)402 void Visualizer::DrawSoundEllipseStereo(const int16_t *buf_left, const int16_t *buf_right, ssize_t samples, size_t half_height)
403 {
404 	const size_t width = w.getWidth();
405 	const size_t left_half_width = width/2;
406 	const size_t right_half_width = width - left_half_width;
407 	const size_t top_half_height = half_height;
408 	const size_t bottom_half_height = w.getHeight() - half_height;
409 
410 	// Makes the radius of each ring be approximately 2 cells wide.
411 	const int32_t radius = 2*Config.visualizer_colors.size();
412 	int32_t x, y;
413 	for (ssize_t i = 0; i < samples; ++i)
414 	{
415 		x = buf_left[i]/32768.0 * (buf_left[i] < 0 ? left_half_width : right_half_width);
416 		y = buf_right[i]/32768.0 * (buf_right[i] < 0 ? top_half_height : bottom_half_height);
417 
418 		// The arguments to the toColor function roughly follow a circle equation
419 		// where the center is not centered around (0,0). For example (x - w)^2 +
420 		// (y-h)+2 = r^2 centers the circle around the point (w,h). Because fonts
421 		// are not all the same size, this will not always generate a perfect
422 		// circle.
423 		auto c = toColor(sqrt(x*x + 4*y*y), radius, true);
424 		w << NC::XY(left_half_width + x, top_half_height + y)
425 		  << c
426 		  << Config.visualizer_chars[1]
427 		  << NC::FormattedColor::End<>(c);
428 	}
429 }
430 
431 /**********************************************************************/
432 
433 #ifdef HAVE_FFTW3_H
DrawFrequencySpectrum(const int16_t * buf,ssize_t samples,size_t y_offset,size_t height)434 void Visualizer::DrawFrequencySpectrum(const int16_t *buf, ssize_t samples, size_t y_offset, size_t height)
435 {
436 	// If right channel is drawn, bars descend from the top to the bottom.
437 	const bool flipped = y_offset > 0;
438 
439 	// copy samples to fftw input array and apply Hamming window
440 	ApplyWindow(m_fftw_input, buf, samples);
441 	fftw_execute(m_fftw_plan);
442 
443 	// Count magnitude of each frequency and normalize
444 	for (size_t i = 0; i < m_fftw_results; ++i)
445 		m_freq_magnitudes[i] = sqrt(
446 			m_fftw_output[i][0]*m_fftw_output[i][0]
447 		+	m_fftw_output[i][1]*m_fftw_output[i][1]
448 		) / (DFT_NONZERO_SIZE);
449 
450 	m_bar_heights.clear();
451 
452 	const size_t win_width = w.getWidth();
453 
454 	size_t cur_bin = 0;
455 	while (cur_bin < m_fftw_results && Bin2Hz(cur_bin) < m_dft_logspace[0])
456 		++cur_bin;
457 	for (size_t x = 0; x < win_width; ++x)
458 	{
459 		double bar_height = 0;
460 
461 		// accumulate bins
462 		size_t count = 0;
463 		// check right bound
464 		while (cur_bin < m_fftw_results && Bin2Hz(cur_bin) < m_dft_logspace[x])
465 		{
466 			// check left bound if not first index
467 			if (x == 0 || Bin2Hz(cur_bin) >= m_dft_logspace[x-1])
468 			{
469 				bar_height += m_freq_magnitudes[cur_bin];
470 				++count;
471 			}
472 			++cur_bin;
473 		}
474 
475 		if (count == 0)
476 			continue;
477 
478 		// average bins
479 		bar_height /= count;
480 
481 		// log scale bar heights
482 		bar_height = (20 * log10(bar_height) + DYNAMIC_RANGE + GAIN) / DYNAMIC_RANGE;
483 		// Scale bar height between 0 and height
484 		bar_height = bar_height > 0 ? bar_height * height : 0;
485 		bar_height = bar_height > height ? height : bar_height;
486 
487 		m_bar_heights.emplace_back(x, bar_height);
488 	}
489 
490 	size_t h_idx = 0;
491 	for (size_t x = 0; x < win_width; ++x)
492 	{
493 		const size_t i = m_bar_heights[h_idx].first;
494 		const double bar_height = m_bar_heights[h_idx].second;
495 		double h = 0;
496 
497 		if (x == i) {
498 			// this data point exists
499 			h = bar_height;
500 			if (h_idx < m_bar_heights.size()-1)
501 				++h_idx;
502 		} else {
503 			// data point does not exist, need to interpolate
504 			h = Interpolate(x, h_idx);
505 		}
506 
507 		for (size_t j = 0; j < h; ++j)
508 		{
509 			size_t y = flipped ? y_offset+j : y_offset+height-j-1;
510 			auto color = toColor(j, height, false);
511 			std::wstring ch;
512 
513 			// select character to draw
514 			if (Config.visualizer_spectrum_smooth_look) {
515 				// smooth
516 				const size_t size = SMOOTH_CHARS.size();
517 				const size_t idx = static_cast<size_t>(size*h) % size;
518 				if (j < h-1 || idx == size-1) {
519 					// full height
520 					ch = SMOOTH_CHARS[size-1];
521 				} else {
522 					// fractional height
523 					if (flipped) {
524 						ch = SMOOTH_CHARS[size-idx-2];
525 						color = NC::FormattedColor(color.color(), {NC::Format::Reverse});
526 					} else {
527 						ch = SMOOTH_CHARS[idx];
528 					}
529 				}
530 			} else  {
531 				// default, non-smooth
532 				ch = Config.visualizer_chars[1];
533 			}
534 
535 			// draw character on screen
536 			w << NC::XY(x, y)
537 			  << color
538 			  << ch
539 			  << NC::FormattedColor::End<>(color);
540 		}
541 	}
542 }
543 
DrawFrequencySpectrumStereo(const int16_t * buf_left,const int16_t * buf_right,ssize_t samples,size_t height)544 void Visualizer::DrawFrequencySpectrumStereo(const int16_t *buf_left, const int16_t *buf_right, ssize_t samples, size_t height)
545 {
546 	DrawFrequencySpectrum(buf_left, samples, 0, height);
547 	DrawFrequencySpectrum(buf_right, samples, height, w.getHeight() - height);
548 }
549 
Interpolate(size_t x,size_t h_idx)550 double Visualizer::Interpolate(size_t x, size_t h_idx)
551 {
552 	const double x_next = m_bar_heights[h_idx].first;
553 	const double h_next = m_bar_heights[h_idx].second;
554 
555 	double dh = 0;
556 	if (h_idx == 0) {
557 		// no data points on left, linear extrap
558 		if (h_idx < m_bar_heights.size()-1) {
559 			const double x_next2 = m_bar_heights[h_idx+1].first;
560 			const double h_next2 = m_bar_heights[h_idx+1].second;
561 			dh = (h_next2 - h_next) / (x_next2 - x_next);
562 		}
563 		return h_next - dh*(x_next-x);
564 	} else if (h_idx == 1) {
565 		// one data point on left, linear interp
566 		const double x_prev = m_bar_heights[h_idx-1].first;
567 		const double h_prev = m_bar_heights[h_idx-1].second;
568 		dh = (h_next - h_prev) / (x_next - x_prev);
569 		return h_next - dh*(x_next-x);
570 	} else if (h_idx < m_bar_heights.size()-1) {
571 		// two data points on both sides, cubic interp
572 		// see https://en.wikipedia.org/wiki/Cubic_Hermite_spline#Interpolation_on_an_arbitrary_interval
573 		const double x_prev2 = m_bar_heights[h_idx-2].first;
574 		const double h_prev2 = m_bar_heights[h_idx-2].second;
575 		const double x_prev = m_bar_heights[h_idx-1].first;
576 		const double h_prev = m_bar_heights[h_idx-1].second;
577 		const double x_next2 = m_bar_heights[h_idx+1].first;
578 		const double h_next2 = m_bar_heights[h_idx+1].second;
579 
580 		const double m0 = (h_prev - h_prev2) / (x_prev - x_prev2);
581 		const double m1 = (h_next2 - h_next) / (x_next2 - x_next);
582 		const double t = (x - x_prev) / (x_next - x_prev);
583 		const double h00 = 2*t*t*t - 3*t*t + 1;
584 		const double h10 = t*t*t - 2*t*t + t;
585 		const double h01 = -2*t*t*t + 3*t*t;
586 		const double h11 = t*t*t - t*t;
587 
588 		return h00*h_prev + h10*(x_next-x_prev)*m0 + h01*h_next + h11*(x_next-x_prev)*m1;
589 	}
590 
591 	// less than two data points on right, no interp, should never happen unless VERY low DFT size
592 	return h_next;
593 }
594 
ApplyWindow(double * output,const int16_t * input,ssize_t samples)595 void Visualizer::ApplyWindow(double *output, const int16_t *input, ssize_t samples)
596 {
597 	// Use Blackman window for low sidelobes and fast sidelobe rolloff
598 	// don't care too much about mainlobe width
599 	const double alpha = 0.16;
600 	const double a0 = (1 - alpha) / 2;
601 	const double a1 = 0.5;
602 	const double a2 = alpha / 2;
603 	const double pi = boost::math::constants::pi<double>();
604 	for (unsigned i = 0; i < samples; ++i)
605 	{
606 		double window = a0 - a1*cos(2*pi*i/(DFT_NONZERO_SIZE-1)) + a2*cos(4*pi*i/(DFT_NONZERO_SIZE-1));
607 		output[i] = window * input[i] / INT16_MAX;
608 	}
609 }
610 
Bin2Hz(size_t bin)611 double Visualizer::Bin2Hz(size_t bin)
612 {
613 	return bin*44100/DFT_TOTAL_SIZE;
614 }
615 
616 // Generate log-scaled vector of frequencies from HZ_MIN to HZ_MAX
GenLogspace()617 void Visualizer::GenLogspace()
618 {
619 	// Calculate number of extra bins needed between 0 HZ and HZ_MIN
620 	const size_t win_width = w.getWidth();
621 	const size_t left_bins = (log10(HZ_MIN) - win_width*log10(HZ_MIN)) / (log10(HZ_MIN) - log10(HZ_MAX));
622 	// Generate logspaced frequencies
623 	m_dft_logspace.resize(win_width);
624 	const double log_scale = log10(HZ_MAX) / (left_bins + m_dft_logspace.size() - 1);
625 	for (size_t i = left_bins; i < m_dft_logspace.size() + left_bins; ++i) {
626 		m_dft_logspace[i - left_bins] = pow(10, i * log_scale);
627 	}
628 }
629 #endif // HAVE_FFTW3_H
630 
InitDataSource()631 void Visualizer::InitDataSource()
632 {
633 	if (!Config.visualizer_fifo_path.empty())
634 		m_source_location = Config.visualizer_fifo_path; // deprecated
635 	else
636 		m_source_location = Config.visualizer_data_source;
637 
638 	// If there's a colon and a location doesn't start with '/' we have a UDP
639 	// sink. Otherwise assume it's a FIFO.
640 	auto colon = m_source_location.rfind(':');
641 	if (m_source_location[0] != '/' && colon != std::string::npos)
642 	{
643 		m_source_port = m_source_location.substr(colon+1);
644 		m_source_location.resize(colon);
645 	}
646 	else
647 		m_source_port.clear();
648 }
649 
InitVisualization()650 void Visualizer::InitVisualization()
651 {
652 	size_t rendered_samples = 0;
653 	switch (Config.visualizer_type)
654 	{
655 	case VisualizerType::Wave:
656 		// Guarantee integral amount of samples per column.
657 		rendered_samples = ceil(44100.0 / Config.visualizer_fps / w.getWidth());
658 		rendered_samples *= w.getWidth();
659 		// Slow the scolling 10 times to make it watchable.
660 		rendered_samples *= 10;
661 		draw = &Visualizer::DrawSoundWave;
662 		drawStereo = &Visualizer::DrawSoundWaveStereo;
663 		break;
664 	case VisualizerType::WaveFilled:
665 		// Guarantee integral amount of samples per column.
666 		rendered_samples = ceil(44100.0 / Config.visualizer_fps / w.getWidth());
667 		rendered_samples *= w.getWidth();
668 		// Slow the scolling 10 times to make it watchable.
669 		rendered_samples *= 10;
670 		draw = &Visualizer::DrawSoundWaveFill;
671 		drawStereo = &Visualizer::DrawSoundWaveFillStereo;
672 		break;
673 #	ifdef HAVE_FFTW3_H
674 	case VisualizerType::Spectrum:
675 		rendered_samples = DFT_NONZERO_SIZE;
676 		draw = &Visualizer::DrawFrequencySpectrum;
677 		drawStereo = &Visualizer::DrawFrequencySpectrumStereo;
678 		break;
679 #	endif // HAVE_FFTW3_H
680 	case VisualizerType::Ellipse:
681 		// Keep constant amount of samples on the screen regardless of fps.
682 		rendered_samples = 44100 / 30;
683 		draw = &Visualizer::DrawSoundEllipse;
684 		drawStereo = &Visualizer::DrawSoundEllipseStereo;
685 		break;
686 	}
687 	if (Config.visualizer_in_stereo)
688 		rendered_samples *= 2;
689 	m_rendered_samples.resize(rendered_samples);
690 
691 	// Keep 500ms worth of samples in the incoming buffer.
692 	size_t buffered_samples = 44100.0 / 2;
693 	if (Config.visualizer_in_stereo)
694 		buffered_samples *= 2;
695 	m_incoming_samples.resize(buffered_samples);
696 	m_buffered_samples.resize(buffered_samples);
697 }
698 
699 /**********************************************************************/
700 
Clear()701 void Visualizer::Clear()
702 {
703 	w.clear();
704 	std::fill(m_rendered_samples.begin(), m_rendered_samples.end(), 0);
705 
706 	// Discard any lingering data from the data source.
707 	if (m_source_fd >= 0)
708 	{
709 		ssize_t bytes_read;
710 		do
711 			bytes_read = read(m_source_fd, m_incoming_samples.data(),
712 			                  sizeof(int16_t) * m_incoming_samples.size());
713 		while (bytes_read > 0);
714 	}
715 
716 }
717 
ToggleVisualizationType()718 void Visualizer::ToggleVisualizationType()
719 {
720 	switch (Config.visualizer_type)
721 	{
722 		case VisualizerType::Wave:
723 			Config.visualizer_type = VisualizerType::WaveFilled;
724 			break;
725 		case VisualizerType::WaveFilled:
726 #			ifdef HAVE_FFTW3_H
727 			Config.visualizer_type = VisualizerType::Spectrum;
728 #			else
729 			Config.visualizer_type = VisualizerType::Ellipse;
730 #			endif // HAVE_FFTW3_H
731 			break;
732 #		ifdef HAVE_FFTW3_H
733 		case VisualizerType::Spectrum:
734 			Config.visualizer_type = VisualizerType::Ellipse;
735 			break;
736 #		endif // HAVE_FFTW3_H
737 		case VisualizerType::Ellipse:
738 			Config.visualizer_type = VisualizerType::Wave;
739 			break;
740 	}
741 	InitVisualization();
742 	Statusbar::printf("Visualization type: %1%", Config.visualizer_type);
743 }
744 
OpenDataSource()745 void Visualizer::OpenDataSource()
746 {
747 	if (m_source_fd >= 0)
748 		return;
749 
750 	if (!m_source_port.empty())
751 	{
752 		addrinfo hints, *res;
753 		memset (&hints, 0, sizeof (hints));
754 		hints.ai_family = PF_UNSPEC;
755 		hints.ai_socktype = SOCK_DGRAM;
756 		hints.ai_protocol = IPPROTO_UDP;
757 
758 		int errcode = getaddrinfo(m_source_location.c_str(), m_source_port.c_str(),
759 		                          &hints, &res);
760 		if (errcode != 0)
761 		{
762 			Statusbar::printf("Couldn't resolve \"%1%:%2%\": %3%",
763 			                  m_source_location, m_source_port, gai_strerror(errcode));
764 			return;
765 		}
766 
767 		for (auto addr = res; addr != nullptr; addr = addr->ai_next)
768 		{
769 			m_source_fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
770 			if (m_source_fd >= 0)
771 			{
772 				// No SOCK_NONBLOCK on MacOS
773 				int socket_flags = fcntl(m_source_fd, F_GETFL, 0);
774 				fcntl(m_source_fd, F_SETFL, socket_flags | O_NONBLOCK);
775 
776 				errcode = bind(m_source_fd, res->ai_addr, res->ai_addrlen);
777 				if (errcode < 0)
778 				{
779 					std::cerr << "Binding a socket failed: " << strerror(errno) << std::endl;
780 					CloseDataSource();
781 				}
782 				else
783 					break;
784 			}
785 			else
786 				std::cerr << "Creation of socket failed: " << strerror(errno) << std::endl;
787 		}
788 
789 		freeaddrinfo(res);
790 	}
791 	else
792 	{
793 		m_source_fd = open(m_source_location.c_str(), O_RDONLY | O_NONBLOCK);
794 		if (m_source_fd < 0)
795 			Statusbar::printf("Couldn't open \"%1%\" for reading PCM data: %2%",
796 			                  m_source_location, strerror(errno));
797 	}
798 }
799 
CloseDataSource()800 void Visualizer::CloseDataSource()
801 {
802 	if (m_source_fd >= 0)
803 		close(m_source_fd);
804 	m_source_fd = -1;
805 }
806 
FindOutputID()807 void Visualizer::FindOutputID()
808 {
809 	m_output_id = -1;
810 	// Look for the output only if its name is specified and we're fetching
811 	// samples from a FIFO.
812 	if (!Config.visualizer_output_name.empty() && m_source_port.empty())
813 	{
814 		for (MPD::OutputIterator out = Mpd.GetOutputs(), end; out != end; ++out)
815 		{
816 			if (out->name() == Config.visualizer_output_name)
817 			{
818 				m_output_id = out->id();
819 				break;
820 			}
821 		}
822 		if (m_output_id == -1)
823 			Statusbar::printf("There is no output named \"%s\"", Config.visualizer_output_name);
824 	}
825 }
826 
ResetAutoScaleMultiplier()827 void Visualizer::ResetAutoScaleMultiplier()
828 {
829 	m_auto_scale_multiplier = 1;
830 }
831 
832 #endif // ENABLE_VISUALIZER
833