-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts.js
193 lines (180 loc) · 5.89 KB
/
scripts.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
"use strict";
(function (window, undefined) {
var document = window.document;
var docElement = document.documentElement;
var docBody = document.body;
var micBtn = document.getElementById('activateMic');
var consoleLog = document.getElementById('consoleLog');
var speechRecognition = window.webkitSpeechRecognition || window.mozSpeechRecognition || window.msSpeechRecognition || window.oSpeechRecognition || window.SpeechRecognition;
var speechGrammarList = window.webkitSpeechGrammarList || window.mozSpeechGrammarList || window.msSpeechGrammarList || window.oSpeechGrammarList || window.SpeechGrammarList;
var speechSynthesis = window.speechSynthesis;
var data = [{
"emoji": "🐀",
"hexncr": "🐀",
"utf16": "\uD83D\uDC00",
"lang.en": "rat",
"lang.zh": "鼠"
}, {
"emoji": "🐄",
"hexncr": "🐄",
"utf16": "\uD83D\uDC04",
"lang.en": "ox",
"lang.zh": "牛"
}, {
"emoji": "🐅",
"hexncr": "🐅",
"utf16": "\uD83D\uDC05",
"lang.en": "tiger",
"lang.zh": "虎"
}, {
"emoji": "🐇",
"hexncr": "🐇",
"utf16": "\uD83D\uDC07",
"lang.en": "rabbit",
"lang.zh": "兔"
}, {
"emoji": "🐉",
"hexncr": "🐉",
"utf16": "\uD83D\uDC09",
"lang.en": "dragon",
"lang.zh": "龙"
}, {
"emoji": "🐍",
"hexncr": "🐍",
"utf16": "\uD83D\uDC0D",
"lang.en": "snake",
"lang.zh": "蛇"
}, {
"emoji": "🐎",
"hexncr": "🐎",
"utf16": "\uD83D\uDC0E",
"lang.en": "horse",
"lang.zh": "马"
}, {
"emoji": "🐐",
"hexncr": "🐐",
"utf16": "\uD83D\uDC10",
"lang.en": "goat",
"lang.zh": "羊"
}, {
"emoji": "🐒",
"hexncr": "🐒",
"utf16": "\uD83D\uDC12",
"lang.en": "monkey",
"lang.zh": "猴"
}, {
"emoji": "🐓",
"hexncr": "🐓",
"utf16": "\uD83D\uDC13",
"lang.en": "chicken",
"lang.zh": "鸡"
}, {
"emoji": "🐕",
"hexncr": "🐕",
"utf16": "\uD83D\uDC15",
"lang.en": "dog",
"lang.zh": "狗"
}, {
"emoji": "🐖",
"hexncr": "🐖",
"utf16": "\uD83D\uDC16",
"lang.en": "pig",
"lang.zh": "猪"
}];
var zodiac = data.map(function (animal) {
return animal['lang.zh'];
});
var grammar = '#JSGF V1.0; grammar zodiac; public <zodiac> = ' + zodiac.join(' | ') + ' ;';
var voices = [];
docElement.className = docElement.className.replace(/(^|\s)no-js(\s|$)/, '$1js$2');
if (speechRecognition !== undefined) {
addClass('speech');
detectSpeech();
} else {
addClass('no-speech');
}
function addClass(className) {
docElement.className = "".concat(docElement.className, " ").concat(className);
}
function detectSpeech() {
var recognition = new speechRecognition();
var speechRecognitionList = new speechGrammarList();
speechRecognitionList.addFromString(grammar, 1);
recognition.grammars = speechRecognitionList;
recognition.lang = 'cmn-Hans-CN';
micBtn.addEventListener('click', function () {
recognition.start();
consoleLog.innerHTML = '语音识别功能激活!请对着麦克风讲话。';
}, false);
recognition.onresult = function (event) {
var last = event.results.length - 1;
var animal = event.results[last][0].transcript;
consoleLog.innerHTML = '您应该说了: ' + animal + '.\n置信度: ' + event.results[0][0].confidence;
activateEmoji(animal);
readResponse('您的生肖是' + animal + '吧');
};
recognition.onspeechend = function () {
recognition.stop();
};
recognition.onnomatch = function () {
consoleLog.innerHTML = '不好意思,听不懂您说了什么。';
};
recognition.onerror = function (event) {
if (event.error == 'no-speech') {
consoleLog.innerHTML = '未检测到语音,请再试一次。';
} else {
consoleLog.innerHTML = 'Recognition error: ' + event.error;
}
};
}
function readResponse(result) {
docBody.style.setProperty('--display', 'block');
populateVoiceList();
speechSynthesis.addEventListener('voiceschanged', function () {
populateVoiceList();
});
var responseForm = document.getElementById('hearResponse');
responseForm.addEventListener('submit', function (event) {
event.preventDefault();
var select = document.getElementById('pickVoice');
speechSynthesis.cancel();
var utterStuff = new SpeechSynthesisUtterance(result);
var selectedVoice = select.selectedOptions[0].getAttribute('data-name');
voices.forEach(function (voice) {
if (voice.name === selectedVoice) {
utterStuff.voice = voice;
}
});
speechSynthesis.speak(utterStuff);
}, false);
}
function populateVoiceList() {
var select = document.getElementById('pickVoice');
voices = speechSynthesis.getVoices();
voices.forEach(function (voice) {
var lang = voice.lang;
if (lang.includes('zh')) {
var option = document.createElement('option');
option.textContent = voice.name + ' (' + voice.lang + ')';
option.setAttribute('data-lang', voice.lang);
option.setAttribute('data-name', voice.name);
select.appendChild(option);
}
});
}
function activateEmoji(result) {
var animalData = data.filter(function (animal) {
return animal['lang.zh'] === result;
});
if (typeof animalData[0] !== 'undefined') {
var animal = animalData[0]['lang.zh'];
var emoji = animalData[0]['hexncr'];
var zodiacContainer = document.querySelector('[data-animal=' + animal + ']');
var zodiacMarkup = "<span class=\"emoji\" role=\"img\" tabindex=\"0\" aria-label=\"".concat(animal, "\">").concat(emoji, "</span>");
zodiacContainer.innerHTML = zodiacMarkup;
} else {
/* To be replaced with some proper error handling */
console.log('不在十二生肖内');
}
}
})(window);