Skip to content

Instantly share code, notes, and snippets.

@hcmiya
Created September 4, 2018 03:36
Show Gist options
  • Save hcmiya/1e1150282dcb08a4c916a8795e09f69c to your computer and use it in GitHub Desktop.
Save hcmiya/1e1150282dcb08a4c916a8795e09f69c to your computer and use it in GitHub Desktop.
小4丼の通知音(2018-09-04)
/* プラウザのJSコンソールに貼り付けて実行させると音が鳴る */
(function() {
const n2hz = note => 440 * Math.pow(2, (note - 57) / 12);
const ctx = new AudioContext();
const notes = [65, 72, 77];
const noteStep = 0.078;
for (let delay = 0; delay < 2; delay++) {
const stime = ctx.currentTime + 0.08 + delay * 0.29;
const gainVal = 0.024 * Math.pow(3.9, -delay);
for (let i = 0; i < notes.length; i++) {
const oc = ctx.createOscillator();
oc.type = 'square';
oc.frequency.value = n2hz(notes[i]);
const gain = ctx.createGain();
gain.gain.value = gainVal;
oc.connect(gain);
gain.connect(ctx.destination);
const ocstime = stime + i * noteStep;
oc.start(ocstime);
gain.gain.setTargetAtTime(0, ocstime, 0.09);
if (i !== notes.length - 1) {
oc.stop(ocstime + noteStep);
} else {
oc.stop(stime + 2);
}
}
}
setTimeout(1200, () => {
ctx.close();
});
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment