diff --git a/modules/graph-OLD.js b/modules/graph-OLD.js deleted file mode 100755 index 164cf31..0000000 --- a/modules/graph-OLD.js +++ /dev/null @@ -1,144 +0,0 @@ - -/** -Graph() - -Constructs a graph of AudioNodes. - -**/ - -/** -.nodes -An array of objects defining graph nodes. See Nodes and Connectors. -**/ - -/** -.connections -An array of objects defining connections. See Nodes and Connectors. -**/ - - -import get from '../../fn/modules/get.js'; -import has from '../../fn/modules/has.js'; -import Privates from '../../fn/modules/privates.js'; - -import Node from './graph/node.js'; -import Connector from './graph/connector.js'; - -import { print } from './print.js'; -import { generateUnique } from './utilities.js'; - -const assign = Object.assign; -const define = Object.defineProperties; - -function addConnector(graph, setting) { - new Connector(graph, setting.source, setting.target, setting.output, setting.input); - return graph; -} - -export default function Graph(context, merger, data, transport, options) { - const graph = this; - const privates = Privates(this); - - privates.merger = merger; - privates.transport = transport; - privates.options = options; - - define(this, { - nodes: { enumerable: true, value: [] }, - connections: { enumerable: true, value: [] } - }); - - if (data.nodes) { - data.nodes.forEach(function(data) { - // Nodes add themselves to the graph - return new Node(graph, context, data.type, data.id, data.label, data.node, merger, transport, options); - }); - } - - if (data.connections) { - data.connections.reduce(addConnector, graph); - } - - print('graph', graph.nodes.length + ' nodes, ' + graph.connections.length + ' connections'); -} - -assign(Graph.prototype, { - - /** - .createNode(type, settings) - - Creates a new AudioNode wrapped in a new node of the Soundstage graph. - - ```js - const delay = stage.createNode('delay', { - delayTime: 0.5 - }); - ``` - - The `type` parameter is a string, and must be one of the [node types](#node-types) - either built-in or registered with Soundstage. The `settings` parameter is an - object of settings specific to that node type. - - The AudioNode is wrapped in an object with an id and label in the `.nodes` - array. The wrapper object is returned. - **/ - - createNode: function (type, data) { - const graph = this; - const privates = Privates(this); - const merger = privates.merger; - const transport = privates.transport; - const notify = privates.notify; - const options = privates.options; - const id = generateUnique(this.nodes.map(get('id'))); - const node = new Node(graph, graph.context, type, id, type, data, merger, transport, options); - - notify(graph.nodes, '.'); - - return node; - }, - - /** - .createConnection(source, target) - - Creates a connection between two nodes in the graph. The parameters - `source` and `target` are node ids. - **/ - - createConnection: function (source, target, output, input) { - return new Connector(this, source, target, output, input); - }, - - /** - .get(id) - - Returns the AudioNode with `id` from the graph, or undefined. - - ```js - const node = stage.get('0'); - ``` - **/ - - get: function(id) { - //console.trace('graph.get(id) renamed graph.getNode(id) !!'); - return this.nodes.find(has('id', id)).node; - }, - - getNode: function(id) { - return this.nodes.find(has('id', id)); - }, - - /** - .identify(node) - - Returns the id of the graph node that wraps the AudioNode `node`. - - ```js - const id = stage.identify(node); - ``` - **/ - - identify: function(node) { - return this.nodes.find(has('node', node)).id; - } -}); diff --git a/modules/graph.js b/modules/graph.js deleted file mode 100644 index 02bc494..0000000 --- a/modules/graph.js +++ /dev/null @@ -1,48 +0,0 @@ - -import Nodes from './graph/nodes.js'; -import Connectors from './graph/connectors.js'; - -const assign = Object.assign; -const define = Object.defineProperties; -const properties = { - nodes: { enumerable: true }, - connectors: { enumerable: true } -}; - - -/* Graph */ - -export default function Graph(nodes, connectors, context, merger, transport) { - // Define properties - properties.nodes.value = new Nodes(this, nodes, context, merger, transport); - properties.connectors.value = new Connectors(properties.nodes.value, connectors); - define(this, properties); -} - -assign(Graph, { - from: function(data) { - return new Graph(data.nodes, data.connectors, data.context); - } -}); - -assign(Graph.prototype, { - find: function(fn) { - return this.nodes.find(fn) || this.connectors.find(fn); - }, - - findAll: function() { - return this.nodes.findAll(fn).concat(this.connectors.findAll(fn)); - }, - - push: function(event) { - if (event.address.startsWith('nodes.')) { - return this.nodes.push(event); - } - - if (event.address.startsWith('connects.')) { - return this.connectors.push(event); - } - - console.log('Event not handled', event); - } -}); diff --git a/modules/graph/connectors.js b/modules/graph/connectors.js index a668376..e984c67 100644 --- a/modules/graph/connectors.js +++ b/modules/graph/connectors.js @@ -16,14 +16,10 @@ const assign = Object.assign; const define = Object.defineProperties; -function getNodeFrom(nodes, src) { +function getObjectFrom(objects, src) { return typeof src === 'object' ? - nodes.find((entry) => entry === src || entry.node === src) : - nodes.find(matches({ id: src })) ; -} - -function create() { - + objects.find((object) => object === src || object.node === src) : + objects.find(matches({ id: src })) ; } @@ -61,7 +57,7 @@ assign(Connectors.prototype, { const privates = Privates(this); let n = -1; while (this[++n]); - return this[n] = new Connector(this, getNodeFrom(privates.nodes, src), getNodeFrom(privates.nodes, tgt), srcChan, tgtChan); + return this[n] = new Connector(this, getObjectFrom(privates.nodes, src), getObjectFrom(privates.nodes, tgt), srcChan, tgtChan); } }), diff --git a/modules/graph/nodes.js b/modules/graph/objects.js similarity index 88% rename from modules/graph/nodes.js rename to modules/graph/objects.js index b9835da..21e46b4 100644 --- a/modules/graph/nodes.js +++ b/modules/graph/objects.js @@ -13,7 +13,7 @@ import { log } from '../print.js'; /** -GraphNodes() +Objects() **/ const assign = Object.assign; @@ -29,7 +29,7 @@ const properties = { } }; -export default function GraphNodes(stage, data = [], context, merger, transport) { +export default function Objects(stage, data = [], context, merger, transport) { // Objects that should be passed onto child nodes const privates = Privates(this); privates.stage = stage; @@ -44,12 +44,12 @@ export default function GraphNodes(stage, data = [], context, merger, transport) // Loop through nodes in data and create entries for them let n = -1, d; while (d = data[++n]) this.create(d.type, d.id, d.node, d.events, context, merger, transport); - log('GraphNodes', data.length + ' nodes'); + log('Objects', data.length + ' nodes'); } -assign(GraphNodes, { +assign(Objects, { /*from: function(data) { - return new GraphNodes(data.stage, data.nodes, data.context, data.merger, data.transport); + return new Objects(data.stage, data.nodes, data.context, data.merger, data.transport); },*/ types: { @@ -57,7 +57,7 @@ assign(GraphNodes, { } }); -define(GraphNodes.prototype, { +define(Objects.prototype, { length: { get: function() { let n = -1; @@ -67,7 +67,7 @@ define(GraphNodes.prototype, { } }); -assign(GraphNodes.prototype, { +assign(Objects.prototype, { create: overload((object) => typeof object, { object: function(data) { const privates = Privates(this); diff --git a/modules/soundstage.js b/modules/soundstage.js index 34050f6..e6fabad 100755 --- a/modules/soundstage.js +++ b/modules/soundstage.js @@ -10,7 +10,8 @@ import requestMedia from './request/request-media.js'; import { context, domTimeAtTime, timeAtDomTime, getOutputLatency } from './context.js'; import { connect, disconnect } from './connect.js'; import constructors from './graph/constructors.js'; -import Graph from './graph.js'; +import Objects from './graph/objects.js'; +import Connectors from './graph/connectors.js'; import Playable, { IDLE } from './playable.js'; import Sequencer from './sequencer/sequencer.js'; import Transport from './transport.js'; @@ -31,9 +32,15 @@ const define = Object.defineProperties; const defaults = { context: context, - nodes: [{ id: '0', type: 'output' }] + objects: [{ id: '0', type: 'output' }] }; +const properties = { + mediaChannelCount: { enumerable: true }, + transport: {}, + objects: { enumerable: true }, + connectors: { enumerable: true } +}; /* Nodes */ @@ -79,7 +86,7 @@ function createOutputMerger(context, target) { Soundstage() ```js -const stage = new Soundstage(nodes, connectors, events, sequences, options); +const stage = new Soundstage(objects, connectors, events, sequences, options); const stage = Soundstage.from(data); ``` @@ -87,7 +94,7 @@ A stage is a graph of AudioNodes and a sequencer of events that control those AudioNodes. **/ -export default function Soundstage(nodes = defaults.nodes, connectors = [], events = [], sequences = [], settings = nothing) { +export default function Soundstage(objects = defaults.objects, connectors = [], events = [], sequences = [], settings = nothing) { if (window.DEBUG) { printGroup('Soundstage()'); } @@ -97,36 +104,30 @@ export default function Soundstage(nodes = defaults.nodes, connectors = [], even const destination = settings.destination || context.destination; const merger = createOutputMerger(context, destination); - /** - .transport - **/ - this.transport = new Transport(context); + /** .transport **/ + const transport = new Transport(context); privates.beat = 0; privates.outputs = { default: merger, - rate: this.transport.outputs.rate, - beat: this.transport.outputs.beat + rate: transport.outputs.rate, + beat: transport.outputs.beat }; - /** - .label - A string name for this Soundstage document. - **/ - //this.label = data.label || ''; + properties.transport.value = transport; - /** - .mediaChannelCount - **/ - define(this, { - mediaChannelCount: { value: undefined, writable: true, configurable: true } - }); + /** .mediaChannelCount **/ + properties.mediaChannelCount.value = settings.mediaChannelCount || 2; + + // Setup the objects graph + /** .objects **/ + properties.objects.value = new Objects(this, objects, context, merger, transport); + /** .connectors **/ + properties.connectors.value = new Connectors(properties.objects.value, connectors); + /* TODO .pipes **/ - // .nodes - // .connections - // .find() - // .findAll() - Graph.call(this, nodes, connectors, context, merger, this.transport); + // Define properties + define(this, properties); // .context // .events @@ -152,11 +153,11 @@ assign(Soundstage, { throw new Error('Soundstage: no adapter for data version ' + data.version); } - return new Soundstage(data.nodes, data.connectors, data.events, data.sequences); + return new Soundstage(data.objects, data.connectors, data.events, data.sequences, data.settings); } }); -mix(Soundstage.prototype, Sequencer.prototype, Graph.prototype/*, Meter.prototype*/); +mix(Soundstage.prototype, Sequencer.prototype/*, Graph.prototype, Meter.prototype*/); define(Soundstage.prototype, { /** @@ -286,7 +287,7 @@ define(Soundstage.prototype, { */ }); -assign(Soundstage.prototype, Sequencer.prototype, Graph.prototype, { +assign(Soundstage.prototype, Sequencer.prototype, /*Graph.prototype,*/ { /*createControl: function(source, target, options) { const privates = Privates(this); @@ -299,12 +300,16 @@ assign(Soundstage.prototype, Sequencer.prototype, Graph.prototype, { },*/ find: function(fn) { - let object = this.nodes && this.nodes.find(fn); + let object = this.objects && this.objects.find(fn); if (object) { return object; } object = this.sequences && this.sequences.find(fn); return object; }, + findAll: function() { + return this.objects.findAll(fn).concat(this.connectors.findAll(fn)); + }, + /* Receive events */ automate: function(address, name, value, curve, duration, time) { @@ -387,12 +392,12 @@ assign(Soundstage.prototype, Sequencer.prototype, Graph.prototype, { .start(this.startTime); // Create dependent playheads for graph nodes that have events - let n = -1, node; - while(node = this.nodes[++n]) { - if (node.events && node.events.length) { + let n = -1, object; + while(object = this.objects[++n]) { + if (object.events && object.events.length) { // 'playhead', events, sequences, transform, target head - .create('playhead', node.events, this.sequences, id, node) + .create('playhead', object.events, this.sequences, id, object) .start(0); } } @@ -479,7 +484,7 @@ assign(Soundstage.prototype, Sequencer.prototype, Graph.prototype, { **/ records: function() { - return this.nodes.reduce((list, node) => { + return this.objects.reduce((list, node) => { const data = node.records && node.records(); return data ? list.concat(data) : list ; }, []); diff --git a/modules/test/index.html b/modules/test/index.html index a608fe1..4e09395 100644 --- a/modules/test/index.html +++ b/modules/test/index.html @@ -32,7 +32,7 @@