|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <boost/accumulators/accumulators.hpp> |
| 4 | +#include <boost/accumulators/statistics.hpp> |
| 5 | +#include <boost/accumulators/statistics/count.hpp> |
| 6 | +#include <boost/accumulators/statistics/kurtosis.hpp> |
| 7 | +#include <boost/accumulators/statistics/mean.hpp> |
| 8 | +#include <boost/accumulators/statistics/median.hpp> |
| 9 | +#include <boost/accumulators/statistics/sum.hpp> |
| 10 | +#include <boost/accumulators/statistics/variance.hpp> |
| 11 | +#include <halp/controls.hpp> |
| 12 | +#include <halp/mappers.hpp> |
| 13 | +#include <halp/meta.hpp> |
| 14 | +#include <ossia/detail/math.hpp> |
| 15 | + |
| 16 | +namespace ao |
| 17 | +{ |
| 18 | +namespace ba = boost::accumulators; |
| 19 | +namespace bt = ba::tag; |
| 20 | +/** |
| 21 | + * @brief Calibrate a value and output it between 0-1 according to |
| 22 | + * the range of inputs |
| 23 | + */ |
| 24 | +struct Accumulator |
| 25 | +{ |
| 26 | +public: |
| 27 | + halp_meta(name, "Accumulator") |
| 28 | + halp_meta(c_name, "accumulator") |
| 29 | + halp_meta(category, "Control/Mappings") |
| 30 | + halp_meta(author, "Jean-Michaël Celerier") |
| 31 | + halp_meta(description, "Accumulate statistics about incoming values") |
| 32 | + halp_meta(manual_url, "https://ossia.io/score-docs/processes/accumulator.html") |
| 33 | + halp_meta(uuid, "5c5b37b5-da06-432a-bc51-81657b6d59e1") |
| 34 | + |
| 35 | + struct inputs_t |
| 36 | + { |
| 37 | + halp::val_port<"In", std::optional<float>> in; |
| 38 | + struct : halp::impulse_button<"Reset"> |
| 39 | + { |
| 40 | + void update(Accumulator& self) |
| 41 | + { |
| 42 | + std::destroy_at(&self.minmax); |
| 43 | + std::construct_at(&self.minmax); |
| 44 | + } |
| 45 | + } reset; |
| 46 | + } inputs; |
| 47 | + |
| 48 | + struct |
| 49 | + { |
| 50 | + struct : halp::val_port<"Count", float> |
| 51 | + { |
| 52 | + struct range |
| 53 | + { |
| 54 | + const float min = 0.f; |
| 55 | + const float max = 1.f; |
| 56 | + const float init = 0.f; |
| 57 | + }; |
| 58 | + } count; |
| 59 | + struct : halp::val_port<"Sum", float> |
| 60 | + { |
| 61 | + struct range |
| 62 | + { |
| 63 | + const float min = 0.f; |
| 64 | + const float max = 1.f; |
| 65 | + const float init = 0.f; |
| 66 | + }; |
| 67 | + } sum; |
| 68 | + struct : halp::val_port<"Consecutive difference", float> |
| 69 | + { |
| 70 | + struct range |
| 71 | + { |
| 72 | + const float min = 0.f; |
| 73 | + const float max = 1.f; |
| 74 | + const float init = 0.f; |
| 75 | + }; |
| 76 | + } diff; |
| 77 | + struct : halp::val_port<"Mean", float> |
| 78 | + { |
| 79 | + struct range |
| 80 | + { |
| 81 | + const float min = 1.f; |
| 82 | + const float max = 0.f; |
| 83 | + const float init = 0.f; |
| 84 | + }; |
| 85 | + } mean; |
| 86 | + struct : halp::val_port<"Variance", float> |
| 87 | + { |
| 88 | + struct range |
| 89 | + { |
| 90 | + const float min = 0.f; |
| 91 | + const float max = 1.f; |
| 92 | + const float init = 0.f; |
| 93 | + }; |
| 94 | + } variance; |
| 95 | + struct : halp::val_port<"Median", float> |
| 96 | + { |
| 97 | + struct range |
| 98 | + { |
| 99 | + const float min = 1.f; |
| 100 | + const float max = 0.f; |
| 101 | + const float init = 0.f; |
| 102 | + }; |
| 103 | + } median; |
| 104 | + struct : halp::val_port<"Kurtosis", float> |
| 105 | + { |
| 106 | + struct range |
| 107 | + { |
| 108 | + const float min = 1.f; |
| 109 | + const float max = 0.f; |
| 110 | + const float init = 0.f; |
| 111 | + }; |
| 112 | + } kurtosis; |
| 113 | + |
| 114 | + struct : halp::val_port<"Min", float> |
| 115 | + { |
| 116 | + struct range |
| 117 | + { |
| 118 | + const float min = 0.f; |
| 119 | + const float max = 1.f; |
| 120 | + const float init = 0.f; |
| 121 | + }; |
| 122 | + } min; |
| 123 | + struct : halp::val_port<"Max", float> |
| 124 | + { |
| 125 | + struct range |
| 126 | + { |
| 127 | + const float min = 0.f; |
| 128 | + const float max = 1.f; |
| 129 | + const float init = 0.f; |
| 130 | + }; |
| 131 | + } max; |
| 132 | + |
| 133 | + } outputs; |
| 134 | + |
| 135 | + using accum = ba::accumulator_set< |
| 136 | + float, ba::stats< |
| 137 | + ba::tag::count, ba::tag::sum, ba::tag::min, ba::tag::max, ba::tag::mean, |
| 138 | + ba::tag::variance, ba::tag::median, ba::tag::kurtosis>>; |
| 139 | + |
| 140 | + accum minmax{}; |
| 141 | + float consecutive_difference{}; |
| 142 | + bool consecutive_difference_sign{}; |
| 143 | + |
| 144 | + void operator()() noexcept |
| 145 | + { |
| 146 | + using namespace ba; |
| 147 | + if(inputs.in.value) |
| 148 | + { |
| 149 | + float v = *inputs.in.value; |
| 150 | + this->minmax(v); |
| 151 | + if(consecutive_difference_sign ^= true) |
| 152 | + consecutive_difference += v; |
| 153 | + else |
| 154 | + consecutive_difference -= v; |
| 155 | + |
| 156 | + outputs.count.value = ba::extract::count(minmax); |
| 157 | + outputs.sum.value = ba::extract::sum(minmax); |
| 158 | + outputs.diff.value = consecutive_difference; |
| 159 | + outputs.min.value = ba::extract::min(minmax); |
| 160 | + outputs.max.value = ba::extract::max(minmax); |
| 161 | + outputs.mean.value = ba::extract::mean(minmax); |
| 162 | + outputs.variance.value = ba::extract::variance(minmax); |
| 163 | + outputs.median.value = ba::extract::median(minmax); |
| 164 | + outputs.kurtosis.value = ba::extract::kurtosis(minmax); |
| 165 | + } |
| 166 | + } |
| 167 | +}; |
| 168 | + |
| 169 | +} |
0 commit comments