aboutsummaryrefslogtreecommitdiff
path: root/src/spec2_tabreceive~.c
blob: 0314886544252ca737b906fde0490c1e3091a29c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/* For information on usage and redistribution, and for a DISCLAIMER OF ALL
* WARRANTIES, see the file, "LICENSE.txt," in this distribution.

iem_spec2 written by Thomas Musil (c) IEM KUG Graz Austria 2002 - 2006 */

#include "m_pd.h"
#include "iemlib.h"

/* ------------------------ spec2_tabreceive_tilde~ ------------------------- */

static t_class *spec2_tabreceive_tilde_class;

typedef struct _spec2_tabreceive_tilde
{
  t_object  x_obj;
  t_float   *x_vec;
  t_symbol  *x_arrayname;
} t_spec2_tabreceive_tilde;

static void spec2_tabreceive_tilde_symbol(t_spec2_tabreceive_tilde *x, t_symbol *s)
{
  x->x_arrayname = s;
}

static t_int *spec2_tabreceive_tilde_perform(t_int *w)
{
  t_spec2_tabreceive_tilde *x = (t_spec2_tabreceive_tilde *)(w[1]);
  t_float *out = (t_float *)(w[2]);
  int n = w[3]+1;
  t_float *vec = x->x_vec;
  
  if(vec)
    while(n--)
      *out++ = *vec++;
    else
      while(n--)
        *out++ = 0.0f;
      return(w+4);
}

static t_int *spec2_tabreceive_tilde_perf16(t_int *w)
{
  t_spec2_tabreceive_tilde *x = (t_spec2_tabreceive_tilde *)(w[1]);
  t_float *out = (t_float *)(w[2]);
  int n = w[3];
  t_float *vec = x->x_vec;
  
  if(vec)
  {
    while(n)
    {
      out[0] = vec[0];
      out[1] = vec[1];
      out[2] = vec[2];
      out[3] = vec[3];
      out[4] = vec[4];
      out[5] = vec[5];
      out[6] = vec[6];
      out[7] = vec[7];
      out[8] = vec[8];
      out[9] = vec[9];
      out[10] = vec[10];
      out[11] = vec[11];
      out[12] = vec[12];
      out[13] = vec[13];
      out[14] = vec[14];
      out[15] = vec[15];
      
      vec += 16;
      out += 16;
      n -= 16;
    }
    out[0] = vec[0];
  }
  else
  {
    while(n)
    {
      out[0] = 0.0f;
      out[1] = 0.0f;
      out[2] = 0.0f;
      out[3] = 0.0f;
      out[4] = 0.0f;
      out[5] = 0.0f;
      out[6] = 0.0f;
      out[7] = 0.0f;
      out[8] = 0.0f;
      out[9] = 0.0f;
      out[10] = 0.0f;
      out[11] = 0.0f;
      out[12] = 0.0f;
      out[13] = 0.0f;
      out[14] = 0.0f;
      out[15] = 0.0f;
      
      out += 16;
      n -= 16;
    }
    out[0] = 0.0f;
  }
  
  return(w+4);
}

static void spec2_tabreceive_tilde_dsp(t_spec2_tabreceive_tilde *x, t_signal **sp)
{
  t_garray *a;
  int vecsize;
  
  if(!(a = (t_garray *)pd_findbyclass(x->x_arrayname, garray_class)))
  {
    if(*x->x_arrayname->s_name)
      error("spec2_tabreceive~: %s: no such array", x->x_arrayname->s_name);
  }
  else if(!garray_getfloatarray(a, &vecsize, &x->x_vec))
    error("%s: bad template for spec2_tabreceive~", x->x_arrayname->s_name);
  else 
  {
    int n = sp[0]->s_n;
    
    if(n < vecsize)
      vecsize = n;
    vecsize /= 2;
    if(vecsize&15)
      dsp_add(spec2_tabreceive_tilde_perform, 3, x, sp[0]->s_vec, vecsize);
    else
      dsp_add(spec2_tabreceive_tilde_perf16, 3, x, sp[0]->s_vec, vecsize);
  }
}

static void *spec2_tabreceive_tilde_new(t_symbol *s)
{
  t_spec2_tabreceive_tilde *x = (t_spec2_tabreceive_tilde *)pd_new(spec2_tabreceive_tilde_class);
  
  x->x_arrayname = s;
  outlet_new(&x->x_obj, &s_signal);
  return (x);
}

void spec2_tabreceive_tilde_setup(void)
{
  spec2_tabreceive_tilde_class = class_new(gensym("spec2_tabreceive~"), (t_newmethod)spec2_tabreceive_tilde_new,
    0, sizeof(t_spec2_tabreceive_tilde), 0, A_DEFSYM, 0);
  class_addmethod(spec2_tabreceive_tilde_class, (t_method)spec2_tabreceive_tilde_dsp, gensym("dsp"), 0);
  class_addsymbol(spec2_tabreceive_tilde_class, (t_method)spec2_tabreceive_tilde_symbol);
  class_sethelpsymbol(spec2_tabreceive_tilde_class, gensym("iemhelp/spec2_tabreceive~-help"));
}