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));
64 /** [re] allocate memory in host's memory space
66 * @param s size, total number of float or integer elements to store.
68 void allocate (size_t s) {
69 if (s == _size) { return; }
70 _data = realloc (_data, sizeof(float) * s);
71 if (_data) { _size = s; }
74 /** clear memory (set to zero) */
76 memset (_data, 0, sizeof(float) * _size);
79 /** access memory as float array
81 * @param off offset in shared memory region
84 float* to_float (size_t off) {
85 if (off >= _size) { return 0; }
86 return &(((float*)_data)[off]);
89 /** access memory as integer array
91 * @param off offset in shared memory region
94 int32_t* to_int (size_t off) {
95 if (off >= _size) { return 0; }
96 return &(((int32_t*)_data)[off]);
99 /** atomically set integer at offset
101 * This involves a memory barrier. This call
102 * is intended for buffers which are
103 * shared with another instance.
105 * @param off offset in shared memory region
106 * @param val value to set
108 void atomic_set_int (size_t off, int32_t val) {
109 g_atomic_int_set (&(((int32_t*)_data)[off]), val);
112 /** atomically read integer at offset
114 * This involves a memory barrier. This call
115 * is intended for buffers which are
116 * shared with another instance.
118 * @param off offset in shared memory region
119 * @returns value at offset
121 int32_t atomic_get_int (size_t off) {
122 return g_atomic_int_get (&(((int32_t*)_data)[off]));
130 /** lua wrapper to memset() */
131 void memset (float *data, const float val, const uint32_t n_samples);
133 * multiply every sample of `data' with the corresponding sample at `mult'.
135 * @param data multiplicand
136 * @param mult multiplicand
137 * @param n_samples number of samples in data and mmult
139 void mmult (float *data, float *mult, const uint32_t n_samples);
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
147 void peaks (float *data, float &min, float &max, uint32_t n_samples);
149 /** non-linear power-scale meter deflection
151 * @param power signal power (dB)
152 * @returns deflected value
154 float log_meter (float power);
155 /** non-linear power-scale meter deflection
157 * @param coeff signal value
158 * @returns deflected value
160 float log_meter_coeff (float coeff);
162 /** 1st order Low Pass filter */
163 class LIBARDOUR_API LowPass {
165 /** instantiate a LPF
167 * @param samplerate samplerate
168 * @param freq cut-off frequency
170 LowPass (double samplerate, float freq);
171 /** process audio data
173 * @param data pointer to audio-data
174 * @param n_samples number of samples to process
176 void proc (float *data, const uint32_t n_samples);
177 /** filter control data
179 * This is useful for parameter smoothing.
181 * @param data pointer to control-data array
182 * @param val target value
183 * @param array length
185 void ctrl (float *data, const float val, const uint32_t n_samples);
186 /** update filter cut-off frequency
188 * @param freq cut-off frequency
190 void set_cutoff (float freq);
191 /** reset filter state */
192 void reset () { _z = 0.f; }
200 class LIBARDOUR_API Biquad {
214 /** Instantiate Biquad Filter
216 * @param samplerate Samplerate
218 Biquad (double samplerate);
219 Biquad (const Biquad &other);
221 /** process audio data
223 * @param data pointer to audio-data
224 * @param n_samples number of samples to process
226 void run (float *data, const uint32_t n_samples);
227 /** setup filter, compute coefficients
229 * @param t filter type (LowPass, HighPass, etc)
230 * @param freq filter frequency
231 * @param Q filter quality
232 * @param gain filter gain
234 void compute (Type t, double freq, double Q, double gain);
236 /** setup filter, set coefficients directly */
237 void configure (double a1, double a2, double b0, double b1, double b2);
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)
243 float dB_at_freq (float freq) const;
245 /** reset filter state */
246 void reset () { _z1 = _z2 = 0.0; }
251 double _b0, _b1, _b2;
254 class LIBARDOUR_API FFTSpectrum {
256 FFTSpectrum (uint32_t window_size, double rate);
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
262 * @param data raw audio data
263 * @param n_samples number of samples to write to analysis buffer
264 * @param offset destination offset
266 void set_data_hann (float const * const data, const uint32_t n_samples, const uint32_t offset = 0);
268 /** process current data in buffer */
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)
276 float power_at_bin (const uint32_t bin, const float norm = 1.f) const;
278 float freq_at_bin (const uint32_t bin) const {
279 return bin * _fft_freq_per_bin;
283 static Glib::Threads::Mutex fft_planner_lock;
286 void init (uint32_t window_size, double rate);
289 uint32_t _fft_window_size;
290 uint32_t _fft_data_size;
291 double _fft_freq_per_bin;
294 float* _fft_data_out;