994d8725ea418576c6c3edacfacd779f49f8dcb1
[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         /** C/C++ Shared Memory
31          *
32          * A convenience class representing a C array of float[] or int32_t[]
33          * data values. This is useful for lua scripts to perform DSP operations
34          * directly using C/C++ with CPU Hardware acceleration.
35          *
36          * Access to this memory area is always 4 byte aligned. The data
37          * is interpreted either as float or as int.
38          *
39          * This memory area can also be shared between different instances
40          * or the same lua plugin (DSP, GUI).
41          *
42          * Since memory allocation is not realtime safe it should be
43          * allocated during dsp_init() or dsp_configure().
44          * The memory is free()ed automatically when the lua instance is
45          * destroyed.
46          */
47         class DspShm {
48                 public:
49                         DspShm ()
50                                 : _data (0)
51                                 , _size (0)
52                         {
53                                 assert (sizeof(float) == sizeof (int32_t));
54                                 assert (sizeof(float) == sizeof (int));
55                         }
56
57                         ~DspShm () {
58                                 free (_data);
59                         }
60
61                         /** [re] allocate memory in host's memory space
62                          *
63                          * @param s size, total number of float or integer elements to store.
64                          */
65                         void allocate (size_t s) {
66                                 _data = realloc (_data, sizeof(float) * s);
67                                 if (_data) { _size = s; }
68                         }
69
70                         /** clear memory (set to zero) */
71                         void clear () {
72                                 memset (_data, 0, sizeof(float) * _size);
73                         }
74
75                         /** access memory as float array
76                          *
77                          * @param off offset in shared memory region
78                          * @returns float[]
79                          */
80                         float* to_float (size_t off) {
81                                 if (off >= _size) { return 0; }
82                                 return &(((float*)_data)[off]);
83                         }
84
85                         /** access memory as integer array
86                          *
87                          * @param off offset in shared memory region
88                          * @returns int_32_t[]
89                          */
90                         int32_t* to_int (size_t off) {
91                                 if (off >= _size) { return 0; }
92                                 return &(((int32_t*)_data)[off]);
93                         }
94
95                         /** atomically set integer at offset
96                          *
97                          * This involves a memory barrier. This call
98                          * is intended for buffers which are
99                          * shared with another instance.
100                          *
101                          * @param off offset in shared memory region
102                          * @param val value to set
103                          */
104                         void atomic_set_int (size_t off, int32_t val) {
105                                 g_atomic_int_set (&(((int32_t*)_data)[off]), val);
106                         }
107
108                         /** atomically read integer at offset
109                          *
110                          * This involves a memory barrier. This call
111                          * is intended for buffers which are
112                          * shared with another instance.
113                          *
114                          * @param off offset in shared memory region
115                          * @returns value at offset
116                          */
117                         int32_t atomic_get_int (size_t off) {
118                                 return g_atomic_int_get (&(((int32_t*)_data)[off]));
119                         }
120
121                 private:
122                         void* _data;
123                         size_t _size;
124         };
125
126         /** lua wrapper to memset() */
127         void memset (float *data, const float val, const uint32_t n_samples);
128         /** matrix multiply
129          * multiply every sample of `data' with the corresponding sample at `mult'.
130          *
131          * @param data multiplicand
132          * @param mult multiplicand
133          * @param n_samples number of samples in data and mmult
134          */
135         void mmult (float *data, float *mult, const uint32_t n_samples);
136         /** calculate peaks
137          *
138          * @param data data to analyze
139          * @param min result, minimum value found in range
140          * @param max result, max value found in range
141          * @param n_samples number of samples to analyze
142          */
143         void peaks (float *data, float &min, float &max, uint32_t n_samples);
144
145         /** non-linear power-scale meter deflection
146          *
147          * @param power signal power (dB)
148          * @returns deflected value
149          */
150         float log_meter (float power);
151         /** non-linear power-scale meter deflection
152          *
153          * @param coeff signal value
154          * @returns deflected value
155          */
156         float log_meter_coeff (float coeff);
157
158         /** 1st order Low Pass filter */
159         class LIBARDOUR_API LowPass {
160                 public:
161                         /** instantiate a LPF
162                          *
163                          * @param samplerate samplerate
164                          * @param freq cut-off frequency
165                          */
166                         LowPass (double samplerate, float freq);
167                         /** process audio data
168                          *
169                          * @param data pointer to audio-data
170                          * @param n_samples number of samples to process
171                          */
172                         void proc (float *data, const uint32_t n_samples);
173                         /** filter control data
174                          *
175                          * This is useful for parameter smoothing.
176                          *
177                          * @param data pointer to control-data array
178                          * @param val target value
179                          * @param array length
180                          */
181                         void ctrl (float *data, const float val, const uint32_t n_samples);
182                         /** update filter cut-off frequency
183                          *
184                          * @param freq cut-off frequency
185                          */
186                         void set_cutoff (float freq);
187                         /** reset filter state */
188                         void reset () { _z =  0.f; }
189                 private:
190                         float _rate;
191                         float _z;
192                         float _a;
193         };
194
195         /** Biquad Filter */
196         class LIBARDOUR_API Biquad {
197                 public:
198                         enum Type {
199                                 LowPass,
200                                 HighPass,
201                                 BandPassSkirt,
202                                 BandPass0dB,
203                                 Notch,
204                                 AllPass,
205                                 Peaking,
206                                 LowShelf,
207                                 HighShelf
208                         };
209
210                         /** Instantiate Biquad Filter
211                          *
212                          * @param samplerate Samplerate
213                          */
214                         Biquad (double samplerate);
215                         Biquad (const Biquad &other);
216
217                         /** process audio data
218                          *
219                          * @param data pointer to audio-data
220                          * @param n_samples number of samples to process
221                          */
222                         void run (float *data, const uint32_t n_samples);
223                         /** setup filter, compute coefficients
224                          *
225                          * @param t filter type (LowPass, HighPass, etc)
226                          * @param freq filter frequency
227                          * @param Q filter quality
228                          * @param gain filter gain
229                          */
230                         void compute (Type t, double freq, double Q, double gain);
231
232                         /** filter transfer function (filter response for spectrum visualization)
233                          * @param freq frequency
234                          * @return gain at given frequency in dB (clamped to -120..+120)
235                          */
236                         float dB_at_freq (float freq) const;
237
238                         /** reset filter state */
239                         void reset () { _z1 = _z2 = 0.0; }
240                 private:
241                         double _rate;
242                         float  _z1, _z2;
243                         double _a1, _a2;
244                         double _b0, _b1, _b2;
245         };
246
247 } } /* namespace */
248 #endif