File: Trigger.h

package info (click to toggle)
lsp-plugins 1.2.5-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 91,856 kB
  • sloc: cpp: 427,831; xml: 57,779; makefile: 9,961; php: 1,005; sh: 18
file content (262 lines) | stat: -rw-r--r-- 8,154 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*
 * Copyright (C) 2021 Linux Studio Plugins Project <https://lsp-plug.in/>
 *           (C) 2021 Vladimir Sadovnikov <sadko4u@gmail.com>
 *
 * This file is part of lsp-dsp-units
 * Created on: 13 авг. 2021 г.
 *
 * lsp-dsp-units is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * lsp-dsp-units is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with lsp-dsp-units. If not, see <https://www.gnu.org/licenses/>.
 */

#ifndef LSP_PLUG_IN_DSP_UNITS_UTIL_TRIGGER_H_
#define LSP_PLUG_IN_DSP_UNITS_UTIL_TRIGGER_H_

#include <lsp-plug.in/dsp-units/version.h>
#include <lsp-plug.in/dsp-units/iface/IStateDumper.h>

namespace lsp
{
    namespace dspu
    {
        enum trg_mode_t
        {
            TRG_MODE_SINGLE,
            TRG_MODE_MANUAL,
            TRG_MODE_REPEAT,
            TRG_MODE_MAX
        };

        enum trg_type_t
        {
            TRG_TYPE_NONE,
            TRG_TYPE_SIMPLE_RISING_EDGE,
            TRG_TYPE_SIMPLE_FALLING_EDGE,
            TRG_TYPE_ADVANCED_RISING_EDGE,
            TRG_TYPE_ADVANCED_FALLING_EDGE,
            TRG_TYPE_MAX
        };

        enum trg_state_t
        {
            TRG_STATE_WAITING,
            TRG_STATE_ARMED,
            TRG_STATE_FIRED,
            TRG_STATE_MAX
        };

        class LSP_DSP_UNITS_PUBLIC Trigger
        {
            private:
                Trigger & operator = (const Trigger &);
                Trigger(const Trigger &);

            protected:
                // To use with Manual and Single Mode
                typedef struct trg_locks_t
                {
                    bool    bSingleLock;
                    bool    bManualAllow;
                    bool    bManualLock;
                } trg_locks_t;

                typedef struct simple_trg_t
                {
                    float   fThreshold;
                } simple_trg_t;

                typedef struct advanced_trg_t
                {
                    float   fThreshold;
                    float   fHysteresis;
                    float   fLowerThreshold;
                    float   fUpperThreshold;
                    bool    bDisarm;
                } advanced_trg_t;

            private:
                float           fPrevious;

                trg_mode_t      enTriggerMode;
                trg_type_t      enTriggerType;
                trg_state_t     enTriggerState;

                size_t          nTriggerHold;
                size_t          nTriggerHoldCounter;

                trg_locks_t     sLocks;

                simple_trg_t    sSimpleTrg;
                advanced_trg_t  sAdvancedTrg;

                bool            bSync;

            public:
                explicit Trigger();
                ~Trigger();

                void            construct();
                void            destroy();

            protected:
                inline void set_simple_trg_threshold(float threshold)
                {
                    sSimpleTrg.fThreshold = threshold;
                }

                inline void update_advanced_trg()
                {
                    sAdvancedTrg.fLowerThreshold = sAdvancedTrg.fThreshold - sAdvancedTrg.fHysteresis;
                    sAdvancedTrg.fUpperThreshold = sAdvancedTrg.fThreshold + sAdvancedTrg.fHysteresis;
                }

                inline void set_advanced_trg_threshold(float threshold)
                {
                    sAdvancedTrg.fThreshold = threshold;

                    update_advanced_trg();
                }

                inline void set_advanced_trg_hysteresis(float hysteresis)
                {
                    if (hysteresis < 0.0f)
                        hysteresis = -hysteresis;

                    sAdvancedTrg.fHysteresis = hysteresis;

                    update_advanced_trg();
                }

            public:

                /** Check that trigger needs settings update.
                 *
                 * @return true if trigger needs settings update.
                 */
                inline bool needs_update() const
                {
                    return bSync;
                }

                /** This method should be called if needs_update() returns true.
                 * before calling process() methods.
                 *
                 */
                void update_settings();

                /** Set the trigger mode.
                 *
                 * @param mode trigger mode.
                 */
                inline void set_trigger_mode(trg_mode_t mode)
                {
                    if ((mode < TRG_MODE_SINGLE) || (mode >= TRG_MODE_MAX) || (enTriggerMode == mode))
                        return;

                    enTriggerMode = mode;
                    bSync = true;
                }

                /** Reset the single trigger.
                 *
                 */
                inline void reset_single_trigger()
                {
                    sLocks.bSingleLock = false;
                    bSync = true;
                }

                /** Activate the manual trigger.
                 *
                 */
                inline void activate_manual_trigger()
                {
                    sLocks.bManualAllow = true;
                    sLocks.bManualLock = false;
                    bSync = true;
                }

                /** Set the post-trigger samples. The trigger can be allowed to fire only after the post-trigger samples have elapsed.
                 *
                 * @param nSamples number of post-trigger samples.
                 */
                inline void set_trigger_hold_samples(size_t nSamples)
                {
                    if (nSamples == nTriggerHold)
                        return;

                    nTriggerHold = nSamples;
                    nTriggerHoldCounter = 0;
                }

                /** Set the trigger type.
                 *
                 * @param type trigger type.
                 */
                inline void set_trigger_type(trg_type_t type)
                {
                    if ((type < TRG_TYPE_NONE) || (type >= TRG_TYPE_MAX) || (enTriggerType == type))
                        return;

                    enTriggerType = type;
                    bSync = true;
                }

                /** Set the trigger threshold.
                 *
                 * @param trigger threshold.
                 */
                inline void set_trigger_threshold(float threshold)
                {
                    set_simple_trg_threshold(threshold);
                    set_advanced_trg_threshold(threshold);
                    bSync = true;
                }

                /** Set the trigger hysteresis.
                 *
                 * @param hysteresis hysteresis.
                 */
                inline void set_trigger_hysteresis(float hysteresis)
                {
                    set_advanced_trg_hysteresis(hysteresis);
                    bSync = true;
                }

                /** Return he trigger state.
                 *
                 * @return trigger state.
                 */
                inline trg_state_t get_trigger_state() const
                {
                    return enTriggerState;
                }

                /** Feed a single sample to the trigger. Query the trigger status afterwards.
                 *
                 */
                void single_sample_processor(float value);

                /**
                 * Dump the state
                 * @param dumper dumper
                 */
                void dump(IStateDumper *v) const;
        };
    }
}




#endif /* LSP_PLUG_IN_DSP_UNITS_UTIL_TRIGGER_H_ */