Raw Mic Input

Disables WebRTC audio processing to allow raw mic input.

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Raw Mic Input
// @namespace    https://6942020.xyz/
// @version      1.4
// @description  Disables WebRTC audio processing to allow raw mic input.
// @author       Joey Watts
// @author       WadeGrimridge
// @match        *://*/*
// @grant        none
// @run-at       document-start
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    let globalRawAudioTrack = null;

    const rawConfig = {
        echoCancellation: false,
        noiseSuppression: false,
        autoGainControl: false,
        voiceIsolation: false,
        googEchoCancellation: false,
        googAutoGainControl: false,
        googAutoGainControl2: false,
        googNoiseSuppression: false,
        googHighpassFilter: false,
        googTypingNoiseDetection: false
    };

    function disableAutogain(constraints) {
        if (!constraints) return;
        if (constraints.audio === true) constraints.audio = {};
        if (constraints.audio && typeof constraints.audio === 'object') {
            Object.assign(constraints.audio, rawConfig);
            if (constraints.audio.mandatory) {
                Object.assign(constraints.audio.mandatory, rawConfig);
            }
        }
    }

    if (navigator.mediaDevices) {
        const origGetUserMedia = navigator.mediaDevices.getUserMedia;
        navigator.mediaDevices.getUserMedia = function(constraints) {
            disableAutogain(constraints);
            return origGetUserMedia.call(this, constraints).then(stream => {
                const track = stream.getAudioTracks()[0];
                if (track) {
                    globalRawAudioTrack = track;
                }
                return stream;
            });
        };
    }

    if (window.RTCPeerConnection) {
        const origAddTransceiver = RTCPeerConnection.prototype.addTransceiver;
        RTCPeerConnection.prototype.addTransceiver = function(trackOrKind, init) {
            if (globalRawAudioTrack && typeof trackOrKind === 'object' && trackOrKind.kind === 'audio') {
                return origAddTransceiver.call(this, globalRawAudioTrack, init);
            }
            return origAddTransceiver.call(this, trackOrKind, init);
        };
    }

    if (window.RTCRtpSender) {
        const origReplaceTrack = RTCRtpSender.prototype.replaceTrack;
        RTCRtpSender.prototype.replaceTrack = function(track) {
            if (track && globalRawAudioTrack && track.kind === 'audio') {
                return origReplaceTrack.call(this, globalRawAudioTrack);
            }
            return origReplaceTrack.call(this, track);
        };
    }

    if (navigator.getUserMedia) {
        const patchLegacy = (orig) => function(constraints, success, error) {
            disableAutogain(constraints);
            return orig.call(this, constraints, (stream) => {
                const track = stream.getAudioTracks()[0];
                if (track) globalRawAudioTrack = track;
                if (success) success(stream);
            }, error);
        };
        navigator.getUserMedia = patchLegacy(navigator.getUserMedia);
        if (navigator.webkitGetUserMedia) navigator.webkitGetUserMedia = patchLegacy(navigator.webkitGetUserMedia);
        if (navigator.mozGetUserMedia) navigator.mozGetUserMedia = patchLegacy(navigator.mozGetUserMedia);
    }
})();