-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathaudio-fx.js
116 lines (84 loc) · 3 KB
/
audio-fx.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
AudioFX = function() {
//---------------------------------------------------------------------------
var VERSION = '0.4.0';
//---------------------------------------------------------------------------
var hasAudio = false, audio = document.createElement('audio'), audioSupported = function(type) { var s = audio.canPlayType(type); return (s === 'probably') || (s === 'maybe'); };
if (audio && audio.canPlayType) {
hasAudio = {
ogg: audioSupported('audio/ogg; codecs="vorbis"'),
mp3: audioSupported('audio/mpeg;'),
m4a: audioSupported('audio/x-m4a;') || audioSupported('audio/aac;'),
wav: audioSupported('audio/wav; codecs="1"'),
loop: (typeof audio.loop === 'boolean') // some browsers (FF) dont support loop yet
}
}
//---------------------------------------------------------------------------
var create = function(src, options, onload) {
var audio = document.createElement('audio');
if (onload) {
var ready = function() {
audio.removeEventListener('canplay', ready, false);
onload();
}
audio.addEventListener('canplay', ready, false);
}
if (options.loop && !hasAudio.loop)
audio.addEventListener('ended', function() { audio.currentTime = 0; audio.play(); }, false);
audio.volume = options.volume || 0.1;
audio.autoplay = options.autoplay;
audio.loop = options.loop;
audio.src = src;
return audio;
}
//---------------------------------------------------------------------------
var choose = function(formats) {
for(var n = 0 ; n < formats.length ; n++)
if (hasAudio && hasAudio[formats[n]])
return formats[n];
};
//---------------------------------------------------------------------------
var find = function(audios) {
var n, audio;
for(n = 0 ; n < audios.length ; n++) {
audio = audios[n];
if (audio.paused || audio.ended)
return audio;
}
};
//---------------------------------------------------------------------------
var afx = function(src, options, onload) {
options = options || {};
var formats = options.formats || [],
format = choose(formats),
pool = [];
src = src + (format ? '.' + format : '');
if (hasAudio) {
for(var n = 0 ; n < (options.pool || 1) ; n++)
pool.push(create(src, options, n == 0 ? onload : null));
}
else {
onload();
}
return {
audio: (pool.length == 1 ? pool[0] : pool),
play: function() {
var audio = find(pool);
if (audio)
audio.play();
},
stop: function() {
var n, audio;
for(n = 0 ; n < pool.length ; n++) {
audio = pool[n];
audio.pause();
audio.currentTime = 0;
}
}
};
};
//---------------------------------------------------------------------------
afx.version = VERSION;
afx.supported = hasAudio;
return afx;
//---------------------------------------------------------------------------
}();