Skip to content

Commit

Permalink
Reorganises mixins
Browse files Browse the repository at this point in the history
  • Loading branch information
stephband committed Feb 7, 2024
1 parent 9ac7bba commit ff07b81
Show file tree
Hide file tree
Showing 29 changed files with 975 additions and 684 deletions.
3 changes: 2 additions & 1 deletion modules/assign-settings.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

import { log, logGroup, logGroupEnd } from './print.js';
import { automato__, getAutomation } from './automate__.js';
import { automato__ } from './automate__.js';
import { getAutomation } from './param.js';

const DEBUG = false;

Expand Down
11 changes: 2 additions & 9 deletions modules/automate.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@

import { hold } from './param.js';

const automation = Symbol('Soundstage.automation');
import { hold, getAutomation } from './param.js';

const curves = {
// time = event[0], value = event[1], curve = event[2], duration = event[3]
'step': (param, event) => param.setValueAtTime(event.value, event.time),
'linear': (param, event) => param.linearRampToValueAtTime(event.value, event.time),
'exponential': (param, event) => param.exponentialRampToValueAtTime(event.value, event.time),
Expand All @@ -12,12 +11,6 @@ const curves = {
'hold': hold
};

function getAutomation(param) {
// Use an expando to store automation events. Automation events are stored
// as Float32 numbers.
return param[automation] || (param[automation] = new Float32Array(64));
}

function automateParam(param, time, value, curve, duration) {
const events = getAutomation(param);

Expand Down
11 changes: 1 addition & 10 deletions modules/automate__.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,16 @@ import id from '../../fn/modules/id.js';
import last from '../../fn/modules/last.js';
import overload from '../../fn/modules/overload.js';

import { getAutomation } from './param.js';
import { isAudioContext, timeAtDomTime } from './context.js';
import { isAudioParam } from './param.js';
import config from '../config.js';

const DEBUG = false;

// 60 frames/sec frame rate
const frameDuration = 1000 / 60;

export function getAutomation(param) {
if (DEBUG && !isAudioParam(param)) {
throw new Error('Not an AudioParam ' + JSON.stringify(param));
}

// Todo: I would love to use a WeakMap to store data about AudioParams,
// but FF refuses to allow AudioParams as WeakMap keys. So... lets use
// an expando.
return param[config.automationEventsKey] || (param[config.automationEventsKey] = []);
}


// Automate audio param
Expand Down
57 changes: 15 additions & 42 deletions modules/graph/node.js → modules/graph/audio-object.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import Privates from '../../../../fn/modules/privates.js';
import remove from '../../../../fn/modules/remove.js';
import { floatToFrequency, toNoteNumber } from '../../../../midi/modules/data.js';

import Node from '../streams/node.js';
import { create } from '../graph/constructors.js';
import { automateParamAtTime, automatePropertyAtTime } from '../automate__.js';
import { isAudioParam } from '../param.js';
Expand All @@ -27,25 +28,25 @@ const blacklist = {


/**
GraphNode()
AudioObject()
**/

const assign = Object.assign;
const define = Object.defineProperties;
const properties = {
stage: {
value: undefined
value: undefined
},

status: {
value: undefined,
writable: true
value: undefined,
writable: true
}
};

function assignId(stage, object, id) {
if (stage.find(matches({ id }))) {
throw new Error('GraphNode: stage already has object with id ' + id);
if (id && stage.find(matches({ id }))) {
throw new Error('AudioObject: stage already has object with id ' + id);
}

// TODO: Cheeky, move this to graph.js? Hmm, circular dependency if we do.
Expand All @@ -69,7 +70,7 @@ function warnNoParam(object, type, name) {
console.warn('Soundstage: dropping "' + type + '" event, node type "' + object.type + '" does not support param "' + name + '"');
}

export default function GraphNode(stage, type, id, data = {}, events = [], context, merger, transport) {
export default function AudioObject(stage, type, id, data = {}, events = [], context, merger, transport) {
// Define identity in the graph
assignId(stage, this, id);

Expand All @@ -90,23 +91,13 @@ export default function GraphNode(stage, type, id, data = {}, events = [], conte
assignSettingz__(this.node, data);
}

assign(GraphNode, {
assign(AudioObject, {
from: function(data) {
return new GraphNode(data.stage, data.type, data.id, data.node, data.events, data.context, data.merger, data.transport);
return new AudioObject(data.stage, data.type, data.id, data.node, data.events, data.context, data.merger, data.transport);
}
});

assign(GraphNode.prototype, {
find: function(fn) {
if (fn(this.node)) { return node; }
},

findAll: function(fn) {
const nodes = [];
if (fn(this.node)) { nodes.push(this.node); }
return nodes;
},

assign(AudioObject.prototype, Node.prototype, {
push: overload(get(1), {
// time, 'start', note, level
start: function(event) {
Expand Down Expand Up @@ -159,36 +150,18 @@ assign(GraphNode.prototype, {
}
}),

// pipe from tree/node, to make this broadcastable, so you can record from
// it?
// pipe: Tree.prototype.pipe

remove: function() {
const graph = this.graph;
const stage = this.stage;

// Remove connections that source or target this
graph.connections && graph.connections
.filter((connection) => connection.source === this.data || connection.target === this.data)
stage.connectors && stage.connectors
.filter((connection) => connection.source === this || connection.target === this)
.forEach((connection) => connection.remove());

// Remove controls that target this
//graph.controls && graph.controls
//.filter((control) => control.target === this.data)
//.forEach((control) => control.remove());

// Remove from nodes
remove(graph.nodes, this);

// Notify observers
//const privates = Privates(graph);
//privates.notify(graph.nodes, '');

return this;
return Node.prototype.remove.call(this);
},

stop: Stream.prototype.stop,
done: Stream.prototype.done,

toJSON: function toJSON() {
const node = this.node;
const data = {};
Expand Down
24 changes: 5 additions & 19 deletions modules/graph/connectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import overload from '../../../../fn/modules/overload.js';
import Privates from '../../../../fn/modules/privates.js';
import remove from '../../../../fn/modules/remove.js';

import Connector from './connector.js';
import { log } from '../print.js';
import Collection from '../streams/collection.js';
import Connector from './connector.js';
import { log } from '../print.js';


const assign = Object.assign;
Expand Down Expand Up @@ -37,15 +38,7 @@ export default function Connectors(nodes, connectors = []) {
log('Connectors', n + ' connections');
}

define(Connectors.prototype, {
length: {
get: function() {
let n = -1;
while (this[++n]);
return n;
}
}
});
mix(Connectors.prototype, Collection.prototype);

assign(Connectors.prototype, {
create: overload(function(){ return arguments.length; }, {
Expand All @@ -59,12 +52,5 @@ assign(Connectors.prototype, {
while (this[++n]);
return this[n] = new Connector(this, getObjectFrom(privates.nodes, src), getObjectFrom(privates.nodes, tgt), srcChan, tgtChan);
}
}),

toJSON: function() {
let n = -1;
while (this[++n]);
this.length = n;
return Array.from(this);
}
})
});
Loading

0 comments on commit ff07b81

Please sign in to comment.