2 * Copyright (C) 2016-2017 Robin Gareus <robin@gareus.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
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.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 #ifndef _dsp_filter_h_
19 #define _dsp_filter_h_
28 #include "pbd/malign.h"
30 #include "ardour/buffer_set.h"
31 #include "ardour/chan_mapping.h"
32 #include "ardour/libardour_visibility.h"
33 #include "ardour/types.h"
35 namespace ARDOUR { namespace DSP {
37 /** C/C++ Shared Memory
39 * A convenience class representing a C array of float[] or int32_t[]
40 * data values. This is useful for lua scripts to perform DSP operations
41 * directly using C/C++ with CPU Hardware acceleration.
43 * Access to this memory area is always 4 byte aligned. The data
44 * is interpreted either as float or as int.
46 * This memory area can also be shared between different instances
47 * or the same lua plugin (DSP, GUI).
49 * Since memory allocation is not realtime safe it should be
50 * allocated during dsp_init() or dsp_configure().
51 * The memory is free()ed automatically when the lua instance is
60 assert (sizeof(float) == sizeof (int32_t));
61 assert (sizeof(float) == sizeof (int));
66 cache_aligned_free (_data);
69 /** [re] allocate memory in host's memory space
71 * @param s size, total number of float or integer elements to store.
73 void allocate (size_t s) {
74 if (s == _size) { return; }
75 cache_aligned_free (_data);
76 cache_aligned_malloc ((void**) &_data, sizeof (float) * s);
77 if (_data) { _size = s; }
80 /** clear memory (set to zero) */
82 memset (_data, 0, sizeof(float) * _size);
85 /** access memory as float array
87 * @param off offset in shared memory region
90 float* to_float (size_t off) {
91 if (off >= _size) { return 0; }
92 return &(((float*)_data)[off]);
95 /** access memory as integer array
97 * @param off offset in shared memory region
100 int32_t* to_int (size_t off) {
101 if (off >= _size) { return 0; }
102 return &(((int32_t*)_data)[off]);
105 /** atomically set integer at offset
107 * This involves a memory barrier. This call
108 * is intended for buffers which are
109 * shared with another instance.
111 * @param off offset in shared memory region
112 * @param val value to set
114 void atomic_set_int (size_t off, int32_t val) {
115 g_atomic_int_set (&(((int32_t*)_data)[off]), val);
118 /** atomically read integer at offset
120 * This involves a memory barrier. This call
121 * is intended for buffers which are
122 * shared with another instance.
124 * @param off offset in shared memory region
125 * @returns value at offset
127 int32_t atomic_get_int (size_t off) {
128 return g_atomic_int_get (&(((int32_t*)_data)[off]));
136 /** lua wrapper to memset() */
137 void memset (float *data, const float val, const uint32_t n_samples);
139 * multiply every sample of `data' with the corresponding sample at `mult'.
141 * @param data multiplicand
142 * @param mult multiplicand
143 * @param n_samples number of samples in data and mmult
145 void mmult (float *data, float *mult, const uint32_t n_samples);
148 * @param data data to analyze
149 * @param min result, minimum value found in range
150 * @param max result, max value found in range
151 * @param n_samples number of samples to analyze
153 void peaks (const float *data, float &min, float &max, uint32_t n_samples);
155 /** non-linear power-scale meter deflection
157 * @param power signal power (dB)
158 * @returns deflected value
160 float log_meter (float power);
161 /** non-linear power-scale meter deflection
163 * @param coeff signal value
164 * @returns deflected value
166 float log_meter_coeff (float coeff);
168 void process_map (BufferSet* bufs,
169 const ChanMapping& in,
170 const ChanMapping& out,
171 pframes_t nframes, samplecnt_t offset,
174 /** 1st order Low Pass filter */
175 class LIBARDOUR_API LowPass {
177 /** instantiate a LPF
179 * @param samplerate samplerate
180 * @param freq cut-off frequency
182 LowPass (double samplerate, float freq);
183 /** process audio data
185 * @param data pointer to audio-data
186 * @param n_samples number of samples to process
188 void proc (float *data, const uint32_t n_samples);
189 /** filter control data
191 * This is useful for parameter smoothing.
193 * @param data pointer to control-data array
194 * @param val target value
195 * @param array length
197 void ctrl (float *data, const float val, const uint32_t n_samples);
198 /** update filter cut-off frequency
200 * @param freq cut-off frequency
202 void set_cutoff (float freq);
203 /** reset filter state */
204 void reset () { _z = 0.f; }
212 class LIBARDOUR_API Biquad {
226 /** Instantiate Biquad Filter
228 * @param samplerate Samplerate
230 Biquad (double samplerate);
231 Biquad (const Biquad &other);
233 /** process audio data
235 * @param data pointer to audio-data
236 * @param n_samples number of samples to process
238 void run (float *data, const uint32_t n_samples);
239 /** setup filter, compute coefficients
241 * @param t filter type (LowPass, HighPass, etc)
242 * @param freq filter frequency
243 * @param Q filter quality
244 * @param gain filter gain
246 void compute (Type t, double freq, double Q, double gain);
248 /** setup filter, set coefficients directly */
249 void configure (double a1, double a2, double b0, double b1, double b2);
251 /** filter transfer function (filter response for spectrum visualization)
252 * @param freq frequency
253 * @return gain at given frequency in dB (clamped to -120..+120)
255 float dB_at_freq (float freq) const;
257 /** reset filter state */
258 void reset () { _z1 = _z2 = 0.0; }
263 double _b0, _b1, _b2;
266 class LIBARDOUR_API FFTSpectrum {
268 FFTSpectrum (uint32_t window_size, double rate);
271 /** set data to be analyzed and pre-process with hanning window
272 * n_samples + offset must not be larger than the configured window_size
274 * @param data raw audio data
275 * @param n_samples number of samples to write to analysis buffer
276 * @param offset destination offset
278 void set_data_hann (float const * const data, const uint32_t n_samples, const uint32_t offset = 0);
280 /** process current data in buffer */
284 * @param bin the frequency bin 0 .. window_size / 2
285 * @param norm gain factor (set equal to @bin for 1/f normalization)
286 * @return signal power at given bin (in dBFS)
288 float power_at_bin (const uint32_t bin, const float norm = 1.f) const;
290 float freq_at_bin (const uint32_t bin) const {
291 return bin * _fft_freq_per_bin;
295 static Glib::Threads::Mutex fft_planner_lock;
298 void init (uint32_t window_size, double rate);
301 uint32_t _fft_window_size;
302 uint32_t _fft_data_size;
303 double _fft_freq_per_bin;
306 float* _fft_data_out;
312 class LIBARDOUR_API Generator {
322 void run (float *data, const uint32_t n_samples);
323 void set_type (Type t);
327 float randf () { return (randi () / 1073741824.f) - 1.f; }
333 float _b0, _b1, _b2, _b3, _b4, _b5, _b6;