-
Notifications
You must be signed in to change notification settings - Fork 0
/
script-soundEmitter.js
70 lines (65 loc) · 2.24 KB
/
script-soundEmitter.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
(function(){
//var SOUND_URL = "http://hifi-content.s3.amazonaws.com/caitlyn/production/pizzaTurntable/newBuiltMixl.wav";
var loopTime = -1; // Loop for how long? -1 is always on.
var soundURL = null;//SoundCache.getSound(SOUND_URL);
var lastSoundURL = null;
var receiverName = "";
var soundLoop = null;
var soundLocal = null;
var soundVolume = null;
var refreshInterval = 500;
var soundData = null;
var injector = null;
var entityID = null;
var properties = null;
this.preload = function(pEntityID) {
entityID = pEntityID;
var intervalID = Script.setInterval(function() {
properties = Entities.getEntityProperties(entityID, ["position", "userData"]);
if (!properties.userData) {
print("Sound emitter "+entityID+" missing user data.");
return;
} try {
soundData = JSON.parse(properties.userData);
// print("SoundURL "+soundData.soundURL+", lastSoundURL"+lastSoundURL);
// need to check that all this stuff even exists and throw error if not.s
soundURL = SoundCache.getSound(soundData.soundURL);
receiverName = soundData.receiverName;
soundVolume = !isNaN(soundData.soundVolume) ? Number(soundData.soundVolume) : 0.0;
soundLoop = soundData.isLoop;
soundLocal = soundData.isLocal;
refreshInterval = !isNaN(soundData.refreshInterval) ? Number(soundData.refreshInterval) : 1000.0;
//refreshInterval = soundData.refreshInterval;
refreshInterval = Math.min(1000, Math.max(refreshInterval, 10)); // cap updates at min 10 ms
} catch (e){}
if (!injector) {
if (soundURL.downloaded) {
injector = Audio.playSound(soundURL, {
position: properties.position,
volume: soundVolume,
loop: true,//soundLoop,
localOnly: true//;//soundLocal
});
}
lastSoundURL = soundData.soundURL;
} else {
if (lastSoundURL != soundData.soundURL) {
injector.stop();
injector = null;
soundURL = SoundCache.getSound(lastSoundURL);
} else {
injector.setOptions({
position: properties.position,
volume: soundVolume
});
}
}
}, refreshInterval);
};
this.unload = function(){
if (injector) {
injector.stop();
injector = null;
}
};
});