From 9da0e4305f078a081c5a828a3365194865157ede Mon Sep 17 00:00:00 2001 From: David Metzler Date: Fri, 3 Feb 2023 12:02:25 +0100 Subject: [PATCH] Fixed recursive volume changes that caused cpu spike, Switched Repeat Modes to NONE|ONE|ALL --- plugins/shortcuts/mpris.js | 31 +++++++++++++++++++++++++------ providers/song-info-front.js | 5 ++--- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/plugins/shortcuts/mpris.js b/plugins/shortcuts/mpris.js index 071b2855..90c1406a 100644 --- a/plugins/shortcuts/mpris.js +++ b/plugins/shortcuts/mpris.js @@ -36,11 +36,11 @@ function registerMPRIS(win) { ipcMain.on('timeChanged', (_, t) => currentSeconds = t); ipcMain.on("repeatChanged", (_, mode) => { - if (mode === "Repeat off") + if (mode === "NONE") player.loopStatus = mpris.LOOP_STATUS_NONE; - else if (mode === "Repeat one") //MPRIS Playlist and Track Codes are switched to look the same as yt-music icons + else if (mode === "ONE") //MPRIS Playlist and Track Codes are switched to look the same as yt-music icons player.loopStatus = mpris.LOOP_STATUS_PLAYLIST; - else if (mode === "Repeat all") + else if (mode === "ALL") player.loopStatus = mpris.LOOP_STATUS_TRACK; }); player.on("loopStatus", (status) => { @@ -88,14 +88,33 @@ function registerMPRIS(win) { shuffle(); }); - ipcMain.on('volumeChanged', (_, value) => { - player.volume = value / 100; + let mprisVolNewer = false; + let autoUpdate = false; + ipcMain.on('volumeChanged', (_, newVol) => { + if (parseInt(player.volume * 100) !== newVol) { + if (mprisVolNewer) { + mprisVolNewer = false; + autoUpdate = false; + } else { + autoUpdate = true; + player.volume = parseFloat((newVol / 100).toFixed(2)); + mprisVolNewer = false; + autoUpdate = false; + } + } }); player.on('volume', (newVolume) => { if (config.plugins.isEnabled('precise-volume')) { // With precise volume we can set the volume to the exact value. - win.webContents.send('setVolume', newVolume * 100) + let newVol = parseInt(newVolume * 100); + if (parseInt(player.volume * 100) !== newVol) { + if (!autoUpdate){ + mprisVolNewer = true; + autoUpdate = false; + win.webContents.send('setVolume', newVol); + } + } } else { // With keyboard shortcuts we can only change the volume in increments of 10, so round it. let deltaVolume = Math.round((newVolume - player.volume) * 10); diff --git a/providers/song-info-front.js b/providers/song-info-front.js index c6c65876..958bcd0a 100644 --- a/providers/song-info-front.js +++ b/providers/song-info-front.js @@ -67,14 +67,13 @@ function setupTimeChangeListener() { } function setupRepeatChangeListener() { - const mp = { NONE: "Repeat off", ONE: "Repeat one", ALL: "Repeat all" } const repeatObserver = new MutationObserver(mutations => { - ipcRenderer.send('repeatChanged', mp[mutations[0].target.__dataHost.getState().queue.repeatMode]) + ipcRenderer.send('repeatChanged', mutations[0].target.__dataHost.getState().queue.repeatMode) }); repeatObserver.observe($('#right-controls .repeat'), {attributeFilter: ["title"]}); // Emit the initial value as well; as it's persistent between launches. - ipcRenderer.send('repeatChanged', mp[$('ytmusic-player-bar').getState().queue.repeatMode]); + ipcRenderer.send('repeatChanged', $('ytmusic-player-bar').getState().queue.repeatMode); } function setupVolumeChangeListener(api) {