From decc10da2bdbb611cce63916f8c6f8b17ea45da1 Mon Sep 17 00:00:00 2001
From: Chris Robinson <chris.kcat@gmail.com>
Date: Sat, 9 Dec 2023 10:02:27 -0800
Subject: More clang-tidy fixes

---
 core/bs2b.cpp | 127 +++++++++++++++++++++++++++++-----------------------------
 1 file changed, 64 insertions(+), 63 deletions(-)

(limited to 'core/bs2b.cpp')

diff --git a/core/bs2b.cpp b/core/bs2b.cpp
index 303bf9bd..9157c4d7 100644
--- a/core/bs2b.cpp
+++ b/core/bs2b.cpp
@@ -26,72 +26,74 @@
 #include <algorithm>
 #include <cmath>
 #include <iterator>
+#include <stdexcept>
 
 #include "alnumbers.h"
 #include "bs2b.h"
 
+namespace {
 
 /* Set up all data. */
-static void init(struct bs2b *bs2b)
+void init(Bs2b::bs2b *bs2b)
 {
     float Fc_lo, Fc_hi;
     float G_lo, G_hi;
-    float x, g;
 
     switch(bs2b->level)
     {
-    case BS2B_LOW_CLEVEL: /* Low crossfeed level */
+    case Bs2b::LowCLevel: /* Low crossfeed level */
         Fc_lo = 360.0f;
         Fc_hi = 501.0f;
         G_lo  = 0.398107170553497f;
         G_hi  = 0.205671765275719f;
         break;
 
-    case BS2B_MIDDLE_CLEVEL: /* Middle crossfeed level */
+    case Bs2b::MiddleCLevel: /* Middle crossfeed level */
         Fc_lo = 500.0f;
         Fc_hi = 711.0f;
         G_lo  = 0.459726988530872f;
         G_hi  = 0.228208484414988f;
         break;
 
-    case BS2B_HIGH_CLEVEL: /* High crossfeed level (virtual speakers are closer to itself) */
+    case Bs2b::HighCLevel: /* High crossfeed level (virtual speakers are closer to itself) */
         Fc_lo = 700.0f;
         Fc_hi = 1021.0f;
         G_lo  = 0.530884444230988f;
         G_hi  = 0.250105790667544f;
         break;
 
-    case BS2B_LOW_ECLEVEL: /* Low easy crossfeed level */
+    case Bs2b::LowECLevel: /* Low easy crossfeed level */
         Fc_lo = 360.0f;
         Fc_hi = 494.0f;
         G_lo  = 0.316227766016838f;
         G_hi  = 0.168236228897329f;
         break;
 
-    case BS2B_MIDDLE_ECLEVEL: /* Middle easy crossfeed level */
+    case Bs2b::MiddleECLevel: /* Middle easy crossfeed level */
         Fc_lo = 500.0f;
         Fc_hi = 689.0f;
         G_lo  = 0.354813389233575f;
         G_hi  = 0.187169483835901f;
         break;
 
-    default: /* High easy crossfeed level */
-        bs2b->level = BS2B_HIGH_ECLEVEL;
+    case Bs2b::HighECLevel: /* High easy crossfeed level */
+    default:
+        bs2b->level = Bs2b::HighECLevel;
 
         Fc_lo = 700.0f;
         Fc_hi = 975.0f;
         G_lo  = 0.398107170553497f;
         G_hi  = 0.205671765275719f;
         break;
-    } /* switch */
+    }
 
-    g = 1.0f / (1.0f - G_hi + G_lo);
+    float g{1.0f / (1.0f - G_hi + G_lo)};
 
     /* $fc = $Fc / $s;
      * $d  = 1 / 2 / pi / $fc;
      * $x  = exp(-1 / $d);
      */
-    x           = std::exp(-al::numbers::pi_v<float>*2.0f*Fc_lo/static_cast<float>(bs2b->srate));
+    float x{      std::exp(-al::numbers::pi_v<float>*2.0f*Fc_lo/static_cast<float>(bs2b->srate))};
     bs2b->b1_lo = x;
     bs2b->a0_lo = G_lo * (1.0f - x) * g;
 
@@ -99,85 +101,84 @@ static void init(struct bs2b *bs2b)
     bs2b->b1_hi = x;
     bs2b->a0_hi = (1.0f - G_hi * (1.0f - x)) * g;
     bs2b->a1_hi = -x * g;
-} /* init */
+}
 
+} // namespace
 
 /* Exported functions.
  * See descriptions in "bs2b.h"
  */
+namespace Bs2b {
 
-void bs2b_set_params(struct bs2b *bs2b, int level, int srate)
-{
-    if(srate <= 0) srate = 1;
-
-    bs2b->level = level;
-    bs2b->srate = srate;
-    init(bs2b);
-} /* bs2b_set_params */
-
-int bs2b_get_level(struct bs2b *bs2b)
+void bs2b::set_params(int level_, int srate_)
 {
-    return bs2b->level;
-} /* bs2b_get_level */
+    if(srate_ < 1)
+        throw std::runtime_error{"BS2B srate < 1"};
 
-int bs2b_get_srate(struct bs2b *bs2b)
-{
-    return bs2b->srate;
-} /* bs2b_get_srate */
+    level = level_;
+    srate = srate_;
+    init(this);
+}
 
-void bs2b_clear(struct bs2b *bs2b)
+void bs2b::clear()
 {
-    std::fill(std::begin(bs2b->history), std::end(bs2b->history), bs2b::t_last_sample{});
-} /* bs2b_clear */
+    history.fill(bs2b::t_last_sample{});
+}
 
-void bs2b_cross_feed(struct bs2b *bs2b, float *Left, float *Right, size_t SamplesToDo)
+void bs2b::cross_feed(float *RESTRICT Left, float *RESTRICT Right, size_t SamplesToDo)
 {
-    const float a0_lo{bs2b->a0_lo};
-    const float b1_lo{bs2b->b1_lo};
-    const float a0_hi{bs2b->a0_hi};
-    const float a1_hi{bs2b->a1_hi};
-    const float b1_hi{bs2b->b1_hi};
-    float lsamples[128][2];
-    float rsamples[128][2];
+    const float a0lo{a0_lo};
+    const float b1lo{b1_lo};
+    const float a0hi{a0_hi};
+    const float a1hi{a1_hi};
+    const float b1hi{b1_hi};
+    std::array<std::array<float,2>,128> samples;
 
     for(size_t base{0};base < SamplesToDo;)
     {
-        const size_t todo{std::min<size_t>(128, SamplesToDo-base)};
+        const size_t todo{std::min(samples.size(), SamplesToDo-base)};
 
         /* Process left input */
-        float z_lo{bs2b->history[0].lo};
-        float z_hi{bs2b->history[0].hi};
+        float z_lo{history[0].lo};
+        float z_hi{history[0].hi};
         for(size_t i{0};i < todo;i++)
         {
-            lsamples[i][0] = a0_lo*Left[i] + z_lo;
-            z_lo = b1_lo*lsamples[i][0];
-
-            lsamples[i][1] = a0_hi*Left[i] + z_hi;
-            z_hi = a1_hi*Left[i] + b1_hi*lsamples[i][1];
+            const float x{Left[i]};
+            float y{a0hi*x + z_hi};
+            z_hi = a1hi*x + b1hi*y;
+            samples[i][0] = y;
+
+            y = a0lo*x + z_lo;
+            z_lo = b1lo*y;
+            samples[i][1] = y;
         }
-        bs2b->history[0].lo = z_lo;
-        bs2b->history[0].hi = z_hi;
+        history[0].lo = z_lo;
+        history[0].hi = z_hi;
 
         /* Process right input */
-        z_lo = bs2b->history[1].lo;
-        z_hi = bs2b->history[1].hi;
+        z_lo = history[1].lo;
+        z_hi = history[1].hi;
         for(size_t i{0};i < todo;i++)
         {
-            rsamples[i][0] = a0_lo*Right[i] + z_lo;
-            z_lo = b1_lo*rsamples[i][0];
-
-            rsamples[i][1] = a0_hi*Right[i] + z_hi;
-            z_hi = a1_hi*Right[i] + b1_hi*rsamples[i][1];
+            const float x{Right[i]};
+            float y{a0lo*x + z_lo};
+            z_lo = b1lo*y;
+            samples[i][0] += y;
+
+            y = a0hi*x + z_hi;
+            z_hi = a1hi*x + b1hi*y;
+            samples[i][1] += y;
         }
-        bs2b->history[1].lo = z_lo;
-        bs2b->history[1].hi = z_hi;
+        history[1].lo = z_lo;
+        history[1].hi = z_hi;
 
-        /* Crossfeed */
         for(size_t i{0};i < todo;i++)
-            *(Left++) = lsamples[i][1] + rsamples[i][0];
+            *(Left++) = samples[i][0];
         for(size_t i{0};i < todo;i++)
-            *(Right++) = rsamples[i][1] + lsamples[i][0];
+            *(Right++) = samples[i][1];
 
         base += todo;
     }
-} /* bs2b_cross_feed */
+}
+
+} // namespace Bs2b
-- 
cgit v1.2.3