Advertisement
Guest User

C sin eq2

a guest
Sep 10th, 2011
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.00 KB | None | 0 0
  1. /*compiled on ubuntu 10.04 Linux*/
  2. #include "m_pd.h"
  3. #include "math.h"
  4.  
  5. #define PI 3.14159265358979323846
  6. #define deg2rad(deg) ((deg * PI) / 180.0)
  7. #define rad2deg(rad) ((180.0 * rad) / PI)
  8.  
  9.  
  10.  
  11. static t_class *sineq_tilde_class;
  12.  
  13. typedef struct _sineq_tilde
  14. {
  15.     t_object x_obj;
  16.     float amp;  /*amplitude*/
  17.     float freq; /*frequency*/
  18.     t_sample f_sineq;
  19.     float f;
  20.     float phase;    /*phase in radians*/
  21.     float vertoff;  /*vertical offset*/
  22.  
  23. } t_sineq_tilde;
  24.  
  25.  
  26. static void sineq_tilde_amp (t_sineq_tilde *x, t_floatarg f)
  27. {
  28.        if ((f >= 0) && (f <= 1))
  29.                x->amp = f;
  30.        else
  31.                x->amp = 0;
  32. }
  33.  
  34.  
  35. static void sineq_tilde_freq (t_sineq_tilde *x, t_floatarg f)
  36. {
  37.        if ((f > 0) && (f <= 20000))
  38.                x->freq = f;
  39.        else
  40.                x->freq = 0;
  41. }
  42.  
  43.  
  44.  
  45. static void sineq_tilde_phase (t_sineq_tilde *x, t_floatarg f)
  46. {
  47.        if ((f >= -2*PI) && (f <= 2*PI))
  48.                x->phase = f;
  49.        else
  50.                if (f < -2*PI) x->phase = -2*PI;
  51.                if (f > 2*PI) x->phase = 2*PI;
  52. }
  53.  
  54.  
  55. static void sineq_tilde_vertoff (t_sineq_tilde *x, t_floatarg f)
  56. {
  57.        if ((f >= -1) && (f <= 1))
  58.                x->vertoff = f;
  59.        else
  60.                if (f < -1) x->vertoff = -1;
  61.                if (f > 1) x->vertoff = 1;
  62. }
  63.  
  64.  
  65.  
  66.  
  67. t_int *sineq_tilde_perform(t_int *w)
  68. {
  69.     t_sineq_tilde *x        = (t_sineq_tilde *)(w[1]);
  70.     t_sample    *out = (t_sample *)(w[2]);
  71.     int         n = (int)           (w[3]);
  72.    
  73.        while (n--)
  74.        {
  75.             *out++ = x->amp*sin(2*PI*x->freq+(deg2rad(x->phase)))+x->vertoff;
  76.        }
  77.        return (w+4);
  78. }
  79.  
  80.  
  81.  
  82. void *sineq_tilde_new(t_floatarg f)
  83. {
  84.     t_sineq_tilde *x =(t_sineq_tilde *)pd_new(sineq_tilde_class);
  85.     x->f_sineq = f;
  86.    
  87.     inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, &s_float);
  88.     floatinlet_new(&x->x_obj, &x->amp);
  89.     floatinlet_new(&x->x_obj, &x->freq);
  90.     floatinlet_new(&x->x_obj, &x->phase);
  91.     floatinlet_new(&x->x_obj, &x->vertoff);
  92.    
  93.     x->amp = 0;
  94.     x->freq = 0;
  95.     x->phase = 0;
  96.     x->vertoff = 0;
  97.    
  98.     outlet_new(&x->x_obj, gensym("signal"));
  99.     return (void *)x;
  100.  
  101. }
  102.  
  103. void sineq_tilde_dsp(t_sineq_tilde *x, t_signal **sp)
  104. {
  105.     dsp_add(sineq_tilde_perform, 3, x,
  106.         sp[0]->s_vec, sp[0]->s_n);
  107. }
  108.  
  109. void sineq_tilde_setup(void)
  110. {
  111.        sineq_tilde_class = class_new(gensym("sineq~"),
  112.                (t_newmethod)sineq_tilde_new, 0,
  113.                sizeof(t_sineq_tilde), 0,
  114.                A_DEFFLOAT, 0);
  115.        class_addmethod(sineq_tilde_class,
  116.                (t_method)sineq_tilde_dsp, gensym("dsp"), 0);
  117.        CLASS_MAINSIGNALIN(sineq_tilde_class,t_sineq_tilde, f);
  118.        class_addmethod(sineq_tilde_class, (t_method)sineq_tilde_amp, gensym("amp"), A_DEFFLOAT, 0);
  119.        class_addmethod(sineq_tilde_class, (t_method)sineq_tilde_freq, gensym("freq"), A_DEFFLOAT, 0);
  120.        class_addmethod(sineq_tilde_class, (t_method)sineq_tilde_phase, gensym("phase"), A_DEFFLOAT, 0);
  121.        class_addmethod(sineq_tilde_class, (t_method)sineq_tilde_vertoff, gensym("vertoff"), A_DEFFLOAT, 0);
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement