-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
878 lines (760 loc) · 27.7 KB
/
Copy pathapp.js
File metadata and controls
878 lines (760 loc) · 27.7 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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
/* ===== Constants ===== */
const MAX_TIMERS = 10;
const STORAGE_KEY = 'sequenceTimerSavedSequences';
/* ===== State & DOM (initialized on DOMContentLoaded) ===== */
let state;
let dom;
let globalAudioCtx = null;
let isHtml5AudioUnlocked = false;
let isPlayingRealSound = false;
/* ===== Initialization ===== */
document.addEventListener('DOMContentLoaded', () => {
state = {
timers: [createTimer()],
currentIndex: 0,
isRunning: false,
isPaused: false,
remaining: 0,
totalForCurrent: 0,
intervalId: null,
savedSequences: []
};
dom = {
timerDisplay: document.getElementById('timerDisplay'),
progressBar: document.getElementById('progressBar'),
timerInfo: document.getElementById('timerInfo'),
sequenceDots: document.getElementById('sequenceDots'),
timerList: document.getElementById('timerList'),
timerHint: document.getElementById('timerHint'),
sequenceList: document.getElementById('sequenceList'),
sequencesEmpty: document.getElementById('sequencesEmpty'),
sequenceNameInput: document.getElementById('sequenceNameInput'),
btnStart: document.getElementById('btnStart'),
btnCancel: document.getElementById('btnCancel'),
btnReset: document.getElementById('btnReset'),
btnAddTimer: document.getElementById('btnAddTimer'),
btnSaveSequence: document.getElementById('btnSaveSequence'),
btnTestSound: document.getElementById('btnTestSound'),
display: document.querySelector('.display')
};
loadSavedSequences();
render();
bindEvents();
initAudioSystem();
// One-time gesture listener to unlock Web Audio API on mobile (iOS/Android) browser autoplay policies
const unlock = () => {
unlockAudio();
document.removeEventListener('click', unlock);
document.removeEventListener('touchstart', unlock);
};
document.addEventListener('click', unlock, { passive: true });
document.addEventListener('touchstart', unlock, { passive: true });
});
/* ===== Event Binding ===== */
function bindEvents() {
dom.btnStart.addEventListener('click', handleStartPause);
dom.btnCancel.addEventListener('click', handleCancelAll);
dom.btnReset.addEventListener('click', handleCancelAndReset);
dom.btnAddTimer.addEventListener('click', handleAddTimer);
dom.btnSaveSequence.addEventListener('click', handleSaveSequence);
/* Delegate events on the timer list */
dom.timerList.addEventListener('click', handleTimerListClick);
/* Delegate events on the sequence list */
dom.sequenceList.addEventListener('click', handleSequenceListClick);
dom.btnTestSound.addEventListener('click', handleTestSound);
/* Allow Enter key to save sequence */
dom.sequenceNameInput.addEventListener('keydown', (event) => {
if (event.key === 'Enter') {
handleSaveSequence();
}
});
}
/* ===== Handlers ===== */
function handleStartPause() {
if (state.isRunning && !state.isPaused) {
pauseSequence();
} else if (state.isPaused) {
resumeSequence();
} else {
startSequence();
}
}
function handleCancelAll() {
cancelAll();
}
function handleCancelAndReset() {
cancelAndReset();
}
function handleAddTimer() {
if (state.timers.length >= MAX_TIMERS || state.isRunning) {
return;
}
state.timers.push(createTimer());
render();
}
function bindScrollPickerEvents(picker, timerId, field) {
const itemHeight = 34;
let scrollTimeout;
const handleScrollUpdate = () => {
const items = picker.querySelectorAll('.scroll-picker__item');
const value = Math.max(0, Math.min(items.length - 1, Math.round(picker.scrollTop / itemHeight)));
const timer = state.timers.find((t) => t.id === timerId);
if (timer && timer[field] !== value) {
timer[field] = value;
}
// Highlight selected item in UI
items.forEach((item, idx) => {
if (idx === value) {
item.classList.add('scroll-picker__item--selected');
} else {
item.classList.remove('scroll-picker__item--selected');
}
});
};
picker.addEventListener('scroll', () => {
// Instant visual feedback for scrolling
const items = picker.querySelectorAll('.scroll-picker__item');
const tempValue = Math.max(0, Math.min(items.length - 1, Math.round(picker.scrollTop / itemHeight)));
items.forEach((item, idx) => {
if (idx === tempValue) {
item.classList.add('scroll-picker__item--selected');
} else {
item.classList.remove('scroll-picker__item--selected');
}
});
clearTimeout(scrollTimeout);
scrollTimeout = setTimeout(handleScrollUpdate, 150);
});
picker.addEventListener('scrollend', () => {
clearTimeout(scrollTimeout);
handleScrollUpdate();
});
// Support smooth scroll to item on direct click
picker.addEventListener('click', (event) => {
if (state.isRunning) return;
const item = event.target.closest('.scroll-picker__item');
if (!item) return;
const val = parseInt(item.dataset.value, 10);
picker.scrollTo({
top: val * itemHeight,
behavior: 'smooth'
});
});
}
function handleTimerListClick(event) {
const removeBtn = event.target.closest('.timer-item__remove');
if (!removeBtn) {
return;
}
if (state.timers.length <= 1 || state.isRunning) {
return;
}
const itemEl = removeBtn.closest('.timer-item');
const timerId = itemEl.dataset.id;
state.timers = state.timers.filter((t) => t.id !== timerId);
render();
}
function handleSaveSequence() {
const name = dom.sequenceNameInput.value.trim();
if (!name) {
dom.sequenceNameInput.focus();
return;
}
const sequenceData = state.timers.map((t) => ({
hours: t.hours,
minutes: t.minutes,
seconds: t.seconds
}));
state.savedSequences.push({ name, timers: sequenceData });
persistSavedSequences();
dom.sequenceNameInput.value = '';
renderSequences();
}
function handleSequenceListClick(event) {
const btn = event.target.closest('.sequence-item__btn');
if (!btn) {
return;
}
const index = parseInt(btn.dataset.index, 10);
if (btn.classList.contains('sequence-item__btn--load')) {
loadSequence(index);
} else if (btn.classList.contains('sequence-item__btn--delete')) {
deleteSequence(index);
}
}
/* ===== Timer Logic ===== */
function startSequence() {
/* Validate at least one timer has time > 0 */
const totalAll = state.timers.reduce((sum, t) => sum + timerToSeconds(t), 0);
if (totalAll <= 0) {
flashDisplay();
return;
}
/* Find first non-zero timer */
state.currentIndex = state.timers.findIndex((t) => timerToSeconds(t) > 0);
if (state.currentIndex === -1) {
return;
}
state.isRunning = true;
state.isPaused = false;
const current = state.timers[state.currentIndex];
state.totalForCurrent = timerToSeconds(current);
state.remaining = state.totalForCurrent;
startInterval();
render();
}
function pauseSequence() {
state.isPaused = true;
clearInterval(state.intervalId);
state.intervalId = null;
render();
}
function resumeSequence() {
state.isPaused = false;
startInterval();
render();
}
function startInterval() {
state.intervalId = setInterval(tick, 1000);
}
function tick() {
state.remaining -= 1;
if (state.remaining <= 0) {
playBellSound();
flashDisplay();
/* Move to next timer */
const nextIndex = findNextTimer(state.currentIndex + 1);
if (nextIndex === -1) {
/* All timers done */
completeSequence();
return;
}
state.currentIndex = nextIndex;
const nextTimer = state.timers[state.currentIndex];
state.totalForCurrent = timerToSeconds(nextTimer);
state.remaining = state.totalForCurrent;
}
updateDisplay();
}
function findNextTimer(startFrom) {
for (let i = startFrom; i < state.timers.length; i++) {
if (timerToSeconds(state.timers[i]) > 0) {
return i;
}
}
return -1;
}
function completeSequence() {
clearInterval(state.intervalId);
state.intervalId = null;
state.isRunning = false;
state.isPaused = false;
state.remaining = 0;
state.currentIndex = 0;
document.title = 'Sequence Timer — Complete!';
render();
/* Reset title after a moment */
setTimeout(() => {
document.title = 'Sequence Timer — Multi-Timer Sequencer';
}, 3000);
}
function cancelAll() {
clearInterval(state.intervalId);
state.intervalId = null;
state.isRunning = false;
state.isPaused = false;
state.remaining = 0;
state.currentIndex = 0;
document.title = 'Sequence Timer — Multi-Timer Sequencer';
render();
}
function cancelAndReset() {
clearInterval(state.intervalId);
state.intervalId = null;
state.isRunning = false;
state.isPaused = false;
state.remaining = 0;
state.currentIndex = 0;
state.timers = [createTimer()];
document.title = 'Sequence Timer — Multi-Timer Sequencer';
render();
}
/* ===== Rendering ===== */
function render() {
renderDisplay();
renderControls();
renderTimerList();
renderSequenceDots();
renderSequences();
updateHint();
}
function updateDisplay() {
dom.timerDisplay.textContent = formatTime(state.remaining);
/* Progress bar */
const progress = state.totalForCurrent > 0
? ((state.totalForCurrent - state.remaining) / state.totalForCurrent) * 100
: 0;
dom.progressBar.style.width = `${progress}%`;
/* Info */
dom.timerInfo.textContent = `Timer ${state.currentIndex + 1} of ${state.timers.length}`;
/* Tab title */
document.title = `${formatTime(state.remaining)} — Sequence Timer`;
/* Dots */
renderSequenceDots();
}
function renderDisplay() {
/* Display state classes */
dom.display.classList.toggle('display--running', state.isRunning && !state.isPaused);
dom.display.classList.toggle('display--paused', state.isPaused);
if (state.isRunning) {
dom.timerDisplay.textContent = formatTime(state.remaining);
const progress = state.totalForCurrent > 0
? ((state.totalForCurrent - state.remaining) / state.totalForCurrent) * 100
: 0;
dom.progressBar.style.width = `${progress}%`;
dom.timerInfo.textContent = state.isPaused
? `Paused — Timer ${state.currentIndex + 1} of ${state.timers.length}`
: `Timer ${state.currentIndex + 1} of ${state.timers.length}`;
} else {
dom.timerDisplay.textContent = '00:00:00';
dom.progressBar.style.width = '0%';
dom.timerInfo.textContent = 'Ready';
}
}
function renderControls() {
if (state.isRunning && !state.isPaused) {
dom.btnStart.innerHTML = '<span class="controls__btn-icon">⏸</span> Pause';
} else if (state.isPaused) {
dom.btnStart.innerHTML = '<span class="controls__btn-icon">▶</span> Resume';
} else {
dom.btnStart.innerHTML = '<span class="controls__btn-icon">▶</span> Start';
}
dom.btnCancel.disabled = !state.isRunning;
dom.btnAddTimer.disabled = state.timers.length >= MAX_TIMERS || state.isRunning;
}
function renderTimerList() {
dom.timerList.innerHTML = state.timers.map((timer, index) => {
const isActive = state.isRunning && index === state.currentIndex;
const isDone = state.isRunning && index < state.currentIndex;
const activeClass = isActive ? ' timer-item--active' : '';
const doneClass = isDone ? ' timer-item--done' : '';
const disabledClass = state.isRunning ? ' timer-item--disabled' : '';
const canRemove = state.timers.length > 1 && !state.isRunning;
return `
<div class="timer-item${activeClass}${doneClass}${disabledClass}" data-id="${timer.id}">
<span class="timer-item__label">${index + 1}</span>
<div class="timer-item__time">
<div class="scroll-picker" data-field="hours" aria-label="Hours for timer ${index + 1}">
<div class="scroll-picker__list">
${generatePickerOptions(0, 99, timer.hours)}
</div>
</div>
<span class="timer-item__separator">:</span>
<div class="scroll-picker" data-field="minutes" aria-label="Minutes for timer ${index + 1}">
<div class="scroll-picker__list">
${generatePickerOptions(0, 59, timer.minutes)}
</div>
</div>
<span class="timer-item__separator">:</span>
<div class="scroll-picker" data-field="seconds" aria-label="Seconds for timer ${index + 1}">
<div class="scroll-picker__list">
${generatePickerOptions(0, 59, timer.seconds)}
</div>
</div>
<div class="timer-item__time-overlay"></div>
<div class="timer-item__time-mask"></div>
</div>
<button class="timer-item__remove" type="button"
${canRemove ? '' : 'disabled'}
aria-label="Remove timer ${index + 1}">×</button>
</div>`;
}).join('');
// After inserting the HTML, set the scroll offsets of all pickers programmatically and bind events
const timerItems = dom.timerList.querySelectorAll('.timer-item');
timerItems.forEach((itemEl) => {
const timerId = itemEl.dataset.id;
const timer = state.timers.find((t) => t.id === timerId);
if (!timer) return;
const pickers = itemEl.querySelectorAll('.scroll-picker');
pickers.forEach((picker) => {
const field = picker.dataset.field;
const value = timer[field];
const itemHeight = 34; // Must match CSS line-height
// Use requestAnimationFrame to ensure picker is fully laid out and scrollable
requestAnimationFrame(() => {
picker.scrollTop = value * itemHeight;
});
// Bind reactive scroll/click events
bindScrollPickerEvents(picker, timerId, field);
});
});
}
function renderSequenceDots() {
if (state.timers.length <= 1) {
dom.sequenceDots.innerHTML = '';
return;
}
dom.sequenceDots.innerHTML = state.timers.map((_, index) => {
let dotClass = 'display__dot';
if (state.isRunning && index === state.currentIndex) {
dotClass += ' display__dot--active';
} else if (state.isRunning && index < state.currentIndex) {
dotClass += ' display__dot--done';
}
return `<span class="${dotClass}"></span>`;
}).join('');
}
function renderSequences() {
if (state.savedSequences.length === 0) {
dom.sequenceList.innerHTML = '';
dom.sequencesEmpty.style.display = '';
return;
}
dom.sequencesEmpty.style.display = 'none';
dom.sequenceList.innerHTML = state.savedSequences.map((seq, index) => {
const timerCount = seq.timers.length;
const totalSec = seq.timers.reduce((sum, t) => sum + (t.hours * 3600 + t.minutes * 60 + t.seconds), 0);
return `
<div class="sequence-item">
<div class="sequence-item__info">
<div class="sequence-item__name">${escapeHtml(seq.name)}</div>
<div class="sequence-item__meta">${timerCount} timer${timerCount !== 1 ? 's' : ''} · ${formatTime(totalSec)} total</div>
</div>
<button class="sequence-item__btn sequence-item__btn--load" data-index="${index}" type="button">Load</button>
<button class="sequence-item__btn sequence-item__btn--delete" data-index="${index}" type="button">Delete</button>
</div>`;
}).join('');
}
function updateHint() {
const remaining = MAX_TIMERS - state.timers.length;
if (state.isRunning) {
dom.timerHint.textContent = 'Sequence is running...';
} else if (remaining <= 0) {
dom.timerHint.textContent = 'Maximum of 10 timers reached.';
} else {
dom.timerHint.textContent = `Add up to ${remaining} more timer${remaining !== 1 ? 's' : ''}. They will run one after another.`;
}
}
function flashDisplay() {
dom.display.classList.remove('display--flash');
/* Force reflow to restart animation */
void dom.display.offsetWidth;
dom.display.classList.add('display--flash');
setTimeout(() => dom.display.classList.remove('display--flash'), 600);
}
/* ===== Sequence Persistence ===== */
function loadSavedSequences() {
try {
const data = localStorage.getItem(STORAGE_KEY);
if (data) {
state.savedSequences = JSON.parse(data);
}
} catch {
state.savedSequences = [];
}
}
function persistSavedSequences() {
localStorage.setItem(STORAGE_KEY, JSON.stringify(state.savedSequences));
}
function loadSequence(index) {
if (state.isRunning) {
cancelAll();
}
const seq = state.savedSequences[index];
if (!seq) {
return;
}
state.timers = seq.timers.map((t) => ({
id: generateId(),
hours: t.hours,
minutes: t.minutes,
seconds: t.seconds
}));
render();
}
function deleteSequence(index) {
state.savedSequences.splice(index, 1);
persistSavedSequences();
renderSequences();
}
/* ===== Audio (Web Audio API Bell Chime with Mobile Autoplay Unlock) ===== */
function initAudioSystem() {
renderBellChime((buffer) => {
if (!buffer) {
console.warn('Could not pre-render bell chime offline. Fallback to live synthesis.');
return;
}
// Normalize the buffer for maximum possible loudness without clipping
normalizeBuffer(buffer);
try {
const wavBlob = bufferToWav(buffer);
const wavUrl = URL.createObjectURL(wavBlob);
dom.bellAudio = new Audio(wavUrl);
dom.bellAudio.volume = 1.0;
} catch (e) {
console.error('Failed to create HTML5 Audio from pre-rendered buffer:', e);
}
});
}
function unlockAudio() {
// 1. Unlock HTML5 Audio if initialized and not yet unlocked
if (dom && dom.bellAudio && !isHtml5AudioUnlocked) {
isHtml5AudioUnlocked = true;
const playPromise = dom.bellAudio.play();
if (playPromise !== undefined) {
playPromise.then(() => {
if (!isPlayingRealSound) {
dom.bellAudio.pause();
dom.bellAudio.currentTime = 0;
}
}).catch((err) => {
isHtml5AudioUnlocked = false;
console.log('HTML5 Audio unlock attempt status:', err.message);
});
}
}
// 2. Unlock/Initialize Web Audio AudioContext
if (globalAudioCtx) {
if (globalAudioCtx.state === 'suspended') {
globalAudioCtx.resume().catch((e) => console.warn('Context resume failed:', e));
}
return;
}
try {
globalAudioCtx = new (window.AudioContext || window.webkitAudioContext)();
// Play an instantaneous silent buffer to warm up mobile hardware context
const buffer = globalAudioCtx.createBuffer(1, 1, 22050);
const source = globalAudioCtx.createBufferSource();
source.buffer = buffer;
source.connect(globalAudioCtx.destination);
source.start(0);
} catch (e) {
console.warn('Audio Context initialization failed:', e);
}
}
function playBellSound() {
isPlayingRealSound = true;
// Attempt HTML5 Audio first (much more stable inside background/timer threads on mobile)
if (dom.bellAudio) {
try {
dom.bellAudio.pause();
dom.bellAudio.currentTime = 0;
const playPromise = dom.bellAudio.play();
if (playPromise !== undefined) {
playPromise.then(() => {
setTimeout(() => {
isPlayingRealSound = false;
}, 2500);
}).catch((err) => {
isPlayingRealSound = false;
console.warn('HTML5 Audio play failed, falling back to Web Audio API:', err);
playBellLive();
});
}
return;
} catch (e) {
isPlayingRealSound = false;
console.warn('HTML5 Audio play error, falling back to Web Audio API:', e);
}
}
playBellLive();
}
function playBellLive() {
isPlayingRealSound = true;
unlockAudio();
if (!globalAudioCtx) {
isPlayingRealSound = false;
return;
}
const now = globalAudioCtx.currentTime;
// C5 (523.25), E5 (659.25), G5 (783.99), and C6 (1046.5) for high clarity
const frequencies = [523.25, 659.25, 783.99, 1046.5];
const gains = [0.35, 0.25, 0.20, 0.15];
frequencies.forEach((freq, i) => {
const osc = globalAudioCtx.createOscillator();
const gainNode = globalAudioCtx.createGain();
osc.type = 'sine';
osc.frequency.setValueAtTime(freq, now);
gainNode.gain.setValueAtTime(gains[i] * 0.8, now); // scale slightly to prevent clipping in sum
gainNode.gain.exponentialRampToValueAtTime(0.0001, now + 2.2);
osc.connect(gainNode);
gainNode.connect(globalAudioCtx.destination);
osc.start(now);
osc.stop(now + 2.5);
});
// Metallic transient strike
const strikeOsc = globalAudioCtx.createOscillator();
const strikeGain = globalAudioCtx.createGain();
strikeOsc.type = 'triangle';
strikeOsc.frequency.setValueAtTime(1500, now);
strikeGain.gain.setValueAtTime(0.12, now);
strikeGain.gain.exponentialRampToValueAtTime(0.0001, now + 0.08);
strikeOsc.connect(strikeGain);
strikeGain.connect(globalAudioCtx.destination);
strikeOsc.start(now);
strikeOsc.stop(now + 0.1);
setTimeout(() => {
isPlayingRealSound = false;
}, 2500);
}
function handleTestSound() {
// Play sound directly, which naturally handles unlocking
playBellSound();
// Visual feedback for playing state
const originalHTML = dom.btnTestSound.innerHTML;
dom.btnTestSound.innerHTML = '<span class="controls__btn-icon">🔔</span> Playing...';
dom.btnTestSound.disabled = true;
setTimeout(() => {
dom.btnTestSound.innerHTML = originalHTML;
dom.btnTestSound.disabled = false;
}, 1500);
}
function renderBellChime(callback) {
const sampleRate = 44100;
const duration = 2.5;
let offlineCtx;
try {
offlineCtx = new (window.OfflineAudioContext || window.webkitOfflineAudioContext)(1, sampleRate * duration, sampleRate);
} catch (e) {
console.warn('OfflineAudioContext not supported:', e);
callback(null);
return;
}
const now = 0;
const frequencies = [523.25, 659.25, 783.99, 1046.5];
const gains = [0.35, 0.25, 0.20, 0.15];
frequencies.forEach((freq, idx) => {
const osc = offlineCtx.createOscillator();
const gainNode = offlineCtx.createGain();
osc.type = 'sine';
osc.frequency.setValueAtTime(freq, now);
gainNode.gain.setValueAtTime(gains[idx], now);
gainNode.gain.exponentialRampToValueAtTime(0.0001, duration);
osc.connect(gainNode);
gainNode.connect(offlineCtx.destination);
osc.start(now);
osc.stop(duration);
});
// Triangle strike transient
const strikeOsc = offlineCtx.createOscillator();
const strikeGain = offlineCtx.createGain();
strikeOsc.type = 'triangle';
strikeOsc.frequency.setValueAtTime(1500, now);
strikeGain.gain.setValueAtTime(0.15, now);
strikeGain.gain.exponentialRampToValueAtTime(0.0001, 0.08);
strikeOsc.connect(strikeGain);
strikeGain.connect(offlineCtx.destination);
strikeOsc.start(now);
strikeOsc.stop(0.1);
offlineCtx.startRendering().then((renderedBuffer) => {
callback(renderedBuffer);
}).catch((err) => {
console.error('Offline rendering failed:', err);
callback(null);
});
}
function normalizeBuffer(buffer) {
const numOfChan = buffer.numberOfChannels;
let maxVal = 0;
// Find peak value
for (let c = 0; c < numOfChan; c++) {
const data = buffer.getChannelData(c);
for (let i = 0; i < data.length; i++) {
const absVal = Math.abs(data[i]);
if (absVal > maxVal) {
maxVal = absVal;
}
}
}
// Scale values to peak at 0.98 amplitude (loud and clean)
if (maxVal > 0) {
const scale = 0.98 / maxVal;
for (let c = 0; c < numOfChan; c++) {
const data = buffer.getChannelData(c);
for (let i = 0; i < data.length; i++) {
data[i] *= scale;
}
}
}
}
function bufferToWav(buffer) {
const numOfChan = buffer.numberOfChannels;
const length = buffer.length * numOfChan * 2 + 44;
const bufferArr = new ArrayBuffer(length);
const view = new DataView(bufferArr);
const channels = [];
let i;
let sample;
let offset = 0;
let pos = 0;
// RIFF WAV Header
setUint32(0x46464952); // "RIFF"
setUint32(length - 8); // chunk size
setUint32(0x45564157); // "WAVE"
setUint32(0x20746d66); // "fmt " subchunk
setUint32(16); // subchunk size
setUint16(1); // audio format (1 = PCM)
setUint16(numOfChan);
setUint32(buffer.sampleRate);
setUint32(buffer.sampleRate * numOfChan * 2); // byte rate
setUint16(numOfChan * 2); // block align
setUint16(16); // bits per sample
setUint32(0x61746164); // "data" subchunk
setUint32(length - pos - 4);
for (i = 0; i < buffer.numberOfChannels; i++) {
channels.push(buffer.getChannelData(i));
}
while (pos < length - 4) {
for (i = 0; i < numOfChan; i++) {
sample = Math.max(-1, Math.min(1, channels[i][offset]));
sample = sample < 0 ? sample * 0x8000 : sample * 0x7FFF;
view.setInt16(pos, sample, true);
pos += 2;
}
offset++;
}
return new Blob([bufferArr], { type: 'audio/wav' });
function setUint16(data) {
view.setUint16(pos, data, true);
pos += 2;
}
function setUint32(data) {
view.setUint32(pos, data, true);
pos += 4;
}
}
/* ===== Utilities ===== */
function createTimer() {
return {
id: generateId(),
hours: 0,
minutes: 0,
seconds: 0
};
}
function generateId() {
return `t_${Date.now()}_${Math.random().toString(36).substring(2, 8)}`;
}
function timerToSeconds(timer) {
return timer.hours * 3600 + timer.minutes * 60 + timer.seconds;
}
function formatTime(totalSeconds) {
const hours = Math.floor(totalSeconds / 3600);
const minutes = Math.floor((totalSeconds % 3600) / 60);
const seconds = totalSeconds % 60;
return `${String(hours).padStart(2, '0')}:${String(minutes).padStart(2, '0')}:${String(seconds).padStart(2, '0')}`;
}
function escapeHtml(text) {
const div = document.createElement('div');
div.textContent = text;
return div.innerHTML;
}
function generatePickerOptions(min, max, currentValue) {
let html = '';
for (let i = min; i <= max; i++) {
const valStr = String(i).padStart(2, '0');
const selectedClass = i === currentValue ? ' scroll-picker__item--selected' : '';
html += `<div class="scroll-picker__item${selectedClass}" data-value="${i}">${valStr}</div>`;
}
return html;
}