diff --git a/popup.js b/popup.js index fe6e7d6..95e8f38 100644 --- a/popup.js +++ b/popup.js @@ -1,52 +1,22 @@ let audioContext = new AudioContext(); -let oscillatorLeft; -let oscillatorRight; -let gainNode = audioContext.createGain(); -gainNode.connect(audioContext.destination); +let oscillator = audioContext.createOscillator(); -function startBinauralBeats(frequency, delta, volume) { - if (oscillatorLeft) oscillatorLeft.stop(); - if (oscillatorRight) oscillatorRight.stop(); +oscillator.connect(audioContext.destination); +oscillator.frequency.setValueAtTime(440, audioContext.currentTime); - oscillatorLeft = audioContext.createOscillator(); - oscillatorRight = audioContext.createOscillator(); +document.getElementById('togglePlayPause').addEventListener('click', async function() { + if (audioContext.state === 'suspended') { + await audioContext.resume(); + } - oscillatorLeft.frequency.setValueAtTime(frequency, audioContext.currentTime); - oscillatorRight.frequency.setValueAtTime(frequency + delta, audioContext.currentTime); - gainNode.gain.setValueAtTime(volume, audioContext.currentTime); - - oscillatorLeft.connect(gainNode); - oscillatorRight.connect(gainNode); - - oscillatorLeft.start(); - oscillatorRight.start(); -} - -document.getElementById('togglePlayPause').addEventListener('click', function() { - startBinauralBeats( - parseFloat(document.getElementById('frequencySlider').value), - parseFloat(document.getElementById('deltaSlider').value), - parseFloat(document.getElementById('volumeSlider').value) - ); - this.textContent = this.textContent === 'Play' ? 'Pause' : 'Play'; -}); - -document.getElementById('frequencySlider').addEventListener('input', function() { - startBinauralBeats( - parseFloat(this.value), - parseFloat(document.getElementById('deltaSlider').value), - parseFloat(document.getElementById('volumeSlider').value) - ); -}); - -document.getElementById('deltaSlider').addEventListener('input', function() { - startBinauralBeats( - parseFloat(document.getElementById('frequencySlider').value), - parseFloat(this.value), - parseFloat(document.getElementById('volumeSlider').value) - ); -}); - -document.getElementById('volumeSlider').addEventListener('input', function() { - gainNode.gain.setValueAtTime(parseFloat(this.value), audioContext.currentTime); + if (this.textContent === 'Play') { + oscillator.start(); + this.textContent = 'Pause'; + } else { + oscillator.stop(); + oscillator = audioContext.createOscillator(); + oscillator.connect(audioContext.destination); + oscillator.frequency.setValueAtTime(440, audioContext.currentTime); + this.textContent = 'Play'; + } });