forked from talkr-app/gif-talkr
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample.js
executable file
·239 lines (206 loc) · 8.36 KB
/
example.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// ----------------------
// Setup
// ----------------------
// Load the Gif
var sup1 = new SuperGif({ gif: document.getElementById('exampleimg') } );
sup1.load(function(){
});
var instructions = document.getElementById('instructions');
// ----------------------
// Text to speech GUI
// ----------------------
// Code for the voice select element
var voiceSelecter = document.getElementById('voiceSelecter');
function getVoices() {
if(voiceSelecter){
voiceSelecter.innerHTML = "";
var voices = speechSynthesis.getVoices();
// iOS returns voices it doesn't let you use.
var bIsiOS = /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;
var iOSVoiceSet = {};
if( bIsiOS ){
var array = ["Maged","Zuzana","Sara","Anna","Melina","Karen","Serena","Moira","Tessa","Samantha","Monica","Paulina","Satu","Amelie","Thomas","Carmit","Lekha","Mariska","Damayanti","Alice","Kyoko","Yuna","Ellen","Xander","Nora","Zosia","Luciana","Joana","Ioana","Milena","Laura","Alva","Kanya","Yelda","Ting-Ting","Sin-Ji","Mei-Jia"];
array.forEach(function(val){
iOSVoiceSet[val] = true;
});
}
voices.forEach(function(voice, i) {
// only some iOS voices are working, but they are all returned.
if( !bIsiOS || voice.name in iOSVoiceSet ){
var option = document.createElement('option');
option.value = voice.name;
option.innerHTML = voice.name;
if( voice.lang.substring(0,2) == "en" ){
voiceSelecter.insertBefore(option, voiceSelecter.firstChild);
} else {
voiceSelecter.appendChild(option);
}
}
});
}
}
getVoices();
// Update the voices when they change (chrome loads asynchronously)
window.speechSynthesis.onvoiceschanged = function(e) {
getVoices();
};
// ----------------------
// Load new GIFs
// ----------------------
var gifurlinput = document.getElementById('gifurlinput')
function loadNewGif(){
var gifURL = gifurlinput.value;
if (gifURL.toLowerCase().indexOf(".gif") == -1) {
document.getElementById('giferrormessage').innerHTML = "Specify a gif file.";
return;
}
function doesFileExist(urlToFile, success) {
var xhr = new XMLHttpRequest();
xhr.open('HEAD', urlToFile, true);
xhr.onload = function(e) {
if (xhr.status != "404") {
if (success) success(urlToFile);
} else document.getElementById('giferrormessage').innerHTML = "That file was not found.";
}
xhr.onerror = function() {
document.getElementById('giferrormessage').innerHTML = "Error loading gif. Make sure the resource exists and has Access-Control-Allow-Origin headers.";
};
xhr.send();
};
function onFileExists() {
var imagecontainer = document.getElementById('imagecontainer')
imagecontainer.innerHTML = "";
imgElement = document.createElement('img');
imgElement.src = gifURL;
imgElement.animatedSrc = gifURL;
imgElement.setAttribute('rel:animated_src',gifURL);
imgElement.setAttribute('rel:auto_play', 0);
imagecontainer.appendChild(imgElement);
instructions.innerHTML = "Please wait..."
if(sup1){
// free memory from previous SuperGif class. The looping animation would
// prevent the frames array from being garbage collected otherwise and memory
// use would grow.
sup1.destroy()
}
sup1 = new SuperGif({ gif: imgElement });
sup1.load(function(){
instructions.innerHTML = "Click on the image below to hear the message."
});
document.getElementById('giferrormessage').innerHTML = "";
}
doesFileExist(gifURL, onFileExists)
}
if(gifurlinput)gifurlinput.addEventListener('input', loadNewGif)
var imgurgifs = [
"https://i.imgur.com/lLHEQ3F.gif", // mona
"https://i.imgur.com/ork8hoP.gif", // lily
"https://i.imgur.com/dS2yZ19.gif", // dog2
"https://i.imgur.com/YpKsOQS.gif", // trump
"https://i.imgur.com/FkEVxI6.gif", // washington
"https://i.imgur.com/dvXw5bu.gif", // brunette
"https://i.imgur.com/RLMkj1P.gif", // kiera
"https://i.imgur.com/JwAmmkx.gif", // dog1
"https://i.imgur.com/vqVsazA.gif", // beiber
"https://i.imgur.com/YphP1gi.gif" // portman
]
var imgurgifindex = 0;
var newgifbutton = document.getElementById("newgifbutton");
if(newgifbutton){
newgifbutton.addEventListener("click", function(){
imgurgifindex += 1;
imgurgifindex = imgurgifindex % imgurgifs.length;
gifurlinput.value = imgurgifs[imgurgifindex];
loadNewGif();
})
}
// ---------------------
// Playing TTS in sync
// ---------------------
// play the specified text
function playsyncronized(){
if(!'speechSynthesis' in window){
instructions.innerHTML = "Speech synthesis is not supported in this browser. Sorry.";
document.getElementById('ttsoptions').style.visibility = "hidden";
}
else {
document.getElementById('ttsoptions').style.visibility = "visible";
if(speechSynthesis.speaking){
return;
}
var text = document.getElementById('texttospeakinput').value;
// get the selected voice
var voice = speechSynthesis.getVoices().filter(function(voice){
return voice.name == voiceSelecter.value;
})[0];
// Splitting each utterance up using punctuation is important. Intra-utterance
// punctuation will add silence to the tts which looks bad unless the mouth stops moving
// correctly. Better to split it into separate utterances so play_for_duration will move when
// talking, and be on frame 0 when not.
// split everything betwen deliminators [.?,!], but include the deliminator.
var substrings = text.match(/[^.?,!]+[.?,!]?/g);
for (var i = 0, l = substrings.length; i < l; ++i) {
var str = substrings[i].trim();
// Make sure there is something to say other than the deliminator
var numpunc = (str.match(/[.?,!]/g) || []).length;
if (str.length - numpunc > 0) {
// suprisingly decent approximation for multiple languages.
// if you change the rate, you would have to adjust
var speakingDurationEstimate = str.length * 50;
// Chinese needs a different calculation. Haven't tried other Asian languages.
if (str.match(/[\u3400-\u9FBF]/)) {
speakingDurationEstimate = str.length * 200;
}
var msg = new SpeechSynthesisUtterance();
(function(dur){
msg.addEventListener('start', function(){
sup1.play_for_duration(dur);
})
})(speakingDurationEstimate);
// The end event is too inacurate to use for animation,
// but perhaps it could be used elsewhere. You might need to push
// the msg to an array or aggressive garbage collection fill prevent the callback
// from firing.
//msg.addEventListener('end', function (){console.log("too late")}
msg.text = str;
//change voice here
msg.voice = voice;
window.speechSynthesis.speak(msg);
}
}
}
}
document.addEventListener("keypress", function(e) {
if (e.which == 13) {
playsyncronized();
}
});
document.addEventListener("keypress", function(e) {
if (e.which == 13) {
playsyncronized();
}
});
document.getElementById('submitQuestion').addEventListener('click', function() {
var question = document.getElementById('questionInput').value;
var openAIKey = document.getElementById('openAIKeyInput').value;
console.log('API key:', openAIKey);
var myHeaders = new Headers();
myHeaders.append('Content-Type', 'application/json');
myHeaders.append('Authorization', 'Bearer ' + openAIKey);
fetch('https://api.openai.com/v1/engines/davinci-codex/completions', {
method: 'POST',
headers: myHeaders,
body: JSON.stringify({
'prompt': question,
'max_tokens': 60
})
})
.then(response => response.json())
.then(data => {
document.getElementById('texttospeakinput').value = data.choices[0].text;
playsyncronized();
})
.catch((error) => {
console.error('Error:', error);
});
});