-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdemo.html
104 lines (80 loc) · 2.68 KB
/
demo.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
93
94
95
96
97
98
99
100
101
102
103
104
<!DOCTYPE html>
<html lang="en">
<head>
<title>Basic-TTS Demo</title>
<style>
a {
color: black;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
hr {
margin-top: 1em;
}
#voice-container {
margin-bottom: 1em;
}
#voices {
outline: none;
padding: 0.25em;
}
</style>
<script src="basic-tts.js"></script>
<script>
window.speechSynthesis.onvoiceschanged = () => {
const mappings = {};
const voices = document.getElementById("voices");
const voiceList = window.speechSynthesis.getVoices();
console.log(`Loaded ${voiceList.length} voice(s)`);
for (const voice of voiceList) {
const option = document.createElement("option");
mappings[voice.voiceURI] = voice;
option.value = voice.voiceURI;
option.label = `${voice.name} (${voice.lang})`;
voices.appendChild(option);
}
window.speechSynthesis.onvoiceschanged = () => {};
const speak = () => {
const voice = mappings[voices.value];
if (!voice) {
console.warn(`No voice found for: ${voices.value}`)
}
const speaker = tts.createSpeaker({
voice: voice.name,
lang: voice.lang,
volume: 1,
pitch: 1,
rate: 1
});
speaker.speak("Hello! This is text to speech").then(() => {
console.log("The speaker has spoken!");
}).catch((err) => {
console.warn(`An error has occurred:`);
console.log(err);
console.log("Sigh...the speaker did not speak :(");
});
};
const button = document.getElementById("activate");
button.onclick = speak;
button.disabled = true;
voices.onchange = () => {
const selected = voices.value;
button.disabled = !mappings[selected];
};
}
</script>
</head>
<body>
<h1>Voice Test</h1>
<div id="voice-container">
<select id="voices">
<option disabled selected value="">Select Voice...</option>
</select>
</div>
<button id="activate">Speak!</button>
<hr>
<span>👉</span><a href="https://github.com/gfyoung/basic-tts/blob/master/demo.html"><strong>Source Code</strong></a>
</body>
</html>