Forums > MaxMSP

[sharing is fun] store coll in pattr

November 14, 2008 | 9:25 am

Dear Maxers,

I’d like to share a Javascript solution for storing contents of colls
in pattr presets.

It works quite well, even when interpolating presets, but there are
some limitations, such as mixing symbols and numbers. Since colls
share a global namespace in a similar way than pattr does, i find it
quite convenient to keep all of those functions in one place, and to
manage everything together with the global pattr presets.

I haven’t tested it extensively in long term projects, but i’d say
it’s as viable as js’ garbage collector is….

It would be nice to have pattr get/setvalue-functionality directly
integrated into coll, but until that day comes, this solution should
fill that gap.

Below are the files in text-format, but i’ve also put up a zip archive
here: http://www.jasch.ch/dl/coll_to_pattr.zip

Enjoy

/*j

————————————————————
save as c2p.js
————————————————————

/*
c2p.js :: connect coll to pattrstorage via javascript
store contents of a coll in a pattr-value array and dump it back
when pattr slots change
jasch 20081114
*/

// autowatch = 0;
inlets = 4;

// variable declarations
var warehouse = new Array();
var supplier = new Array();
var i = 0;
var j = 0;
var index = 0;
var count = 0;

// unique marker string
const marker = "#-#";

/* standard functions */

function bang()
{
if(inlet == 0) { // bang on first inlet :: new content
warehouse[index++] = "bang";
} else if(inlet == 1) { // bang on second inlet :: new keyword
warehouse[index++] = marker;
warehouse[index++] = "bang";
} else if(inlet == 2) { // bang on third inlet -> read finished ::
get contents
outlet(0, "dump");
} else if(inlet == 3) { // bang on fourth inlet -> dump finished ::
notify pattrwarehouse
notifyclients();
}
}
bang.immediate = 1;

function msg_int(a)
{
if(inlet == 0) { // content
warehouse[index++] = a;
} else if(inlet == 1) { // index
warehouse[index++] = marker;
warehouse[index++] = a;
}
}

function msg_float(a)
{
if(inlet == 0) { // content
warehouse[index++] = a;
}
}
msg_int.immediate = 1;

function list()
{
if(inlet == 0) {
var b = arrayfromargs(arguments);
for(i = 0; i < b.length; i++){
warehouse[index++] = b[i];
}
}
}
list.immediate = 1;

function anything()
{
if(inlet == 0){
var a = arrayfromargs(messagename, arguments);
for(i = 0; i < a.length; i++){
warehouse[index++] = a[i];
}
}
}
anything.immediate = 1;

function symbol(s)
{
if(inlet == 0){
warehouse[index++] = s;
} else if(inlet == 1) {
warehouse[index++] = marker;
warehouse[index++] = s;
}
}
symbol.immediate = 1;

/* pattr communication */

/* when notified, pattr calls this function to retrieve the warehouse
array */
function getvalueof()
{
return warehouse;
}
getvalueof.local = 1;

/* pattr sends new values to array, we update coll */
function setvalueof()
{
warehouse.length = 0;
warehouse = arrayfromargs(arguments);
dump();
}
setvalueof.local = 1;

/* call this function BEFORE storing a pattr preset */
function store()
{
warehouse.length = 0;
index = 0;
notifyclients();
post(warehouse);
outlet(0, "dump");
}
store.immediate = 1;

/* call this function AFTER recalling a pattr preset */
function dump()
{
outlet(0, "clear"); // reset the client coll
supplier.length = 0;
count = 0;

for(i = 0; i < warehouse.length; i++) {
if(warehouse[i] == marker) {
if(supplier.length > 0) {
if(supplier.length > 1) {
if(typeof(supplier[0]) == "number") {
supplier[0] = supplier[0].toFixed(0);
}
outlet(0, "store", supplier); // supply the entry to coll
}
supplier.length = 0;
count = 0;
}
} else {
supplier[count] = warehouse[i];
count++;
}
}
if(supplier.length > 1) {
if(typeof(supplier[0]) == "number") {
supplier[0] = supplier[0].toFixed(0);
}
outlet(0, "store", supplier); // supply the entry to coll
}

}
dump.immediate = 1;

function clear()
{
warehouse.length = supply.length = count = index = 0;
}
clear.immediate = 1;

————————————————————
save as coll_to_pattr.help (Max 4)
————————————————————

max v2;
#N vpatcher 5 49 906 551;
#P origin 3 0;
#P window setfont "Sans Serif" 9.;
#P window linecount 1;
#P comment 295 341 68 196617 view coll Nr.;
#P number 368 340 35 9 0 0 8224 3 0 0 0 221 221 221 255 181 181 0 0 0;
#N vpatcher 53 271 519 498;
#P window setfont "Sans Serif" 9.;
#P flonum 359 71 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P objectname value[3];
#P flonum 322 71 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P objectname value[2];
#P flonum 285 71 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P objectname value[1];
#P flonum 248 71 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P objectname value[0];
#P newex 25 43 34 196617 r c2p;
#P newex 80 173 125 196617 autopattr @autorestore 0;
#X prestore value[0] 1 0 0.;
#X prestore value[1] 1 0 0.;
#X prestore value[2] 1 0 0.;
#X prestore value[3] 1 0 0.;
#P objectname u415000059;
#N coll 4_voice 1;
#P newobj 80 96 101 196617 coll 4_voice 1;
#P newex 80 118 100 196617 js c2p.js;
#P objectname js_store[1];
#N coll 3_voice 1;
#P newobj 80 43 100 196617 coll 3_voice 1;
#P newex 80 65 100 196617 js c2p.js;
#P objectname js_store[0];
#P fasten 0 0 1 0 85 86 72 86 72 36 85 36;
#P fasten 5 0 0 0 30 64 85 64;
#P connect 1 0 0 0;
#P fasten 2 0 3 0 85 139 72 139 72 89 85 89;
#P connect 3 0 2 0;
#P fasten 5 0 2 0 30 117 85 117;
#P connect 1 1 0 1;
#P connect 3 1 2 1;
#P connect 1 2 0 2;
#P connect 3 2 2 2;
#P connect 1 3 0 3;
#P connect 3 3 2 3;
#P pop;
#P newobj 126 241 100 196617 p subpatch;
#P objectname sub;
#N coll 2_voice 1;
#P newobj 126 180 101 196617 coll 2_voice 1;
#P newex 126 202 100 196617 js c2p.js;
#P objectname js_store[1];
#P hidden newex 449 363 60 196617 loadmess 1;
#P hidden message 368 364 79 196617 refer $1_voice;
#P user jit.cellblock 296 359 536 428 3 9 1 1 60 17 0 0 1 0 1 0 0 0 0
0 0 0 247 247 247 255 255 255 0 0 0 232 232 245 0 0 0 215 215 240 1 1
1 0 4 0 0 0;
#P comment 575 409 187 196617 a 1 2 -> b 3 4: numbers get interpolated;
#P comment 575 394 154 196617 a b c -> 1 2 3: interpolation fails;
#P comment 575 341 34 196617 Note:;
#P newex 295 172 47 196617 t l store;
#P newex 332 200 34 196617 s c2p;
#P message 610 265 14 196617 2;
#P flonum 407 293 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P objectname value[3];
#P flonum 370 293 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P objectname value[2];
#P flonum 333 293 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P objectname value[1];
#P flonum 296 293 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P objectname value[0];
#P comment 780 28 85 196617 jasch 20081114;
#P comment 628 266 109 196617 recall a pattr preset;
#P window linecount 2;
#P comment 628 284 139 196617 c2p automatically pushes the contents
out to the collection;
#P window linecount 1;
#P newex 586 282 37 196617 s psto;
#P objectname u106000012[2];
#P message 586 265 14 196617 1;
#P comment 628 192 109 196617 store the pattr preset;
#P window linecount 2;
#P comment 628 153 122 196617 harvest contents from the collection;
#P window linecount 1;
#P newex 586 169 34 196617 s c2p;
#P message 586 152 40 196617 store;
#P newex 586 208 37 196617 s psto;
#P objectname u106000012[1];
#P newex 415 172 37 196617 r psto;
#P objectname u106000012;
#P message 586 191 43 196617 store 2;
#P comment 576 247 92 196617 to recall a preset:;
#P user ubumenu 418 127 86 196617 0 1 1 0;
#X add read;
#X add write;
#X add clientwindow;
#X add storagewindow;
#X prefix_set 0 0 0;
#P newex 71 127 34 196617 r c2p;
#P flonum 364 127 52 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P number 311 127 51 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P button 295 127 15 0;
#P newex 295 150 66 196617 pack store 0;
#P window linecount 2;
#P newex 295 223 67 196617 pattrstorage orfeo;
#X autorestore orfeo.xml;
#X client_rect 10 59 650 299;
#X storage_rect 10 59 650 299;
#P objectname orfeo;
#P window linecount 1;
#N coll 1_voice 1;
#P newobj 126 127 100 196617 coll 1_voice 1;
#P newex 126 149 100 196617 js c2p.js;
#P objectname js_store[0];
#P comment 576 134 90 196617 to store a preset:;
#P window setfont "Sans Serif" 18.;
#P comment 19 17 114 196626 coll to pattr;
#P window setfont "Sans Serif" 9.;
#P comment 139 27 246 196617 store contents of collections in
pattrstorage presets;
#P user panel 567 126 220 110;
#X brgb 247 247 247;
#X frgb 165 165 165;
#X border 1;
#X rounded 0;
#X shadow 0;
#X done;
#P user panel 567 240 220 75;
#X brgb 247 247 247;
#X frgb 165 165 165;
#X border 1;
#X rounded 0;
#X shadow 0;
#X done;
#P window linecount 2;
#P newex 370 223 76 196617 autopattr @autorestore 0;
#X prestore value[0] 1 0 0.;
#X prestore value[1] 1 0 0.;
#X prestore value[2] 1 0 0.;
#X prestore value[3] 1 0 0.;
#P objectname u059000061;
#P window linecount 3;
#P comment 575 355 190 196617 pattr can’t interpolate coll entries
that mix symbol and int/float data-types at the same element-index in
the same line:;
#P user panel 5 4 876 50;
#X brgb 239 239 239;
#X frgb 181 181 181;
#X border 1;
#X rounded 0;
#X shadow 0;
#X done;
#P user panel 295 358 242 71;
#X brgb 191 191 191;
#X frgb 190 190 190;
#X border 1;
#X rounded 0;
#X shadow 0;
#X done;
#P fasten 9 0 10 0 131 170 118 170 118 120 131 120;
#P fasten 16 0 9 0 76 146 131 146;
#P connect 10 0 9 0;
#P fasten 44 0 45 0 131 223 118 223 118 173 131 173;
#P connect 45 0 44 0;
#P fasten 16 0 44 0 76 199 131 199;
#P connect 10 1 9 1;
#P connect 45 1 44 1;
#P connect 10 2 9 2;
#P connect 45 2 44 2;
#P connect 10 3 9 3;
#P connect 45 3 44 3;
#P connect 13 0 12 0;
#P connect 12 0 37 0;
#P fasten 15 0 11 0 369 195 300 195;
#P connect 37 0 11 0;
#P fasten 17 1 11 0 461 220 300 220;
#P fasten 20 0 11 0 420 198 300 198;
#P hidden connect 42 0 41 0;
#P connect 37 1 36 0;
#P fasten 14 0 12 1 316 146 356 146;
#P hidden connect 43 0 47 0;
#P hidden connect 47 0 42 0;
#P connect 22 0 23 0;
#P connect 19 0 21 0;
#P connect 26 0 27 0;
#P fasten 35 0 27 0 615 281 591 281;
#P pop;

————————————————————
copy to clipboard and save as coll_to_pattr.maxhelp (Max 5)
————————————————————

– Pasted Max Patch, click to expand. –

————————————————————
example presets: save as orfeo.xml
————————————————————


December 26, 2008 | 11:04 pm

exactly what i was searching tonight…
congratulation Jash…
;-)

ChianLi


Viewing 2 posts - 1 through 2 (of 2 total)