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 #ifndef NCMPCPP_VISUALIZER_H
22 #define NCMPCPP_VISUALIZER_H
23 
24 #include "config.h"
25 
26 #ifdef ENABLE_VISUALIZER
27 
28 #include <boost/date_time/posix_time/posix_time_types.hpp>
29 #include "curses/window.h"
30 #include "interfaces.h"
31 #include "screens/screen.h"
32 #include "utility/sample_buffer.h"
33 
34 #ifdef HAVE_FFTW3_H
35 # include <fftw3.h>
36 #endif
37 
38 
39 struct Visualizer: Screen<NC::Window>, Tabbable
40 {
41 	Visualizer();
42 
43 	virtual void switchTo() override;
44 	virtual void resize() override;
45 
46 	virtual std::wstring title() override;
typeVisualizer47 	virtual ScreenType type() override { return ScreenType::Visualizer; }
48 
49 	virtual void update() override;
scrollVisualizer50 	virtual void scroll(NC::Scroll) override { }
51 
52 	virtual int windowTimeout() override;
53 
mouseButtonPressedVisualizer54 	virtual void mouseButtonPressed(MEVENT) override { }
55 
isLockableVisualizer56 	virtual bool isLockable() override { return true; }
isMergableVisualizer57 	virtual bool isMergable() override { return true; }
58 
59 	void Clear();
60 	void OpenDataSource();
61 	void CloseDataSource();
62 
63 	void ToggleVisualizationType();
64 	void FindOutputID();
65 	void ResetAutoScaleMultiplier();
66 
67 private:
68 	void DrawSoundWave(const int16_t *, ssize_t, size_t, size_t);
69 	void DrawSoundWaveStereo(const int16_t *, const int16_t *, ssize_t, size_t);
70 	void DrawSoundWaveFill(const int16_t *, ssize_t, size_t, size_t);
71 	void DrawSoundWaveFillStereo(const int16_t *, const int16_t *, ssize_t, size_t);
72 	void DrawSoundEllipse(const int16_t *, ssize_t, size_t, size_t);
73 	void DrawSoundEllipseStereo(const int16_t *, const int16_t *, ssize_t, size_t);
74 #	ifdef HAVE_FFTW3_H
75 	void DrawFrequencySpectrum(const int16_t *, ssize_t, size_t, size_t);
76 	void DrawFrequencySpectrumStereo(const int16_t *, const int16_t *, ssize_t, size_t);
77 	void ApplyWindow(double *, const int16_t *, ssize_t);
78 	void GenLogspace();
79 	double Bin2Hz(size_t);
80 	double Interpolate(size_t, size_t);
81 #	endif // HAVE_FFTW3_H
82 
83 	void InitDataSource();
84 	void InitVisualization();
85 
86 	void (Visualizer::*draw)(const int16_t *, ssize_t, size_t, size_t);
87 	void (Visualizer::*drawStereo)(const int16_t *, const int16_t *, ssize_t, size_t);
88 
89 	int m_output_id;
90 	bool m_reset_output;
91 
92 	int m_source_fd;
93 	std::string m_source_location;
94 	std::string m_source_port;
95 
96 	std::vector<int16_t> m_rendered_samples;
97 	std::vector<int16_t> m_incoming_samples;
98 	SampleBuffer m_buffered_samples;
99 	size_t m_sample_consumption_rate;
100 	size_t m_sample_consumption_rate_up_ctr;
101 	size_t m_sample_consumption_rate_dn_ctr;
102 
103 	double m_auto_scale_multiplier;
104 #	ifdef HAVE_FFTW3_H
105 	size_t m_fftw_results;
106 	double *m_fftw_input;
107 	fftw_complex *m_fftw_output;
108 	fftw_plan m_fftw_plan;
109 	const uint32_t DFT_NONZERO_SIZE;
110 	const uint32_t DFT_TOTAL_SIZE;
111 	const double DYNAMIC_RANGE;
112 	const double HZ_MIN;
113 	const double HZ_MAX;
114 	const double GAIN;
115 	const std::wstring SMOOTH_CHARS;
116 	std::vector<double> m_dft_logspace;
117 	std::vector<std::pair<size_t, double>> m_bar_heights;
118 
119 	std::vector<double> m_freq_magnitudes;
120 #	endif // HAVE_FFTW3_H
121 };
122 
123 extern Visualizer *myVisualizer;
124 
125 #endif // ENABLE_VISUALIZER
126 
127 #endif // NCMPCPP_VISUALIZER_H
128 
129 /* vim: set tabstop=4 softtabstop=4 shiftwidth=4 noexpandtab : */
130