// ----
// ---- file   : osc_fm3s_ftri.cpp
// ---- author : bsp
// ---- 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 osc_fm3s_ftri.cpp -o osc_fm3s_ftri.o
// ---- created: 11Nov2023 19:16:40
// ----
// ----
// ----

#include <plugin.h>

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


#define OVERSAMPLE_FACTOR  16.0f

#define PARAM_COLOR_1            0
#define PARAM_COLOR_2            1
#define PARAM_COLOR_3            2
#define PARAM_PHASE_RAND         3
#define PARAM_RATIO_2            4
#define PARAM_RATIO_3            5
#define PARAM_LEVEL_2            6
#define PARAM_LEVEL_3            7
#define NUM_PARAMS               8
static const char *loc_param_names[NUM_PARAMS] = {
   "color_1",                 // 0: COLOR_1
   "color_2",                 // 1: COLOR_2
   "color_3",                 // 2: COLOR_3
   "phase_rand",              // 3: PHASE_RAND
   "ratio_2",                 // 4: RATIO_2
   "ratio_3",                 // 5: RATIO_3
   "level_2",                 // 6: LEVEL_2
   "level_3",                 // 7: LEVEL_3

};
static float loc_param_resets[NUM_PARAMS] = {
   0.5f,                      // 0: COLOR_1
   0.5f,                      // 1: COLOR_2
   0.5f,                      // 2: COLOR_3
   0.0f,                      // 3: PHASE_RAND
   0.5f,                      // 4: RATIO_2
   0.5f,                      // 5: RATIO_3
   0.0f,                      // 6: LEVEL_2
   0.0f,                      // 7: LEVEL_3

};

#define MOD_COLOR_1              0
#define MOD_COLOR_2              1
#define MOD_COLOR_3              2
#define MOD_PHASE_23             3
#define MOD_RATIO_2              4
#define MOD_RATIO_3              5
#define MOD_LEVEL_2              6
#define MOD_LEVEL_3              7
#define NUM_MODS                 8
static const char *loc_mod_names[NUM_MODS] = {
   "color_1",      // 0: COLOR_1
   "color_2",      // 1: COLOR_2
   "color_3",      // 2: COLOR_3
   "phase_23",     // 3: PHASE_23
   "ratio_2",      // 4: RATIO_2
   "ratio_3",      // 5: RATIO_3
   "level_2",      // 6: LEVEL_2
   "level_3",      // 7: LEVEL_3

};

typedef struct osc_fm3s_ftri_info_s {
   st_plugin_info_t base;
} osc_fm3s_ftri_info_t;

typedef struct osc_fm3s_ftri_shared_s {
   st_plugin_shared_t base;
   float params[NUM_PARAMS];

} osc_fm3s_ftri_shared_t;

typedef struct osc_fm3s_ftri_voice_s {
   st_plugin_voice_t base;
   float             sample_rate;
   float             note_speed_fixed;
   float             note_speed_cur;
   float             note_speed_inc;
   float             note_cur;
   float             note_inc;
   float             mods[NUM_MODS];
   float mod_color_1_cur;
   float mod_color_1_inc;
   float mod_color_2_cur;
   float mod_color_2_inc;
   float mod_color_3_cur;
   float mod_color_3_inc;
   float mod_phase_23_cur;
   float mod_phase_23_inc;
   float mod_ratio_2_cur;
   float mod_ratio_2_inc;
   float mod_ratio_3_cur;
   float mod_ratio_3_inc;
   float mod_level_2_cur;
   float mod_level_2_inc;
   float mod_level_3_cur;
   float mod_level_3_inc;

   short tmp1_i2f;
   unsigned short tmp2_lfsr_state;
   short tmp3_lfsr_state_signed;
   float tmp4_seq;
   short tmp5_i2f;
   unsigned short tmp6_lfsr_state;
   short tmp7_lfsr_state_signed;
   float tmp8_seq;
   short tmp9_i2f;
   unsigned short tmp10_lfsr_state;
   short tmp11_lfsr_state_signed;
   float tmp12_seq;
   float tmp13_tri_phase;
   float tmp14_tri_speed;
   float tmp15_freq;
   float tmp16_tri_tmp;
   float tmp17_phase;
   float tmp18_tri_phase;
   float tmp19_tri_speed;
   float tmp20_freq;
   float tmp21_tri_tmp;
   float tmp22_phase;
   float tmp23_tri_phase;
   float tmp24_tri_speed;
   float tmp25_freq;
   float tmp26_tri_tmp;
   float tmp27_phase;
   float tmp28_seq;
   float tmp29_svf_lp;
   float tmp30_svf_hp;
   float tmp31_svf_bp;
   float tmp32_freq;
   float tmp33_svf_lp;
   float tmp34_svf_hp;
   float tmp35_svf_bp;
   float tmp36_freq;
   float tmp37_seq;
   float tmp38_seq;
   float tmp39_seq;
   float tmp40_svf_lp;
   float tmp41_svf_hp;
   float tmp42_svf_bp;
   float tmp43_freq;
   float tmp44_svf_lp;
   float tmp45_svf_hp;
   float tmp46_svf_bp;
   float tmp47_freq;
   float tmp48_seq;
   float tmp49_seq;
   float tmp50_seq;
   float tmp51_seq;
   float tmp52_svf_lp;
   float tmp53_svf_hp;
   float tmp54_svf_bp;
   float tmp55_freq;
   float tmp56_svf_lp;
   float tmp57_svf_hp;
   float tmp58_svf_bp;
   float tmp59_freq;
   float var_x;
   float var_v_phrand_1;
   float var_v_phrand_2;
   float var_v_phrand_3;
   float var_v_color_1;
   float var_v_color_2;
   float var_v_color_3;

} osc_fm3s_ftri_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 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 float mathLogf(const float _x) {
   union {
      float f;
      unsigned int ui;
   } bx;
   bx.f = _x;
   const unsigned int ex = bx.ui >> 23;
   const signed int t = (signed int)ex - (signed int)127;
   const unsigned int s = (t < 0) ? (-t) : t;
   bx.ui = 1065353216u | (bx.ui & 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);
}

static float loc_bipolar_to_scale(const float _t, const float _div, const float _mul) {
   // t (-1..1) => /_div .. *_mul
   
   float s;

   if(_t < 0.0f)
   {
      s = (1.0f / _div);
      s = 1.0f + (s - 1.0f) * -_t;
      if(s < 0.0f)
         s = 0.0f;
   }
   else
   {
      s = _mul;
      s = 1.0f + (s - 1.0f) * _t;
   }
   
   return s;
}

static void loc_init(st_plugin_voice_t *_voice);

void loc_init(st_plugin_voice_t *_voice) {
   ST_PLUGIN_VOICE_CAST(osc_fm3s_ftri_voice_t);
   ST_PLUGIN_VOICE_SHARED_CAST(osc_fm3s_ftri_shared_t);

   float out = 0.0f;
   // -------- lane "init" modIdx=0
   
   // -- mod="fsr" dstVar=out
   voice->tmp1_i2f = (int)(out * 2048);  // IntFallback: F2I
   voice->tmp2_lfsr_state ^= voice->tmp2_lfsr_state >> 7;
   voice->tmp2_lfsr_state ^= voice->tmp2_lfsr_state << 9;
   voice->tmp2_lfsr_state ^= voice->tmp2_lfsr_state >> 13;
   voice->tmp3_lfsr_state_signed = (voice->tmp2_lfsr_state & 65520);
   voice->tmp1_i2f = voice->tmp3_lfsr_state_signed >> 4;
   out = voice->tmp1_i2f / ((float)(2048));  // IntFallback: I2F
   
   // -- mod="$p_phase_rand" dstVar=out
   voice->tmp4_seq = shared->params[PARAM_PHASE_RAND];
   out *= voice->tmp4_seq;
   
   // -- mod="sto v_phrand_1" dstVar=out
   voice->var_v_phrand_1 = out;
   
   // -- mod="fsr" dstVar=out
   voice->tmp5_i2f = (int)(out * 2048);  // IntFallback: F2I
   voice->tmp6_lfsr_state ^= voice->tmp6_lfsr_state >> 7;
   voice->tmp6_lfsr_state ^= voice->tmp6_lfsr_state << 9;
   voice->tmp6_lfsr_state ^= voice->tmp6_lfsr_state >> 13;
   voice->tmp7_lfsr_state_signed = (voice->tmp6_lfsr_state & 65520);
   voice->tmp5_i2f = voice->tmp7_lfsr_state_signed >> 4;
   out = voice->tmp5_i2f / ((float)(2048));  // IntFallback: I2F
   
   // -- mod="$p_phase_rand" dstVar=out
   voice->tmp8_seq = shared->params[PARAM_PHASE_RAND];
   out *= voice->tmp8_seq;
   
   // -- mod="sto v_phrand_2" dstVar=out
   voice->var_v_phrand_2 = out;
   
   // -- mod="fsr" dstVar=out
   voice->tmp9_i2f = (int)(out * 2048);  // IntFallback: F2I
   voice->tmp10_lfsr_state ^= voice->tmp10_lfsr_state >> 7;
   voice->tmp10_lfsr_state ^= voice->tmp10_lfsr_state << 9;
   voice->tmp10_lfsr_state ^= voice->tmp10_lfsr_state >> 13;
   voice->tmp11_lfsr_state_signed = (voice->tmp10_lfsr_state & 65520);
   voice->tmp9_i2f = voice->tmp11_lfsr_state_signed >> 4;
   out = voice->tmp9_i2f / ((float)(2048));  // IntFallback: I2F
   
   // -- mod="$p_phase_rand" dstVar=out
   voice->tmp12_seq = shared->params[PARAM_PHASE_RAND];
   out *= voice->tmp12_seq;
   
   // -- mod="sto v_phrand_3" dstVar=out
   voice->var_v_phrand_3 = out;
} /* end init */

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];
}

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

static float ST_PLUGIN_API loc_get_param_value(st_plugin_shared_t *_shared,
                                               unsigned int        _paramIdx
                                               ) {
   ST_PLUGIN_SHARED_CAST(osc_fm3s_ftri_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(osc_fm3s_ftri_shared_t);
   shared->params[_paramIdx] = _value;
}

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];
}

static void ST_PLUGIN_API loc_set_sample_rate(st_plugin_voice_t *_voice,
                                              float              _sampleRate
                                              ) {
   ST_PLUGIN_VOICE_CAST(osc_fm3s_ftri_voice_t);
   voice->sample_rate = _sampleRate;
}

static void ST_PLUGIN_API loc_note_on(st_plugin_voice_t  *_voice,
                                      int                 _bGlide,
                                      unsigned char       _note,
                                      float               _vel
                                      ) {
   ST_PLUGIN_VOICE_CAST(osc_fm3s_ftri_voice_t);
   ST_PLUGIN_VOICE_SHARED_CAST(osc_fm3s_ftri_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 = (0.5f / (voice->sample_rate * OVERSAMPLE_FACTOR));
#else
      voice->note_speed_fixed = (0.5f / voice->sample_rate);
#endif // OVERSAMPLE_FACTOR


      voice->tmp3_lfsr_state_signed = 0;
      voice->tmp7_lfsr_state_signed = 0;
      voice->tmp11_lfsr_state_signed = 0;
      voice->tmp13_tri_phase = 0.0f;
      voice->tmp18_tri_phase = 0.0f;
      voice->tmp23_tri_phase = 0.0f;
      voice->tmp29_svf_lp = 0.0f;
      voice->tmp30_svf_hp = 0.0f;
      voice->tmp31_svf_bp = 0.0f;
      voice->tmp33_svf_lp = 0.0f;
      voice->tmp34_svf_hp = 0.0f;
      voice->tmp35_svf_bp = 0.0f;
      voice->tmp40_svf_lp = 0.0f;
      voice->tmp41_svf_hp = 0.0f;
      voice->tmp42_svf_bp = 0.0f;
      voice->tmp44_svf_lp = 0.0f;
      voice->tmp45_svf_hp = 0.0f;
      voice->tmp46_svf_bp = 0.0f;
      voice->tmp52_svf_lp = 0.0f;
      voice->tmp53_svf_hp = 0.0f;
      voice->tmp54_svf_bp = 0.0f;
      voice->tmp56_svf_lp = 0.0f;
      voice->tmp57_svf_hp = 0.0f;
      voice->tmp58_svf_bp = 0.0f;

      voice->var_x = 0.0f;
      voice->var_v_phrand_1 = 0.0f;
      voice->var_v_phrand_2 = 0.0f;
      voice->var_v_phrand_3 = 0.0f;
      voice->var_v_color_1 = 0.0f;
      voice->var_v_color_2 = 0.0f;
      voice->var_v_color_3 = 0.0f;

      loc_init(&voice->base);

   }
}

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(osc_fm3s_ftri_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(osc_fm3s_ftri_voice_t);
   ST_PLUGIN_VOICE_SHARED_CAST(osc_fm3s_ftri_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 modcolor_1      = shared->params[PARAM_COLOR_1     ] * 0.5f                + voice->mods[MOD_COLOR_1      ];
   float modcolor_2      = shared->params[PARAM_COLOR_2     ] * 0.5f                + voice->mods[MOD_COLOR_2      ];
   float modcolor_3      = shared->params[PARAM_COLOR_3     ] * 0.5f                + voice->mods[MOD_COLOR_3      ];
   float modphase_23     = voice->mods[MOD_PHASE_23     ];
   float modratio_2      = shared->params[PARAM_RATIO_2     ]                       + voice->mods[MOD_RATIO_2      ];
   float modratio_3      = shared->params[PARAM_RATIO_3     ]                       + voice->mods[MOD_RATIO_3      ];
   float modlevel_2      = shared->params[PARAM_LEVEL_2     ]                       + voice->mods[MOD_LEVEL_2      ];
   float modlevel_3      = shared->params[PARAM_LEVEL_3     ]                       + voice->mods[MOD_LEVEL_3      ];

   if(_numFrames > 0u)
   {
      // lerp
      float recBlockSize = (1.0f / _numFrames);
      voice->note_speed_inc = (noteSpeed - voice->note_speed_cur) * recBlockSize;
      voice->note_inc       = (_note - voice->note_cur) * recBlockSize;
      voice->mod_color_1_inc      = (modcolor_1         - voice->mod_color_1_cur       ) * recBlockSize;
      voice->mod_color_2_inc      = (modcolor_2         - voice->mod_color_2_cur       ) * recBlockSize;
      voice->mod_color_3_inc      = (modcolor_3         - voice->mod_color_3_cur       ) * recBlockSize;
      voice->mod_phase_23_inc     = (modphase_23        - voice->mod_phase_23_cur      ) * recBlockSize;
      voice->mod_ratio_2_inc      = (modratio_2         - voice->mod_ratio_2_cur       ) * recBlockSize;
      voice->mod_ratio_3_inc      = (modratio_3         - voice->mod_ratio_3_cur       ) * recBlockSize;
      voice->mod_level_2_inc      = (modlevel_2         - voice->mod_level_2_cur       ) * recBlockSize;
      voice->mod_level_3_inc      = (modlevel_3         - voice->mod_level_3_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_color_1_cur      = modcolor_1;
      voice->mod_color_1_inc      = 0.0f;
      voice->mod_color_2_cur      = modcolor_2;
      voice->mod_color_2_inc      = 0.0f;
      voice->mod_color_3_cur      = modcolor_3;
      voice->mod_color_3_inc      = 0.0f;
      voice->mod_phase_23_cur     = modphase_23;
      voice->mod_phase_23_inc     = 0.0f;
      voice->mod_ratio_2_cur      = modratio_2;
      voice->mod_ratio_2_inc      = 0.0f;
      voice->mod_ratio_3_cur      = modratio_3;
      voice->mod_ratio_3_inc      = 0.0f;
      voice->mod_level_2_cur      = modlevel_2;
      voice->mod_level_2_inc      = 0.0f;
      voice->mod_level_3_cur      = modlevel_3;
      voice->mod_level_3_inc      = 0.0f;

   
   }

   // printf("xxx note_cur=%f\n", voice->note_cur);
}

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(osc_fm3s_ftri_voice_t);
   ST_PLUGIN_VOICE_SHARED_CAST(osc_fm3s_ftri_shared_t);

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

#ifdef OVERSAMPLE_FACTOR
      for(unsigned int osi = 0u; osi < OVERSAMPLE_FACTOR; osi++)
#endif // OVERSAMPLE_FACTOR
      {
         float tmp_f;
         float tmp2_f;
         
         // ========
         // ======== lane "out" modIdx=0
         // ========
         
         // -- mod="$m_color_1" dstVar=out
         out = voice->mod_color_1_cur;
         
         // -- mod="p2s" dstVar=out
         out  = (mathPowerf(2.0f, out * 7.0f) - 1.0f);
         out *= 0.00392157f;
         
         // -- mod="clp" dstVar=out
         if(out > 1.0f) out = 1.0f;
         else if(out < 0.0f) out = 0.0f;
         
         // -- mod="sto v_color_1" dstVar=out
         voice->var_v_color_1 = out;
         
         // -- mod="$m_color_2" dstVar=out
         out = voice->mod_color_2_cur;
         
         // -- mod="p2s" dstVar=out
         out  = (mathPowerf(2.0f, out * 7.0f) - 1.0f);
         out *= 0.00392157f;
         
         // -- mod="clp" dstVar=out
         if(out > 1.0f) out = 1.0f;
         else if(out < 0.0f) out = 0.0f;
         
         // -- mod="sto v_color_2" dstVar=out
         voice->var_v_color_2 = out;
         
         // -- mod="$m_color_3" dstVar=out
         out = voice->mod_color_3_cur;
         
         // -- mod="p2s" dstVar=out
         out  = (mathPowerf(2.0f, out * 7.0f) - 1.0f);
         out *= 0.00392157f;
         
         // -- mod="clp" dstVar=out
         if(out > 1.0f) out = 1.0f;
         else if(out < 0.0f) out = 0.0f;
         
         // -- mod="sto v_color_3" dstVar=out
         voice->var_v_color_3 = out;
         
         // -- mod="tri" dstVar=out
         voice->tmp15_freq = 1;
         voice->tmp14_tri_speed = voice->note_speed_cur * voice->tmp15_freq;
         
         // ---- mod="tri" input "phase" seq 1/1
         
         // -- mod="tri" dstVar=voice->tmp17_phase
         
         // ---- mod="tri" input "freq" seq 1/1
         
         // -- mod="$m_ratio_2" dstVar=voice->tmp20_freq
         voice->tmp20_freq = voice->mod_ratio_2_cur;
         
         // -- mod="fma" dstVar=voice->tmp20_freq
         voice->tmp20_freq = (voice->tmp20_freq * 2.0f) + -1.0f;
         
         // -- mod="bts" dstVar=voice->tmp20_freq
         voice->tmp20_freq = loc_bipolar_to_scale(voice->tmp20_freq, 4096.0f, 4.0f);
         
         // -- mod="pow" dstVar=voice->tmp20_freq
         voice->tmp20_freq = voice->tmp20_freq * voice->tmp20_freq * voice->tmp20_freq;
         
         // -- mod="qua" dstVar=voice->tmp20_freq
         voice->tmp20_freq = ((int)(voice->tmp20_freq * 100.0f) / 100.0f);
         voice->tmp19_tri_speed = voice->note_speed_cur * voice->tmp20_freq;
         
         // ---- mod="tri" input "phase" seq 1/1
         
         // -- mod="tri" dstVar=voice->tmp22_phase
         
         // ---- mod="tri" input "freq" seq 1/1
         
         // -- mod="$m_ratio_3" dstVar=voice->tmp25_freq
         voice->tmp25_freq = voice->mod_ratio_3_cur;
         
         // -- mod="fma" dstVar=voice->tmp25_freq
         voice->tmp25_freq = (voice->tmp25_freq * 2.0f) + -1.0f;
         
         // -- mod="bts" dstVar=voice->tmp25_freq
         voice->tmp25_freq = loc_bipolar_to_scale(voice->tmp25_freq, 4096.0f, 4.0f);
         
         // -- mod="pow" dstVar=voice->tmp25_freq
         voice->tmp25_freq = voice->tmp25_freq * voice->tmp25_freq * voice->tmp25_freq;
         
         // -- mod="qua" dstVar=voice->tmp25_freq
         voice->tmp25_freq = ((int)(voice->tmp25_freq * 100.0f) / 100.0f);
         voice->tmp24_tri_speed = voice->note_speed_cur * voice->tmp25_freq;
         
         // ---- mod="tri" input "phase" seq 1/1
         
         // -- mod="$v_phrand_3" dstVar=voice->tmp27_phase
         voice->tmp27_phase = voice->var_v_phrand_3;
         
         // -- mod="$m_phase_23" dstVar=voice->tmp27_phase
         voice->tmp28_seq = voice->mod_phase_23_cur;
         voice->tmp27_phase += voice->tmp28_seq;
         voice->tmp26_tri_tmp = ((voice->tmp23_tri_phase + voice->tmp27_phase));
         voice->tmp26_tri_tmp = ffrac_s(voice->tmp26_tri_tmp);
         voice->tmp22_phase = (voice->tmp26_tri_tmp < 0.5f) ? (-1.0 + voice->tmp26_tri_tmp * 4.0f) : (1.0 - (voice->tmp26_tri_tmp - 0.5f)*4);
         voice->tmp23_tri_phase = ffrac_s(voice->tmp23_tri_phase + voice->tmp24_tri_speed);
         
         // -- mod="svf" dstVar=voice->tmp22_phase
         
         // ---- mod="svf" input "freq" seq 1/1
         
         // -- mod="$v_color_3" dstVar=voice->tmp32_freq
         voice->tmp32_freq = voice->var_v_color_3;
         voice->tmp29_svf_lp = voice->tmp29_svf_lp + (voice->tmp31_svf_bp * voice->tmp32_freq);
         voice->tmp30_svf_hp = voice->tmp22_phase - voice->tmp29_svf_lp - (voice->tmp31_svf_bp * 1.0f);
         voice->tmp31_svf_bp = voice->tmp31_svf_bp + (voice->tmp30_svf_hp * voice->tmp32_freq);
         voice->tmp22_phase = voice->tmp29_svf_lp;
         
         // -- mod="svf" dstVar=voice->tmp22_phase
         
         // ---- mod="svf" input "freq" seq 1/1
         
         // -- mod="$v_color_3" dstVar=voice->tmp36_freq
         voice->tmp36_freq = voice->var_v_color_3;
         voice->tmp33_svf_lp = voice->tmp33_svf_lp + (voice->tmp35_svf_bp * voice->tmp36_freq);
         voice->tmp34_svf_hp = voice->tmp22_phase - voice->tmp33_svf_lp - (voice->tmp35_svf_bp * 1.0f);
         voice->tmp35_svf_bp = voice->tmp35_svf_bp + (voice->tmp34_svf_hp * voice->tmp36_freq);
         voice->tmp22_phase = voice->tmp33_svf_lp;
         
         // -- mod="$m_level_3" dstVar=voice->tmp22_phase
         voice->tmp37_seq = voice->mod_level_3_cur;
         
         // -- mod="2" dstVar=voice->tmp37_seq
         voice->tmp38_seq = 2.0f;
         voice->tmp37_seq *= voice->tmp38_seq;
         
         // -- mod="pow" dstVar=voice->tmp37_seq
         voice->tmp37_seq = voice->tmp37_seq * voice->tmp37_seq;
         voice->tmp22_phase *= voice->tmp37_seq;
         
         // -- mod="$v_phrand_2" dstVar=voice->tmp22_phase
         voice->tmp39_seq = voice->var_v_phrand_2;
         voice->tmp22_phase += voice->tmp39_seq;
         voice->tmp21_tri_tmp = ((voice->tmp18_tri_phase + voice->tmp22_phase));
         voice->tmp21_tri_tmp = ffrac_s(voice->tmp21_tri_tmp);
         voice->tmp17_phase = (voice->tmp21_tri_tmp < 0.5f) ? (-1.0 + voice->tmp21_tri_tmp * 4.0f) : (1.0 - (voice->tmp21_tri_tmp - 0.5f)*4);
         voice->tmp18_tri_phase = ffrac_s(voice->tmp18_tri_phase + voice->tmp19_tri_speed);
         
         // -- mod="svf" dstVar=voice->tmp17_phase
         
         // ---- mod="svf" input "freq" seq 1/1
         
         // -- mod="$v_color_2" dstVar=voice->tmp43_freq
         voice->tmp43_freq = voice->var_v_color_2;
         voice->tmp40_svf_lp = voice->tmp40_svf_lp + (voice->tmp42_svf_bp * voice->tmp43_freq);
         voice->tmp41_svf_hp = voice->tmp17_phase - voice->tmp40_svf_lp - (voice->tmp42_svf_bp * 1.0f);
         voice->tmp42_svf_bp = voice->tmp42_svf_bp + (voice->tmp41_svf_hp * voice->tmp43_freq);
         voice->tmp17_phase = voice->tmp40_svf_lp;
         
         // -- mod="svf" dstVar=voice->tmp17_phase
         
         // ---- mod="svf" input "freq" seq 1/1
         
         // -- mod="$v_color_2" dstVar=voice->tmp47_freq
         voice->tmp47_freq = voice->var_v_color_2;
         voice->tmp44_svf_lp = voice->tmp44_svf_lp + (voice->tmp46_svf_bp * voice->tmp47_freq);
         voice->tmp45_svf_hp = voice->tmp17_phase - voice->tmp44_svf_lp - (voice->tmp46_svf_bp * 1.0f);
         voice->tmp46_svf_bp = voice->tmp46_svf_bp + (voice->tmp45_svf_hp * voice->tmp47_freq);
         voice->tmp17_phase = voice->tmp44_svf_lp;
         
         // -- mod="$m_level_2" dstVar=voice->tmp17_phase
         voice->tmp48_seq = voice->mod_level_2_cur;
         
         // -- mod="2" dstVar=voice->tmp48_seq
         voice->tmp49_seq = 2.0f;
         voice->tmp48_seq *= voice->tmp49_seq;
         
         // -- mod="pow" dstVar=voice->tmp48_seq
         voice->tmp48_seq = voice->tmp48_seq * voice->tmp48_seq;
         voice->tmp17_phase *= voice->tmp48_seq;
         
         // -- mod="$v_phrand_1" dstVar=voice->tmp17_phase
         voice->tmp50_seq = voice->var_v_phrand_1;
         voice->tmp17_phase += voice->tmp50_seq;
         
         // -- mod="$m_phase_23" dstVar=voice->tmp17_phase
         voice->tmp51_seq = voice->mod_phase_23_cur;
         voice->tmp17_phase += voice->tmp51_seq;
         voice->tmp16_tri_tmp = ((voice->tmp13_tri_phase + voice->tmp17_phase));
         voice->tmp16_tri_tmp = ffrac_s(voice->tmp16_tri_tmp);
         out = (voice->tmp16_tri_tmp < 0.5f) ? (-1.0 + voice->tmp16_tri_tmp * 4.0f) : (1.0 - (voice->tmp16_tri_tmp - 0.5f)*4);
         voice->tmp13_tri_phase = ffrac_s(voice->tmp13_tri_phase + voice->tmp14_tri_speed);
         
         // -- mod="svf" dstVar=out
         
         // ---- mod="svf" input "freq" seq 1/1
         
         // -- mod="$v_color_1" dstVar=voice->tmp55_freq
         voice->tmp55_freq = voice->var_v_color_1;
         voice->tmp52_svf_lp = voice->tmp52_svf_lp + (voice->tmp54_svf_bp * voice->tmp55_freq);
         voice->tmp53_svf_hp = out - voice->tmp52_svf_lp - (voice->tmp54_svf_bp * 1.0f);
         voice->tmp54_svf_bp = voice->tmp54_svf_bp + (voice->tmp53_svf_hp * voice->tmp55_freq);
         out = voice->tmp52_svf_lp;
         
         // -- mod="svf" dstVar=out
         
         // ---- mod="svf" input "freq" seq 1/1
         
         // -- mod="$v_color_1" dstVar=voice->tmp59_freq
         voice->tmp59_freq = voice->var_v_color_1;
         voice->tmp56_svf_lp = voice->tmp56_svf_lp + (voice->tmp58_svf_bp * voice->tmp59_freq);
         voice->tmp57_svf_hp = out - voice->tmp56_svf_lp - (voice->tmp58_svf_bp * 1.0f);
         voice->tmp58_svf_bp = voice->tmp58_svf_bp + (voice->tmp57_svf_hp * voice->tmp59_freq);
         out = voice->tmp56_svf_lp;
   
         /* end calc */

#ifdef OVERSAMPLE_FACTOR
         outOS += out;
#endif // OVERSAMPLE_FACTOR
      }
#ifdef OVERSAMPLE_FACTOR
      // Apply lowpass filter before downsampling
      //   (note) normalized Fc = F/Fs = 0.442947 / sqrt(oversample_factorČ - 1)
      out = outOS * (1.0f / OVERSAMPLE_FACTOR);
#endif // OVERSAMPLE_FACTOR
      _samplesOut[k]      = out;
      _samplesOut[k + 1u] = out;

      // Next frame
      k += 2u;
      voice->note_speed_cur += voice->note_speed_inc;
      voice->mod_color_1_cur       += voice->mod_color_1_inc;
      voice->mod_color_2_cur       += voice->mod_color_2_inc;
      voice->mod_color_3_cur       += voice->mod_color_3_inc;
      voice->mod_phase_23_cur      += voice->mod_phase_23_inc;
      voice->mod_ratio_2_cur       += voice->mod_ratio_2_inc;
      voice->mod_ratio_3_cur       += voice->mod_ratio_3_inc;
      voice->mod_level_2_cur       += voice->mod_level_2_inc;
      voice->mod_level_3_cur       += voice->mod_level_3_inc;
   } /* loop numFrames */
}

static st_plugin_shared_t *ST_PLUGIN_API loc_shared_new(st_plugin_info_t *_info) {
   osc_fm3s_ftri_shared_t *ret = (osc_fm3s_ftri_shared_t *)malloc(sizeof(osc_fm3s_ftri_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) {
   osc_fm3s_ftri_voice_t *voice = (osc_fm3s_ftri_voice_t *)malloc(sizeof(osc_fm3s_ftri_voice_t));
   if(NULL != voice)
   {
      memset((void*)voice, 0, sizeof(*voice));
      voice->base.info = _info;
      voice->tmp2_lfsr_state = 17545;
      voice->tmp6_lfsr_state = 56505;
      voice->tmp10_lfsr_state = 61706;
   }
   return &voice->base;
}

static void ST_PLUGIN_API loc_voice_delete(st_plugin_voice_t *_voice) {
   ST_PLUGIN_VOICE_CAST(osc_fm3s_ftri_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°..90° (rev qtbl)
   int j = 4096;
   k = 0;
   loop(4096)
      cycle_sine_tbl_f[k++] = qcos[--j];

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

   // 180°..360° (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

extern "C" {
st_plugin_info_t *osc_fm3s_ftri_init(void) {
   osc_fm3s_ftri_info_t *ret = (osc_fm3s_ftri_info_t *)malloc(sizeof(osc_fm3s_ftri_info_t));

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

      ret->base.api_version = ST_PLUGIN_API_VERSION;
      ret->base.id          = "osc_fm3s_ftri";  // unique id. don't change this in future builds.
      ret->base.author      = "bsp";
      ret->base.name        = "fm3s osc ftri";
      ret->base.short_name  = "fm3s osc ftri";
      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;
      ret->base.get_param_name   = &loc_get_param_name;
      ret->base.get_param_reset  = &loc_get_param_reset;
      ret->base.get_param_value  = &loc_get_param_value;
      ret->base.set_param_value  = &loc_set_param_value;
      ret->base.get_mod_name     = &loc_get_mod_name;
      ret->base.set_sample_rate  = &loc_set_sample_rate;
      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 osc_fm3s_ftri_init();
   }
   return NULL;
}
#endif // STFX_SKIP_MAIN_INIT

} // extern "C"
