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