Addressed PR feedback - now dynamically reads userdata
This commit is contained in:
parent
70942925bc
commit
e2dfbf1b02
@ -3,13 +3,12 @@
|
|||||||
// This entity script will allow you to create an ambient sound that loops when a person is within a given
|
// This entity script will allow you to create an ambient sound that loops when a person is within a given
|
||||||
// range of this entity. Great way to add one or more ambisonic soundfields to your environment.
|
// range of this entity. Great way to add one or more ambisonic soundfields to your environment.
|
||||||
//
|
//
|
||||||
// In the userData section for the entity, add two values:
|
// In the userData section for the entity, add/edit three values:
|
||||||
// userData.SoundURL should be a string giving the URL to the sound file. Defaults to 100 meters if not set.
|
// userData.SoundURL should be a string giving the URL to the sound file. Defaults to 100 meters if not set.
|
||||||
// userData.range should be an integer for the max distance away from the entity where the sound will be audible.
|
// userData.range should be an integer for the max distance away from the entity where the sound will be audible.
|
||||||
// userData.volume is the max volume at which the clip should play.
|
// userData.volume is the max volume at which the clip should play. Defaults to 1.0 full volume)
|
||||||
//
|
//
|
||||||
// Note: When you update the above values, you need to reload the entity script for it to see the changes. Also,
|
// Remember that the entity has to be visible to the user for the sound to play at all, so make sure the entity is
|
||||||
// remember that the entity has to be visible to the user for the sound to play at all, so make sure the entity is
|
|
||||||
// large enough to be loaded at the range you set, particularly for large ranges.
|
// large enough to be loaded at the range you set, particularly for large ranges.
|
||||||
//
|
//
|
||||||
// Copyright 2016 High Fidelity, Inc.
|
// Copyright 2016 High Fidelity, Inc.
|
||||||
@ -24,7 +23,7 @@
|
|||||||
var DEFAULT_URL = "http://hifi-content.s3.amazonaws.com/ken/samples/forest_ambiX.wav";
|
var DEFAULT_URL = "http://hifi-content.s3.amazonaws.com/ken/samples/forest_ambiX.wav";
|
||||||
var DEFAULT_VOLUME = 1.0;
|
var DEFAULT_VOLUME = 1.0;
|
||||||
|
|
||||||
var soundURL = DEFAULT_URL;
|
var soundURL = "";
|
||||||
var range = DEFAULT_RANGE;
|
var range = DEFAULT_RANGE;
|
||||||
var maxVolume = DEFAULT_VOLUME;
|
var maxVolume = DEFAULT_VOLUME;
|
||||||
var UPDATE_INTERVAL_MSECS = 100;
|
var UPDATE_INTERVAL_MSECS = 100;
|
||||||
@ -33,40 +32,61 @@
|
|||||||
var ambientSound;
|
var ambientSound;
|
||||||
var center;
|
var center;
|
||||||
var soundPlaying = false;
|
var soundPlaying = false;
|
||||||
var updateTimer = false;
|
var checkTimer = false;
|
||||||
|
var _this;
|
||||||
var WANT_DEBUG = false;
|
|
||||||
|
|
||||||
|
var WANT_DEBUG = true;
|
||||||
function debugPrint(string) {
|
function debugPrint(string) {
|
||||||
if (WANT_DEBUG) {
|
if (WANT_DEBUG) {
|
||||||
print(string);
|
print(string);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.updateSettings = function() {
|
||||||
|
// Check user data on the entity for any changes
|
||||||
|
var oldSoundURL = soundURL;
|
||||||
|
var props = Entities.getEntityProperties(entity, [ "userData" ]);
|
||||||
|
if (props.userData) {
|
||||||
|
var data = JSON.parse(props.userData);
|
||||||
|
if (data.soundURL && !(soundURL === data.soundURL)) {
|
||||||
|
soundURL = data.soundURL;
|
||||||
|
debugPrint("Read ambient sound URL: " + soundURL);
|
||||||
|
} else if (!data.soundURL) {
|
||||||
|
soundURL = DEFAULT_URL;
|
||||||
|
}
|
||||||
|
if (data.range && !(range === data.range)) {
|
||||||
|
range = data.range;
|
||||||
|
debugPrint("Read ambient sound range: " + range);
|
||||||
|
}
|
||||||
|
if (data.volume && !(maxVolume === data.volume)) {
|
||||||
|
maxVolume = data.volume;
|
||||||
|
debugPrint("Read ambient sound volume: " + maxVolume);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!(soundURL === oldSoundURL) || (soundURL === "")) {
|
||||||
|
debugPrint("Loading ambient sound into cache");
|
||||||
|
ambientSound = SoundCache.getSound(soundURL);
|
||||||
|
if (soundPlaying && soundPlaying.playing) {
|
||||||
|
soundPlaying.stop();
|
||||||
|
soundPlaying = false;
|
||||||
|
Entities.editEntity(entity, { color: { red: 128, green: 128, blue: 128 }});
|
||||||
|
debugPrint("Restarting ambient sound");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
this.preload = function(entityID) {
|
this.preload = function(entityID) {
|
||||||
// Load the sound and range from the entity userData fields, and note the position of the entity.
|
// Load the sound and range from the entity userData fields, and note the position of the entity.
|
||||||
debugPrint("Ambient sound preload");
|
debugPrint("Ambient sound preload");
|
||||||
entity = entityID;
|
entity = entityID;
|
||||||
var props = Entities.getEntityProperties(entityID, [ "userData" ]);
|
_this = this;
|
||||||
var data = JSON.parse(props.userData);
|
checkTimer = Script.setInterval(this.maybeUpdate, UPDATE_INTERVAL_MSECS);
|
||||||
if (data.soundURL) {
|
|
||||||
soundURL = data.soundURL;
|
|
||||||
debugPrint("Read ambient sound URL: " + soundURL);
|
|
||||||
}
|
|
||||||
ambientSound = SoundCache.getSound(soundURL);
|
|
||||||
if (data.range) {
|
|
||||||
range = data.range;
|
|
||||||
debugPrint("Read ambient sound range: " + range);
|
|
||||||
}
|
|
||||||
if (data.volume) {
|
|
||||||
maxVolume = data.volume;
|
|
||||||
debugPrint("Read ambient sound volume: " + maxVolume);
|
|
||||||
}
|
|
||||||
updateTimer = Script.setInterval(this.update, UPDATE_INTERVAL_MSECS);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
this.update = function() {
|
this.maybeUpdate = function() {
|
||||||
// Every UPDATE_INTERVAL_MSECS, update the volume of the ambient sound based on distance from my avatar
|
// Every UPDATE_INTERVAL_MSECS, update the volume of the ambient sound based on distance from my avatar
|
||||||
|
_this.updateSettings();
|
||||||
|
var props = Entities.getEntityProperties(entity);
|
||||||
var HYSTERESIS_FRACTION = 0.1;
|
var HYSTERESIS_FRACTION = 0.1;
|
||||||
var props = Entities.getEntityProperties(entity, [ "position" ]);
|
var props = Entities.getEntityProperties(entity, [ "position" ]);
|
||||||
center = props.position;
|
center = props.position;
|
||||||
@ -74,7 +94,7 @@
|
|||||||
if (distance <= range) {
|
if (distance <= range) {
|
||||||
var volume = (1.0 - distance / range) * maxVolume;
|
var volume = (1.0 - distance / range) * maxVolume;
|
||||||
if (!soundPlaying && ambientSound.downloaded) {
|
if (!soundPlaying && ambientSound.downloaded) {
|
||||||
soundPlaying = Audio.playSound(ambientSound, { loop: true, volume: volume });
|
soundPlaying = Audio.playSound(ambientSound, { loop: true, localOnly: true, volume: volume });
|
||||||
debugPrint("Starting ambient sound, volume: " + volume);
|
debugPrint("Starting ambient sound, volume: " + volume);
|
||||||
Entities.editEntity(entity, { color: { red: 255, green: 0, blue: 0 }});
|
Entities.editEntity(entity, { color: { red: 255, green: 0, blue: 0 }});
|
||||||
} else if (soundPlaying && soundPlaying.playing) {
|
} else if (soundPlaying && soundPlaying.playing) {
|
||||||
@ -90,12 +110,12 @@
|
|||||||
|
|
||||||
this.unload = function(entityID) {
|
this.unload = function(entityID) {
|
||||||
debugPrint("Ambient sound unload");
|
debugPrint("Ambient sound unload");
|
||||||
if (updateTimer) {
|
if (checkTimer) {
|
||||||
Script.clearInterval(updateTimer);
|
Script.clearInterval(checkTimer);
|
||||||
}
|
}
|
||||||
if (soundPlaying && soundPlaying.playing) {
|
if (soundPlaying && soundPlaying.playing) {
|
||||||
soundPlaying.stop();
|
soundPlaying.stop();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
})
|
})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user