-
Notifications
You must be signed in to change notification settings - Fork 0
/
lyricsjs.js
207 lines (161 loc) · 5.82 KB
/
lyricsjs.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
/*Synchronises lyrics with music so you can easy animate in real time.
Useful for webpage based videos, karaoke etc*/
(function(){
"use strict";
var wordsContainer,
sourceType,
vheight,
vwidth,
musicSource,
lyricsJSON;
var totalTime, now_time;
var LyricsJs = function (settings) {
//Init
wordsContainer = settings['wordsContainer'] || '',
sourceType = settings['sourceType'] || '',
vheight = settings['vheight'] || "100%",
vwidth = settings['vwidth'] || "100%",
musicSource = settings['musicSource'] || '',
lyricsJSON = settings['lyricsJSON'] || '';
/*if( wordsContainer!="" ){
containr = document.querySelector(wordsContainer);
}else{
return false;
//document.createElement('div').id = "lyrics";
//document.getElementsByTagName('body')[0].appendChild(flyDiv);
//containr = document.getElementById('#lyrics');
}//Get the container */
if (typeof(sourceType) !== "undefined") {
if(sourceType == 'youtube'){
processYOUTUBE();
}
if(sourceType == 'html5'){
processHTML5();
}
}
}
window.LyricsJs = LyricsJs;
function processHTML5(){
var song = (musicSource) ? document.querySelector(musicSource) : '';
song.addEventListener('timeupdate',function (){
var duration = song.duration,
currentTime = song.currentTime;
totalTime = timeInSecs(duration),
now_time = timeInSecs(currentTime);
showLyricsFunction();
});
}
function processYOUTUBE(){
var song = (musicSource) ? musicSource : "PWjRMuyXl2o";
// This code loads the IFrame Player API code asynchronously.
var tag = document.createElement("script");
tag.src = "//www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName("script")[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
// This function creates an <iframe> (and YouTube player)
// after the API code downloads.
var player,t;
window.onYouTubeIframeAPIReady = function() {
player = new YT.Player("player", {
"height": vheight,
"width": vwidth,
"videoId": song,
"events": {
"onReady": onPlayerReady,
"onStateChange": onPlayerStateChange
}
});
}
// 4. The API will call this function when the video player is ready.
function onPlayerReady(event) {
event.target.playVideo();
}
/*function onPlayerStateChange(event){
function updateTime() {
//if (event.data == YT.PlayerState.PLAYING) {
console.log(player.getCurrentTime());
//}
}
t = setInterval(updateTime,1000)
}*/
function onPlayerStateChange(event){
if(event.data==YT.PlayerState.PLAYING) { // playing
t = setInterval(function(){
var currentTime = player.getCurrentTime();
var minutes = Math.floor(currentTime / 60) < 10 ? '0' + Math.floor(currentTime / 60.000) :
Math.floor(currentTime / 60.000);
var seconds = Math.floor(currentTime % 60) < 10 ? '0' + Math.floor(currentTime % 60.000) :
Math.floor(currentTime % 60.000);
//console.log( minutes+'.'+seconds );
totalTime = '',
now_time = getSeconds(minutes+':'+seconds);
showLyricsFunction()
}, 100); // 100 means repeat in 100 ms
} else { // not playing
clearInterval(t);
}
}
}
function showLyricsFunction(){
//console.log( wordsContainer );
var first_time = '',
last_time = '',
next_time = '',
wordsContainr = document.querySelector(wordsContainer);
//Sequence json
if(lyricsJSON.length){
lyricsJSON.forEach(function(line,index) {
//console.log(line);
var content_time = (typeof line['time'] != 'undefined') ? hmsToMillisecs(line['time']) : "",
content = (typeof line['content'] != 'undefined') ? line['content'] : "",
animation = (typeof line['animation'] != 'undefined') ? line['animation'] : "noanimation";
first_time = hmsToMillisecs(lyricsJSON[0]['time']);
if (index < lyricsJSON.length - 1) {
//console.log("Next: " + lyricsJSON[index + 1]['time']);
next_time = hmsToMillisecs(lyricsJSON[index + 1]['time']);
}
if (index > 0) {
//console.log("Previous: " + lyricsJSON[index - 1]['time']);
last_time = hmsToMillisecs(lyricsJSON[index - 1]['time']);
}
//settings.before();//Execute a function before lyrics appear
console.log((now_time/2)+'-'+(content_time/2))
if( content && content_time ){
//Lyrication
if(((now_time/2) >= (content_time/2)) && now_time < next_time){
wordsContainr.classList.add(animation);
wordsContainr.innerHTML = '<span class="'+animation+'">'+content+'</span>';
//console.log(wordsContainer);
}else if(now_time < first_time || now_time > next_time){
wordsContainr.classList.remove(animation);
wordsContainr.innerHTML = '';
}
//Return elapsed time persentage
document.getElementById('time').style.width = Math.floor((now_time / totalTime)*100)+'%';
}
//settings.after();//Execute a function after lyrics appear
});
}
}
var timeInSecs = function(the_time){
var sec = new Number(),
min = new Number(),
str;
sec = Math.floor( the_time );
min = Math.floor( sec / 60 );
min = min >= 10 ? min : '0' + min;
sec = Math.floor( sec % 60 );
sec = sec >= 10 ? sec : '0' + sec;
return hmsToMillisecs(min+':'+sec);
}
function hmsToMillisecs(str) {
var p = str.split(':'),
s = 0, m = 1;
while (p.length > 0) {
s += m * parseInt(p.pop(), 10);
m *= 60;
}
//return (s % 1) * 1000;
return (s) * 1000;
}
})();