src/util/sound.js (view raw)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
// Short synthesized blips, so there are no audio files to ship. Silent until the
// first move, since browsers only allow audio after a user gesture.
let ctx = null;
function tone(freq, ms, gain) {
if (!ctx)
ctx = new (window.AudioContext || window.webkitAudioContext)();
const osc = ctx.createOscillator();
const vol = ctx.createGain();
osc.type = "triangle";
osc.frequency.value = freq;
vol.gain.value = gain;
osc.connect(vol).connect(ctx.destination);
const t = ctx.currentTime;
vol.gain.setValueAtTime(gain, t);
vol.gain.exponentialRampToValueAtTime(0.0001, t + ms / 1000);
osc.start(t);
osc.stop(t + ms / 1000);
}
export const sound = {
move: () => tone(220, 70, 0.05),
capture: () => tone(150, 110, 0.07),
check: () => tone(440, 130, 0.06),
end: () => {
tone(330, 160, 0.06);
setTimeout(() => tone(247, 220, 0.06), 120);
},
};
|