Support precise volume changes in MPRIS when possible

This commit is contained in:
Jonathan Müller
2022-07-11 20:20:13 +02:00
parent d9c51063f4
commit dfba3d9c2d
2 changed files with 36 additions and 26 deletions

View File

@ -11,6 +11,7 @@ module.exports = (_options) => {
document.addEventListener('apiLoaded', e => { document.addEventListener('apiLoaded', e => {
api = e.detail; api = e.detail;
ipcRenderer.on('changeVolume', (_, toIncrease) => changeVolume(toIncrease)); ipcRenderer.on('changeVolume', (_, toIncrease) => changeVolume(toIncrease));
ipcRenderer.on('setVolume', (_, value) => setVolume(value));
firstRun(); firstRun();
}, { once: true, passive: true }) }, { once: true, passive: true })
}; };
@ -163,26 +164,29 @@ function setupSliderObserver() {
}); });
} }
/** if (toIncrease = false) then volume decrease */ function setVolume(value) {
function changeVolume(toIncrease) { api.setVolume(value);
// Apply volume change if valid
const steps = Number(options.steps || 1);
api.setVolume(toIncrease ?
Math.min(api.getVolume() + steps, 100) :
Math.max(api.getVolume() - steps, 0));
// Save the new volume // Save the new volume
saveVolume(api.getVolume()); saveVolume(value);
// change slider position (important) // change slider position (important)
updateVolumeSlider(); updateVolumeSlider();
// Change tooltips to new value // Change tooltips to new value
setTooltip(options.savedVolume); setTooltip(value);
// Show volume slider // Show volume slider
showVolumeSlider(); showVolumeSlider();
// Show volume HUD // Show volume HUD
showVolumeHud(options.savedVolume); showVolumeHud(value);
}
/** if (toIncrease = false) then volume decrease */
function changeVolume(toIncrease) {
// Apply volume change if valid
const steps = Number(options.steps || 1);
setVolume(toIncrease ?
Math.min(api.getVolume() + steps, 100) :
Math.max(api.getVolume() - steps, 0));
} }
function updateVolumeSlider() { function updateVolumeSlider() {

View File

@ -2,6 +2,7 @@ const mpris = require("mpris-service");
const { ipcMain } = require("electron"); const { ipcMain } = require("electron");
const registerCallback = require("../../providers/song-info"); const registerCallback = require("../../providers/song-info");
const getSongControls = require("../../providers/song-controls"); const getSongControls = require("../../providers/song-controls");
const config = require("../../config");
function setupMPRIS() { function setupMPRIS() {
const player = mpris({ const player = mpris({
@ -97,25 +98,30 @@ function registerMPRIS(win) {
player.volume = value; player.volume = value;
}); });
player.on('volume', (newVolume) => { player.on('volume', (newVolume) => {
// With keyboard shortcuts we can only change the volume in increments of 10, so round it. if (config.plugins.isEnabled('precise-volume')) {
const deltaVolume = Math.round((newVolume - player.volume) / 10); // With precise volume we can set the volume to the exact value.
win.webContents.send('setVolume', newVolume)
if (deltaVolume > 0) {
for (let i = 0; i < deltaVolume; i++)
volumePlus10();
} else { } else {
for (let i = 0; i < -deltaVolume; i++) // With keyboard shortcuts we can only change the volume in increments of 10, so round it.
volumeMinus10(); const deltaVolume = Math.round((newVolume - player.volume) / 10);
if (deltaVolume > 0) {
for (let i = 0; i < deltaVolume; i++)
volumePlus10();
} else {
for (let i = 0; i < -deltaVolume; i++)
volumeMinus10();
}
} }
}); });
registerCallback(songInfo => { registerCallback(songInfo => {
if (player) { if (player) {
const data = { const data = {
'mpris:length': secToMicro(songInfo.songDuration), 'mpris:length': secToMicro(songInfo.songDuration),
'mpris:artUrl': songInfo.imageSrc, 'mpris:artUrl': songInfo.imageSrc,
'xesam:title': songInfo.title, 'xesam:title': songInfo.title,
'xesam:artist': [songInfo.artist], 'xesam:artist': [songInfo.artist],
'mpris:trackid': '/' 'mpris:trackid': '/'
}; };
if (songInfo.album) data['xesam:album'] = songInfo.album; if (songInfo.album) data['xesam:album'] = songInfo.album;