The Simple Web (Copy 14436)
✨ NUT THAIWATTANANON
<button onclick="osci.start()"> First Time </button> <button onclick="gain.gain.value = 0"> Shhhhhhhh </button> <button onclick="gain.gain.value = 0.5"> Booom!! </button> <p> </p> <input type="range" max="0.5" min="0" step="0.1" value="0" onclick="change(this)"> <input type="range" max="3" min="0" step="1" value="0" onclick="changetype(this)"> <p> </p> <button onclick="note(261)"> C </button> <button onclick="note(293)"> D </button> <button onclick="note(329)"> E </button> <button onclick="note(349)"> F </button> <button onclick="note(391)"> G </button> <button onclick="note(440)"> A </button> <button onclick="note(523)"> B </button> <script> // window.addEventListener("keydown", checkKeyPress, false); audio = new(window.AudioContext || window.webkitAudioContext)() //สร้าง osci กับ gain osci = audio.createOscillator() gain = audio.createGain() //เชื่อม osci->gain->audio destination osci.connect(gain) gain.connect(audio.destination) //เชื่อม // osci.start() // osci.frequency.setValueAtTime(260,audio.currentTime) // osci.frequency.value = 500 // ได้ผลเหมือนข้างบน โดยจะเปลี่ยนทันที // osci.type = 'sine' // osci.disconnect(audio.destination) gain.gain.value = 0; function change(ele){ gain.gain.value = ele.value; } function changetype(ele1){ y = ele1.value if(y == 0){ osci.type = 'sine'; } if(y == 1){ osci.type = 'square'; } if(y == 2){ osci.type = 'triangle'; } if(y == 3){ osci.type = 'sawtooth'; } } function note(x) { osci.frequency.setValueAtTime(x, audio.currentTime); gain.gain.value = 0.4; gain.gain.linearRampToValueAtTime(0, audio.currentTime + 0.5); } document.body.onkeydown = function(event) { y = event.key; console.log(y) if (y == "a") { note(261); } if (y == "s") { note(293) } if (y == "d") { note(329) } if (y == "f") { note(349) } if (y == "g") { note(391) } if (y == "h") { note(440) } if (y == "j") { note(523) } } </script>