One-Ear Audio

Force audio to play in one ear

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

// ==UserScript==
// @name         One-Ear Audio
// @namespace    0x539.BiStereo
// @version      0.1.1
// @description  Force audio to play in one ear
// @author       The0x539
// @match        *://*.twitch.tv/*
// @match        *://*.youtube.com/*
// @grant        GM.registerMenuCommand
// @grant        GM.unregisterMenuCommand
// @license      AGPL-3.0
// ==/UserScript==

'use strict';

class Reroute {
	constructor(element) {
		this.element = element;
		this.ctx = new AudioContext();

		this.source = this.ctx.createMediaElementSource(this.element);
		this.split = this.ctx.createChannelSplitter(2);
		this.merge = this.ctx.createChannelMerger(2);

		this.source.connect(this.split);
		this.setSide(null);
		this.merge.connect(this.ctx.destination);
	}

	setSide(side) {
		this.split.disconnect();
		this.split.connect(this.merge, 0, side ?? 0);
		this.split.connect(this.merge, 1, side ?? 1);
	}
}

const reroutes = new Map();
const getReroute = (element) => {
	if (!reroutes.has(element)) {
		reroutes.set(element, new Reroute(element));
	}
	return reroutes.get(element);
}

let setupDone = false;
let currentSide = null;

function applyToPage(side) {
	currentSide = side;

	if (setupDone) {
		for (const reroute of reroutes.values()) {
			reroute.setSide(currentSide);
		}
		return;
	} else {
		setupDone = true;
	}

	const rerouteAll = () => {
		for (const element of document.querySelectorAll('video')) {
			getReroute(element).setSide(currentSide);
		}
	};

	rerouteAll();

	// TODO: a way to deactivate the full page override, disconnecting this observer
	const observer = new MutationObserver((mutations) => {
		mutations.forEach((mutation) => {
			if (mutation.type === 'childList' && mutation.addedNodes.length > 0) {
				rerouteAll();
			}
		});
	});

	observer.observe(document.body, { childList: true, subtree: true });
}

GM.registerMenuCommand('Page -> Left', () => applyToPage(0));
GM.registerMenuCommand('Page -> Stereo', () => applyToPage(null));
GM.registerMenuCommand('Page -> Right', () => applyToPage(1));