-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
177 lines (156 loc) · 6.6 KB
/
script.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/**
* Main application file that initializes all components
* and sets up event listeners for user interaction
*/
import AudioController from './audio.js';
import Visualizer from './visualizer.js';
import Effects from './effects.js';
import Recorder from './recorder.js';
class SoundExplorer {
constructor() {
// Initialize core components
this.audioController = new AudioController();
this.effects = new Effects();
this.recorder = new Recorder(this.audioController);
// Initialize visualizer after first interaction
this.visualizer = null;
// Track active oscillators
this.activeOscillators = new Map();
// Check if running on mobile
this.isMobile = /iPhone|iPad|iPod|Android/i.test(navigator.userAgent);
this.setupEventListeners();
this.setupInitialInteraction();
this.setupMobileHandling();
}
setupInitialInteraction() {
const initializeAudio = () => {
if (!this.visualizer) {
this.visualizer = new Visualizer(this.audioController.analyser);
}
document.removeEventListener('click', initializeAudio);
document.removeEventListener('touchstart', initializeAudio);
document.removeEventListener('keydown', initializeAudio);
};
document.addEventListener('click', initializeAudio);
document.addEventListener('touchstart', initializeAudio);
document.addEventListener('keydown', initializeAudio);
}
setupMobileHandling() {
if (this.isMobile) {
// Add touch events for sound pads
document.querySelectorAll('.sound-pad').forEach(pad => {
pad.addEventListener('touchstart', (e) => {
e.preventDefault(); // Prevent default touch behavior
const note = pad.dataset.note;
const oscillator = this.audioController.playSound(note, true);
this.activeOscillators.set(note, oscillator);
this.effects.createRipple(pad);
this.recorder.recordNote(note);
});
pad.addEventListener('touchend', (e) => {
e.preventDefault();
const note = pad.dataset.note;
const oscillator = this.activeOscillators.get(note);
if (oscillator) {
oscillator.stop();
this.activeOscillators.delete(note);
}
});
});
}
}
setupEventListeners() {
// Set up mouse events for sound pads (for desktop)
document.querySelectorAll('.sound-pad').forEach(pad => {
pad.addEventListener('mousedown', () => {
const note = pad.dataset.note;
const oscillator = this.audioController.playSound(note, true);
this.activeOscillators.set(note, oscillator);
this.effects.createRipple(pad);
this.recorder.recordNote(note);
});
pad.addEventListener('mouseup', () => {
const note = pad.dataset.note;
const oscillator = this.activeOscillators.get(note);
if (oscillator) {
oscillator.stop();
this.activeOscillators.delete(note);
this.recorder.recordNoteEnd(note);
}
});
pad.addEventListener('mouseleave', () => {
const note = pad.dataset.note;
const oscillator = this.activeOscillators.get(note);
if (oscillator) {
oscillator.stop();
this.activeOscillators.delete(note);
this.recorder.recordNoteEnd(note);
}
});
});
// Add window blur event to stop all sounds when switching tabs
window.addEventListener('blur', () => {
this.activeOscillators.forEach((oscillator, note) => {
oscillator.stop();
this.recorder.recordNoteEnd(note);
});
this.activeOscillators.clear();
document.querySelectorAll('.sound-pad').forEach(pad => {
pad.classList.remove('active');
});
});
// Map keyboard keys to notes
const keyMap = {
'a': 'C', 's': 'D', 'd': 'E', 'f': 'F',
'g': 'G', 'h': 'A', 'j': 'B', 'k': 'C2'
};
// Set up keyboard controls
document.addEventListener('keydown', (e) => {
const note = keyMap[e.key.toLowerCase()];
if (note && !e.repeat) { // Prevent repeated keydown events
const pad = document.querySelector(`[data-note="${note}"]`);
if (pad) {
pad.classList.add('active');
const oscillator = this.audioController.playSound(note, true);
this.activeOscillators.set(note, oscillator);
this.effects.createRipple(pad);
this.recorder.recordNote(note);
}
}
});
document.addEventListener('keyup', (e) => {
const note = keyMap[e.key.toLowerCase()];
if (note) {
const pad = document.querySelector(`[data-note="${note}"]`);
if (pad) {
pad.classList.remove('active');
const oscillator = this.activeOscillators.get(note);
if (oscillator) {
oscillator.stop();
this.activeOscillators.delete(note);
this.recorder.recordNoteEnd(note);
}
}
}
});
// Set up volume and mute controls
const volumeSlider = document.getElementById('volumeSlider');
const muteBtn = document.getElementById('muteBtn');
volumeSlider.addEventListener('input', (e) => {
this.audioController.setVolume(e.target.value / 100);
});
muteBtn.addEventListener('click', () => {
const isMuted = this.audioController.toggleMute();
muteBtn.textContent = isMuted ? '🔈' : '🔊';
});
// Set up waveform selection
const waveformSelect = document.getElementById('waveform');
waveformSelect.addEventListener('change', (e) => {
this.audioController.waveform = e.target.value;
});
}
}
// Initialize the application when the page loads
window.addEventListener('load', () => {
new SoundExplorer();
});