This repository has been archived by the owner on Apr 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsoundbank.js
266 lines (261 loc) · 9.18 KB
/
soundbank.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
(function (){
var _ = function(m)
{
if("babelfish" in window)
return window.babelfish.translate(m);
return m;
};
var soundbank = {
loadedSounds: {},
configure: function(data)
{
window.soundbank.config.values = data;
for(var i = 0; i < window.soundbank.config.values.sounds.length; ++i)
if(window.soundbank.config.values.sounds[i].preload)
window.soundbank.loadSound(window.soundbank.config.values.sounds[i]);
window.soundbank.getHostedNoises();
},
install: function(sounds)
{
var newSounds = [];
for(var i = 0; i < sounds.length; ++i)
if(!window.soundbank.haveNoise(sounds[i]))
newSounds.push(sounds[i]);
for(var i = 0; i < newSounds.length; ++i)
{
window.soundbank.config.values.sounds.push(newSounds[i]);
if(newSounds[i].preload)
window.soundbank.loadSound(newSounds[i]);
}
window.soundbank.save();
},
installCustom: function(name, file, volume, preload)
{
if(file.val().trim() == '')
{
file.focus();
file.animate({opacity: 0.5}, 'slow', 'swing', function(){ file.animate({opacity: 1}, 'fast'); });
return;
}
if(name.val().trim() == '' || window.soundbank.haveNoise(name.val().trim()))
{
name.focus();
name.animate({opacity: 0.5}, 'slow', 'swing', function(){ name.animate({opacity: 1}, 'fast'); });
return;
}
window.soundbank.install(
[{ file: file.val().trim(), name: name.val().trim(), volume: volume.val() * 1, preload: preload.val() == 1 }]
);
window.settings.open();
},
haveNoise: function(noise)
{
for(var i = 0; i < window.soundbank.config.values.sounds.length; ++i)
if(window.soundbank.config.values.sounds[i].name == noise.name)
return true;
},
save: function()
{
window.settings.configuration.soundbank = window.soundbank.config.values;
window.settings.saveConfiguration();
},
load: function()
{
var defaults = window.soundbank.config.values;
window.freshy.waitFor('settings', function() { window.settings.setDefaults('soundbank', defaults); });
window.freshy.systemLoaded('soundbank');
},
getHostedNoises: function()
{
for(var i = 0; i < window.soundbank.config.values.hosts.length; ++i)
$.getScript(window.soundbank.config.values.hosts[i]);
},
loadSound: function(sound)
{
window.soundbank.loadedSounds[sound.name] = new Audio(decodeURIComponent(sound.file));
window.soundbank.loadedSounds[sound.name].volume = sound.volume / 100.0;
return true;
},
play: function(name)
{
if(window.soundbank.config.values.mute || !name)
return;
if(!window.soundbank.loadedSounds[name] && !window.soundbank.loadNamed(name))
return;
window.soundbank.loadedSounds[name].play();
},
loadNamed: function(name)
{
for(var i = 0; i < window.soundbank.config.values.sounds.length; ++i)
if(window.soundbank.config.values.sounds[i].name == name)
return window.soundbank.loadSound(window.soundbank.config.values.sounds[i]);
return false;
},
config:
{
values: { hosts: [ 'https://i.animemusic.me/sounds/soundbank.js' ], sounds: [], mute: 0, volume: 100 },
get: function()
{
return [
{
title: _('Sound notifications'),
type: 'left',
options: [
{ type: 'select', name: 'enabled', value: window.soundbank.config.values.mute ? '0' : '1', options: [{value:'0', label:_('Sounds disabled')},{value:'1', label:_('Sounds enabled')}] },
{ type: 'select', name: 'reset', value: 0, options: [{value: 0, label:_('Reset')},{value: 1, label:_('Reload from host')}] },
{ type: 'custom', content: window.soundbank.config.getSoundList() }
]
},
{
title: _('Add custom notification sound'),
type: 'left',
options: [
{ type: 'custom', content: window.soundbank.config.getCustomForm() }
]
}
];
},
getSelector: function(showEmpty, showHidden, currentValue)
{
var sounds = showEmpty ? [{value: '', label: ''}] : [];
for(var i = 0; i < window.soundbank.config.values.sounds.length; ++i)
{
var sound = window.soundbank.config.values.sounds[i];
if(!sound.hidden || showHidden)
sounds.push({value: sound.name, label: sound.name});
}
return {
type: 'select',
name: 'sound',
value: currentValue,
options: sounds
};
},
set: function(config, value)
{
if(config.name == 'enabled')
{
window.soundbank.config.values.mute = (value == 0);
}
else if(config.name == 'volume')
{
var newValue = value * 1;
if("sound" in config)
{
if(!isNaN(newValue))
window.soundbank.config.values.sounds[config.sound].volume = Math.min(100,Math.max(0, newValue));
value = window.soundbank.config.values.sounds[config.sound].volume;
}
else
{
if(!isNaN(newValue))
window.soundbank.config.values.volume = Math.min(100,Math.max(0, newValue));
value = window.soundbank.config.values.volume;
}
window.soundbank.config.applyVolume();
}
else if(config.name == 'preload')
{
window.soundbank.config.values.sounds[config.idx].preload = (value == '1');
if(window.soundbank.config.values.sounds[config.idx].preload)
window.soundbank.loadSound(window.soundbank.config.values.sounds[config.idx]);
}
else if(config.name == 'reset' && value == 1)
{
window.soundbank.config.values.sounds = [];
window.soundbank.loadedSounds = {};
window.soundbank.getHostedNoises();
window.settings.cleanup();
return 0;
}
window.soundbank.save();
return value;
},
applyVolume: function()
{
for(var i = 0; i < window.soundbank.config.values.sounds.length; ++i)
{
var name = window.soundbank.config.values.sounds[i].name;
if(name in window.soundbank.loadedSounds)
window.soundbank.loadedSounds[name].volume = (window.soundbank.config.values.sounds[i].volume / 100.0) * (window.soundbank.config.values.volume / 100.0);
}
},
getCustomForm: function()
{
var name = $('<input type="text" size="15" />');
var file = $('<input type="text" size="28" />');
var volume = $('<input type="text" size="3" value="100" />%');
var preload = $('<select><option value="0">'+_('No')+'</option><option value="1">'+_('Yes')+'</option></select>');
preload.val(1);
var submit = $('<button style="cursor:pointer"><i class="icon icon-check-purple" /></button>');
submit.click(function(){ window.soundbank.installCustom(name, file, volume, preload); });
volume.keyup(function()
{
var newValue = volume.val() * 1;
if(isNaN(newValue) || newValue < 0 || newValue > 100)
return false;
return true;
});
return $('<table style="position:relative"></table>')
.append($('<tr><th>'+_('Name')+'</th><th>'+_('Volume')+'</th><th>'+_('Preload')+'</th></tr>'))
.append($('<tr></tr>').append(name).append($('<td></td').append(name), $('<td></td>').append(volume), $('<td></td>').append(preload), $('<td></td>').append(submit)))
.append($('<tr><th colspan="3">'+_('Soundfile (must use https://)')+'</th></tr>'))
.append($('<tr></tr>').append($('<td colspan="3"></td>').append(file)))
},
getSoundList: function()
{
var list = $('<table style="position:relative"><tr><th style="width:15px;"> </th><th>'+_('Name')+'</th><th>'+_('Volume')+'</th><th>'+_('Preload')+'</th></table>');
for(var i = -1; i < window.soundbank.config.values.sounds.length; ++i)
if(i < 0 || !window.soundbank.config.values.sounds[i].hidden)
list.append(window.soundbank.config.getSoundConfigurator(i));
return list;
},
getSoundConfigurator: function(idx)
{
if(idx < 0)
{
var item = $('<tr><td> </td><td>'+_('Master channel')+'</td></tr>');
var volume = $('<input style="text-align:right"type="text" size="3"/>');
volume.val(window.soundbank.config.values.volume);
volume.keyup(function(){
var validated = window.soundbank.config.set({name: 'volume'}, volume.val());
if(validated != volume.val())
volume.val(validated);
});
item.append($('<td></td>').append(volume).append('%'));
return item;
}
var sound = window.soundbank.config.values.sounds[idx];
var volume = $('<input style="text-align:right"type="text" size="3"/>');
volume.val(sound.volume);
volume.keyup(function(){
var validated = window.soundbank.config.set({name: 'volume', sound: idx }, volume.val());
if(validated != volume.val())
volume.val(validated);
});
return $('<tr></tr>')
.append($('<td style="cursor:pointer"><i class="icon icon-chat-admin" /> </td>').click(function(){window.soundbank.play(sound.name);}))
.append($('<td>'+sound.name+'</td>'))
.append($('<td></td>').append(volume).append('%'))
.append($('<td></td>').append(
window.settings.buildOption(window.soundbank,
{
type: 'select',
name: 'preload',
idx: idx,
value: window.soundbank.config.values.sounds[idx].preload ? '1' : '0',
options: [{value:'0',label:_('No')},{value:'1',label:_('Yes')}]
}
)
));
}
}
};
if("soundbank" in window)
{
soundbank.loadedSounds = window.soundbank.loadedSounds;
soundbank.config.values = window.soundbank.config.values;
}
window.soundbank = soundbank;
window.soundbank.load();
})();