-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathlyrics.js
153 lines (145 loc) · 5.21 KB
/
lyrics.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
(function(root, factory) {
'use strict';
if (typeof define === 'function' && define.amd) {
// AMD support.
define([], factory);
} else if (typeof exports === 'object') {
// NodeJS support.
module.exports = factory();
} else {
// Browser global support.
root.Lyrics = factory();
}
}(this, function() {
'use strict';
var Lyrics = function (text_lrc) {
/* Private */
this.timestamp_offset = 0;
this.lyrics_all = undefined;
this.meta_info = undefined;
this.ID_TAGS = [
{name: 'artist', id: 'ar'},
{name: 'album', id: 'al'},
{name: 'title', id: 'ti'},
{name: 'author', id: 'au'},
{name: 'length', id: 'length'},
{name: 'by', id: 'by'},
{name: 'offset', id: 'offset', handler: this.setTimestampOffset},
{name: 'createdBy', id: 're'},
{name: 'createdByVersion', id: 've'}
];
/* Initialization */
for (var i = 0; i < this.ID_TAGS.length; i++) {
this.ID_TAGS[i].re = new RegExp('\\[' + this.ID_TAGS[i].id + ':(.*)\\]$', 'g');
}
if (text_lrc) {
this.load(text_lrc);
}
};
Lyrics.prototype = {
constructor: Lyrics,
load: function (text_lrc) {
this.lyrics_all = new Array();
this.meta_info = new Object();
this.timestamp_offset = 0;
var lines_all = String(text_lrc).split('\n');
for (var i = 0; i < lines_all.length; i++) {
var line = lines_all[i].replace(/(^\s*)|(\s*$)/g, '');
if (!line) {
continue;
}
//Parse ID Tags
var is_id_tag = false;
for (var j = 0; j < this.ID_TAGS.length; j++) {
var match = this.ID_TAGS[j].re.exec(line);
if (!match || match.length < 2) {
continue;
}
is_id_tag = true;
var value = match[1].replace(/(^\s*)|(\s*$)/g, '');
if (typeof this.ID_TAGS[j].handler == 'function') {
this.meta_info[String(this.ID_TAGS[j].name)] = this.ID_TAGS[j].handler.call(this, value);
} else {
this.meta_info[String(this.ID_TAGS[j].name)] = String(value);
}
}
if (is_id_tag) {
continue;
}
//Parse lyric
var timestamp_all = Array();
while (true) {
var match = /^(\[\d+:\d+(.\d+)?\])(.*)/g.exec(line);
if (match) {
timestamp_all.push(match[1]);
line = match[match.length - 1].replace(/(^\s*)|(\s*$)/g, '');
} else {
break;
}
}
for (var j = 0; j < timestamp_all.length; j++) {
var ts_match = /^\[(\d{1,2}):(\d|[0-5]\d)(\.(\d+))?\]$/g.exec(timestamp_all[j]);
if (ts_match) {
this.lyrics_all.push({
timestamp: Number(ts_match[1]) * 60 + Number(ts_match[2]) + (ts_match[4] ? Number('0.' + ts_match[4]) : 0),
text: line
});
}
}
}
this.lyrics_all.sort(function (a, b) {
return (a.timestamp > b.timestamp ? 1 : -1);
});
if (!this.lyrics_all.length) {
this.lyrics_all = undefined;
}
if (this.isEmpty(this.meta_info)) {
this.meta_info = undefined;
}
return (this.lyrics_all !== undefined || this.meta_info !== undefined) ? true : false;
},
getLyrics: function () {
return this.lyrics_all;
},
getLyric: function (idx) {
try {
return this.lyrics_all[idx];
} catch (e) {
return undefined;
}
},
getIDTags: function () {
return this.meta_info;
},
select: function (ts) {
if (isNaN(ts)) {
return -1;
}
var timestamp = Number(ts) + this.timestamp_offset;
var i = 0;
if (timestamp < this.lyrics_all[0].timestamp) {
return -1;
}
for (i = 0; i < (this.lyrics_all.length - 1); i++) {
if (this.lyrics_all[i].timestamp <= timestamp
&& this.lyrics_all[i + 1].timestamp > timestamp) {
break;
}
}
return i;
},
setTimestampOffset: function (offset) {
this.timestamp_offset = isNaN(offset) ? 0 : Number(offset) / 1000;
return Number(offset);
},
isEmpty: function (obj) {
for (var prop in obj) {
if (obj.hasOwnProperty(prop)) {
return false;
}
}
return true;
}
};
return Lyrics;
}));