Discord Web Stereo and High Bitrate

Enable stereo microphone input and set the highest bitrate for Discord Web.

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Discord Web Stereo and High Bitrate
// @namespace    http://tampermonkey.net/
// @version      0.5
// @description  Enable stereo microphone input and set the highest bitrate for Discord Web.
// @match        https://discord.com/*
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    // Hook into UnifiedConnection.setTransceiverEncodingParameters
    const hookSetTransceiverEncodingParameters = () => {
        const originalSetParameters = UnifiedConnection.prototype.setTransceiverEncodingParameters;
        UnifiedConnection.prototype.setTransceiverEncodingParameters = function(parameters) {
            if (parameters && parameters.encodings) {
                for (const encoding of parameters.encodings) {
                    encoding.maxBitrate = 1329600; // Set this to the desired bitrate (1329.60 kbps)
                    encoding.channels = 2; // Enable stereo
                }
            }
            return originalSetParameters.apply(this, arguments);
        };
    };

    // Hook into setTransportOptions
    const hookSetTransportOptions = () => {
        const originalSetTransportOptions = RTCRtpSender.prototype.setTransportOptions;
        RTCRtpSender.prototype.setTransportOptions = function(options) {
            if (options && options.audio && options.audio.transportOptions) {
                options.audio.transportOptions.encodingParams = {
                    channels: 2 // Enable stereo
                };
            }
            return originalSetTransportOptions.apply(this, arguments);
        };
    };

    // Initialize hooks when the page loads
    const init = () => {
        hookSetTransceiverEncodingParameters();
        hookSetTransportOptions();
    };

    // Wait for the Discord Web app to fully load before initializing
    window.addEventListener('load', init);
})();