-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
206 lines (206 loc) · 6.05 KB
/
script.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
function File(path,title,artist,album,key) {
this.path = path;
this.title = title;
this.artist = artist;
this.album = album;
this.key = key;
}
function getName(item,by) {
var res = item.title;
if (by !== "title") {
res = item[by]+" ("+res+")";
}
return res;
}
function urlKeys(obj,startWithAmp) {
startWithAmp = typeof startWithAmp === "boolean" ? startWithAmp : false;
var res = "";
for (var item in obj) {
if (obj.hasOwnProperty(item)) {
res += "&"+item+"="+obj[item];
}
}
if (!startWithAmp) {
res = res.substr(1);
}
return res;
}
function setupSongList(by) {
select.innerHTML = "";
var options = [];
var option;
for (var item in cipher) {
if (cipher.hasOwnProperty(item)) {
option = document.createElement("option");
option.value = item;
option.textContent = getName(cipher[item],by);
if (item === chosen.key) {
option.selected = "selected";
}
options.push(option);
}
}
options.sort(function(a,b) {
return a.textContent.toLowerCase().localeCompare(b.textContent.toLowerCase());
});
for (var i = 0; i < options.length; i++) {
select.appendChild(options[i]);
}
updateExtraBy(by);
}
function updateExtraBy(by) {
if (by !== "title") {
extra.by = by;
} else if (extra.hasOwnProperty("by")) {
delete extra.by;
}
}
var interface;
var select;
var cipher = {};
var extra = {};
var chosen;
window.addEventListener("DOMContentLoaded",function() {
if (window.AudioContext || window.webkitAudioContext) {
songs = parseXMLFromString(songs).getElementsByTagName("song");
var key;
for (var i = 0; i < songs.length; i++) {
key = songs[i].getAttribute("id");
cipher[key] = new File(songs[i].getAttribute("file"),songs[i].querySelector("title").textContent,songs[i].querySelector("artist").textContent,songs[i].querySelector("album").textContent,key);
}
var query = extractQuery();
chosen = cipher.hasOwnProperty(query.f) ? cipher[query.f] : cipher.muffet;
var interface = document.getElementById("interface");
var width = 700;
if (query.width) {
query.width = parseInt(query.width);
if (!isNaN(query.width) && query.width > 0) {
width = query.width;
extra.width = query.width;
}
}
var clipping = false;
if (query.clip) {
query.clip = query.clip.toLowerCase();
if (query.clip === "true" || query.clip === "false") {
clipping = query.clip === "true";
extra.clip = query.clip;
}
}
var loop = false;
if (query.loop) {
query.loop = query.loop.toLowerCase();
if (query.loop === "true" || query.loop === "false") {
loop = query.loop === "true";
extra.loop = query.loop;
}
}
var jade = "#4ac925";
var aradia = "#a10000";
var interfaceContainer = interface;
interface = new WFPInterface({
audio: {
file: "audio/"+chosen.path,
loop: loop,
autoplay: true
},
width: width,
container: interface,
waveform: {
height: 400,
color: jade,
refreshRate: 45
},
bars: {
height: 16,
color: jade
},
timeline: {
height: 128,
playhead: {
arrowColor: jade,
lineColor: aradia
},
bar: {
color: "#005682"
},
delimiters: {
color: "white",
text: {
color: "#ffbe00"
}
},
waveform: {
meanType: MeanType.GEOMETRIC,
pastColor: "#3232c8",
futureColor: "#6464dc",
meanPastColor: aradia,
meanFutureColor: "#77003c",
clippingExists: clipping
}
},
buttons: {
type: "button",
buttonClassName: "btn clearRight"
},
metadata: {
exists: true,
title: chosen.title,
artist: "A noise created by <em>\""+chosen.artist+"\"</em>",
album: "From the collection of noises know as <em>\""+chosen.album+"\"</em>",
containerClassName: "metadata",
titleClassName: "metadataTitle",
artistClassName: "metadataArtist",
albumClassName: "metadataAlbum"
}
});
interface.player.addEventListener("decoded",function() {
interface.player.setWaveformThickness(2);
});
select = document.createElement("select");
var by = "title";
if (query.by) {
query.by = query.by.toLowerCase();
if (query.by === "title" || query.by === "artist" || query.by === "album") {
by = query.by;
}
}
select.addEventListener("change",function() {
location.href = location.origin+location.pathname+"?f="+select.value+urlKeys(extra,true);
});
setupSongList(by);
var sortList = document.createElement("select");
var setupOption = function(value,text) {
var option = document.createElement("option");
option.value = value;
option.textContent = text;
if (by === value) {
option.selected = "selected";
}
sortList.appendChild(option);
};
setupOption("title","Sort by Title");
setupOption("artist","Sort by Artist");
setupOption("album","Sort by Album");
sortList.addEventListener("change",function() {
setupSongList(sortList.value);
});
var p;
var hang = setTimeout(function() {
p = document.createElement("p");
p.innerHTML = "Note that this tech demo can sometimes hang on Opera, giving no error message to tell me that this is unsupported. Try refreshing the page, and if that doesn't fix the problem switch to another browser.<br />If you are not using Opera and you are seeing this message, either your computer is slow, the song you requested is long, or both.";
document.body.appendChild(p);
},10000);
interface.player.addEventListener("decoded",function() {
clearTimeout(hang);
if (typeof p !== "undefined") {
document.body.removeChild(p);
}
interfaceContainer.appendChild(select);
interfaceContainer.appendChild(sortList);
sortList.style.marginLeft = (width - (select.offsetWidth + sortList.offsetWidth))+"px"; // width all - (width normal + width this)
});
} else {
document.body.innerHTML = "<p>ERROR: You are using an unsupported browser. To see which browsers are compatible with this tech demo, see <a target='_blank' href='http://caniuse.com/#feat=audio-api'>this page</a>.</p>";
}
});