Skip to content

Instantly share code, notes, and snippets.

@Korilakkuma
Last active September 8, 2019 08:42
Show Gist options
  • Save Korilakkuma/2b4404226abf84e964d1dc6e582ee8c3 to your computer and use it in GitHub Desktop.
Save Korilakkuma/2b4404226abf84e964d1dc6e582ee8c3 to your computer and use it in GitHub Desktop.
// Safari は, ベンダープレフィックスが必要
window.AudioContext = window.AudioContext || window.webkitAudioContext;
const audiocontext = new AudioContext();
// Autoplay Policy 対策
document.addEventListener('click', async () => {
if (audiocontext.state !== 'running') {
await audiocontext.resume();
}
// Create the instance of `AudioNode`
const oscillator = audiocontext.createOscillator();
const delay = audiocontext.createDelay(5);
// `OscillatorNode` -> `AudioDestinationNode`
oscillator.connect(audiocontext.destination);
// `OscillatorNode` -> `DelayNode` -> `AudioDestinationNode`
oscillator.connect(delay);
delay.connect(audiocontext.destination);
// `AudioParam`
delay.delayTime.value = 5;
// Immediate pronounce
oscillator.start(0);
// Stop after 1 sec
oscillator.stop(audiocontext.currentTime + 1);
}, false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment