Skip to content

Commit

Permalink
Refactoring, adds import map
Browse files Browse the repository at this point in the history
  • Loading branch information
stephband committed Sep 9, 2024
1 parent 47c284e commit 6ba3c16
Show file tree
Hide file tree
Showing 14 changed files with 77 additions and 46 deletions.
8 changes: 8 additions & 0 deletions deno.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"imports": {
"dom/": "https://cdn.jsdelivr.net/gh/stephband/dom@master/modules/",
"fn/": "https://cdn.jsdelivr.net/gh/stephband/fn@master/modules/",
"bolt/": "https://cdn.jsdelivr.net/gh/stephband/bolt-2@main/",
"literal/": "https://cdn.jsdelivr.net/gh/stephband/literal@main/"
}
}
10 changes: 5 additions & 5 deletions modules/assign-settings.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

import { log, logGroup, logGroupEnd } from './print.js';
import { log, group, groupEnd } from './print.js';
import { automate, getAutomation } from './param.js';

const DEBUG = false;
Expand Down Expand Up @@ -36,7 +36,7 @@ function assignSetting(node, key, value, notify) {
}

export function assignSettings(node, defaults, settings, ignored) {
if (DEBUG) { logGroup('assign', node.constructor.name, (settings ? Object.keys(settings).join(', ') : '')); }
if (DEBUG) { group('assign', node.constructor.name, (settings ? Object.keys(settings).join(', ') : '')); }

const keys = {};

Expand Down Expand Up @@ -67,11 +67,11 @@ export function assignSettings(node, defaults, settings, ignored) {
}
}

if (DEBUG) { logGroupEnd(); }
if (DEBUG) { groupEnd(); }
}

export function assignSettingz__(node, settings, ignored) {
if (DEBUG) { logGroup('assign', node.constructor.name, (settings ? Object.keys(settings).join(', ') : '')); }
if (DEBUG) { group('assign', node.constructor.name, (settings ? Object.keys(settings).join(', ') : '')); }

var key;

Expand All @@ -89,7 +89,7 @@ export function assignSettingz__(node, settings, ignored) {
}
}

if (DEBUG) { logGroupEnd(); }
if (DEBUG) { groupEnd(); }

return node;
}
2 changes: 1 addition & 1 deletion modules/mixins/playable.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const json = JSON.stringify(playable); // {}
```
**/

import { logGroup, logGroupEnd } from '../print.js';
import { group, groupEnd } from '../print.js';

const DEBUG = window.DEBUG;
const assign = Object.assign;
Expand Down
4 changes: 2 additions & 2 deletions modules/print.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const log = window.console ?
} :
noop ;

export const logGroup = window.console ?
export const group = window.console ?
function(name, message, text = '', ...args) {
if (typeof text === 'string') {
text = truncate(48, text);
Expand All @@ -40,6 +40,6 @@ export const logGroup = window.console ?
} :
noop ;

export const logGroupEnd = window.console ?
export const groupEnd = window.console ?
console.groupEnd.bind(console) :
noop ;
7 changes: 4 additions & 3 deletions modules/request/request-buffer.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { print as log } from '../print.js';
import { log } from '../print.js';

// Fetch audio buffer from a URL and decode it

var requests = {};

export function requestBuffer(context, url) {
return requests[url] || (requests[url] = new Promise(function(accept, reject) {
return requests[url] || (requests[url] = new Promise((resolve, reject) => {
if (window.DEBUG) { log('Loading', 'buffer', url); }
var request = new XMLHttpRequest();
request.open('GET', url, true);
request.responseType = 'arraybuffer';
request.onload = () => context.decodeAudioData(request.response, accept, reject);
request.onload = () => context.decodeAudioData(request.response, resolve, reject);
request.onerror = reject;
request.send();
}));
Expand Down
4 changes: 3 additions & 1 deletion modules/request/request-data.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { print as log } from '../print.js';
import { log } from '../print.js';
import get from '../../../fn/modules/get.js';

export function requestData(url) {
if (window.DEBUG) { log('Loading', 'data', url); }

return url.slice(-3) === '.js' ?
// Import JS module
import(url).then(get('default')) :
Expand Down
1 change: 0 additions & 1 deletion modules/sequencer/play-head.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,6 @@ assign(PlayHead.prototype, {
let n = -1, event;
while (event = buffer[++n]) distribute(this, this.target, event, b2);
buffer.length = 0;

return buffer;
},

Expand Down
32 changes: 21 additions & 11 deletions modules/test/index.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
<!DOCTYPE html>
<!-- Literal template "/Users/stephband/Sites/cruncher/editor.html.literal" -->

<html lang="en">
<head>
<title>Test</title>
Expand Down Expand Up @@ -32,7 +30,7 @@ <h1>Test Soundstage</h1>
import Soundstage from '../soundstage.js';

const stage = Soundstage.from({
objects: [{
objects: [/*{
id: 1,
type: 'tick',
node: {},
Expand All @@ -42,7 +40,7 @@ <h1>Test Soundstage</h1>
[2, 'note', 240, 0.25, 0.2],
[3, 'note', 240, 0.25, 0.2],
]
}, {
}, */{
id: 2,
type: 'instrument',
node: {
Expand All @@ -64,13 +62,23 @@ <h1>Test Soundstage</h1>

properties: {},
output: 'mix'
}
},

gain: 1
},
events: [
[0, 'note', 320, 0.5, 0.2],
[1, 'note', 240, 0.25, 0.2],
[2, 'note', 240, 0.25, 0.2],
[3, 'note', 240, 0.25, 0.2],
[0, 'note', 42, 0.75, 0.2],
[1, 'note', 49, 0.75, 0.2],
[2, 'note', 49, 0.75, 0.2],
[3, 'note', 49, 0.75, 0.2],
[4, 'note', 42, 0.75, 0.2],
[5, 'note', 49, 0.75, 0.2],
[6, 'note', 49, 0.75, 0.2],
[7, 'note', 49, 0.75, 0.2],
[8, 'note', 42, 0.75, 0.2],
[9, 'note', 49, 0.75, 0.2],
[10, 'note', 49, 0.75, 0.2],
[11, 'note', 49, 0.75, 0.2],
]
}, {
id: 'output',
Expand All @@ -81,7 +89,7 @@ <h1>Test Soundstage</h1>
}],

connectors: [
[1, 'output'],
//[1, 'output'],
[2, 'output']
]
});
Expand Down Expand Up @@ -131,7 +139,9 @@ <h1>Test Soundstage</h1>
window.stage = stage;

events('click', document.getElementById('start')).each((e) => {
stage.start(context.currentTime + 0.4).stop(context.currentTime + 5);
stage
.start(context.currentTime + 0.4)
.stop(context.currentTime + 15);
});
</script>
</body>
Expand Down
6 changes: 3 additions & 3 deletions nodes/graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ output by the `.connect()` and `.disconnect()` methods.
**/

import Privates from '../../fn/modules/privates.js';
import { logGroup, logGroupEnd } from '../modules/print.js';
import { group, groupEnd } from '../modules/print.js';
import { connect, disconnect } from '../modules/connect.js';
import constructors from '../modules/graph/constructors.js';
import Sink from './sink.js';
Expand Down Expand Up @@ -136,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 (DEBUG) { group('mixin ', 'GraphNode', data.nodes && data.nodes.map((n) => n.type).join(', ')); }

const privates = Privates(this);
privates.outputId = data.output || 'output' ;
Expand Down Expand Up @@ -206,7 +206,7 @@ export default function NodeGraph(context, data, transport) {
seal(nodes);
data.connections && data.connections.reduce(createConnection, nodes);

if (DEBUG) { logGroupEnd(); }
if (DEBUG) { groupEnd(); }
}

assign(NodeGraph, { types });
Expand Down
16 changes: 7 additions & 9 deletions nodes/instrument.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ AudioParam of the child node `'filter'`.

import isDefined from '../../fn/modules/is-defined.js';
import Privates from '../../fn/modules/privates.js';
import { logGroup, logGroupEnd } from './print.js';
import { group, groupEnd } from './print.js';
import Voice, { defaults as voiceDefaults } from './voice.js';
import NodeGraph from './graph.js';
import Pool from '../modules/pool.js';
Expand Down Expand Up @@ -101,7 +101,7 @@ function isIdle(node) {

export default class Instrument extends GainNode {
constructor(context, settings, transport) {
if (DEBUG) { logGroup(new.target === Instrument ? 'Node' : 'mixin ', 'Instrument'); }
if (DEBUG) { group(new.target === Instrument ? 'Node' : 'mixin ', 'Instrument'); }

// Init gain node
super(context, settings);
Expand Down Expand Up @@ -145,7 +145,7 @@ export default class Instrument extends GainNode {
// TODO: if this includes voice property, this.voice is overridden, not good
assignSettingz__(this, defaults);

if (DEBUG) { logGroupEnd(); }
if (DEBUG) { groupEnd(); }
}
}

Expand All @@ -165,16 +165,14 @@ assign(Instrument.prototype, NodeGraph.prototype, {
```
**/

start: function(time, note, velocity = 1) {
start: function(time, note, gain = 1) {
if (!isDefined(note)) {
throw new Error('Attempt to .start() a note without passing a note name.')
}

const privates = Privates(this);

return privates.voices
.create(this.context, this.voice)
.start(time, note, velocity);
return Privates(this).voices
.create(this.context, this.voice)
.start(time, note, gain);
},

/**
Expand Down
6 changes: 3 additions & 3 deletions nodes/looper.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const looper = stage.createNode('looper', {
Creates a node that records and loops audio.
**/

import { print, logGroup, logGroupEnd } from './print.js';
import { print, group, groupEnd } from './print.js';
import Privates from '../../fn/modules/privates.js';
import NodeGraph from './graph.js';
import Playable from './play-node.js';
Expand Down Expand Up @@ -82,7 +82,7 @@ const properties = {

export default class Looper extends GainNode {
constructor(context, settings, transport) {
if (DEBUG) { logGroup(new.target === Looper ? 'Node' : 'mixin ', 'Looper'); }
if (DEBUG) { group(new.target === Looper ? 'Node' : 'mixin ', 'Looper'); }

// Init gain node
super(context, settings);
Expand Down Expand Up @@ -120,7 +120,7 @@ export default class Looper extends GainNode {
// Connect sources to output
this.sources.forEach((source) => source.connect(this.get('wet')));

if (DEBUG) { logGroupEnd(); }
if (DEBUG) { groupEnd(); }
}
}

Expand Down
4 changes: 2 additions & 2 deletions nodes/print.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ export const log = window.console ?
function(name, message, ...args) { console.log('%c' + name + ' %c' + message, 'color: #b5002f; font-weight: 600;', 'color: #8e9e9d; font-weight: 300;', ...args); } :
noop ;

export const logGroup = window.console ?
export const group = window.console ?
function(name, message, ...args) { console.groupCollapsed('%c' + name + ' %c' + message, 'color: #b5002f; font-weight: 600;', 'color: #8e9e9d; font-weight: 300;', ...args); } :
noop ;

export const logGroupEnd = window.console ?
export const groupEnd = window.console ?
console.groupEnd.bind(console) :
noop ;
17 changes: 15 additions & 2 deletions nodes/sample-set.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import { assignSettingz__ } from '../modules/assign-settings.js';
import { fadeInFromTime, fadeOutToTime, releaseAtTime } from '../modules/param.js';
import NodeGraph from './graph.js';
import Playable from '../modules/mixins/playable.js';
import { log, group, groupEnd } from '../modules/print.js';

const DEBUG = window.DEBUG;
const assign = Object.assign;
Expand Down Expand Up @@ -89,11 +90,21 @@ const properties = {
privates.src = src;
privates.regions = sampleMap.data;

if (window.DEBUG) { console.log(); }

group('Loading', 'samples');

// Populate buffer cache with buffer data
return Promise.all(privates.regions.map((region) =>
requestBuffer(context, region.src)
.then((buffer) => cache[region.src] = buffer)
));

groupEnd();
})
.then((o) => { ;
console.log('Loaded');
return o;
})
.catch((e) => {
throw new Error('Sample src "' + src + '" not found. ' + e.message);
Expand Down Expand Up @@ -213,7 +224,7 @@ function startSources(sources, destination, detuneNode, map, time, frequency = 4

sources.length = 0;

console.log('NOTE ----------');
//console.log('NOTE ----------');

// Neuter velocity 0s - they dont seem to get filtered below
return gain === 0 ? sources : map
Expand Down Expand Up @@ -242,7 +253,7 @@ console.log('NOTE ----------');
source.region = region;
source.bufferNode = setupBufferNode(context, source, detuneNode, region, buffer, time, frequency);

console.log('START', gain, region.src);
console.log('Sample', time, frequency, gain, region.src);

startSource(context, source, source.bufferNode, region.attack, time);

Expand Down Expand Up @@ -287,6 +298,7 @@ export default function Sample(context, settings, transport) {

// Setup
Sample.reset(this, arguments);
Sample.load();
}

Sample.reset = function reset(node, args) {
Expand All @@ -299,6 +311,7 @@ assign(Sample.prototype, Playable.prototype, NodeGraph.prototype, {
start: function(time, frequency = defaults.frequency, gain = defaults.gain) {
// Wait for src and buffers to load
if (this.promise) {
console.log('Sample loading, note promised', time, frequency, gain);
this.promise.then(() => this.start(time, frequency, gain));
return this;
}
Expand Down
6 changes: 3 additions & 3 deletions nodes/track.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { logGroup, logGroupEnd } from './print.js';
import { group, groupEnd } from './print.js';
import Privates from '../../fn/modules/privates.js';
import NodeGraph from './graph.js';
import Recorder from './recorder.js';
Expand Down Expand Up @@ -43,7 +43,7 @@ const properties = {

export default class Track extends GainNode {
constructor(context, settings, transport) {
if (DEBUG) { logGroup(new.target === Track ? 'Node' : 'mixin ', 'Track'); }
if (DEBUG) { group(new.target === Track ? 'Node' : 'mixin ', 'Track'); }

// Init gain node
super(context, settings);
Expand All @@ -66,7 +66,7 @@ export default class Track extends GainNode {
this.wet = this.get('wet').gain;
this.sources = [];

if (DEBUG) { logGroupEnd(); }
if (DEBUG) { groupEnd(); }
}
}

Expand Down

0 comments on commit 6ba3c16

Please sign in to comment.