aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--nusmuk-utils/_mtx_preset-help.pd8
-rw-r--r--nusmuk-utils/_mtx_preset.pd54
-rw-r--r--nusmuk-utils/mtx_preset-help.pd50
-rw-r--r--nusmuk-utils/mtx_preset.c303
4 files changed, 415 insertions, 0 deletions
diff --git a/nusmuk-utils/_mtx_preset-help.pd b/nusmuk-utils/_mtx_preset-help.pd
new file mode 100644
index 0000000..4f5ce00
--- /dev/null
+++ b/nusmuk-utils/_mtx_preset-help.pd
@@ -0,0 +1,8 @@
+#N canvas 717 414 450 300 10;
+#X msg 163 139 read test.txt;
+#X msg 183 167 write test.txt;
+#X obj 163 201 _mtx_preset 12 3;
+#X text 40 16 same as mtx_preset but provide read / write fonctionalities
+;
+#X connect 0 0 2 0;
+#X connect 1 0 2 0;
diff --git a/nusmuk-utils/_mtx_preset.pd b/nusmuk-utils/_mtx_preset.pd
new file mode 100644
index 0000000..9790e52
--- /dev/null
+++ b/nusmuk-utils/_mtx_preset.pd
@@ -0,0 +1,54 @@
+#N canvas 1048 381 730 584 10;
+#X obj 232 527 outlet;
+#X obj 139 40 inlet;
+#X obj 139 67 route read write;
+#X obj 232 400 spigot 1;
+#X msg 248 116 0;
+#X msg 185 117 1;
+#X obj 302 526 textfile;
+#X msg 334 466 clear;
+#X obj 302 399 spigot 0;
+#X obj 139 222 textfile;
+#X msg 139 154 read \$1;
+#X obj 139 176 t b a;
+#X obj 139 199 until;
+#X obj 277 318 f;
+#X obj 347 369 == 0;
+#X obj 185 95 t b a b b b;
+#X obj 302 442 list trim;
+#X obj 302 420 list prepend add;
+#X msg 381 467 write \$1;
+#X obj 381 444 symbol;
+#X obj 139 95 symbol;
+#X msg 216 230 getRow;
+#X obj 232 273 mtx_preset \$1 \$2;
+#X connect 1 0 2 0;
+#X connect 2 0 20 0;
+#X connect 2 1 15 0;
+#X connect 2 2 22 0;
+#X connect 3 0 0 0;
+#X connect 4 0 13 0;
+#X connect 5 0 13 0;
+#X connect 7 0 6 0;
+#X connect 8 0 17 0;
+#X connect 9 0 22 0;
+#X connect 9 1 12 1;
+#X connect 10 0 11 0;
+#X connect 11 0 12 0;
+#X connect 12 0 9 0;
+#X connect 13 0 3 1;
+#X connect 13 0 14 0;
+#X connect 14 0 8 1;
+#X connect 15 0 5 0;
+#X connect 15 1 19 0;
+#X connect 15 2 21 0;
+#X connect 15 3 7 0;
+#X connect 15 4 4 0;
+#X connect 16 0 6 0;
+#X connect 17 0 16 0;
+#X connect 18 0 6 0;
+#X connect 19 0 18 0;
+#X connect 20 0 10 0;
+#X connect 21 0 22 0;
+#X connect 22 0 3 0;
+#X connect 22 0 8 0;
diff --git a/nusmuk-utils/mtx_preset-help.pd b/nusmuk-utils/mtx_preset-help.pd
new file mode 100644
index 0000000..afe6d9e
--- /dev/null
+++ b/nusmuk-utils/mtx_preset-help.pd
@@ -0,0 +1,50 @@
+#N canvas 627 385 770 468 10;
+#X msg 116 142 matrix 10 3 0 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;
+#X msg 124 175 matrix 9 2 0 1 2 3 4 5 6 7 8 10 11 12 13 14 15 16 17
+18;
+#X msg 102 106 matrix 10 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
+0 0 0 0 0 0 0 0 0 0;
+#X msg 138 198 matrix 11 4 0 1 2 3 4 5 6 7 8 9 111 10 11 12 13 14 15
+16 17 18 19 199 20 21 22 23 24 25 26 27 28 29 299 30 31 32 33 34 35
+36 37 38 39 333;
+#X msg 151 244 row 2 0 1 2 3 4 5 6 7 8 9 10 11 12;
+#X floatatom 610 295 5 0 0 0 - - -, f 5;
+#X msg 557 106 getMatrix;
+#X msg 212 338 element 0 2 200;
+#X msg 569 135 getRow;
+#X msg 581 166 getRow 1;
+#X msg 595 211 getRow \$1;
+#X floatatom 593 193 5 0 0 0 - - -, f 5;
+#X msg 609 238 mixRow 1 1;
+#X msg 611 266 mixRow 0 1;
+#X msg 610 314 mixRow 0 0 \$1;
+#X obj 102 411 mtx_preset 10 3;
+#X obj 102 438 print;
+#X msg 180 292 col 8 4 5 6;
+#X obj 557 366 t a;
+#X text 180 18 preset interpolation object;
+#X text 113 79 set values;
+#X text 557 82 get values;
+#X msg 167 267 setRow 2 10;
+#X msg 195 313 setCol 9 11;
+#X connect 0 0 15 0;
+#X connect 1 0 15 0;
+#X connect 2 0 15 0;
+#X connect 3 0 15 0;
+#X connect 4 0 15 0;
+#X connect 5 0 14 0;
+#X connect 6 0 18 0;
+#X connect 7 0 15 0;
+#X connect 8 0 18 0;
+#X connect 9 0 18 0;
+#X connect 10 0 18 0;
+#X connect 11 0 10 0;
+#X connect 12 0 18 0;
+#X connect 13 0 18 0;
+#X connect 14 0 18 0;
+#X connect 15 0 16 0;
+#X connect 17 0 15 0;
+#X connect 18 0 15 0;
+#X connect 22 0 15 0;
+#X connect 23 0 15 0;
diff --git a/nusmuk-utils/mtx_preset.c b/nusmuk-utils/mtx_preset.c
new file mode 100644
index 0000000..afd681c
--- /dev/null
+++ b/nusmuk-utils/mtx_preset.c
@@ -0,0 +1,303 @@
+/*
+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_symbol *s, int argc, t_atom *argv)
+{
+// reception de row Y values...
+// TODO : optimized
+ 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 > 1) && (argv[1].a_type == A_FLOAT))
+ x->matrix[i][row] = atom_getfloatarg(1,argc,argv);
+}
+
+void mtx_preset_setcol(t_mtx_preset *x, t_symbol *s, int argc, t_atom *argv)
+{
+// reception de col X values...
+// TODO : optimized
+ 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 > 1) && (argv[1].a_type == A_FLOAT))
+ x->matrix[col][i] = atom_getfloatarg(1,argc,argv);
+}
+
+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_GIMME, 0); // set Row
+ // row float float -> put value to a row
+ class_addmethod(mtx_preset_class, (t_method)mtx_preset_setcol, gensym("setCol"),A_GIMME, 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
+}