Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatic Gain Control toggle #161

Open
Vendicated opened this issue Oct 23, 2023 · 10 comments
Open

Automatic Gain Control toggle #161

Vendicated opened this issue Oct 23, 2023 · 10 comments
Labels
enhancement New feature or request

Comments

@Vendicated
Copy link
Member

Discord has a toggle for automatic gain control. It currently has no effect on vesktop though. it would be great to implement it

@Plarpoon
Copy link

Hi all, wanted to report that the problem described in issue #142 is still present in the current version!

I myself didn't realize it was Vesktop bug so I actually spent a day looking at pavucontrol and banging my head against it. Thank you for reporting that it cannot be disabled, you put my mind at peace!

@PopLamina

This comment was marked as spam.

@makidoll
Copy link

makidoll commented Feb 7, 2024

It might be a little cursed but I cloned the project locally and added the source code from this chrome extension into src/renderer/index.ts. It patches getUserMedia and such to permanently disable agc.

https://chromewebstore.google.com/detail/disable-automatic-gain-co/clpapnmmlmecieknddelobgikompchkk

@jessicamaybe
Copy link

It might be a little cursed but I cloned the project locally and added the source code from this chrome extension into src/renderer/index.ts. It patches getUserMedia and such to permanently disable agc.

https://chromewebstore.google.com/detail/disable-automatic-gain-co/clpapnmmlmecieknddelobgikompchkk

Would you mind sharing your changes if possible?

@makidoll
Copy link

diff --git a/src/renderer/index.ts b/src/renderer/index.ts
index ebe6bc6..d891c26 100644
--- a/src/renderer/index.ts
+++ b/src/renderer/index.ts
@@ -57,3 +57,81 @@ VesktopNative.arrpc.onActivity(data => {
 
     arRPC.handleEvent(new MessageEvent("message", { data }));
 });
+
+// force disable automatic gain control
+
+(function () {
+    function setLegacyChromeConstraint(constraint, name, value) {
+        if (constraint.mandatory && name in constraint.mandatory) {
+            constraint.mandatory[name] = value;
+            return;
+        }
+        if (constraint.optional) {
+            const element = constraint.optional.find(opt => name in opt);
+            if (element) {
+                element[name] = value;
+                return;
+            }
+        }
+        // `mandatory` options throw errors for unknown keys, so avoid that by
+        // setting it under optional.
+        if (!constraint.optional) {
+            constraint.optional = [];
+        }
+        constraint.optional.push({ [name]: value });
+    }
+    function setConstraint(constraint, name, value) {
+        if (constraint.advanced) {
+            const element = constraint.advanced.find(opt => name in opt);
+            if (element) {
+                element[name] = value;
+                return;
+            }
+        }
+        constraint[name] = value;
+    }
+    function disableAutogain(constraints) {
+        console.log("Automatically unsetting gain!", constraints);
+        if (constraints && constraints.audio) {
+            if (typeof constraints.audio !== "object") {
+                constraints.audio = {};
+            }
+            if (constraints.audio.optional || constraints.audio.mandatory) {
+                setLegacyChromeConstraint(constraints.audio, "googAutoGainControl", false);
+                setLegacyChromeConstraint(constraints.audio, "googAutoGainControl2", false);
+            } else {
+                setConstraint(constraints.audio, "autoGainControl", false);
+            }
+        }
+    }
+
+    function patchFunction(object, name, createNewFunction) {
+        if (name in object) {
+            var original = object[name];
+            object[name] = createNewFunction(original);
+        }
+    }
+
+    patchFunction(navigator.mediaDevices, "getUserMedia", function (original) {
+        return function getUserMedia(constraints) {
+            disableAutogain(constraints);
+            return original.call(this, constraints);
+        };
+    });
+    function patchDeprecatedGetUserMedia(original) {
+        return function getUserMedia(constraints, success, error) {
+            disableAutogain(constraints);
+            return original.call(this, constraints, success, error);
+        };
+    }
+    patchFunction(navigator, "getUserMedia", patchDeprecatedGetUserMedia);
+    patchFunction(navigator, "mozGetUserMedia", patchDeprecatedGetUserMedia);
+    patchFunction(navigator, "webkitGetUserMedia", patchDeprecatedGetUserMedia);
+    patchFunction(MediaStreamTrack.prototype, "applyConstraints", function (original) {
+        return function applyConstraints(constraints) {
+            disableAutogain(constraints);
+            return original.call(this, constraints);
+        };
+    });
+    console.log("Disable Autogain by Joey Watts!", navigator.mediaDevices.getUserMedia);
+})();

@TapGhoul
Copy link

TapGhoul commented Mar 21, 2024

Hey, @Vendicated is there anything that the above patch would need for me to make an MR to fix this that would make this suitable to be merged into mainline? The issue is quite irritating, so I'll happily fix it myself if you're busy.

@jvyden
Copy link

jvyden commented May 24, 2024

For Nix users I wrote this to include the above patch by @makidoll with your build.

It also extends the patch to disable echo cancellation.

      (vesktop.overrideAttrs (previousAttrs: {
        patches = previousAttrs.patches ++ [
          (fetchpatch {
            name = "micfix-b0730e139805c4eea0d610be8fac28c1ed75aced.patch";
            url = "https://gist.githubusercontent.com/jvyden/4aa114a1118a06f3be96710df95f311c/raw/b0730e139805c4eea0d610be8fac28c1ed75aced/micfix.patch";
            hash = "sha256-EIK7/CtKpruf4/N2vn8XSCNkyDCL8I6ciXOljkvgz5A=";
          })
        ];
      }))

@Foxite

This comment was marked as spam.

@VoidgirlChloe

This comment was marked as spam.

@PavelDobCZ23
Copy link

PavelDobCZ23 commented Jul 8, 2024

For now, if anybody wants this resolved, add this argument to your .desktop file of Vesktop:
--disable-features=WebRtcAllowInputVolumeAdjustment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

10 participants