-
Notifications
You must be signed in to change notification settings - Fork 2
/
audio.js
274 lines (253 loc) · 6.77 KB
/
audio.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
// var jukebox;
var audio_clips={
hover:'Cursor1.mp3',
select:'Item1.mp3',
unselect:'Cursor2.mp3',
cancel:'Cancel1.mp3',
miss:'Evasion2.mp3',
hpup:'Recovery7.mp3',
mpup:'Recovery3.mp3',
noeffect:'',
revive:'Cold10.mp3'
};
var audio_cached=0;
var sound_list={};
var audio_handles={}
var music_handle=null;
var sound_volume = 100;
var music_volume = 100;
function init_audio(callback)
{
soundManager = new SoundManager();
soundManager.url = swfdir;
soundManager.flashVersion = 9; // optional: shiny features (default = 8)
soundManager.useFlashBlock = false; // optionally, enable when you're ready to dive in
soundManager.onready(callback);
soundManager.ontimeout(callback);
//soundManager.useHTML5Audio = true;
soundManager.reboot(); // start SM2 init.
//jukebox=document.getElementById('Jukebox');
}
function load_static_sounds()
{
for (var clip in audio_clips)
{
if(audio_clips[clip])
{
try {
load_sound(sound+audio_clips[clip])
//jukebox.load_sound(sound+audio_clips[clip],9);
//audio_cached++;
}
catch(e){
alert(e);
}
}
}
}
function create_clip(fullurl)
{
index = 0;
while (soundManager.getSoundByID && soundManager.getSoundByID(fullurl + index))
index++;
if (!soundManager.canPlayURL(fullurl))
{
if (fullurl.slice(-4)=='.mp3')
{
fullurl = fullurl.slice(0,-4) + '.mp3';
}
if (!soundManager.canPlayURL(fullurl))
{
window.status = fullurl + ' cannot play. '
}
}
clip = soundManager.createSound({
id: fullurl+index,
url: fullurl,
autoLoad: true,
loops: 1
});
return clip;
}
function load_sound(fullurl)
{
if(!(fullurl in audio_handles))
{
clip = create_clip(fullurl);
audio_handles[fullurl] = [clip];
}
}
function count_initializing_clips()
{
var count = 0;
for (url in audio_handles)
{
handle = audio_handles[url][0];
if ((handle.networkState && handle.networkState == handle.NETWORK_LOADING) || (handle.readyState && handle.readyState == 1))
count++;
}
return count;
}
function add_to_sound_list(url)
{
if(url=='') return;
if(!(url in sound_list))
sound_list[url]=0;
sound_list[url]++;
}
function cache_fight_sounds()
{
sound_list={};
var pindex,gindex,cindex;
var party,group,character;
var personality,ability,item;
var index,index2,index3;
for(pindex in fight.parties)
{
party=fight.parties[pindex];
for(gindex in party.groups)
{
group=party.groups[gindex];
for(cindex in group.characters)
{
character=group.characters[cindex];
personality=personalities[character.personalityid];
for(index in personality)
{
if(index.slice(-5)=='_data')
{
for(index2 in personality[index].sounds)
{
add_to_sound_list(personality[index].sounds[index2]);
}
}
}
for(index in character.abilities)
{
ability=abilities[character.abilities[index]];
for(index2 in ability.impact_data.sounds)
add_to_sound_list(ability.impact_data.sounds[index2]);
}
for(index in character.inventory)
{
item=items[character.inventory[index].item];
for(index2 in item.fight_impact_data.sounds)
add_to_sound_list(item.fight_impact_data.sounds[index2]);
for(index2 in item.use_impact_data.sounds)
add_to_sound_list(item.use_impact_data.sounds[index2]);
}
}
}
}
//now load the sounds
for(index in sound_list)
{
load_sound(sound+index);
//jukebox.load_sound(sound+index,sound_list[index]);
audio_cached++;
}
}
function is_audio_cache_loaded()
{
var count = count_initializing_clips();
//var count=jukebox.countInitializingPlayers();
if(count>audio_cached) audio_cached=count;
if(count==0)
{
audio_cached=0;
return [1,1];
}
return [audio_cached-count,audio_cached];
}
function set_music_volume(vol)
{
//if(jukebox) jukebox.setMusicVolume(vol);
music_volume = vol;
}
function set_sound_volume(vol)
{
//if(jukebox) jukebox.setSoundVolume(vol);
sound_volume = vol;
}
function prep_voices(url,number)
{
fullurl = sound+url;
load_sound(fullurl);
//jukebox.load_sound(sound+url,number);
if (audio_handles[fullurl].length < number)
{
nextclip = create_clip(fullurl);
audio_handles[fullurl].unshift(nextclip);
}
}
function get_clip(fullurl)
{
if (!audio_handles[fullurl]) return null;
clip = audio_handles[fullurl].shift()
if (audio_handles[fullurl].length == 0 || !audio_handles[fullurl][0].ended)
{
nextclip = create_clip(fullurl);
audio_handles[fullurl].unshift(nextclip);
}
audio_handles[fullurl].push(clip);
return clip;
}
function stop_music()
{
if (music_handle)
music_handle.pause();
/*
if(jukebox)
try {
music_handle.stop();
//jukebox.stop_music();
}
catch(e){
alert(e);
}
*/
}
function play_music(url)
{
if (url)
{
stop_music();
load_sound(music+url);
music_handle = get_clip(music+url);
music_handle.setVolume(music_volume);
music_handle.play({loops:999});
/*
if(jukebox)
try {
jukebox.play_music(music+url);
}
catch(e){
alert(e);
}
*/
}
}
function play_sound(url)
{
if(url)
{
clip = get_clip(sound+url)
clip.setVolume(sound_volume);
clip.play();
return clip;
/*
if(jukebox)
try {
jukebox.play_sound(sound+url);
}
catch(e){
alert(e);
}
*/
}
return null;
}
function pan_sound(clip, pan)
{
clip.setPan(pan);
}