-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwaveform.js
253 lines (233 loc) · 7.84 KB
/
waveform.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
(function() {
var JSONP, Waveform,
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
window.Waveform = Waveform = (function() {
Waveform.name = 'Waveform';
function Waveform(options) {
this.redraw = __bind(this.redraw, this);
this.container = options.container;
this.canvas = options.canvas;
this.data = options.data || [];
this.outerColor = options.outerColor || "transparent";
this.innerColor = options.innerColor || "#000000";
this.interpolate = true;
if (options.interpolate === false) {
this.interpolate = false;
}
if (this.canvas == null) {
if (this.container) {
this.canvas = this.createCanvas(this.container, options.width || this.container.clientWidth, options.height || this.container.clientHeight);
} else {
throw "Either canvas or container option must be passed";
}
}
this.patchCanvasForIE(this.canvas);
this.context = this.canvas.getContext("2d");
this.width = parseInt(this.context.canvas.width, 10);
this.height = parseInt(this.context.canvas.height, 10);
if (options.data) {
this.update(options);
}
}
Waveform.prototype.setData = function(data) {
return this.data = data;
};
Waveform.prototype.setDataInterpolated = function(data) {
return this.setData(this.interpolateArray(data, this.width));
};
Waveform.prototype.setDataCropped = function(data) {
return this.setData(this.expandArray(data, this.width));
};
Waveform.prototype.update = function(options) {
if (options.interpolate != null) {
this.interpolate = options.interpolate;
}
if (this.interpolate === false) {
this.setDataCropped(options.data);
} else {
this.setDataInterpolated(options.data);
}
return this.redraw();
};
Waveform.prototype.redraw = function() {
var d, i, middle, t, _i, _len, _ref, _results;
this.clear();
this.context.fillStyle = this.innerColor;
middle = this.height / 2;
i = 0;
_ref = this.data;
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
d = _ref[_i];
t = this.width / this.data.length;
if (typeof this.innerColor === "function") {
this.context.fillStyle = this.innerColor(i / this.width, d);
}
this.context.clearRect(t * i, middle - middle * d, t, middle * d * 2);
this.context.fillRect(t * i, middle - middle * d, t, middle * d * 2);
_results.push(i++);
}
return _results;
};
Waveform.prototype.clear = function() {
this.context.fillStyle = this.outerColor;
this.context.clearRect(0, 0, this.width, this.height);
return this.context.fillRect(0, 0, this.width, this.height);
};
Waveform.prototype.patchCanvasForIE = function(canvas) {
var oldGetContext;
if (typeof window.G_vmlCanvasManager !== "undefined") {
canvas = window.G_vmlCanvasManager.initElement(canvas);
oldGetContext = canvas.getContext;
return canvas.getContext = function(a) {
var ctx;
ctx = oldGetContext.apply(canvas, arguments);
canvas.getContext = oldGetContext;
return ctx;
};
}
};
Waveform.prototype.createCanvas = function(container, width, height) {
var canvas;
canvas = document.createElement("canvas");
container.appendChild(canvas);
canvas.width = width;
canvas.height = height;
return canvas;
};
Waveform.prototype.expandArray = function(data, limit, defaultValue) {
var i, newData, _i, _ref;
if (defaultValue == null) {
defaultValue = 0.0;
}
newData = [];
if (data.length > limit) {
newData = data.slice(data.length - limit, data.length);
} else {
for (i = _i = 0, _ref = limit - 1; 0 <= _ref ? _i <= _ref : _i >= _ref; i = 0 <= _ref ? ++_i : --_i) {
newData[i] = data[i] || defaultValue;
}
}
return newData;
};
Waveform.prototype.linearInterpolate = function(before, after, atPoint) {
return before + (after - before) * atPoint;
};
Waveform.prototype.interpolateArray = function(data, fitCount) {
var after, atPoint, before, i, newData, springFactor, tmp;
newData = new Array();
springFactor = new Number((data.length - 1) / (fitCount - 1));
newData[0] = data[0];
i = 1;
while (i < fitCount - 1) {
tmp = i * springFactor;
before = new Number(Math.floor(tmp)).toFixed();
after = new Number(Math.ceil(tmp)).toFixed();
atPoint = tmp - before;
newData[i] = this.linearInterpolate(data[before], data[after], atPoint);
i++;
}
newData[fitCount - 1] = data[data.length - 1];
return newData;
};
Waveform.prototype.optionsForSyncedStream = function(options) {
var innerColorWasSet, that;
if (options == null) {
options = {};
}
innerColorWasSet = false;
that = this;
return {
whileplaying: this.redraw,
whileloading: function() {
var stream;
if (!innerColorWasSet) {
stream = this;
that.innerColor = function(x, y) {
if (x < stream.position / stream.durationEstimate) {
return options.playedColor || "rgba(255, 102, 0, 0.8)";
} else if (x < stream.bytesLoaded / stream.bytesTotal) {
return options.loadedColor || "rgba(0, 0, 0, 0.8)";
} else {
return options.defaultColor || "rgba(0, 0, 0, 0.4)";
}
};
innerColorWasSet = true;
}
return this.redraw;
}
};
};
Waveform.prototype.dataFromSoundCloudTrack = function(track) {
var _this = this;
return JSONP.get("http://waveformjs.org/w", {
url: track.waveform_url
}, function(data) {
return _this.update({
data: data
});
});
};
return Waveform;
})();
JSONP = (function() {
var config, counter, encode, head, jsonp, key, load, query, setDefaults, window;
load = function(url) {
var done, head, script;
script = document.createElement("script");
done = false;
script.src = url;
script.async = true;
script.onload = script.onreadystatechange = function() {
if (!done && (!this.readyState || this.readyState === "loaded" || this.readyState === "complete")) {
done = true;
script.onload = script.onreadystatechange = null;
if (script && script.parentNode) {
return script.parentNode.removeChild(script);
}
}
};
if (!head) {
head = document.getElementsByTagName("head")[0];
}
return head.appendChild(script);
};
encode = function(str) {
return encodeURIComponent(str);
};
jsonp = function(url, params, callback, callbackName) {
var key, query;
query = ((url || "").indexOf("?") === -1 ? "?" : "&");
params = params || {};
for (key in params) {
if (params.hasOwnProperty(key)) {
query += encode(key) + "=" + encode(params[key]) + "&";
}
}
jsonp = "json" + (++counter);
window[jsonp] = function(data) {
callback(data);
try {
delete window[jsonp];
} catch (_error) {}
return window[jsonp] = null;
};
load(url + query + (callbackName || config["callbackName"] || "callback") + "=" + jsonp);
return jsonp;
};
setDefaults = function(obj) {
var config;
return config = obj;
};
counter = 0;
head = void 0;
query = void 0;
key = void 0;
window = this;
config = {};
return {
get: jsonp,
init: setDefaults
};
})();
}).call(this);