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/8927408 to your computer and use it in GitHub Desktop.

Select an option

Save srikumarks/8927408 to your computer and use it in GitHub Desktop.
A Steller SoundModel maker for the simple example in http://blog.teamtreehouse.com/building-a-synthesizer-with-the-web-audio-api
// For completeness, I include the steller API object and the current audio context as arguments.
function theremin(steller, sh) {
var osc = null;
var gain = sh.audioContext.createGain();
var sm = steller.SoundModel({}, [], [gain]);
sm.start = sh.fire(function (clock) {
if (osc) { osc.stop(clock.t1); }
osc = AC.createOscillator();
osc.frequency.value = sm.frequency.value;
osc.connect(gain);
osc.start(clock.t1);
});
sm.stop = sh.fire(function (clock) {
if (osc) {
osc.stop(clock.t1);
osc = null;
}
});
sm.gain = steller.Param({min: 0.01, max: 1.0, value: 0.25, audioParam: gain.gain, mapping: 'log'});
sm.frequency = steller.Param({min: 261.63, max: 493.88, value: 440.0, mapping: 'log'});
sm.frequency.watch(function (v) {
if (osc) {
osc.frequency.value = v;
}
});
return sm;
}
// Usage
var sh = new steller.Scheduler(new steller.AudioContext);
var th = theremin(steller, sh).connect(); // connect by default to the audio context's destination.
th.frequency.value = 220;
sh.play(sh.track(th.start, sh.delay(0.5), th.stop));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment