Added state for metronome and fixed onEndReached for mobile

This commit is contained in:
Clément Le Bihan
2023-11-30 00:27:50 +01:00
parent a4a10eb7f2
commit 0cb8dd2693
2 changed files with 35 additions and 25 deletions

View File

@@ -1,4 +1,4 @@
import { useEffect, useRef } from 'react';
import { useEffect, useRef, useState } from 'react';
import { Slider, Text, View, IconButton, Icon } from 'native-base';
import { MaterialCommunityIcons } from '@expo/vector-icons';
import { Audio } from 'expo-av';
@@ -6,7 +6,7 @@ import { VolumeHigh, VolumeSlash } from 'iconsax-react-native';
export const MetronomeControls = ({ paused = false, bpm }: { paused?: boolean; bpm: number }) => {
const audio = useRef<Audio.Sound | null>(null);
const enabled = useRef<boolean>(false);
const [enabled, setEnabled] = useState<boolean>(false);
const volume = useRef<number>(50);
useEffect(() => {
@@ -25,7 +25,7 @@ export const MetronomeControls = ({ paused = false, bpm }: { paused?: boolean; b
useEffect(() => {
if (paused) return;
const int = setInterval(() => {
if (!enabled.current) return;
if (!enabled) return;
if (!audio.current) return;
audio.current?.playAsync();
}, 60000 / bpm);
@@ -60,7 +60,7 @@ export const MetronomeControls = ({ paused = false, bpm }: { paused?: boolean; b
<VolumeHigh size={24} color="white" />
)
}
onPress={() => (enabled.current = !enabled.current)}
onPress={() => setEnabled(!enabled)}
/>
<Slider
maxWidth={'500px'}

View File

@@ -41,15 +41,16 @@ const getCursorToPlay = (
const PartitionMagic = ({ songID, onEndReached, onError, onReady }: ParitionMagicProps) => {
const { data, isLoading, isError } = useQuery(API.getSongCursorInfos(songID));
const [currentCurIdx, setCurrentCurIdx] = React.useState(-1);
const currentCurIdx = React.useRef(-1);
const [endPartitionReached, setEndPartitionReached] = React.useState(false);
const [isPartitionSvgLoaded, setIsPartitionSvgLoaded] = React.useState(false);
const partitionOffset = useSharedValue(0);
const pianoCC = React.useContext(PianoCC);
const pianoSounds = React.useRef<Record<string, Audio.Sound>>();
const pianoSounds = React.useRef<Record<string, Audio.Sound> | null>(null);
const cursorPaddingVertical = 10;
const cursorPaddingHorizontal = 3;
const cursorDisplayIdx = currentCurIdx === -1 ? 0 : currentCurIdx;
const cursorDisplayIdx = currentCurIdx.current === -1 ? 0 : currentCurIdx.current;
const cursorBorderWidth = (data?.cursors[cursorDisplayIdx]?.width ?? 0) / 6;
const cursorWidth = (data?.cursors[cursorDisplayIdx]?.width ?? 0) + cursorPaddingHorizontal * 2;
@@ -57,6 +58,12 @@ const PartitionMagic = ({ songID, onEndReached, onError, onReady }: ParitionMagi
const cursorTop = (data?.cursors[cursorDisplayIdx]?.y ?? 0) - cursorPaddingVertical;
const cursorLeft = (data?.cursors[0]?.x ?? 0) - cursorPaddingHorizontal;
if (!endPartitionReached && currentCurIdx.current + 1 === data?.cursors.length) {
// weird contraption but the mobile don't want classic functions to be called
// with the withTiming function :(
setEndPartitionReached(true);
}
// React.useEffect(() => {
// if (!pianoSounds.current) {
// Promise.all(
@@ -92,37 +99,40 @@ const PartitionMagic = ({ songID, onEndReached, onError, onReady }: ParitionMagi
}
}, [isPartitionSvgLoaded, isLoading]);
React.useEffect(() => {
if (endPartitionReached) {
onEndReached();
}
}, [endPartitionReached]);
const transitionDuration = 200;
getCursorToPlay(
data?.cursors ?? [],
currentCurIdx,
currentCurIdx.current,
pianoCC.timestamp - transitionDuration,
(cursor, idx) => {
// cursor.notes.forEach(({ note, duration }) => {
// try {
// const sound = pianoSounds.current![note]!;
// sound.playAsync().catch(console.error);
// setTimeout(() => {
// sound.stopAsync();
// }, duration - 10);
// } catch (e) {
// console.log(e);
// }
// });
currentCurIdx.current = idx;
if (pianoSounds.current) {
cursor.notes.forEach(({ note, duration }) => {
try {
const sound = pianoSounds.current![note]!;
sound.playAsync().catch(console.error);
setTimeout(() => {
sound.stopAsync();
}, duration - 10);
} catch (e) {
console.log(e);
}
});
}
partitionOffset.value = withTiming(
-(cursor.x - data!.cursors[0]!.x) / partitionDims[0],
{
duration: transitionDuration,
easing: Easing.inOut(Easing.ease),
},
() => {
if (idx === data!.cursors.length - 1) {
onEndReached();
}
}
);
setCurrentCurIdx(idx);
}
);