1b4cb6eed83c53a62e72058ec671ea757da5e4ea
[ardour.git] / libs / ardour / ardour / dsp_filter.h
1 /*
2  * Copyright (C) 2016 Robin Gareus <robin@gareus.org>
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17  *
18  */
19 #ifndef _dsp_filter_h_
20 #define _dsp_filter_h_
21
22 #include <stdint.h>
23 #include <string.h>
24 #include <assert.h>
25 #include <glib.h>
26 #include "ardour/libardour_visibility.h"
27
28 namespace ARDOUR { namespace DSP {
29
30         /** a convenience class for lua scripts to use C memory for DSP operations.
31          *
32          * It should be allocated during dsp_init() or dsp_configure().
33          */
34         class DspShm {
35                 public:
36                         DspShm ()
37                                 : _data (0)
38                                 , _size (0)
39                         {
40                                 assert (sizeof(float) == sizeof (int32_t));
41                                 assert (sizeof(float) == sizeof (int));
42                         }
43
44                         ~DspShm () {
45                                 free (_data);
46                         }
47
48                         void allocate (size_t s) {
49                                 _data = realloc (_data, sizeof(float) * s);
50                                 if (_data) { _size = s; }
51                         }
52
53                         void clear () {
54                                 memset (_data, 0, sizeof(float) * _size);
55                         }
56
57                         float* to_float (size_t off) {
58                                 if (off >= _size) { return 0; }
59                                 return &(((float*)_data)[off]);
60                         }
61
62                         int32_t* to_int (size_t off) {
63                                 if (off >= _size) { return 0; }
64                                 return &(((int32_t*)_data)[off]);
65                         }
66
67                         void atomic_set_int (size_t off, int32_t val) {
68                                 g_atomic_int_set (&(((int32_t*)_data)[off]), val);
69                         }
70
71                         int32_t atomic_get_int (size_t off) {
72                                 return g_atomic_int_get (&(((int32_t*)_data)[off]));
73                         }
74
75                 private:
76                         void* _data;
77                         size_t _size;
78         };
79
80         void memset (float *data, const float val, const uint32_t n_samples);
81         void mmult (float *data, float *mult, const uint32_t n_samples);
82         void peaks (float *data, float &min, float &max, uint32_t n_samples);
83
84         float log_meter (float power);
85         float log_meter_coeff (float coeff);
86
87         class LIBARDOUR_API LowPass {
88                 public:
89                         LowPass (double samplerate, float freq);
90                         void proc (float *data, const uint32_t n_samples);
91                         void ctrl (float *data, const float val, const uint32_t n_samples);
92                         void set_cutoff (float freq);
93                         void reset () { _z =  0.f; }
94                 private:
95                         float _rate;
96                         float _z;
97                         float _a;
98         };
99
100         class LIBARDOUR_API BiQuad {
101                 public:
102                         enum Type {
103                                 LowPass,
104                                 HighPass,
105                                 BandPassSkirt,
106                                 BandPass0dB,
107                                 Notch,
108                                 AllPass,
109                                 Peaking,
110                                 LowShelf,
111                                 HighShelf
112                         };
113
114                         BiQuad (double samplerate);
115                         BiQuad (const BiQuad &other);
116
117                         void run (float *data, const uint32_t n_samples);
118                         void compute (Type, double freq, double Q, double gain);
119                         void reset () { _z1 = _z2 = 0.0; }
120                 private:
121                         double _rate;
122                         float  _z1, _z2;
123                         double _a1, _a2;
124                         double _b0, _b1, _b2;
125         };
126
127 } } /* namespace */
128 #endif