forked from copy/iwbtc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
audio.js
209 lines (173 loc) · 3.95 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
"use strict";
/**
* @constructor
*/
function AudioManager(enabled)
{
this.muted = !enabled;
this.works = true;
if(!window.Audio)
{
this.works = false;
}
// save the planet, use ogg/vorbis
else if(!new Audio().canPlayType("audio/ogg; codecs=vorbis"))
{
this.works = false;
}
//this.works = false;
this.playing = [];
// files that are enqueued when the game is muted, so
// they can be played as soon as someone unmutes
this.playQueue = [];
}
/**
* @param {boolean=} loop
* @param {boolean=} once
* @param {number=} volume
* @param {number=} startTime
*/
AudioManager.prototype.play = function(file, loop, once, volume, startTime)
{
if(!this.works)
{
return;
}
if(this.muted)
{
this.playQueue.push([new Date, Array.toArray(arguments)]);
return;
}
var existing = this.playing.filter(Function.byIndex("file", file)),
audioObject = existing.find(playable),
audio;
function playable(obj)
{
return (obj.audio.ended || obj.audio.paused) != !!once;
}
if(audioObject)
{
// if the audio ended and can be player more than once
// OR it's running and shouldn't be played more than once,
// restart it
audio = audioObject.audio;
}
else
{
audio = new Audio(this.path + file);
this.playing.push({
audio: audio,
file: file,
});
}
var dontPlay = false;
if(startTime !== undefined)
{
// audio.duration is NaN if it's not known yet, so this
// check should be safe
if(audio.duration && (audio.duration < startTime))
{
// we're too late
dontPlay = true;
}
}
else
{
startTime = 0;
}
if(!dontPlay)
{
if(volume !== undefined)
{
audio.volume = volume;
}
if(audio.readyState < 4)
{
audio.addEventListener("loadedmetadata", function()
{
audio.currentTime = startTime;
});
}
else
{
audio.currentTime = startTime;
}
audio.loop = loop;
audio.play();
}
};
/**
* @param {function()=} onload
* @param {function()=} onerror
*/
AudioManager.prototype.preload = function(file, onload, onerror)
{
// no check is made if the game is muted
if(this.works && !this.muted)
{
var existing = this.playing.find(Function.byIndex("file", file));
if(existing)
{
if(onload)
{
setTimeout(onload, 0);
}
return;
}
var audio = new Audio(this.path + file);
audio.muted = this.muted;
if(onload)
{
audio.addEventListener("canplaythrough", onload);
if(onerror)
{
audio.addEventListener("error", onerror);
}
}
audio.load();
this.playing.push({
audio: audio,
file: file,
});
return audio;
}
else
{
setTimeout(onload, 0);
}
};
AudioManager.prototype.stop = function(file)
{
if(this.works)
{
var audioObjs = this.playing.filter(Function.byIndex("file", file));
audioObjs.forEach(stop);
}
function stop(obj)
{
obj.audio.pause();
//obj.currentTime = 0;
}
};
AudioManager.prototype.toggleMute = function()
{
if(this.works)
{
var muted = this.muted = !this.muted;
this.playing.forEach(setMute);
}
if(!this.muted)
{
var self = this;
this.playQueue.forEach(function(args)
{
args[1][4] = (Date.now() - args[0]) / 1000;
self.play.apply(self, args[1]);
});
this.playQueue = [];
}
function setMute(audioObj)
{
audioObj.audio.muted = muted;
}
};