-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
206 lines (170 loc) · 6.11 KB
/
script.js
File metadata and controls
206 lines (170 loc) · 6.11 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
// Alphabet configuration
const alphabetData = [
{ letter: 'A', word: 'Allosaurus' },
{ letter: 'B', word: 'Banana' },
{ letter: 'C', word: 'Cat' },
{ letter: 'D', word: 'Diplodocus' },
{ letter: 'E', word: 'Elephant' },
{ letter: 'F', word: 'Fruit' },
{ letter: 'G', word: 'Giraffe' },
{ letter: 'H', word: 'Hedgehog' },
{ letter: 'I', word: 'Iguana' },
{ letter: 'J', word: 'Jam' },
{ letter: 'K', word: 'Kangaroo' },
{ letter: 'L', word: 'Lemon' },
{ letter: 'M', word: 'Monkey' },
{ letter: 'N', word: 'Narwhal' },
{ letter: 'O', word: 'Orange' },
{ letter: 'P', word: 'Panda' },
{ letter: 'Q', word: 'Quail' },
{ letter: 'R', word: 'Raptor' },
{ letter: 'S', word: 'Snake' },
{ letter: 'T', word: 'T-Rex' },
{ letter: 'U', word: 'Unicorn' },
{ letter: 'V', word: 'Velociraptor' },
{ letter: 'W', word: 'Watermelon' },
{ letter: 'X', word: 'Fox' },
{ letter: 'Y', word: 'Yak' },
{ letter: 'Z', word: 'Zebra' }
];
const mediaPairs = alphabetData.map(item => ({
letter: item.letter,
image: `assets/letter_${item.letter.toLowerCase()}.png`,
sound: `assets/sounds/letter_${item.letter.toLowerCase()}.mp3`,
word: item.word
}));
let currentIndex = 0;
let isTransitioning = false;
let audioUnlocked = false;
// Pre-load voices for browsers that support it
let voices = [];
function loadVoices() {
// Strictly English only (en-US, en-GB, en-AU, etc.)
const allVoices = window.speechSynthesis.getVoices();
voices = allVoices.filter(v => v.lang.toLowerCase().startsWith('en'));
if (voices.length > 0) {
console.log(`Loaded ${voices.length} English voices`);
}
}
loadVoices();
if (window.speechSynthesis.onvoiceschanged !== undefined) {
window.speechSynthesis.onvoiceschanged = loadVoices;
}
// Persistent Audio Element (Crucial for iOS)
const audioPlayer = new Audio();
audioPlayer.preload = 'auto';
// DOM Elements
const mainImage = document.getElementById('main-image');
const imageContainer = document.getElementById('image-container');
const transitionOverlay = document.getElementById('transition-overlay');
/**
* Play sound synchronously within the user gesture context.
*/
function triggerSound(index) {
const pair = mediaPairs[index];
// Stop current
audioPlayer.pause();
// Set new source and play
audioPlayer.src = pair.sound;
const playPromise = audioPlayer.play();
if (playPromise !== undefined) {
playPromise.catch(error => {
console.log("Audio file blocked or missing, trying speech fallback:", error);
speakLetterFallback(pair.letter, pair.word);
});
}
}
/**
* Fallback to Speech Synthesis.
* Note: On iOS, this MUST be triggered during the user swipe/click.
*/
function speakLetterFallback(letter, word) {
const ssu = new SpeechSynthesisUtterance(`${letter}! ${letter} is for ${word}!`);
// Ensure we have voices
if (voices.length === 0) loadVoices();
// Pick a random voice if available
if (voices.length > 0) {
ssu.voice = voices[Math.floor(Math.random() * voices.length)];
}
// Even if there's only one voice, randomize pitch and rate for variety!
ssu.rate = 0.7 + Math.random() * 0.4; // Random rate between 0.7 and 1.1
ssu.pitch = 0.8 + Math.random() * 1.0; // Random pitch between 0.8 and 1.8
window.speechSynthesis.cancel();
window.speechSynthesis.speak(ssu);
}
/**
* Initial "Unlock" for iOS.
* Triggers both an empty speech and the first audio play to satisfy Safari's gesture requirements.
*/
function unlockAudio() {
if (audioUnlocked) return;
// 1. Unlocking SpeechSynthesis (the "empty speak" trick)
const silentSpeak = new SpeechSynthesisUtterance('');
silentSpeak.volume = 0;
window.speechSynthesis.speak(silentSpeak);
// 2. Unlocking HTML5 Audio
triggerSound(currentIndex);
audioUnlocked = true;
console.log("Speech & Audio Unlocked for Safari");
}
function goToImage(index) {
if (isTransitioning) return;
isTransitioning = true;
// Trigger sound IMMEDIATELY in gesture context
triggerSound(index);
// Visual transition
transitionOverlay.classList.add('active');
mainImage.classList.add('fade-out');
setTimeout(() => {
currentIndex = index;
loadCurrentImage();
setTimeout(() => {
transitionOverlay.classList.remove('active');
mainImage.classList.remove('fade-out');
mainImage.classList.add('fade-in');
setTimeout(() => {
mainImage.classList.remove('fade-in');
isTransitioning = false;
}, 500);
}, 100);
}, 400);
}
function loadCurrentImage() {
mainImage.classList.add('loading');
const pair = mediaPairs[currentIndex];
mainImage.src = pair.image;
mainImage.alt = `Letter ${pair.letter} - ${pair.word}`;
mainImage.onload = () => mainImage.classList.remove('loading');
}
function handleClick(e) {
// Safari restriction: all playback must start synchronously in the click handler
if (!audioUnlocked) {
unlockAudio();
// Stay on first letter for the first click to allow user to hear 'A'
return;
}
if (isTransitioning) return;
const nextIndex = (currentIndex + 1) % mediaPairs.length;
goToImage(nextIndex);
}
function init() {
loadCurrentImage();
imageContainer.addEventListener('click', handleClick);
// Handle Space/Arrow keys (will also attempt unlock)
document.addEventListener('keydown', (e) => {
if (!audioUnlocked) unlockAudio();
if (e.key === 'ArrowRight' || e.key === ' ') {
e.preventDefault();
goToImage((currentIndex + 1) % mediaPairs.length);
} else if (e.key === 'ArrowLeft') {
e.preventDefault();
const prevIndex = (currentIndex - 1 + mediaPairs.length) % mediaPairs.length;
goToImage(prevIndex);
}
});
}
document.addEventListener('DOMContentLoaded', init);
// Prevent dragging/scrolling (iOS bounce fix)
document.addEventListener('touchmove', (e) => {
if (e.scale !== 1) e.preventDefault();
}, { passive: false });