Skip to content

Instantly share code, notes, and snippets.

@garsaud
Created August 16, 2017 17:48
Show Gist options
  • Save garsaud/20596284dd67bae1614330c465bf3e62 to your computer and use it in GitHub Desktop.
Save garsaud/20596284dd67bae1614330c465bf3e62 to your computer and use it in GitHub Desktop.
Electronic music in javascript
// | . | . | . | . |
notes1 = '30934903403050302080473000102000';
notes2 = '90000010900100009000001090010000';
tAudio = 0;
audioCtx = new AudioContext;
scriptProcessor = audioCtx.createScriptProcessor(512, 0, 1);
scriptProcessor.connect(audioCtx.destination);
scriptProcessor.onaudioprocess = e => {
data = e.outputBuffer.getChannelData(0);
for (i = 0; i < data.length; i++)
sample = 0,
bpm = tAudio * 130 / 60,
position = bpm * 4,
// lead note
noteIndex = position | 0,
note1 = notes1[noteIndex%notes1.length],
positionInNote = bpm%.5,
data[i] = tAudio*note1*128 % 1
+ (Math.random()-.5) * Math.pow(1-positionInNote, 16)
+ (Math.random()-.5) / 1e3 * 512 * notes2[noteIndex%notes2.length] %1,
tAudio += 1 / audioCtx.sampleRate
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment