Last active
October 24, 2023 16:02
-
-
Save bhagman/f3043b28fa6bdf6a630bb192a8bf4a37 to your computer and use it in GitHub Desktop.
YouTube Music Volume Fix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name YouTube Music Volume Fix | |
// @author Brett Hagman | |
// @namespace https://gist.github.com/bhagman/f3043b28fa6bdf6a630bb192a8bf4a37 | |
// @version 1.0.0 | |
// @description Changes YTM volume slider to change based on a curve more suited to human hearing. | |
// @match https://music.youtube.com/* | |
// @grant none | |
// @run-at document-end | |
// ==/UserScript== | |
// Based on Greasyfork script here: | |
// https://greasyfork.org/en/scripts/397686-youtube-music-fix-volume-ratio/code | |
( | |
function() { | |
'use strict'; | |
let player = document.querySelector('ytmusic-player-bar'); | |
if (!player) { | |
console.error("Couldn't capture YTM music player."); | |
return; | |
} | |
function manipulate () { | |
// Capture original function | |
const setVolume = player.playerApi_.setVolume; | |
// Prepare volume-slider visibility timer | |
player.volumeSliderTimeout = -1; | |
if (!setVolume) { | |
console.log("Player wasn't ready, try again"); | |
setTimeout(manipulate, 10); | |
return; | |
} | |
// Replace original function | |
player.playerApi_.setVolume = function (volume) { | |
// console.log('called with volume: ', volume); | |
if (volume != player.currentVolume) { | |
player.currentVolume = volume; | |
// const newVolume = (volume / 100) ** 2 * 100; | |
// used two math sites to obtain this curve | |
// https://mycurvefit.com/ | |
// https://www.desmos.com/calculator | |
// mycurvefit -- gaussian curve, 0:0, 20:2, 50:16, 80:50, 100:100 | |
// Finally, subtracted 1 because... reasons. :P (important to get low end closer to 0, than upper end closer to max) | |
let newVolume = Math.round(549.5614 * Math.E **(-(((volume-209.232)**2)/(2*59.14962**2))) - 1) | |
if (newVolume < 0) newVolume = 0; | |
if (newVolume > 100) newVolume = 100; | |
// console.log('cV: ', player.currentVolume, ', xV: ', newVolume); | |
// Show the volume slider for 5 seconds | |
document.querySelector("#volume-slider").classList.add("on-hover"); | |
if (player.volumeSliderTimeout >= 0) clearTimeout(player.volumeSliderTimeout); | |
player.volumeSliderTimeout = setTimeout(() => { document.querySelector("#volume-slider").classList.remove("on-hover");}, 5000); | |
// Call original function | |
return setVolume.call(this, newVolume); | |
} | |
}; | |
// Get initial volume | |
if (isNaN(player.currentVolume)) { | |
player.currentVolume = -1; | |
player.playerApi_.setVolume(player.playerApi_.getVolume()); | |
// console.log('snagged and set currentVolume: ', player.currentVolume); | |
} | |
// Shim in our getVolume function | |
player.playerApi_.getVolume = function () { | |
return player.currentVolume; | |
} | |
} | |
manipulate(); | |
} | |
)(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment