-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvoices.js
34 lines (30 loc) · 908 Bytes
/
voices.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
function userLanguageInFiveCharacters() {
return 'nl-NL'
return window.navigator.language
}
function userLanguageInTwoCharacters() {
var lang = userLanguageInFiveCharacters()
var twoCharacters = lang.match(/^.{2}/g)[0]
return twoCharacters
}
var synth = window.speechSynthesis
var voices = []
function Speak(languageInFiveCharacters, text) {
for (var i = 0; i < voices.length; i++) {
var voice = voices[i]
console.log("speak in " + languageInFiveCharacters + ": " + text)
if (voice.lang === languageInFiveCharacters) {
var utterance = new SpeechSynthesisUtterance(text)
utterance.voice = voice
synth.speak(utterance)
return
}
}
}
console.log("Speak defined")
window.speechSynthesis.onvoiceschanged = function () {
console.log("⚠️ voices have loaded")
window.speechSynthesis.getVoices().forEach(function(voice) {
voices.push(voice)
})
}