// ----
// ---- file   : lut_bipolar_test_1.cpp
// ---- author : 
// ---- legal  : Distributed under terms of the MIT license (https://opensource.org/licenses/MIT)
// ----
// ----          Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
// ----          associated documentation files (the "Software"), to deal in the Software without restriction, including
// ----          without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// ----          copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to
// ----          the following conditions:
// ----
// ----          The above copyright notice and this permission notice shall be included in all copies or substantial
// ----          portions of the Software.
// ----
// ----          THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
// ----          NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// ----          IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// ----          WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
// ----          SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// ----
// ---- info   : auto-generated by "Cycle"
// ----           $ g++ -Wall -Wno-unused-function -Wno-unused-variable -I../../tksampler -c lut_bipolar_test_1.cpp -o lut_bipolar_test_1.o
// ---- created: 12Apr2026 11:57:20
// ----
// ----
// ----

// (note) pre-define CYCLE_SKIP_UI to skip all code used only for UI/editing purposes (e.g. when compiling for standalone replay)
// #define CYCLE_SKIP_UI  defined

#include <plugin.h>

#include <stdlib.h>
#include <math.h>
#include <string.h>

#define PARAM_AMP                0
#define PARAM_WS_EXP             1
#define NUM_PARAMS               2
#ifndef CYCLE_SKIP_UI
static const char *loc_param_names[NUM_PARAMS
#if defined(_MSC_VER) && 0 == NUM_PARAMS
                                   +1
#endif
                                   ] = {
#if defined(_MSC_VER) && 0 == NUM_PARAMS
                                   ""
#else
   "amp",                     // 0: AMP
   "ws_exp",                  // 1: WS_EXP

#endif
};
#endif // CYCLE_SKIP_UI
static float loc_param_resets[NUM_PARAMS
#if defined(_MSC_VER) && 0 == NUM_PARAMS
                                   +1
#endif
                              ] = {
#if defined(_MSC_VER) && 0 == NUM_PARAMS
   0.0f
#else
   0.5f,                      // 0: AMP
   0.5f,                      // 1: WS_EXP

#endif
};

#define MOD_AMP                  0
#define MOD_WS_EXP               1
#define NUM_MODS                 2
#ifndef CYCLE_SKIP_UI
static const char *loc_mod_names[NUM_MODS
#if defined(_MSC_VER) && 0 == NUM_MODS
   +1
#endif
                                 ] = {
#if defined(_MSC_VER) && 0 == NUM_MODS
   ""
#else
   "amp",                  // 0: AMP
   "ws_exp",               // 1: WS_EXP

#endif
};
#endif // CYCLE_SKIP_UI

typedef struct lut_bipolar_test_1_info_s {
   st_plugin_info_t base;
} lut_bipolar_test_1_info_t;

typedef struct lut_bipolar_test_1_shared_s {
   st_plugin_shared_t base;
   float params[NUM_PARAMS
#if defined(_MSC_VER) && 0 == NUM_PARAMS
                +1
#endif
                ];

} lut_bipolar_test_1_shared_t;

typedef struct lut_bipolar_test_1_voice_s {
   st_plugin_voice_t base;
   float             sample_rate;
   float             bpm;
   float             velocity;
   float             note_speed_fixed;
   float             note_speed_cur;
   float             note_speed_inc;
   float             note_cur;
   float             note_inc;
   float             mods[NUM_MODS
#if defined(_MSC_VER) && 0 == NUM_MODS
                          +1
#endif
                          ];
   float mod_amp_cur;
   float mod_amp_inc;
   float mod_ws_exp_cur;
   float mod_ws_exp_inc;

   float tmp1_sin_phase;
   float tmp2;
   float tmp3;
   short tmp4;
   short tmp5;
   short tmp6;
   float tmp7;
   float tmp8;
   float var_x;
   float sr_factor;

} lut_bipolar_test_1_voice_t;

#define loop(X)  for(unsigned int i = 0u; i < (X); i++)
#define clamp(a,b,c) (((a)<(b))?(b):(((a)>(c))?(c):(a)))

static inline float mathLerpf(float _a, float _b, float _t) { return _a + (_b - _a) * _t; }
static inline float mathClampf(float a, float b, float c) { return (((a)<(b))?(b):(((a)>(c))?(c):(a))); }
static inline float mathMinf(float a, float b) { return (a<b)?a:b; }
static inline float mathMaxf(float a, float b) { return (a>b)?a:b; }
static inline float mathAbsMaxf(float _x, float _y) { return ( ( (_x<0.0f)?-_x:_x)>((_y<0.0f)?-_y:_y)?_x:_y ); }
static inline float mathAbsMinf(float _x, float _y) { return ( ((_x<0.0f)?-_x:_x)<((_y<0.0f)?-_y:_y)?_x:_y ); }
static inline float frac(float _x) { return _x - ((int)_x); }

static inline float winLinear(const float *_s, float _index) {
   int idx = (int)_index;
   float r = _index - (float)idx;
   return mathLerpf(_s[idx], _s[idx+1], r);
}

static float ffrac_s(float _f) { int i; if(_f >= 0.0f) { i = (int)_f; return _f - (float)i; } else { i = (int)-_f; return 1.0f - (-_f - (float)i); } }
static short curve_0[1024] = {
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 
33, 34, 35, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 48, 49, 50, 51, 52, 53, 54, 55, 55, 56, 57, 58, 59, 60, 61, 
61, 62, 63, 64, 65, 66, 66, 67, 68, 69, 70, 71, 71, 72, 73, 74, 75, 75, 76, 77, 78, 79, 79, 80, 81, 82, 82, 83, 84, 85, 85, 86, 
87, 88, 88, 89, 90, 91, 91, 92, 93, 94, 94, 95, 96, 96, 97, 98, 99, 99, 100, 101, 101, 102, 103, 104, 104, 105, 106, 106, 107, 108, 108, 109, 
110, 110, 111, 112, 112, 113, 114, 114, 115, 116, 116, 117, 118, 118, 119, 120, 120, 121, 122, 122, 123, 124, 124, 125, 125, 126, 127, 127, 128, 129, 129, 130, 
130, 131, 132, 132, 133, 133, 134, 135, 135, 136, 137, 137, 138, 138, 139, 140, 140, 141, 141, 142, 143, 143, 144, 144, 145, 145, 146, 147, 147, 148, 148, 149, 
150, 150, 151, 151, 152, 152, 153, 154, 154, 155, 155, 156, 156, 157, 158, 158, 159, 159, 160, 160, 161, 161, 162, 163, 163, 164, 164, 165, 165, 166, 166, 167, 
167, 168, 169, 169, 170, 170, 171, 171, 172, 172, 173, 173, 174, 175, 175, 176, 176, 177, 177, 178, 178, 179, 179, 180, 180, 181, 182, 182, 183, 183, 184, 184, 
185, 185, 186, 186, 187, 187, 188, 188, 189, 190, 190, 191, 191, 192, 192, 193, 193, 194, 194, 195, 195, 196, 196, 197, 197, 198, 199, 199, 200, 200, 201, 201, 
202, 202, 203, 203, 204, 204, 205, 205, 206, 206, 207, 208, 208, 209, 209, 210, 210, 211, 211, 212, 212, 213, 213, 214, 214, 215, 216, 216, 217, 217, 218, 218, 
219, 219, 220, 220, 221, 222, 222, 223, 223, 224, 224, 225, 225, 226, 226, 227, 228, 228, 229, 229, 230, 230, 231, 231, 232, 233, 233, 234, 234, 235, 235, 236, 
236, 237, 238, 238, 239, 239, 240, 240, 241, 242, 242, 243, 243, 244, 244, 245, 246, 246, 247, 247, 248, 249, 249, 250, 250, 251, 251, 252, 253, 253, 254, 254, 
255, 256, 256, 257, 258, 258, 259, 259, 260, 261, 261, 262, 262, 263, 264, 264, 265, 266, 266, 267, 268, 268, 269, 269, 270, 271, 271, 272, 273, 273, 274, 275, 
275, 276, 277, 277, 278, 279, 279, 280, 281, 281, 282, 283, 283, 284, 285, 286, 286, 287, 288, 288, 289, 290, 291, 291, 292, 293, 293, 294, 295, 296, 296, 297, 
298, 299, 299, 300, 301, 302, 302, 303, 304, 305, 305, 306, 307, 308, 309, 309, 310, 311, 312, 313, 313, 314, 315, 316, 317, 318, 318, 319, 320, 321, 322, 323, 
323, 324, 325, 326, 327, 328, 329, 330, 330, 331, 332, 333, 334, 335, 336, 337, 338, 339, 340, 341, 341, 342, 343, 344, 345, 346, 347, 348, 349, 350, 351, 352, 
353, 354, 355, 356, 357, 358, 359, 360, 361, 363, 364, 365, 366, 367, 368, 369, 370, 371, 372, 373, 375, 376, 377, 378, 379, 380, 382, 383, 384, 385, 386, 388, 
389, 390, 391, 392, 394, 395, 396, 398, 399, 400, 401, 403, 404, 405, 407, 408, 409, 411, 412, 414, 415, 416, 418, 419, 421, 422, 424, 425, 426, 428, 429, 431, 
432, 434, 436, 437, 439, 440, 442, 444, 445, 447, 448, 450, 452, 453, 455, 457, 459, 460, 462, 464, 466, 468, 469, 471, 473, 475, 477, 479, 481, 483, 485, 487, 
489, 491, 493, 495, 497, 499, 501, 503, 505, 507, 510, 512, 514, 516, 519, 521, 523, 525, 528, 530, 532, 535, 537, 540, 542, 544, 547, 549, 552, 554, 557, 559, 
562, 564, 567, 569, 572, 575, 577, 580, 583, 585, 588, 591, 593, 596, 599, 602, 604, 607, 610, 613, 616, 619, 621, 624, 627, 630, 633, 636, 639, 642, 645, 648, 
651, 654, 657, 661, 664, 667, 670, 673, 676, 680, 683, 686, 689, 693, 696, 699, 703, 706, 710, 713, 716, 720, 723, 727, 730, 734, 737, 741, 745, 748, 752, 756, 
759, 763, 767, 770, 774, 778, 782, 786, 789, 793, 797, 801, 805, 809, 813, 817, 821, 825, 829, 833, 837, 841, 845, 850, 854, 858, 862, 866, 871, 875, 879, 883, 
888, 892, 897, 901, 905, 910, 914, 919, 923, 928, 932, 937, 941, 946, 951, 955, 960, 965, 969, 974, 979, 984, 988, 993, 998, 1003, 1008, 1012, 1017, 1022, 1027, 1032, 
1037, 1042, 1047, 1052, 1057, 1062, 1067, 1072, 1077, 1082, 1087, 1092, 1097, 1102, 1107, 1113, 1118, 1123, 1128, 1133, 1138, 1144, 1149, 1154, 1159, 1164, 1170, 1175, 1180, 1185, 1191, 1196, 
1201, 1206, 1212, 1217, 1222, 1228, 1233, 1238, 1243, 1249, 1254, 1259, 1265, 1270, 1275, 1280, 1286, 1291, 1296, 1302, 1307, 1312, 1317, 1323, 1328, 1333, 1338, 1344, 1349, 1354, 1359, 1365, 
1370, 1375, 1380, 1385, 1390, 1396, 1401, 1406, 1411, 1416, 1421, 1426, 1431, 1436, 1441, 1446, 1451, 1456, 1461, 1466, 1471, 1476, 1481, 1486, 1491, 1496, 1501, 1505, 1510, 1515, 1520, 1525, 
1529, 1534, 1539, 1544, 1548, 1553, 1558, 1562, 1567, 1571, 1576, 1580, 1585, 1590, 1594, 1598, 1603, 1607, 1612, 1616, 1621, 1625, 1629, 1634, 1638, 1642, 1646, 1651, 1655, 1659, 1663, 1667, 
1671, 1676, 1680, 1684, 1688, 1692, 1696, 1700, 1704, 1708, 1712, 1715, 1719, 1723, 1727, 1731, 1735, 1739, 1742, 1746, 1750, 1753, 1757, 1761, 1764, 1768, 1772, 1775, 1779, 1782, 1786, 1789, 
1793, 1796, 1800, 1803, 1807, 1810, 1813, 1817, 1820, 1823, 1827, 1830, 1833, 1836, 1840, 1843, 1846, 1849, 1852, 1856, 1859, 1862, 1865, 1868, 1871, 1874, 1877, 1880, 1883, 1886, 1889, 1892, 
1895, 1898, 1900, 1903, 1906, 1909, 1912, 1915, 1917, 1920, 1923, 1926, 1928, 1931, 1934, 1936, 1939, 1942, 1944, 1947, 1949, 1952, 1955, 1957, 1960, 1962, 1965, 1967, 1970, 1972, 1974, 1977, 
1979, 1982, 1984, 1986, 1989, 1991, 1994, 1996, 1998, 2000, 2003, 2005, 2007, 2009, 2012, 2014, 2016, 2018, 2020, 2023, 2025, 2027, 2029, 2031, 2033, 2035, 2037, 2039, 2041, 2043, 2045, 2047, 
};

#define USE_CYCLE_SINE_TBL  defined
static float cycle_sine_tbl_f[16384];

static float mathLogf(const float _x) {
   union {
      float f;
      unsigned int u;
   } bx;
   bx.f = _x;
   const unsigned int ex = bx.u >> 23;
   const signed int t = (signed int)ex - (signed int)127;
   const unsigned int s = (t < 0) ? (-t) : t;
   bx.u = 1065353216u | (bx.u & 8388607u);
   return
      -1.7417939f + (2.8212026f + (-1.4699568f + (0.44717955f - 0.056570851f * bx.f)*bx.f)*bx.f)*bx.f
      + 0.6931471806f * t;
}

static float mathPowerf(float _x, float _y) {
   float r;
   if(_y != 0.0f)
   {
      if(_x < 0.0f)
      {
         r = (float)( -expf(_y*mathLogf(-_x)) );
      }
      else if(_x > 0.0f)
      {
         r = (float)( expf(_y*mathLogf(_x)) );
      }
      else
      {
         r = 0.0f;
      }
   }
   else
   {
      r = 1.0f;
   }
   return Dstplugin_fix_denorm_32(r);
}



#ifndef CYCLE_SKIP_UI
static unsigned int loc_copy_chars(char *_d, const unsigned int _dSize, const char *_s) {
   unsigned int r = 0u;
   if(NULL != _d)
   {
      // Write min(dSize,len(s)) characters to 'd'
      char *d = _d;
      if(NULL != _s)
      {
         const char *s = _s;
         for(;;)
         {
            if(r < _dSize)
            {
               char c = s[r++];
               *d++ = c;
               if(0 == c)
                  break;
            }
            else
            {
               if(r > 0u)
                  d[-1] = 0;  // force ASCIIZ
               break;
            }
         }
      }
      else
      {
         if(_dSize > 0u)
         {
            _d[0] = 0;
            r = 1u;
         }
      }
   }
   else
   {
      // Return total length of source string
      if(NULL != _s)
      {
         const char *s = _s;
         for(;;)
         {
            char c = s[r++];
            if(0 == c)
               break;
         }
      }
   }
   return r;
}
#endif // CYCLE_SKIP_UI

#ifndef CYCLE_SKIP_UI
static unsigned int loc_copy_floats(float              *_d,
                                    const unsigned int  _dSize,
                                    const float        *_s,
                                    const unsigned int  _sNum
                                    ) {
   unsigned int r = 0u;
   if(NULL != _d)
   {
      // Write min(dSize,sNum) characters to 'd'
      if(NULL != _s)
      {
         unsigned int num = (_sNum > _dSize) ? _dSize : _sNum;
         memcpy((void*)_d, (const void*)_s, num * sizeof(float));
         r = num;
      }
   }
   else
   {
      // Query total number of presets
      r = _sNum;
   }
   return r;
}
#endif // CYCLE_SKIP_UI

#ifndef CYCLE_SKIP_UI
static const char *ST_PLUGIN_API loc_get_param_name(st_plugin_info_t *_info,
                                                    unsigned int      _paramIdx
                                                    ) {
   (void)_info;
   return loc_param_names[_paramIdx];
}
#endif // CYCLE_SKIP_UI

#ifndef CYCLE_SKIP_UI
static const char *ST_PLUGIN_API loc_get_param_group_name(st_plugin_info_t *_info,
                                                          unsigned int      _paramGroupIdx
                                                          ) {
   (void)_info;
   const char *r = NULL;
   return r;
}
#endif // CYCLE_SKIP_UI

#ifndef CYCLE_SKIP_UI
static unsigned int ST_PLUGIN_API loc_get_param_group_idx(st_plugin_info_t *_info,
                                                          unsigned int      _paramIdx
                                                          ) {
   (void)_info;
   unsigned int r = ~0u;
   return r;
}
#endif // CYCLE_SKIP_UI

#ifndef CYCLE_SKIP_UI
static const char *ST_PLUGIN_API loc_get_param_section_name(st_plugin_info_t *_info,
                                                            unsigned int      _paramSectionIdx
                                                            ) {
   (void)_info;
   const char *r = NULL;
   return r;
}
#endif // CYCLE_SKIP_UI

#ifndef CYCLE_SKIP_UI
static unsigned int ST_PLUGIN_API loc_get_param_section_idx(st_plugin_info_t *_info,
                                                            unsigned int      _paramIdx
                                                            ) {
   (void)_info;
   unsigned int r = ~0u;
   return r;
}
#endif // CYCLE_SKIP_UI

#ifndef CYCLE_SKIP_UI
static float ST_PLUGIN_API loc_get_param_reset(st_plugin_info_t *_info,
                                               unsigned int      _paramIdx
                                               ) {
   (void)_info;
   return loc_param_resets[_paramIdx];
}
#endif // CYCLE_SKIP_UI

static float ST_PLUGIN_API loc_get_param_value(st_plugin_shared_t *_shared,
                                               unsigned int        _paramIdx
                                               ) {
   ST_PLUGIN_SHARED_CAST(lut_bipolar_test_1_shared_t);
   return shared->params[_paramIdx];
}

static void ST_PLUGIN_API loc_set_param_value(st_plugin_shared_t *_shared,
                                              unsigned int        _paramIdx,
                                              float               _value
                                              ) {
   ST_PLUGIN_SHARED_CAST(lut_bipolar_test_1_shared_t);
   shared->params[_paramIdx] = _value;
}

#ifndef CYCLE_SKIP_UI
static unsigned int ST_PLUGIN_API loc_query_dynamic_param_preset_values(st_plugin_shared_t *_shared,
                                                                        const unsigned int  _paramIdx,
                                                                        float              *_retValues,
                                                                        const unsigned int  _retValuesSize
                                                                        ) {
   ST_PLUGIN_SHARED_CAST(lut_bipolar_test_1_shared_t);
   unsigned int r = 0u;
   return r;
}
#endif // CYCLE_SKIP_UI

#ifndef CYCLE_SKIP_UI
static unsigned int ST_PLUGIN_API loc_query_dynamic_param_preset_name(st_plugin_shared_t *_shared,
                                                                      const unsigned int  _paramIdx,
                                                                      const unsigned int  _presetIdx,
                                                                      char               *_retBuf,
                                                                      const unsigned int  _retBufSize
                                                                      ) {
   ST_PLUGIN_SHARED_CAST(lut_bipolar_test_1_shared_t);
   unsigned int r = 0u;
   return r;
}
#endif // CYCLE_SKIP_UI

#ifndef CYCLE_SKIP_UI
static unsigned int ST_PLUGIN_API loc_get_array_param_size(st_plugin_info_t   *_info,
                                                           const unsigned int  _paramIdx
                                                           ) {
   unsigned int r = 0u;
   switch(_paramIdx)
   {
      default:
         break;
   }
   return r;
}
#endif // CYCLE_SKIP_UI

#ifndef CYCLE_SKIP_UI
static unsigned int ST_PLUGIN_API loc_get_array_param_num_variations(st_plugin_info_t   *_info,
                                                                     const unsigned int  _paramIdx
                                                                     ) {
   unsigned int r = 0u;
   switch(_paramIdx)
   {
      default:
         break;
   }
   return r;
}
#endif // CYCLE_SKIP_UI

#ifndef CYCLE_SKIP_UI
static float * ST_PLUGIN_API loc_get_array_param_variation_ptr(st_plugin_shared_t *_shared,
                                                               const unsigned int  _paramIdx,
                                                               const unsigned int  _variationIdx
                                                               ) {
   ST_PLUGIN_SHARED_CAST(lut_bipolar_test_1_shared_t);
   float *r = NULL;
   switch(_paramIdx)
   {
      default:
         break;
   }
   return r;
}
#endif // CYCLE_SKIP_UI

#ifndef CYCLE_SKIP_UI
static void ST_PLUGIN_API loc_set_array_param_edit_variation_idx(st_plugin_shared_t *_shared,
                                                                 const unsigned int  _paramIdx,
                                                                 const int           _variationIdx
                                                                 ) {
   ST_PLUGIN_SHARED_CAST(lut_bipolar_test_1_shared_t);
   switch(_paramIdx)
   {
      default:
         break;
   }
}
#endif // CYCLE_SKIP_UI

#ifndef CYCLE_SKIP_UI
static const char *ST_PLUGIN_API loc_get_array_param_element_name(st_plugin_info_t   *_info,
                                                                  const unsigned int  _paramIdx,
                                                                  const unsigned int  _elementIdx
                                                                  ) {
   ST_PLUGIN_INFO_CAST(lut_bipolar_test_1_info_t);
   const char *r = NULL;
   switch(_paramIdx)
   {
      default:
         break;
   }
   return r;
}
#endif // CYCLE_SKIP_UI

#ifndef CYCLE_SKIP_UI
static int ST_PLUGIN_API loc_get_array_param_element_value_range(st_plugin_info_t   *_info,
                                                                 const unsigned int  _paramIdx,
                                                                 const unsigned int  _elementIdx,
                                                                 float              *_retStorageMin,
                                                                 float              *_retStorageMax,
                                                                 float              *_retDisplayMin,
                                                                 float              *_retDisplayMax,
                                                                 unsigned int       *_retDisplayPrecision
                                                                 ) {
   (void)_elementIdx;
   switch(_paramIdx)
   {
      default:
         break;
   }
   return 0;
}
#endif // CYCLE_SKIP_UI

#ifndef CYCLE_SKIP_UI
static float ST_PLUGIN_API loc_get_array_param_element_reset(st_plugin_info_t   *_info,
                                                             const unsigned int  _paramIdx,
                                                             const unsigned int  _elementIdx
                                                             ) {
   ST_PLUGIN_INFO_CAST(lut_bipolar_test_1_info_t);
   float r = -999999.0f/*INVALID_VALUE*/;
   switch(_paramIdx)
   {
      default:
         break;
   }
   return r;
}
#endif // CYCLE_SKIP_UI

#ifndef CYCLE_SKIP_UI
static void ST_PLUGIN_API loc_update_lut(st_plugin_info_t   *_info,
                                         unsigned int        _idx,
                                         const float        *_src,
                                         const unsigned int  _srcNumElements
                                         ) {
   (void)_info;
   unsigned int copySz = 0u;
   (void)copySz;
   switch(_idx)
   {
      default:
         break;
      case 0: // "ws"
         copySz = Dstplugin_min(_srcNumElements, 1024);
         for(unsigned int i = 0u; i < copySz; i++)
            curve_0[i] = (short)(_src[i] * 2048);
         break;
   }
}
#endif // CYCLE_SKIP_UI

#ifndef CYCLE_SKIP_UI
static const char *ST_PLUGIN_API loc_get_mod_name(st_plugin_info_t *_info,
                                                  unsigned int      _modIdx
                                                  ) {
   (void)_info;
   return loc_mod_names[_modIdx];
}
#endif // CYCLE_SKIP_UI

static void ST_PLUGIN_API loc_set_sample_rate(st_plugin_voice_t *_voice,
                                              float              _sampleRate
                                              ) {
   ST_PLUGIN_VOICE_CAST(lut_bipolar_test_1_voice_t);
   if(_sampleRate != voice->sample_rate)
   {
      voice->sr_factor = 48000.0f / _sampleRate;
   }
   voice->sample_rate = _sampleRate;
}

static void ST_PLUGIN_API loc_set_bpm(st_plugin_voice_t *_voice,
                                      float              _bpm
                                      ) {
   ST_PLUGIN_VOICE_CAST(lut_bipolar_test_1_voice_t);
   voice->bpm = _bpm;
}

static void ST_PLUGIN_API loc_note_on(st_plugin_voice_t  *_voice,
                                      int                 _bGlide,
                                      unsigned char       _note,
                                      float               _vel
                                      ) {
   ST_PLUGIN_VOICE_CAST(lut_bipolar_test_1_voice_t);
   ST_PLUGIN_VOICE_SHARED_CAST(lut_bipolar_test_1_shared_t);
   (void)_bGlide;
   (void)_note;
   (void)_vel;
   if(!_bGlide)
   {
      memset((void*)voice->mods, 0, sizeof(voice->mods));
#ifdef OVERSAMPLE_FACTOR
      voice->note_speed_fixed = (261.63f/*C-5*/ / (voice->sample_rate * OVERSAMPLE_FACTOR));
#else
      voice->note_speed_fixed = (261.63f/*C-5*/ / voice->sample_rate);
#endif // OVERSAMPLE_FACTOR
      voice->velocity = _vel;
      voice->tmp1_sin_phase = 0.0f;
      voice->var_x = 0.0f;
   }
}

static void ST_PLUGIN_API loc_set_mod_value(st_plugin_voice_t *_voice,
                                            unsigned int       _modIdx,
                                            float              _value,
                                            unsigned           _frameOffset
                                            ) {
   ST_PLUGIN_VOICE_CAST(lut_bipolar_test_1_voice_t);
   (void)_frameOffset;
   voice->mods[_modIdx] = _value;
}

static void ST_PLUGIN_API loc_prepare_block(st_plugin_voice_t *_voice,
                                            unsigned int       _numFrames,
                                            float              _freqHz,
                                            float              _note,
                                            float              _vol,
                                            float              _pan
                                            ) {
   ST_PLUGIN_VOICE_CAST(lut_bipolar_test_1_voice_t);
   ST_PLUGIN_VOICE_SHARED_CAST(lut_bipolar_test_1_shared_t);
   (void)_note;
   (void)_vol;
   (void)_pan;

#ifdef OVERSAMPLE_FACTOR
   float noteSpeed = _freqHz / (voice->sample_rate * OVERSAMPLE_FACTOR);
#else
   float noteSpeed = _freqHz / voice->sample_rate;
#endif // OVERSAMPLE_FACTOR

   float modamp          = shared->params[PARAM_AMP         ]                       + voice->mods[MOD_AMP          ];
   float modws_exp       = shared->params[PARAM_WS_EXP      ]                       + voice->mods[MOD_WS_EXP       ];

   if(_numFrames > 0u)
   {
      // lerp
#ifdef OVERSAMPLE_FACTOR
      float recBlockSize = (1.0f / (_numFrames * OVERSAMPLE_FACTOR));
#else
      float recBlockSize = (1.0f / _numFrames);
#endif // OVERSAMPLE_FACTOR
      voice->note_speed_inc = (noteSpeed - voice->note_speed_cur) * recBlockSize;
      voice->note_inc       = (_note - voice->note_cur) * recBlockSize;
      voice->mod_amp_inc          = (modamp             - voice->mod_amp_cur           ) * recBlockSize;
      voice->mod_ws_exp_inc       = (modws_exp          - voice->mod_ws_exp_cur        ) * recBlockSize;
   }
   else
   {
      // initial params/modulation (first block, not rendered)
      voice->note_speed_cur = noteSpeed;
      voice->note_speed_inc = 0.0f;
      voice->note_cur       = _note;
      voice->note_inc       = 0.0f;
      voice->mod_amp_cur          = modamp;
      voice->mod_amp_inc          = 0.0f;
      voice->mod_ws_exp_cur       = modws_exp;
      voice->mod_ws_exp_inc       = 0.0f;
   }

   // printf("xxx note_cur=%f\n", voice->note_cur);
   // printf("xxx prepare_block: numFrames=%u moda=%f\n", _numFrames, moda);
   // printf("xxx voice->voicebus_idx_0=%u voice->base.voice_bus_read_offset=%u\n", voice->voicebus_idx_0, voice->base.voice_bus_read_offset);
}

static void ST_PLUGIN_API loc_process_replace(st_plugin_voice_t  *_voice,
                                              int                 _bMonoIn,
                                              const float        *_samplesIn,
                                              float              *_samplesOut,
                                              unsigned int        _numFrames
                                              ) {
   (void)_bMonoIn;
   (void)_samplesIn;

   ST_PLUGIN_VOICE_CAST(lut_bipolar_test_1_voice_t);
   ST_PLUGIN_VOICE_SHARED_CAST(lut_bipolar_test_1_shared_t);

   // Mono output (replicate left to right channel)
   unsigned int j = 0u;
#ifndef STEREO
   unsigned int jStep = _bMonoIn ? 1u : 2u;
#endif // STEREO
   unsigned int k = 0u;
   for(unsigned int i = 0u; i < _numFrames; i++)
   {
      const float inL = _samplesIn[j];
#ifdef STEREO
      float outL;
      float outR;
      const float inR = _samplesIn[j + 1u];
#endif // STEREO
      float out;
#ifdef OVERSAMPLE_FACTOR
#ifdef STEREO
      float outOSL = 0.0f;
      float outOSR = 0.0f;
#else
      float outOS = 0.0f;
#endif // STEREO
#endif // OVERSAMPLE_FACTOR

#ifdef OVERSAMPLE_FACTOR
      for(unsigned int osi = 0u; osi < OVERSAMPLE_FACTOR; osi++)
#endif // OVERSAMPLE_FACTOR
      {
#ifdef STEREO
         out = outL = inL;
         outR = inR;
#else
         out = inL;
#endif // STEREO
      float tmp_f; (void)tmp_f;
      float tmp2_f; (void)tmp2_f;
      
      // ========
      // ======== lane "out" modIdx=0 modString=sin
      // ========
      
      // -- mod="sin" dstVar=out
      voice->tmp3/*sin_freq*/ = 1;
      voice->tmp2/*sin_speed*/ = voice->note_speed_cur * voice->tmp3/*sin_freq*/;
      voice->tmp3/*sin_tmp*/ = (voice->tmp1_sin_phase);
      voice->tmp3/*sin_tmp*/ = ffrac_s(voice->tmp3/*sin_tmp*/);
      out = cycle_sine_tbl_f[(unsigned short)(16384 * voice->tmp3/*sin_tmp*/)&16383u];
      voice->tmp1_sin_phase = ffrac_s(voice->tmp1_sin_phase + voice->tmp2/*sin_speed*/);
      
      // -- mod="pow" dstVar=out
      
      // ---- mod="pow" input "exp" seq 1/1
      
      // -- mod="$m_ws_exp" dstVar=voice->tmp2/*exp*/
      voice->tmp2/*exp*/ = voice->mod_ws_exp_cur;
      out = mathPowerf(out, voice->tmp2/*exp*/);
      
      // -- mod="lut" dstVar=out
      voice->tmp4/*lut_bip_sign*/ = (out < 0.0f);
      if(voice->tmp4/*lut_bip_sign*/) out = -out;
      voice->tmp2/*lut_f*/ = (out * 1024);
      voice->tmp3/*lut_frac*/ = voice->tmp2/*lut_f*/ - (int)voice->tmp2/*lut_f*/;
      voice->tmp5/*lut_idx_a*/ = (int)voice->tmp2/*lut_f*/;
      if(voice->tmp5/*lut_idx_a*/ < 0)
      {
         voice->tmp5/*lut_idx_a*/ = 0;
         voice->tmp6/*lut_idx_b*/ = 0;
         voice->tmp3/*lut_frac*/ = 0.0f;
      }
      else if(voice->tmp5/*lut_idx_a*/ > 1023)
      {
         voice->tmp5/*lut_idx_a*/ = 1023;
         voice->tmp6/*lut_idx_b*/ = 1023;
      }
      else
      {
         voice->tmp6/*lut_idx_b*/ = voice->tmp5/*lut_idx_a*/ + 1;
         if(voice->tmp6/*lut_idx_b*/ > 1023)
            voice->tmp6/*lut_idx_b*/ = 1023;
      }
      voice->tmp7/*lut_a*/ = curve_0[(unsigned int)voice->tmp5/*lut_idx_a*/] * (1.0f / 2048);
      voice->tmp8/*lut_b*/ = curve_0[(unsigned int)voice->tmp6/*lut_idx_b*/] * (1.0f / 2048);
      out = voice->tmp7/*lut_a*/ + (voice->tmp8/*lut_b*/ - voice->tmp7/*lut_a*/) * voice->tmp3/*lut_frac*/;
      if(voice->tmp4/*lut_bip_sign*/) out = -out;
      
      // -- mod="$m_amp" dstVar=out
      voice->tmp2/*seq*/ = out;
      voice->tmp2/*seq*/ = voice->mod_amp_cur;
      
      // -- mod="2" dstVar=voice->tmp2/*seq*/
      voice->tmp2/*seq*/ *= 2.0f;
      out *= voice->tmp2/*seq*/;
      
      // -- mod="clp" dstVar=out
      if(out > 0.999f) out = 0.999f;
      else if(out < -0.999f) out = -0.999f;
      

      /* end calc */

#ifdef OVERSAMPLE_FACTOR
#ifdef STEREO
         outOSL += outL;
         outOSR += outR;
#else
         outOS += out;
#endif // STEREO
#endif // OVERSAMPLE_FACTOR
         voice->note_speed_cur += voice->note_speed_inc;
         voice->note_cur       += voice->note_inc;
         voice->mod_amp_cur        += voice->mod_amp_inc;
         voice->mod_ws_exp_cur     += voice->mod_ws_exp_inc;
      }
#ifdef OVERSAMPLE_FACTOR
      // Apply lowpass filter before downsampling
      //   (note) normalized Fc = F/Fs = 0.442947 / sqrt(oversample_factor^2 - 1)
#ifdef STEREO
      outL = outOSL * (1.0f / OVERSAMPLE_FACTOR);
      outR = outOSR * (1.0f / OVERSAMPLE_FACTOR);
#else
      out = outOS * (1.0f / OVERSAMPLE_FACTOR);
#endif // STEREO
#endif // OVERSAMPLE_FACTOR
#ifdef STEREO
      outL = Dstplugin_fix_denorm_32(outL);
      outR = Dstplugin_fix_denorm_32(outR);
#else
      out = Dstplugin_fix_denorm_32(out);
#endif // STEREO
#ifdef STEREO
      _samplesOut[k]      = outL;
      _samplesOut[k + 1u] = outR;
#else
      _samplesOut[k]      = out;
      _samplesOut[k + 1u] = out;
#endif // STEREO

      // Next frame
      k += 2u;
#ifdef STEREO
      j += 2u;
#else
      j += jStep;
#endif // STEREO
   } /* loop numFrames */
}

static st_plugin_shared_t *ST_PLUGIN_API loc_shared_new(st_plugin_info_t *_info) {
   lut_bipolar_test_1_shared_t *ret = (lut_bipolar_test_1_shared_t *)malloc(sizeof(lut_bipolar_test_1_shared_t));
   if(NULL != ret)
   {
      memset((void*)ret, 0, sizeof(*ret));
      ret->base.info  = _info;
      memcpy((void*)ret->params, (void*)loc_param_resets, NUM_PARAMS * sizeof(float));
   }
   return &ret->base;
}

static void ST_PLUGIN_API loc_shared_delete(st_plugin_shared_t *_shared) {
   free((void*)_shared);
}

static st_plugin_voice_t *ST_PLUGIN_API loc_voice_new(st_plugin_info_t *_info, unsigned int _voiceIdx) {
   lut_bipolar_test_1_voice_t *voice = (lut_bipolar_test_1_voice_t *)malloc(sizeof(lut_bipolar_test_1_voice_t));
   if(NULL != voice)
   {
      memset((void*)voice, 0, sizeof(*voice));
      voice->base.info = _info;
   }
   return &voice->base;
}

static void ST_PLUGIN_API loc_voice_delete(st_plugin_voice_t *_voice) {
   ST_PLUGIN_VOICE_CAST(lut_bipolar_test_1_voice_t);

   free((void*)_voice);
   }

static void ST_PLUGIN_API loc_plugin_exit(st_plugin_info_t *_info) {
   free((void*)_info);
}

#ifdef USE_CYCLE_SINE_TBL
static void loc_calc_sine_tbl(void) {
#define QCOS_BITS 16
#define QCOS_ONE  (1 << QCOS_BITS)
#define QCOS_MASK (QCOS_ONE - 1)
   // (note) same as in TSR "C" implementation
   float *qcos = cycle_sine_tbl_f + 4096; // quarter cos tbl

   // calc quarter cos tbl
   int k = 0;
   for(int a = 0; a < (QCOS_ONE/4); a += (QCOS_ONE/16384)/*4*/)  // 4096 elements
   {
      int x = a - ((int)(QCOS_ONE * 0.25 + 0.5)) + ((a + ((int)(QCOS_ONE * 0.25 + 0.5)))&~QCOS_MASK);
      x = ((x * ((x<0?-x:x) - ((int)(QCOS_ONE * 0.5 + 0.5)))) >> QCOS_BITS) << 4;
      x += (((((int)(QCOS_ONE * 0.225 + 0.5)) * x) >> QCOS_BITS) * ((x<0?-x:x) - QCOS_ONE)) >> QCOS_BITS;
      qcos[k++] = x / float(QCOS_ONE);
   }

   // 0..90deg (rev qtbl)
   int j = 4096;
   k = 0;
   loop(4096)
      cycle_sine_tbl_f[k++] = qcos[--j];

   // 90..180deg = cos tbl
   k += 4096;

   // 180..360deg (y-flip first half of tbl)
   j = 0;
   loop(8192)
      cycle_sine_tbl_f[k++] = -cycle_sine_tbl_f[j++];

#ifdef USE_CYCLE_SINE_TBL_I
   loop(16384)
      cycle_sine_tbl_i[i] = (short)(cycle_sine_tbl_f[i] * 2048.0f/*FX_ONE*/);
#endif // USE_CYCLE_SINE_TBL_I
}
#endif // USE_CYCLE_SINE_TBL

#ifdef USE_CYCLE_HSE_TBL
#endif // USE_CYCLE_HSE_TBL

extern "C" {
st_plugin_info_t *lut_bipolar_test_1_init(void) {
   lut_bipolar_test_1_info_t *ret = (lut_bipolar_test_1_info_t *)malloc(sizeof(lut_bipolar_test_1_info_t));

   if(NULL != ret)
   {
      memset(ret, 0, sizeof(*ret));

      ret->base.api_version = ST_PLUGIN_API_VERSION;
      ret->base.id          = "lut_bipolar_test_1_0x4c699a38";  // unique id. don't change this in future builds.
      ret->base.author      = "";
      ret->base.name        = "lut_bipolar_test_1";
      ret->base.short_name  = "lut_bipolar_test_1";
      ret->base.flags       = ST_PLUGIN_FLAG_OSC;
      ret->base.category    = ST_PLUGIN_CAT_UNKNOWN;
      ret->base.num_params  = NUM_PARAMS;
      ret->base.num_mods    = NUM_MODS;

      ret->base.shared_new                          = &loc_shared_new;
      ret->base.shared_delete                       = &loc_shared_delete;
      ret->base.voice_new                           = &loc_voice_new;
      ret->base.voice_delete                        = &loc_voice_delete;
#ifndef CYCLE_SKIP_UI
      ret->base.get_param_name                      = &loc_get_param_name;
      ret->base.get_param_group_name                = &loc_get_param_group_name;
      ret->base.get_param_group_idx                 = &loc_get_param_group_idx;
      ret->base.get_param_section_name              = &loc_get_param_section_name;
      ret->base.get_param_section_idx               = &loc_get_param_section_idx;
      ret->base.get_param_reset                     = &loc_get_param_reset;
      ret->base.query_dynamic_param_preset_values   = &loc_query_dynamic_param_preset_values;
      ret->base.query_dynamic_param_preset_name     = &loc_query_dynamic_param_preset_name;
      ret->base.get_array_param_size                = &loc_get_array_param_size;
      ret->base.get_array_param_num_variations      = &loc_get_array_param_num_variations;
      ret->base.get_array_param_variation_ptr       = &loc_get_array_param_variation_ptr;
      ret->base.set_array_param_edit_variation_idx  = &loc_set_array_param_edit_variation_idx;
      ret->base.get_array_param_element_name        = &loc_get_array_param_element_name;
      ret->base.get_array_param_element_value_range = &loc_get_array_param_element_value_range;
      ret->base.get_array_param_element_reset       = &loc_get_array_param_element_reset;
      ret->base.update_lut                          = &loc_update_lut;
#endif // CYCLE_SKIP_UI
      ret->base.get_param_value                     = &loc_get_param_value;
      ret->base.set_param_value                     = &loc_set_param_value;
#ifndef CYCLE_SKIP_UI
      ret->base.get_mod_name                        = &loc_get_mod_name;
#endif // CYCLE_SKIP_UI
      ret->base.set_sample_rate                     = &loc_set_sample_rate;
      ret->base.set_bpm                             = &loc_set_bpm;
      ret->base.note_on                             = &loc_note_on;
      ret->base.set_mod_value                       = &loc_set_mod_value;
      ret->base.prepare_block                       = &loc_prepare_block;
      ret->base.process_replace                     = &loc_process_replace;
      ret->base.plugin_exit                         = &loc_plugin_exit;

#ifdef USE_CYCLE_SINE_TBL
      loc_calc_sine_tbl();
#endif // USE_CYCLE_SINE_TBL
   }

   return &ret->base;
}

#ifndef STFX_SKIP_MAIN_INIT
ST_PLUGIN_APICALL st_plugin_info_t *ST_PLUGIN_API st_plugin_init(unsigned int _pluginIdx) {
   switch(_pluginIdx)
   {
      case 0u:
         return lut_bipolar_test_1_init();
   }
   return NULL;
}
#endif // STFX_SKIP_MAIN_INIT

} // extern "C"
