Skip to content

Instantly share code, notes, and snippets.

@Korilakkuma
Created June 11, 2017 13:37
Show Gist options
  • Save Korilakkuma/5c8066ded838d2c8999b47c2ce2cbfe1 to your computer and use it in GitHub Desktop.
Save Korilakkuma/5c8066ded838d2c8999b47c2ce2cbfe1 to your computer and use it in GitHub Desktop.
Create kick sound by OscillatorNode (Refer to http://www11.plala.or.jp/sothicblue/webaudio-drum/)
window.AudioContext = window.AudioContext || window.webkitAudioContext;
const context = new AudioContext();
const eg = context.createGain();
const attack = 0;
const decay = 1;
const sustain = 1;
const release = 0.05;
let oscillator;
const intervalid = setInterval(() => {
oscillator = context.createOscillator();
oscillator.connect(eg);
eg.connect(context.destination);
const t0 = context.currentTime;
const t1 = t0 + attack;
const stopTime = t0 + release;
oscillator.frequency.cancelScheduledValues(t0);
oscillator.frequency.setValueAtTime(220, t0);
oscillator.frequency.linearRampToValueAtTime(10, stopTime);
eg.gain.cancelScheduledValues(t0);
eg.gain.setValueAtTime(0, t0);
eg.gain.linearRampToValueAtTime(1, t1);
eg.gain.setTargetAtTime(sustain, t1, decay);
eg.gain.linearRampToValueAtTime(0, release);
oscillator.start(t0);
oscillator.stop(stopTime);
}, 120);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment