From 3f6e0e3d9666c7fc0f7599aa35535ad0f843a88a Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Thu, 26 Jan 2023 02:13:20 -0800 Subject: Combine multiple similar functions into one --- core/ambidefs.cpp | 140 +++++++----------------------------------------------- 1 file changed, 16 insertions(+), 124 deletions(-) diff --git a/core/ambidefs.cpp b/core/ambidefs.cpp index 4d78bf84..70d6f356 100644 --- a/core/ambidefs.cpp +++ b/core/ambidefs.cpp @@ -71,25 +71,6 @@ constexpr std::array FirstOrderEncoder{{ }}; static_assert(FirstOrderDecoder.size() == FirstOrderEncoder.size(), "First-order mismatch"); -auto CalcFirstOrderUp() -{ - std::array res{}; - - for(size_t i{0};i < FirstOrderDecoder[0].size();++i) - { - for(size_t j{0};j < FirstOrderEncoder[0].size();++j) - { - double sum{0.0}; - for(size_t k{0};k < FirstOrderDecoder.size();++k) - sum += double{FirstOrderDecoder[k][i]} * FirstOrderEncoder[k][j]; - res[i][j] = static_cast(sum); - } - } - - return res; -} - - /* This calculates a 2D first-order "upsampler" matrix. Same as the first-order * matrix, just using a more optimized speaker array for horizontal-only * content. @@ -108,24 +89,6 @@ constexpr std::array FirstOrder2DEncoder{{ }}; static_assert(FirstOrder2DDecoder.size() == FirstOrder2DEncoder.size(), "First-order 2D mismatch"); -auto CalcFirstOrder2DUp() -{ - std::array res{}; - - for(size_t i{0};i < FirstOrder2DDecoder[0].size();++i) - { - for(size_t j{0};j < FirstOrder2DEncoder[0].size();++j) - { - double sum{0.0}; - for(size_t k{0};k < FirstOrder2DDecoder.size();++k) - sum += double{FirstOrder2DDecoder[k][i]} * FirstOrder2DEncoder[k][j]; - res[i][j] = static_cast(sum); - } - } - - return res; -} - /* This calculates a second-order "upsampler" matrix. Same as the first-order * matrix, just using a slightly more dense speaker array suitable for second- @@ -161,25 +124,6 @@ constexpr std::array SecondOrderEncoder{{ }}; static_assert(SecondOrderDecoder.size() == SecondOrderEncoder.size(), "Second-order mismatch"); -auto CalcSecondOrderUp() -{ - std::array res{}; - - for(size_t i{0};i < SecondOrderDecoder[0].size();++i) - { - for(size_t j{0};j < SecondOrderEncoder[0].size();++j) - { - double sum{0.0}; - for(size_t k{0};k < SecondOrderDecoder.size();++k) - sum += double{SecondOrderDecoder[k][i]} * SecondOrderEncoder[k][j]; - res[i][j] = static_cast(sum); - } - } - - return res; -} - - /* This calculates a 2D second-order "upsampler" matrix. Same as the second- * order matrix, just using a more optimized speaker array for horizontal-only * content. @@ -203,24 +147,6 @@ constexpr std::array SecondOrder2DEncoder{{ static_assert(SecondOrder2DDecoder.size() == SecondOrder2DEncoder.size(), "Second-order 2D mismatch"); -auto CalcSecondOrder2DUp() -{ - std::array res{}; - - for(size_t i{0};i < SecondOrder2DDecoder[0].size();++i) - { - for(size_t j{0};j < SecondOrder2DEncoder[0].size();++j) - { - double sum{0.0}; - for(size_t k{0};k < SecondOrder2DDecoder.size();++k) - sum += double{SecondOrder2DDecoder[k][i]} * SecondOrder2DEncoder[k][j]; - res[i][j] = static_cast(sum); - } - } - - return res; -} - /* This calculates a third-order "upsampler" matrix. Same as the first-order * matrix, just using a more dense speaker array suitable for third-order @@ -272,25 +198,6 @@ constexpr std::array ThirdOrderEncoder{{ }}; static_assert(ThirdOrderDecoder.size() == ThirdOrderEncoder.size(), "Third-order mismatch"); -auto CalcThirdOrderUp() -{ - std::array res{}; - - for(size_t i{0};i < ThirdOrderDecoder[0].size();++i) - { - for(size_t j{0};j < ThirdOrderEncoder[0].size();++j) - { - double sum{0.0}; - for(size_t k{0};k < ThirdOrderDecoder.size();++k) - sum += double{ThirdOrderDecoder[k][i]} * ThirdOrderEncoder[k][j]; - res[i][j] = static_cast(sum); - } - } - - return res; -} - - /* This calculates a 2D third-order "upsampler" matrix. Same as the third-order * matrix, just using a more optimized speaker array for horizontal-only * content. @@ -317,24 +224,6 @@ constexpr std::array ThirdOrder2DEncoder{{ }}; static_assert(ThirdOrder2DDecoder.size() == ThirdOrder2DEncoder.size(), "Third-order 2D mismatch"); -auto CalcThirdOrder2DUp() -{ - std::array res{}; - - for(size_t i{0};i < ThirdOrder2DDecoder[0].size();++i) - { - for(size_t j{0};j < ThirdOrder2DEncoder[0].size();++j) - { - double sum{0.0}; - for(size_t k{0};k < ThirdOrder2DDecoder.size();++k) - sum += double{ThirdOrder2DDecoder[k][i]} * ThirdOrder2DEncoder[k][j]; - res[i][j] = static_cast(sum); - } - } - - return res; -} - /* This calculates a 2D fourth-order "upsampler" matrix. There is no 3D fourth- * order upsampler since fourth-order is the max order we'll be supporting for @@ -367,17 +256,20 @@ constexpr std::array FourthOrder2DEncoder{{ }}; static_assert(FourthOrder2DDecoder.size() == FourthOrder2DEncoder.size(), "Fourth-order 2D mismatch"); -auto CalcFourthOrder2DUp() + +template +auto CalcAmbiUpsampler(const std::array,M> &decoder, + const std::array &encoder) { - std::array res{}; + std::array res{}; - for(size_t i{0};i < FourthOrder2DDecoder[0].size();++i) + for(size_t i{0};i < decoder[0].size();++i) { - for(size_t j{0};j < FourthOrder2DEncoder[0].size();++j) + for(size_t j{0};j < encoder[0].size();++j) { double sum{0.0}; - for(size_t k{0};k < FourthOrder2DDecoder.size();++k) - sum += double{FourthOrder2DDecoder[k][i]} * FourthOrder2DEncoder[k][j]; + for(size_t k{0};k < decoder.size();++k) + sum += double{decoder[k][i]} * encoder[k][j]; res[i][j] = static_cast(sum); } } @@ -387,13 +279,13 @@ auto CalcFourthOrder2DUp() } // namespace -const std::array AmbiScale::FirstOrderUp{CalcFirstOrderUp()}; -const std::array AmbiScale::FirstOrder2DUp{CalcFirstOrder2DUp()}; -const std::array AmbiScale::SecondOrderUp{CalcSecondOrderUp()}; -const std::array AmbiScale::SecondOrder2DUp{CalcSecondOrder2DUp()}; -const std::array AmbiScale::ThirdOrderUp{CalcThirdOrderUp()}; -const std::array AmbiScale::ThirdOrder2DUp{CalcThirdOrder2DUp()}; -const std::array AmbiScale::FourthOrder2DUp{CalcFourthOrder2DUp()}; +const std::array AmbiScale::FirstOrderUp{CalcAmbiUpsampler(FirstOrderDecoder, FirstOrderEncoder)}; +const std::array AmbiScale::FirstOrder2DUp{CalcAmbiUpsampler(FirstOrder2DDecoder, FirstOrder2DEncoder)}; +const std::array AmbiScale::SecondOrderUp{CalcAmbiUpsampler(SecondOrderDecoder, SecondOrderEncoder)}; +const std::array AmbiScale::SecondOrder2DUp{CalcAmbiUpsampler(SecondOrder2DDecoder, SecondOrder2DEncoder)}; +const std::array AmbiScale::ThirdOrderUp{CalcAmbiUpsampler(ThirdOrderDecoder, ThirdOrderEncoder)}; +const std::array AmbiScale::ThirdOrder2DUp{CalcAmbiUpsampler(ThirdOrder2DDecoder, ThirdOrder2DEncoder)}; +const std::array AmbiScale::FourthOrder2DUp{CalcAmbiUpsampler(FourthOrder2DDecoder, FourthOrder2DEncoder)}; std::array AmbiScale::GetHFOrderScales(const uint src_order, -- cgit v1.2.3