Last active
September 16, 2019 22:28
-
-
Save eeropic/00dd54ea8ae0f02615a5e6fbdb167c6b to your computer and use it in GitHub Desktop.
minimal pd parser -> paper.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var str=`#N canvas 654 312 778 598 12; | |
#X obj 70 130 midiin 1, f 38; | |
#X floatatom 140 250 5 0 0 0 - - -; | |
#X symbolatom 70 190 10 0 0 0 - - -; | |
#X obj 30 130 bng 15 250 50 0 empty empty empty 17 7 0 10 -262144 -1 | |
-1; | |
#X obj 140 280 tgl 15 0 empty empty empty 17 7 0 10 -262144 -1 -1 0 | |
1; | |
#X obj 170 280 nbx 5 14 -1e+37 1e+37 0 0 empty empty empty 0 -8 0 10 | |
-262144 -1 -1 0 256; | |
#X obj 360 40 vsl 15 128 0 127 0 0 sendisymbi recsymbi labeli 0 -9 | |
0 10 -262144 -1 -1 0 1; | |
#X obj 310 210 hsl 128 15 0 127 0 0 empty empty empty -2 -8 0 10 -262144 | |
-1 -1 0 1; | |
#X obj 260 230 vradio 15 1 0 8 empty empty empty 0 -8 0 10 -262144 | |
-1 -1 0; | |
#X obj 260 370 hradio 15 1 0 8 empty empty empty 0 -8 0 10 -262144 | |
-1 -1 0; | |
#X obj 70 350 vu 15 120 empty empty -1 -8 0 10 -66577 -1 1 0; | |
#X obj 331 425 cnv 15 100 60 empty empty canvas1 20 12 0 14 -233017 | |
-66577 0; | |
#X text 90 50 kommentti valilyonti 123 jes; | |
#X msg 70 160 \$1 123 teksti, f 26; | |
#X obj 520 160 expr $i1*2 \; $i2*$i1, f 33; | |
#X floatatom 520 110 5 0 0 0 - - -; | |
#X floatatom 620 110 5 0 0 0 - - -; | |
#X floatatom 520 220 5 0 0 0 - - -; | |
#X floatatom 650 220 5 0 0 0 - - -; | |
#X floatatom 468 66 7 10 20 3 labeli receivesymboli sendisymboli; | |
#X text 55 76 kommentti valilyonti 123 jes, f 40; | |
#X obj 70 100 midiin; | |
#X connect 0 0 13 0; | |
#X connect 0 1 1 0; | |
#X connect 1 0 4 0; | |
#X connect 1 0 5 0; | |
#X connect 3 0 13 0; | |
#X connect 6 0 1 0; | |
#X connect 6 0 7 0; | |
#X connect 7 0 8 0; | |
#X connect 8 0 9 0; | |
#X connect 13 0 2 0; | |
#X connect 14 0 17 0; | |
#X connect 14 1 18 0; | |
#X connect 15 0 14 0; | |
#X connect 16 0 14 1;` | |
var test="keke;jepa;nepa;" | |
console.log(test.split(";")) | |
var pdData=str | |
.split(/;\r|;\n/) | |
.map(x => x.replace(/\n|\r/g, " ")) | |
.map(x => x.split(" ")) | |
function parseObj(objArray){ | |
let objTypes={ | |
bng(a){ | |
}, | |
tgl(a){ | |
}, | |
nbx(a){ | |
}, | |
vsl(a){ | |
}, | |
hsl(a){ | |
}, | |
vradio(a){ | |
}, | |
hradio(a){ | |
}, | |
vu(a){ | |
}, | |
cnv(a){ | |
}, | |
expr(a){ | |
}, | |
other(a){ | |
} | |
} | |
let type=objArray[3] | |
console.log('obj ',objArray) | |
return (objTypes[type] || objTypes['other'])(objArray); | |
} | |
function parse(arr){ | |
arr.shift() | |
let types={ | |
canvas(a){ | |
}, | |
coords(a){}, | |
obj(a){ | |
parseObj(a) | |
}, | |
floatatom(a){ | |
}, | |
symbolatom(a){ | |
}, | |
text(a){ | |
}, | |
other(a){ | |
} | |
} | |
console.log('at ', arr) | |
let type=arr[0] | |
return (types[type] || types['other'])(arr); | |
} | |
class Connection { | |
constructor(source=null, sink=null){ | |
this.source=source | |
this.sink=sink | |
} | |
} | |
class Layout { | |
constructor({x = 0, y = 0, width = null, height = null}={}){ | |
this.x = x | |
this.y = y | |
this.width = width | |
this.height = height | |
} | |
} | |
class Node { | |
constructor({id=null, proto=null, layout=null, args=null}={}){ | |
this.id=id | |
this.proto=proto | |
this.layout=layout | |
this.args=args | |
} | |
} | |
class Patch { | |
constructor(){ | |
this.nodes=[] | |
this.connections=[] | |
this.args=[] | |
this.layout={} | |
} | |
} | |
var patch = { | |
nodes: [], | |
connections: [], | |
args: [], | |
layout: {} } | |
/* | |
node | |
{ | |
id: <id>, | |
proto: <object type>, | |
args: [<arg1>, ..., <argN>], | |
layout: {<key>: <value>}, | |
data: [<number1>, ..., <numberN>], | |
subpatch: <a patch object> | |
} | |
*/ | |
var objs=pdData | |
.filter(x => x[0] == "#X" && x[1]!="connect") | |
console.log(objs) | |
var nodes=objs | |
.map(function(tokens, idx){ | |
let proto = tokens[1] == "obj" ? tokens[4] : tokens[1] | |
let node = new Node({ | |
id: idx, | |
proto: proto, | |
layout: new Layout({x:parseInt(tokens[2]),y:parseInt(tokens[3])}) | |
}) | |
if(proto == "vsl" || proto == "hsl"){ | |
node.layout.width = parseInt(tokens[5]) | |
node.layout.height = parseInt(tokens[6]) | |
} | |
if(proto == "hradio" || proto == "vradio"){ | |
node.numCells = parseInt(tokens[8]) | |
node.layout.size = parseInt(tokens[5]) | |
} | |
else if(proto == "bng" || proto == "tgl")node.layout.width = node.layout.height = parseInt(tokens[5]) | |
return node | |
}) | |
console.log(nodes) | |
var connections=pdData | |
.filter(x => x[1]=="connect") | |
.map(x => new Connection({id:x[2], port:x[3]},{id:x[4], port:x[5]})) | |
console.log(connections) | |
nodes.forEach(function(elem,idx){ | |
let shape=new Shape.Rectangle({ | |
size: [ | |
elem.layout.width || elem.proto.length*7.225+4, | |
elem.layout.height || 19 | |
], | |
point: [elem.layout.x,elem.layout.y], | |
fillColor:"#FFF", | |
strokeColor:"#000", | |
strokeWidth:1, | |
}) | |
let group=new Group(shape) | |
if( | |
elem.proto!="tgl" && | |
elem.proto!="bng" && | |
elem.proto!="vsl" && | |
elem.proto!="hsl" && | |
elem.proto!="vradio" && | |
elem.proto!="hradio" | |
){ | |
let shapeText=new PointText({ | |
fontFamily: "DejaVu Sans Mono", | |
position: new Point(elem.layout.x+2,elem.layout.y+13), | |
fontSize: 12, | |
content: elem.proto | |
}) | |
group.appendTop(shapeText) | |
} | |
}) | |
connections.forEach(function(elem,idx){ | |
let src=nodes[elem.source.id] | |
let tgt=nodes[elem.sink.id] | |
let yOffset = src.layout.height || 19 | |
let shape=new Path({ | |
segments:[[src.layout.x,src.layout.y+yOffset],[tgt.layout.x,tgt.layout.y]], | |
strokeColor:"#000", | |
strokeWidth:1, | |
}) | |
}) | |
/* | |
var test = keke.reduce( | |
function(acc, val, idx){ | |
acc[keys[idx]]=val | |
return acc | |
}, {}) | |
*/ | |
//obj | |
//msg | |
//floatatom | |
//symbolatom | |
//text (comment) | |
//0 left 1 right 2 top 3 bottom label position | |
//console.log(test) | |
/* | |
var pd2=objects.map( | |
function(elem,idx,arr){ | |
if(elem[0]=="#A"){ | |
} | |
else if(elem[0]=="#N"){ | |
} | |
else if(elem[0]=="#X"){ | |
parse(elem) | |
} | |
}) | |
*/ | |
/* | |
pd-fileutils json format | |
{ | |
nodes: [<node1>, ..., <nodeN>], | |
connections: [<connection1>, ..., <connectionN>], | |
args: [<arg1>, ..., <argN>], | |
layout: {<key>: <value>}, | |
} | |
node | |
{ | |
id: <id>, | |
proto: <object type>, | |
args: [<arg1>, ..., <argN>], | |
layout: {<key>: <value>}, | |
data: [<number1>, ..., <numberN>], | |
subpatch: <a patch object> | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment