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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
|
/* For information on usage and redistribution, and for a DISCLAIMER OF ALL
* WARRANTIES, see the file, "LICENSE.txt," in this distribution.
iem_ambi written by Thomas Musil, Copyright (c) IEM KUG Graz Austria 2000 - 2006 */
#include "m_pd.h"
#include "iemlib.h"
#include <math.h>
/* -------------------------- ambi_encode ------------------------------ */
typedef struct _ambi_encode
{
t_object x_obj;
t_atom *x_at;
int x_size;
int x_size2d;
int x_size3d;
t_float x_sqrt3;
t_float x_sqrt10_4;
t_float x_sqrt15;
t_float x_sqrt6_4;
t_float x_sqrt35_2;
t_float x_sqrt70_4;
t_float x_sqrt5_2;
t_float x_sqrt126_16;
t_float x_sqrt315_2;
t_float x_sqrt105_2;
t_float x_pi_over_180;
t_float *x_ambi_order_weight;
int x_colrow;
int x_n_order;
} t_ambi_encode;
static t_class *ambi_encode_class;
static void ambi_encode_ambi_weight(t_ambi_encode *x, t_symbol *s, int argc, t_atom *argv)
{
if(argc > x->x_n_order)
{
int i, n=x->x_n_order;
for(i=0; i<=n; i++)
{
x->x_ambi_order_weight[i] = atom_getfloat(argv++);
}
}
else
post("ambi_encode-ERROR: ambi_weight needs %d float weights", x->x_n_order+1);
}
static void ambi_encode_do_2d(t_ambi_encode *x, t_floatarg phi)
{
t_float c, s, cc, ss, c2, s2, c3, s3, s4, c4, s5, c5, s6, c6;
t_float *awght = x->x_ambi_order_weight;
t_atom *at=x->x_at;
phi *= x->x_pi_over_180;
c = cos(phi);
s = sin(phi);
cc = c*c;
ss = s*s;
SETFLOAT(at, (t_float)x->x_colrow);
at++;
SETFLOAT(at, awght[0]);
at++;
SETFLOAT(at, c*awght[1]);
at++;
SETFLOAT(at, s*awght[1]);
at++;
if(x->x_n_order >= 2)
{
c2 = cc - ss;
s2 = 2.0f*s*c;
SETFLOAT(at, c2*awght[2]);
at++;
SETFLOAT(at, s2*awght[2]);
at++;
if(x->x_n_order >= 3)
{
c3 = c*(4.0f*cc - 3.0f);
s3 = s*(3.0f - 4.0f*ss);
SETFLOAT(at, c3*awght[3]);
at++;
SETFLOAT(at, s3*awght[3]);
at++;
if(x->x_n_order >= 4)
{
c4 = 1.0f + 8.0f*cc*(cc - 1.0f);
s4 = 2.0f*s2*c2;
SETFLOAT(at, c4*awght[4]);
at++;
SETFLOAT(at, s4*awght[4]);
at++;
if(x->x_n_order >= 5)
{
c5 = c*(1.0f + 4.0f*ss*(ss - 3.0f*cc));
s5 = s*(1.0f + 4.0f*cc*(cc - 3.0f*ss));
SETFLOAT(at, c5*awght[5]);
at++;
SETFLOAT(at, s5*awght[5]);
at++;
if(x->x_n_order >= 6)
{
c6 = c3*c3 - s3*s3;
s6 = 2.0f*s3*c3;
SETFLOAT(at, c6*awght[6]);
at++;
SETFLOAT(at, s6*awght[6]);
at++;
if(x->x_n_order >= 7)
{
SETFLOAT(at, cos(7.0f*phi)*awght[7]);
at++;
SETFLOAT(at, sin(7.0f*phi)*awght[7]);
at++;
if(x->x_n_order >= 8)
{
SETFLOAT(at, (c4*c4 - s4*s4)*awght[8]);
at++;
SETFLOAT(at, 2.0f*s4*c4*awght[8]);
at++;
if(x->x_n_order >= 9)
{
SETFLOAT(at, cos(9.0f*phi)*awght[9]);
at++;
SETFLOAT(at, sin(9.0f*phi)*awght[9]);
at++;
if(x->x_n_order >= 10)
{
SETFLOAT(at, (c5*c5 - s5*s5)*awght[10]);
at++;
SETFLOAT(at, 2.0f*s5*c5*awght[10]);
at++;
if(x->x_n_order >= 11)
{
SETFLOAT(at, cos(11.0f*phi)*awght[11]);
at++;
SETFLOAT(at, sin(11.0f*phi)*awght[11]);
at++;
if(x->x_n_order >= 12)
{
SETFLOAT(at, (c6*c6 - s6*s6)*awght[12]);
at++;
SETFLOAT(at, 2.0f*s6*c6*awght[12]);
}
if(x->x_n_order >= 13)
post("ambi_encode-ERROR: do not support Ambisonic-Order greater than 12 in 2d !!!");
}
}
}
}
}
}
}
}
}
}
}
static void ambi_encode_do_3d(t_ambi_encode *x, t_symbol *s, int argc, t_atom *argv)
{
t_float delta, phi;
t_float cd, x1, y, z, x2, y2, z2, x2my2, x2m3y2, p3x2my2, xy, xz, yz, m1p5z2, m1p7z2, m3p7z2;
t_float *awght = x->x_ambi_order_weight;
t_atom *at=x->x_at;
delta = atom_getfloat(argv++)*x->x_pi_over_180;
phi = atom_getfloat(argv)*x->x_pi_over_180;
cd = cos(delta);
x1 = cd * cos(phi);
y = cd * sin(phi);
z = sin(delta);
xy = x1*y;
xz = x1*z;
yz = y*z;
x2 = x1*x1;
y2 = y*y;
z2 = z*z;
x2my2 = x2 - y2;
x2m3y2 = x2my2 - 2.0f*y2;
p3x2my2 = 2.0f*x2 + x2my2;
m1p5z2 = 5.0f*z2 - 1.0f;
m1p7z2 = 2.0f*z2 + m1p5z2;
m3p7z2 = m1p7z2 - 2.0f;
SETFLOAT(at, (t_float)x->x_colrow);
at++;
SETFLOAT(at, awght[0]);
at++;
SETFLOAT(at, x1*awght[1]);
at++;
SETFLOAT(at, y*awght[1]);
at++;
SETFLOAT(at, z*awght[1]);
at++;
if(x->x_n_order >= 2)
{
SETFLOAT(at, 0.5f*x->x_sqrt3*x2my2*awght[2]);
at++;
SETFLOAT(at, x->x_sqrt3*xy*awght[2]);
at++;
SETFLOAT(at, x->x_sqrt3*xz*awght[2]);
at++;
SETFLOAT(at, x->x_sqrt3*yz*awght[2]);
at++;
SETFLOAT(at, 0.5f*(3.0f*z2 - 1.0f)*awght[2]);
at++;
if(x->x_n_order >= 3)
{
SETFLOAT(at, x->x_sqrt10_4*x1*x2m3y2*awght[3]);
at++;
SETFLOAT(at, x->x_sqrt10_4*y*p3x2my2*awght[3]);
at++;
SETFLOAT(at, 0.5f*x->x_sqrt15*z*x2my2*awght[3]);
at++;
SETFLOAT(at, x->x_sqrt15*xy*z*awght[3]);
at++;
SETFLOAT(at, x->x_sqrt6_4*x1*m1p5z2*awght[3]);
at++;
SETFLOAT(at, x->x_sqrt6_4*y*m1p5z2*awght[3]);
at++;
SETFLOAT(at, 0.5f*z*(m1p5z2 - 2.0f)*awght[3]);
at++;
if(x->x_n_order >= 4)
{
SETFLOAT(at, 0.25f*x->x_sqrt35_2*(x2my2*x2my2 - 4.0f*x2*y2)*awght[4]);
at++;
SETFLOAT(at, x->x_sqrt35_2*xy*x2my2*awght[4]);
at++;
SETFLOAT(at, x->x_sqrt70_4*xz*x2m3y2*awght[4]);
at++;
SETFLOAT(at, x->x_sqrt70_4*yz*p3x2my2*awght[4]);
at++;
SETFLOAT(at, 0.5f*x->x_sqrt5_2*x2my2*m1p7z2*awght[4]);
at++;
SETFLOAT(at, x->x_sqrt5_2*xy*m1p7z2*awght[4]);
at++;
SETFLOAT(at, x->x_sqrt10_4*xz*m3p7z2*awght[4]);
at++;
SETFLOAT(at, x->x_sqrt10_4*yz*m3p7z2*awght[4]);
at++;
SETFLOAT(at, 0.125f*(5.0f*(z2 - 1.0f)*(m1p7z2 + 2.0f) + 8.0f)*awght[4]);
at++;
if(x->x_n_order >= 5)
{
SETFLOAT(at, x->x_sqrt126_16*x1*(x2*(x2 - 10.0f*y2) + 5.0f*y2*y2)*awght[5]);
at++;
SETFLOAT(at, x->x_sqrt126_16*y*(y2*(y2 - 10.0f*x2) + 5.0f*x2*x2)*awght[5]);
at++;
SETFLOAT(at, 0.25f*x->x_sqrt315_2*z*(y2*(y2 - 6.0f*x2) + x2*x2)*awght[5]);
at++;
SETFLOAT(at, x->x_sqrt315_2*xy*z*x2my2*awght[5]);
at++;
SETFLOAT(at, 0.25f*x->x_sqrt70_4*x1*(9.0f*z2 - 1.0f)*x2m3y2*awght[5]);
at++;
SETFLOAT(at, 0.25f*x->x_sqrt70_4*y*(9.0f*z2 - 1.0f)*p3x2my2*awght[5]);
at++;
SETFLOAT(at, 0.5f*x->x_sqrt105_2*x2my2*z*(3.0f*z2 - 1.0f)*awght[5]);
at++;
SETFLOAT(at, x->x_sqrt105_2*xy*z*(3.0f*z2 - 1.0f)*awght[5]);
at++;
SETFLOAT(at, 0.125f*x->x_sqrt15*x1*(z2*(21.0f*z2 - 14.0f) + 1.0f)*awght[5]);
at++;
SETFLOAT(at, 0.125f*x->x_sqrt15*y*(z2*(21.0f*z2 - 14.0f) + 1.0f)*awght[5]);
at++;
SETFLOAT(at, 0.125f*z*(z2*(63.0f*z2 - 70.0f) + 15.0f)*awght[5]);
}
if(x->x_n_order > 5)
post("ambi_encode-ERROR: do not support Ambisonic-Order greater than 5 in 3d !!!");
}
}
}
}
static void ambi_encode_float(t_ambi_encode *x, t_floatarg phi)
{
x->x_colrow = -1;
ambi_encode_do_2d(x, phi);
outlet_list(x->x_obj.ob_outlet, &s_list, x->x_size2d, x->x_at+1);
}
static void ambi_encode_list(t_ambi_encode *x, t_symbol *s, int argc, t_atom *argv)
{
if(argc <= 0)
{
post("ambi_encode ERROR: list-input needs 2 angles: delta [rad] and phi [rad]");
return;
}
else if(argc == 1)
{
ambi_encode_float(x, atom_getfloat(argv));
}
else
{
x->x_colrow = -1;
ambi_encode_do_3d(x, &s_list, 2, argv);
outlet_list(x->x_obj.ob_outlet, &s_list, x->x_size3d, x->x_at+1);
}
}
static void ambi_encode_row(t_ambi_encode *x, t_symbol *s, int argc, t_atom *argv)
{
if(argc == 2)
{
x->x_colrow = (int)atom_getint(argv++);
ambi_encode_do_2d(x, atom_getfloat(argv));
outlet_anything(x->x_obj.ob_outlet, s, x->x_size2d+1, x->x_at);
}
else if(argc >= 3)
{
x->x_colrow = (int)atom_getint(argv++);
ambi_encode_do_3d(x, &s_list, 2, argv);
outlet_anything(x->x_obj.ob_outlet, s, x->x_size3d+1, x->x_at);
}
else
{
post("ambi_encode-ERROR: row needs <float> row-index + <float> angle ( + <float> angle)");
}
}
static void ambi_encode_col(t_ambi_encode *x, t_symbol *s, int argc, t_atom *argv)
{
if(argc == 2)
{
x->x_colrow = (int)atom_getint(argv++);
ambi_encode_do_2d(x, atom_getfloat(argv));
outlet_anything(x->x_obj.ob_outlet, s, x->x_size2d+1, x->x_at);
}
else if(argc >= 3)
{
x->x_colrow = (int)atom_getint(argv++);
ambi_encode_do_3d(x, &s_list, 2, argv);
outlet_anything(x->x_obj.ob_outlet, s, x->x_size3d+1, x->x_at);
}
else
{
post("ambi_encode-ERROR: col needs <float> col-index + <float> angle ( + <float> angle)");
}
}
static void ambi_encode_free(t_ambi_encode *x)
{
freebytes(x->x_ambi_order_weight, (x->x_n_order+1) * sizeof(t_float));
freebytes(x->x_at, x->x_size * sizeof(t_atom));
}
static void *ambi_encode_new(t_floatarg forder)
{
t_ambi_encode *x = (t_ambi_encode *)pd_new(ambi_encode_class);
t_atom *at;
int i=(int)forder;
if(i < 1)
i = 1;
if(i > 12)
i = 12;
x->x_n_order = i;
x->x_size = 6*6 + 1;
x->x_size2d = 2*i + 1;
x->x_size3d = (i + 1)*(i + 1);
x->x_sqrt3 = (t_float)(sqrt(3.0));
x->x_sqrt5_2 = (t_float)(sqrt(5.0) / 2.0);
x->x_sqrt6_4 = (t_float)(sqrt(6.0) / 4.0);
x->x_sqrt10_4 = (t_float)(sqrt(10.0) / 4.0);
x->x_sqrt15 = (t_float)(sqrt(15.0));
x->x_sqrt35_2 = (t_float)(sqrt(35.0) / 2.0);
x->x_sqrt70_4 = (t_float)(sqrt(70.0) / 4.0);
x->x_sqrt126_16 = (t_float)(sqrt(126.0) / 16.0);
x->x_sqrt315_2 = (t_float)(sqrt(315.0) / 2.0);
x->x_sqrt105_2 = (t_float)(sqrt(105.0) / 2.0);
x->x_pi_over_180 = (t_float)(4.0 * atan(1.0)/180.0);
x->x_colrow = 0;
x->x_ambi_order_weight = (t_float *)getbytes((x->x_n_order+1) * sizeof(t_float));
x->x_at = (t_atom *)getbytes(x->x_size * sizeof(t_atom));
at=x->x_at;
SETFLOAT(at, -1.0f);/*row index*/
at++;
SETFLOAT(at, 1.0f);/*W channel*/
for(i=0; i<=x->x_n_order; i++)
x->x_ambi_order_weight[i] = 1.0f;
outlet_new(&x->x_obj, &s_list);
return (x);
}
void ambi_encode_setup(void)
{
ambi_encode_class = class_new(gensym("ambi_encode"), (t_newmethod)ambi_encode_new, (t_method)ambi_encode_free,
sizeof(t_ambi_encode), 0, A_DEFFLOAT, 0);
class_addlist(ambi_encode_class, (t_method)ambi_encode_list);
class_addfloat(ambi_encode_class, (t_method)ambi_encode_float);
class_addmethod(ambi_encode_class, (t_method)ambi_encode_row, gensym("row"), A_GIMME, 0);
class_addmethod(ambi_encode_class, (t_method)ambi_encode_col, gensym("col"), A_GIMME, 0);
class_addmethod(ambi_encode_class, (t_method)ambi_encode_ambi_weight, gensym("ambi_weight"), A_GIMME, 0);
// class_sethelpsymbol(ambi_encode_class, gensym("iemhelp2/ambi_encode-help"));
}
|