-
Notifications
You must be signed in to change notification settings - Fork 0
/
tracks.js
110 lines (87 loc) · 2.59 KB
/
tracks.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
var client_id = "3bac0c7e3cff7e9c3a0fd3bbbb57718c";
SC.initialize({
client_id: client_id
});
var loadStream = function (track) {
return function (callback) {
SC.stream("/tracks/" + track, function (stream) {
stream.track = track;
stream.load({
onload: function () { callback(null, stream); }
});
});
};
};
var container = $(".tracks");
container.css({ position: "relative" });
var tracks = [];
$("tr", container).each(function (i, el) {
var trackId = $(el).data("track");
if (!trackId) return;
$.get("http://api.soundcloud.com/tracks/" + trackId + ".json?client_id=" + client_id, function (data) {
$("td", el).html("<img src='" + data.waveform_url + "' id='wave" + trackId + "'>");
});
tracks.push(loadStream(trackId));
});
async.parallel(tracks, function (err, streams) {
var maxLength = 0;
for (var i = streams.length - 1; i >= 0; i--) {
var stream = streams[i];
if (stream.duration > maxLength) {
maxLength = stream.duration;
}
};
for (var i = streams.length - 1; i >= 0; i--) {
var stream = streams[i];
var widthPercentage = stream.duration / maxLength * 100;
$("#wave" + stream.track).width(widthPercentage + "%");
};
var updateCursor = function () {
var wave = $("td", container).first();
var containerWidth = wave.width();
var songDuration = maxLength;
var playedOffset = stream.position;
var pixelOffset = playedOffset * containerWidth / songDuration;
var cursor = $("#playbackCursor");
cursor.css({ left: wave.position().left + pixelOffset, height: container.height() });
if (playedOffset < songDuration && isPlaying) {
window.setTimeout(updateCursor, 100);
}
};
var createCursor = function () {
var cursor = $("<div id='playbackCursor'></div>");
cursor.css({
position: "absolute",
top: 0,
left: $("td", container).first().position().left,
height: container.height(),
"background-color": "#000",
width: 1
});
container.append(cursor);
};
createCursor();
var controls = $("#controls");
controls.text("Play");
var isPlaying = false;
controls.click(function (event) {
if (isPlaying) {
controls.text("Play");
isPlaying = false;
for (var i = streams.length - 1; i >= 0; i--) {
var stream = streams[i];
stream.pause();
};
}
else {
controls.text("Pause");
isPlaying = true;
for (var i = streams.length - 1; i >= 0; i--) {
var stream = streams[i];
stream.play();
};
}
updateCursor();
event.preventDefault();
});
});