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_
29 #include "ardour/buffer_set.h"
30 #include "ardour/chan_mapping.h"
31 #include "ardour/libardour_visibility.h"
32 #include "ardour/types.h"
34 namespace ARDOUR { namespace DSP {
36 /** C/C++ Shared Memory
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.
42 * Access to this memory area is always 4 byte aligned. The data
43 * is interpreted either as float or as int.
45 * This memory area can also be shared between different instances
46 * or the same lua plugin (DSP, GUI).
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
59 assert (sizeof(float) == sizeof (int32_t));
60 assert (sizeof(float) == sizeof (int));
68 /** [re] allocate memory in host's memory space
70 * @param s size, total number of float or integer elements to store.
72 void allocate (size_t s) {
73 if (s == _size) { return; }
74 _data = realloc (_data, sizeof(float) * s);
75 if (_data) { _size = s; }
78 /** clear memory (set to zero) */
80 memset (_data, 0, sizeof(float) * _size);
83 /** access memory as float array
85 * @param off offset in shared memory region
88 float* to_float (size_t off) {
89 if (off >= _size) { return 0; }
90 return &(((float*)_data)[off]);
93 /** access memory as integer array
95 * @param off offset in shared memory region
98 int32_t* to_int (size_t off) {
99 if (off >= _size) { return 0; }
100 return &(((int32_t*)_data)[off]);
103 /** atomically set integer at offset
105 * This involves a memory barrier. This call
106 * is intended for buffers which are
107 * shared with another instance.
109 * @param off offset in shared memory region
110 * @param val value to set
112 void atomic_set_int (size_t off, int32_t val) {
113 g_atomic_int_set (&(((int32_t*)_data)[off]), val);
116 /** atomically read integer at offset
118 * This involves a memory barrier. This call
119 * is intended for buffers which are
120 * shared with another instance.
122 * @param off offset in shared memory region
123 * @returns value at offset
125 int32_t atomic_get_int (size_t off) {
126 return g_atomic_int_get (&(((int32_t*)_data)[off]));
134 /** lua wrapper to memset() */
135 void memset (float *data, const float val, const uint32_t n_samples);
137 * multiply every sample of `data' with the corresponding sample at `mult'.
139 * @param data multiplicand
140 * @param mult multiplicand
141 * @param n_samples number of samples in data and mmult
143 void mmult (float *data, float *mult, const uint32_t n_samples);
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
151 void peaks (float *data, float &min, float &max, uint32_t n_samples);
153 /** non-linear power-scale meter deflection
155 * @param power signal power (dB)
156 * @returns deflected value
158 float log_meter (float power);
159 /** non-linear power-scale meter deflection
161 * @param coeff signal value
162 * @returns deflected value
164 float log_meter_coeff (float coeff);
166 void process_map (BufferSet* bufs,
167 const ChanMapping& in,
168 const ChanMapping& out,
169 pframes_t nframes, framecnt_t offset,
172 /** 1st order Low Pass filter */
173 class LIBARDOUR_API LowPass {
175 /** instantiate a LPF
177 * @param samplerate samplerate
178 * @param freq cut-off frequency
180 LowPass (double samplerate, float freq);
181 /** process audio data
183 * @param data pointer to audio-data
184 * @param n_samples number of samples to process
186 void proc (float *data, const uint32_t n_samples);
187 /** filter control data
189 * This is useful for parameter smoothing.
191 * @param data pointer to control-data array
192 * @param val target value
193 * @param array length
195 void ctrl (float *data, const float val, const uint32_t n_samples);
196 /** update filter cut-off frequency
198 * @param freq cut-off frequency
200 void set_cutoff (float freq);
201 /** reset filter state */
202 void reset () { _z = 0.f; }
210 class LIBARDOUR_API Biquad {
224 /** Instantiate Biquad Filter
226 * @param samplerate Samplerate
228 Biquad (double samplerate);
229 Biquad (const Biquad &other);
231 /** process audio data
233 * @param data pointer to audio-data
234 * @param n_samples number of samples to process
236 void run (float *data, const uint32_t n_samples);
237 /** setup filter, compute coefficients
239 * @param t filter type (LowPass, HighPass, etc)
240 * @param freq filter frequency
241 * @param Q filter quality
242 * @param gain filter gain
244 void compute (Type t, double freq, double Q, double gain);
246 /** setup filter, set coefficients directly */
247 void configure (double a1, double a2, double b0, double b1, double b2);
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)
253 float dB_at_freq (float freq) const;
255 /** reset filter state */
256 void reset () { _z1 = _z2 = 0.0; }
261 double _b0, _b1, _b2;
264 class LIBARDOUR_API FFTSpectrum {
266 FFTSpectrum (uint32_t window_size, double rate);
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
272 * @param data raw audio data
273 * @param n_samples number of samples to write to analysis buffer
274 * @param offset destination offset
276 void set_data_hann (float const * const data, const uint32_t n_samples, const uint32_t offset = 0);
278 /** process current data in buffer */
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)
286 float power_at_bin (const uint32_t bin, const float norm = 1.f) const;
288 float freq_at_bin (const uint32_t bin) const {
289 return bin * _fft_freq_per_bin;
293 static Glib::Threads::Mutex fft_planner_lock;
296 void init (uint32_t window_size, double rate);
299 uint32_t _fft_window_size;
300 uint32_t _fft_data_size;
301 double _fft_freq_per_bin;
304 float* _fft_data_out;