2 * Copyright (C) 2016 Robin Gareus <robin@gareus.org>
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.
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
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.
19 #ifndef _dsp_filter_h_
20 #define _dsp_filter_h_
28 #include "ardour/libardour_visibility.h"
30 namespace ARDOUR { namespace DSP {
32 /** C/C++ Shared Memory
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.
38 * Access to this memory area is always 4 byte aligned. The data
39 * is interpreted either as float or as int.
41 * This memory area can also be shared between different instances
42 * or the same lua plugin (DSP, GUI).
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
55 assert (sizeof(float) == sizeof (int32_t));
56 assert (sizeof(float) == sizeof (int));
63 /** [re] allocate memory in host's memory space
65 * @param s size, total number of float or integer elements to store.
67 void allocate (size_t s) {
68 _data = realloc (_data, sizeof(float) * s);
69 if (_data) { _size = s; }
72 /** clear memory (set to zero) */
74 memset (_data, 0, sizeof(float) * _size);
77 /** access memory as float array
79 * @param off offset in shared memory region
82 float* to_float (size_t off) {
83 if (off >= _size) { return 0; }
84 return &(((float*)_data)[off]);
87 /** access memory as integer array
89 * @param off offset in shared memory region
92 int32_t* to_int (size_t off) {
93 if (off >= _size) { return 0; }
94 return &(((int32_t*)_data)[off]);
97 /** atomically set integer at offset
99 * This involves a memory barrier. This call
100 * is intended for buffers which are
101 * shared with another instance.
103 * @param off offset in shared memory region
104 * @param val value to set
106 void atomic_set_int (size_t off, int32_t val) {
107 g_atomic_int_set (&(((int32_t*)_data)[off]), val);
110 /** atomically read integer at offset
112 * This involves a memory barrier. This call
113 * is intended for buffers which are
114 * shared with another instance.
116 * @param off offset in shared memory region
117 * @returns value at offset
119 int32_t atomic_get_int (size_t off) {
120 return g_atomic_int_get (&(((int32_t*)_data)[off]));
128 /** lua wrapper to memset() */
129 void memset (float *data, const float val, const uint32_t n_samples);
131 * multiply every sample of `data' with the corresponding sample at `mult'.
133 * @param data multiplicand
134 * @param mult multiplicand
135 * @param n_samples number of samples in data and mmult
137 void mmult (float *data, float *mult, const uint32_t n_samples);
140 * @param data data to analyze
141 * @param min result, minimum value found in range
142 * @param max result, max value found in range
143 * @param n_samples number of samples to analyze
145 void peaks (float *data, float &min, float &max, uint32_t n_samples);
147 /** non-linear power-scale meter deflection
149 * @param power signal power (dB)
150 * @returns deflected value
152 float log_meter (float power);
153 /** non-linear power-scale meter deflection
155 * @param coeff signal value
156 * @returns deflected value
158 float log_meter_coeff (float coeff);
160 /** 1st order Low Pass filter */
161 class LIBARDOUR_API LowPass {
163 /** instantiate a LPF
165 * @param samplerate samplerate
166 * @param freq cut-off frequency
168 LowPass (double samplerate, float freq);
169 /** process audio data
171 * @param data pointer to audio-data
172 * @param n_samples number of samples to process
174 void proc (float *data, const uint32_t n_samples);
175 /** filter control data
177 * This is useful for parameter smoothing.
179 * @param data pointer to control-data array
180 * @param val target value
181 * @param array length
183 void ctrl (float *data, const float val, const uint32_t n_samples);
184 /** update filter cut-off frequency
186 * @param freq cut-off frequency
188 void set_cutoff (float freq);
189 /** reset filter state */
190 void reset () { _z = 0.f; }
198 class LIBARDOUR_API Biquad {
212 /** Instantiate Biquad Filter
214 * @param samplerate Samplerate
216 Biquad (double samplerate);
217 Biquad (const Biquad &other);
219 /** process audio data
221 * @param data pointer to audio-data
222 * @param n_samples number of samples to process
224 void run (float *data, const uint32_t n_samples);
225 /** setup filter, compute coefficients
227 * @param t filter type (LowPass, HighPass, etc)
228 * @param freq filter frequency
229 * @param Q filter quality
230 * @param gain filter gain
232 void compute (Type t, double freq, double Q, double gain);
234 /** setup filter, set coefficients directly */
235 void configure (double a1, double a2, double b0, double b1, double b2);
237 /** filter transfer function (filter response for spectrum visualization)
238 * @param freq frequency
239 * @return gain at given frequency in dB (clamped to -120..+120)
241 float dB_at_freq (float freq) const;
243 /** reset filter state */
244 void reset () { _z1 = _z2 = 0.0; }
249 double _b0, _b1, _b2;
252 class LIBARDOUR_API FFTSpectrum {
254 FFTSpectrum (uint32_t window_size, double rate);
257 /** set data to be analyzed and pre-process with hanning window
258 * n_samples + offset must not be larger than the configured window_size
260 * @param data raw audio data
261 * @param n_samples number of samples to write to analysis buffer
262 * @param offset destination offset
264 void set_data_hann (float const * const data, const uint32_t n_samples, const uint32_t offset = 0);
266 /** process current data in buffer */
270 * @param bin the frequency bin 0 .. window_size / 2
271 * @param norm gain factor (set equal to @bin for 1/f normalization)
272 * @return signal power at given bin (in dBFS)
274 float power_at_bin (const uint32_t bin, const float norm = 1.f) const;
276 float freq_at_bin (const uint32_t bin) const {
277 return bin * _fft_freq_per_bin;
281 static Glib::Threads::Mutex fft_planner_lock;
284 void init (uint32_t window_size, double rate);
287 uint32_t _fft_window_size;
288 uint32_t _fft_data_size;
289 double _fft_freq_per_bin;
292 float* _fft_data_out;