connected mpris shuffle, fixed volume, mpris volumes allowed 0.0-1.0

This commit is contained in:
David Metzler
2023-01-28 15:43:16 +01:00
parent a4a3564136
commit 317e3af412

View File

@ -1,5 +1,5 @@
const mpris = require("mpris-service"); 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"); const config = require("../../config");
@ -20,7 +20,7 @@ function setupMPRIS() {
function registerMPRIS(win) { function registerMPRIS(win) {
const songControls = getSongControls(win); const songControls = getSongControls(win);
const { playPause, next, previous, volumeMinus10, volumePlus10 } = songControls; const {playPause, next, previous, volumeMinus10, volumePlus10, shuffle} = songControls;
try { try {
const secToMicro = n => Math.round(Number(n) * 1e6); const secToMicro = n => Math.round(Number(n) * 1e6);
const microToSec = n => Math.round(Number(n) / 1e6); const microToSec = n => Math.round(Number(n) / 1e6);
@ -35,33 +35,23 @@ function registerMPRIS(win) {
let currentSeconds = 0; let currentSeconds = 0;
ipcMain.on('timeChanged', (_, t) => currentSeconds = t); ipcMain.on('timeChanged', (_, t) => currentSeconds = t);
let currentLoopStatus = undefined;
let manuallySwitchingStatus = false;
ipcMain.on("repeatChanged", (_, mode) => { ipcMain.on("repeatChanged", (_, mode) => {
if (manuallySwitchingStatus)
return;
if (mode === "Repeat off") if (mode === "Repeat off")
currentLoopStatus = "None"; player.loopStatus = mpris.LOOP_STATUS_NONE;
else if (mode === "Repeat one") else if (mode === "Repeat one") //MPRIS Playlist and Track Codes are switched to look the same as yt-music icons
currentLoopStatus = "Track"; player.loopStatus = mpris.LOOP_STATUS_PLAYLIST;
else if (mode === "Repeat all") else if (mode === "Repeat all")
currentLoopStatus = "Playlist"; player.loopStatus = mpris.LOOP_STATUS_TRACK;
player.loopStatus = currentLoopStatus;
}); });
player.on("loopStatus", (status) => { player.on("loopStatus", (status) => {
// switchRepeat cycles between states in that order // switchRepeat cycles between states in that order
const switches = ["None", "Playlist", "Track"]; const switches = [mpris.LOOP_STATUS_NONE, mpris.LOOP_STATUS_PLAYLIST, mpris.LOOP_STATUS_TRACK];
const currentIndex = switches.indexOf(currentLoopStatus); const currentIndex = switches.indexOf(player.loopStatus);
const targetIndex = switches.indexOf(status); const targetIndex = switches.indexOf(status);
// Get a delta in the range [0,2] // Get a delta in the range [0,2]
const delta = (targetIndex - currentIndex + 3) % 3; const delta = (targetIndex - currentIndex + 3) % 3;
manuallySwitchingStatus = true;
songControls.switchRepeat(delta); songControls.switchRepeat(delta);
manuallySwitchingStatus = false;
}) })
player.getPosition = () => secToMicro(currentSeconds) player.getPosition = () => secToMicro(currentSeconds)
@ -72,19 +62,19 @@ function registerMPRIS(win) {
}); });
player.on("play", () => { player.on("play", () => {
if (player.playbackStatus !== 'Playing') { if (player.playbackStatus !== mpris.PLAYBACK_STATUS_PLAYING) {
player.playbackStatus = 'Playing'; player.playbackStatus = mpris.PLAYBACK_STATUS_PLAYING;
playPause() playPause()
} }
}); });
player.on("pause", () => { player.on("pause", () => {
if (player.playbackStatus !== 'Paused') { if (player.playbackStatus !== mpris.PLAYBACK_STATUS_PAUSED) {
player.playbackStatus = 'Paused'; player.playbackStatus = mpris.PLAYBACK_STATUS_PAUSED;
playPause() playPause()
} }
}); });
player.on("playpause", () => { player.on("playpause", () => {
player.playbackStatus = player.playbackStatus === 'Playing' ? "Paused" : "Playing"; player.playbackStatus = player.playbackStatus === mpris.PLAYBACK_STATUS_PLAYING ? mpris.PLAYBACK_STATUS_PAUSED : mpris.PLAYBACK_STATUS_PLAYING;
playPause(); playPause();
}); });
@ -94,40 +84,48 @@ function registerMPRIS(win) {
player.on('seek', seekBy); player.on('seek', seekBy);
player.on('position', seekTo); player.on('position', seekTo);
player.on('shuffle', (enableShuffle) => {
shuffle();
});
ipcMain.on('volumeChanged', (_, value) => { ipcMain.on('volumeChanged', (_, value) => {
player.volume = value; if (config.plugins.isEnabled('precise-volume')) {
player.volume = value / 100;
}
}); });
player.on('volume', (newVolume) => { player.on('volume', (newVolume) => {
if (config.plugins.isEnabled('precise-volume')) { if (config.plugins.isEnabled('precise-volume')) {
// With precise volume we can set the volume to the exact value. // With precise volume we can set the volume to the exact value.
win.webContents.send('setVolume', newVolume) win.webContents.send('setVolume', newVolume * 100)
} else { } else {
// With keyboard shortcuts we can only change the volume in increments of 10, so round it. // With keyboard shortcuts we can only change the volume in increments of 10, so round it.
const deltaVolume = Math.round((newVolume - player.volume) / 10); let deltaVolume = Math.round((newVolume - player.volume) * 10);
while (deltaVolume !== 0 && deltaVolume > 0) {
if (deltaVolume > 0) { volumePlus10();
for (let i = 0; i < deltaVolume; i++) player.volume = player.volume + 0.1;
volumePlus10(); deltaVolume--;
} else { }
for (let i = 0; i < -deltaVolume; i++) while (deltaVolume !== 0 && deltaVolume < 0) {
volumeMinus10(); volumeMinus10();
player.volume = player.volume - 0.1;
deltaVolume++;
} }
} }
}); });
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;
player.metadata = data; player.metadata = data;
player.seeked(secToMicro(songInfo.elapsedSeconds)) player.seeked(secToMicro(songInfo.elapsedSeconds));
player.playbackStatus = songInfo.isPaused ? "Paused" : "Playing" player.playbackStatus = songInfo.isPaused ? mpris.PLAYBACK_STATUS_PAUSED : mpris.PLAYBACK_STATUS_PLAYING;
} }
}) })