|
1 | | -const { ipcRenderer } = require("electron"); |
2 | | -const { Howl } = require("howler"); |
| 1 | +const { ipcRenderer } = require('electron'); |
| 2 | +const { Howl } = require('howler'); |
3 | 3 |
|
4 | 4 | // Extracted from https://github.com/bitfasching/VolumeFader |
5 | | -require("./fader"); |
| 5 | +require('./fader'); |
6 | 6 |
|
7 | 7 | let transitionAudio; // Howler audio used to fade out the current music |
8 | 8 | let firstVideo = true; |
9 | 9 | let waitForTransition; |
10 | 10 |
|
11 | | -const config = require('../../config/defaults').plugins.crossfade; |
| 11 | +const defaultConfig = require('../../config/defaults').plugins.crossfade; |
| 12 | + |
| 13 | +const configProvider = require('./config'); |
| 14 | +let config = configProvider.getAll(); |
| 15 | + |
| 16 | +console.log({ config }); |
| 17 | + |
| 18 | +configProvider.subscribeAll((newConfig) => { |
| 19 | + config = newConfig; |
| 20 | +}); |
| 21 | + |
| 22 | +const configGetNum = (key) => Number(config[key]) || defaultConfig[key]; |
12 | 23 |
|
13 | 24 | const getStreamURL = async (videoID) => { |
14 | | - const url = await ipcRenderer.invoke("audio-url", videoID); |
15 | | - return url; |
| 25 | + const url = await ipcRenderer.invoke('audio-url', videoID); |
| 26 | + return url; |
16 | 27 | }; |
17 | 28 |
|
18 | 29 | const getVideoIDFromURL = (url) => { |
19 | | - return new URLSearchParams(url.split("?")?.at(-1)).get("v"); |
| 30 | + return new URLSearchParams(url.split('?')?.at(-1)).get('v'); |
20 | 31 | }; |
21 | 32 |
|
22 | 33 | const isReadyToCrossfade = () => { |
23 | | - return transitionAudio && transitionAudio.state() === "loaded"; |
| 34 | + return transitionAudio && transitionAudio.state() === 'loaded'; |
24 | 35 | }; |
25 | 36 |
|
26 | 37 | const watchVideoIDChanges = (cb) => { |
27 | | - navigation.addEventListener("navigate", (event) => { |
28 | | - const currentVideoID = getVideoIDFromURL( |
29 | | - event.currentTarget.currentEntry.url |
30 | | - ); |
31 | | - const nextVideoID = getVideoIDFromURL(event.destination.url); |
32 | | - |
33 | | - if ( |
34 | | - nextVideoID && |
35 | | - currentVideoID && |
36 | | - (firstVideo || nextVideoID !== currentVideoID) |
37 | | - ) { |
38 | | - if (isReadyToCrossfade()) { |
39 | | - crossfade(() => { |
40 | | - cb(nextVideoID); |
41 | | - }); |
42 | | - } else { |
43 | | - cb(nextVideoID); |
44 | | - firstVideo = false; |
45 | | - } |
46 | | - } |
47 | | - }); |
| 38 | + navigation.addEventListener('navigate', (event) => { |
| 39 | + const currentVideoID = getVideoIDFromURL( |
| 40 | + event.currentTarget.currentEntry.url, |
| 41 | + ); |
| 42 | + const nextVideoID = getVideoIDFromURL(event.destination.url); |
| 43 | + |
| 44 | + if ( |
| 45 | + nextVideoID && |
| 46 | + currentVideoID && |
| 47 | + (firstVideo || nextVideoID !== currentVideoID) |
| 48 | + ) { |
| 49 | + if (isReadyToCrossfade()) { |
| 50 | + crossfade(() => { |
| 51 | + cb(nextVideoID); |
| 52 | + }); |
| 53 | + } else { |
| 54 | + cb(nextVideoID); |
| 55 | + firstVideo = false; |
| 56 | + } |
| 57 | + } |
| 58 | + }); |
48 | 59 | }; |
49 | 60 |
|
50 | 61 | const createAudioForCrossfade = async (url) => { |
51 | | - if (transitionAudio) { |
52 | | - transitionAudio.unload(); |
53 | | - } |
54 | | - transitionAudio = new Howl({ |
55 | | - src: url, |
56 | | - html5: true, |
57 | | - volume: 0, |
58 | | - }); |
59 | | - await syncVideoWithTransitionAudio(); |
| 62 | + if (transitionAudio) { |
| 63 | + transitionAudio.unload(); |
| 64 | + } |
| 65 | + transitionAudio = new Howl({ |
| 66 | + src: url, |
| 67 | + html5: true, |
| 68 | + volume: 0, |
| 69 | + }); |
| 70 | + await syncVideoWithTransitionAudio(); |
60 | 71 | }; |
61 | 72 |
|
62 | 73 | const syncVideoWithTransitionAudio = async () => { |
63 | | - const video = document.querySelector("video"); |
64 | | - const videoFader = new VolumeFader(video, { |
65 | | - fadeScaling: config.fadeScaling, |
66 | | - fadeDuration: config.fadeInDuration, |
67 | | - }); |
68 | | - |
69 | | - await transitionAudio.play(); |
70 | | - await transitionAudio.seek(video.currentTime); |
71 | | - |
72 | | - video.onseeking = () => { |
73 | | - transitionAudio.seek(video.currentTime); |
74 | | - }; |
75 | | - video.onpause = () => { |
76 | | - transitionAudio.pause(); |
77 | | - }; |
78 | | - video.onplay = async () => { |
79 | | - await transitionAudio.play(); |
80 | | - await transitionAudio.seek(video.currentTime); |
81 | | - |
82 | | - // Fade in |
83 | | - const videoVolume = video.volume; |
84 | | - video.volume = 0; |
85 | | - videoFader.fadeTo(videoVolume); |
86 | | - }; |
87 | | - |
88 | | - // Exit just before the end for the transition |
89 | | - const transitionBeforeEnd = () => { |
90 | | - if ( |
91 | | - video.currentTime >= |
92 | | - video.duration - config.exitMusicBeforeEnd && |
93 | | - isReadyToCrossfade() |
94 | | - ) { |
95 | | - video.removeEventListener("timeupdate", transitionBeforeEnd); |
96 | | - |
97 | | - // Go to next video - XXX: does not support "repeat 1" mode |
98 | | - document.querySelector(".next-button").click(); |
99 | | - } |
100 | | - }; |
101 | | - video.ontimeupdate = transitionBeforeEnd; |
| 74 | + const video = document.querySelector('video'); |
| 75 | + |
| 76 | + const videoFader = new VolumeFader(video, { |
| 77 | + fadeScaling: configGetNum('fadeScaling'), |
| 78 | + fadeDuration: configGetNum('fadeInDuration'), |
| 79 | + }); |
| 80 | + |
| 81 | + await transitionAudio.play(); |
| 82 | + await transitionAudio.seek(video.currentTime); |
| 83 | + |
| 84 | + video.onseeking = () => { |
| 85 | + transitionAudio.seek(video.currentTime); |
| 86 | + }; |
| 87 | + video.onpause = () => { |
| 88 | + transitionAudio.pause(); |
| 89 | + }; |
| 90 | + video.onplay = async () => { |
| 91 | + await transitionAudio.play(); |
| 92 | + await transitionAudio.seek(video.currentTime); |
| 93 | + |
| 94 | + // Fade in |
| 95 | + const videoVolume = video.volume; |
| 96 | + video.volume = 0; |
| 97 | + videoFader.fadeTo(videoVolume); |
| 98 | + }; |
| 99 | + |
| 100 | + // Exit just before the end for the transition |
| 101 | + const transitionBeforeEnd = () => { |
| 102 | + if ( |
| 103 | + video.currentTime >= |
| 104 | + video.duration - configGetNum('secondsBeforeEnd') && |
| 105 | + isReadyToCrossfade() |
| 106 | + ) { |
| 107 | + video.removeEventListener('timeupdate', transitionBeforeEnd); |
| 108 | + |
| 109 | + // Go to next video - XXX: does not support "repeat 1" mode |
| 110 | + document.querySelector('.next-button').click(); |
| 111 | + } |
| 112 | + }; |
| 113 | + video.ontimeupdate = transitionBeforeEnd; |
102 | 114 | }; |
103 | 115 |
|
104 | 116 | const onApiLoaded = () => { |
105 | | - watchVideoIDChanges(async (videoID) => { |
106 | | - await waitForTransition; |
107 | | - const url = await getStreamURL(videoID); |
108 | | - await createAudioForCrossfade(url); |
109 | | - }); |
| 117 | + watchVideoIDChanges(async (videoID) => { |
| 118 | + await waitForTransition; |
| 119 | + const url = await getStreamURL(videoID); |
| 120 | + await createAudioForCrossfade(url); |
| 121 | + }); |
110 | 122 | }; |
111 | 123 |
|
112 | | -const crossfade = (cb) => { |
113 | | - if (!isReadyToCrossfade()) { |
114 | | - cb(); |
115 | | - return; |
116 | | - } |
117 | | - |
118 | | - let resolveTransition; |
119 | | - waitForTransition = new Promise(function (resolve, reject) { |
120 | | - resolveTransition = resolve; |
121 | | - }); |
122 | | - |
123 | | - const video = document.querySelector("video"); |
124 | | - |
125 | | - const fader = new VolumeFader(transitionAudio._sounds[0]._node, { |
126 | | - initialVolume: video.volume, |
127 | | - fadeScaling: config.fadeScaling, |
128 | | - fadeDuration: config.fadeOutDuration, |
129 | | - }); |
130 | | - |
131 | | - // Fade out the music |
132 | | - video.volume = 0; |
133 | | - fader.fadeOut(() => { |
134 | | - resolveTransition(); |
135 | | - cb(); |
136 | | - }); |
| 124 | +const crossfade = async (cb) => { |
| 125 | + if (!isReadyToCrossfade()) { |
| 126 | + cb(); |
| 127 | + return; |
| 128 | + } |
| 129 | + |
| 130 | + let resolveTransition; |
| 131 | + waitForTransition = new Promise(function (resolve, reject) { |
| 132 | + resolveTransition = resolve; |
| 133 | + }); |
| 134 | + |
| 135 | + const video = document.querySelector('video'); |
| 136 | + |
| 137 | + const fader = new VolumeFader(transitionAudio._sounds[0]._node, { |
| 138 | + initialVolume: video.volume, |
| 139 | + fadeScaling: configGetNum('fadeScaling'), |
| 140 | + fadeDuration: configGetNum('fadeOutDuration'), |
| 141 | + }); |
| 142 | + |
| 143 | + // Fade out the music |
| 144 | + video.volume = 0; |
| 145 | + fader.fadeOut(() => { |
| 146 | + resolveTransition(); |
| 147 | + cb(); |
| 148 | + }); |
137 | 149 | }; |
138 | 150 |
|
139 | | -module.exports = (options) => { |
140 | | - Object.assign(config, options); |
141 | | - |
142 | | - document.addEventListener("apiLoaded", onApiLoaded, { |
143 | | - once: true, |
144 | | - passive: true, |
145 | | - }); |
| 151 | +module.exports = () => { |
| 152 | + document.addEventListener('apiLoaded', onApiLoaded, { |
| 153 | + once: true, |
| 154 | + passive: true, |
| 155 | + }); |
146 | 156 | }; |
0 commit comments