[WIP] Add ability to play sound
Signed-off-by: Danila Fedorin <danila.fedorin@gmail.com>
This commit is contained in:
15
content/blog/music_theory/playsound.js
Normal file
15
content/blog/music_theory/playsound.js
Normal file
@@ -0,0 +1,15 @@
|
||||
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
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user