trim extra debugging code

This commit is contained in:
humbletim 2017-01-17 01:23:03 -05:00
parent 827aa68d3f
commit c7a561aff9

View file

@ -22,19 +22,15 @@
//
(function(){
var VERSION = "0.0.1";
// This sample clip and range will be used if you don't add userData to the entity (see above)
var DEFAULT_RANGE = 100;
var DEFAULT_URL = "http://hifi-content.s3.amazonaws.com/ken/samples/forest_ambiX.wav";
var DEFAULT_VOLUME = 1.0;
var soundURL = "";
var soundName = "";
var startTime;
var soundOptions = {
loop: true,
localOnly: true,
//ignorePenumbra: true,
};
var range = DEFAULT_RANGE;
var maxVolume = DEFAULT_VOLUME;
@ -59,12 +55,6 @@
}
}
var WANT_DEBUG_OVERLAY = false;
var LINEHEIGHT = 0.1;
// Optionally enable debug overlays using a Settings value
WANT_DEBUG_OVERLAY = WANT_DEBUG_OVERLAY || /ambientSound/.test(Settings.getValue("WANT_DEBUG_OVERLAY"));
var WANT_DEBUG_BROADCASTS = WANT_DEBUG_OVERLAY && "ambientSound.js";
this.updateSettings = function() {
// Check user data on the entity for any changes
var oldSoundURL = soundURL;
@ -73,7 +63,6 @@
var data = JSON.parse(props.userData);
if (data.soundURL && !(soundURL === data.soundURL)) {
soundURL = data.soundURL;
soundName = (soundURL||"").split("/").pop(); // just filename part
debugPrint("Read ambient sound URL: " + soundURL);
}
if (data.range && !(range === data.range)) {
@ -111,13 +100,6 @@
debugPrint("Failed to download ambient sound: " + soundURL);
debugState("error");
}
debugPrint("onStateChanged: " + JSON.stringify({
sound: soundName,
state: resource.state,
stateName: Object.keys(Resource.State).filter(function(key) {
return Resource.State[key] === resource.state;
})
}));
}
resource.stateChanged.connect(onStateChanged);
onStateChanged(resource.state);
@ -149,13 +131,9 @@
var data = JSON.parse(props.userData);
data.disabled = !data.disabled;
debugPrint(hint + " -- triggering ambient sound " + (data.disabled ? "OFF" : "ON") + " (" + soundName + ")");
debugPrint(hint + " -- triggering ambient sound " + (data.disabled ? "OFF" : "ON") + " (" + soundURL + ")");
var oldState = _debugState;
if (WANT_DEBUG_BROADCASTS) {
Messages.sendMessage(WANT_DEBUG_BROADCASTS, JSON.stringify({ palName: MyAvatar.sessionDisplayName, soundName: soundName, hint: hint, scriptTimestamp: props.scriptTimestamp, oldState: oldState, newState: _debugState, age: props.age }));
}
this.cleanup();
// Save the userData and bump scriptTimestamp, which causes all nearby listeners to apply the state change
@ -182,14 +160,10 @@
this.preload = function(entityID) {
// Load the sound and range from the entity userData fields, and note the position of the entity.
debugPrint("Ambient sound preload " + VERSION);
debugPrint("Ambient sound preload");
entity = entityID;
_this = this;
if (WANT_DEBUG_OVERLAY) {
_createDebugOverlays();
}
var props = Entities.getEntityProperties(entity, [ "userData" ]);
if (props.userData) {
var data = JSON.parse(props.userData);
@ -209,12 +183,7 @@
var HYSTERESIS_FRACTION = 0.1;
var props = Entities.getEntityProperties(entity, [ "position", "rotation" ]);
if (!props.position) {
// FIXME: this mysterious state has been happening while testing
// and might indicate a bug where an Entity can become unreachable without `unload` having been called..
print("FIXME: ambientSound.js -- expected Entity unavailable!")
if (WANT_DEBUG_BROADCASTS) {
Messages.sendMessage(WANT_DEBUG_BROADCASTS, JSON.stringify({ palName: MyAvatar.sessionDisplayName, soundName: soundName, hint: "FIXME: maybeUpdate", oldState: _debugState }));
}
return _this.cleanup();
}
center = props.position;
@ -225,7 +194,7 @@
soundOptions.orientation = rotation;
soundOptions.volume = volume;
if (!soundPlaying && ambientSound && ambientSound.downloaded) {
debugState("playing", "Starting ambient sound: " + soundName + " (duration: " + ambientSound.duration + ")");
debugState("playing", "Starting ambient sound: " + soundURL + " (duration: " + ambientSound.duration + ")");
soundPlaying = Audio.playSound(ambientSound, soundOptions);
} else if (soundPlaying && soundPlaying.playing) {
soundPlaying.setOptions(soundOptions);
@ -233,22 +202,12 @@
} else if (soundPlaying && soundPlaying.playing && (distance > range * HYSTERESIS_FRACTION)) {
soundPlaying.stop();
soundPlaying = false;
debugState("idle", "Out of range, stopping ambient sound: " + soundName);
}
if (WANT_DEBUG_OVERLAY) {
updateDebugOverlay(distance);
debugState("idle", "Out of range, stopping ambient sound: " + soundURL);
}
}
this.unload = function(entityID) {
debugPrint("Ambient sound unload ");
if (WANT_DEBUG_BROADCASTS) {
var offset = ambientSound && (new Date - startTime)/1000 % ambientSound.duration;
Messages.sendMessage(WANT_DEBUG_BROADCASTS, JSON.stringify({ palName: MyAvatar.sessionDisplayName, soundName: soundName, hint: "unload", oldState: _debugState, offset: offset }));
}
if (WANT_DEBUG_OVERLAY) {
_removeDebugOverlays();
}
this.cleanup();
};
@ -263,105 +222,4 @@
}
};
// Visual debugging overlay (to see set WANT_DEBUG_OVERLAY = true)
var DEBUG_COLORS = {
//preload: { red: 0, green: 80, blue: 80 },
disabled: { red: 0, green: 0, blue: 0, alpha: 0.0 },
downloading: { red: 255, green: 255, blue: 0 },
error: { red: 255, green: 0, blue: 0 },
playing: { red: 0, green: 200, blue: 0 },
idle: { red: 0, green: 100, blue: 0 }
};
var _debugOverlay;
var _debugState = "";
function debugState(state, message) {
if (state === "playing") {
startTime = new Date;
}
_debugState = state;
if (message) {
debugPrint(message);
}
updateDebugOverlay();
if (WANT_DEBUG_BROADCASTS) {
// Broadcast state changes to make multi-user scenarios easier to verify from a single console
Messages.sendMessage(WANT_DEBUG_BROADCASTS, JSON.stringify({ palName: MyAvatar.sessionDisplayName, soundName: soundName, state: state }));
}
}
function updateDebugOverlay(distance) {
var props = Entities.getEntityProperties(entity, [ "name", "dimensions" ]);
if (!props.dimensions) {
return print("ambientSound.js: updateDebugOverlay -- entity no longer available " + entity);
}
var options = soundPlaying && soundPlaying.options;
if (options) {
var offset = soundPlaying.playing && ambientSound && (new Date - startTime)/1000 % ambientSound.duration;
var deg = Quat.safeEulerAngles(options.orientation);
var orientation = [ deg.x, deg.y, deg.z].map(Math.round).join(", ");
var volume = options.volume;
}
var info = {
//loudness: soundPlaying.loudness && soundPlaying.loudness.toFixed(4) || undefined,
offset: offset && ("00"+offset.toFixed(1)).substr(-4)+"s" || undefined,
orientation: orientation,
injector: soundPlaying && soundPlaying.playing && "playing",
resource: ambientSound && ambientSound.downloaded && "ready (" + ambientSound.duration.toFixed(1) + "s)",
name: props.name || undefined,
uuid: entity.split(/\W/)[1], // extracts just the first part of the UUID
sound: soundName,
volume: Math.max(0,volume||0).toFixed(2) + " / " + maxVolume.toFixed(2),
distance: (distance||0).toFixed(1) + "m / " + range.toFixed(1) + "m",
state: _debugState.toUpperCase(),
};
// Pretty print key/value pairs, excluding any undefined values
var outputText = Object.keys(info).filter(function(key) {
return info[key] !== undefined;
}).map(function(key) {
return key + ": " + info[key];
}).join("\n");
// Calculate a local position for displaying info just above the Entity
var textSize = Overlays.textSize(_debugOverlay, outputText);
var size = {
x: textSize.width + LINEHEIGHT,
y: textSize.height + LINEHEIGHT
};
var pos = { x: 0, y: props.dimensions.y + size.y/2, z: 0 };
var backgroundColor = DEBUG_COLORS[_debugState];
var backgroundAlpha = backgroundColor ? backgroundColor.alpha : 0.6;
Overlays.editOverlay(_debugOverlay, {
visible: true,
backgroundColor: backgroundColor,
backgroundAlpha: backgroundAlpha,
text: outputText,
localPosition: pos,
size: size,
});
}
function _removeDebugOverlays() {
if (_debugOverlay) {
Overlays.deleteOverlay(_debugOverlay);
_debugOverlay = 0;
}
}
function _createDebugOverlays() {
_debugOverlay = Overlays.addOverlay("text3d", {
visible: true,
lineHeight: LINEHEIGHT,
leftMargin: LINEHEIGHT/2,
topMargin: LINEHEIGHT/2,
localPosition: Vec3.ZERO,
parentID: entity,
ignoreRayIntersection: true,
isFacingAvatar: true,
textAlpha: 0.6,
//drawInFront: true,
});
}
})