From 18f4af10448a2f7e68b2962260cb8b1bea1e88a5 Mon Sep 17 00:00:00 2001 From: Stephen Date: Fri, 29 Sep 2023 16:03:58 +0200 Subject: [PATCH] Remove circular reference --- nodes/graph.js | 30 +++++++++++++++++------------- nodes/instrument.js | 18 +++++++++++------- nodes/sample-set.js | 4 ++-- nodes/voice.js | 2 +- 4 files changed, 31 insertions(+), 23 deletions(-) diff --git a/nodes/graph.js b/nodes/graph.js index 1b7dfc8..6022ffe 100755 --- a/nodes/graph.js +++ b/nodes/graph.js @@ -58,22 +58,18 @@ output by the `.connect()` and `.disconnect()` methods. import Privates from '../../fn/modules/privates.js'; import { logGroup, logGroupEnd } from '../modules/print.js'; import { connect, disconnect } from '../modules/connect.js'; -import nativeConstructors from '../modules/constructors.js'; +import baseConstructors from '../modules/constructors.js'; +import Sink from './sink.js'; -import Mix from './mix.js'; -import SampleSet from './sample-set.js'; -import Tone from './tone.js'; - -const DEBUG = false;//window.DEBUG; +const DEBUG = window.DEBUG && window.DEBUG.soundstage !== false; const assign = Object.assign; const define = Object.defineProperties; const seal = Object.seal; -export const constructors = assign({ - mix: Mix, - samples: SampleSet, - tone: Tone, -}, nativeConstructors); +const constructors = assign({ + sink: Sink +}, baseConstructors); + function create(type, context, settings, transport) { const Constructor = constructors[type]; @@ -140,7 +136,7 @@ function createConnection(nodes, data) { } export default function NodeGraph(context, data, transport) { - if (DEBUG) { logGroup('mixin ', 'GraphNode', data.nodes && data.nodes.map((n) => n.type).join(', ')); } + if (window.DEBUG) { logGroup('mixin ', 'GraphNode', data.nodes && data.nodes.map((n) => n.type).join(', ')); } const privates = Privates(this); privates.outputId = data.output || 'output' ; @@ -210,7 +206,7 @@ export default function NodeGraph(context, data, transport) { seal(nodes); data.connections && data.connections.reduce(createConnection, nodes); - if (DEBUG) { logGroupEnd(); } + if (window.DEBUG) { logGroupEnd(); } } assign(NodeGraph.prototype, { @@ -250,3 +246,11 @@ assign(NodeGraph.prototype, { return privates.nodes && privates.nodes[id]; } }); + +NodeGraph.register = function register(name, constructor) { + if (constructors[name]) { + throw new Error('Soundstage: constructor "' + name+ '" already registered in NodeGraph'); + } + + constructors[name] = constructor; +}; diff --git a/nodes/instrument.js b/nodes/instrument.js index 1bfaaa8..83cc312 100755 --- a/nodes/instrument.js +++ b/nodes/instrument.js @@ -40,19 +40,23 @@ import isDefined from '../../fn/modules/is-defined.js'; import Privates from '../../fn/modules/privates.js'; import { logGroup, logGroupEnd } from './print.js'; import Voice, { defaults as voiceDefaults } from './voice.js'; -import NodeGraph, { constructors } from './graph.js'; -import Pool from '../modules/pool.js'; +import NodeGraph from './graph.js'; +import Pool from '../modules/pool.js'; import { assignSettingz__ } from '../modules/assign-settings.js'; import { connect, disconnect } from '../modules/connect.js'; +import Mix from './mix.js'; +import Samples from './sample-set.js'; +import Tone from './tone.js'; + const DEBUG = window.DEBUG; const assign = Object.assign; const define = Object.defineProperties; -import Sink from './sink.js'; -assign(constructors, { - sink: Sink -}); +// Register node constructors in NodeGraph +NodeGraph.register('mix', Mix); +NodeGraph.register('samples', Samples); +NodeGraph.register('tone', Tone); export const config = { tuning: 440 @@ -62,7 +66,7 @@ const graph = { nodes: [ { id: 'sink', type: 'sink' }, { id: 'pitch', type: 'constant', data: { offset: 0 } }, - { id: 'detune', type: 'gain', data: { gain: 100 } }, + { id: 'detune', type: 'gain', data: { gain: 100 } }, { id: 'modulation', type: 'constant', data: { offset: 120 } }, { id: 'output', type: 'gain', data: { channelInterpretation: 'speakers', diff --git a/nodes/sample-set.js b/nodes/sample-set.js index 21ce24e..ab79ced 100755 --- a/nodes/sample-set.js +++ b/nodes/sample-set.js @@ -44,8 +44,8 @@ import { requestBuffer } from '../modules/request-buffer.js'; import { requestData } from '../modules/request-data.js'; import { assignSettingz__ } from '../modules/assign-settings.js'; import { fadeInFromTime, fadeOutToTime, releaseAtTime } from '../modules/param.js'; -import NodeGraph from './graph.js'; -import Playable from '../modules/playable.js'; +import NodeGraph from './graph.js'; +import Playable from '../modules/playable.js'; const DEBUG = window.DEBUG; const assign = Object.assign; diff --git a/nodes/voice.js b/nodes/voice.js index 97d921f..3088aed 100755 --- a/nodes/voice.js +++ b/nodes/voice.js @@ -282,7 +282,7 @@ assign(Voice.prototype, Playable.prototype, NodeGraph.prototype, { // Start command target const commands = privates.commands; - +console.log(note, frequency, commands); // Quick out if (!commands) { return this; }