From cf3a260646c64f3a05e88d6e550597afe705dfe8 Mon Sep 17 00:00:00 2001 From: Philip Rosedale Date: Wed, 21 Dec 2016 13:13:59 -0800 Subject: [PATCH 1/3] added ability to rotate entity and sound direction --- .../tutorials/entity_scripts/ambientSound.js | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/scripts/tutorials/entity_scripts/ambientSound.js b/scripts/tutorials/entity_scripts/ambientSound.js index 6a892ed139..f3eb93c76f 100644 --- a/scripts/tutorials/entity_scripts/ambientSound.js +++ b/scripts/tutorials/entity_scripts/ambientSound.js @@ -8,6 +8,9 @@ // 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. Defaults to 1.0 full volume) // +// The rotation of the entity is copied to the ambisonic field, so by rotating the entity you will rotate the +// direction in-which a certain sound comes from. +// // 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. // @@ -27,6 +30,7 @@ var range = DEFAULT_RANGE; var maxVolume = DEFAULT_VOLUME; var UPDATE_INTERVAL_MSECS = 100; + var rotation; var entity; var ambientSound; @@ -92,23 +96,28 @@ this.maybeUpdate = function() { // 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 props = Entities.getEntityProperties(entity, [ "position" ]); + var props = Entities.getEntityProperties(entity, [ "position", "rotation" ]); center = props.position; + rotation = props.rotation; var distance = Vec3.length(Vec3.subtract(MyAvatar.position, center)); if (distance <= range) { var volume = (1.0 - distance / range) * maxVolume; if (!soundPlaying && ambientSound.downloaded) { - soundPlaying = Audio.playSound(ambientSound, { loop: true, localOnly: true, volume: volume }); + soundPlaying = Audio.playSound(ambientSound, { loop: true, + localOnly: true, + rotation: rotation, + volume: volume }); debugPrint("Starting ambient sound, volume: " + volume); if (WANT_COLOR_CHANGE) { Entities.editEntity(entity, { color: COLOR_ON }); } } else if (soundPlaying && soundPlaying.playing) { - soundPlaying.setOptions( { volume: volume } ); + debugPrint("Setting volume and rotation: " + Quat.safeEulerAngles(rotation).y); + soundPlaying.setOptions( { volume: volume, orientation: rotation } ); } + } else if (soundPlaying && soundPlaying.playing && (distance > range * HYSTERESIS_FRACTION)) { soundPlaying.stop(); soundPlaying = false; From 632bf997f39b42c527e5ef09a666a7311b5b0da2 Mon Sep 17 00:00:00 2001 From: Philip Rosedale Date: Wed, 21 Dec 2016 13:15:44 -0800 Subject: [PATCH 2/3] debug off --- scripts/tutorials/entity_scripts/ambientSound.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/scripts/tutorials/entity_scripts/ambientSound.js b/scripts/tutorials/entity_scripts/ambientSound.js index f3eb93c76f..a8e7db239c 100644 --- a/scripts/tutorials/entity_scripts/ambientSound.js +++ b/scripts/tutorials/entity_scripts/ambientSound.js @@ -39,11 +39,11 @@ var checkTimer = false; var _this; - var WANT_COLOR_CHANGE = true; + var WANT_COLOR_CHANGE = false; var COLOR_OFF = { red: 128, green: 128, blue: 128 }; var COLOR_ON = { red: 255, green: 0, blue: 0 }; - var WANT_DEBUG = true; + var WANT_DEBUG = false; function debugPrint(string) { if (WANT_DEBUG) { print(string); @@ -114,7 +114,6 @@ } } else if (soundPlaying && soundPlaying.playing) { - debugPrint("Setting volume and rotation: " + Quat.safeEulerAngles(rotation).y); soundPlaying.setOptions( { volume: volume, orientation: rotation } ); } From b02d14fcc2f899fbb8ce5036b85b22f2affed5ae Mon Sep 17 00:00:00 2001 From: Philip Rosedale Date: Wed, 21 Dec 2016 13:17:08 -0800 Subject: [PATCH 3/3] set right thing initially --- scripts/tutorials/entity_scripts/ambientSound.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/tutorials/entity_scripts/ambientSound.js b/scripts/tutorials/entity_scripts/ambientSound.js index a8e7db239c..620b371400 100644 --- a/scripts/tutorials/entity_scripts/ambientSound.js +++ b/scripts/tutorials/entity_scripts/ambientSound.js @@ -106,7 +106,7 @@ if (!soundPlaying && ambientSound.downloaded) { soundPlaying = Audio.playSound(ambientSound, { loop: true, localOnly: true, - rotation: rotation, + orientation: rotation, volume: volume }); debugPrint("Starting ambient sound, volume: " + volume); if (WANT_COLOR_CHANGE) {