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_
26 #include "ardour/libardour_visibility.h"
28 namespace ARDOUR { namespace DSP {
30 /** C/C++ Shared Memory
32 * A convenience class representing a C array of float[] or int32_t[]
33 * data values. This is useful for lua scripts to perform DSP operations
34 * directly using C/C++ with CPU Hardware acceleration.
36 * Access to this memory area is always 4 byte aligned. The data
37 * is interpreted either as float or as int.
39 * This memory area can also be shared between different instances
40 * or the same lua plugin (DSP, GUI).
42 * Since memory allocation is not realtime safe it should be
43 * allocated during dsp_init() or dsp_configure().
44 * The memory is free()ed automatically when the lua instance is
53 assert (sizeof(float) == sizeof (int32_t));
54 assert (sizeof(float) == sizeof (int));
61 /** [re] allocate memory in host's memory space
63 * @param s size, total number of float or integer elements to store.
65 void allocate (size_t s) {
66 _data = realloc (_data, sizeof(float) * s);
67 if (_data) { _size = s; }
70 /** clear memory (set to zero) */
72 memset (_data, 0, sizeof(float) * _size);
75 /** access memory as float array
77 * @param off offset in shared memory region
80 float* to_float (size_t off) {
81 if (off >= _size) { return 0; }
82 return &(((float*)_data)[off]);
85 /** access memory as integer array
87 * @param off offset in shared memory region
90 int32_t* to_int (size_t off) {
91 if (off >= _size) { return 0; }
92 return &(((int32_t*)_data)[off]);
95 /** atomically set integer at offset
97 * This involves a memory barrier. This call
98 * is intended for buffers which are
99 * shared with another instance.
101 * @param off offset in shared memory region
102 * @param val value to set
104 void atomic_set_int (size_t off, int32_t val) {
105 g_atomic_int_set (&(((int32_t*)_data)[off]), val);
108 /** atomically read integer at offset
110 * This involves a memory barrier. This call
111 * is intended for buffers which are
112 * shared with another instance.
114 * @param off offset in shared memory region
115 * @returns value at offset
117 int32_t atomic_get_int (size_t off) {
118 return g_atomic_int_get (&(((int32_t*)_data)[off]));
126 /** lua wrapper to memset() */
127 void memset (float *data, const float val, const uint32_t n_samples);
129 * multiply every sample of `data' with the corresponding sample at `mult'.
131 * @param data multiplicand
132 * @param mult multiplicand
133 * @param n_samples number of samples in data and mmult
135 void mmult (float *data, float *mult, const uint32_t n_samples);
138 * @param data data to analyze
139 * @param min result, minimum value found in range
140 * @param max result, max value found in range
141 * @param n_samples number of samples to analyze
143 void peaks (float *data, float &min, float &max, uint32_t n_samples);
145 /** non-linear power-scale meter deflection
147 * @param power signal power (dB)
148 * @returns deflected value
150 float log_meter (float power);
151 /** non-linear power-scale meter deflection
153 * @param coeff signal value
154 * @returns deflected value
156 float log_meter_coeff (float coeff);
158 /** 1st order Low Pass filter */
159 class LIBARDOUR_API LowPass {
161 /** instantiate a LPF
163 * @param samplerate samplerate
164 * @param freq cut-off frequency
166 LowPass (double samplerate, float freq);
167 /** process audio data
169 * @param data pointer to audio-data
170 * @param n_samples number of samples to process
172 void proc (float *data, const uint32_t n_samples);
173 /** filter control data
175 * This is useful for parameter smoothing.
177 * @param data pointer to control-data array
178 * @param val target value
179 * @param array length
181 void ctrl (float *data, const float val, const uint32_t n_samples);
182 /** update filter cut-off frequency
184 * @param freq cut-off frequency
186 void set_cutoff (float freq);
187 /** reset filter state */
188 void reset () { _z = 0.f; }
196 class LIBARDOUR_API Biquad {
210 /** Instantiate Biquad Filter
212 * @param samplerate Samplerate
214 Biquad (double samplerate);
215 Biquad (const Biquad &other);
217 /** process audio data
219 * @param data pointer to audio-data
220 * @param n_samples number of samples to process
222 void run (float *data, const uint32_t n_samples);
223 /** setup filter, compute coefficients
225 * @param t filter type (LowPass, HighPass, etc)
226 * @param freq filter frequency
227 * @param Q filter quality
228 * @param gain filter gain
230 void compute (Type t, double freq, double Q, double gain);
232 /** filter transfer function (filter response for spectrum visualization)
233 * @param freq frequency
234 * @return gain at given frequency in dB (clamped to -120..+120)
236 float dB_at_freq (float freq) const;
238 /** reset filter state */
239 void reset () { _z1 = _z2 = 0.0; }
244 double _b0, _b1, _b2;