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
|
/*
mtx_preset : store and interpol presets that are in a matrix form
Copyright (C) 2014 Cyrile Henry
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "m_pd.h"
#include "math.h"
#define sizemaxX 1000
#define sizemaxY 1000
static t_class *mtx_preset_class;
typedef struct _mtx_preset {
t_object x_obj;
t_outlet *main_outlet;
t_int sizeX;
t_int sizeY;
t_float matrix[sizemaxX][sizemaxY+1];
} t_mtx_preset;
t_float mix(t_float X, t_float Y, t_float mix)
{
return (1-mix)*X + mix*Y ;
}
#define min(X, Y) (((X) < (Y)) ? (X) : (Y))
#define max(X, Y) (((X) > (Y)) ? (X) : (Y))
void *mtx_preset_new(t_symbol *s, int argc, t_atom *argv)
{
int i, j;
t_mtx_preset *x = (t_mtx_preset *)pd_new(mtx_preset_class);
x->sizeX = 2;
if ((argc>0) && (argv[0].a_type == A_FLOAT))
x->sizeX = atom_getfloatarg(0, argc, argv);
if (x->sizeX > sizemaxX)
x->sizeX = sizemaxX;
if (x->sizeX < 1)
x->sizeX = 1;
x->sizeY = 2;
if ((argc>1) && (argv[1].a_type == A_FLOAT))
x->sizeY = atom_getfloatarg(1, argc, argv);
if (x->sizeY > sizemaxX)
x->sizeY = sizemaxX;
if (x->sizeY < 1)
x->sizeY = 1;
for (i=0; i < sizemaxX; i++)
for (j=0; j < sizemaxY; j++)
x->matrix[i][j] = 0;
x->main_outlet = outlet_new(&x->x_obj, 0);
return (void *)x;
}
void mtx_preset_resize(t_mtx_preset *x, t_float sizeX, t_float sizeY)
{
x->sizeX = sizeX;
x->sizeY = sizeY;
if (x->sizeX > sizemaxX)
x->sizeX = sizemaxX;
if (x->sizeX < 1)
x->sizeX = 1;
if (x->sizeY > sizemaxX)
x->sizeY = sizemaxX;
if (x->sizeY < 1)
x->sizeY = 1;
}
void mtx_preset_matrix(t_mtx_preset *x, t_symbol *s, int argc, t_atom *argv)
{
int X,Y, mtxX, mtxY, i, j,k;
if ((argc > 3) && (argv[0].a_type == A_FLOAT) && (argv[1].a_type == A_FLOAT) && (argv[2].a_type == A_FLOAT)) {
// reception de matrix X Y values
// copy data, carfull if matrix and mtx_interpol have different size
mtxX = atom_getfloatarg(0,argc,argv);
mtxY = atom_getfloatarg(1,argc,argv);
X = min(x->sizeX, mtxX);
Y = min(x->sizeY, mtxY);
for (i=0; i < X; i++) {
for (j=0; j < Y; j++) {
k= j*mtxX + i + 2;
if ((argc > k) && (argv[k].a_type == A_FLOAT))
x->matrix[i][j] = atom_getfloatarg(k,argc,argv);
}
}
}
else if ((argc > 1) && (argv[1].a_type == A_FLOAT)) {
// reception de matrix value
for (i=0; i < x->sizeX; i++) {
for (j=0; j < x->sizeY; j++) {
x->matrix[i][j] = atom_getfloatarg(0,argc,argv);
}
}
}
else {
error("bad matrix");
return;
}
}
void mtx_preset_row(t_mtx_preset *x, t_symbol *s, int argc, t_atom *argv)
{
// reception de row Y values...
int row, i;
if ((argc > 0) && (argv[0].a_type == A_FLOAT))
row = atom_getfloatarg(0,argc,argv);
else {
error("bad row");
return;
}
row = min(x->sizeY, row);
for (i=0; i < x->sizeX; i++)
if ((argc > i+1) && (argv[i+1].a_type == A_FLOAT))
x->matrix[i][row] = atom_getfloatarg(i+1,argc,argv);
}
void mtx_preset_col(t_mtx_preset *x, t_symbol *s, int argc, t_atom *argv)
{
// reception de col X values...
int col, i;
if ((argc > 0) && (argv[0].a_type == A_FLOAT))
col = atom_getfloatarg(0,argc,argv);
else {
error("bad col");
return;
}
col = min(x->sizeX, col);
for (i=0; i < x->sizeY; i++)
if ((argc > i+1) && (argv[i+1].a_type == A_FLOAT))
x->matrix[col][i] = atom_getfloatarg(i+1,argc,argv);
}
void mtx_preset_setrow(t_mtx_preset *x, t_float rowtoset, t_float value)
{
// reception de row Y value
int row, i;
row = min(x->sizeY, rowtoset);
row = max(0, row);
for (i=0; i < x->sizeX; i++)
x->matrix[i][row] = value;
}
void mtx_preset_setcol(t_mtx_preset *x, t_float coltoset, t_float value)
{
// reception de col X value
int col, i;
col = min(x->sizeX, coltoset);
col = max(0, col);
for (i=0; i < x->sizeY; i++)
x->matrix[col][i] = value;
}
void mtx_preset_element(t_mtx_preset *x, t_float posX, t_float posY, t_float value)
{
// reception de element X Y value
if ((posX < x->sizeX) && (posX < x->sizeX)) {
x->matrix[(int)posX][(int)posY] = value;
}
}
void mtx_preset_getMatrix(t_mtx_preset *x)
{
int i, j;
// dump matrix tailleX tailleY datas...
t_atom std_out[x->sizeX*x->sizeY+2];
SETFLOAT(&(std_out[0]),x->sizeX);
SETFLOAT(&(std_out[1]),x->sizeY);
for (i=0; i < x->sizeX; i++)
for (j=0; j < x->sizeY; j++)
SETFLOAT(&(std_out[i+x->sizeX*j+2]),x->matrix[i][j]);
outlet_anything(x->main_outlet, gensym("matrix"),x->sizeX*x->sizeY+2,std_out);
}
void mtx_preset_getRow(t_mtx_preset *x, t_symbol *s, int argc, t_atom *argv)
{
int i,j, a,b;
t_float c;
t_atom std_out[x->sizeX+1];
if (argc == 0)
for (i=0; i< x->sizeY; i++) {
SETFLOAT(&(std_out[0]),i);
for (j=0; j < x->sizeX; j++) {
SETFLOAT(&(std_out[j+1]),x->matrix[j][i]);
}
outlet_anything(x->main_outlet, gensym("row"),x->sizeX+1,std_out);
}
else if ((argc > 0) && (argv[0].a_type == A_FLOAT)) {
a = floor(atom_getfloatarg(0,argc,argv));
b = a+1;
c = atom_getfloatarg(0,argc,argv)-a;
for (i=0; i < x->sizeX; i++) {
SETFLOAT(&(std_out[i]),mix(x->matrix[i][a], x->matrix[i][b], c));
}
outlet_list(x->main_outlet, gensym("list"), x->sizeX, std_out);
}
}
void mtx_preset_getElement(t_mtx_preset *x, t_float posX, t_float posY)
{
int a,b;
a = min(posX, x->sizeX);
a = max(a,0);
b = min(posY, x->sizeY);
b = max(b,0);
outlet_float(x->main_outlet, x->matrix[a][b]);
}
void mtx_preset_mixRow(t_mtx_preset *x, t_symbol *s, int argc, t_atom *argv)
{
int i, j;
t_atom std_out[x->sizeX];
t_float row[x->sizeX];
for (i=0; i < x->sizeX; i++)
row[i] = 0;
for (j=0; j<min(argc, x->sizeY); j++)
for (i=0; i < x->sizeX; i++)
row[i] += x->matrix[i][j] * atom_getfloatarg(j, argc, argv);
for (i=0; i < x->sizeX; i++)
SETFLOAT(&(std_out[i]),row[i]);
outlet_list(x->main_outlet, gensym("list"), x->sizeX, std_out);
}
void mtx_preset_setup(void) {
mtx_preset_class = class_new(gensym("mtx_preset"),
(t_newmethod)mtx_preset_new,
0, sizeof(t_mtx_preset),
CLASS_DEFAULT, A_GIMME, 0);
class_addmethod(mtx_preset_class, (t_method)mtx_preset_matrix, gensym("matrix"),A_GIMME, 0); // put Matrix
// matrix value -> all matrix will be at the float value
// matrix sizeX sizeY values ... -> put the matrix in 0 0
class_addmethod(mtx_preset_class, (t_method)mtx_preset_row, gensym("row"),A_GIMME, 0); // put Row
// row float float ... -> put all value to a row
class_addmethod(mtx_preset_class, (t_method)mtx_preset_col, gensym("col"),A_GIMME, 0); // put Col
// coll float float ... -> put all value to a col
class_addmethod(mtx_preset_class, (t_method)mtx_preset_setrow, gensym("setRow"),A_FLOAT, A_FLOAT, 0); // set Row
// row float float -> put value to a row
class_addmethod(mtx_preset_class, (t_method)mtx_preset_setcol, gensym("setCol"),A_FLOAT, A_FLOAT, 0); // set Col
// coll float float -> put value to a col
class_addmethod(mtx_preset_class, (t_method)mtx_preset_element, gensym("element"),A_FLOAT, A_FLOAT, A_FLOAT, 0); // put 1 element
// element posX posY value
class_addmethod(mtx_preset_class, (t_method)mtx_preset_getMatrix, gensym("getMatrix"), 0); // get matrix (matrix sizeX sizeY value)
class_addmethod(mtx_preset_class, (t_method)mtx_preset_getRow, gensym("getRow"), A_GIMME, 0); // get row
// getRow -> dump all row (row now_number row values)
// getRow row_number -> dump 1 row (values)
// row_number can be float : row will be interpolated
class_addmethod(mtx_preset_class, (t_method)mtx_preset_getElement,gensym("getElement"),A_FLOAT, A_FLOAT, 0); // get 1 element
// get posX posY -> dump 1 element
// posX and posY can be float : value will be interpolated in X and Y
class_addmethod(mtx_preset_class, (t_method)mtx_preset_mixRow, gensym("mixRow"), A_GIMME, 0); // interpol between all row
// mixRow value0 value1 value2 : (value0*row0 + value1*row1 + value2*row2)
class_addmethod(mtx_preset_class, (t_method)mtx_preset_resize, gensym("resize"),A_FLOAT, A_FLOAT, 0); // resize matrix
}
|