From 31b965e8f609dd18315b1a3626f58a5c9594404a Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Mon, 18 Sep 2023 17:18:01 +0200 Subject: [PATCH] Add volume/enable state and follow the music's bpm for the metronome --- front/components/Metronome.tsx | 33 +++++++++++++++++++++++++---- front/components/PartitionCoord.tsx | 3 +++ front/components/PartitionView.tsx | 4 +++- front/views/PlayView.tsx | 4 +++- 4 files changed, 38 insertions(+), 6 deletions(-) diff --git a/front/components/Metronome.tsx b/front/components/Metronome.tsx index 8e1e7ab..9013cd9 100644 --- a/front/components/Metronome.tsx +++ b/front/components/Metronome.tsx @@ -1,13 +1,38 @@ -import { useEffect, useRef } from 'react'; +import { useEffect, useRef, useState } from 'react'; +import { Slider, Switch, Text, View } from 'native-base'; export const Metronome = ({ paused = false, bpm }: { paused?: boolean; bpm: number }) => { const ref = useRef(null); + const enabled = useRef(false); + const volume = useRef(50); + useEffect(() => { + if (paused) return; const int = setInterval(() => { - if (!ref.current) ref.current = new Audio("/assets/metronome.mp3"); + console.log(enabled.current, volume.current); + if (!enabled.current) return; + if (!ref.current) ref.current = new Audio('/assets/metronome.mp3'); + ref.current.volume = volume.current / 100; ref.current.play(); }, 60000 / bpm); return () => clearInterval(int); - }, []); - return null; + }, [bpm, paused]); + return ( + + Metronome Settings + Enabled: + (enabled.current = !enabled.current)} /> + Volume: + (volume.current = x)} + > + + + + + + + ); }; diff --git a/front/components/PartitionCoord.tsx b/front/components/PartitionCoord.tsx index 60fbace..b0a094d 100644 --- a/front/components/PartitionCoord.tsx +++ b/front/components/PartitionCoord.tsx @@ -6,6 +6,7 @@ import { PianoCursorPosition } from '../models/PianoGame'; type PartitionCoordProps = { // The Buffer of the MusicXML file retreived from the API file: string; + bpmRef: React.MutableRefObject; onPartitionReady: () => void; onEndReached: () => void; onResume: () => void; @@ -18,6 +19,7 @@ const PartitionCoord = ({ onEndReached, onPause, onResume, + bpmRef, }: PartitionCoordProps) => { const [partitionData, setPartitionData] = React.useState< [[number, number], string, PianoCursorPosition[]] | null @@ -28,6 +30,7 @@ const PartitionCoord = ({ {!partitionData && ( { setPartitionData([dims, base64data, a]); onPartitionReady(); diff --git a/front/components/PartitionView.tsx b/front/components/PartitionView.tsx index 8fb7784..009796e 100644 --- a/front/components/PartitionView.tsx +++ b/front/components/PartitionView.tsx @@ -1,7 +1,7 @@ /* eslint-disable no-mixed-spaces-and-tabs */ // Inspired from OSMD example project // https://github.com/opensheetmusicdisplay/react-opensheetmusicdisplay/blob/master/src/lib/OpenSheetMusicDisplay.jsx -import React, { useEffect } from 'react'; +import React, { MutableRefObject, useEffect } from 'react'; import { CursorType, Fraction, @@ -19,6 +19,7 @@ type PartitionViewProps = { base64data: string, cursorInfos: PianoCursorPosition[] ) => void; + bpmRef: MutableRefObject; onEndReached: () => void; // Timestamp of the play session, in milisecond timestamp: number; @@ -62,6 +63,7 @@ const PartitionView = (props: PartitionViewProps) => { _osmd.render(); _osmd.cursor.show(); const bpm = _osmd.Sheet.HasBPMInfo ? _osmd.Sheet.getExpressionsStartTempoInBPM() : 60; + props.bpmRef.current = bpm; const wholeNoteLength = Math.round((60 / bpm) * 4000); const curPos = []; while (!_osmd.cursor.iterator.EndReached) { diff --git a/front/views/PlayView.tsx b/front/views/PlayView.tsx index 962e05b..92c9fd2 100644 --- a/front/views/PlayView.tsx +++ b/front/views/PlayView.tsx @@ -84,6 +84,7 @@ const PlayView = ({ songId, type, route }: RouteProps) => { const toast = useToast(); const [lastScoreMessage, setLastScoreMessage] = useState(); const webSocket = useRef(); + const bpm = useRef(60); const [paused, setPause] = useState(true); const stopwatch = useStopwatch(); const [time, setTime] = useState(0); @@ -349,6 +350,7 @@ const PlayView = ({ songId, type, route }: RouteProps) => { > ) => { {!partitionRendered && } - +