-
Notifications
You must be signed in to change notification settings - Fork 0
/
karaoke-js.js
436 lines (400 loc) · 12.9 KB
/
karaoke-js.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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
class Karaoke {
constructor(data, elementSelector) {
this.scale = 1;
this.threshold = 30;
this.last = 0;
this.elem = null;
this.subtitle = null;
this.height = 0;
this.duration = 0;
/**
* Check if line is timestamp or not
* @param {String} text
* @returns {Bool}
*/
this.isTimestamp = function (text) {
return text.indexOf('-->') > -1;
};
/**
* Parse timestamp
* @param {String} text
* @returns
*/
this.parseTimestamp = function (text) {
let arr = text.split('-->');
return {
start: arr[0].trim(),
end: arr[1].trim()
};
};
this.parseTimestampMills = function (text) {
let ts = this.parseTimestamp(text);
let start = this.convertTimestampToMills(ts.start);
let end = this.convertTimestampToMills(ts.end);
return {
start: start,
end: end
};
};
this.convertTimestampToMills = function (text) {
let secStr = '';
let msStr = '0';
if (text.indexOf(',') > -1) {
let arr = text.split(',');
secStr = arr[0];
msStr = arr[1];
} else if (text.indexOf('.') > -1) {
let arr = text.split('.');
secStr = arr[0];
msStr = arr[1];
} else {
secStr = text;
}
let ms1 = this.dmsToDdFromString(secStr) * 3600000;
let ms2 = parseFloat(msStr);
return ms1 + ms2;
};
this.truncate = function (val) {
return Math.floor(val);
};
/**
* Concatenantion text1 and text2 separated by carriage return
* @param {String} text1
* @param {String} text2
* @returns Combination of text1 and text2 separated by carriage return
*/
this.concat = function (text1, text2) {
let arr = [];
if (text1 != null) {
arr.push(text1);
}
if (text2.length > 0) {
arr.push(text2);
}
return arr.join('\r\n');
};
/**
* Convert float to hour, minute and second with padding
* @param {float} d
* @param {float} fixed
* @param {float} padding
* @returns {String}
*/
this.dToDmsReal = function (d, fixed, padding) {
if (typeof fixed == 'undefined' || fixed == null) {
fixed = 3;
}
let absDd = Math.abs(d);
let deg = absDd | 0;
let frac = absDd - deg;
let min = (frac * 60) | 0;
let sec = frac * 3600 - min * 60;
sec = sec.toFixed(fixed);
if (padding) {
if (deg < 10) {
deg = '0' + deg;
}
if (min < 10) {
min = '0' + min;
}
if (sec < 10) {
sec = '0' + sec;
}
}
return [deg, min, sec];
};
/**
* Convert hour to hour, minte and second with zero padding and colon separator
* @param {Number} dd
* @returns {String}
*/
this.ddToDms = function (dd) {
let absDd = Math.abs(dd);
let deg = absDd | 0;
let frac = absDd - deg;
let min = (frac * 60) | 0;
let sec = frac * 3600 - min * 60;
sec = Math.round(sec * 1000) / 1000;
return {
d: deg,
m: min,
s: sec
};
};
/**
* Convert hour, minte and second to hour
* @param {Integer} d
* @param {Integer} m
* @param {float} s
* @returns {String}
*/
this.dmsToDd = function (d, m, s) {
let degrees = typeof (d) !== "undefined" ? parseFloat(d) : 0;
let minutes = typeof (m) !== "undefined" ? parseFloat(m) / 60 : 0;
let seconds = typeof (s) !== "undefined" ? parseFloat(s) / 3600 : 0;
return degrees + minutes + seconds;
};
/**
* Split hour, minute and second into array
* @param {String} dmsStr
* @returns {Array}
*/
this.dmsToDdFromString = function (dmsStr) {
let dms = dmsStr.split(':');
return this.dmsToDd(dms[0], dms[1], dms[2]);
};
/**
* Convert milisecond to timestamp string
* @param {float} ms
* @returns {String}
*/
this.ddToDmsSrtFormat = function (ms) {
let dms = this.ddToDms(ms);
let d = dms.d;
let m = dms.m;
let s = dms.s.toFixed(3);
if (d < 10) {
d = '0' + d;
}
if (m < 10) {
m = '0' + m;
}
if (s < 10) {
s = '0' + s;
}
return d + ':' + m + ':' + s.replace('.', ',');
};
/**
* Parse string to floating point
* @param {String} value
* @returns {float}
*/
this.parseFloat = function (value) {
if (value != null && typeof value == 'string') {
if (value.length > 0) {
value = value.replace(/[^.\d]/g, '');
if (value == '') {
return 0;
}
return Number(value);
}
} else {
return value;
}
return 0;
};
/**
* Convert millisecond to unit
* @param {float} value
* @returns {float} unit
*/
this.msToUnit = function (value) {
return value * this.scale * this.zoom;
};
/**
* Convert unit to millisecond
* @param {float} value
* @returns {float}
*/
this.unitToMs = function (value) {
return value / (this.scale * this.zoom);
};
/**
* Create data
* @param {Object} tsData
* @param {String} text
* @returns {Object}
*/
this.createData = function (tsData, text) {
if (tsData) {
let timestamp = this.parseTimestampMills(tsData);
let start = timestamp.start;
let end = timestamp.end;
let duration = end - start;
return {
start: start,
end: end,
duration: duration,
text: text
};
}
return {
start: 0,
end: 0,
duration: 0,
text: text
};
};
/**
* Get minimum duration
* @param {Object} data
* @returns {float}
*/
this.getMinimumDuration = function (data) {
let min = -1;
for (let i in data.data) {
if (min > data.data[i].duration || min == -1) {
min = data.data[i].duration;
}
}
return min;
};
/**
* Parse raw data
* @param {String} rawData
* @returns {Object}
*/
this.parseRawData = function (rawData) {
rawData = rawData.trim().replace(/\r?\n/g, "\r\n");
let tempData = rawData.split(/\n/g);
let j = -1;
let data = [];
let nonempty = 0;
for (let i = 0; i < tempData.length; i++) {
tempData[i] = tempData[i].trim();
if (this.isTimestamp(tempData[i])) {
j++;
data[j] = this.createData(tempData[i]);
}
else {
if (j > -1) {
data[j].text = this.concat(data[j].text, tempData[i]);
}
nonempty++;
}
}
if (typeof data[j].text == 'undefined') {
data[j].text = '';
}
return { data: data, nonempty: nonempty, totalLength: tempData.length };
};
this.renderPrompt = function (data, selector) {
this.subtitle = data;
let elem = document.querySelector(selector);
this.elem = elem;
let minDur = this.getMinimumDuration(data);
let minHeight = 32; // minimum height
this.scale = minHeight / minDur;
for (let i in data.data) {
let dur = data.data[i].duration;
let start = data.data[i].start;
let height = this.scale * dur;
let top = this.scale * start;
let textContainer = document.createElement('div');
textContainer.style.height = Math.round(height) + 'px';
textContainer.style.top = Math.round(top) + 'px';
textContainer.innerText = data.data[i].text;
textContainer.setAttribute('data-index', i);
elem.appendChild(textContainer);
}
elem.style.height = (this.scale * this.duration) + 'px';
};
this.getIndex = function(ellapsed)
{
for (let i in this.subtitle.data) {
if(this.subtitle.data[i].start <= ellapsed && this.subtitle.data[i].end >= ellapsed)
{
return i;
}
}
return -1;
}
this.animate = function () {
let now = (new Date()).getTime();
if (now - this.last >= this.threshold) {
this.height = this.elem.parentNode.offsetHeight;
let ellapsed = now - this.getStart();
let offset = this.height / 4;
let top = offset - (ellapsed * this.scale);
let selected = this.getIndex(ellapsed);
this.markSelected(selected);
this.elem.style.top = top + 'px';
this.last = now;
}
};
this.updatePosition = function(ellapsed)
{
this.height = this.elem.parentNode.offsetHeight;
let offset = this.height / 4;
let top = offset - (ellapsed * this.scale);
let selected = this.getIndex(ellapsed);
this.markSelected(selected);
this.elem.style.top = top + 'px';
}
this.readStored = false;
this.offsetStored = 0;
this.getStart = function()
{
if(this.readStored)
{
return this.offsetStored;
}
else
{
let stored = localStorage.getItem('offset_'+this.songId);
if(typeof stored == 'undefined' || stored == null)
{
return this.start;
}
else
{
stored = stored.replace(/[^0-9.]/g, '');
if(stored == '')
{
stored = '0';
}
let parsed = parseInt(stored);
if(parsed == 0)
{
return this.start;
}
this.offsetStored = parsed;
this.readStored = true;
return parsed;
}
}
}
this.markSelected = function(selected)
{
if(this.lastSelected != selected)
{
if(selected == -1)
{
// clean up
this.cleanup();
}
else
{
// mark up
this.markup(selected);
}
}
this.lastSelected = selected;
}
this.cleanup = function()
{
let numb = this.elem.childNodes.length;
for(let i = 0; i<numb; i++)
{
this.elem.childNodes[i].classList.remove('marked');
}
}
this.markup = function(selected)
{
this.cleanup();
this.elem.childNodes[selected].classList.add('marked');
}
this.lastSelected = -1;
this.songId = '';
this.init = function (data, elementSelector) {
this.start = data.start;
this.songId = data.song_id;
this.duration = data.duration;
let parsed = this.parseRawData(data.lyric);
this.subtitle = parsed;
this.renderPrompt(parsed, elementSelector);
};
let _this = this;
this.init(data, elementSelector);
}
}