Update popup.js

This commit is contained in:
pliny
2023-08-12 18:06:25 -07:00
committed by GitHub
parent a5dbffd88c
commit 17a4367beb

View File

@@ -1,50 +1,38 @@
let audioContext;
let audioContext = new AudioContext();
let oscillatorLeft;
let oscillatorRight;
let gainNode;
let gainNode = audioContext.createGain();
gainNode.connect(audioContext.destination);
function startBinauralBeats(frequency, delta, volume) {
if (oscillatorLeft) oscillatorLeft.stop();
if (oscillatorRight) oscillatorRight.stop();
if (!audioContext) {
audioContext = new AudioContext();
oscillatorLeft = audioContext.createOscillator();
oscillatorRight = audioContext.createOscillator();
gainNode = audioContext.createGain();
oscillatorLeft.connect(gainNode);
oscillatorRight.connect(gainNode);
gainNode.connect(audioContext.destination);
}
function updateBinauralBeats(frequency, delta, volume) {
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() {
if (oscillatorLeft && oscillatorRight) {
oscillatorLeft.stop();
oscillatorRight.stop();
oscillatorLeft = null;
oscillatorRight = null;
this.textContent = 'Play';
} else {
oscillatorLeft = audioContext.createOscillator();
oscillatorRight = audioContext.createOscillator();
oscillatorLeft.connect(gainNode);
oscillatorRight.connect(gainNode);
updateBinauralBeats(
parseFloat(document.getElementById('frequencySlider').value),
parseFloat(document.getElementById('deltaSlider').value),
parseFloat(document.getElementById('volumeSlider').value)
);
oscillatorLeft.start();
oscillatorRight.start();
this.textContent = 'Pause';
}
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() {
updateBinauralBeats(
startBinauralBeats(
parseFloat(this.value),
parseFloat(document.getElementById('deltaSlider').value),
parseFloat(document.getElementById('volumeSlider').value)
@@ -52,7 +40,7 @@ document.getElementById('frequencySlider').addEventListener('input', function()
});
document.getElementById('deltaSlider').addEventListener('input', function() {
updateBinauralBeats(
startBinauralBeats(
parseFloat(document.getElementById('frequencySlider').value),
parseFloat(this.value),
parseFloat(document.getElementById('volumeSlider').value)