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