forked from cyruzzo/AboveVTT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SoundPad.js
459 lines (386 loc) · 11.8 KB
/
SoundPad.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/**
*
*/
var demo_soundpad = {
"Music": [
{
"name": "Heroic Demise",
"src": "https://opengameart.org/sites/default/files/Heroic%20Demise%20%28New%29_0.mp3",
},
{
"name": "Its our Battle",
"url": "",
"src": "https://opengameart.org/sites/default/files/It%27s%20our%20battle.ogg"
},
{
"name": "Lost in the meadows",
"url": "",
"src": "https://opengameart.org/sites/default/files/lost%20in%20the%20meadows_0.flac"
},
{
"name": "Mystical Town",
"url": "",
"src": "https://opengameart.org/sites/default/files/symphony%20-%20mystical%20town%2001_1.ogg"
}
],
"Backgrounds": [
{
"name": "Rain",
"src": "https://opengameart.org/sites/default/files/audio_preview/1_7.mp3.ogg",
"loop": true
}
],
"Fx": [
{
"name": "Knife Slice",
"src": "https://opengameart.org/sites/default/files/knifesharpener1.flac",
},
{
"name": "Projectile",
"src": "https://opengameart.org/sites/default/files/audio_preview/la.mp3.ogg",
},
{
"name": "Swing",
"url": "",
"src": "https://drive.google.com/uc?id=19wYW7PGs4B383D2E-zPmR2zoQYjC74LL"
},
{
"name": "Sword",
"url": "",
"src": "https://drive.google.com/uc?id=175IZxBm2hsZMA9tobphEYO7Id2mLZzzl"
},
{
"name": "Slime",
"url": "",
"src": "https://drive.google.com/uc?id=1opi5czN0Jh8NN2jZejygIx_dPdxBdFYa"
},
{
"name": "Door",
"url": "",
"src": "https://drive.google.com/uc?id=1tbWhDFU-Nn0zdPkJZu66dCOF2u9I5PGU"
},
{
"name": "Creak",
"url": "",
"src": "https://drive.google.com/uc?id=1htEzWVBlpinRvRvba7zMqpUdoLsJ2a3Y"
}
],
"Monsters": [
{
"name": "Gutteral Beast",
"url": "",
"src": "https://drive.google.com/uc?id=1CLW4mr3REB6EV-RTy58_64i6Z43Sa8Ef"
},
{
"name": "Ogre",
"url": "",
"src": "https://drive.google.com/uc?id=1VwbLG6IvilU6D4bPRdAnQZXfxWiWAq84"
}
]
};
function audio_onplay(e){
var channel=($(e.target).attr('data-channel'));
var audio_object=e.target;
var data={
channel: channel,
time: e.target.currentTime,
volume: e.target.volume,
}
window.MB.sendMessage("custom/myVTT/playchannel",data);
}
function audio_onpause(e){
var channel=($(e.target).attr('data-channel'));
var data={
channel: channel,
}
window.MB.sendMessage("custom/myVTT/pausechannel",data);
}
function audio_onvolumechange(e){
}
function audio_playchannel(channel,time,volume){
element=$("audio[data-channel="+channel+"]").get(0);
element.currentTime=time;
element.volume=volume;
if(element){
element.play();
}
}
function audio_pausechannel(channel){
element=$("audio[data-channel="+channel+"]").get(0);
if(element){
element.pause();
}
}
function audio_changesettings(channel,volume,loop){
element=$("audio[data-channel="+channel+"]").get(0);
element.volume=volume;
element.loop=loop;
}
function build_soundpad(soundpad) {
window.CURRENT_SOUNDPAD=soundpad;
target=$("#soundpad");
console.log("loading soundpad");
target.empty();
var id_count = 0;
btn_addsection=$("<button class='soundpad_addsection'>Add Section</button>");
target.append(btn_addsection);
btn_addsection.click(function(){
newname=prompt("Add a name for the new Soundpad Section");
if(newname){
soundpad[newname]=[];
build_soundpad(soundpad);
}
});
for (let section in soundpad) {
s=$("<div class='soundpad-section'/>");
section_title=$("<div class='soundpad-section-title'>"+section+"</div>");
s.append(section_title);
btn_addsound=$("<button class='soundpad_addsound'>Add Sound</button>");
btn_addsound.click(function(){
soundpad[section].push({
name:'new',
url:'',
});
build_soundpad(soundpad);
});
section_title.append(btn_addsound);
for (let i = 0; i < soundpad[section].length; i++) {
line=$("<div class='soundpad-line'/>")
line.attr("data-section",section);
line.attr("data-position",i);
let title=$("<div class='soundpad-line-title'/>");
title.html(soundpad[section][i].name);
line.append(title);
let edit_title=$("<input>");
edit_title.val(soundpad[section][i].name);
edit_title.hide();
line.append(edit_title);
//line.append("<input class='soundpad-line-title'>"++"</div>");
let audio = $("<audio/>");
audio.attr('data-channel',id_count);
audio.attr("controls","");
audio.attr("controlsList","nodownload");
audio.attr("preload","true");
source = $("<source/>");
source.attr("src", soundpad[section][i].src);
audio.append(source);
line.append(audio);
if(soundpad[section][i].volume){
audio.get(0).volume=soundpad[section][i].volume;
}
else{
audio.get(0).volume=0.5;
}
if (window.DM) {
audio.on('play', audio_onplay);
audio.on('seeked', audio_onplay);
audio.on('pause', audio_onpause);
audio.on('volumechange', function(e) {
var channel = ($(e.target).attr('data-channel'));
var data = {
channel: channel,
volume: e.target.volume,
loop: e.target.loop,
}
window.MB.sendMessage("custom/myVTT/changechannel", data);
// persist volume
soundpad[section][i].volume=e.target.volume;
persist_soundpad();
});
}
let loop_btn=$("<button class='loop-button'/>");
let loop_img=$("<img class='loop-img'>");
loop_img.attr("src",window.EXTENSION_PATH+"assets/loop.png");
loop_btn.append(loop_img);
if(soundpad[section][i].loop){
audio.get(0).loop=true;
loop_btn.addClass('loop-pressed');
}
loop_btn.click(function() {
var looping = !soundpad[section][i].loop;
audio.get(0).loop = looping;
soundpad[section][i].loop = looping;
if (looping)
$(this).addClass('loop-pressed');
else
$(this).removeClass('loop-pressed');
persist_soundpad();
var data = {
channel: audio.attr('data-channel'),
volume: audio.get(0).volume,
loop: audio.get(0).loop,
}
window.MB.sendMessage("custom/myVTT/changechannel", data);
}
);
line.append(loop_btn);
let url=$("<input type='text'>");
url.val(soundpad[section][i].src);
url.attr("placeholder","http://www.audio.com/mp3");
url.hide();
line.append(url);
let btn_save=$("<button class='soundpad_save'>OK</button>");
btn_save.hide();
line.append(btn_save);
let btn_cancel=$("<button class='soundpad_cancel'>CANCEL</button>");
btn_cancel.hide();
line.append(btn_cancel);
let btn_edit=$("<button class='soundpad_editsound'>E</button>");
line.append(btn_edit);
let btn_del=$("<button class='soundpad_delsound'>X</button>");
line.append(btn_del);
btn_edit.click(function(){
audio.hide();
title.hide();
edit_title.show();
btn_edit.hide();
btn_del.hide();
btn_save.show();
btn_cancel.show();
url.show();
});
btn_cancel.click(function(){
audio.show();
title.show();
edit_title.hide();
btn_edit.show();
btn_del.show();
btn_save.hide();
btn_cancel.hide();
url.hide();
});
btn_save.click(function(){
soundpad[section][i].name=edit_title.val();
var newurl=url.val();
if(newurl.startsWith("https://drive.google.com") && newurl.indexOf("uc?id=") < 0)
newurl='https://drive.google.com/uc?id=' + newurl.split('/')[5];
url.val(newurl);
soundpad[section][i].src=newurl;
build_soundpad(soundpad);
data={
soundpad: soundpad
}
window.MB.sendMessage("custom/myVTT/soundpad",data);
});
btn_del.click(function(){
soundpad[section].splice(i,1);
build_soundpad(soundpad);
data={
soundpad: soundpad
}
window.MB.sendMessage("custom/myVTT/soundpad",data);
});
s.append(line);
id_count++;
}
target.append(s);
}
soundpad_check_editable(); // hide / show buttons
if(window.DM)
persist_soundpad();
}
function soundpad_check_editable(){
if($("#soundpad_enable_edit").is(":checked")){
$(".soundpad_editsound").show();
$(".soundpad_delsound").show();
$(".soundpad_addsound").show();
$(".soundpad_addsection").show();
}
else{
$(".soundpad_editsound").hide();
$(".soundpad_delsound").hide();
$(".soundpad_addsound").hide();
$(".soundpad_addsection").hide();
}
}
function persist_soundpad(){
localStorage.setItem("Soundpads",JSON.stringify(window.SOUNDPADS));
}
function init_audio(){
window.CURRENT_SOUNDPAD={};
if (localStorage.getItem("Soundpads") != null) {
window.SOUNDPADS = $.parseJSON(localStorage.getItem("Soundpads"));
}
else {
window.SOUNDPADS = {};
}
if(!("DEMO" in window.SOUNDPADS)){
window.SOUNDPADS['DEMO']=demo_soundpad;
}
soundsPanel.header.append("<div class='panel-warning'>EXPERIMENTAL FEATURE (still ugly but should work)</div>");
youtube_section=$("<div class='youtube_section'/>");;
youtube_section.append("Animated Map Volume): ");
youtube_volume = $('<input type="range" min="0." max="100" value="50" step="1" class="slider" id="youtube_volume">');
youtube_section.append(youtube_volume);
soundsPanel.body.append(youtube_section);
youtube_volume.on("change", function() {
if (window.YTPLAYER) {
window.YTPLAYER.setVolume($("#youtube_volume").val());
data={
volume: $("#youtube_volume").val()
};
window.MB.sendMessage("custom/myVTT/changeyoutube",data);
}
});
if(!window.DM)
youtube_section.hide();
if(window.DM){
selector_section=$("<div/>");
soundpad_selector=$("<select id='soundpad_selector'/>");
soundpad_selector.append("<option value=''>-</option>");
for(k in window.SOUNDPADS){
soundpad_selector.append($("<option/>").attr('value',k).html(k));
}
selector_section.append("Load Soundpad:");
selector_section.append(soundpad_selector);
selector_section.append("Enable Edit:");
soundpad_enable_edit=$("<input type='checkbox' id='soundpad_enable_edit'>");
soundpad_enable_edit.change(soundpad_check_editable);
selector_section.append(soundpad_enable_edit);
btn_addsoundpad=$("<button class='soundpad_add'>NEW</button>");
selector_section.append(btn_addsoundpad);
soundsPanel.body.append(selector_section);
btn_addsoundpad.click(function(){
var newname=prompt("New Soundpad Name");
if(newname){
window.SOUNDPADS[newname]={};
soundpad_selector.append($("<option/>").attr('value',newname).html(newname));
soundpad_selector.val(newname);
$("#soundpad_enable_edit").prop('checked', true);
build_soundpad(window.SOUNDPADS[newname]);
}
});
btn_delsoundpad=$("<button class='soundpad_del'>DEL</button>");
btn_delsoundpad.click(function(){
c=confirm("Are you sure that you want to delete the current soundpad?");
if(c){
delete window.SOUNDPADS[soundpad_selector.val()];
$("#soundpad").empty();
soundpad_selector.empty();
soundpad_selector.append("<option value=''>-</option>");
for (k in window.SOUNDPADS) {
soundpad_selector.append($("<option/>").attr('value', k).html(k));
}
persist_soundpad();
}
});
selector_section.append(btn_delsoundpad);
soundpad_selector.on("change",function(){
$("#soundpad").empty();
soundpad_id=$("#soundpad_selector").val();
if(soundpad_id!=""){
build_soundpad(window.SOUNDPADS[soundpad_id]);
data={
soundpad: window.SOUNDPADS[soundpad_id]
}
window.MB.sendMessage("custom/myVTT/soundpad",data);
}
});
}
soundpad_element=$("<div id='soundpad'>");
soundsPanel.body.append(soundpad_element);
if(!window.DM){
soundpad_element.hide();
soundsPanel.body.append("Music is entirely controlled by the DM. As a player you'll find a master volume control here... but sorry.. it's not ready :('");
}
}