rename BufferSet::get* to BufferSet::get_available*
[ardour.git] / libs / ardour / dsp_filter.cc
index 8a10d304b13e80dd2cebca5bd2be1ccf80d6df2e..1462fba4312aa4701b217a96b96f69d35004e6a6 100644 (file)
@@ -1,20 +1,20 @@
 /*
- * Copyright (C) 2016 Robin Gareus <robin@gareus.org>
+ * Copyright (C) 2016-2017 Paul Davis <paul@linuxaudiosystems.com>
+ * Copyright (C) 2016-2018 Robin Gareus <robin@gareus.org>
  *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or (at your option) any later version.
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
- *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
 #include <algorithm>
@@ -75,10 +75,9 @@ ARDOUR::DSP::peaks (const float *data, float &min, float &max, uint32_t n_sample
 }
 
 void
-ARDOUR::DSP::process_map (BufferSet* bufs, const ChanMapping& in, const ChanMapping& out, pframes_t nframes, framecnt_t offset, const DataType& dt)
+ARDOUR::DSP::process_map (BufferSet* bufs, const ChanMapping& in, const ChanMapping& out, pframes_t nframes, samplecnt_t offset, const DataType& dt)
 {
        const ChanMapping::Mappings& im (in.mappings());
-       const ChanMapping::Mappings& om (out.mappings());
 
        for (ChanMapping::Mappings::const_iterator tm = im.begin(); tm != im.end(); ++tm) {
                if (tm->first != dt) { continue; }
@@ -86,7 +85,7 @@ ARDOUR::DSP::process_map (BufferSet* bufs, const ChanMapping& in, const ChanMapp
                        bool valid;
                        const uint32_t idx = out.get (dt, i->second, &valid);
                        if (valid && idx != i->first) {
-                               bufs->get (dt, idx).read_from (bufs->get (dt, i->first), nframes, offset, offset);
+                               bufs->get_available (dt, idx).read_from (bufs->get_available (dt, i->first), nframes, offset, offset);
                        }
                }
        }
@@ -96,7 +95,7 @@ ARDOUR::DSP::process_map (BufferSet* bufs, const ChanMapping& in, const ChanMapp
                        bool valid;
                        in.get_src (dt, i->first, &valid);
                        if (!valid) {
-                               bufs->get (dt, i->second).silence (nframes, offset);
+                               bufs->get_available (dt, i->second).silence (nframes, offset);
                        }
                }
        }
@@ -354,6 +353,7 @@ FFTSpectrum::~FFTSpectrum ()
 void
 FFTSpectrum::init (uint32_t window_size, double rate)
 {
+       assert (window_size > 0);
        Glib::Threads::Mutex::Lock lk (fft_planner_lock);
 
        _fft_window_size = window_size;
@@ -424,3 +424,85 @@ FFTSpectrum::power_at_bin (const uint32_t b, const float norm) const {
        const float a = _fft_power[b] * norm;
        return a > 1e-12 ? 10.0 * fast_log10 (a) : -INFINITY;
 }
+
+Generator::Generator ()
+       : _type (UniformWhiteNoise)
+       , _rseed (1)
+{
+       set_type (UniformWhiteNoise);
+}
+
+void
+Generator::set_type (Generator::Type t) {
+       _type = t;
+       _b0 = _b1 = _b2 = _b3 = _b4 = _b5 = _b6 = 0;
+       _pass = false;
+       _rn = 0;
+}
+
+void
+Generator::run (float *data, const uint32_t n_samples)
+{
+       switch (_type) {
+               default:
+               case UniformWhiteNoise:
+                       for (uint32_t i = 0; i < n_samples; ++i) {
+                               data[i] = randf();
+                       }
+                       break;
+               case GaussianWhiteNoise:
+                       for (uint32_t i = 0 ; i < n_samples; ++i) {
+                               data[i] = 0.7079f * grandf();
+                       }
+                       break;
+               case PinkNoise:
+                       for (uint32_t i = 0 ; i < n_samples; ++i) {
+                               const float white = .39572f * randf ();
+                               _b0 = .99886f * _b0 + white * .0555179f;
+                               _b1 = .99332f * _b1 + white * .0750759f;
+                               _b2 = .96900f * _b2 + white * .1538520f;
+                               _b3 = .86650f * _b3 + white * .3104856f;
+                               _b4 = .55000f * _b4 + white * .5329522f;
+                               _b5 = -.7616f * _b5 - white * .0168980f;
+                               data[i] = _b0 + _b1 + _b2 + _b3 + _b4 + _b5 + _b6 + white * 0.5362f;
+                               _b6 = white * 0.115926f;
+                       }
+                       break;
+       }
+}
+
+inline uint32_t
+Generator::randi ()
+{
+       // 31bit Park-Miller-Carta Pseudo-Random Number Generator
+       uint32_t hi, lo;
+       lo = 16807 * (_rseed & 0xffff);
+       hi = 16807 * (_rseed >> 16);
+       lo += (hi & 0x7fff) << 16;
+       lo += hi >> 15;
+       lo = (lo & 0x7fffffff) + (lo >> 31);
+       return (_rseed = lo);
+}
+
+inline float
+Generator::grandf ()
+{
+       float x1, x2, r;
+
+       if (_pass) {
+               _pass = false;
+               return _rn;
+       }
+
+       do {
+               x1 = randf ();
+               x2 = randf ();
+               r = x1 * x1 + x2 * x2;
+       } while ((r >= 1.0f) || (r < 1e-22f));
+
+       r = sqrtf (-2.f * logf (r) / r);
+
+       _pass = true;
+       _rn = r * x2;
+       return r * x1;
+}