content/hifi-content/caitlyn/dev/Scripts/soundLoopEmitterWithGlobal.js
2022-02-13 22:19:19 +01:00

84 lines
2.7 KiB
JavaScript

(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 soundNonPositional = 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", "rotation", "userData"]);
if (!properties.userData) {
print("Sound emitter "+entityID+" missing user data.");
return;
} try {
soundData = JSON.parse(properties.userData);
soundURL = SoundCache.getSound(soundData.soundURL);
receiverName = soundData.receiverName;
soundVolume = !isNaN(soundData.soundVolume) ? Number(soundData.soundVolume) : 0.0;
soundLoop = soundData.isLoop;
soundLocal = soundData.isLocal;
soundNonPositional = soundData.nonPositional;
refreshInterval = !isNaN(soundData.refreshInterval) ? Number(soundData.refreshInterval) : 1000.0;
refreshInterval = Math.min(1000, Math.max(refreshInterval, 10)); // cap updates at min 10 ms
} catch (e){}
var injectorOptions = null;
if (!injector) {
if (soundURL.downloaded) {
if (soundNonPositional) injectorOptions = {
position: Camera.position,
rotation: Camera.rotation,
volume: soundVolume,
loop: true,
localOnly: true
}; else injectorOptions = {
position: properties.position,
rotation: properties.rotation,
volume: soundVolume,
loop: true,
localOnly: true
};
injector = Audio.playSound(soundURL, injectorOptions);
}
lastSoundURL = soundData.soundURL;
} else {
if (lastSoundURL != soundData.soundURL) {
injector.stop();
injector = null;
soundURL = SoundCache.getSound(lastSoundURL);
} else {
if (soundNonPositional) injectorOptions = {
position: Camera.position,
rotation: Camera.rotation,
volume: soundVolume
}; else injectorOptions = {
position: properties.position,
rotation: properties.rotation,
volume: soundVolume
};
injector.setOptions(injectorOptions);
}
}
}, refreshInterval);
};
this.unload = function(){
if (injector) {
injector.stop();
injector = null;
}
};
});