-
Notifications
You must be signed in to change notification settings - Fork 0
/
speech.js
52 lines (41 loc) · 1.4 KB
/
speech.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
/*
* Check for browser support
*/
var supportMsg = '';
if ('speechSynthesis' in window) {
supportMsg = 'Your browser <strong>supports</strong> speech synthesis.';
} else {
supportMsg = 'Sorry your browser <strong>does not support</strong> speech synthesis.<br>Try this in <a href="https://www.google.co.uk/intl/en/chrome/browser/canary.html">Chrome Canary</a>.';
}
// Fetch the list of voices and populate the voice options.
function loadVoices() {
// Fetch the available voices.
var voices = speechSynthesis.getVoices();
return voices;
}
// Execute loadVoices.
loadVoices();
// Chrome loads voices asynchronously.
window.speechSynthesis.onvoiceschanged = function(e) {
loadVoices();
};
// Create a new utterance for the specified text and add it to
// the queue.
function speak(text) {
// Create a new instance of SpeechSynthesisUtterance.
var msg = new SpeechSynthesisUtterance();
// Set the text.
msg.text = text;
// Set the attributes.
msg.volume = parseFloat(volumeInput.value);
msg.rate = parseFloat(rateInput.value);
msg.pitch = parseFloat(pitchInput.value);
// If a voice has been selected, find the voice and set the
// utterance instance's voice attribute.
if (voiceSelect.value) {
msg.voice = speechSynthesis.getVoices()
.filter(function(voice) { return voice.name == voiceSelect.value; })[0];
}
// Queue this utterance.
window.speechSynthesis.speak(msg);
}