Skip to content

Instantly share code, notes, and snippets.

@NHQ
Last active August 29, 2015 14:16
Show Gist options
  • Save NHQ/cd30b9503e35f13ec5cd to your computer and use it in GitHub Desktop.
Save NHQ/cd30b9503e35f13ec5cd to your computer and use it in GitHub Desktop.
var asynth = require('../');
var jynth = require('jynth');
var synth = jynth();
var recording = true;
var playing = true;
var samples = [];
process.stdin.setRawMode(true);
process.stdin.on('data', function (buf) {
if (buf[0] === 3) process.exit();
if (buf[0] === ' '.charCodeAt(0)) {
recording = !recording;
console.log('recording ' + recording);
if (recording) samples = [];
}
if (buf[0] === 'p'.charCodeAt(0)) {
playing = !playing;
console.log('playing ' + playing);
}
});
process.stdin.resume();
var notes = {};
var s = asynth(function (note, t) {
var freq = 440 * Math.pow(2, (note.key - 49) / 12);
if (!notes[note.key]) {
notes[note.key] = true;
setTimeout(function () {
notes[note.key] = false;
}, 8000);
samples.splice(0);
}
var x = synth(t)
.sine(0.1, freq)
.amod(freq/200,freq/100,3)
.sample
;
var y = synth(t)
.sine(1, freq)
.sample
;
var value = x + y;
if (recording) samples.push(value);
if (samples.length > 44000 * 16) samples.splice(0, 44000);
return value;
});
s.push(function (t, i) {
if (!playing) return 0;
return samples[i % samples.length];
});
s.play();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment