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

Support changing playback rate #275

Merged
merged 4 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/renderer/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ export const App = () => {

if (!isRunning) {
const extraParameters = useSettingsStore.getState().playback.mpvExtraParameters;
const properties = {
const properties: Record<string, any> = {
speed: usePlayerStore.getState().current.speed,
...getMpvProperties(useSettingsStore.getState().playback.mpvProperties),
};

Expand Down
4 changes: 4 additions & 0 deletions src/renderer/components/audio-player/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import { useSettingsStore } from '/@/renderer/store/settings.store';
import type { CrossfadeStyle } from '/@/renderer/types';
import { PlaybackStyle, PlayerStatus } from '/@/renderer/types';
import { useSpeed } from '/@/renderer/store';

interface AudioPlayerProps extends ReactPlayerProps {
crossfadeDuration: number;
Expand Down Expand Up @@ -59,6 +60,7 @@ export const AudioPlayer = forwardRef(
const [isTransitioning, setIsTransitioning] = useState(false);
const audioDeviceId = useSettingsStore((state) => state.playback.audioDeviceId);
const playback = useSettingsStore((state) => state.playback.mpvProperties);
const playbackSpeed = useSpeed();

const [webAudio, setWebAudio] = useState<WebAudio | null>(null);
const [player1Source, setPlayer1Source] = useState<MediaElementAudioSourceNode | null>(
Expand Down Expand Up @@ -307,6 +309,7 @@ export const AudioPlayer = forwardRef(
}}
height={0}
muted={muted}
playbackRate={playbackSpeed}
playing={currentPlayer === 1 && status === PlayerStatus.PLAYING}
progressInterval={isTransitioning ? 10 : 250}
url={player1?.streamUrl}
Expand All @@ -325,6 +328,7 @@ export const AudioPlayer = forwardRef(
}}
height={0}
muted={muted}
playbackRate={playbackSpeed}
playing={currentPlayer === 2 && status === PlayerStatus.PLAYING}
progressInterval={isTransitioning ? 10 : 250}
url={player2?.streamUrl}
Expand Down
1 change: 1 addition & 0 deletions src/renderer/features/player/components/player-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ const StyledPlayerButton = styled(UnstyledButton)<StyledPlayerButtonProps>`
}

&:hover {
color: var(--playerbar-btn-fg-hover);
background: var(--playerbar-btn-bg-hover);

svg {
Expand Down
53 changes: 44 additions & 9 deletions src/renderer/features/player/components/right-controls.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,21 @@ import {
useHotkeySettings,
useMuted,
useSidebarStore,
useSpeed,
useVolume,
} from '/@/renderer/store';
import { useRightControls } from '../hooks/use-right-controls';
import { PlayerButton } from './player-button';
import { LibraryItem, ServerType, Song } from '/@/renderer/api/types';
import { useCreateFavorite, useDeleteFavorite, useSetRating } from '/@/renderer/features/shared';
import { Rating } from '/@/renderer/components';
import { DropdownMenu, Rating } from '/@/renderer/components';
import { PlayerbarSlider } from '/@/renderer/features/player/components/playerbar-slider';

const ipc = isElectron() ? window.electron.ipc : null;
const remote = isElectron() ? window.electron.remote : null;

const PLAYBACK_SPEEDS = [0.25, 0.5, 0.75, 1.0, 1.25, 1.5, 1.75, 2.0];

export const RightControls = () => {
const isMinWidth = useMediaQuery('(max-width: 480px)');
const volume = useVolume();
Expand All @@ -38,8 +41,16 @@ export const RightControls = () => {
const { setSideBar } = useAppStoreActions();
const { rightExpanded: isQueueExpanded } = useSidebarStore();
const { bindings } = useHotkeySettings();
const { handleVolumeSlider, handleVolumeWheel, handleMute, handleVolumeDown, handleVolumeUp } =
useRightControls();
const {
handleVolumeSlider,
handleVolumeWheel,
handleMute,
handleVolumeDown,
handleVolumeUp,
handleSpeed,
} = useRightControls();

const speed = useSpeed();

const updateRatingMutation = useSetRating({});
const addToFavoritesMutation = useCreateFavorite({});
Expand Down Expand Up @@ -184,6 +195,28 @@ export const RightControls = () => {
align="center"
spacing="xs"
>
<DropdownMenu>
<DropdownMenu.Target>
<PlayerButton
icon={<>{speed} x</>}
tooltip={{
label: 'Playback speed',
openDelay: 500,
}}
variant="secondary"
/>
</DropdownMenu.Target>
<DropdownMenu.Dropdown>
{PLAYBACK_SPEEDS.map((speed) => (
<DropdownMenu.Item
key={`speed-select-${speed}`}
onClick={() => handleSpeed(Number(speed))}
>
{speed}
</DropdownMenu.Item>
))}
</DropdownMenu.Dropdown>
</DropdownMenu>
<PlayerButton
icon={
currentSong?.userFavorite ? (
Expand All @@ -209,12 +242,14 @@ export const RightControls = () => {
variant="secondary"
onClick={handleToggleFavorite}
/>
<PlayerButton
icon={<HiOutlineQueueList size="1.1rem" />}
tooltip={{ label: 'View queue', openDelay: 500 }}
variant="secondary"
onClick={handleToggleQueue}
/>
{!isMinWidth ? (
<PlayerButton
icon={<HiOutlineQueueList size="1.1rem" />}
tooltip={{ label: 'View queue', openDelay: 500 }}
variant="secondary"
onClick={handleToggleQueue}
/>
) : null}
<Group
noWrap
spacing="xs"
Expand Down
22 changes: 21 additions & 1 deletion src/renderer/features/player/hooks/use-right-controls.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { useCallback, useEffect, WheelEvent } from 'react';
import isElectron from 'is-electron';
import { useMuted, usePlayerControls, useVolume } from '/@/renderer/store';
import {
useMuted,
usePlayerControls,
useSetCurrentSpeed,
useSpeed,
useVolume,
} from '/@/renderer/store';
import { useGeneralSettings } from '/@/renderer/store/settings.store';

const mpvPlayer = isElectron() ? window.electron.mpvPlayer : null;
Expand Down Expand Up @@ -37,13 +43,16 @@ export const useRightControls = () => {
const volume = useVolume();
const muted = useMuted();
const { volumeWheelStep } = useGeneralSettings();
const speed = useSpeed();
const setCurrentSpeed = useSetCurrentSpeed();

// Ensure that the mpv player volume is set on startup
useEffect(() => {
remote?.updateVolume(volume);

if (mpvPlayer) {
mpvPlayer.volume(volume);
mpvPlayer.setProperties({ speed });

if (muted) {
mpvPlayer.mute(muted);
Expand All @@ -53,6 +62,16 @@ export const useRightControls = () => {
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

const handleSpeed = useCallback(
(e: number) => {
setCurrentSpeed(e);
if (mpvPlayer) {
mpvPlayer?.setProperties({ speed: e });
}
},
[setCurrentSpeed],
);

const handleVolumeSlider = (e: number) => {
mpvPlayer?.volume(e);
remote?.updateVolume(e);
Expand Down Expand Up @@ -123,6 +142,7 @@ export const useRightControls = () => {

return {
handleMute,
handleSpeed,
handleVolumeDown,
handleVolumeSlider,
handleVolumeSliderState,
Expand Down
12 changes: 12 additions & 0 deletions src/renderer/store/player.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface PlayerState {
seek: boolean;
shuffledIndex: number;
song?: QueueSong;
speed: number;
status: PlayerStatus;
time: number;
};
Expand Down Expand Up @@ -81,6 +82,7 @@ export interface PlayerSlice extends PlayerState {
reorderQueue: (rowUniqueIds: string[], afterUniqueId?: string) => PlayerData;
restoreQueue: (data: Partial<PlayerState>) => PlayerData;
setCurrentIndex: (index: number) => PlayerData;
setCurrentSpeed: (speed: number) => void;
setCurrentTime: (time: number, seek?: boolean) => void;
setCurrentTrack: (uniqueId: string) => PlayerData;
setFavorite: (ids: string[], favorite: boolean) => string[];
Expand Down Expand Up @@ -739,6 +741,11 @@ export const usePlayerStore = create<PlayerSlice>()(

return get().actions.getPlayerData();
},
setCurrentSpeed: (speed) => {
set((state) => {
state.current.speed = speed;
});
},
setCurrentTime: (time, seek = false) => {
set((state) => {
state.current.seek = seek;
Expand Down Expand Up @@ -913,6 +920,7 @@ export const usePlayerStore = create<PlayerSlice>()(
seek: false,
shuffledIndex: 0,
song: {} as QueueSong,
speed: 1.0,
status: PlayerStatus.PAUSED,
time: 0,
},
Expand Down Expand Up @@ -1027,6 +1035,10 @@ export const useVolume = () => usePlayerStore((state) => state.volume);

export const useMuted = () => usePlayerStore((state) => state.muted);

export const useSpeed = () => usePlayerStore((state) => state.current.speed);

export const useSetCurrentSpeed = () => usePlayerStore((state) => state.actions.setCurrentSpeed);

export const useSetQueueFavorite = () => usePlayerStore((state) => state.actions.setFavorite);

export const useSetQueueRating = () => usePlayerStore((state) => state.actions.setRating);
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/themes/default.scss
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
--input-placeholder-fg: rgb(107, 108, 109);
--input-active-fg: rgb(193, 193, 193);
--input-active-bg: rgba(255, 255, 255, 10%);
--dropdown-menu-bg: rgb(32, 32, 32);
--dropdown-menu-bg: rgba(32, 32, 32, 95%);
--dropdown-menu-fg: rgb(235, 235, 235);
--dropdown-menu-item-padding: 0.8rem;
--dropdown-menu-item-font-size: 1rem;
Expand Down