From 344e318b19dc4b1e688ac95fe9db4a3b9c071cd8 Mon Sep 17 00:00:00 2001 From: Jean Pierre Cimalando Date: Thu, 12 Mar 2020 14:03:33 +0100 Subject: [PATCH] Add some functions to lookup filter type. Add the bpk_2p alias. --- src/sfizz/Region.cpp | 47 ++++++++++++----------------------------- src/sfizz/SfzFilter.cpp | 47 +++++++++++++++++++++++++++++++++++++++++ src/sfizz/SfzFilter.h | 12 +++++++++++ 3 files changed, 73 insertions(+), 33 deletions(-) diff --git a/src/sfizz/Region.cpp b/src/sfizz/Region.cpp index 35daf0a6..8dc962b4 100644 --- a/src/sfizz/Region.cpp +++ b/src/sfizz/Region.cpp @@ -498,32 +498,13 @@ bool sfz::Region::parseOpcode(const Opcode& opcode) if (!extendIfNecessary(filters, filterIndex + 1, Default::numFilters)) return false; - switch (hash(opcode.value)) { - case hash("lpf_1p"): filters[filterIndex].type = FilterType::kFilterLpf1p; break; - case hash("hpf_1p"): filters[filterIndex].type = FilterType::kFilterHpf1p; break; - case hash("lpf_2p"): filters[filterIndex].type = FilterType::kFilterLpf2p; break; - case hash("hpf_2p"): filters[filterIndex].type = FilterType::kFilterHpf2p; break; - case hash("bpf_2p"): filters[filterIndex].type = FilterType::kFilterBpf2p; break; - case hash("brf_2p"): filters[filterIndex].type = FilterType::kFilterBrf2p; break; - case hash("bpf_1p"): filters[filterIndex].type = FilterType::kFilterBpf1p; break; - case hash("brf_1p"): filters[filterIndex].type = FilterType::kFilterBrf1p; break; - case hash("apf_1p"): filters[filterIndex].type = FilterType::kFilterApf1p; break; - case hash("lpf_2p_sv"): filters[filterIndex].type = FilterType::kFilterLpf2pSv; break; - case hash("hpf_2p_sv"): filters[filterIndex].type = FilterType::kFilterHpf2pSv; break; - case hash("bpf_2p_sv"): filters[filterIndex].type = FilterType::kFilterBpf2pSv; break; - case hash("brf_2p_sv"): filters[filterIndex].type = FilterType::kFilterBrf2pSv; break; - case hash("lpf_4p"): filters[filterIndex].type = FilterType::kFilterLpf4p; break; - case hash("hpf_4p"): filters[filterIndex].type = FilterType::kFilterHpf4p; break; - case hash("lpf_6p"): filters[filterIndex].type = FilterType::kFilterLpf6p; break; - case hash("hpf_6p"): filters[filterIndex].type = FilterType::kFilterHpf6p; break; - case hash("pink"): filters[filterIndex].type = FilterType::kFilterPink; break; - case hash("lsh"): filters[filterIndex].type = FilterType::kFilterLsh; break; - case hash("hsh"): filters[filterIndex].type = FilterType::kFilterHsh; break; - case hash("pkf_2p"): [[fallthrough]]; - case hash("peq"): filters[filterIndex].type = FilterType::kFilterPeq; break; - default: - filters[filterIndex].type = FilterType::kFilterNone; - DBG("Unknown filter type: " << std::string(opcode.value)); + absl::optional ftype = Filter::typeFromName(opcode.value); + + if (ftype) + filters[filterIndex].type = *ftype; + else { + filters[filterIndex].type = FilterType::kFilterNone; + DBG("Unknown filter type: " << std::string(opcode.value)); } } break; @@ -629,13 +610,13 @@ bool sfz::Region::parseOpcode(const Opcode& opcode) if (!extendIfNecessary(equalizers, eqNumber, Default::numEQs)) return false; - switch (hash(opcode.value)) { - case hash("peak"): equalizers[eqNumber - 1].type = EqType::kEqPeak; break; - case hash("lshelf"): equalizers[eqNumber - 1].type = EqType::kEqLowShelf; break; - case hash("hshelf"): equalizers[eqNumber - 1].type = EqType::kEqHighShelf; break; - default: - equalizers[eqNumber - 1].type = EqType::kEqNone; - DBG("Unknown EQ type: " << std::string(opcode.value)); + absl::optional ftype = FilterEq::typeFromName(opcode.value); + + if (ftype) + equalizers[eqNumber - 1].type = *ftype; + else { + equalizers[eqNumber - 1].type = EqType::kEqNone; + DBG("Unknown EQ type: " << std::string(opcode.value)); } } break; diff --git a/src/sfizz/SfzFilter.cpp b/src/sfizz/SfzFilter.cpp index fa7d354c..a172c68b 100644 --- a/src/sfizz/SfzFilter.cpp +++ b/src/sfizz/SfzFilter.cpp @@ -7,6 +7,7 @@ #include "Config.h" #include "SfzFilter.h" #include "SfzFilterImpls.cxx" +#include "StringViewHelpers.h" #include #include "SIMDHelpers.h" #include "Debug.h" @@ -203,6 +204,39 @@ void Filter::setType(FilterType type) } } +absl::optional Filter::typeFromName(absl::string_view name) +{ + absl::optional ftype; + + switch (hash(name)) { + case hash("lpf_1p"): ftype = kFilterLpf1p; break; + case hash("hpf_1p"): ftype = kFilterHpf1p; break; + case hash("lpf_2p"): ftype = kFilterLpf2p; break; + case hash("hpf_2p"): ftype = kFilterHpf2p; break; + case hash("bpf_2p"): ftype = kFilterBpf2p; break; + case hash("brf_2p"): ftype = kFilterBrf2p; break; + case hash("bpf_1p"): ftype = kFilterBpf1p; break; + case hash("brf_1p"): ftype = kFilterBrf1p; break; + case hash("apf_1p"): ftype = kFilterApf1p; break; + case hash("lpf_2p_sv"): ftype = kFilterLpf2pSv; break; + case hash("hpf_2p_sv"): ftype = kFilterHpf2pSv; break; + case hash("bpf_2p_sv"): ftype = kFilterBpf2pSv; break; + case hash("brf_2p_sv"): ftype = kFilterBrf2pSv; break; + case hash("lpf_4p"): ftype = kFilterLpf4p; break; + case hash("hpf_4p"): ftype = kFilterHpf4p; break; + case hash("lpf_6p"): ftype = kFilterLpf6p; break; + case hash("hpf_6p"): ftype = kFilterHpf6p; break; + case hash("pink"): ftype = kFilterPink; break; + case hash("lsh"): ftype = kFilterLsh; break; + case hash("hsh"): ftype = kFilterHsh; break; + case hash("bpk_2p"): [[fallthrough]]; + case hash("pkf_2p"): [[fallthrough]]; + case hash("peq"): ftype = kFilterPeq; break; + } + + return ftype; +} + sfzFilterDsp *Filter::Impl::getDsp(unsigned channels, FilterType type) { switch (idDsp(channels, type)) { @@ -410,6 +444,19 @@ void FilterEq::setType(EqType type) } } +absl::optional FilterEq::typeFromName(absl::string_view name) +{ + absl::optional ftype; + + switch (hash(name)) { + case hash("peak"): ftype = kEqPeak; break; + case hash("lshelf"): ftype = kEqLowShelf; break; + case hash("hshelf"): ftype = kEqHighShelf; break; + } + + return ftype; +} + sfzFilterDsp *FilterEq::Impl::getDsp(unsigned channels, EqType type) { switch (idDsp(channels, type)) { diff --git a/src/sfizz/SfzFilter.h b/src/sfizz/SfzFilter.h index ec6a8191..62cba612 100644 --- a/src/sfizz/SfzFilter.h +++ b/src/sfizz/SfzFilter.h @@ -5,6 +5,8 @@ // If not, contact the sfizz maintainers at https://github.com/sfztools/sfizz #pragma once +#include "absl/strings/string_view.h" +#include "absl/types/optional.h" #include namespace sfz { @@ -82,6 +84,11 @@ public: */ void setType(FilterType type); + /** + Get the filter type associated with the given name. + */ + static absl::optional typeFromName(absl::string_view name); + private: struct Impl; std::unique_ptr P; @@ -187,6 +194,11 @@ public: */ void setType(EqType type); + /** + Get the filter type associated with the given name. + */ + static absl::optional typeFromName(absl::string_view name); + private: struct Impl; std::unique_ptr P;