repo: janusweb
action: commit
revision: 
path_from: 
revision_from: 16e21fe6932499688372c1f6328035ff3674c0a6:
path_to: 
revision_to: 
git.thebackupbox.net
janusweb
git clone git://git.thebackupbox.net/janusweb
commit 16e21fe6932499688372c1f6328035ff3674c0a6
Author: James Baicoianu 
Date:   Tue Mar 26 01:54:56 2024 -0700

    Fade audio out when tab is backgrounded or minimized

diff --git a/scripts/room.js b/scripts/room.js
index bf690c7548c4adea24e89f72cd73cb1146c96bba..
index ..2e6ed7581c14157ea1dfd569d3da99d6da57a344 100644
--- a/scripts/room.js
+++ b/scripts/room.js
@@ -151,6 +151,8 @@ elation.require([
         this.loadFromSource(this.source);
       }
       elation.events.add(this, 'thing_remove', elation.bind(this, this.onThingRemove));
+
+      document.addEventListener('visibilitychange', ev => this.handleVisibilityChange());
     }
     this.createChildren = function() {
       this.collidable = false;
@@ -2074,6 +2076,7 @@ elation.require([
           sync:          ['property', 'sync'],
           js_id:         ['property', 'roomid'],
           pickable:      ['property', 'pickable'],
+          audionodes:    ['property', 'audionodes'],

           skybox:         ['property', 'skybox'],
           skybox_intensity: ['property', 'skybox_intensity'],
@@ -2802,7 +2805,7 @@ console.log('dispatch to parent', event, this, event.target);
             ctx = this.audionodes.listener.context;
         //gain.gain.setValueAtTime(0, ctx.currentTime);
         gain.gain.cancelScheduledValues(ctx.currentTime);
-        gain.gain.linearRampToValueAtTime(0, ctx.currentTime);
+        gain.gain.linearRampToValueAtTime(gain.gain.value, ctx.currentTime);
         gain.gain.linearRampToValueAtTime(value, ctx.currentTime + time);
       }
     }
@@ -2934,6 +2937,19 @@ console.log('unknown material', mat);
     this.updatePointerLock = function() {
       this.engine.systems.controls.pointerLockEnabled = this.pointerlock;
     }
+    this.handleVisibilityChange = function(ev) {
+      if (this.audionodes) {
+        if (document.visibilityState == 'visible') {
+          if (this.audioFadeTimer) clearTimeout(this.audioFadeTimer);
+          if (this.audionodes.gain.gain.value < 1) {
+            this.fadeAudioIn(1);
+          }
+        } else {
+          if (this.audioFadeTimer) clearTimeout(this.audioFadeTimer);
+          this.audioFadeTimer = setTimeout(() => this.fadeAudioOut(10), 10000);
+        }
+      }
+    }
   }, elation.engine.things.generic);
 });

-----END OF PAGE-----