From 17824edb9c631e462f5ef2ee3f0a75181efad5cd Mon Sep 17 00:00:00 2001
From: Martin Klampfer <martin.klampfer@fairkom.eu>
Date: Mon, 14 Jun 2021 18:48:21 +0200
Subject: [PATCH] (WIP) volume change per channel. in language selection
 sidebar, a volume for specifi channes can be set. these are handed to the
 audiomanager, which maintains the state of the volume levels per channel. on
 channel switch, the corresponding value is used.

---
 .../ui/components/actions-bar/component.jsx   |  2 +-
 .../ui/components/translations/component.jsx  | 12 ++++++++
 .../ui/services/audio-manager/index.js        | 29 +++++++++++++++++++
 3 files changed, 42 insertions(+), 1 deletion(-)

diff --git a/bigbluebutton-html5/imports/ui/components/actions-bar/component.jsx b/bigbluebutton-html5/imports/ui/components/actions-bar/component.jsx
index 95d8be778e..727adbb2fe 100755
--- a/bigbluebutton-html5/imports/ui/components/actions-bar/component.jsx
+++ b/bigbluebutton-html5/imports/ui/components/actions-bar/component.jsx
@@ -128,7 +128,7 @@ class ActionsBar extends PureComponent {
           }
           if (result) {
             AudioManager.setFloorOutputVolume(FLOOR_TRANSLATION_VOLUME);
-            transaudio.volume = 1
+            // transaudio.volume = 1
           } else {
             AudioManager.setFloorOutputVolume(1.0);
           }
diff --git a/bigbluebutton-html5/imports/ui/components/translations/component.jsx b/bigbluebutton-html5/imports/ui/components/translations/component.jsx
index 35534823d2..e936343b21 100644
--- a/bigbluebutton-html5/imports/ui/components/translations/component.jsx
+++ b/bigbluebutton-html5/imports/ui/components/translations/component.jsx
@@ -130,6 +130,11 @@ class Translations extends Component{
         pEvent.preventDefault();
     }
 
+    setLanguageVolume(pExt, pVol) {
+        console.log("#@# pExt: " + pExt + "pVol: " + pVol);
+        AudioManager.$translationChannelVolumeChanged.next({ extension: pExt, volume: pVol});
+    }
+
     render() {
         const {
             intl,
@@ -185,11 +190,18 @@ class Translations extends Component{
                         : null
                     }
                 </p>
+
                 <div>{intl.formatMessage(intlMessages.speechDetectionThreshold)}:</div>
                 <form onSubmit={this.handleSubmit}>
                     <input id="speechDetectionThreshold" type="number" value={this.state.speechDetectionThreshold} onChange={this.setThreshold.bind(this)} />
                     <input type="submit" onClick={ this.updateThreshold.bind(this) } value="Set" />
                 </form>
+
+                <button onClick={ _ => this.setLanguageVolume(0,.5)}>1,0.5</button>
+                <button onClick={ _ => this.setLanguageVolume(0,1)}>1,1</button>
+                <button onClick={ _ => this.setLanguageVolume(1,.5)}>2,0.5</button>
+                <button onClick={ _ => this.setLanguageVolume(1,1)}>2,1</button>
+
             </div>
         );
     }
diff --git a/bigbluebutton-html5/imports/ui/services/audio-manager/index.js b/bigbluebutton-html5/imports/ui/services/audio-manager/index.js
index 69b6472400..337528e321 100755
--- a/bigbluebutton-html5/imports/ui/services/audio-manager/index.js
+++ b/bigbluebutton-html5/imports/ui/services/audio-manager/index.js
@@ -60,6 +60,9 @@ class AudioManager {
     this.$translatorSpeechDetectionThresholdChanged = new BehaviorSubject(TRANSLATOR_SPEECH_DETECTION_THRESHOLD)
     this.$translatorSpeakingChanged = new BehaviorSubject(false)
 
+    this.$translationChannelVolumeChanged = new BehaviorSubject({ extension: -1, volume: 0 });
+    this.translationChannelVolume = [];
+
     this.defineProperties({
       isMuted: false,
       isConnected: false,
@@ -98,6 +101,18 @@ class AudioManager {
       }
     });
 
+    this.$translationChannelVolumeChanged.subscribe((pLang) => {
+        if(
+            pLang.hasOwnProperty("extension") &&
+            pLang.hasOwnProperty("volume")
+        ) {
+          this.translationChannelVolume[pLang.extension] = pLang.volume;
+          let audioElement = document.getElementById("translation-media");
+          if(audioElement) audioElement.volume = pLang.volume;
+        }
+    })
+
+
   }
 
   init(userData, audioEventHandler) {
@@ -809,6 +824,16 @@ class AudioManager {
       //create a dummy stream that does nothing at all
       let ac = new AudioContext();
       let dest = ac.createMediaStreamDestination();
+      let audioElement = document.getElementById("translation-media");
+      console.log("#@# languageExtension: " + languageExtension);
+      let tIdx = parseInt((languageExtension+ '').charAt(2));
+      console.log("#@# tIdx: " + tIdx);
+      console.log("#@# this.translationChannelVolume: ");
+      console.log(this.translationChannelVolume);
+      if(audioElement) audioElement.volume =
+          Array.isArray(this.translationChannelVolume) && typeof this.translationChannelVolume[tIdx] !== 'undefined' ?
+              this.translationChannelVolume[tIdx] : 1;
+
       if (languageExtension >= 0) {
         const callOptions = {
           isListenOnly: true,
@@ -827,6 +852,7 @@ class AudioManager {
         resolve(-1);
       }
     });
+
   }
 
   async openTranslatorChannel(languageExtension, onConnected) {
@@ -928,6 +954,9 @@ class AudioManager {
   async notifyMuteStateListener() {
     this.muteStateCallbacks.forEach(callback => callback());
   }
+
+
+
 }
 
 const audioManager = new AudioManager();
-- 
GitLab