mirror of
https://github.com/elder-plinius/binaural-beats-generator.git
synced 2026-07-13 06:26:34 +02:00
Update popup.js
This commit is contained in:
@@ -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';
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user