-
-
Notifications
You must be signed in to change notification settings - Fork 828
/
ttstest.html
92 lines (82 loc) · 2.84 KB
/
ttstest.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>TTS Audio Stream Demo</title>
<style>
body {
font-family: Arial, sans-serif;
max-width: 800px;
margin: 0 auto;
padding: 20px;
}
textarea {
width: 100%;
height: 100px;
}
button, select {
margin-top: 10px;
}
</style>
</head>
<body>
<h1>TTS Audio Stream Demo</h1>
<textarea id="text-input" placeholder="Enter text to speak"></textarea>
<br>
<select id="voice-select"></select>
<br>
<button id="speak-button">Speak</button>
<button id="pause-button">Pause</button>
<button id="resume-button">Resume</button>
<button id="stop-button">Stop</button>
<script>
const textInput = document.getElementById('text-input');
const voiceSelect = document.getElementById('voice-select');
const speakButton = document.getElementById('speak-button');
const pauseButton = document.getElementById('pause-button');
const resumeButton = document.getElementById('resume-button');
const stopButton = document.getElementById('stop-button');
let voices = [];
function loadVoices() {
voices = speechSynthesis.getVoices();
voiceSelect.innerHTML = voices
.map((voice, index) => `<option value="${index}">${voice.name} (${voice.lang})</option>`)
.join('');
}
// Load voices when they are ready
speechSynthesis.onvoiceschanged = loadVoices;
// Initial load attempt
loadVoices();
function speak(text) {
const utterance = new SpeechSynthesisUtterance(text);
const selectedVoice = voices[voiceSelect.value];
if (selectedVoice) {
utterance.voice = selectedVoice;
}
utterance.addEventListener('start', () => {
console.log('Speech started');
});
utterance.addEventListener('end', () => {
console.log('Speech ended');
});
speechSynthesis.speak(utterance);
}
speakButton.addEventListener('click', () => {
const text = textInput.value;
if (text) {
speak(text);
}
});
pauseButton.addEventListener('click', () => {
speechSynthesis.pause();
});
resumeButton.addEventListener('click', () => {
speechSynthesis.resume();
});
stopButton.addEventListener('click', () => {
speechSynthesis.cancel();
});
</script>
</body>
</html>