-
Notifications
You must be signed in to change notification settings - Fork 0
/
audio.js
103 lines (85 loc) · 3.15 KB
/
audio.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
let oscillatorLeft;
let oscillatorRight;
let isPlaying = false;
const baseFrequencyInput = document.getElementById('baseFrequency');
const beatFrequencyInput = document.getElementById('beatFrequency');
const toggleButton = document.getElementById('toggleButton');
const brainwaveStateSelect = document.getElementById('brainwaveState');
const brainwaveRanges = {
delta: { min: 0.5, max: 4 },
theta: { min: 4, max: 8 },
alpha: { min: 8, max: 13 },
beta: { min: 13, max: 30 },
gamma: { min: 30, max: 100 },
custom: { min: 0.5, max: 100 }
};
baseFrequencyInput.addEventListener('input', updateFrequency);
beatFrequencyInput.addEventListener('input', updateFrequency);
toggleButton.addEventListener('click', toggleSound);
brainwaveStateSelect.addEventListener('change', updateBrainwaveState);
function initAudio() {
if (!oscillatorLeft || !oscillatorRight) {
oscillatorLeft = new Tone.Oscillator().toDestination();
oscillatorRight = new Tone.Oscillator().toDestination();
oscillatorLeft.set({ pan: -1 });
oscillatorRight.set({ pan: 1 });
Tone.Destination.volume.value = -20;
}
updateFrequency();
}
function updateFrequency() {
const baseFrequency = parseFloat(baseFrequencyInput.value);
const beatFrequency = parseFloat(beatFrequencyInput.value);
document.getElementById('baseFrequencyValue').textContent = baseFrequency;
document.getElementById('beatFrequencyValue').textContent = beatFrequency.toFixed(1);
if (oscillatorLeft && oscillatorRight) {
oscillatorLeft.frequency.value = baseFrequency;
oscillatorRight.frequency.value = baseFrequency + beatFrequency;
}
}
function updateBrainwaveState() {
const selectedState = brainwaveStateSelect.value;
const range = brainwaveRanges[ selectedState ];
beatFrequencyInput.min = range.min;
beatFrequencyInput.max = range.max;
beatFrequencyInput.value = (range.min + range.max) / 2;
updateFrequency();
}
async function toggleSound() {
try {
await Tone.start();
} catch (error) {
console.error('Failed to start audio:', error);
handleAudioError('Failed to start audio. Please check your audio settings and try again.');
return;
}
if (!oscillatorLeft || !oscillatorRight) {
initAudio();
}
if (isPlaying) {
oscillatorLeft.stop();
oscillatorRight.stop();
toggleButton.textContent = 'Start';
toggleButton.setAttribute('aria-pressed', 'false');
} else {
oscillatorLeft.start();
oscillatorRight.start();
toggleButton.textContent = 'Stop';
toggleButton.setAttribute('aria-pressed', 'true');
}
isPlaying = !isPlaying;
}
function handleAudioError(message) {
baseFrequencyInput.disabled = true;
beatFrequencyInput.disabled = true;
toggleButton.disabled = true;
brainwaveStateSelect.disabled = true;
const errorDiv = document.createElement('div');
errorDiv.textContent = message;
errorDiv.style.color = 'red';
errorDiv.style.marginTop = '1rem';
document.querySelector('.container').appendChild(errorDiv);
}
// Initial setup
initAudio();
updateBrainwaveState();