From 47bbfdbf1e2a6193157397938e76b16a1f60e789 Mon Sep 17 00:00:00 2001 From: Vasile Vilvoiu Date: Fri, 16 Jul 2021 18:32:27 +0300 Subject: Add support for arbitrary scales, with custom units. Add support for linear scales. Logging of scale to stderr. Closes #9. --- src/value-map.cpp | 59 +++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 51 insertions(+), 8 deletions(-) (limited to 'src/value-map.cpp') diff --git a/src/value-map.cpp b/src/value-map.cpp index c3ad28b..8a547c4 100644 --- a/src/value-map.cpp +++ b/src/value-map.cpp @@ -6,26 +6,69 @@ */ #include "value-map.hpp" -ValueMap::ValueMap(double lower, double upper) : lower_(lower), upper_(upper) +ValueMap::ValueMap(double lower, double upper, const std::string& unit) : lower_(lower), upper_(upper), unit_(unit) { } -dBFSValueMap::dBFSValueMap(double mindb) : ValueMap(mindb, 0) +std::string +ValueMap::GetUnit() const { + return unit_; } -RealWindow -dBFSValueMap::Map(const RealWindow& input) +std::unique_ptr +ValueMap::Build(ValueMapType type, double lower, double upper, std::string unit) +{ + switch (type) { + case ValueMapType::kLinear: + return std::make_unique(lower, upper, unit); + + case ValueMapType::kDecibel: + return std::make_unique(lower, upper, unit); + + default: + throw std::runtime_error("unknown value map type"); + } +} + +LinearValueMap::LinearValueMap(double lower, double upper, const std::string &unit) + : ValueMap(lower, upper, unit) +{ +} + +RealWindow LinearValueMap::Map(const RealWindow &input) +{ + auto n = input.size(); + RealWindow output(n); + + for (unsigned int i = 0; i < n; i ++) { + output[i] = std::clamp(input[i] / n, this->lower_, this->upper_); + output[i] = (output[i] - this->lower_) / (this->upper_ - this->lower_); + } + + return output; +} + +DecibelValueMap::DecibelValueMap(double lower, double upper, const std::string &unit) + : ValueMap(lower, upper, unit) +{ +} + +RealWindow DecibelValueMap::Map(const RealWindow &input) { auto n = input.size(); - RealWindow output; - output.resize(n); + RealWindow output(n); for (unsigned int i = 0; i < n; i ++) { output[i] = 20.0 * std::log10(input[i] / n); - output[i] = std::clamp(output[i], this->lower_, 0.0f); - output[i] = 1.0f - output[i] / this->lower_; + output[i] = std::clamp(output[i], this->lower_, this->upper_); + output[i] = (output[i] - this->lower_) / (this->upper_ - this->lower_); } return output; +} + +std::string DecibelValueMap::GetUnit() const +{ + return "dB" + unit_; } \ No newline at end of file -- cgit v1.2.3