Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[crossfade] add menu options #1065

Merged
merged 13 commits into from
Apr 4, 2023
7 changes: 7 additions & 0 deletions config/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,13 @@ const defaultConfig = {
"skip-silences": {
onlySkipBeginning: false,
},
"crossfade": {
enabled: false,
fadeInDuration: 1500, // ms
fadeOutDuration: 5000, // ms
exitMusicBeforeEnd: 10, // s
fadeScaling: "linear", // 'linear', 'logarithmic' or a positive number in dB
},
visualizer: {
enabled: false,
type: "butterchurn",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@
"butterchurn": "^2.6.7",
"butterchurn-presets": "^2.4.7",
"chokidar": "^3.5.3",
"custom-electron-prompt": "^1.5.1",
"custom-electron-prompt": "^1.5.4",
"custom-electron-titlebar": "^4.1.6",
"electron-better-web-request": "^1.0.1",
"electron-debug": "^3.2.0",
Expand Down
23 changes: 7 additions & 16 deletions plugins/crossfade/front.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,7 @@ let transitionAudio; // Howler audio used to fade out the current music
let firstVideo = true;
let waitForTransition;

// Crossfade options that can be overridden in plugin options
let crossfadeOptions = {
fadeInDuration: 1500, // ms
fadeOutDuration: 5000, // ms
exitMusicBeforeEnd: 10, // s
fadeScaling: "linear",
};
const defaultOptions = require('../../config/defaults').plugins.crossfade;

const getStreamURL = async (videoID) => {
const url = await ipcRenderer.invoke("audio-url", videoID);
Expand Down Expand Up @@ -68,8 +62,8 @@ const createAudioForCrossfade = async (url) => {
const syncVideoWithTransitionAudio = async () => {
const video = document.querySelector("video");
const videoFader = new VolumeFader(video, {
fadeScaling: crossfadeOptions.fadeScaling,
fadeDuration: crossfadeOptions.fadeInDuration,
fadeScaling: defaultOptions.fadeScaling,
fadeDuration: defaultOptions.fadeInDuration,
});

await transitionAudio.play();
Expand All @@ -95,7 +89,7 @@ const syncVideoWithTransitionAudio = async () => {
const transitionBeforeEnd = () => {
if (
video.currentTime >=
video.duration - crossfadeOptions.exitMusicBeforeEnd &&
video.duration - defaultOptions.exitMusicBeforeEnd &&
isReadyToCrossfade()
) {
video.removeEventListener("timeupdate", transitionBeforeEnd);
Expand Down Expand Up @@ -130,8 +124,8 @@ const crossfade = (cb) => {

const fader = new VolumeFader(transitionAudio._sounds[0]._node, {
initialVolume: video.volume,
fadeScaling: crossfadeOptions.fadeScaling,
fadeDuration: crossfadeOptions.fadeOutDuration,
fadeScaling: defaultOptions.fadeScaling,
fadeDuration: defaultOptions.fadeOutDuration,
});

// Fade out the music
Expand All @@ -143,10 +137,7 @@ const crossfade = (cb) => {
};

module.exports = (options) => {
crossfadeOptions = {
...crossfadeOptions,
options,
};
Object.assign(defaultOptions, options);

document.addEventListener("apiLoaded", onApiLoaded, {
once: true,
Expand Down
73 changes: 73 additions & 0 deletions plugins/crossfade/menu.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
const { setOptions } = require("../../config/plugins");
const defaultOptions = require("../../config/defaults").plugins.crossfade;

const prompt = require("custom-electron-prompt");
const promptOptions = require("../../providers/prompt-options");

module.exports = (win, options) => [
{
label: "Advanced",
click: async () => {
const newOptions = await promptCrossfadeValues(win, options);
setOptions("crossfade", { ...options, ...newOptions });
},
},
];

async function promptCrossfadeValues(win, options) {
const res = await prompt(
{
title: "Crossfade Options",
label: "",
type: "multiInput",
multiInputOptions: [
{
label: "Fade in duration (ms)",
value: options.fadeInDuration || defaultOptions.fadeInDuration,
inputAttrs: {
type: "number",
required: true,
min: 0,
step: 100,
},
},
{
label: "Fade out duration (ms)",
value: options.fadeOutDuration || defaultOptions.fadeOutDuration,
inputAttrs: {
type: "number",
required: true,
min: 0,
step: 100,
},
},
{
label: "Crossfade x seconds before end",
value:
options.exitMusicBeforeEnd || defaultOptions.exitMusicBeforeEnd,
inputAttrs: {
type: "number",
required: true,
min: 0,
},
},
{
label: "Fade scaling",
selectOptions: { linear: "Linear", exponential: "Exponential" },
value: options.fadeScaling || defaultOptions.fadeScaling,
},
],
resizable: true,
height: 355,
...promptOptions(),
},
win,
).catch(console.error);
if (!res) return undefined;
return {
fadeInDuration: res[0],
fadeOutDuration: res[1],
exitMusicBeforeEnd: res[2],
fadeScaling: res[3],
};
}
10 changes: 5 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2425,12 +2425,12 @@ __metadata:
languageName: node
linkType: hard

"custom-electron-prompt@npm:^1.5.1":
version: 1.5.1
resolution: "custom-electron-prompt@npm:1.5.1"
"custom-electron-prompt@npm:^1.5.4":
version: 1.5.4
resolution: "custom-electron-prompt@npm:1.5.4"
peerDependencies:
electron: ">=10.0.0"
checksum: 43a0d72a7a3471135822cb210d580285f70080d9d3a7b03f82cd4be403059fe20ea05ebdd1f9534928c386ab25a353e678f2cfb3f4ca016b41f3366bff700767
checksum: 93995b5f0e9d14401a8c4fdd358af32d8b7585b59b111667cfa55f9505109c08914f3140953125b854e5d09e811de8c76c7fec718934c13e8a1ad09fe1b85270
languageName: node
linkType: hard

Expand Down Expand Up @@ -8981,7 +8981,7 @@ __metadata:
butterchurn: ^2.6.7
butterchurn-presets: ^2.4.7
chokidar: ^3.5.3
custom-electron-prompt: ^1.5.1
custom-electron-prompt: ^1.5.4
custom-electron-titlebar: ^4.1.6
del-cli: ^5.0.0
electron: ^22.0.2
Expand Down