-
Notifications
You must be signed in to change notification settings - Fork 0
/
index copy.html
234 lines (196 loc) · 8.01 KB
/
index copy.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Echolocation Click Generator</title>
<style>
/* Styling for the touch area, start button, visual feedback, and controls */
#container {
position: relative;
width: 100%;
height: 100vh;
text-align: center;
}
.touch-area {
width: 100%;
height: 70vh;
background: #ccc;
user-select: none;
margin-top: 20px;
position: relative;
}
.start-button {
width: 200px;
padding: 10px;
font-size: 16px;
cursor: pointer;
margin: auto;
display: block;
}
.visual-feedback {
width: 20px;
height: 20px;
border-radius: 50%;
background: blue;
position: absolute;
transform: translate(-50%, -50%);
pointer-events: none;
}
.control {
margin: 5px 0;
}
</style>
</head>
<body>
<div id="container">
<button id="startButton" class="start-button">Start Click Generator</button>
<input id="minClickRate" class="control" type="text" placeholder="Min Click Rate (Hz)" value="1" />
<input id="maxClickRate" class="control" type="text" placeholder="Max Click Rate (Hz)" value="10" />
<input id="minFrequency" class="control" type="text" placeholder="Min Frequency (kHz)" value="6" />
<input id="maxFrequency" class="control" type="text" placeholder="Max Frequency (kHz)" value="16" />
<label for="volumeControl">Volume: </label>
<input id="volumeControl" class="control" type="range" min="0" max="1" step="0.01" value="0.5" />
<div id="touchArea" class="touch-area" style="display:none;">
<div id="feedbackCircle" class="visual-feedback" style="display:none;"></div>
</div>
<p id="infoText"></p>
</div>
<script>
// Declare variables
let audioContext;
let isAudioStarted = false; // Flag for AudioContext initialization
let isClicking = false; // Flag for active clicking
let lastClickTime = 0; // Timestamp of the last click
let clickRateRange = { min: 1, max: 10 }; // Click rate range in Hz
let frequencyRange = { min: 6000, max: 16000 }; // Frequency range in Hz
let volume = 0.5; // Default volume
const startButton = document.getElementById('startButton');
const touchArea = document.getElementById('touchArea');
const feedbackCircle = document.getElementById('feedbackCircle');
const infoText = document.getElementById('infoText');
const minClickRateInput = document.getElementById('minClickRate');
const maxClickRateInput = document.getElementById('maxClickRate');
const minFrequencyInput = document.getElementById('minFrequency');
const maxFrequencyInput = document.getElementById('maxFrequency');
const volumeControl = document.getElementById('volumeControl');
startButton.addEventListener('click', initializeAudioContext);
minClickRateInput.addEventListener('change', updateSettings);
maxClickRateInput.addEventListener('change', updateSettings);
minFrequencyInput.addEventListener('change', updateSettings);
maxFrequencyInput.addEventListener('change', updateSettings);
volumeControl.addEventListener('input', updateVolume);
// Timing control
let clickTimer; // Timer ID for click generation
function initializeAudioContext() {
if (!isAudioStarted) {
audioContext = new AudioContext();
touchArea.style.display = 'block';
startButton.style.display = 'none';
isAudioStarted = true;
}
}
function updateSettings() {
clickRateRange.min = parseFloat(minClickRateInput.value) || 1;
clickRateRange.max = parseFloat(maxClickRateInput.value) || 10;
frequencyRange.min = (parseFloat(minFrequencyInput.value) || 6) * 1000;
frequencyRange.max = (parseFloat(maxFrequencyInput.value) || 16) * 1000;
}
function updateVolume() {
volume = parseFloat(volumeControl.value);
}
touchArea.addEventListener('touchstart', handleTouchStart, false);
touchArea.addEventListener('touchmove', handleTouchMove, false);
touchArea.addEventListener('touchend', handleTouchEnd, false);
touchArea.addEventListener('mousedown', handleTouchStart, false); // For non-touch devices
touchArea.addEventListener('mousemove', handleTouchMove, false);
touchArea.addEventListener('mouseup', handleTouchEnd, false);
function handleTouchStart(event) {
event.preventDefault();
isClicking = true;
lastClickTime = performance.now();
feedbackCircle.style.display = 'block';
clickTimer = setInterval(generateClickIfDue, 1);
}
function handleTouchMove(event) {
if (isClicking) {
event.preventDefault();
updateTouchPosition(event);
}
}
function handleTouchEnd() {
clearInterval(clickTimer);
isClicking = false;
feedbackCircle.style.display = 'none';
infoText.textContent += " | Touch released.";
}
function updateTouchPosition(event) {
const x = event.touches ? event.touches[0].clientX : event.clientX;
const y = event.touches ? event.touches[0].clientY : event.clientY;
setPositionFeedback(x, y);
}
function generateClickIfDue() {
const currentClickRateRange = clickRateRange.max - clickRateRange.min;
const currentFrequencyRange = frequencyRange.max - frequencyRange.min;
const currentTime = performance.now();
if (currentTime - lastClickTime >= (1000 / currentClickRateRange)) {
const x = (feedbackCircle.offsetLeft + feedbackCircle.clientWidth / 2) / touchArea.clientWidth;
const y = (feedbackCircle.offsetTop + feedbackCircle.clientHeight / 2) / touchArea.clientHeight;
const clickFrequency = y * currentFrequencyRange + frequencyRange.min;
const clickRate = x * currentClickRateRange + clickRateRange.min;
const clickIntervalMs = 1000 / clickRate;
if (currentTime - lastClickTime >= clickIntervalMs) {
generateClick(clickFrequency);
lastClickTime = currentTime;
}
infoText.textContent = `X: ${x.toFixed(2)}, Y: ${y.toFixed(2)}, Frequency: ${clickFrequency.toFixed(0)} Hz, Click every: ${clickIntervalMs.toFixed(0)} ms, Volume: ${Math.round(volume * 100)}%`;
}
}
function generateClick(frequency) {
// Create an oscillator for each click
const oscillator = audioContext.createOscillator();
oscillator.type = 'sine';
oscillator.frequency.setValueAtTime(frequency, audioContext.currentTime);
// Create a gain node to control the volume
const gainNode = audioContext.createGain();
gainNode.gain.setValueCurveAtTime([volume, 0], audioContext.currentTime, 0.01); // Click lasts for 10ms
// Connect and play the oscillator for a single click
oscillator.connect(gainNode);
gainNode.connect(audioContext.destination);
oscillator.start();
oscillator.stop(audioContext.currentTime + 0.01); // Stop after 10ms
}
// function generateClick(frequency) {
// const oscillator = audioContext.createOscillator();
// oscillator.type = 'sine';
// oscillator.frequency.setValueAtTime(frequency, audioContext.currentTime);
// const gainNode = audioContext.createGain();
// // Set gain to max (1 for 100%) for loudest click possible
// gainNode.gain.setValueAtTime(1, audioContext.currentTime);
// // Volume is controlled by the slider and can be adjusted in real time
// gainNode.gain.value *= volume;
// gainNode.gain.exponentialRampToValueAtTime(0.001, audioContext.currentTime + 0.01);
// oscillator.connect(gainNode);
// gainNode.connect(audioContext.destination);
// oscillator.start();
// oscillator.stop(audioContext.currentTime + 0.01); // Stop oscillator after 10ms
// }
// function generateClick(frequency) {
// const oscillator = audioContext.createOscillator();
// oscillator.type = 'sine';
// oscillator.frequency.setValueAtTime(frequency, audioContext.currentTime);
// const gainNode = audioContext.createGain();
// gainNode.gain.setValueAtTime(volume, audioContext.currentTime); // Set volume
// gainNode.gain.exponentialRampToValueAtTime(0.001, audioContext.currentTime + 0.01); // Ramp to almost 0 in 10 ms
// oscillator.connect(gainNode);
// gainNode.connect(audioContext.destination);
// oscillator.start();
// oscillator.stop(audioContext.currentTime + 0.01); // Stop oscillator after 10ms
// }
function setPositionFeedback(x, y) {
feedbackCircle.style.left = `${x}px`;
feedbackCircle.style.top = `${y}px`;
}
</script>
</body>
</html>