16 lines
790 B
JavaScript
16 lines
790 B
JavaScript
|
|
window.addEventListener("load", (event) => {
|
||
|
|
for (const elt of document.getElementsByClassName("mt-sound-play-button")) {
|
||
|
|
elt.addEventListener("click", (event) => {
|
||
|
|
const audioCtx = new (window.AudioContext || window.webkitAudioContext)();
|
||
|
|
for (const freq of event.target.getAttribute("data-sound-info").split(",")) {
|
||
|
|
const oscillator = audioCtx.createOscillator();
|
||
|
|
oscillator.type = "sine"; // waveform: sine, square, sawtooth, triangle
|
||
|
|
oscillator.frequency.value = parseInt(freq); // Hz
|
||
|
|
oscillator.connect(audioCtx.destination);
|
||
|
|
oscillator.start();
|
||
|
|
oscillator.stop(audioCtx.currentTime + 2); // stop after 1 second
|
||
|
|
}
|
||
|
|
});
|
||
|
|
}
|
||
|
|
});
|