From 404fbe8701c48fe3abeabd8fc662c08c9c8e470f Mon Sep 17 00:00:00 2001 From: pliny <133052465+elder-plinius@users.noreply.github.com> Date: Sat, 12 Aug 2023 18:11:33 -0700 Subject: [PATCH] Update popup.js --- popup.js | 98 +++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 83 insertions(+), 15 deletions(-) diff --git a/popup.js b/popup.js index 95e8f38..26c4e3d 100644 --- a/popup.js +++ b/popup.js @@ -1,22 +1,90 @@ -let audioContext = new AudioContext(); -let oscillator = audioContext.createOscillator(); +let audioContext = new (window.AudioContext || window.webkitAudioContext)(); +let oscillatorLeft, oscillatorRight, gainNode; +let isPlaying = false; -oscillator.connect(audioContext.destination); -oscillator.frequency.setValueAtTime(440, audioContext.currentTime); +function startBinauralBeats() { + oscillatorLeft = audioContext.createOscillator(); + oscillatorRight = audioContext.createOscillator(); + gainNode = audioContext.createGain(); -document.getElementById('togglePlayPause').addEventListener('click', async function() { - if (audioContext.state === 'suspended') { - await audioContext.resume(); + oscillatorLeft.type = 'sine'; + oscillatorRight.type = 'sine'; + + updateFrequencies(); + updateVolume(); + + oscillatorLeft.connect(gainNode); + oscillatorRight.connect(gainNode); + gainNode.connect(audioContext.destination); + + oscillatorLeft.start(); + oscillatorRight.start(); + + isPlaying = true; +} + +function stopBinauralBeats() { + oscillatorLeft.stop(); + oscillatorRight.stop(); + isPlaying = false; +} + +function updateFrequencies() { + let frequency = parseFloat(document.getElementById('frequencySlider').value); + let delta = parseFloat(document.getElementById('deltaSlider').value); + oscillatorLeft.frequency.setValueAtTime(frequency, audioContext.currentTime); + oscillatorRight.frequency.setValueAtTime(frequency + delta, audioContext.currentTime); +} + +function updateVolume() { + let volume = parseFloat(document.getElementById('volumeSlider').value); + gainNode.gain.setValueAtTime(volume, audioContext.currentTime); +} + +function randomizeValues() { + let randomFrequency = Math.floor(Math.random() * 100) + 1; + let randomDelta = Math.floor(Math.random() * 30) + 1; + + document.getElementById('frequencySlider').value = randomFrequency; + document.getElementById('deltaSlider').value = randomDelta; + + document.getElementById('frequencyDisplay').textContent = `Frequency: ${randomFrequency} Hz`; + document.getElementById('deltaDisplay').textContent = `Delta: ${randomDelta} Hz`; + + if (isPlaying) { + updateFrequencies(); } +} - 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); +document.getElementById('togglePlayPause').addEventListener('click', function() { + if (isPlaying) { + stopBinauralBeats(); this.textContent = 'Play'; + } else { + startBinauralBeats(); + this.textContent = 'Pause'; } }); + +document.getElementById('frequencySlider').addEventListener('input', function() { + document.getElementById('frequencyDisplay').textContent = `Frequency: ${this.value} Hz`; + if (isPlaying) { + updateFrequencies(); + } +}); + +document.getElementById('deltaSlider').addEventListener('input', function() { + document.getElementById('deltaDisplay').textContent = `Delta: ${this.value} Hz`; + if (isPlaying) { + updateFrequencies(); + } +}); + +document.getElementById('volumeSlider').addEventListener('input', function() { + document.getElementById('volumeDisplay').textContent = `Volume: ${Math.round(this.value * 100)}%`; + if (isPlaying) { + updateVolume(); + } +}); + +document.getElementById('randomize').addEventListener('click', randomizeValues);