forked from playcanvas/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomponent.js
More file actions
355 lines (310 loc) · 13.8 KB
/
component.js
File metadata and controls
355 lines (310 loc) · 13.8 KB
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
pc.extend(pc, function () {
/**
* @component
* @name pc.AudioSourceComponent
* @class The AudioSource Component controls playback of an audio sample. This class will be deprecated in favor of {@link pc.SoundComponent}.
* @description Create a new AudioSource Component
* @param {pc.AudioSourceComponentSystem} system The ComponentSystem that created this Component
* @param {pc.Entity} entity The entity that the Component is attached to
* @extends pc.Component
* @property {pc.Asset[]} assets The list of audio assets - can also be an array of asset ids.
* @property {Boolean} activate If true the audio will begin playing as soon as the Pack is loaded
* @property {Number} volume The volume modifier to play the audio with. In range 0-1.
* @property {Number} pitch The pitch modifier to play the audio with. Must be larger than 0.01
* @property {Boolean} loop If true the audio will restart when it finishes playing
* @property {Boolean} 3d If true the audio will play back at the location of the Entity in space, so the audio will be affect by the position of the {@link pc.AudioListenerComponent}
* @property {String} distanceModel Determines which algorithm to use to reduce the volume of the audio as it moves away from the listener. Can be one of 'linear', 'inverse' or 'exponential'. Default is 'inverse'.
* @property {Number} minDistance The minimum distance from the listener at which audio falloff begins.
* @property {Number} maxDistance The maximum distance from the listener at which audio falloff stops. Note the volume of the audio is not 0 after this distance, but just doesn't fall off anymore
* @property {Number} rollOffFactor The factor used in the falloff equation.
*/
var AudioSourceComponent = function (system, entity) {
this.on("set_assets", this.onSetAssets, this);
this.on("set_loop", this.onSetLoop, this);
this.on("set_volume", this.onSetVolume, this);
this.on("set_pitch", this.onSetPitch, this);
this.on("set_minDistance", this.onSetMinDistance, this);
this.on("set_maxDistance", this.onSetMaxDistance, this);
this.on("set_rollOffFactor", this.onSetRollOffFactor, this);
this.on("set_distanceModel", this.onSetDistanceModel, this);
this.on("set_3d", this.onSet3d, this);
};
AudioSourceComponent = pc.inherits(AudioSourceComponent, pc.Component);
pc.extend(AudioSourceComponent.prototype, {
/**
* @function
* @name pc.AudioSourceComponent#play
* @description Begin playback of an audio asset in the component attached to an entity
* @param {String} name The name of the Asset to play
*/
play: function(name) {
if (!this.enabled || !this.entity.enabled) {
return;
}
if (this.channel) {
// If we are currently playing a channel, stop it.
this.stop();
}
var channel;
var componentData = this.data;
if(componentData.sources[name]) {
if (!componentData['3d']) {
channel = this.system.manager.playSound(componentData.sources[name], componentData);
componentData.currentSource = name;
componentData.channel = channel;
} else {
var pos = this.entity.getPosition();
channel = this.system.manager.playSound3d(componentData.sources[name], pos, componentData);
componentData.currentSource = name;
componentData.channel = channel;
}
}
},
/**
* @function
* @name pc.AudioSourceComponent#pause
* @description Pause playback of the audio that is playing on the Entity. Playback can be resumed by calling {@link pc.AudioSourceComponent#unpause}
*/
pause: function() {
if (this.channel) {
this.channel.pause();
}
},
/**
* @function
* @name pc.AudioSourceComponent#unpause
* @description Resume playback of the audio if paused. Playback is resumed at the time it was paused.
*/
unpause: function () {
if (this.channel && this.channel.paused) {
this.channel.unpause();
}
},
/**
* @function
* @name pc.AudioSourceComponent#stop
* @description Stop playback on an Entity. Playback can not be resumed after being stopped.
*/
stop: function() {
if(this.channel) {
this.channel.stop();
this.channel = null;
}
},
onSetAssets: function (name, oldValue, newValue) {
var newAssets = [];
var i, len = newValue.length;
if (oldValue && oldValue.length) {
for (i = 0; i < oldValue.length; i++) {
// unsubscribe from change event for old assets
if (oldValue[i]) {
var asset = this.system.app.assets.get(oldValue[i]);
if (asset) {
asset.off('change', this.onAssetChanged, this);
asset.off('remove', this.onAssetRemoved, this);
if (this.currentSource === asset.name) {
this.stop();
}
}
}
}
}
if (len) {
for (i = 0; i < len; i++) {
if (oldValue.indexOf(newValue[i]) < 0) {
if (newValue[i] instanceof pc.Asset) {
newAssets.push(newValue[i].id);
} else {
newAssets.push(newValue[i]);
}
}
}
}
if(!this.system._inTools && newAssets.length) { // Only load audio data if we are not in the tools and if changes have been made
this.loadAudioSourceAssets(newAssets);
}
},
onAssetChanged: function (asset, attribute, newValue, oldValue) {
if (attribute === 'resource') {
var sources = this.data.sources;
if (sources) {
this.data.sources[asset.name] = newValue;
if (this.data.currentSource === asset.name) {
// replace current sound if necessary
if (this.channel) {
if (this.channel.paused) {
this.play(asset.name);
this.pause();
} else {
this.play(asset.name);
}
}
}
}
}
},
onAssetRemoved: function (asset) {
asset.off('remove', this.onAssetRemoved, this);
if (this.data.sources[asset.name]) {
delete this.data.sources[asset.name];
if (this.data.currentSource === asset.name) {
this.stop();
this.data.currentSource = null;
}
}
},
onSetLoop: function (name, oldValue, newValue) {
if (oldValue != newValue) {
if (this.channel) {
this.channel.setLoop(newValue);
}
}
},
onSetVolume: function (name, oldValue, newValue) {
if (oldValue != newValue) {
if (this.channel) {
this.channel.setVolume(newValue);
}
}
},
onSetPitch: function (name, oldValue, newValue) {
if (oldValue != newValue) {
if (this.channel) {
this.channel.setPitch(newValue);
}
}
},
onSetMaxDistance: function (name, oldValue, newValue) {
if (oldValue != newValue) {
if (this.channel instanceof pc.Channel3d) {
this.channel.setMaxDistance(newValue);
}
}
},
onSetMinDistance: function (name, oldValue, newValue) {
if (oldValue != newValue) {
if (this.channel instanceof pc.Channel3d) {
this.channel.setMinDistance(newValue);
}
}
},
onSetRollOffFactor: function (name, oldValue, newValue) {
if (oldValue != newValue) {
if (this.channel instanceof pc.Channel3d) {
this.channel.setRollOffFactor(newValue);
}
}
},
onSetDistanceModel: function (name, oldValue, newValue) {
if (oldValue !== newValue) {
if (this.channel instanceof pc.Channel3d) {
this.channel.setDistanceModel(newValue);
}
}
},
onSet3d: function (name, oldValue, newValue) {
if (oldValue !== newValue) {
if (this.system.initialized && this.currentSource) {
var paused = false;
var suspended = false;
if (this.channel) {
paused = this.channel.paused;
suspended = this.channel.suspended;
}
this.play(this.currentSource);
if (this.channel) {
this.channel.paused = paused;
this.channel.suspended = suspended;
}
}
}
},
onEnable: function () {
AudioSourceComponent._super.onEnable.call(this);
// load assets that haven't been loaded yet
var assets = this.data.assets;
if (assets) {
var registry = this.system.app.assets;
for (var i = 0, len = assets.length; i < len; i++) {
var asset = assets[i];
if (! (asset instanceof pc.Asset))
asset = registry.get(asset);
if (asset && !asset.resource) {
registry.load(asset);
}
}
}
if (this.system.initialized) {
if (this.data.activate && !this.channel) {
this.play(this.currentSource);
} else {
this.unpause();
}
}
},
onDisable: function () {
AudioSourceComponent._super.onDisable.call(this);
this.pause();
},
loadAudioSourceAssets: function (ids) {
var self = this;
var assets = ids.map(function (id) {
return this.system.app.assets.get(id);
}, this);
var sources = {};
var currentSource = null;
var count = assets.length;
// make sure progress continues even if some audio doesn't load
var _error = function (e) {
count--;
};
// once all assets are accounted for continue
var _done = function () {
this.data.sources = sources;
this.data.currentSource = currentSource;
if (this.enabled && this.activate && currentSource) {
this.onEnable();
}
}.bind(this);
assets.forEach(function (asset, index) {
if (asset) {
// set the current source to the first entry (before calling set, so that it can play if needed)
currentSource = currentSource || asset.name;
// subscribe to change events to reload sounds if necessary
asset.off('change', this.onAssetChanged, this);
asset.on('change', this.onAssetChanged, this);
asset.off('remove', this.onAssetRemoved, this);
asset.on('remove', this.onAssetRemoved, this);
asset.off('error', _error, this);
asset.on('error', _error, this);
asset.ready(function(asset) {
sources[asset.name] = asset.resource;
count--;
if (count === 0) {
_done();
}
});
if (! asset.resource && self.enabled && self.entity.enabled)
this.system.app.assets.load(asset);
} else {
// don't wait for assets that aren't in the registry
count--;
if (count === 0) {
_done();
}
// but if they are added insert them into source list
this.system.app.assets.on("add:" + ids[index], function (asset) {
asset.ready(function (asset) {
self.data.sources[asset.name] = asset.resource;
});
if (! asset.resource)
self.system.app.assets.load(asset);
});
}
}, this);
}
});
return {
AudioSourceComponent: AudioSourceComponent
};
}());