Repository navigation
Expand file tree
/
Copy pathrenderer.js
More file actions
244 lines (198 loc) · 6.96 KB
/
Copy pathrenderer.js
File metadata and controls
244 lines (198 loc) · 6.96 KB
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
const { ipcRenderer } = require('electron');
let audioContext;
let micStream;
let micSource;
let micGain;
let soundGain;
let destination;
let sounds = [];
let selectedOutputDevice = null;
// Initialize Audio Context
function initAudioContext() {
if (!audioContext) {
audioContext = new (window.AudioContext || window.webkitAudioContext)();
// Create gain nodes
micGain = audioContext.createGain();
soundGain = audioContext.createGain();
// Create destination (this will route to BlackHole if set up)
destination = audioContext.createMediaStreamDestination();
// Connect gain nodes to destination
micGain.connect(destination);
soundGain.connect(destination);
// Also connect to speakers so you can hear
micGain.connect(audioContext.destination);
soundGain.connect(audioContext.destination);
console.log('Audio context initialized');
console.log('MediaStreamDestination stream:', destination.stream);
}
}
// Get available microphones
async function getMicrophones() {
try {
const devices = await navigator.mediaDevices.enumerateDevices();
const audioInputs = devices.filter(device => device.kind === 'audioinput');
const micSelect = document.getElementById('micSelect');
micSelect.innerHTML = '<option value="">Select Microphone</option>';
audioInputs.forEach(device => {
const option = document.createElement('option');
option.value = device.deviceId;
option.textContent = device.label || `Microphone ${micSelect.options.length}`;
micSelect.appendChild(option);
});
} catch (err) {
console.error('Error getting microphones:', err);
}
}
// Get available audio output devices
async function getOutputDevices() {
try {
const devices = await navigator.mediaDevices.enumerateDevices();
const audioOutputs = devices.filter(device => device.kind === 'audiooutput');
const outputSelect = document.getElementById('outputSelect');
outputSelect.innerHTML = '<option value="">Select Output (BlackHole)</option>';
audioOutputs.forEach(device => {
const option = document.createElement('option');
option.value = device.deviceId;
option.textContent = device.label || `Output ${outputSelect.options.length}`;
outputSelect.appendChild(option);
});
} catch (err) {
console.error('Error getting output devices:', err);
}
}
// Setup microphone
async function setupMicrophone(deviceId) {
try {
initAudioContext();
// Stop existing stream
if (micStream) {
micStream.getTracks().forEach(track => track.stop());
}
// Get new stream
const constraints = {
audio: deviceId ? { deviceId: { exact: deviceId } } : true,
video: false
};
micStream = await navigator.mediaDevices.getUserMedia(constraints);
// Connect microphone to audio graph
if (micSource) {
micSource.disconnect();
}
micSource = audioContext.createMediaStreamSource(micStream);
micSource.connect(micGain);
console.log('Microphone setup complete');
} catch (err) {
console.error('Error setting up microphone:', err);
alert('Error accessing microphone. Please check permissions.');
}
}
// Play sound
function playSound(soundPath) {
initAudioContext();
const audio = new Audio(soundPath);
const source = audioContext.createMediaElementSource(audio);
source.connect(soundGain);
audio.play().catch(err => {
console.error('Error playing sound:', err);
});
// Visual feedback
const button = event.target;
button.classList.add('playing');
audio.onended = () => {
button.classList.remove('playing');
source.disconnect();
};
}
// Load sounds from app folder
async function loadSounds() {
const files = await ipcRenderer.invoke('get-sounds');
sounds = files;
displaySounds();
}
// Display sound buttons
function displaySounds() {
const soundGrid = document.getElementById('soundGrid');
soundGrid.innerHTML = '';
sounds.forEach((sound, index) => {
const button = document.createElement('button');
button.className = 'sound-button';
button.textContent = sound.name;
button.dataset.soundIndex = index;
button.onclick = () => {
// Check if sound is already playing
if (button.currentAudio) {
button.currentAudio.pause();
button.currentAudio.currentTime = 0;
button.currentAudio = null;
button.classList.remove('playing');
return;
}
const audio = new Audio(sound.path);
button.currentAudio = audio;
if (audioContext) {
const source = audioContext.createMediaElementSource(audio);
source.connect(soundGain);
}
audio.play().catch(err => console.error('Error playing sound:', err));
button.classList.add('playing');
audio.onended = () => {
button.classList.remove('playing');
button.currentAudio = null;
};
};
soundGrid.appendChild(button);
});
}
// Event Listeners
// Toggle setup instructions
document.getElementById('infoHeader').addEventListener('click', () => {
const content = document.getElementById('infoContent');
const icon = document.querySelector('.toggle-icon');
if (content.style.display === 'none') {
content.style.display = 'block';
icon.classList.add('expanded');
} else {
content.style.display = 'none';
icon.classList.remove('expanded');
}
});
document.getElementById('openSoundsFolderBtn').addEventListener('click', async () => {
await ipcRenderer.invoke('open-sounds-folder');
});
document.getElementById('outputSelect').addEventListener('change', (e) => {
selectedOutputDevice = e.target.value;
console.log('Output device selected:', selectedOutputDevice);
// Apply to existing audio context if available
if (audioContext && audioContext.destination && audioContext.destination.setSinkId) {
audioContext.destination.setSinkId(selectedOutputDevice)
.then(() => console.log('Audio output set to:', selectedOutputDevice))
.catch(err => console.error('Error setting output device:', err));
}
});
document.getElementById('micSelect').addEventListener('change', (e) => {
if (e.target.value) {
setupMicrophone(e.target.value);
}
});
document.getElementById('micVolume').addEventListener('input', (e) => {
const volume = e.target.value / 100;
if (micGain) {
micGain.gain.value = volume;
}
document.getElementById('micVolumeLabel').textContent = `${e.target.value}%`;
});
document.getElementById('soundVolume').addEventListener('input', (e) => {
const volume = e.target.value / 100;
if (soundGain) {
soundGain.gain.value = volume;
}
document.getElementById('soundVolumeLabel').textContent = `${e.target.value}%`;
});
// Initialize
getMicrophones();
getOutputDevices();
loadSounds();
// Request microphone permission on load
navigator.mediaDevices.getUserMedia({ audio: true })
.then(() => getMicrophones())
.catch(err => console.error('Microphone permission error:', err));