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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
|
/* For information on usage and redistribution, and for a DISCLAIMER OF ALL
* WARRANTIES, see the file, "LICENSE.txt," in this distribution.
iem_tab written by Thomas Musil, Copyright (c) IEM KUG Graz Austria 2000 - 2009 */
#include "m_pd.h"
#include "iemlib.h"
#include "iem_tab.h"
#include <math.h>
/* -------------------------- tab_carth2polar ------------------------------ */
/* x_beg_mem_dst_mag[i] = sqrt(x_beg_mem_src_re[i]*x_beg_mem_src_re[i] + x_beg_mem_src_im[i]*x_beg_mem_src_im[i]) */
/* x_beg_mem_dst_arg[i] = atan2(x_beg_mem_src_im[i], x_beg_mem_src_re[i]) */
typedef struct _tab_carth2polar
{
t_object x_obj;
int x_size_src_re;
int x_size_dst_mag;
int x_size_src_im;
int x_size_dst_arg;
int x_offset_src_re;
int x_offset_dst_mag;
int x_offset_src_im;
int x_offset_dst_arg;
iemarray_t *x_beg_mem_src_re;
iemarray_t *x_beg_mem_dst_mag;
iemarray_t *x_beg_mem_src_im;
iemarray_t *x_beg_mem_dst_arg;
t_symbol *x_sym_src_re;
t_symbol *x_sym_dst_mag;
t_symbol *x_sym_src_im;
t_symbol *x_sym_dst_arg;
} t_tab_carth2polar;
static t_class *tab_carth2polar_class;
static void tab_carth2polar_src_re(t_tab_carth2polar *x, t_symbol *s)
{
x->x_sym_src_re = s;
}
static void tab_carth2polar_src_im(t_tab_carth2polar *x, t_symbol *s)
{
x->x_sym_src_im = s;
}
static void tab_carth2polar_dst_mag(t_tab_carth2polar *x, t_symbol *s)
{
x->x_sym_dst_mag = s;
}
static void tab_carth2polar_dst_arg(t_tab_carth2polar *x, t_symbol *s)
{
x->x_sym_dst_arg = s;
}
static void tab_carth2polar_bang(t_tab_carth2polar *x)
{
int i, n;
int ok_src_re, ok_dst_mag;
int ok_src_im, ok_dst_arg;
iemarray_t *vec_src_re, *vec_dst_mag;
iemarray_t *vec_src_im, *vec_dst_arg;
ok_src_re = iem_tab_check_arrays(gensym("tab_carth2polar"), x->x_sym_src_re, &x->x_beg_mem_src_re, &x->x_size_src_re, 0);
ok_dst_mag = iem_tab_check_arrays(gensym("tab_carth2polar"), x->x_sym_dst_mag, &x->x_beg_mem_dst_mag, &x->x_size_dst_mag, 0);
ok_src_im = iem_tab_check_arrays(gensym("tab_carth2polar"), x->x_sym_src_im, &x->x_beg_mem_src_im, &x->x_size_src_im, 0);
ok_dst_arg = iem_tab_check_arrays(gensym("tab_carth2polar"), x->x_sym_dst_arg, &x->x_beg_mem_dst_arg, &x->x_size_dst_arg, 0);
if(ok_src_re && ok_dst_mag && ok_src_im && ok_dst_arg)
{
if(x->x_size_src_re < x->x_size_dst_mag)
n = x->x_size_src_re;
else
n = x->x_size_dst_mag;
if(x->x_size_src_im < n)
n = x->x_size_src_im;
if(x->x_size_dst_arg < n)
n = x->x_size_dst_arg;
vec_src_re = x->x_beg_mem_src_re;
vec_dst_mag = x->x_beg_mem_dst_mag;
vec_src_im = x->x_beg_mem_src_im;
vec_dst_arg = x->x_beg_mem_dst_arg;
if(n)
{
t_garray *a;
t_float rcp_two_pi=0.125f/atan(1.0);
for(i=0; i<n; i++)
{
t_float re, im, mag, arg;
re = iemarray_getfloat(vec_src_re, i);
im = iemarray_getfloat(vec_src_im, i);
mag = sqrt(re*re + im*im);
arg = atan2(im, re) * rcp_two_pi;
iemarray_setfloat(vec_dst_mag, i, mag);
iemarray_setfloat(vec_dst_arg, i, arg);
}
outlet_bang(x->x_obj.ob_outlet);
a = (t_garray *)pd_findbyclass(x->x_sym_dst_mag, garray_class);
garray_redraw(a);
a = (t_garray *)pd_findbyclass(x->x_sym_dst_arg, garray_class);
garray_redraw(a);
}
}
}
static void tab_carth2polar_list(t_tab_carth2polar *x, t_symbol *s, int argc, t_atom *argv)
{
int i, n;
int beg_src_re, beg_dst_mag;
int beg_src_im, beg_dst_arg;
int ok_src_re, ok_dst_mag;
int ok_src_im, ok_dst_arg;
iemarray_t *vec_src_re, *vec_dst_mag;
iemarray_t *vec_src_im, *vec_dst_arg;
if((argc >= 5) &&
IS_A_FLOAT(argv,0) &&
IS_A_FLOAT(argv,1) &&
IS_A_FLOAT(argv,2) &&
IS_A_FLOAT(argv,3) &&
IS_A_FLOAT(argv,4))
{
beg_src_re = (int)atom_getintarg(0, argc, argv);
beg_src_im = (int)atom_getintarg(1, argc, argv);
beg_dst_mag = (int)atom_getintarg(2, argc, argv);
beg_dst_arg = (int)atom_getintarg(3, argc, argv);
n = (int)atom_getintarg(4, argc, argv);
if(beg_src_re < 0)
beg_src_re = 0;
if(beg_dst_mag < 0)
beg_dst_mag = 0;
if(beg_src_im < 0)
beg_src_im = 0;
if(beg_dst_arg < 0)
beg_dst_arg = 0;
if(n < 0)
n = 0;
ok_src_re = iem_tab_check_arrays(gensym("tab_carth2polar"), x->x_sym_src_re, &x->x_beg_mem_src_re, &x->x_size_src_re, beg_src_re+n);
ok_dst_mag = iem_tab_check_arrays(gensym("tab_carth2polar"), x->x_sym_dst_mag, &x->x_beg_mem_dst_mag, &x->x_size_dst_mag, beg_dst_mag+n);
ok_src_im = iem_tab_check_arrays(gensym("tab_carth2polar"), x->x_sym_src_im, &x->x_beg_mem_src_im, &x->x_size_src_im, beg_src_im+n);
ok_dst_arg = iem_tab_check_arrays(gensym("tab_carth2polar"), x->x_sym_dst_arg, &x->x_beg_mem_dst_arg, &x->x_size_dst_arg, beg_dst_arg+n);
if(ok_src_re && ok_dst_mag && ok_src_im && ok_dst_arg)
{
vec_src_re = x->x_beg_mem_src_re + beg_src_re;
vec_dst_mag = x->x_beg_mem_dst_mag + beg_dst_mag;
vec_src_im = x->x_beg_mem_src_im + beg_src_im;
vec_dst_arg = x->x_beg_mem_dst_arg + beg_dst_arg;
if(n)
{
t_garray *a;
t_float rcp_two_pi=0.125f/atan(1.0);
for(i=0; i<n; i++)
{
t_float re, im, mag, arg;
re = iemarray_getfloat(vec_src_re, i);
im = iemarray_getfloat(vec_src_im, i);
mag = sqrt(re*re + im*im);
arg = atan2(im, re) * rcp_two_pi;
iemarray_setfloat(vec_dst_mag, i, mag);
iemarray_setfloat(vec_dst_arg, i, arg);
}
outlet_bang(x->x_obj.ob_outlet);
a = (t_garray *)pd_findbyclass(x->x_sym_dst_mag, garray_class);
garray_redraw(a);
a = (t_garray *)pd_findbyclass(x->x_sym_dst_arg, garray_class);
garray_redraw(a);
}
}
}
else
{
post("tab_carth2polar-ERROR: list need 5 float arguments:");
post(" source_real_offset + source_imag_offset + destination_magnitude_offset + destination_phase_argument_offset + number_of_samples_to_convert");
}
}
static void tab_carth2polar_free(t_tab_carth2polar *x)
{
}
static void *tab_carth2polar_new(t_symbol *s, int argc, t_atom *argv)
{
t_tab_carth2polar *x = (t_tab_carth2polar *)pd_new(tab_carth2polar_class);
t_symbol *src_re, *dst_mag, *src_im, *dst_arg;
if((argc >= 4) &&
IS_A_SYMBOL(argv,0) &&
IS_A_SYMBOL(argv,1) &&
IS_A_SYMBOL(argv,2) &&
IS_A_SYMBOL(argv,3))
{
src_re = (t_symbol *)atom_getsymbolarg(0, argc, argv);
src_im = (t_symbol *)atom_getsymbolarg(1, argc, argv);
dst_mag = (t_symbol *)atom_getsymbolarg(2, argc, argv);
dst_arg = (t_symbol *)atom_getsymbolarg(3, argc, argv);
}
else
{
post("tab_carth2polar-ERROR: need 4 symbols arguments:");
post(" source_real_array_name + source_imag_array_name + destination_magnitude_array_name + destination_phase_argument_array_name");
return(0);
}
x->x_sym_src_re = src_re;
x->x_sym_src_im = src_im;
x->x_sym_dst_mag = dst_mag;
x->x_sym_dst_arg = dst_arg;
outlet_new(&x->x_obj, &s_bang);
return(x);
}
void tab_carth2polar_setup(void)
{
tab_carth2polar_class = class_new(gensym("tab_carth2polar"), (t_newmethod)tab_carth2polar_new, (t_method)tab_carth2polar_free,
sizeof(t_tab_carth2polar), 0, A_GIMME, 0);
class_addbang(tab_carth2polar_class, (t_method)tab_carth2polar_bang);
class_addlist(tab_carth2polar_class, (t_method)tab_carth2polar_list);
class_addmethod(tab_carth2polar_class, (t_method)tab_carth2polar_src_re, gensym("src_re"), A_DEFSYMBOL, 0);
class_addmethod(tab_carth2polar_class, (t_method)tab_carth2polar_src_im, gensym("src_im"), A_DEFSYMBOL, 0);
class_addmethod(tab_carth2polar_class, (t_method)tab_carth2polar_src_re, gensym("src1_re"), A_DEFSYMBOL, 0);
class_addmethod(tab_carth2polar_class, (t_method)tab_carth2polar_src_im, gensym("src1_im"), A_DEFSYMBOL, 0);
class_addmethod(tab_carth2polar_class, (t_method)tab_carth2polar_dst_mag, gensym("dst_mag"), A_DEFSYMBOL, 0);
class_addmethod(tab_carth2polar_class, (t_method)tab_carth2polar_dst_arg, gensym("dst_arg"), A_DEFSYMBOL, 0);
// class_sethelpsymbol(tab_carth2polar_class, gensym("iemhelp2/help-tab_carth2polar"));
}
|