Skip to content

Commit 4b9af14

Browse files
authored
Merge pull request #1065 from Araxeus/add-crossfade-menu-options
[crossfade] add menu options
2 parents f8db04e + 55a442e commit 4b9af14

File tree

9 files changed

+194
-33
lines changed

9 files changed

+194
-33
lines changed

config/defaults.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,13 @@ const defaultConfig = {
105105
"skip-silences": {
106106
onlySkipBeginning: false,
107107
},
108+
"crossfade": {
109+
enabled: false,
110+
fadeInDuration: 1500, // ms
111+
fadeOutDuration: 5000, // ms
112+
secondsBeforeEnd: 10, // s
113+
fadeScaling: "linear", // 'linear', 'logarithmic' or a positive number in dB
114+
},
108115
visualizer: {
109116
enabled: false,
110117
type: "butterchurn",

config/dynamic.js

Lines changed: 69 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const { ipcRenderer, ipcMain } = require("electron");
22

33
const defaultConfig = require("./defaults");
44
const { getOptions, setOptions, setMenuOptions } = require("./plugins");
5+
const { sendToFront } = require("../providers/app-controls");
56

67
const activePlugins = {};
78
/**
@@ -58,6 +59,9 @@ module.exports.PluginConfig = class PluginConfig {
5859
#defaultConfig;
5960
#enableFront;
6061

62+
#subscribers = {};
63+
#allSubscribers = [];
64+
6165
constructor(name, { enableFront = false, initialOptions = undefined } = {}) {
6266
const pluginDefaultConfig = defaultConfig.plugins[name] || {};
6367
const pluginConfig = initialOptions || getOptions(name) || {};
@@ -80,11 +84,13 @@ module.exports.PluginConfig = class PluginConfig {
8084

8185
set = (option, value) => {
8286
this.#config[option] = value;
87+
this.#onChange(option);
8388
this.#save();
8489
};
8590

8691
toggle = (option) => {
8792
this.#config[option] = !this.#config[option];
93+
this.#onChange(option);
8894
this.#save();
8995
};
9096

@@ -93,7 +99,18 @@ module.exports.PluginConfig = class PluginConfig {
9399
};
94100

95101
setAll = (options) => {
96-
this.#config = { ...this.#config, ...options };
102+
if (!options || typeof options !== "object")
103+
throw new Error("Options must be an object.");
104+
105+
let changed = false;
106+
for (const [key, val] of Object.entries(options)) {
107+
if (this.#config[key] !== val) {
108+
this.#config[key] = val;
109+
this.#onChange(key, false);
110+
changed = true;
111+
}
112+
}
113+
if (changed) this.#allSubscribers.forEach((fn) => fn(this.#config));
97114
this.#save();
98115
};
99116

@@ -109,31 +126,80 @@ module.exports.PluginConfig = class PluginConfig {
109126
setAndMaybeRestart = (option, value) => {
110127
this.#config[option] = value;
111128
setMenuOptions(this.#name, this.#config);
129+
this.#onChange(option);
130+
};
131+
132+
subscribe = (valueName, fn) => {
133+
this.#subscribers[valueName] = fn;
134+
};
135+
136+
subscribeAll = (fn) => {
137+
this.#allSubscribers.push(fn);
112138
};
113139

114140
/** Called only from back */
115141
#save() {
116142
setOptions(this.#name, this.#config);
117143
}
118144

145+
#onChange(valueName, single = true) {
146+
this.#subscribers[valueName]?.(this.#config[valueName]);
147+
if (single) this.#allSubscribers.forEach((fn) => fn(this.#config));
148+
}
149+
119150
#setupFront() {
151+
const ignoredMethods = ["subscribe", "subscribeAll"];
152+
120153
if (process.type === "renderer") {
121154
for (const [fnName, fn] of Object.entries(this)) {
122-
if (typeof fn !== "function") return;
155+
if (typeof fn !== "function" || fn.name in ignoredMethods) return;
123156
this[fnName] = async (...args) => {
124157
return await ipcRenderer.invoke(
125158
`${this.name}-config-${fnName}`,
126159
...args,
127160
);
128161
};
162+
163+
this.subscribe = (valueName, fn) => {
164+
if (valueName in this.#subscribers) {
165+
console.error(`Already subscribed to ${valueName}`);
166+
}
167+
this.#subscribers[valueName] = fn;
168+
ipcRenderer.on(
169+
`${this.name}-config-changed-${valueName}`,
170+
(_, value) => {
171+
fn(value);
172+
},
173+
);
174+
ipcRenderer.send(`${this.name}-config-subscribe`, valueName);
175+
};
176+
177+
this.subscribeAll = (fn) => {
178+
ipcRenderer.on(`${this.name}-config-changed`, (_, value) => {
179+
fn(value);
180+
});
181+
ipcRenderer.send(`${this.name}-config-subscribe-all`);
182+
};
129183
}
130184
} else if (process.type === "browser") {
131185
for (const [fnName, fn] of Object.entries(this)) {
132-
if (typeof fn !== "function") return;
186+
if (typeof fn !== "function" || fn.name in ignoredMethods) return;
133187
ipcMain.handle(`${this.name}-config-${fnName}`, (_, ...args) => {
134188
return fn(...args);
135189
});
136190
}
191+
192+
ipcMain.on(`${this.name}-config-subscribe`, (_, valueName) => {
193+
this.subscribe(valueName, (value) => {
194+
sendToFront(`${this.name}-config-changed-${valueName}`, value);
195+
});
196+
});
197+
198+
ipcMain.on(`${this.name}-config-subscribe-all`, () => {
199+
this.subscribeAll((value) => {
200+
sendToFront(`${this.name}-config-changed`, value);
201+
});
202+
});
137203
}
138204
}
139205
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@
115115
"browser-id3-writer": "^4.4.0",
116116
"butterchurn": "^2.6.7",
117117
"butterchurn-presets": "^2.4.7",
118-
"custom-electron-prompt": "^1.5.4",
118+
"custom-electron-prompt": "^1.5.7",
119119
"custom-electron-titlebar": "^4.1.6",
120120
"electron-better-web-request": "^1.0.1",
121121
"electron-debug": "^3.2.0",

plugins/crossfade/back.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
const { ipcMain } = require("electron");
22
const { Innertube } = require("youtubei.js");
33

4-
module.exports = async (win, options) => {
4+
require("./config");
5+
6+
module.exports = async () => {
57
const yt = await Innertube.create();
68

79
ipcMain.handle("audio-url", async (_, videoID) => {

plugins/crossfade/config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const { PluginConfig } = require("../../config/dynamic");
2+
const config = new PluginConfig("crossfade", { enableFront: true });
3+
module.exports = { ...config };

plugins/crossfade/front.js

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,12 @@ let transitionAudio; // Howler audio used to fade out the current music
88
let firstVideo = true;
99
let waitForTransition;
1010

11-
// Crossfade options that can be overridden in plugin options
12-
let crossfadeOptions = {
13-
fadeInDuration: 1500, // ms
14-
fadeOutDuration: 5000, // ms
15-
exitMusicBeforeEnd: 10, // s
16-
fadeScaling: "linear",
17-
};
11+
const defaultConfig = require("../../config/defaults").plugins.crossfade;
12+
13+
const configProvider = require("./config");
14+
let config;
15+
16+
const configGetNum = (key) => Number(config[key]) || defaultConfig[key];
1817

1918
const getStreamURL = async (videoID) => {
2019
const url = await ipcRenderer.invoke("audio-url", videoID);
@@ -32,7 +31,7 @@ const isReadyToCrossfade = () => {
3231
const watchVideoIDChanges = (cb) => {
3332
navigation.addEventListener("navigate", (event) => {
3433
const currentVideoID = getVideoIDFromURL(
35-
event.currentTarget.currentEntry.url
34+
event.currentTarget.currentEntry.url,
3635
);
3736
const nextVideoID = getVideoIDFromURL(event.destination.url);
3837

@@ -67,9 +66,10 @@ const createAudioForCrossfade = async (url) => {
6766

6867
const syncVideoWithTransitionAudio = async () => {
6968
const video = document.querySelector("video");
69+
7070
const videoFader = new VolumeFader(video, {
71-
fadeScaling: crossfadeOptions.fadeScaling,
72-
fadeDuration: crossfadeOptions.fadeInDuration,
71+
fadeScaling: configGetNum("fadeScaling"),
72+
fadeDuration: configGetNum("fadeInDuration"),
7373
});
7474

7575
await transitionAudio.play();
@@ -94,8 +94,7 @@ const syncVideoWithTransitionAudio = async () => {
9494
// Exit just before the end for the transition
9595
const transitionBeforeEnd = () => {
9696
if (
97-
video.currentTime >=
98-
video.duration - crossfadeOptions.exitMusicBeforeEnd &&
97+
video.currentTime >= video.duration - configGetNum("secondsBeforeEnd") &&
9998
isReadyToCrossfade()
10099
) {
101100
video.removeEventListener("timeupdate", transitionBeforeEnd);
@@ -115,7 +114,7 @@ const onApiLoaded = () => {
115114
});
116115
};
117116

118-
const crossfade = (cb) => {
117+
const crossfade = async (cb) => {
119118
if (!isReadyToCrossfade()) {
120119
cb();
121120
return;
@@ -130,8 +129,8 @@ const crossfade = (cb) => {
130129

131130
const fader = new VolumeFader(transitionAudio._sounds[0]._node, {
132131
initialVolume: video.volume,
133-
fadeScaling: crossfadeOptions.fadeScaling,
134-
fadeDuration: crossfadeOptions.fadeOutDuration,
132+
fadeScaling: configGetNum("fadeScaling"),
133+
fadeDuration: configGetNum("fadeOutDuration"),
135134
});
136135

137136
// Fade out the music
@@ -142,11 +141,12 @@ const crossfade = (cb) => {
142141
});
143142
};
144143

145-
module.exports = (options) => {
146-
crossfadeOptions = {
147-
...crossfadeOptions,
148-
options,
149-
};
144+
module.exports = async () => {
145+
config = await configProvider.getAll();
146+
147+
configProvider.subscribeAll((newConfig) => {
148+
config = newConfig;
149+
});
150150

151151
document.addEventListener("apiLoaded", onApiLoaded, {
152152
once: true,

plugins/crossfade/menu.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
const config = require("./config");
2+
const defaultOptions = require("../../config/defaults").plugins.crossfade;
3+
4+
const prompt = require("custom-electron-prompt");
5+
const promptOptions = require("../../providers/prompt-options");
6+
7+
module.exports = (win) => [
8+
{
9+
label: "Advanced",
10+
click: async () => {
11+
const newOptions = await promptCrossfadeValues(win, config.getAll());
12+
if (newOptions) config.setAll(newOptions);
13+
},
14+
},
15+
];
16+
17+
async function promptCrossfadeValues(win, options) {
18+
const res = await prompt(
19+
{
20+
title: "Crossfade Options",
21+
type: "multiInput",
22+
multiInputOptions: [
23+
{
24+
label: "Fade in duration (ms)",
25+
value: options.fadeInDuration || defaultOptions.fadeInDuration,
26+
inputAttrs: {
27+
type: "number",
28+
required: true,
29+
min: 0,
30+
step: 100,
31+
},
32+
},
33+
{
34+
label: "Fade out duration (ms)",
35+
value: options.fadeOutDuration || defaultOptions.fadeOutDuration,
36+
inputAttrs: {
37+
type: "number",
38+
required: true,
39+
min: 0,
40+
step: 100,
41+
},
42+
},
43+
{
44+
label: "Crossfade x seconds before end",
45+
value:
46+
options.secondsBeforeEnd || defaultOptions.secondsBeforeEnd,
47+
inputAttrs: {
48+
type: "number",
49+
required: true,
50+
min: 0,
51+
},
52+
},
53+
{
54+
label: "Fade scaling",
55+
selectOptions: { linear: "Linear", logarithmic: "Logarithmic" },
56+
value: options.fadeScaling || defaultOptions.fadeScaling,
57+
},
58+
],
59+
resizable: true,
60+
height: 360,
61+
...promptOptions(),
62+
},
63+
win,
64+
).catch(console.error);
65+
if (!res) return undefined;
66+
return {
67+
fadeInDuration: Number(res[0]),
68+
fadeOutDuration: Number(res[1]),
69+
secondsBeforeEnd: Number(res[2]),
70+
fadeScaling: res[3],
71+
};
72+
}

providers/app-controls.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
const path = require("path");
22

3-
const is = require("electron-is");
4-
53
const { app, BrowserWindow, ipcMain, ipcRenderer } = require("electron");
64
const config = require("../config");
75

86
module.exports.restart = () => {
9-
is.main() ? restart() : ipcRenderer.send('restart');
7+
process.type === 'browser' ? restart() : ipcRenderer.send('restart');
108
};
119

1210
module.exports.setupAppControls = () => {
@@ -21,3 +19,16 @@ function restart() {
2119
// execPath will be undefined if not running portable app, resulting in default behavior
2220
app.quit();
2321
}
22+
23+
function sendToFront(channel, ...args) {
24+
BrowserWindow.getAllWindows().forEach(win => {
25+
win.webContents.send(channel, ...args);
26+
});
27+
}
28+
29+
module.exports.sendToFront =
30+
process.type === 'browser'
31+
? sendToFront
32+
: () => {
33+
console.error('sendToFront called from renderer');
34+
};

yarn.lock

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2382,12 +2382,12 @@ __metadata:
23822382
languageName: node
23832383
linkType: hard
23842384

2385-
"custom-electron-prompt@npm:^1.5.4":
2386-
version: 1.5.4
2387-
resolution: "custom-electron-prompt@npm:1.5.4"
2385+
"custom-electron-prompt@npm:^1.5.7":
2386+
version: 1.5.7
2387+
resolution: "custom-electron-prompt@npm:1.5.7"
23882388
peerDependencies:
23892389
electron: ">=10.0.0"
2390-
checksum: 93995b5f0e9d14401a8c4fdd358af32d8b7585b59b111667cfa55f9505109c08914f3140953125b854e5d09e811de8c76c7fec718934c13e8a1ad09fe1b85270
2390+
checksum: 7dd7b2fb6e0acdee35474893d0e98b5e701c411c76be716cc02c5c9ac42db4fdaa7d526e22fd8c7047c2f143559d185bed8731bd455a1d11982404512d5f5021
23912391
languageName: node
23922392
linkType: hard
23932393

@@ -8883,7 +8883,7 @@ __metadata:
88838883
browser-id3-writer: ^4.4.0
88848884
butterchurn: ^2.6.7
88858885
butterchurn-presets: ^2.4.7
8886-
custom-electron-prompt: ^1.5.4
8886+
custom-electron-prompt: ^1.5.7
88878887
custom-electron-titlebar: ^4.1.6
88888888
del-cli: ^5.0.0
88898889
electron: ^22.0.2

0 commit comments

Comments
 (0)