-
Notifications
You must be signed in to change notification settings - Fork 9
/
Audio.js
executable file
·115 lines (91 loc) · 2.53 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
var Sburb = (function(Sburb){
Sburb.globalVolume = 1;
///////////////////////////////////////
//Sound Class
///////////////////////////////////////
//Constructor
Sburb.Sound = function(asset){
if (asset) {
this.asset = asset;
var that = this;
window.addEventListener('beforeunload', function() {
that.pause();
});
}
}
//play this sound
Sburb.Sound.prototype.play = function(pos) {
if(window.chrome) {
if(this.playedOnce) {
// console.log("load again");
this.asset.load();
} else {
this.playedOnce = true;
}
if(pos) {
// chrome doesnt like us changing the play time
// unless we're already playing
var oThis = this;
this.asset.addEventListener('playing', function() {
oThis.asset.currentTime = pos;
oThis.asset.pause();
oThis.asset.removeEventListener('playing', arguments.callee);
oThis.asset.play();
},false);
}
} else if(pos) {
this.asset.currentTime = pos;
}
this.fixVolume();
this.asset.play();
}
//pause this sound
Sburb.Sound.prototype.pause = function() {
this.asset.pause();
//console.log("pausing the sound...");
}
//stop this sound
Sburb.Sound.prototype.stop = function() {
this.pause();
this.asset.currentTime = 0;
//console.log("stopping the sound...");
}
//has the sound stopped
Sburb.Sound.prototype.ended = function() {
return this.asset.ended;
}
//ensure the sound is playing at the global volume
Sburb.Sound.prototype.fixVolume = function(){
this.asset.volume = Sburb.globalVolume;
//console.log("fixing the volume...");
}
/////////////////////////////////////
//BGM Class (inherits Sound)
/////////////////////////////////////
//constructor
Sburb.BGM = function(asset, startLoop, priority) {
Sburb.Sound.call(this,asset);
this.startLoop = 0;
this.endLoop = 0;
this.setLoopPoints(startLoop?startLoop:0);
}
Sburb.BGM.prototype = new Sburb.Sound();
//set the points in the sound to loop
Sburb.BGM.prototype.setLoopPoints = function(start, end) {
tmpAsset = this.asset
tmpAsset.addEventListener('ended', function() {
// console.log("I'm loopin' as hard as I can cap'n! (via event listener)");
tmpAsset.currentTime = start;
tmpAsset.play();
},false);
this.startLoop = start;
this.endLoop = end;
// do we need to have an end point? does that even make sense
}
//loop the sound
Sburb.BGM.prototype.loop = function() {
// console.log("looping...");
this.play(this.startLoop);
}
return Sburb;
})(Sburb || {});