Skip to content

Instantly share code, notes, and snippets.

@srikumarks
Last active August 29, 2015 13:56
Show Gist options
  • Select an option

  • Save srikumarks/8897741 to your computer and use it in GitHub Desktop.

Select an option

Save srikumarks/8897741 to your computer and use it in GitHub Desktop.
Example of using Steller's GraphNodeSet's graph serialization/deserialization.
define('node_types', ['steller'], function (S) {
return {
sine: function (freq) {
var osc = this.audioContext.createOscillator();
osc.frequency.value = freq;
osc.start(0);
return S.SoundModel(this, [], [osc]);
},
gain: function (multiplier) {
var gain = this.audioContext.createGain();
gain.gain.value = multiplier;
return S.SoundModel(this, [gain], [gain]);
}
};
});
// Make a dead simple graph and write it as a JSON string to the console.
// This kind of code may be part of a GUI for connecting and playing
// with the types of nodes defined in 'node_types'.
require(['steller', 'node_types'], function (S, node_types) {
var AC = new S.AudioContext;
var gns = new S.GraphNodeSet(AC, node_types);
var sineOsc = gns.node('sine', 880.0);
var gain = gns.node('gain', 0.25);
sineOsc.connect(gain);
var modelSpec = gns.saveAsModel('oscil', [], [{node: gain}]);
console.log(JSON.stringify(modelSpec));
gain.connect(AC.destination);
// Plays a sine tone at 880Hz and prints -
// {"type":"SoundModel","nodes":[{"id":"1","setup":[{"fn":"node","args":["sine",880]},{"fn":"connect","args":[2]}]},{"id":"2","setup":[{"fn":"node","args":["gain",0.25]}]},{"id":"destination","setup":[]}],"name":"oscil","inputs":[],"outputs":[{"node":2}],"params":[]}
// on the console. I leave out the connection between the gain and the destination
// from the captured model because we don't want that connection happening every time
// an 'oscil' is instantiated.
});
// Loads the dead simple graph printed by the previous module.
// Note that both the loading and saving modules share the
// same 'node_types' definitions.
require(['steller', 'node_types'], function (S, node_types) {
var AC = new S.AudioContext;
var gns = new S.GraphNodeSet(AC, node_types);
gns.loadDefinitions([{"type":"SoundModel","nodes":[{"id":"1","setup":[{"fn":"node","args":["sine",880]},{"fn":"connect","args":[2]}]},{"id":"2","setup":[{"fn":"node","args":["gain",0.25]}]},{"id":"destination","setup":[]}],"name":"oscil","inputs":[],"outputs":[{"node":2}],"params":[]}]);
var oscil = gns.node('oscil');
oscil.connect(AC.destination);
// Plays the same sine tone.
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment