Update popup.js

This commit is contained in:
pliny
2023-08-12 18:07:42 -07:00
committed by GitHub
parent 17a4367beb
commit cd2eb23e47
+17 -47
View File
@@ -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';
}
});