-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
224 lines (194 loc) · 5.91 KB
/
main.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*!
*
* WebRTC Lab
* @author dodortus (codejs.co.kr / [email protected])
*
*/
$(function() {
if (typeof webkitSpeechRecognition != 'function') {
alert('크롬에서만 동작 합니다.');
return false;
}
var recognition = new webkitSpeechRecognition();
var isRecognizing = false;
var ignoreOnend = false;
var finalTranscript = '';
var audio = document.getElementById('audio');
var $btnMic = $('#btn-mic');
var $result = $('#result');
var $iconMusic = $('#icon-music');
recognition.continuous = true;
recognition.interimResults = true;
recognition.onstart = function() {
console.log('onstart', arguments);
isRecognizing = true;
$btnMic.attr('class', 'on');
};
recognition.onend = function() {
console.log('onend', arguments);
isRecognizing = false;
if (ignoreOnend) {
return false;
}
// DO end process
$btnMic.attr('class', 'off');
if (!finalTranscript) {
console.log('empty finalTranscript');
return false;
}
if (window.getSelection) {
window.getSelection().removeAllRanges();
var range = document.createRange();
range.selectNode(document.getElementById('final-span'));
window.getSelection().addRange(range);
}
};
recognition.onresult = function(event) {
console.log('onresult', event);
var interimTranscript = '';
if (typeof(event.results) == 'undefined') {
recognition.onend = null;
recognition.stop();
return;
}
for (var i = event.resultIndex; i < event.results.length; ++i) {
if (event.results[i].isFinal) {
finalTranscript += event.results[i][0].transcript;
} else {
interimTranscript += event.results[i][0].transcript;
}
}
finalTranscript = capitalize(finalTranscript);
final_span.innerHTML = linebreak(finalTranscript);
interim_span.innerHTML = linebreak(interimTranscript);
console.log('finalTranscript', finalTranscript);
console.log('interimTranscript', interimTranscript);
fireCommand(interimTranscript);
};
/**
* changeColor
*
*/
/*
.red { background: red; }
.blue { background: blue; }
.green { background: green; }
.yellow { background: yellow; }
.orange { background: orange; }
.grey { background: grey; }
.gold { background: gold; }
.white { background: white; }
.black { background: black; }
*/
function fireCommand(string) {
if (string.endsWith('레드')) {
$result.attr('class', 'red');
} else if (string.endsWith('블루')) {
$result.attr('class', 'blue');
} else if (string.endsWith('그린')) {
$result.attr('class', 'green');
} else if (string.endsWith('옐로우')) {
$result.attr('class', 'yellow');
} else if (string.endsWith('오렌지')) {
$result.attr('class', 'orange');
} else if (string.endsWith('그레이')) {
$result.attr('class', 'grey');
} else if (string.endsWith('골드')) {
$result.attr('class', 'gold');
} else if (string.endsWith('화이트')) {
$result.attr('class', 'white');
} else if (string.endsWith('블랙')) {
$result.attr('class', 'black');
} else if (string.endsWith('알람') || string.endsWith('알 람')) {
alert('알람');
} else if (string.endsWith('노래 켜') || string.endsWith('음악 켜')) {
audio.play();
$iconMusic.addClass('visible');
} else if (string.endsWith('노래 꺼') || string.endsWith('음악 꺼')) {
audio.pause();
$iconMusic.removeClass('visible');
} else if (string.endsWith('볼륨 업') || string.endsWith('볼륨업')) {
audio.volume += 0.2;
} else if (string.endsWith('볼륨 다운') || string.endsWith('볼륨다운')) {
audio.volume -= 0.2;
} else if (string.endsWith('스피치') || string.endsWith('말해줘') || string.endsWith('말 해 줘')) {
textToSpeech($('#final_span').text() || '전 음성 인식된 글자를 읽습니다.');
}
}
recognition.onerror = function(event) {
console.log('onerror', event);
if (event.error == 'no-speech') {
ignoreOnend = true;
} else if (event.error == 'audio-capture') {
ignoreOnend = true;
} else if (event.error == 'not-allowed') {
ignoreOnend = true;
}
$btnMic.attr('class', 'off');
};
var two_line = /\n\n/g;
var one_line = /\n/g;
var first_char = /\S/;
function linebreak(s) {
return s.replace(two_line, '<p></p>').replace(one_line, '<br>');
}
function capitalize(s) {
return s.replace(first_char, function(m) {
return m.toUpperCase();
});
}
function start(event) {
if (isRecognizing) {
recognition.stop();
return;
}
recognition.lang = 'ko-KR';
recognition.start();
ignoreOnend = false;
finalTranscript = '';
final_span.innerHTML = '';
interim_span.innerHTML = '';
}
/**
* textToSpeech
* 지원: 크롬, 사파리, 오페라, 엣지
*/
function textToSpeech(text) {
console.log('textToSpeech', arguments);
/*
var u = new SpeechSynthesisUtterance();
u.text = 'Hello world';
u.lang = 'en-US';
u.rate = 1.2;
u.onend = function(event) {
log('Finished in ' + event.elapsedTime + ' seconds.');
};
speechSynthesis.speak(u);
*/
// simple version
speechSynthesis.speak(new SpeechSynthesisUtterance(text));
}
/**
* requestServer
* key - AIzaSyDiMqfg8frtoZflA_2LPqfGdpjmgTMgWhg
*/
function requestServer() {
$.ajax({
method: 'post',
url: 'https://www.google.com/speech-api/v2/recognize?output=json&lang=en-us&key=AIzaSyDiMqfg8frtoZflA_2LPqfGdpjmgTMgWhg',
data: '/examples/speech-recognition/hello.wav',
contentType: 'audio/l16; rate=16000;', // 'audio/x-flac; rate=44100;',
success: function(data) {
},
error: function(xhr) {
}
});
}
/**
* init
*/
$btnMic.click(start);
$('#btn-tts').click(function() {
textToSpeech($('#final_span').text() || '전 음성 인식된 글자를 읽습니다.');
});
});