octavia

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


octavia

/

src

/

app

/

filter.hh

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 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
/*
                                    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.
*/

#ifndef APP_FILTER_HH
#define APP_FILTER_HH

#include <cmath>
#include <cassert>
#include <cstddef>

#include <vector>
#include <numeric>

namespace Filter
{

double constexpr Pi {3.1415926535897932384626433832795028841971};

void savitzky_golay(std::vector<double>& bars, std::size_t size, std::size_t width = 3, double const threshold = 0.0);

std::vector<double> resample(std::vector<double> out, std::size_t interpolate, std::size_t decimate);

double center_frequency(double const low, double const high);
double q_factor_band_pass(double const low, double const high);
double q_factor_notch(double const low, double const high);

class Biquad {
public:
  virtual ~Biquad() = default;

  virtual void init(double const sample_rate, double const freq, double const q, double const db_gain) = 0;

  double process(double const x) {
    double const y = _b0 * x + _b1 * _x1 - _a1 * _y1 + _b2 * _x2 - _a2 * _y2;
    _x2 = _x1;
    _x1 = x;
    _y2 = _y1;
    _y1 = y;
    return y;
  }

  void clear() {
    _x1 = 0;
    _y1 = 0;
    _x2 = 0;
    _y2 = 0;
  }

protected:
  void normalize() {
    _a1 /= _a0;
    _a2 /= _a0;
    _b0 /= _a0;
    _b1 /= _a0;
    _b2 /= _a0;
  }

  double _a0 {0};
  double _a1 {0};
  double _a2 {0};
  double _b0 {0};
  double _b1 {0};
  double _b2 {0};

private:
  double _x1 {0};
  double _y1 {0};
  double _x2 {0};
  double _y2 {0};
};

class All_Pass : public Biquad {
public:
  void init(double const sample_rate, double const freq, double const q, double const db_gain = 0) {
    double const w = 2.0 * Pi * (freq / sample_rate);
    double const wcos = std::cos(w);
    double const wsin = std::sin(w);
    double const alpha = wsin / (2.0 * q);
    double const wcos1 = 1.0 - wcos;

    _a0 = 1.0 + alpha;
    _a1 = -2.0 * wcos;
    _a2 = 1.0 - alpha;
    _b0 = 1.0 - alpha;
    _b1 = -2.0 * wcos;
    _b2 = 1.0 + alpha;

    clear();
    normalize();
  }
};

class Low_Pass : public Biquad {
public:
  void init(double const sample_rate, double const freq, double const q, double const db_gain = 0) {
    double const w = 2.0 * Pi * (freq / sample_rate);
    double const wcos = std::cos(w);
    double const wsin = std::sin(w);
    double const alpha = wsin / (2.0 * q);
    double const wcos1 = 1.0 - wcos;

    _a0 = 1.0 + alpha;
    _a1 = -2.0 * wcos;
    _a2 = 1.0 - alpha;
    _b0 = wcos1 / 2.0;
    _b1 = wcos1;
    _b2 = wcos1 / 2.0;

    clear();
    normalize();
  }
};

class High_Pass : public Biquad {
public:
  void init(double const sample_rate, double const freq, double const q, double const db_gain = 0) {
    double const w = 2.0 * Pi * (freq / sample_rate);
    double const wcos = std::cos(w);
    double const wsin = std::sin(w);
    double const alpha = wsin / (2.0 * q);
    double const wcos1 = 1.0 + wcos;

    _a0 = 1.0 + alpha;
    _a1 = -2.0 * wcos;
    _a2 = 1.0 - alpha;
    _b0 = wcos1 / 2.0;
    _b1 = -wcos1;
    _b2 = wcos1 / 2.0;

    clear();
    normalize();
  }
};

class Band_Pass_1 : public Biquad {
public:
  void init(double const sample_rate, double const freq, double const bw, double const db_gain = 0) {
    double const w = 2.0 * Pi * (freq / sample_rate);
    double const wcos = std::cos(w);
    double const wsin = std::sin(w);
    double const alpha = wsin * std::sinh(std::log(2.0) / 2.0 * bw * w / wsin);

    _a0 = 1.0 + alpha;
    _a1 = -2.0 * wcos;
    _a2 = 1.0 - alpha;
    _b0 = bw * alpha;
    _b1 = 0;
    _b2 = -bw * alpha;

    clear();
    normalize();
  }
};

class Band_Pass_2 : public Biquad {
public:
  void init(double const sample_rate, double const freq, double const bw, double const db_gain = 0) {
    double const w = 2.0 * Pi * (freq / sample_rate);
    double const wcos = std::cos(w);
    double const wsin = std::sin(w);
    double const alpha = wsin * std::sinh(std::log(2.0) / 2.0 * bw * w / wsin);

    _a0 = 1.0 + alpha;
    _a1 = -2.0 * wcos;
    _a2 = 1.0 - alpha;
    _b0 = alpha;
    _b1 = 0;
    _b2 = -alpha;

    clear();
    normalize();
  }
};

class notch : public Biquad {
public:
  void init(double const sample_rate, double const freq, double const bw, double const db_gain = 0) {
    double const w = 2.0 * Pi * (freq / sample_rate);
    double const wcos = std::cos(w);
    double const wsin = std::sin(w);
    double const alpha = wsin * std::sinh(std::log(2.0) / 2.0 * bw * w / wsin);

    _a0 = 1.0 + alpha;
    _a1 = -2.0 * wcos;
    _a2 = 1.0 - alpha;
    _b0 = 1;
    _b1 = -2.0 * wcos;
    _b2 = 1;

    clear();
    normalize();
  }
};

class peak : public Biquad {
public:
  void init(double const sample_rate, double const freq, double const bw, double const db_gain = 0) {
    double const a = std::pow(10.0, db_gain / 40.0);
    double const w = 2.0 * Pi * (freq / sample_rate);
    double const wcos = std::cos(w);
    double const wsin = std::sin(w);
    double const alpha = wsin * std::sinh(std::log(2.0) / 2.0 * bw * w / wsin);

    _a0 = 1.0 + alpha / a;
    _a1 = -2.0 * wcos;
    _a2 = 1.0 - alpha / a;
    _b0 = 1.0 + alpha * a;
    _b1 = -2.0 * wcos;
    _b2 = 1.0 - alpha * a;

    clear();
    normalize();
  }
};

class Low_Shelf : public Biquad {
public:
  void init(double const sample_rate, double const freq, double const s, double const db_gain) {
    double const a = std::pow(10.0, db_gain / 40.0);
    double const w = 2.0 * Pi * (freq / sample_rate);
    double const wcos = std::cos(w);
    double const wsin = std::sin(w);
    double const alpha = wsin / 2.0 * std::sqrt((a + 1.0 / a) * (1.0 / s - 1.0) + 2.0);
    double const alpha2 = 2.0 * std::sqrt(a) * alpha;

    _a0 = (a + 1.0) + (a - 1.0) * wcos + alpha2;
    _a1 = -2.0 * ((a - 1.0) + (a + 1.0) * wcos);
    _a2 = (a + 1.0) + (a - 1.0) * wcos - alpha2;
    _b0 = a * ((a + 1.0) - (a - 1.0) * wcos + alpha2);
    _b1 = 2.0 * a * ((a - 1.0) - (a + 1.0) * wcos);
    _b2 = a * ((a + 1.0) - (a - 1.0) * wcos - alpha2);

    clear();
    normalize();
  }
};

class High_Shelf : public Biquad {
public:
  void init(double const sample_rate, double const freq, double const s, double const db_gain) {
    double const a = std::pow(10.0, db_gain / 40.0);
    double const w = 2.0 * Pi * (freq / sample_rate);
    double const wcos = std::cos(w);
    double const wsin = std::sin(w);
    double const alpha = wsin / 2.0 * std::sqrt((a + 1.0 / a) * (1.0 / s - 1.0) + 2.0);
    double const alpha2 = 2.0 * std::sqrt(a) * alpha;

    _a0 = (a + 1.0) - (a - 1.0) * wcos + alpha2;
    _a1 = 2.0 * ((a - 1.0) - (a + 1.0) * wcos);
    _a2 = (a + 1.0) - (a - 1.0) * wcos - alpha2;
    _b0 = a * ((a + 1.0) + (a - 1.0) * wcos + alpha2);
    _b1 = -2.0 * a * ((a - 1.0) + (a + 1.0) * wcos);
    _b2 = a * ((a + 1.0) + (a - 1.0) * wcos - alpha2);

    clear();
    normalize();
  }
};

} // namespace Filter

#endif // APP_FILTER_HH
Back to Top