Optimize plugin-processing for non-automated params
[ardour.git] / libs / ardour / ardour / dsp_filter.h
index a8f000acc660940d3b8a96d38308ac6788339e61..f3100efac55ac168682d4d4e519cfb1ff44cab2e 100644 (file)
 #include <string.h>
 #include <assert.h>
 #include <glib.h>
+#include <glibmm.h>
+#include <fftw3.h>
+
+#include "pbd/malign.h"
+
+#include "ardour/buffer_set.h"
+#include "ardour/chan_mapping.h"
 #include "ardour/libardour_visibility.h"
+#include "ardour/types.h"
 
 namespace ARDOUR { namespace DSP {
 
@@ -46,16 +54,17 @@ namespace ARDOUR { namespace DSP {
         */
        class DspShm {
                public:
-                       DspShm ()
+                       DspShm (size_t s = 0)
                                : _data (0)
                                , _size (0)
                        {
                                assert (sizeof(float) == sizeof (int32_t));
                                assert (sizeof(float) == sizeof (int));
+                               allocate (s);
                        }
 
                        ~DspShm () {
-                               free (_data);
+                               cache_aligned_free (_data);
                        }
 
                        /** [re] allocate memory in host's memory space
@@ -63,7 +72,9 @@ namespace ARDOUR { namespace DSP {
                         * @param s size, total number of float or integer elements to store.
                         */
                        void allocate (size_t s) {
-                               _data = realloc (_data, sizeof(float) * s);
+                               if (s == _size) { return; }
+                               cache_aligned_free (_data);
+                               cache_aligned_malloc ((void**) &_data, sizeof (float) * s);
                                if (_data) { _size = s; }
                        }
 
@@ -140,7 +151,7 @@ namespace ARDOUR { namespace DSP {
         * @param max result, max value found in range
         * @param n_samples number of samples to analyze
         */
-       void peaks (float *data, float &min, float &max, uint32_t n_samples);
+       void peaks (const float *data, float &min, float &max, uint32_t n_samples);
 
        /** non-linear power-scale meter deflection
         *
@@ -155,6 +166,12 @@ namespace ARDOUR { namespace DSP {
         */
        float log_meter_coeff (float coeff);
 
+       void process_map (BufferSet* bufs,
+                         const ChanMapping& in,
+                         const ChanMapping& out,
+                         pframes_t nframes, samplecnt_t offset,
+                         const DataType&);
+
        /** 1st order Low Pass filter */
        class LIBARDOUR_API LowPass {
                public:
@@ -193,7 +210,7 @@ namespace ARDOUR { namespace DSP {
        };
 
        /** Biquad Filter */
-       class LIBARDOUR_API BiQuad {
+       class LIBARDOUR_API Biquad {
                public:
                        enum Type {
                                LowPass,
@@ -211,8 +228,8 @@ namespace ARDOUR { namespace DSP {
                         *
                         * @param samplerate Samplerate
                         */
-                       BiQuad (double samplerate);
-                       BiQuad (const BiQuad &other);
+                       Biquad (double samplerate);
+                       Biquad (const Biquad &other);
 
                        /** process audio data
                         *
@@ -229,6 +246,9 @@ namespace ARDOUR { namespace DSP {
                         */
                        void compute (Type t, double freq, double Q, double gain);
 
+                       /** setup filter, set coefficients directly */
+                       void configure (double a1, double a2, double b0, double b1, double b2);
+
                        /** filter transfer function (filter response for spectrum visualization)
                         * @param freq frequency
                         * @return gain at given frequency in dB (clamped to -120..+120)
@@ -244,5 +264,79 @@ namespace ARDOUR { namespace DSP {
                        double _b0, _b1, _b2;
        };
 
+       class LIBARDOUR_API FFTSpectrum {
+               public:
+                       FFTSpectrum (uint32_t window_size, double rate);
+                       ~FFTSpectrum ();
+
+                       /** set data to be analyzed and pre-process with hanning window
+                        * n_samples + offset must not be larger than the configured window_size
+                        *
+                        * @param data raw audio data
+                        * @param n_samples number of samples to write to analysis buffer
+                        * @param offset destination offset
+                        */
+                       void set_data_hann (float const * const data, const uint32_t n_samples, const uint32_t offset = 0);
+
+                       /** process current data in buffer */
+                       void execute ();
+
+                       /** query
+                        * @param bin the frequency bin 0 .. window_size / 2
+                        * @param norm gain factor (set equal to @bin for 1/f normalization)
+                        * @return signal power at given bin (in dBFS)
+                        */
+                       float power_at_bin (const uint32_t bin, const float norm = 1.f) const;
+
+                       float freq_at_bin (const uint32_t bin) const {
+                               return bin * _fft_freq_per_bin;
+                       }
+
+               private:
+                       static Glib::Threads::Mutex fft_planner_lock;
+                       float* hann_window;
+
+                       void init (uint32_t window_size, double rate);
+                       void reset ();
+
+                       uint32_t _fft_window_size;
+                       uint32_t _fft_data_size;
+                       double   _fft_freq_per_bin;
+
+                       float* _fft_data_in;
+                       float* _fft_data_out;
+                       float* _fft_power;
+
+                       fftwf_plan _fftplan;
+       };
+
+       class LIBARDOUR_API Generator {
+               public:
+                       Generator ();
+
+                       enum Type {
+                               UniformWhiteNoise,
+                               GaussianWhiteNoise,
+                               PinkNoise,
+                       };
+
+                       void run (float *data, const uint32_t n_samples);
+                       void set_type (Type t);
+
+               private:
+                       uint32_t randi ();
+                       float    randf () { return (randi () / 1073741824.f) - 1.f; }
+                       float    grandf ();
+
+                       Type     _type;
+                       uint32_t _rseed;
+                       /* pink-noise */
+                       float _b0, _b1, _b2, _b3, _b4, _b5, _b6;
+                       /* gaussian white */
+                       bool _pass;
+                       float _rn;
+
+       };
+
 } } /* namespace */
 #endif