content/hifi-content/caitlyn/production/soundEmitter/soundLoopEmitter_2.js
2022-02-13 22:19:19 +01:00

85 lines
2.9 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 soundFollowsPlayer = null;
var refreshInterval = null;
var stopDistance = null; // how far away from the listener until it stops the sound?
var soundData = null;
var injector = null;
var entityID = null;
var properties = null;
var playPos = null;
var playerEmitterDistance = null;
var stopSound = false;
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;
soundFollowsPlayer = soundData.isFollowPlayer;
soundLocal = soundData.isLocal;
playPos = soundFollowsPlayer ? MyAvatar.position : properties.position;
playerEmitterDistance = Vec3.distance(MyAvatar.position, properties.position);
refreshInterval = !isNaN(soundData.refreshInterval) ? Number(soundData.refreshInterval) : 5000.0;
refreshInterval = Math.min(1000, Math.max(refreshInterval, 10)); // cap updates at min 10 ms
if (injector && soundFollowsPlayer && (playerEmitterDistance>stopDistance)) {
injector.stop();
injector = null;
stopSound = true;
refreshInterval = 5000;
} else stopSound = false;
} catch (e){}
if (!stopSound){
if (!injector) {
if (soundURL.downloaded) {
injector = Audio.playSound(soundURL, {
position: playPos,
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: playPos,
volume: soundVolume
});
}
}
}
}, refreshInterval);
};
this.unload = function(){
if (injector) {
injector.stop();
injector = null;
}
};
});