mirror of
https://github.com/elder-plinius/binaural-beats-generator.git
synced 2026-02-12 16:52:50 +00:00
Update popup.js
This commit is contained in:
54
popup.js
54
popup.js
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user