mirror of
https://github.com/elder-plinius/binaural-beats-generator.git
synced 2026-02-12 16:52:50 +00:00
Update background.js
This commit is contained in:
@@ -1,17 +1,21 @@
|
||||
let audioContext = new (window.AudioContext || window.webkitAudioContext)();
|
||||
let oscillatorLeft, oscillatorRight, gainNode;
|
||||
let isPlaying = false;
|
||||
let audioContext = null;
|
||||
let oscillatorLeft = null;
|
||||
let oscillatorRight = null;
|
||||
let gainNode = null;
|
||||
|
||||
function startBinauralBeats(frequency = 440, delta = 10) {
|
||||
if (audioContext) {
|
||||
stopBinauralBeats();
|
||||
}
|
||||
|
||||
audioContext = new AudioContext();
|
||||
|
||||
function startBinauralBeats() {
|
||||
oscillatorLeft = audioContext.createOscillator();
|
||||
oscillatorRight = audioContext.createOscillator();
|
||||
gainNode = audioContext.createGain();
|
||||
|
||||
oscillatorLeft.type = 'sine';
|
||||
oscillatorRight.type = 'sine';
|
||||
|
||||
updateFrequencies();
|
||||
updateVolume();
|
||||
oscillatorLeft.frequency.value = frequency;
|
||||
oscillatorRight.frequency.value = frequency + delta;
|
||||
|
||||
oscillatorLeft.connect(gainNode);
|
||||
oscillatorRight.connect(gainNode);
|
||||
@@ -19,38 +23,38 @@ function startBinauralBeats() {
|
||||
|
||||
oscillatorLeft.start();
|
||||
oscillatorRight.start();
|
||||
|
||||
isPlaying = true;
|
||||
}
|
||||
|
||||
function stopBinauralBeats() {
|
||||
oscillatorLeft.stop();
|
||||
oscillatorRight.stop();
|
||||
isPlaying = false;
|
||||
if (oscillatorLeft) oscillatorLeft.stop();
|
||||
if (oscillatorRight) oscillatorRight.stop();
|
||||
if (audioContext) audioContext.close();
|
||||
|
||||
oscillatorLeft = null;
|
||||
oscillatorRight = null;
|
||||
audioContext = null;
|
||||
}
|
||||
|
||||
function updateFrequencies(frequency, delta) {
|
||||
oscillatorLeft.frequency.setValueAtTime(frequency, audioContext.currentTime);
|
||||
oscillatorRight.frequency.setValueAtTime(frequency + delta, audioContext.currentTime);
|
||||
}
|
||||
|
||||
function updateVolume(volume) {
|
||||
gainNode.gain.setValueAtTime(volume, audioContext.currentTime);
|
||||
}
|
||||
|
||||
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
|
||||
switch (message.action) {
|
||||
case 'start':
|
||||
startBinauralBeats();
|
||||
break;
|
||||
case 'stop':
|
||||
stopBinauralBeats();
|
||||
break;
|
||||
case 'updateFrequencies':
|
||||
updateFrequencies(message.frequency, message.delta);
|
||||
break;
|
||||
case 'updateVolume':
|
||||
updateVolume(message.volume);
|
||||
break;
|
||||
}
|
||||
chrome.runtime.onConnect.addListener((port) => {
|
||||
port.onMessage.addListener((message) => {
|
||||
switch (message.action) {
|
||||
case 'start':
|
||||
startBinauralBeats();
|
||||
break;
|
||||
case 'stop':
|
||||
stopBinauralBeats();
|
||||
break;
|
||||
case 'updateFrequencies':
|
||||
if (oscillatorLeft && oscillatorRight) {
|
||||
oscillatorLeft.frequency.value = message.frequency;
|
||||
oscillatorRight.frequency.value = message.frequency + message.delta;
|
||||
}
|
||||
break;
|
||||
case 'updateVolume':
|
||||
if (gainNode) {
|
||||
gainNode.gain.value = message.volume;
|
||||
}
|
||||
break;
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user