-
Notifications
You must be signed in to change notification settings - Fork 0
/
ayat.js
74 lines (61 loc) · 2.15 KB
/
ayat.js
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
var originalText = document.getElementById("original-text").textContent;
var words = originalText.split(" ");
var index = 0;
var speed = 1.0;
var isPaused = false;
var isPlaying = false;
var playButton = document.getElementById("play-button");
var highlightedIndexes = []; // Indexes of words already highlighted
function readNextWord() {
if (isPaused) {
return; // If paused, do not proceed to the next word
}
if (index < words.length) {
var word = words[index];
var utterance = new SpeechSynthesisUtterance(word);
// Select a French male voice (if available)
var voices = speechSynthesis.getVoices();
var frenchMaleVoice = voices.find(voice => voice.lang === 'fr-FR' && voice.name.includes('Male'));
if (frenchMaleVoice) {
utterance.voice = frenchMaleVoice;
} else {
// Use default voice if no French male voice is available
utterance.voice = voices.find(voice => voice.default);
}
utterance.rate = 1.5 ;
utterance.pause = 0;
var textElement = document.getElementById("original-text");
var originalContent = textElement.innerHTML;
if (!highlightedIndexes.includes(index)) {
words[index] = '<span class="highlight">' + word + '</span>';
textElement.innerHTML = words.join(" ");
highlightedIndexes.push(index);
}
utterance.onend = function() {
index++;
readNextWord();
};
speechSynthesis.speak(utterance);
} else {
isPlaying = false;
playButton.textContent = "Lire";
}
}
playButton.addEventListener("click", function() {
if (!isPlaying) {
if (index >= words.length) {
index = 0;
}
isPlaying = true;
playButton.textContent = "Pause";
readNextWord();
} else {
isPaused = !isPaused;
if (isPaused) {
playButton.textContent = "Continuer";
} else {
playButton.textContent = "Pause";
readNextWord(); // Resume playback after pause
}
}
});