forked from mieky/pulinaboksi
-
Notifications
You must be signed in to change notification settings - Fork 7
/
script2.js
137 lines (121 loc) · 4.29 KB
/
script2.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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
"use strict";
var synth = window.speechSynthesis;
var lang = "fi-FI";
var voice = null;
// Pronounciation fixes for letters the browser can't say right in Finnish
var CHARACTER_PRONOUNCIATIONS = {
"i": "ii",
",": "pilkku",
".": "piste",
";": "puolipiste",
":": "kaksoispiste",
">": "suurempi kuin merkki",
"?": "kysymysmerkki",
"!": "huutomerkki",
"∞": "ääretön",
")": "oikea kaarisulje",
"]": "oikea hakasulje",
"}": "oikea aaltosulje",
"≠": "erisuuri",
"≈": "likimäärin",
"√": "neliöjuuri",
"å": "ruotsalainen å"
};
var WORDS = [
"aasi", "aita", "amme", "appelsiini", "aurinko", "auto",
"banaani", "banaani", "banaani", "blimpsisblömpsis böbberöö",
"celsius", "cowboy",
"delfiini", "dinosaurus", "donitsi",
"etana", "elokuva",
"faarao", "farkut",
"gerbiili", "gorilla",
"haarukka", "halla", "hattara", "helle", "helistin", "hömppä", "höpsö","hanna",
"ilta", "imuri", "intiaani", "isi", "iskä",
"jekku", "jalkapallo", "jakkara", "jukurtti", "ju-na", "jänis", "jäätelö",
"kakku", "kameli", "karkki", "kello", "kivi", "kiivi", "kilpikonna", "kuppi", "karhu", "koira", "käärme", "kala",
"kana", "kesä", "kevät", "kännykkä", "koko",
"lamppu", "lapsi", "lasi", "lautanen", "leijona", "leikki", "lentokone", "liukumäki", "lyhty", "lintu", "logo",
"mi-ke", "makkara", "makarooni", "mopo", "myyrä", "muurahainen", "möhköfantti", "mörkö",
"naru", "nökö", "nalle", "nakupelle", "nenä", "norsu", "näk'käri",
"orava", "omena", "okko", "oja",
"peppi pitkätossu", "paloauto", "parkkipaikka", "piirakka", "piimä", "pilli", "poliisi", "potta", "puuro", "pöytä", "papukaija",
"quesadilla",
"rusina", "raketti",
"salaatti", "sauna", "sininen", "sipuli", "soittopeli", "suihku", "syksy",
"taika", "talvi", "takka", "talvi", "tarra", "telkkari", "tietokone", "tikkari", "timantti", "trampoliini", "tyyny",
"uimahousut",
"veitsi", "västäräkki", "voltti", "vilja", "varpu", "valkeapää", "vegepöni",
"watti",
"yö",
"zumba",
"äiti",
"öylätti"
];
// Munge the words array into a convenient hash indexed by the starting letter
var CHARACTER_TO_WORD = (function(words) {
return words.reduce(function(acc, word) {
var letter = word[0];
if (!acc[letter]) {
acc[letter] = [];
}
acc[letter].push(word);
return acc;
}, {})
}(WORDS));
function findVoice(lang) {
var voices = window.speechSynthesis.getVoices();
for (var i = 0; i < voices.length; i++) {
if (voices[i].lang === lang) {
return voices[i];
}
}
return undefined;
}
voice = findVoice(lang);
if (speechSynthesis.onvoiceschanged !== undefined) {
speechSynthesis.onvoiceschanged = function() {
voice = findVoice(lang);
}
}
// document.body.addEventListener("keypress", function(e) {
// var str = ("" + String.fromCharCode(e.keyCode)).toLowerCase();
// sayWordForCharacter(str[0]);
// });
// document.body.addEventListener("click", function(e) {
// var character = getRandomCharacter();
// sayWordForCharacter(character);
// });
// document.body.addEventListener("touchstart", function(e) {
// var character = getRandomCharacter();
// sayWordForCharacter(character);
// });
document.querySelector('#speak-input').addEventListener("input", function(e) {
sayWordForCharacter();
})
function getRandomCharacter() {
return String.fromCharCode(Math.round(Math.random() * (122 - 97) + 97));
}
function showCharacter(character) {
var charactersToShow = character.toUpperCase();
if (character.toLowerCase() !== character.toUpperCase()) {
charactersToShow += " " + character.toLowerCase();
}
document.querySelector(".character").innerText = charactersToShow;
}
function hideInfo() {
document.querySelector(".info").classList.add("successfully-used");
}
function sayWordForCharacter() {
const character = 'x';
if (synth.speaking) {
synth.cancel();
}
var phrase = document.querySelector('#speak-input').value;;
showCharacter(phrase);
hideInfo();
var utterThis = new SpeechSynthesisUtterance(phrase);
utterThis.voice = voice;
utterThis.pitch = 1;
utterThis.rate = 0.9;
synth.speak(utterThis);
}