~ubuntu-branches/ubuntu/saucy/pd-smlib/saucy

« back to all changes in this revision

Viewing changes to vvplus.c

  • Committer: Bazaar Package Importer
  • Author(s): Hans-Christoph Steiner
  • Date: 2010-11-10 15:17:58 UTC
  • Revision ID: james.westby@ubuntu.com-20101110151758-3acjf69kiudo3gh4
Tags: upstream-0.12.1
ImportĀ upstreamĀ versionĀ 0.12.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "defines.h"
 
2
 
 
3
/*--------------- vvplus ---------------*/
 
4
/* vector addition
 
5
*/
 
6
 
 
7
static t_class *vvplus_class;
 
8
static t_class *vvplus_scal_class;
 
9
 
 
10
typedef struct _vvplus
 
11
{
 
12
  t_object x_obj;
 
13
 
 
14
  t_int n1, n2;
 
15
 
 
16
  t_float *buf1, *buf2;
 
17
 
 
18
  t_float f;
 
19
} t_vvplus;
 
20
 
 
21
 
 
22
static void vvplus_lst2(t_vvplus *x, t_symbol *s, int argc, t_atom *argv)
 
23
{
 
24
  t_float *fp;
 
25
  if (x->n2 != argc) {
 
26
    freebytes(x->buf2, x->n2 * sizeof(t_float));
 
27
    x->n2 = argc;
 
28
    x->buf2=(t_float *)getbytes(sizeof(t_float)*x->n2);
 
29
  };
 
30
  fp = x->buf2;
 
31
  while(argc--)*fp++=atom_getfloat(argv++);
 
32
}
 
33
 
 
34
static void vvplus_lst(t_vvplus *x, t_symbol *s, int argc, t_atom *argv)
 
35
{
 
36
  t_float *fp;
 
37
  t_atom  *ap;
 
38
  int n;
 
39
 
 
40
  if (argc){
 
41
    if (x->n1 != argc) {
 
42
      freebytes(x->buf1, x->n1 * sizeof(t_float));
 
43
      x->n1 = argc;
 
44
      x->buf1=(t_float *)getbytes(sizeof(t_float)*x->n1);
 
45
    };
 
46
    fp = x->buf1;
 
47
    while(argc--)*fp++=atom_getfloat(argv++);
 
48
  }
 
49
 
 
50
  if (x->n1*x->n2==1){
 
51
    outlet_float(x->x_obj.ob_outlet, *x->buf1+*x->buf2);
 
52
    return;
 
53
  }
 
54
  if (x->n1==1){
 
55
    t_atom *a;
 
56
    int i = x->n2;
 
57
    t_float f = *x->buf1;
 
58
    fp = x->buf2;
 
59
    n = x->n2;
 
60
    ap = (t_atom *)getbytes(sizeof(t_atom)*n);
 
61
    a = ap;
 
62
    while(i--){
 
63
      SETFLOAT(a, *fp+++f);
 
64
      a++;
 
65
    }
 
66
  } else if (x->n2==1){
 
67
    t_float f = *x->buf2;
 
68
    t_atom *a;
 
69
    int i = x->n1;
 
70
    n = x->n1;
 
71
    ap = (t_atom *)getbytes(sizeof(t_atom)*n);
 
72
    a = ap;
 
73
    fp = x->buf1;
 
74
    while(i--){
 
75
      SETFLOAT(a, *fp+++f);
 
76
      a++;
 
77
    }
 
78
  } else {
 
79
    t_atom *a;
 
80
    int i;
 
81
    t_float *fp2=x->buf2;
 
82
    fp = x->buf1;
 
83
    n = x->n1;
 
84
    if (x->n1!=x->n2){
 
85
      post("scalar multiplication: truncating vectors to the same length");
 
86
      if (x->n2<x->n1)n=x->n2;
 
87
    }
 
88
    ap = (t_atom *)getbytes(sizeof(t_atom)*n);
 
89
    a = ap;
 
90
    i=n;
 
91
    while(i--){
 
92
      SETFLOAT(a, *fp+++*fp2++);
 
93
      a++;
 
94
    }
 
95
  }
 
96
  outlet_list(x->x_obj.ob_outlet, gensym("list"), n, ap);
 
97
  freebytes(ap, sizeof(t_atom)*n);
 
98
}
 
99
static void vvplus_free(t_vvplus *x)
 
100
{
 
101
  freebytes(x->buf1, sizeof(t_float)*x->n1);
 
102
  freebytes(x->buf2, sizeof(t_float)*x->n2);
 
103
}
 
104
 
 
105
static void *vvplus_new(t_symbol *s, int argc, t_atom *argv)
 
106
{
 
107
  t_vvplus *x;
 
108
 
 
109
  if (argc-1){
 
110
    x = (t_vvplus *)pd_new(vvplus_class);
 
111
    inlet_new(&x->x_obj, &x->x_obj.ob_pd, gensym("list"), gensym(""));
 
112
  } else x = (t_vvplus *)pd_new(vvplus_scal_class);
 
113
 
 
114
  outlet_new(&x->x_obj, 0);
 
115
 
 
116
  x->n1   =1;
 
117
  x->buf1 =(t_float*)getbytes(sizeof(t_float));
 
118
  *x->buf1=0;
 
119
 
 
120
  if (argc)vvplus_lst2(x, gensym("list"), argc, argv);
 
121
  else {
 
122
    x->n2   =1;
 
123
    x->buf2 =(t_float*)getbytes(sizeof(t_float));
 
124
    *x->buf2=0;
 
125
  }
 
126
 
 
127
  if (argc==1)floatinlet_new(&x->x_obj, x->buf2);
 
128
 
 
129
  return (x);
 
130
}
 
131
 
 
132
void vvplus_setup(void)
 
133
{
 
134
  vvplus_class = class_new(gensym("vvplus"), (t_newmethod)vvplus_new, 
 
135
                            (t_method)vvplus_free, sizeof(t_vvplus), 0, A_GIMME, 0);
 
136
  class_addcreator((t_newmethod)vvplus_new, gensym("vv+"), A_GIMME, 0);
 
137
  class_addlist(vvplus_class, vvplus_lst);
 
138
  class_addmethod  (vvplus_class, (t_method)vvplus_lst2, gensym(""), A_GIMME, 0);
 
139
  vvplus_scal_class = class_new(gensym("vv+"), 0, (t_method)vvplus_free, 
 
140
                                 sizeof(t_vvplus), 0, 0);
 
141
  class_addlist(vvplus_scal_class, vvplus_lst);
 
142
}