Last active
January 22, 2018 21:27
-
-
Save eeropic/51ce9a469b296d62021f8b1c51381417 to your computer and use it in GitHub Desktop.
tonejs patch / graph dev
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
| include('https://cdnjs.cloudflare.com/ajax/libs/tone/0.12.7/Tone.min.js') | |
| const DEBUG=0; | |
| function log(item){if(typeof console != "undefined" && console.log && DEBUG){var props=[];for(var a in arguments){props.push(arguments[a])}console.log(props.join(","));}} | |
| var nodeCount=0; | |
| var edgeCount=0; | |
| function omitKeys(key,value){ | |
| if (key=="Tone") return undefined; | |
| else if (key=="Nexus") return undefined; | |
| else return value; | |
| } | |
| //ids for tone objects for graph access | |
| Tone.prototype.id={ | |
| get() { | |
| return this.id | |
| }, | |
| set(value) { | |
| this.id=value; | |
| } | |
| } | |
| //mirror functions for connections, for | |
| //syncing the graph manipulation with tone objects | |
| Tone.prototype.plug=function(dest, ...args){ | |
| var edgeId=edgeCount++; | |
| var edge={} | |
| //audio->audio | |
| if(arguments.length==1){ | |
| if(dest=="Tone"){ | |
| log(this.id+' connected to '+'Tone.Master') | |
| edge.id='conn'+edgeId | |
| edge.src={id:this.id,port:'audio'} | |
| edge.tgt={id:'Tone.Master',port:'audio'} | |
| this.connect(dest) | |
| graph.edges.push(edge) | |
| return this | |
| } | |
| else{ | |
| log(this.id+' connected to '+dest.id) | |
| edge.id='conn'+edgeId, | |
| edge.src={id:this.id,port:'audio'}, | |
| edge.tgt={id:dest.id,port:'audio'} | |
| this.connect(dest) | |
| graph.edges.push(edge) | |
| return this | |
| } | |
| } | |
| //audio/param->param | |
| if(arguments.length>1){ | |
| log(this.id+' connected to '+dest.id) | |
| edge.id='conn'+edgeId, | |
| edge.src={id:this.id,port:'audio'}, | |
| edge.tgt={id:dest.id,port:args[0]} | |
| this.connect(dest[args[0]]) | |
| graph.edges.push(edge) | |
| return this | |
| } | |
| } | |
| Tone.prototype.unplug=function(dest){ | |
| log(this+' disconnected from '+dest) | |
| this.disconnect(dest) | |
| return this | |
| } | |
| Tone.prototype.set=function(param,value){ | |
| log('set ',this,param,value) | |
| this[param]=value | |
| graph.getNode(this.id).parameters[param]=value | |
| return this | |
| } | |
| //mirror functions for parameter manipulation | |
| //how to retrieve the parent object / detect which object the property belongs to? | |
| Tone.Param.prototype.set=function(v){ | |
| log(this.parent) | |
| log('set ',this,v) | |
| //console.log('ff '+getNodeById(graph,this.id)) | |
| this.value=v | |
| } | |
| var Patch=function(){ | |
| this.libs={} | |
| } | |
| var patch=new Patch() | |
| patch.libs["Tone"]=Tone | |
| var Graph=function(){ | |
| this.nodes=[] | |
| this.edges=[] | |
| this.getNode=function (id){ | |
| return this.nodes.find(obj => obj.id == id) | |
| } | |
| this.add=function(obj, ...args){ | |
| var libraryName=obj.split("/")[0]; | |
| var objectName=obj.split("/")[1]; | |
| var library=patch.libs[libraryName] | |
| var idNum=nodeCount++; | |
| var node={ | |
| type: obj, | |
| id:''+objectName+idNum, | |
| } | |
| var newObj=new library[objectName](...args) | |
| //copy object creation arguments to the node object | |
| if(typeof args[0]=='object')node.parameters=args[0] | |
| newObj.id=''+objectName+idNum; | |
| node[libraryName]=newObj | |
| this.nodes.push(node) | |
| return newObj | |
| } | |
| } | |
| //graph.add() | |
| var graph=new Graph(); | |
| var osc=graph.add('Tone/Oscillator',{ | |
| frequency:200, | |
| type:'custom', | |
| partials:[0.6,0.7,1.1,1.2] | |
| }) | |
| osc.start() | |
| osc.set('frequency',80) | |
| var lfo = graph.add('Tone/LFO',{ | |
| frequency:"16n", | |
| min:400, | |
| max:500 | |
| }); | |
| lfo.start() | |
| lfo.plug(osc, 'frequency') | |
| osc.plug(Tone.Master) | |
| //list all tone objects to be added | |
| var toneKeys=Object.getOwnPropertyNames(Tone); | |
| var ignoreKeys=['equalPowerScale','dbToGain','gainToDb','intervalToFrequencyRatio','TransportTime','Frequency','Time','TimeBase','Context','now','isPast','loaded','Type','version','setContext','getContext','initialized','supported','context','extend','Offline','OfflineContext','Listener','Draw','Mono','Master','Expr','Transport','TransportRepeatEvent']; | |
| for(var i=16;i<toneKeys.length;i++){ | |
| if(ignoreKeys.indexOf(toneKeys[i])==-1){ | |
| //var obj=graph.add(Tone[toneKeys[i]]) | |
| } | |
| } | |
| console.log(JSON.parse(JSON.stringify(graph,omitKeys))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment