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
|
/*
* iemmatrix
*
* objects for manipulating simple matrices
* mostly refering to matlab/octave matrix functions
*
* (c) IOhannes m zmölnig, forum::für::umläute
*
* IEM, Graz
*
* this code is published under the LGPL
*
*/
#include "iemmatrix.h"
/*
the sigmatrix objects ::
mtx_*~ : multiply a n-vector of in~ with a matrix to get a m-vector of out~
line~ between the 2 matrices, to make it useable as a mixer
*/
/* --------------------------- matrix~ ----------------------------------
*
* multiply a n-vector of signals with a (n*m) matrix, to get m output-streams.
* make the (n*m)-matrix of scalars to be liny~
*
* 1703:forum::für::umläute:2001
*/
static t_class *mtx_multilde_class;
typedef struct _mtx_multilde {
t_object x_obj;
t_float time;
int ticksleft;
int retarget;
t_float msec2tick;
t_float**value;
t_float**target;
t_float**increment; /* single precision is really a bad, especially when doing long line~s.
* but the biginc (like in msp's line~ (d_ctl.c) is far too expensive... */
t_float**sigIN;
t_float**sigOUT;
t_float *sigBUF;
int n_sigIN; /* columns */
int n_sigOUT; /* rows */
int compatibility; /* 0=default; 1=zexy; 2=iemlib */
} t_mtx_multilde;
/* the message thing */
static void mtx_multilde_matrix_default(t_mtx_multilde *x, t_symbol *s, int argc, t_atom *argv)
{
int col, row, c=0, r=0;
if (argc<2){
post("matrix~ : bad matrix !");
return;
}
row = atom_getfloat(argv++);
col = atom_getfloat(argv++);
argc-=2;
if((col!=x->n_sigOUT)||(row!=x->n_sigIN)){
post("matrix~ : matrix dimensions do not match !!");
return;
}
if(argc<row*col){
post("matrix~ : reduced matrices not yet supported");
return;
}
if (x->time<=0) {
for(r=0; r<row; r++)
for(c=0; c<col; c++)
x->target[c][r]=x->value[c][r]=atom_getfloat(argv++);
x->time=x->ticksleft=x->retarget=0;
} else {
for(r=0; r<row; r++)
for(c=0; c<col; c++)
x->target[c][r]=atom_getfloat(argv++);
x->retarget=1;
}
}
static void mtx_multilde_matrix_zexy(t_mtx_multilde *x, t_symbol *s, int argc, t_atom *argv)
{
int col, row, c=0, r=0;
if (argc<2){
post("matrix~ : bad matrix !");
return;
}
row = atom_getfloat(argv++);
col = atom_getfloat(argv++);
argc-=2;
if((col!=x->n_sigOUT)||(row!=x->n_sigIN)){
post("matrix~ : matrix dimensions do not match !!");
return;
}
if(argc<row*col){
post("matrix~ : reduced matrices not yet supported");
return;
}
if (x->time<=0) {
for(r=0; r<row; r++)
for(c=0; c<col; c++)
x->target[c][r]=x->value[c][r]=atom_getfloat(argv++);
x->time=x->ticksleft=x->retarget=0;
} else {
for(r=0; r<row; r++)
for(c=0; c<col; c++)
x->target[c][r]=atom_getfloat(argv++);
x->retarget=1;
}
}
static void mtx_multilde_matrix_iemlib(t_mtx_multilde *x, t_symbol *s, int argc, t_atom *argv)
{
int col, row, c=0, r=0;
if (argc<2){
post("matrix~ : bad matrix !");
return;
}
row = atom_getfloat(argv++);
col = atom_getfloat(argv++);
argc-=2;
if((col!=x->n_sigOUT)||(row!=x->n_sigIN)){
post("matrix~ : matrix dimensions do not match !!");
return;
}
if(argc<row*col){
post("matrix~ : reduced matrices not yet supported");
return;
}
if (x->time<=0) {
for(r=0; r<row; r++)
for(c=0; c<col; c++)
x->target[c][r]=x->value[c][r]=atom_getfloat(argv++);
x->time=x->ticksleft=x->retarget=0;
} else {
for(r=0; r<row; r++)
for(c=0; c<col; c++)
x->target[c][r]=atom_getfloat(argv++);
x->retarget=1;
}
}
static void mtx_multilde_matrix(t_mtx_multilde *x, t_symbol *s, int argc, t_atom *argv){
switch (x->compatibility){
default:mtx_multilde_matrix_default(x,s,argc,argv);
break;
case 1:mtx_multilde_matrix_zexy(x,s,argc,argv);
break;
case 2:mtx_multilde_matrix_iemlib(x,s,argc,argv);
break;
}
}
static void mtx_multilde_stop(t_mtx_multilde *x)
{
int c = x->n_sigOUT, r;
t_float *tgt, *val;
while(c--){
tgt=x->target[c];
val=x->value [c];
r=x->n_sigIN;
while(r--)*tgt++=*val++;
}
x->ticksleft = x->retarget = 0;
}
/* the dsp thing */
static t_int *mtx_multilde_perform(t_int *w)
{
t_mtx_multilde *x = (t_mtx_multilde *)(w[1]);
int n = (int)(w[2]);
int r, c;
t_float **out = x->sigOUT;
t_float **in = x->sigIN;
t_float *buf = x->sigBUF, *sigBUF = buf;
t_float **value = x->value;
t_float **target = x->target;
t_float **increment = x->increment;
t_float *inc, *val, *tgt;
int n_IN=x->n_sigIN, n_OUT=x->n_sigOUT;
if (x->retarget) {
int nticks = x->time * x->msec2tick;
t_float oneovernos;
if (!nticks) nticks = 1;
oneovernos = 1./(nticks*n);
x->ticksleft = nticks;
c = n_OUT;
while(c--) {
inc=increment[c];
val=value[c];
tgt=target[c];
r=n_IN;
while(r--)*inc++=(*tgt++-*val++)*oneovernos;
}
x->retarget = 0;
}
if (x->ticksleft) {
int N=n-1;
n=-1;
// while (n--) {
while(n++<N){
c = n_OUT;
while(c--) {
t_float sum = 0;
val = value[c]+n_IN-1;
inc = increment[c]+n_IN-1;
r=n_IN;
while(r--)sum+=in[r][n]*(*val--+=*inc--);
sigBUF[c]=sum;
}
buf = sigBUF;
c = n_OUT;
while(c--)out[c][n]=*buf++;
}
if (!--x->ticksleft) {
c = n_OUT;
while(c--){
val=value[c];
tgt=target[c];
r=n_IN;
while(r--)*val++=*tgt++;
}
}
} else { /* no ticks left */
while (n--) {
c = n_OUT;
while(c--) {
t_float sum = 0;
val = value[c]+n_IN-1;
r = n_IN;
while(r--)sum+=in[r][n]**val--;
sigBUF[c]=sum;
}
buf = sigBUF;
c = n_OUT;
while(c--)out[c][n]=*buf++;
}
}
return (w+3);
}
static void mtx_multilde_dsp(t_mtx_multilde *x, t_signal **sp)
{
int o = x->n_sigOUT, i=x->n_sigIN, n=0;
t_float **dummy = x->sigIN;
while(i--)*dummy++=sp[n++]->s_vec;
dummy =x->sigOUT;
while(o--)dummy[o]=sp[n++]->s_vec;
x->msec2tick = sp[0]->s_sr / (1000.f * sp[0]->s_n);
dsp_add(mtx_multilde_perform, 2, x, sp[0]->s_n);
}
/* setup/setdown things */
static void mtx_multilde_free(t_mtx_multilde *x)
{
int i = x->n_sigOUT;
while(i--) {
freebytes(x->value [i], x->n_sigOUT * sizeof(t_float *));
freebytes(x->target [i], x->n_sigOUT * sizeof(t_float *));
freebytes(x->increment[i], x->n_sigOUT * sizeof(t_float *));
}
freebytes(x->value, sizeof(x->value));
freebytes(x->target, sizeof(x->target));
freebytes(x->increment, sizeof(x->increment));
freebytes(x->sigIN, x->n_sigIN * sizeof(t_float *));
freebytes(x->sigOUT, x->n_sigOUT * sizeof(t_float *));
freebytes(x->sigBUF, x->n_sigOUT * sizeof(t_float ));
}
static void *mtx_multilde_new(t_symbol *s, int argc, t_atom *argv)
{
t_mtx_multilde *x = (t_mtx_multilde *)pd_new(mtx_multilde_class);
int i;
int InOut=0;
x->compatibility=0;
if(s==gensym("matrix~")){ // zexy-compat
error("[matrix~] is deprecated");//; use [mtx_*~] instead");
x->compatibility=1;
InOut=1;
}
if(s==gensym("matrix_mul~")){ // iemlib-compat
error("[matrix_mul~] is deprecated; use [mtx_*~] instead");
x->compatibility=2;
InOut=1;
}
x->time = 0;
switch (argc) {
case 0:
x->n_sigIN = x->n_sigOUT = 1;
break;
case 1:
x->n_sigIN = x->n_sigOUT = atom_getfloat(argv);
break;
default:
x->time= atom_getfloat(argv+2);
case 2:
if(InOut){
x->n_sigIN = atom_getfloat(argv);
x->n_sigOUT = atom_getfloat(argv+1);
} else {
x->n_sigOUT = atom_getfloat(argv);
x->n_sigIN = atom_getfloat(argv+1);
}
break;
}
if (x->time<0) x->time=0;
if (x->n_sigIN <1) x->n_sigIN =1;
if (x->n_sigOUT<1) x->n_sigOUT=1;
/* the inlets */
i=x->n_sigIN-1;
while(i--)inlet_new(&x->x_obj,&x->x_obj.ob_pd,&s_signal,&s_signal);
inlet_new(&x->x_obj, &x->x_obj.ob_pd, gensym("matrix"), gensym(""));
floatinlet_new(&x->x_obj, &x->time);
/* the outlets */
i=x->n_sigOUT;
while(i--)outlet_new(&x->x_obj,&s_signal);
/* make all the buffers */
x->sigIN = (t_float**)getbytes(x->n_sigIN * sizeof(t_float*));
x->sigOUT = (t_float**)getbytes(x->n_sigOUT * sizeof(t_float*));
x->sigBUF = (t_float *)getbytes(x->n_sigOUT * sizeof(t_float ));
x->value = (t_float **)getbytes(x->n_sigOUT * sizeof(t_float));
x->target = (t_float **)getbytes(x->n_sigOUT * sizeof(t_float));
x->increment = (t_float **)getbytes(x->n_sigOUT * sizeof(t_float));
i = x->n_sigOUT;
while(i--){
int j = x->n_sigIN;
x->sigOUT [i] = 0;
x->value [i] = (t_float *)getbytes(x->n_sigIN * sizeof(t_float));
x->target [i] = (t_float *)getbytes(x->n_sigIN * sizeof(t_float));
x->increment[i] = (t_float *)getbytes(x->n_sigIN * sizeof(t_float));
while(j--)x->value[i][j]=x->target[i][j]=x->increment[i][j]=0;
}
i = x->n_sigIN;
while(i--)x->sigIN[i] = 0;
x->msec2tick = x->ticksleft = x->retarget = 0;
return (x);
}
static void mtx_multilde_setup(void)
{
mtx_multilde_class = class_new(gensym("mtx_*~"), (t_newmethod)mtx_multilde_new,
(t_method)mtx_multilde_free,
sizeof(t_mtx_multilde), 0, A_GIMME, 0);
class_addcreator((t_newmethod)mtx_multilde_new, gensym("matrix~"), A_GIMME,0);
class_addmethod(mtx_multilde_class, (t_method)mtx_multilde_dsp, gensym("dsp"), 0);
class_addmethod(mtx_multilde_class, nullfn, gensym("signal"), 0);
class_addmethod(mtx_multilde_class, (t_method)mtx_multilde_matrix, gensym(""), A_GIMME, 0);
class_addmethod(mtx_multilde_class, (t_method)mtx_multilde_stop, gensym("stop"), 0);
class_sethelpsymbol(mtx_multilde_class, gensym("iemmatrix/matrix~"));
}
void mtx_tilde_setup(void)
{
mtx_multilde_setup();
}
|