1 /*
2  * Copyright (C) 2020 Linux Studio Plugins Project <https://lsp-plug.in/>
3  *           (C) 2020 Vladimir Sadovnikov <sadko4u@gmail.com>
4  *
5  * This file is part of lsp-plugins
6  * Created on: 26 июля 2016 г.
7  *
8  * lsp-plugins is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * any later version.
12  *
13  * lsp-plugins is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with lsp-plugins. If not, see <https://www.gnu.org/licenses/>.
20  */
21 
22 #ifndef CORE_FILTERS_EQUALIZER_H_
23 #define CORE_FILTERS_EQUALIZER_H_
24 
25 #include <core/IStateDumper.h>
26 #include <core/filters/FilterBank.h>
27 #include <core/filters/Filter.h>
28 
29 namespace lsp
30 {
31     enum equalizer_mode_t
32     {
33         EQM_BYPASS, // Bypass signal
34         EQM_IIR,    // All filters are recursive filters with infinite impulse response filters
35         EQM_FIR,    // All filters are non-recursive filters with finite impulse response filters
36         EQM_FFT,    // Approximation of the frequency chart in the frequency range
37         EQM_SPM     // Equalizer acts as a Spectral Processing Module
38     };
39 
40     /**
41      * Set of multiple controllable sequential filters combined into one managed object
42      */
43     class Equalizer
44     {
45         private:
46             Equalizer & operator = (const Equalizer &);
47 
48         protected:
49             enum eq_flags_t
50             {
51                 EF_REBUILD = 1 << 0,
52                 EF_CLEAR   = 1 << 1
53             };
54 
55         protected:
56             FilterBank          sBank;              // Filter bank
57             Filter             *vFilters;           // List of filters
58             size_t              nFilters;           // Number of filters
59             size_t              nSampleRate;        // Sample rate
60             size_t              nFirSize;           // FIR filter size
61             size_t              nFirRank;           // FFT rank
62             size_t              nLatency;           // Equalizer latency
63             size_t              nBufSize;           // Buffer size
64             equalizer_mode_t    nMode;              // Equalizer mode
65 
66             float              *vInBuffer;          // Input buffer data
67             float              *vOutBuffer;         // Output buffer data
68             float              *vConv;              // Convolution data
69             float              *vFft;               // FFT transform data buffer (real + imaginary)
70             float              *vTemp;              // Temporary buffer for miscellaneous calculations
71 
72             size_t              nFlags;             // Flag that identifies that equalizer has to be rebuilt
73             uint8_t            *pData;              // Allocation data
74 
75         protected:
76             void                reconfigure();
77 
78         public:
79             explicit Equalizer();
80             ~Equalizer();
81 
82             /**
83              * Construct the object being part of memory chunk
84              */
85             void                construct();
86 
87             /** Initialize equalizer
88              *
89              * @param filters number of filters
90              * @param fir FIR filter rank (impulse response size)
91              * @return true on success
92              */
93             bool                init(size_t filters, size_t fir_rank);
94 
95             /** Destroy equalizer
96              *
97              */
98             void                destroy();
99 
100         public:
101             /** Update filter parameters
102              * @param id ID of the filter
103              * @param params  filter parameters
104              * @return true on success
105              */
106             bool                set_params(size_t id, const filter_params_t *params);
107 
108             /** Get filter parameters
109              * @param id ID of the filter
110              * @param params  filter parameters
111              * @return true on success
112              */
113             bool                get_params(size_t id, filter_params_t *params);
114 
115             /** Check that filter is active
116              *
117              * @param id ID of filter
118              * @return true if filter is active
119              */
filter_active(size_t id)120             inline bool         filter_active(size_t id) const { return (id < nFilters) ? vFilters[id].active() : false; }
121 
122             /** Check that filter is inactive
123              *
124              * @param id ID of filter
125              * @return true if filter is inactive
126              */
filter_inactive(size_t id)127             inline bool         filter_inactive(size_t id) const { return (id < nFilters) ? vFilters[id].inactive() : false; }
128 
129             /** Set equalizer mode
130              *
131              * @param mode equalizer mode
132              */
133             void                set_mode(equalizer_mode_t mode);
134 
135             /** Set sample rate
136              *
137              * @param sr sample rate
138              */
139             void                set_sample_rate(size_t sr);
140 
141             /** Get equalizer mode
142              *
143              * @return equalizer mode
144              */
get_mode()145             inline equalizer_mode_t get_mode() const { return nMode; }
146 
147             /** Get equalizer latency
148              *
149              * @return equalizer latency
150              */
151             size_t              get_latency();
152 
153             /**
154              * Get maximum possible latency for the equalizer
155              * @return maximum possible latency
156              */
max_latency()157             inline size_t       max_latency() const { return nFirSize + (nFirSize >> 1); }
158 
159             /** Get frequency chart of the specific filter
160              *
161              * @param id ID of the filter
162              * @param re real part of the frequency chart
163              * @param im imaginary part of the frequency chart
164              * @param f frequencies to calculate value
165              * @param count number of dots for the chart
166              * @return status of operation
167              */
168             bool                freq_chart(size_t id, float *re, float *im, const float *f, size_t count);
169 
170             /** Get frequency chart of the specific filter
171              *
172              * @param id ID of the filter
173              * @param c complex numbers that contain the filter transfer function
174              * @param f frequencies to calculate filter transfer function
175              * @param count number of points
176              * @return status of operation
177              */
178             bool                freq_chart(size_t id, float *c, const float *f, size_t count);
179 
180             /**
181              * Get frequency chart of the whole equalizer
182              * @param re real part of the frequency chart
183              * @param im imaginary part of the frequency chart
184              * @param f frequencies to calculate value
185              * @param count number of dots for the chart
186              */
187             void                freq_chart(float *re, float *im, const float *f, size_t count);
188 
189             /**
190              * Get frequency chart of the whole equalizer
191              * @param c complex numbers that contain the filter transfer function
192              * @param f frequencies to calculate filter transfer function
193              * @param count number of points
194              */
195             void                freq_chart(float *c, const float *f, size_t count);
196 
197             /** Process the signal
198              *
199              * @param out output signal samples
200              * @param in input signal samples
201              * @param samples number of samples to process
202              */
203             void                process(float *out, const float *in, size_t samples);
204 
205             /**
206              * Dump the state
207              * @param dumper dumper
208              */
209             void                dump(IStateDumper *v) const;
210     };
211 
212 } /* namespace lsp */
213 
214 #endif /* CORE_FILTERS_EQUALIZER_H_ */
215