-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
345 lines (258 loc) · 9.08 KB
/
index.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
/*Initialize Tumblr*/
var initTumblr = function(tag){
$("#illustration").css("background-image", "url(img/loading.gif)");
$("#illustration").click(function(){});
var num_posts = 50;
//We get the list of post with the specified tag
var tumblr_url = "http://api.tumblr.com/v2/blog/soundcloudvideoclip.tumblr.com/posts/photo?api_key=QXmLiokbFl8PFldxAlznGCKPIQ44oTf18NW2UqZzsjAIDWjp5h&tag="+tag;
$.ajax({url: tumblr_url+"&limit="+num_posts+"&jsonp=?", dataType: "jsonp", success: function(data){
window.tumblr_api_read = data.response || null;
var total_posts = tumblr_api_read["total_posts"];
console.log(tag+":"+total_posts);
getTumblrImagesList(window.tumblr_api_read.posts.length, total_posts, num_posts ,tumblr_url);
}});
}
var getTumblrImagesList = function(counter, total,num_posts , tumblr_url){
if(counter < total){
console.log("getting more images from Tumblr");
$.ajax({url: tumblr_url+"&limit="+num_posts+"&offset="+counter+"&jsonp=?", dataType: "jsonp", success: function(data){
window.tumblr_api_read.posts = window.tumblr_api_read.posts.concat(data.response.posts);
getTumblrImagesList(counter+num_posts, total, num_posts, tumblr_url);
}
})
} else {
console.log("displaying Tumblr images");
displayTumblrImages();
}
}
var displayTumblrImages = function(){
//We add those photos to an array
if (window.tumblr_api_read != null) {
for (var i = 0; i < tumblr_api_read.posts.length; i++) {
//We get the url of the images with a width of 500px
window.tumblrImages[i] = {"url": "#", "img": "#"};
window.tumblrImages[i]["img"]=tumblr_api_read.posts[i]["photos"][0]["alt_sizes"][1]["url"];
window.tumblrImages[i]["url"]=tumblr_api_read.posts[i]["post_url"];
//We preload the images
var image = $('<img />').attr('src', window.tumblrImages[i]["img"]);
}
}
if(tumblrImages.length!=0){
//Kinda random ordering;
tumblrImages.sort(function() {return 0.5 - Math.random();});
} else {
window.tumblrImages[0] = {"url": "#", "img": "#"};
tumblrImages[0]["img"]="http://lorempixel.com/500/500/abstract/";
tumblrImages[0]["url"]="http://lorempixel.com/";
}
//We set the illustration to be the first of the images.
$("#illustration").css("background-image", "url("+window.tumblrImages[0]["img"]+")");
$("#illustration").click(function(){window.open(window.tumblrImages[0]["url"],'mywindow')});
}
/*Initialize SoundCloud*/
var initSoundCloud = function(trackId){
//We get the track and initialize the waveform (see http://waveformjs.org)
SC.get("/tracks/"+trackId, function(track){
var waveform = new Waveform({
container: document.getElementById("waveform")
});
waveform.dataFromSoundCloudTrack(track);
/* We set the proper options for the stream*/
var streamOptions = waveform.optionsForSyncedStream();
streamOptions["ontimedcomments"] = function(comments){
/* Each time there is a comment, it will show the next image */
if(Math.abs(comments[0].timestamp-stream.position) < 3000){
$("#comment").text(comments[0].body);
}
window.ImgCount=(window.ImgCount+1)%window.tumblrImages.length;
$("#illustration").css("background-image", "url("+window.tumblrImages[ImgCount]["img"]+")");
$("#illustration").click(function(){window.open(window.tumblrImages[ImgCount]["url"],'mywindow')});
};
streamOptions["onfinish"] = function(){
$("#playButton").attr("src","img/Play.png");
window.paused = !window.paused;
}
//We stream the song
SC.stream(track.uri, streamOptions, function(stream){
window.stream = stream;
//Hack to display/preload the song*/
stream.play();
stream.pause();
//Display time indication
this.iid = setInterval(function() {
secP = '0'+parseInt(stream.position / 1000)%60;
secP = secP.slice(-2);
minP = parseInt(stream.position / 1000 / 60)%60;
secD = '0'+parseInt(stream.duration / 1000)%60;
secD = secD.slice(-2);
minD = parseInt(stream.duration / 1000 / 60)%60;
$("#time").text(minP+':'+secP+' / '+minD+':'+secD);
}, 500);
});
});
}
var initRaphael = function(){
// Creates canvas 500x75 in div #cursor
var paper = Raphael("cursor", 450, 75);
var set = paper.set();
//We redraw the line to follow the mouse position.
$('#cursor').mousemove(function(e){
var offset = $('#cursor').offset().left;
var pos = (e.clientX-offset);
set.remove()
set.push(
paper.path("M"+pos+" 0V75").attr("stroke","#fff")
);
});
//Set play position in the song according to the cursor position
$('#cursor').click(function(e){
var duration = window.stream.duration;
var cursor_position = e.clientX-$('#cursor').offset().left;
var requested_song_position = duration * (cursor_position)/450;
if(requested_song_position < window.stream.position){
window.ImgCount=0;
$("#comment").text("");
window.stream.setPosition(0);
}
window.stream.setPosition(requested_song_position);
});
$('#cursor').mouseleave(function(e){
set.remove();
});
}
var selectSong = function(url, tag){
//Reset a bunch of stuff
window.ImgCount = 0;
window.tumblrImages = [];
window.paused = true;
$("#playImage").attr('src','img/play.png');
$("#newSongForm").hide();
$("#content").show();
$('#waveform').empty();
$('#blu').unbind('mouseenter').unbind('mouseleave');
$('#comment').empty();
this.iid && clearInterval(this.iid);
$("#comments_div").show();
if(window.stream != null){
window.stream.destruct();
}
//We get the new song id, initialize the stream, the waveform and get the images from Tumblr
SC.get('/resolve', { url: url }, function(track){
initSoundCloud(track.id);
})
$("#playButton").attr("src","img/Play.png");
initTumblr(tag);
}
/*Utility functions*/
var togglePause = function(){
window.stream.togglePause();
if(!window.paused){
$("#playButton").attr("src","img/Play.png");
} else {
$("#playButton").attr("src","img/Pause.png");
}
window.paused = !window.paused;
}
var toggleComments = function(){
if(window.commentsShown == true){
$("#comment").hide();
$("#commentButton").attr("src","img/Comment.png");
}else{
$("#comment").show();
$("#commentButton").attr("src","img/Nocomment.png");
}
window.commentsShown = !window.commentsShown;
}
var showForm = function(){
$("#content").hide();
$("#comments_div").hide();
$("#newSongForm").show();
}
var hideForm = function(){
$("#newSongForm").hide();
$("#comments_div").show();
$("#content").show();
}
//Generates the menu based on the list of songs
var generateNav = function(songs){
$('nav').append($("<ul></ul>"));
for(var i = 0; i < songs.length; i++){
var item = $("<li><a href='javascript:selectSong(songs["+i+"].url,songs["+i+"].tag);'>"+songs[i].title+"</a></li>");
$('nav ul').append(item);
}
var item = $("<li> </li>");
$('nav ul').append(item);
var item = $("<li><a href='javascript:showForm()'>New Song</a></li>");
$('nav ul').append(item);
}
/*Main*/
var songs = [ {
"title": "Color Theory",
"artist": "Tea Leigh",
"url": "https://soundcloud.com/tea-leigh/color-theory-1",
"tag": "Color Theory"
},
{
"title": "Clouds",
"artist": "Marekhemmann",
"url": "https://soundcloud.com/marekhemmann/clouds",
"tag": "Clouds"
} ,
{
"title": "Yonkers",
"artist": "Tyler The Creator",
"url": "https://soundcloud.com/diamondmedia360/tyler-the-creator-yonkers-prod",
"tag": "Yonkers"
} ,
{
"title": "L.O.V.E",
"artist": "Onra",
"url": "https://soundcloud.com/onra/l-o-v-e",
"tag": "Love"
} ,
{
"title": "Icarus",
"artist": "Madeon",
"url": "https://soundcloud.com/madeon/madeon-icarus",
"tag": "Icarus"
} ,
{
"title": "Hours",
"artist": "Tycho",
"url": "https://soundcloud.com/tycho/tycho-hours",
"tag": "Hours"
} ,
{
"title": "New Theory",
"artist": "RAC",
"url": "https://soundcloud.com/rac/washed-out-new-theory-rac-mix",
"tag": "New Theory"
} ,
{
"title": "Too Insistent",
"artist": "Inertiamusic",
"url": "https://soundcloud.com/inertiamusic/the-do-too-insistent",
"tag": "Too Insistent"
} ,
{
"title": "If U Got It",
"artist": "Chris Malinchak",
"url": "https://soundcloud.com/chrismalinchak/if-u-got-it",
"tag": "If U Got It"
} ,
{
"title": "Pharao Black Magic",
"artist": "Future Classic",
"url": "https://soundcloud.com/futureclassic/pharao-black-magic-hermes",
"tag": "Pharao Black Magic"
}
];
$(document).ready(function() {
//Authenticate with SoundCloud
SC.initialize({
client_id: "16be599a525a2df3fc4b5a20da9927c4"
});
generateNav(songs);
selectSong(songs[0].url,songs[0].tag);
initRaphael();
});