1 | var AssetLoader = require("./assets/asset-loader");
|
2 | var loadSound = require("./assets/load-sound");
|
3 |
|
4 | window.AudioContext = window.AudioContext || window.webkitAudioContext;
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | function SoundManager(manifest) {
|
13 | |
14 |
|
15 |
|
16 |
|
17 |
|
18 | this.muted = false;
|
19 | |
20 |
|
21 |
|
22 |
|
23 |
|
24 | this.looping = {};
|
25 |
|
26 | |
27 |
|
28 |
|
29 |
|
30 |
|
31 | this.context = new window.AudioContext();
|
32 |
|
33 | this.gainNode = this.context.createGain();
|
34 | this.gainNode.connect(this.context.destination);
|
35 | this.volume = this.gainNode.gain.value;
|
36 | this.installSafariWorkaround();
|
37 | this.assets = new AssetLoader(manifest, loadSound.bind(undefined, this.context));
|
38 | }
|
39 | SoundManager.prototype.installSafariWorkaround = function() {
|
40 |
|
41 |
|
42 | var firstTouchHandler = function() {
|
43 | window.removeEventListener("click", firstTouchHandler);
|
44 | window.removeEventListener("keydown", firstTouchHandler);
|
45 | window.removeEventListener("touchstart", firstTouchHandler);
|
46 |
|
47 | var source = this.context.createOscillator();
|
48 | source.connect(this.gainNode);
|
49 | source.start(0);
|
50 | source.stop(0);
|
51 |
|
52 | if (this.firstPlay) {
|
53 | this.play(this.firstPlay, this.firstPlayLoop);
|
54 | } else {
|
55 | this.firstPlay = "workaround";
|
56 | }
|
57 | }.bind(this);
|
58 | window.addEventListener("click", firstTouchHandler);
|
59 | window.addEventListener("keydown", firstTouchHandler);
|
60 | window.addEventListener("touchstart", firstTouchHandler);
|
61 | };
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 | SoundManager.prototype.play = function(name, loop) {
|
68 | if (loop && this.looping[name]) {
|
69 | return;
|
70 | }
|
71 | if (!this.firstPlay) {
|
72 |
|
73 | this.firstPlay = name;
|
74 | this.firstPlayLoop = loop;
|
75 | return;
|
76 | }
|
77 | var snd = this.assets.get(name);
|
78 | if (snd === undefined) {
|
79 | console.error("Unknown sound: " + name);
|
80 | }
|
81 | var source = this.context.createBufferSource();
|
82 | source.buffer = snd;
|
83 | source.connect(this.gainNode);
|
84 | if (loop) {
|
85 | source.loop = true;
|
86 | source.loopStart = loop.loopStart || 0;
|
87 | source.loopEnd = loop.loopEnd || 0;
|
88 | this.looping[name] = source;
|
89 | }
|
90 | source.start(0);
|
91 | };
|
92 |
|
93 |
|
94 |
|
95 |
|
96 | SoundManager.prototype.stop = function(name) {
|
97 | if (!this.looping[name]) {
|
98 | return;
|
99 | }
|
100 | this.looping[name].stop(0);
|
101 | delete this.looping[name];
|
102 | };
|
103 |
|
104 |
|
105 |
|
106 | SoundManager.prototype.mute = function() {
|
107 | this.gainNode.gain.value = 0;
|
108 | this.muted = true;
|
109 | };
|
110 |
|
111 |
|
112 |
|
113 | SoundManager.prototype.unmute = function() {
|
114 | this.gainNode.gain.value = this.volume;
|
115 | this.muted = false;
|
116 | };
|
117 |
|
118 |
|
119 |
|
120 |
|
121 | SoundManager.prototype.setVolume = function(gain) {
|
122 | this.volume = gain;
|
123 | this.gainNode.gain.value = gain;
|
124 | this.muted = false;
|
125 | };
|
126 |
|
127 |
|
128 |
|
129 |
|
130 | SoundManager.prototype.isMuted = function() {
|
131 | return this.muted;
|
132 | };
|
133 |
|
134 |
|
135 | function FakeSoundManager() {}
|
136 | FakeSoundManager.prototype.play = function() {};
|
137 | FakeSoundManager.prototype.stop = function() {};
|
138 | FakeSoundManager.prototype.mute = function() {};
|
139 | FakeSoundManager.prototype.unmute = function() {};
|
140 | FakeSoundManager.prototype.setVolume = function() {};
|
141 | FakeSoundManager.prototype.isMuted = function() {
|
142 | return true;
|
143 | };
|
144 |
|
145 | if (window.AudioContext) {
|
146 | module.exports = SoundManager;
|
147 | } else {
|
148 | console.warn("This browser doesn't support the Web Audio API.");
|
149 | module.exports = FakeSoundManager;
|
150 | }
|