/*----------------------------------------------------------------------------- * Set the frequency of triangular oscillator * The frequency is converted in a slope value. * The initial value is set according to frac_phase which is a position * in the period relative to the beginning of the period. * For example: 0 is the beginning of the period, 1/4 is at 1/4 of the period * relative to the beginning. * -----------------------------------------------------------------------------*/ void set_triangle_frequency(triang_modulator mod, float freq, float sample_rate, float frac_phase) { float ns_period; /* period in numbers of sample */ if (freq <= 0f) { freq = 0.5f; } mod.freq = freq; ns_period = sample_rate / freq; /* the slope of a triangular osc (0 up to +1 down to -1 up to 0....) is equivalent * to the slope of a saw osc (0 . +4) */ mod.inc = 4f / ns_period; /* positive slope */ /* The initial value and the sign of the slope depend of initial phase: * initial value = = (ns_period * frac_phase) * slope */ mod.val = ns_period * frac_phase * mod.inc; if (1f <= mod.val && mod.val < 3f) { mod.val = 2f - mod.val; /* 1.0 down to -1.0 */ mod.inc = -mod.inc; /* negative slope */ } else if (3f <= mod.val) { mod.val = mod.val - 4f; /* -1.0 up to +1.0. */ } /* else val < 1.0 */ }
/*----------------------------------------------------------------------------- * Get current value of triangular oscillator * y(n) = y(n-1) + dy * -----------------------------------------------------------------------------*/ float get_mod_triang(triang_modulator mod) { mod.val = mod.val + mod.inc; if (mod.val >= 1f) { mod.inc = -mod.inc; return(1f); } if (mod.val <= -1f) { mod.inc = -mod.inc; return(-1f); } return(mod.val); }
public modulator() { sinus = new sinus_modulator(); triang = new triang_modulator(); }