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: 04 мая 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 PLUGINS_TRIGGER_H_
23 #define PLUGINS_TRIGGER_H_
24 
25 #include <metadata/plugins.h>
26 
27 #include <core/plugin.h>
28 #include <core/protocol/midi.h>
29 #include <core/util/MeterGraph.h>
30 #include <core/util/Blink.h>
31 #include <core/util/Sidechain.h>
32 
33 #include <plugins/sampler.h>
34 
35 namespace lsp
36 {
37     class trigger_base: public plugin_t, public trigger_base_metadata
38     {
39         protected:
40             enum state_t
41             {
42                 T_OFF,
43                 T_DETECT,
44                 T_ON,
45                 T_RELEASE
46             };
47 
48             enum source_t
49             {
50                 S_MIDDLE,
51                 S_SIDE,
52                 S_LEFT,
53                 S_RIGHT
54             };
55 
56             enum mode_t
57             {
58                 M_PEAK,
59                 M_RMS,
60                 M_LPF,
61                 M_UNIFORM,
62             };
63 
64             typedef struct channel_t
65             {
66                 float      *vCtl;           // Control chain
67                 Bypass      sBypass;        // Bypass
68                 MeterGraph  sGraph;         // Metering graph
69                 bool        bVisible;       // Visibility flag
70 
71                 IPort      *pIn;            // Input port
72                 IPort      *pOut;           // Output port
73                 IPort      *pGraph;         // Graph port
74                 IPort      *pMeter;         // Metering port
75                 IPort      *pVisible;       // Visibility port
76             } channel_t;
77 
78         protected:
79             // Sidechain
80             Sidechain               sSidechain;             // Sidechain
81             Equalizer               sScEq;                  // Sidechain equalizer
82             float                  *vTmp;                   // Temporary buffer
83 
84             // Instantiation parameters
85             size_t                  nFiles;                 // Number of files
86             size_t                  nChannels;              // Number of channels
87             bool                    bMidiPorts;             // Has MIDI port
88 
89             // Processors and buffers
90             sampler_kernel          sKernel;                // Output kernel
91             MeterGraph              sFunction;              // Function
92             MeterGraph              sVelocity;              // Trigger velocity level
93             Blink                   sActive;                // Activity blink
94             channel_t               vChannels[TRACKS_MAX];  // Output channels
95             float                  *vTimePoints;            // Time points buffer
96 
97             // Processing variables
98             ssize_t                 nCounter;               // Counter for detect/release
99             size_t                  nState;                 // Trigger state
100             float                   fVelocity;              // Current velocity value
101             bool                    bFunctionActive;        // Function activity
102             bool                    bVelocityActive;        // Velocity activity
103 
104             // Parameters
105             size_t                  nNote;                  // Trigger note
106             size_t                  nChannel;               // Channel
107             float                   fDry;                   // Dry amount
108             float                   fWet;                   // Wet amount
109             bool                    bPause;                 // Pause analysis refresh
110             bool                    bClear;                 // Clear analysis
111             bool                    bUISync;                // Synchronize with UI
112 
113             size_t                  nDetectCounter;         // Detect counter
114             size_t                  nReleaseCounter;        // Release counter
115             float                   fDetectLevel;           // Detection level
116             float                   fDetectTime;            // Trigger detection time
117             float                   fReleaseLevel;          // Release level
118             float                   fReleaseTime;           // Release time
119             float                   fDynamics;              // Dynamics
120             float                   fDynaTop;               // Dynamics top
121             float                   fDynaBottom;            // Dynamics bottom
122             float_buffer_t         *pIDisplay;              // Inline display buffer
123 
124             // Control ports
125             IPort                  *pFunction;              // Trigger function
126             IPort                  *pFunctionLevel;         // Function level
127             IPort                  *pFunctionActive;        // Function activity
128             IPort                  *pVelocity;              // Trigger velocity
129             IPort                  *pVelocityLevel;         // Trigger velocity level
130             IPort                  *pVelocityActive;        // Trigger velocity activity
131             IPort                  *pActive;                // Trigger activity flag
132 
133             IPort                  *pMidiIn;                // MIDI input port
134             IPort                  *pMidiOut;               // MIDI output port
135             IPort                  *pChannel;               // Note port
136             IPort                  *pNote;                  // Note port
137             IPort                  *pOctave;                // Octave port
138             IPort                  *pMidiNote;              // Output midi note #
139 
140             IPort                  *pBypass;                // Bypass port
141             IPort                  *pDry;                   // Dry output
142             IPort                  *pWet;                   // Wet output
143             IPort                  *pGain;                  // Gain output
144             IPort                  *pPause;                 // Pause analysis
145             IPort                  *pClear;                 // Clear analysis
146             IPort                  *pPreamp;                // Pre-amplification
147             IPort                  *pScHpfMode;             // Sidechain high-pass filter mode
148             IPort                  *pScHpfFreq;             // Sidechain high-pass filter frequency
149             IPort                  *pScLpfMode;             // Sidechain low-pass filter mode
150             IPort                  *pScLpfFreq;             // Sidechain low-pass filter frequency
151 
152             IPort                  *pSource;                // Source port
153             IPort                  *pMode;                  // Mode port
154             IPort                  *pDetectLevel;           // Detection level port
155             IPort                  *pDetectTime;            // Detection time
156             IPort                  *pReleaseLevel;          // Release level port
157             IPort                  *pReleaseTime;           // Release time
158             IPort                  *pDynamics;              // Dynamics
159             IPort                  *pDynaRange1;            // Dynamics range 1
160             IPort                  *pDynaRange2;            // Dynamics range 1
161             IPort                  *pReactivity;            // Reactivity
162             IPort                  *pReleaseValue;          // Release value
163 
164         protected:
165             void                trigger_on(size_t timestamp, float level);
166             void                trigger_off(size_t timestamp, float level);
167             void                process_samples(const float *sc, size_t samples);
168             inline void         update_counters();
169             size_t              decode_mode();
170             size_t              decode_source();
171 
172         public:
173             explicit trigger_base(const plugin_metadata_t &metadata, size_t files, size_t channels, bool midi);
174             virtual ~trigger_base();
175 
176         public:
177             virtual void init(IWrapper *wrapper);
178             virtual void destroy();
179 
180             virtual void update_settings();
181             virtual void update_sample_rate(long sr);
182             virtual void ui_activated();
183 
184             virtual void process(size_t samples);
185             virtual bool inline_display(ICanvas *cv, size_t width, size_t height);
186 
187     };
188 
189     class trigger_mono: public trigger_base, public trigger_mono_metadata
190     {
191         public:
192             explicit trigger_mono();
193             virtual ~trigger_mono();
194     };
195 
196     class trigger_stereo: public trigger_base, public trigger_stereo_metadata
197     {
198         public:
199             explicit trigger_stereo();
200             virtual ~trigger_stereo();
201     };
202 
203     class trigger_midi_mono: public trigger_base, public trigger_midi_mono_metadata
204     {
205         public:
206             explicit trigger_midi_mono();
207             virtual ~trigger_midi_mono();
208     };
209 
210     class trigger_midi_stereo: public trigger_base, public trigger_midi_stereo_metadata
211     {
212         public:
213             explicit trigger_midi_stereo();
214             virtual ~trigger_midi_stereo();
215     };
216 
217 }
218 
219 #endif /* PLUGINS_TRIGGER_H_ */
220