octavia

octobanana's customizable text-based audio visualization interactive application.


octavia

/

src

/

app

/

filter.cc

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
/*
                                    88888888
                                  888888888888
                                 88888888888888
                                8888888888888888
                               888888888888888888
                              888888  8888  888888
                              88888    88    88888
                              888888  8888  888888
                              88888888888888888888
                              88888888888888888888
                             8888888888888888888888
                          8888888888888888888888888888
                        88888888888888888888888888888888
                              88888888888888888888
                            888888888888888888888888
                           888888  8888888888  888888
                           888     8888  8888     888
                                   888    888

                                   OCTOBANANA

Licensed under the MIT License

Copyright (c) 2020 Brett Robinson <https://octobanana.com/>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#include "app/filter.hh"

namespace Filter
{

template<typename T>
class Ring_Buffer {
  using reference = typename std::vector<T>::reference;
  using const_reference = typename std::vector<T>::const_reference;
  using size_type = typename std::vector<T>::size_type;

public:

  Ring_Buffer(Ring_Buffer&&) = default;

  Ring_Buffer(Ring_Buffer const&) = default;

  explicit Ring_Buffer(size_type const size, T const& value = T()) : _buf(size, value) {
  }

  template<typename It>
  Ring_Buffer(It begin, It end) : _buf(begin, end) {
  }

  Ring_Buffer(std::vector<T> const& buf) : _buf(buf) {
  }

  Ring_Buffer(std::vector<T>&& buf) : _buf(buf) {
  }

  Ring_Buffer(std::initializer_list<T> init) : _buf(init) {
  }

  ~Ring_Buffer() = default;

  Ring_Buffer& operator=(Ring_Buffer&&) = default;

  Ring_Buffer& operator=(Ring_Buffer const&) = default;

  reference operator[](size_type const pos) {
    return _buf[get_pos(pos)];
  }

  const_reference operator[](size_type const pos) const {
    return _buf[get_pos(pos)];
  }

  size_type size() const noexcept {
    return _buf.size();
  }

  reference push(T const& arg) {
    reference ref = _buf[_index];
    ref = arg;
    increase_index();
    return ref;
  }

private:
  size_type get_pos(size_type const pos) const noexcept {
    return (_index + pos) % _buf.size();
  }

  void increase_index() {
    if (++_index >= _buf.size()) {_index = 0;}
  }

  std::size_t _index {0};
  std::vector<T> _buf;
};

void savitzky_golay(std::vector<double>& bars, std::size_t size, std::size_t width, double const threshold) {
  if (size == 0 || width == 0) {return;}
  if (width >= size) {width = size - 1;}
  auto const part = static_cast<std::size_t>(std::trunc(width / 2.0));
  if (part == 0) {return;}

  Ring_Buffer<double> win {bars.begin(), bars.begin() + static_cast<long int>(width)};
  auto const c = 1.0 / (part * 2 + 1);
  auto const end = size - part;

  for (std::size_t i = part; i < end; ++i) {
    double res {0};

    for (std::size_t j = 0; j < width; ++j) {
      res += (win[j] * c) + j - part;
    }

    win.push(bars[i + part + 1]);

    if (threshold == 0.0 || std::abs(bars[i] - res) < threshold) {
      bars[i] = res;
    }
  }
}

template<typename T>
static constexpr T lerp(T const a, T const b, T const t) {
  return a + (t * (b - a));
}

std::vector<double> resample(std::vector<double> out, std::size_t interpolate, std::size_t decimate) {
  if (interpolate == decimate || (interpolate <= 1 && decimate <= 1)) {return out;}

  // Filter::Low_Pass filter_interpolate;
  // Filter::Low_Pass filter_decimate;
  // filter_interpolate.init(interpolate, interpolate / 2, 1.0);
  // filter_decimate.init(decimate, decimate / 2 , 1.0);

  auto const d = std::gcd(interpolate, decimate);
  interpolate /= d;
  decimate /= d;

  if (interpolate > 1) {
    std::vector<double> vec;
    auto const n = interpolate;
    for (std::size_t i = 0; i + 1 < out.size(); ++i) {
      auto const val = out[i];
      auto const next = out[i + 1];
      auto const f = (val - next) / n;
      for (std::size_t j = 0; j < n; ++j) {
        vec.emplace_back(val + (f * j));
      }
    }
    auto const last = vec.back();
    for (std::size_t j = 0; j < n; ++j) {
      vec.emplace_back(last);
    }
    out = vec;
  }

  // for (auto& e : out) {
  //   e = filter_decimate.process(filter_interpolate.process(e));
  // }

  if (decimate > 1) {
    std::vector<double> vec;
    auto const n = decimate;
    for (std::size_t i = 0; i + n - 1 < out.size(); i += n) {
      // double val = 0;
      // for (std::size_t j = 0; j < n; ++j) {
      //   val += out[i + j];
      // }
      // vec.emplace_back(val / n);

      // auto const val = out[i];
      // vec.emplace_back(val);

      // auto const val = out[i];
      // auto min = val;
      // auto max = val;
      // for (std::size_t j = 1; j < n; ++j) {
      //   min = std::min(min, out[i + j]);
      //   max = std::max(max, out[i + j]);
      // }
      // vec.emplace_back(lerp(min, max, 0.5));

      auto const val = out[i];
      auto max = val;
      for (std::size_t j = 1; j < n; ++j) {
        max = std::max(max, out[i + j]);
      }
      vec.emplace_back(max);
    }
    out = vec;
  }

  return out;
}

double center_frequency(double const low, double const high) {
  if (high / low < 1.1) {
    return (low + high) / 2.0;
  }
  return std::sqrt(low * high);
}

double q_factor_band_pass(double const low, double const high) {
  return center_frequency(low, high) / (high - low);
}

double q_factor_notch(double const low, double const high) {
  return (high - low) / center_frequency(low, high);
}

} // namespace Filter
Back to Top