Merge pull request #9254 from PhilipRosedale/ambisonicRotation

Ambisonic rotation
This commit is contained in:
Chris Collins 2016-12-23 09:25:48 -08:00 committed by GitHub
commit 9bacab07e6

View file

@ -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.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) // 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 // 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.
// //
@ -27,6 +30,7 @@
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;
var rotation;
var entity; var entity;
var ambientSound; var ambientSound;
@ -35,11 +39,11 @@
var checkTimer = false; var checkTimer = false;
var _this; var _this;
var WANT_COLOR_CHANGE = true; var WANT_COLOR_CHANGE = false;
var COLOR_OFF = { red: 128, green: 128, blue: 128 }; var COLOR_OFF = { red: 128, green: 128, blue: 128 };
var COLOR_ON = { red: 255, green: 0, blue: 0 }; var COLOR_ON = { red: 255, green: 0, blue: 0 };
var WANT_DEBUG = true; var WANT_DEBUG = false;
function debugPrint(string) { function debugPrint(string) {
if (WANT_DEBUG) { if (WANT_DEBUG) {
print(string); print(string);
@ -92,23 +96,27 @@
this.maybeUpdate = 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(); _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", "rotation" ]);
center = props.position; center = props.position;
rotation = props.rotation;
var distance = Vec3.length(Vec3.subtract(MyAvatar.position, center)); var distance = Vec3.length(Vec3.subtract(MyAvatar.position, center));
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, localOnly: true, volume: volume }); soundPlaying = Audio.playSound(ambientSound, { loop: true,
localOnly: true,
orientation: rotation,
volume: volume });
debugPrint("Starting ambient sound, volume: " + volume); debugPrint("Starting ambient sound, volume: " + volume);
if (WANT_COLOR_CHANGE) { if (WANT_COLOR_CHANGE) {
Entities.editEntity(entity, { color: COLOR_ON }); Entities.editEntity(entity, { color: COLOR_ON });
} }
} else if (soundPlaying && soundPlaying.playing) { } else if (soundPlaying && soundPlaying.playing) {
soundPlaying.setOptions( { volume: volume } ); soundPlaying.setOptions( { volume: volume, orientation: rotation } );
} }
} else if (soundPlaying && soundPlaying.playing && (distance > range * HYSTERESIS_FRACTION)) { } else if (soundPlaying && soundPlaying.playing && (distance > range * HYSTERESIS_FRACTION)) {
soundPlaying.stop(); soundPlaying.stop();
soundPlaying = false; soundPlaying = false;