-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
235 lines (206 loc) · 9.41 KB
/
index.html
File metadata and controls
235 lines (206 loc) · 9.41 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Interval Timer with Audio Cues</title>
<script src="https://cdn.tailwindcss.com"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tone/14.7.77/Tone.js"></script>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;700&display=swap" rel="stylesheet">
<style>
body {
font-family: 'Inter', sans-serif;
}
button {
transition: background-color 0.2s ease-in-out, transform 0.1s ease-in-out;
}
button:active {
transform: scale(0.98);
}
</style>
</head>
<body class="bg-gray-900 text-white flex items-center justify-center min-h-screen">
<div class="bg-gray-800 p-8 rounded-2xl shadow-2xl w-full max-w-md text-center space-y-6">
<h1 class="text-3xl font-bold text-cyan-400">Interval Timer</h1>
<div class="flex flex-col items-center">
<label for="duration" class="text-sm font-medium text-gray-400 mb-2">Set Duration (MM:SS)</label>
<input type="text" id="duration" value="5:00" placeholder="MM:SS" pattern="[0-9]{1,2}:[0-5][0-9]" class="bg-gray-700 text-white text-center w-32 text-lg p-2 rounded-md border border-gray-600 focus:ring-2 focus:ring-cyan-500 focus:outline-none">
</div>
<div id="timerDisplay" class="text-7xl font-bold text-gray-100 tracking-wider">
5:00
</div>
<div class="flex justify-center">
<button id="playPauseBtn" class="bg-cyan-600 hover:bg-cyan-500 text-white font-bold py-3 px-10 rounded-lg shadow-lg text-lg">
Play
</button>
</div>
<div id="statusMessage" class="text-green-400 h-6"></div>
</div>
<script>
const durationInput = document.getElementById('duration');
const timerDisplay = document.getElementById('timerDisplay');
const playPauseBtn = document.getElementById('playPauseBtn');
const statusMessage = document.getElementById('statusMessage');
let timerId = null;
let isPlaying = false;
let remainingSeconds = 0;
let lastMinuteNotified = -1;
const sineSynth = new Tone.Synth({
oscillator: { type: 'triangle' },
envelope: { attack: 0.005, decay: 0.1, sustain: 0.3, release: 1 }
}).toDestination();
/**
* Updates the timer display every second.
*/
function updateTimer() {
if (remainingSeconds <= 0) {
clearInterval(timerId);
playSound(sineSynth, 'C5', '8n', 4, 250); // Play final chime
statusMessage.textContent = 'Timer complete!';
resetTimerState();
return;
}
remainingSeconds--;
displayTime();
checkAndPlaySounds();
}
/**
* Checks the current time and plays sounds according to the rules.
*/
function checkAndPlaySounds() {
const currentMinute = Math.floor(remainingSeconds / 60);
const currentSecondInMinute = remainingSeconds % 60;
// When we reach X minutes left (at X:00), a low-pitched tone rings X times.
if (currentSecondInMinute === 0 && remainingSeconds > 0 && currentMinute !== lastMinuteNotified) {
const timesToRing = currentMinute;
lastMinuteNotified = currentMinute; // Mark this minute as notified
console.log(`Ringing low tone ${timesToRing} times for minute ${currentMinute}`);
playSound(sineSynth, 'E4', '16n', timesToRing, 500);
}
// When we reach X:30, a high-pitched tone rings 2 times.
else if (currentSecondInMinute === 30) {
console.log('Ringing high tone 2 times for xx:30.');
playSound(sineSynth, 'C5', '16n', 2, 200);
}
// When we reach X:15 or X:45, the high-pitched tone rings 1 time.
else if (currentSecondInMinute === 15 || currentSecondInMinute === 45) {
console.log('Ringing high tone 1 time for xx:15 or xx:45.');
playSound(sineSynth, 'C5', '16n', 1, 200);
}
}
/**
* Parses a time string in "MM:SS" format.
* @param {string} timeString - The string to parse.
* @returns {number|null} The total seconds, or null if invalid.
*/
function parseTime(timeString) {
const parts = timeString.split(':');
if (parts.length !== 2) return null;
const minutes = parseInt(parts[0], 10);
const seconds = parseInt(parts[1], 10);
if (isNaN(minutes) || isNaN(seconds) || seconds > 59 || seconds < 0 || minutes < 0) {
return null;
}
return (minutes * 60) + seconds;
}
/**
* Toggles the timer between play and pause states.
*/
function toggleTimer() {
// Requirement for web audio: start context on a user gesture.
if (Tone.context.state !== 'running') {
Tone.start();
console.log('Audio context started.');
}
if (isPlaying) {
clearInterval(timerId);
isPlaying = false;
playPauseBtn.textContent = 'Play';
playPauseBtn.classList.remove('bg-red-600', 'hover:bg-red-500');
playPauseBtn.classList.add('bg-cyan-600', 'hover:bg-cyan-500');
statusMessage.textContent = 'Timer paused.';
} else {
// If it's a new start, get the duration from the input
if (remainingSeconds <= 0 || timerId === null) {
const totalSeconds = parseTime(durationInput.value);
if (totalSeconds === null || totalSeconds <= 0) {
statusMessage.textContent = 'Invalid format. Use MM:SS.';
statusMessage.className = 'text-red-400 h-6';
return;
}
remainingSeconds = totalSeconds;
lastMinuteNotified = -1;
}
displayTime();
timerId = setInterval(updateTimer, 1000);
isPlaying = true;
playPauseBtn.textContent = 'Pause';
playPauseBtn.classList.remove('bg-cyan-600', 'hover:bg-cyan-500');
playPauseBtn.classList.add('bg-red-600', 'hover:bg-red-500');
statusMessage.textContent = 'Timer running...';
}
}
/**
* Resets the timer state to its initial values.
*/
function resetTimerState() {
isPlaying = false;
timerId = null;
playPauseBtn.textContent = 'Play';
playPauseBtn.classList.remove('bg-red-600', 'hover:bg-red-500');
playPauseBtn.classList.add('bg-cyan-600', 'hover:bg-cyan-500');
updateDisplayFromInput();
}
/**
* Formats seconds into MM:SS format and updates the display.
*/
function displayTime() {
const minutes = Math.floor(remainingSeconds / 60);
const seconds = remainingSeconds % 60;
timerDisplay.textContent = `${String(minutes).padStart(2, '0')}:${String(seconds).padStart(2, '0')}`;
}
/**
* Plays a specified tone a number of times with an interval.
* @param {Tone.Synth} synth - The Tone.js synth to use.
* @param {string} note - The note to play (e.g., 'C4').
* @param {string} duration - The duration of the note (e.g., '8n').
* @param {number} times - How many times to play the note.
* @param {number} intervalMs - The interval in milliseconds between notes.
*/
function playSound(synth, note, duration, times, intervalMs) {
for (let i = 0; i < times; i++) {
setTimeout(() => {
synth.triggerAttackRelease(note, duration);
}, i * intervalMs);
}
}
/**
* Updates the timer display based on the current input value.
*/
function updateDisplayFromInput() {
const totalSeconds = parseTime(durationInput.value);
if (totalSeconds === null) {
timerDisplay.textContent = "00:00";
statusMessage.textContent = "Invalid format. Use MM:SS.";
statusMessage.className = 'text-red-400 h-6';
} else {
statusMessage.textContent = "";
statusMessage.className = 'h-6';
const minutes = Math.floor(totalSeconds / 60);
const seconds = totalSeconds % 60;
timerDisplay.textContent = `${String(minutes).padStart(2, '0')}:${String(seconds).padStart(2, '0')}`;
}
remainingSeconds = 0;
}
playPauseBtn.addEventListener('click', toggleTimer);
durationInput.addEventListener('change', () => {
if (!isPlaying) {
resetTimerState();
}
});
window.onload = updateDisplayFromInput;
</script>
</body>
</html>