Update popup.js

This commit is contained in:
pliny
2023-08-12 17:51:13 -07:00
committed by GitHub
parent fbf0f61a6a
commit 4401d1f9f4

View File

@@ -1,73 +1,23 @@
let audioContext = new (window.AudioContext || window.webkitAudioContext)();
let oscillatorLeft, oscillatorRight, gainNode;
let isPlaying = false;
function randomizeValues() {
// Random frequency between 1 and 100
let randomFrequency = Math.floor(Math.random() * 100) + 1;
function startBinauralBeats() {
oscillatorLeft = audioContext.createOscillator();
oscillatorRight = audioContext.createOscillator();
gainNode = audioContext.createGain();
// Random delta between 1 and 30
let randomDelta = Math.floor(Math.random() * 30) + 1;
oscillatorLeft.type = 'sine';
oscillatorRight.type = 'sine';
// Set the slider values
document.getElementById('frequencySlider').value = randomFrequency;
document.getElementById('deltaSlider').value = randomDelta;
updateFrequencies();
updateVolume();
// Update the displayed values
document.getElementById('frequencyDisplay').textContent = `Frequency: ${randomFrequency} Hz`;
document.getElementById('deltaDisplay').textContent = `Delta: ${randomDelta} Hz`;
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);
}
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`;
// Update the binaural beats if they are playing
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();
}
});
// Add an event listener to the randomize button
document.getElementById('randomize').addEventListener('click', randomizeValues);