Guest User

C sin wave equation in PD

a guest
Sep 9th, 2011
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.06 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.     t_float amp;    /*amplitude*/
  17.     t_float freq;   /*frequency*/
  18.     t_sample f_sineq;
  19.     t_float f;
  20.     t_float phase;  /*phase in radians*/
  21.     t_float vertoff;    /*vertical offset*/
  22.  
  23. } t_sineq_tilde;
  24.  
  25. void *sineq_tilde_new(t_floatarg f)
  26. {
  27.     t_sineq_tilde *x =(t_sineq_tilde *)pd_new(sineq_tilde_class);
  28.     x->f_sineq = f;
  29.    
  30.     inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, &s_float);
  31.     floatinlet_new(&x->x_obj, &x->amp);
  32.     floatinlet_new(&x->x_obj, &x->freq);
  33.     floatinlet_new(&x->x_obj, &x->phase);
  34.     floatinlet_new(&x->x_obj, &x->vertoff);
  35.    
  36.     x->amp = 0;
  37.     x->freq = 0;
  38.     x->phase = 0;
  39.     x->vertoff = 0;
  40.    
  41.     outlet_new(&x->x_obj, gensym("signal"));
  42.     return (void *)x;
  43.  
  44. }
  45.  
  46. t_int *sineq_tilde_perform(t_int *w)
  47. {
  48.     t_sineq_tilde *x    = (t_sineq_tilde *)(w[1]);
  49.     t_float         *in1 =      (t_float *)(w[2]); 
  50.     t_float         *in2 =      (t_float *)(w[3]);
  51.     t_float         *in3 =      (t_float *)(w[4]); 
  52.     t_float         *in4 =      (t_float *)(w[5]); 
  53.     t_sample        *out =      (t_sample *)(w[6]);
  54.     int                 n =     (int)       (w[7]);
  55.    
  56.     /*not sure how to implement the t value here*/
  57.     /*t_sample f_sineq =     y=amp*sin(freq*time+phase)+vertoff */
  58.     while (n--)
  59.     {  
  60.         float amp=w[2];
  61.         float freq=w[3];
  62.         float phase=w[4];
  63.         float vertoff=w[5];
  64.         *out++ = amp*sin(2*PI*freq+(deg2rad(phase)))+vertoff;
  65.     }
  66.     return (w+8);
  67. }
  68.    
  69. void sineq_tilde_dsp(t_sineq_tilde *x, t_signal **sp)
  70. {
  71.     dsp_add(sineq_tilde_perform, 7, x,
  72.         sp[0]->s_vec, sp[1]->s_vec, sp[2]->s_vec, sp[3]->s_vec,
  73.         sp[4]->s_vec, sp[0]->s_n);
  74. }
  75.  
  76. void sineq_tilde_setup(void)
  77. {
  78.     sineq_tilde_class = class_new(gensym("sineq~"),
  79.         (t_newmethod)sineq_tilde_new, 0,
  80.         sizeof(t_sineq_tilde), 0,
  81.         A_DEFFLOAT, 0);
  82.     class_addmethod(sineq_tilde_class,
  83.         (t_method)sineq_tilde_dsp, gensym("dsp"), 0);
  84.     CLASS_MAINSIGNALIN(sineq_tilde_class,t_sineq_tilde, f);
  85. }
Advertisement
Add Comment
Please, Sign In to add comment