(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); 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; } }; });