/*----------------------------------------------------------------------------- * Sets the frequency of sinus oscillator. * * @param mod pointer on modulator structure. * @param freq frequency of the oscillator in Hz. * @param sample_rate sample rate on audio output in Hz. * @param phase initial phase of the oscillator in degree (0 to 360). * -----------------------------------------------------------------------------*/ void set_sinus_frequency(sinus_modulator mod, float freq, float sample_rate, float phase) { float w = 2f * Mathf.PI * freq / sample_rate; /* initial angle */ float a; mod.a1 = 2f * Mathf.Cos(w); a = (2f * Mathf.PI / 360f) * phase; mod.buffer2 = Mathf.Sin(a - w); /* y(n-1) = sin(-intial angle) */ mod.buffer1 = Mathf.Sin(a); /* y(n) = sin(initial phase) */ mod.reset_buffer2 = Mathf.Sin(Mathf.PI / 2f - w); /* reset value for PI/2 */ }
/*----------------------------------------------------------------------------- * Gets current value of sinus modulator: * y(n) = a1 . y(n-1) - y(n-2) * out = a1 . buffer1 - buffer2 * * @param pointer on modulator structure. * @return current value of the modulator sine wave. * -----------------------------------------------------------------------------*/ float get_mod_sinus(sinus_modulator mod) { float outp; outp = mod.a1 * mod.buffer1 - mod.buffer2; mod.buffer2 = mod.buffer1; if (outp >= 1.0f) /* reset in case of instability near PI/2 */ { outp = 1.0f; /* forces output to the right value */ mod.buffer2 = mod.reset_buffer2; } if (outp <= -1.0f) /* reset in case of instability near -PI/2 */ { outp = -1.0f; /* forces output to the right value */ mod.buffer2 = -mod.reset_buffer2; } mod.buffer1 = outp; return(outp); }
public modulator() { sinus = new sinus_modulator(); triang = new triang_modulator(); }