Skip to content

Commit

Permalink
Assign settings accepts string unit values
Browse files Browse the repository at this point in the history
  • Loading branch information
stephband committed Oct 16, 2023
1 parent ee2f6bf commit ff3744b
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 22 deletions.
3 changes: 2 additions & 1 deletion modules/assign-settings.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

import { log, logGroup, logGroupEnd } from './print.js';
import parseValue32 from './parse/parse-value-32.js';
import { automato__, getAutomation } from './automate.js';

const DEBUG = false;
Expand All @@ -20,7 +21,7 @@ function assignSetting(node, key, value, notify) {
getAutomation(node[key]).length = 0;

// node, name, time, curve, value, duration, notify, context
automato__(node, key, node.context.currentTime, 'step', value, null, notify);
automato__(node, key, node.context.currentTime, 'step', parseValue32(value), null, notify);
}

// Or an AudioNode?
Expand Down
34 changes: 34 additions & 0 deletions modules/parse/parse-value-32.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

import toGain from '../../../fn/modules/to-gain.js';

// Be generous in what we accept, space-wise, but exclude spaces between the
// number and the unit
const runit = /\d([^\s\d]*)\s*$/;

const units = {
'': Math.fround,
'db': (value) => Math.fround(toGain(value)),
'khz': (value) => Math.fround(value * 1000),
'hz': Math.fround,
'ms': (value) => Math.fround(value / 1000),
's': Math.fround
};

export default function parseValue32(value) {
// Return 32-bit representation of number
if (typeof value === 'number') {
return Math.fround(value);
}

var entry = runit.exec(value);
if (!entry) {
throw new Error('Cannot parse value "' + value + '" (accepted units ' + Object.keys(units).join(', ') + ')');
}

const unit = entry[1].toLowerCase();
if (!units[unit]) {
throw new Error('Cannot parse value "' + string + '" (accepted units ' + Object.keys(units).join(', ') + ')');
}

return units[unit](parseFloat(value));
};
2 changes: 1 addition & 1 deletion nodes/graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ export default function NodeGraph(context, data, transport) {
privates.outputId = data.output || 'output' ;

// Create nodes
const nodes = privates.nodes = data.nodes && data.nodes.reduce(function(nodes, data) {
const nodes = privates.nodes = data.nodes && data.nodes.reduce((nodes, data) => {
nodes[data.id] = create(data.type, context, data.data, transport);
return nodes;
}, {});
Expand Down
37 changes: 17 additions & 20 deletions nodes/mix.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@

import isDefined from '../../fn/modules/is-defined.js';
import parseGain from '../modules/parse/parse-gain.js';
import NodeGraph from './graph.js';

const assign = Object.assign;

/**
Mix(context, settings)
Expand All @@ -10,18 +16,17 @@ const mix = stage.createNode('mix', {
```
**/


import NodeGraph from './graph.js';

const graph = {
nodes: [
{ id: 'pan', type: 'pan', data: { pan: 0 }}
{ id: 'pan', type: 'pan', data: { pan: 0 } }
],

connections: [
{ source: 'self', target: 'pan' }
],

gain: 1,

properties: {
/**
.gain
Expand All @@ -38,27 +43,19 @@ const graph = {
output: 'pan'
};

const settings = {};

export default class Mix extends GainNode {
constructor(context, options, transport) {
// Parse gain supplied as string eg. '0dB', or number
settings.gain = parseGain(isDefined(options.gain) ? options.gain : graph.gain);

// Init gain node
super(context, options);
super(context, settings);

// Set up the node graph
NodeGraph.call(this, context, graph, transport);
}

// Inherit from NodeGraph. We don't seem able to do this with Object.assign
// to prototype. Another stupid limitation of class syntax? Who the hell
// thought forcing class syntax on AudioNodes was a good idea?
get() {
return NodeGraph.prototype.get.apply(this, arguments);
}

connect() {
return NodeGraph.prototype.connect.apply(this, arguments);
}

disconnect() {
return NodeGraph.prototype.disconnect.apply(this, arguments);
}
}

assign(Mix.prototype, NodeGraph.prototype);

0 comments on commit ff3744b

Please sign in to comment.