# The Official Reverberation! **Made by Yeoh Tian Huai** This will probably be the last commit before I can properly make more changes so I will add the code here: Main.js file ``` import "./style.css"; var frequencyFetcher = document.getElementById("frequency"); var frequencyDisplay = document.getElementById("frequencyDisplay"); var amplitudeFetcher = document.getElementById("amplitude"); var amplitudeDisplay = document.getElementById("amplitudeDisplay"); var hideGraph = document.getElementById("hideGraph"); var showGraph = document.getElementById("showGraph"); showGraph.classList.add("hidden"); hideGraph.addEventListener("click", function(){ hideGraph.classList.add("hidden"); amplitudeFetcher.classList.add("hidden"); frequencyFetcher.classList.add("hidden"); canvas.classList.add("hidden"); showGraph.classList.remove("hidden"); window.setTimeout(function() { window.dispatchEvent(new Event('resize')); }, 10); }); showGraph.addEventListener("click", function(){ showGraph.classList.add("hidden"); amplitudeFetcher.classList.remove("hidden"); frequencyFetcher.classList.remove("hidden"); canvas.classList.remove("hidden"); hideGraph.classList.remove("hidden"); window.setTimeout(function() { window.dispatchEvent(new Event('resize')); }, 10); }); const canvas = document.getElementById("graph"); const ctx = canvas.getContext("2d"); frequencyDisplay.innerHTML = frequencyFetcher.value; var frequency = frequencyFetcher.value; var amplitude = amplitudeFetcher.value/100; var audioContext = null; var source = null; var isSource = false; frequencyFetcher.oninput = function() { frequencyDisplay.value = this.value; frequency = this.value; redrawCanvas(); if(isSource === true){ playSound(); } }; frequencyDisplay.oninput = function(){ frequencyFetcher.value = this.value; frequency = this.value; redrawCanvas(); if(isSource === true){ playSound(); } }; amplitudeDisplay.innerHTML = amplitudeFetcher.value; amplitudeDisplay.oninput = function(){ amplitudeFetcher.value = this.value; amplitude = this.value/100; redrawCanvas(); if(isSource === true){ playSound(); } }; amplitudeFetcher.oninput = function() { amplitudeDisplay.value = this.value; amplitude = this.value/100; redrawCanvas(); if(isSource === true){ playSound(); } }; function redrawCanvas() { var duration = 0.02; var sr = 44100; const wave = sine_wave(amplitude, frequency, duration, sr); canvas.width = window.innerWidth; canvas.height = 200; ctx.clearRect(0, 0, canvas.width, canvas.height); ctx.beginPath(); ctx.moveTo(0, canvas.height / 2); for (let i = 0; i < wave.length; i++) { const x = (canvas.width * i) / wave.length; const y = (canvas.height / 2) - wave[i] * (canvas.height / 2); ctx.lineTo(x, y); } ctx.strokeStyle = "#EEEEEE"; ctx.lineWidth = 5; ctx.stroke(); } function sine_wave(amplitude, frequency, duration, sr) { const samples = duration * sr; const wave = []; for (let i = 0; i < samples; i++) { const sample = amplitude * Math.sin((2 * Math.PI * frequency * i) / sr); wave.push(sample); } return wave; } function playSound() { if (!audioContext) { audioContext = new (window.AudioContext || window.webkitAudioContext)(); } if (source) { source.stop(); source.disconnect(); } var duration = 100; var sr = 44100; var samples = duration * sr; var wave = sine_wave(amplitude, frequency, duration, sr); var buffer = audioContext.createBuffer(1, samples, sr); var data = buffer.getChannelData(0); for (let i = 0; i < samples; i++) { data[i] = wave[i]; } source = audioContext.createBufferSource(); source.buffer = buffer; source.connect(audioContext.destination); source.start(); isSource = true; } function stopSound() { if (source) { source.stop(); source.disconnect(); source = null; isSource = false; } } document.getElementById('playButton').addEventListener('click', playSound); document.getElementById('stopButton').addEventListener('click', stopSound); redrawCanvas(); ``` Style.css ``` body{ background-color: var(--backGround); } #root { max-width: 1280px; margin: 0 auto; padding: 2rem; text-align: center; } :root{ --backGround: #222831; } h1{ font-family: avenir; font-size: 4lvw; color: #00ADB5; } p{ color: #00ADB5; } .slider { -webkit-appearance: none; width: 100%; height: 15px; border-radius: 5px; background: #393E46; outline: none; opacity: 0.7; -webkit-transition: .2s; transition: opacity .2s; } .slider:hover { opacity: 1; } .slider::-webkit-slider-thumb { -webkit-appearance: none; appearance: none; width: 25px; height: 25px; border-radius: 50%; background: #00ADB5; cursor: pointer; } .slider::-moz-range-thumb { width: 25px; height: 25px; border-radius: 50%; background: #00ADB5; cursor: pointer; } .numberOption{ display: inline-block; width: 8lvw; padding: 2lvw; border: 0.5lvw solid #393E46; background-color: #222831; color: #00ADB5; border-radius: 1lvw; text-align: center; font-size: 4lvw; } .soundButtons{ font-size: 15lvw; background-color: var(--backGround); border: none; } .visibleButtons{ font-size: 7lvw; background-color: var(--backGround); border: none; color: #EEEEEE; } #feedbackForm{ font-size: 5lvw; text-align: center; } .inputFormFields{ font-size: 4lvw; background-color: #393E46; color: #00ADB5; text-align: center; margin: 1vw; } .hidden { height: 0; width: 0; overflow: hidden; } #graph{ width: 100%; } #graph2{ width: 100%; } ``` Index.html ``` Reverberation <<<<<<< HEAD ======= >>>>>>> origin/main

Frequency: hertz

Amplitude: %


Frequency2: hertz

Amplitude2: %

Try the metronome!

Beats Per Second (BPS):

Both sine waves together now!

Try and hear the beating effect if you play any 2 waves with a difference in frequency of less then or equal to 6, like for example, 440 and 444, together!

What is the metronome for?
Hint: 444 - 440 = 4

<<<<<<< HEAD ======= >>>>>>> origin/main

Developer: Yeoh TH (Tian Huai)







``` wave02.js ``` import "./style.css"; var frequencyFetcher = document.getElementById("frequency2"); var frequencyDisplay = document.getElementById("frequencyDisplay2"); var amplitudeFetcher = document.getElementById("amplitude2"); var amplitudeDisplay = document.getElementById("amplitudeDisplay2"); var hideGraph = document.getElementById("hideGraph2"); var showGraph = document.getElementById("showGraph2"); showGraph.classList.add("hidden"); hideGraph.addEventListener("click", function(){ hideGraph.classList.add("hidden"); amplitudeFetcher.classList.add("hidden"); frequencyFetcher.classList.add("hidden"); canvas.classList.add("hidden"); showGraph.classList.remove("hidden"); window.setTimeout(function() { window.dispatchEvent(new Event('resize')); }, 10); }); showGraph.addEventListener("click", function(){ showGraph.classList.add("hidden"); amplitudeFetcher.classList.remove("hidden"); frequencyFetcher.classList.remove("hidden"); canvas.classList.remove("hidden"); hideGraph.classList.remove("hidden"); window.setTimeout(function() { window.dispatchEvent(new Event('resize')); }, 10); }); const canvas = document.getElementById("graph2"); const ctx = canvas.getContext("2d"); frequencyDisplay.innerHTML = frequencyFetcher.value; var frequency = frequencyFetcher.value; var amplitude = amplitudeFetcher.value/100; var audioContext; var source = null; var isSource = false; frequencyFetcher.oninput = function() { frequencyDisplay.value = this.value; frequency = this.value; redrawCanvas(); if(isSource === true){ playSound(); } }; frequencyDisplay.oninput = function(){ frequencyFetcher.value = this.value; frequency = this.value; redrawCanvas(); if(isSource === true){ playSound(); } }; amplitudeDisplay.innerHTML = amplitudeFetcher.value; amplitudeDisplay.oninput = function(){ amplitudeFetcher.value = this.value; amplitude = this.value/100; redrawCanvas(); if(isSource === true){ playSound(); } }; amplitudeFetcher.oninput = function() { amplitudeDisplay.value = this.value; amplitude = this.value/100; redrawCanvas(); if(isSource === true){ playSound(); } }; function redrawCanvas() { var duration = 0.02; var sr = 44100; const wave = sine_wave(amplitude, frequency, duration, sr); canvas.width = window.innerWidth; canvas.height = 200; ctx.clearRect(0, 0, canvas.width, canvas.height); ctx.beginPath(); ctx.moveTo(0, canvas.height / 2); for (let i = 0; i < wave.length; i++) { const x = (canvas.width * i) / wave.length; const y = (canvas.height / 2) - wave[i] * (canvas.height / 2); ctx.lineTo(x, y); } ctx.strokeStyle = "#EEEEEE"; ctx.lineWidth = 5; ctx.stroke(); } function sine_wave(amplitude, frequency, duration, sr) { const samples = duration * sr; const wave = []; for (let i = 0; i < samples; i++) { const sample = amplitude * Math.sin((2 * Math.PI * frequency * i) / sr); wave.push(sample); } return wave; } function playSound() { if (!audioContext) { audioContext = new (window.AudioContext || window.webkitAudioContext)(); } if (source) { source.stop(); source.disconnect(); } var duration = 100; var sr = 44100; var samples = duration * sr; var wave = sine_wave(amplitude, frequency, duration, sr); var buffer = audioContext.createBuffer(1, samples, sr); var data = buffer.getChannelData(0); for (let i = 0; i < samples; i++) { data[i] = wave[i]; } source = audioContext.createBufferSource(); source.buffer = buffer; source.connect(audioContext.destination); source.start(); isSource = true; } function stopSound() { if (source) { source.stop(); source.disconnect(); source = null; isSource = false; } } document.getElementById('playButton2').addEventListener('click', playSound); document.getElementById('stopButton2').addEventListener('click', stopSound); redrawCanvas(); ``` Second graphBoth.js ``` import './style.css'; var audioContext = new AudioContext(); var playButton = document.getElementById("playButton3"); var stopButton = document.getElementById("stopButton3"); var audioBuffer; var source; var intervalId; function loadSound(url) { return fetch(url) .then(response => response.arrayBuffer()) .then(buffer => audioContext.decodeAudioData(buffer)) .then(decodedData => { audioBuffer = decodedData; }); } function playSound() { if (!audioBuffer) { return; } var bps = document.getElementById("bpsInput").value; var interval = 1 / bps * 1000; stopSound(); source = audioContext.createBufferSource(); source.buffer = audioBuffer; source.connect(audioContext.destination); source.start(); intervalId = setInterval(function() { source.stop(); source.disconnect(); source = audioContext.createBufferSource(); source.buffer = audioBuffer; source.connect(audioContext.destination); source.start(); }, interval); } function stopSound() { if (source) { clearInterval(intervalId); source.stop(); source.disconnect(); } } loadSound("./metronomeWood.wav") .then(function() { console.log("Sound file loaded successfully."); }) .catch(function(error) { console.log("Error loading sound file:", error); }); playButton.addEventListener("click", playSound); stopButton.addEventListener("click", stopSound); ``` MIT License Copyright (c) 2025 YeohTH Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ### thats all folks!