forked from Yokohama-Miyazawa/CheapVTuber
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
360 lines (317 loc) · 10.9 KB
/
script.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
let ctx = null
let audioSrc = null
let analyser = null
let sampleInterval = null
let source = null
let stream = null
let audioTrack = null
let prevSpec = 0
let threshold;
const body = document.getElementById("body");
const settingSwitch = document.getElementById("setting-switch");
const controlPanel = document.getElementById("control-panel");
const backgroundUploader = document.getElementById('background');
const characterUploaders = document.querySelectorAll('.character-image-input');
const setCharacter = document.getElementById("set-character");
const faceElement = document.getElementById('face');
const mouthElement = document.getElementById('mouth');
const muteButton = document.getElementById("mute");
const resetButton = document.getElementById("reset");
const range = document.getElementById("threshold");
const currentThreshold = document.getElementById("current-threshold");
const positionResetButton = document.getElementById("position-reset");
const characterSize = document.getElementById("size");
const currentCharacterSize = document.getElementById("current-size");
const crosswise = document.getElementById("crosswise");
const currentCrosswise = document.getElementById("current-crosswise");
const vertical = document.getElementById("vertical");
const currentVertical = document.getElementById("current-vertical");
const currentMicInput = document.getElementById("currentMicInput");
const imgPath = './image/';
let mouthClose = imgPath + 'mouth_close.png';
let mouthOpenLight = imgPath + 'mouth_open_light.png';
let mouthOpen = imgPath + 'mouth_open.png';
const dbName = 'settingDB';
const dbVersion = '1';
const layoutStore = 'layout';
const characterStore = 'character';
const backgroundStore = 'background';
let db;
threshold = range.value;
currentThreshold.value = threshold;
if('serviceWorker' in navigator && window.location.hostname != "localhost") {
navigator.serviceWorker.register('/CheapVTuber/sw.js');
};
openDB = () => {
let req = indexedDB.open(dbName, dbVersion);
req.onsuccess = (evt) => {
console.log("IndexedDB opened.");
db = evt.currentTarget.result;
let bgTrans = db.transaction(backgroundStore, 'readonly');
let bgStore = bgTrans.objectStore(backgroundStore);
let bgGetReq = bgStore.get(0);
bgGetReq.onerror = (evt) => {
console.error("bgGetReq:", evt.target.errorCode);
}
bgGetReq.onsuccess = (evt) => {
if (typeof evt.target.result != 'undefined') {
let image = evt.target.result.img;
let blobUrl = window.URL.createObjectURL(image);
body.style.backgroundImage = `url("${blobUrl}")`;
console.log("background image changed");
body.onload = () => {
window.URL.revokeObjectURL(blobUrl);
console.log("blobURL revoked.");
}
}
}
let charaTrans = db.transaction(characterStore, 'readonly');
let charaStore = charaTrans.objectStore(characterStore);
let charaGetReq = charaStore.get(0);
charaGetReq.onerror = (evt) => {
console.error("charaGetReq:", evt.target.errorCode);
}
charaGetReq.onsuccess = (evt) => {
if (typeof evt.target.result != 'undefined') {
faceElement.src = window.URL.createObjectURL(evt.target.result.face);
mouthClose = window.URL.createObjectURL(evt.target.result.mouthClose);
mouthOpenLight = window.URL.createObjectURL(evt.target.result.mouthOpenLight);
mouthOpen = window.URL.createObjectURL(evt.target.result.mouthOpen);
mouthElement.src = mouthClose;
}
}
let layTrans = db.transaction(layoutStore, 'readonly');
let layStore = layTrans.objectStore(layoutStore);
let layGetReq;
Array("size", "top", "left").forEach((k) => {
layGetReq = layStore.get(k);
layGetReq.onerror = (evt) => {
console.error("layGetReq:", evt.target.errorCode);
}
layGetReq.onsuccess = (evt) => {
if (typeof evt.target.result != 'undefined') {
let value = evt.target.result.value;
switch(k) {
case 'size':
faceElement.style.width = mouthElement.style.width = `${value}px`;
characterSize.value = currentCharacterSize.value = value;
break;
case 'top':
faceElement.style.top = mouthElement.style.top = `${value}px`;
vertical.value = currentVertical.value = value;
break;
case 'left':
faceElement.style.left = mouthElement.style.left = `${value}px`;
crosswise.value = currentCrosswise.value = value;
break;
}
}
}
});
};
req.onerror = (evt) => {
console.error("openDB:", evt.target.errorCode);
};
req.onupgradeneeded = (evt) => {
let backgroundObjectStore = evt.currentTarget.result.createObjectStore(backgroundStore, {keyPath : 'id'})
backgroundObjectStore.createIndex("id", "id", { unique: true });
backgroundObjectStore.createIndex("img", "img", { unique: false });
let characterObjectStore = evt.currentTarget.result.createObjectStore(characterStore, {keyPath: 'id'})
characterObjectStore.createIndex("id", "id", { unique: true });
characterObjectStore.createIndex("face", "face", { unique: false });
characterObjectStore.createIndex("mouthClose", "mouthClose", { unique: false });
characterObjectStore.createIndex("mouthOpenLight", "mouthOpenLight", { unique: false });
characterObjectStore.createIndex("mouthOpen", "mouthOpen", { unique: false });
let layoutObjectStore = evt.currentTarget.result.createObjectStore(layoutStore, {keyPath: 'kind'})
layoutObjectStore.createIndex("kind", "kind", { unique: true });
layoutObjectStore.createIndex("value", "value", { unique: false });
console.log("IndexedDB upgraded.");
};
}
webAudioSetup = async () => {
// Web Audio APIの初期化
ctx = new AudioContext();
analyser = ctx.createAnalyser();
analyser.fftSize = 2048;
//analyser.connect(ctx.destination);
stream = await navigator.mediaDevices.getUserMedia({audio: true});
audioTrack = stream.getAudioTracks()[0];
source = ctx.createMediaStreamSource(stream);
source.connect(analyser);
sampleInterval = setInterval(() => {
let spectrums = new Uint8Array(analyser.fftSize)
analyser.getByteFrequencyData(spectrums)
syncLip(spectrums)
}, 100)
}
syncLip = (spectrums) => {
let imgSrc;
// 人間の声の周波数帯のみ集める
let totalSpectrum = spectrums.slice(4, 51).reduce(function(a, x) { return a + x })
currentMicInput.innerText = `${totalSpectrum}`;
if ((totalSpectrum >= threshold) && (totalSpectrum > prevSpec)) {
let array = [mouthOpen, mouthOpenLight];
imgSrc = array[Math.floor(Math.random() * array.length)];
} else {
imgSrc = mouthClose;
}
mouthElement.src = imgSrc;
prevSpec = totalSpectrum;
}
settingSwitch.onclick = () => {
if(controlPanel.style.display == "block") {
controlPanel.style.display = "none";
} else {
controlPanel.style.display = "block";
}
}
muteButton.onclick = () => {
if (muteButton.innerText == "UNMUTE") { // become unmute
if(!ctx) {
webAudioSetup()
} else {
audioTrack.enabled = true;
}
muteButton.innerText = "MUTE";
} else {
audioTrack.enabled = false;
mouthElement.src = mouthClose;
muteButton.innerText = "UNMUTE";
}
}
resetButton.onclick = () => {
threshold = 700;
range.value = threshold;
currentThreshold.value = threshold;
}
range.oninput = (e) => {
threshold = range.value;
currentThreshold.value = threshold;
}
currentThreshold.onchange = (e) => {
range.value = currentThreshold.value;
range.oninput();
}
setLayoutDB = (kind, value) => {
let obj = {'kind': kind, 'value': value};
let trans = db.transaction(layoutStore, 'readwrite');
let store = trans.objectStore(layoutStore);
let req;
try {
req = store.put(obj);
} catch(e) {
throw e;
}
}
positionResetButton.onclick = () => {
let size = 400;
let left = 0;
let top = 0;
characterSize.value = size;
crosswise.value = left;
vertical.value = top;
characterSize.oninput();
crosswise.oninput();
vertical.oninput();
}
characterSize.oninput = (e) => {
let size = characterSize.value;
currentCharacterSize.value = size;
faceElement.style.width = mouthElement.style.width = `${size}px`;
setLayoutDB('size', size);
}
currentCharacterSize.onchange = (e) => {
characterSize.value = currentCharacterSize.value;
characterSize.oninput();
}
crosswise.oninput = (e) => {
let left = crosswise.value;
currentCrosswise.value = left;
faceElement.style.left = mouthElement.style.left = `${left}px`;
setLayoutDB('left', left);
}
currentCrosswise.onchange = (e) => {
crosswise.value = currentCrosswise.value;
crosswise.oninput();
}
vertical.oninput = (e) => {
let top = vertical.value;
currentVertical.value = top;
faceElement.style.top = mouthElement.style.top = `${top}px`;
setLayoutDB('top', top);
}
currentVertical.onchange = (e) => {
vertical.value = currentVertical.value;
vertical.oninput();
}
backgroundUploader.onchange = (e) => {
let file = e.target.files[0];
console.log(file);
let obj = {'id': 0, 'img': file};
let trans = db.transaction(backgroundStore, 'readwrite');
let store = trans.objectStore(backgroundStore);
let req;
try {
req = store.put(obj);
} catch(e) {
throw e;
}
req.onsuccess = (evt) => {
let blobUrl = window.URL.createObjectURL(file);
body.style.backgroundImage = `url("${blobUrl}")`;
console.log("image changed");
body.onload = () => {
window.URL.revokeObjectURL(blobUrl);
console.log("blobURL revoked.");
}
}
req.onerror = () => {
console.error("backgroundUploader:", this.error);
}
}
setCharacter.onclick = () => {
let uploaders = Array.from(characterUploaders);
if(uploaders.some((t) => {return (t.value === 'undefined' || t.value === '')})){
alert("There are unset images.");
return;
}
let face, close, halfOpen, open;
characterUploaders.forEach((t) => {
switch(t.id) {
case 'input-face':
face = t.files[0];
break;
case 'input-mouth_close':
close = t.files[0];
break;
case 'input-mouth_open_light':
halfOpen = t.files[0];
break;
case 'input-mouth_open':
open = t.files[0];
break;
}
});
let obj = {'id': 0, 'face': face, 'mouthClose': close, 'mouthOpenLight': halfOpen, 'mouthOpen': open};
let trans = db.transaction(characterStore, 'readwrite');
let store = trans.objectStore(characterStore);
let req;
try {
req = store.put(obj);
} catch(e) {
throw e;
}
req.onsuccess = (evt) => {
faceElement.src = window.URL.createObjectURL(face);
mouthClose = window.URL.createObjectURL(close);
mouthOpenLight = window.URL.createObjectURL(halfOpen);
mouthOpen = window.URL.createObjectURL(open);
mouthElement.src = mouthClose;
console.log("character image changed");
}
req.onerror = () => {
console.error("characterUploader:", this.error);
}
}
controlPanel.style.display = "none";
openDB()