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: 21 янв. 2015 г.
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_PLUGINS_SPECTRUM_ANALYZER_H_
23 #define CORE_PLUGINS_SPECTRUM_ANALYZER_H_
24 
25 #include <core/plugin.h>
26 #include <core/util/Analyzer.h>
27 #include <core/util/Counter.h>
28 
29 #include <metadata/plugins.h>
30 
31 namespace lsp
32 {
33 
34     class spectrum_analyzer_base: public plugin_t
35     {
36         protected:
37             typedef struct sa_channel_t
38             {
39                 bool        bOn;                // Enabled flag
40                 bool        bFreeze;            // Freeze flag
41                 bool        bSolo;              // Soloing flag
42                 bool        bSend;              // Send to UI flag
43                 float       fGain;              // Makeup gain
44                 float       fHue;               // Hue
45                 float      *vIn;                // Input buffer pointer
46                 float      *vOut;               // Output buffer pointer
47 
48                 // Port references
49                 IPort      *pIn;                // Input samples
50                 IPort      *pOut;               // Output samples
51                 IPort      *pOn;                // FFT on
52                 IPort      *pSolo;              // Soloing flag
53                 IPort      *pFreeze;            // Freeze flag
54                 IPort      *pHue;               // Hue of the graph color
55                 IPort      *pShift;             // Shift gain
56             } sa_channel_t;
57 
58             typedef struct sa_spectralizer_t
59             {
60                 size_t      nPortId;            // Last port identifier
61                 ssize_t     nChannelId;         // Channel identifier
62 
63                 IPort      *pPortId;            // Port identifier
64                 IPort      *pFBuffer;           // Frame buffer port
65             } sa_spectralizer_t;
66 
67             enum mode_t
68             {
69                 SA_ANALYZER,
70                 SA_ANALYZER_STEREO,
71                 SA_MASTERING,
72                 SA_MASTERING_STEREO,
73                 SA_SPECTRALIZER,
74                 SA_SPECTRALIZER_STEREO
75             };
76 
77             enum flags_t
78             {
79                 F_MASTERING     = 1 << 0,
80                 F_SMOOTH_LOG    = 1 << 1,
81                 F_LOG_SCALE     = 1 << 2,
82                 F_BOOST         = 1 << 3
83             };
84 
85         protected:
86             bool                create_channels(size_t channels);
87             mode_t              decode_mode(size_t mode);
88 
89         protected:
90             Analyzer            sAnalyzer;
91             Counter             sCounter;
92             size_t              nChannels;
93             sa_channel_t       *vChannels;
94             float             **vAnalyze;           // Analysis buffers
95             float              *vFrequences;
96             float              *vMFrequences;
97             uint32_t           *vIndexes;
98             uint8_t            *pData;
99 
100             bool                bBypass;
101             size_t              nChannel;
102             float               fSelector;
103             float               fMinFreq;
104             float               fMaxFreq;
105             float               fReactivity;        // Reactivity
106             float               fTau;               // Time constant (dependent on reactivity)
107             float               fPreamp;            // Preamplification level
108             float               fZoom;              // Zoom
109             mode_t              enMode;
110             bool                bLogScale;
111 
112             IPort              *pBypass;
113             IPort              *pMode;
114             IPort              *pTolerance;
115             IPort              *pWindow;
116             IPort              *pEnvelope;
117             IPort              *pPreamp;
118             IPort              *pZoom;
119             IPort              *pReactivity;
120             IPort              *pChannel;
121             IPort              *pSelector;
122             IPort              *pFrequency;
123             IPort              *pLevel;
124             IPort              *pLogScale;
125             IPort              *pFftData;
126 
127             IPort              *pFreeze;
128             IPort              *pSpp;
129             sa_spectralizer_t   vSpc[2];
130 
131             float_buffer_t     *pIDisplay;      // Inline display buffer
132 
133         protected:
134             void                update_multiple_settings();
135             void                update_x2_settings(ssize_t ch1, ssize_t ch2);
136             void                update_spectralizer_x2_settings(ssize_t ch1, ssize_t ch2);
137 
138             void                process_multiple();
139             void                process_spectralizer();
140             void                get_spectrum(float *dst, size_t channel, size_t flags);
141 
142         public:
143             explicit spectrum_analyzer_base(const plugin_metadata_t &metadata);
144             virtual ~spectrum_analyzer_base();
145 
146         public:
147             virtual void init(IWrapper *wrapper);
148             virtual void destroy();
149 
150             virtual void update_settings();
151             virtual void update_sample_rate(long sr);
152 
153             virtual void process(size_t samples);
154             virtual bool inline_display(ICanvas *cv, size_t width, size_t height);
155     };
156 
157     class spectrum_analyzer_x1: public spectrum_analyzer_base, public spectrum_analyzer_x1_metadata
158     {
159         public:
160             spectrum_analyzer_x1();
161     };
162 
163     class spectrum_analyzer_x2: public spectrum_analyzer_base, public spectrum_analyzer_x2_metadata
164     {
165         public:
166             spectrum_analyzer_x2();
167     };
168 
169     class spectrum_analyzer_x4: public spectrum_analyzer_base, public spectrum_analyzer_x4_metadata
170     {
171         public:
172             spectrum_analyzer_x4();
173     };
174 
175 
176     class spectrum_analyzer_x8: public spectrum_analyzer_base, public spectrum_analyzer_x8_metadata
177     {
178         public:
179             spectrum_analyzer_x8();
180     };
181 
182     class spectrum_analyzer_x12: public spectrum_analyzer_base, public spectrum_analyzer_x12_metadata
183     {
184         public:
185             spectrum_analyzer_x12();
186     };
187 
188     class spectrum_analyzer_x16: public spectrum_analyzer_base, public spectrum_analyzer_x16_metadata
189     {
190         public:
191             spectrum_analyzer_x16();
192     };
193 
194 } /* namespace ddb */
195 
196 #endif /* CORE_PLUGINS_SPECTRUM_ANALYZER_H_ */
197