diff --git a/front/Navigation.tsx b/front/Navigation.tsx index 71efac4..0869719 100644 --- a/front/Navigation.tsx +++ b/front/Navigation.tsx @@ -16,6 +16,7 @@ import { Center } from 'native-base'; import LoadingComponent from './components/Loading'; import ProfileView from './views/ProfileView'; import useColorScheme from './hooks/colorScheme'; +import VirtualPiano from './components/VirtualPiano/VirtualPiano'; const Stack = createNativeStackNavigator(); @@ -41,6 +42,8 @@ export const Router = () => { }); const colorScheme = useColorScheme(); + return + return ( = ["C", "D", "E", "F", "G", "A", "B"]; +const accidentalsList: Array = ["#", "b", "##", "bb"]; + +const getKeyIndex = (k: PianoKey, keys: PianoKey[]) => { + for (let i = 0; i < keys.length; i++) { + if ( + keys[i]?.note === k.note && + ((keys[i]?.accidental && keys[i]?.accidental === k.accidental) || + (!keys[i]?.accidental && !k.accidental)) + ) { + return i; + } + } + return -1; +}; + type OctaveProps = { number: number; - startNote: Note; - endNote: Note; - onNoteDown: (note: Note) => void; - onNoteUp: (note: Note) => void; + startNote: PianoKey; + endNote: PianoKey; + onNoteDown: (note: PianoKey) => void; + onNoteUp: (note: PianoKey) => void; }; const Octave = ({ @@ -16,28 +38,31 @@ const Octave = ({ onNoteDown, onNoteUp, }: OctaveProps) => { - const notesList: Array = ["C", "D", "E", "F", "G", "A", "B"]; - const startNoteIndex = notesList.indexOf(startNote); - const endNoteIndex = notesList.indexOf(endNote); - const whiteKeys = notesList.slice(startNoteIndex, endNoteIndex + 1); + const oK: PianoKey[] = octaveKeys.map((k) => { + return { ...k, number: number }; + }); + + const startNoteIndex = getKeyIndex(startNote, oK); + const endNoteIndex = getKeyIndex(endNote, oK); + const keys = oK.slice(startNoteIndex, endNoteIndex + 1); return ( - {whiteKeys.map((note) => { + {keys.map((key, i) => { return ( onNoteDown(note + number)} - onPressOut={() => onNoteUp(note + number)} + onPressIn={() => onNoteDown(key)} + onPressOut={() => onNoteUp(key)} > - {note} + {key.toString()} ); diff --git a/front/components/VirtualPiano/VirtualPiano.tsx b/front/components/VirtualPiano/VirtualPiano.tsx index 37ecf30..93922f2 100644 --- a/front/components/VirtualPiano/VirtualPiano.tsx +++ b/front/components/VirtualPiano/VirtualPiano.tsx @@ -1,30 +1,19 @@ import { Row, Box } from "native-base"; import React, { useState, useEffect } from "react"; import Octave from "./Octave"; - -export type Note = "C" | "D" | "E" | "F" | "G" | "A" | "B"; -export type Accidental = "#" | "b" | "##" | "bb"; - -export type NoteValue = { - note: Note; - accidental?: Accidental; - octave?: number; -}; - -export type NoteNameBehavior = "always" | "onpress" | "onhighlight" | "never"; -export type KeyPressStyle = "subtle" | "vivid"; +import { Note, PianoKey, NoteNameBehavior, KeyPressStyle, octaveKeys } from "../../models/Piano"; type VirtualPianoProps = Parameters[0] & { - onNoteDown: (note: Note) => void; - onNoteUp: (note: Note) => void; + onNoteDown: (note: PianoKey) => void; + onNoteUp: (note: PianoKey) => void; startOctave: number; - startNote: Note; + startNote: PianoKey; endOctave: number; - endNote: Note; + endNote: PianoKey; showNoteNames: NoteNameBehavior; // default "onpress" - highlightedNotes: Array; + highlightedNotes: Array; highlightColor: string; - specialHighlightedNotes: Array; + specialHighlightedNotes: Array; specialHighlightColor: string; showOctaveNumbers: boolean; keyPressStyle: KeyPressStyle; @@ -47,20 +36,12 @@ const VirtualPiano = ({ keyPressStyle, vividKeyPressColor, }: VirtualPianoProps) => { - const notesList: Array = ["C", "D", "E", "F", "G", "A", "B"]; - const nbOctaves = endOctave - startOctave; - const nbWhiteKeys = - notesList.length * (endOctave - startOctave) - - notesList.indexOf(startNote) - - (notesList.length - notesList.indexOf(endNote) + 1); const octaveList = []; for (let octaveNum = startOctave; octaveNum <= endOctave; octaveNum++) { octaveList.push(octaveNum); }; - - return ( {octaveList.map((octaveNum) => { @@ -68,8 +49,8 @@ const VirtualPiano = ({ @@ -87,9 +68,9 @@ VirtualPiano.defaultProps = { console.log("Note up: " + n); }, startOctave: 2, - startNote: "C", + startNote: new PianoKey("C"), endOctave: 6, - endNote: "C", + endNote: new PianoKey("C"), showNoteNames: "onpress", highlightedNotes: [], highlightColor: "red", diff --git a/front/models/Piano.ts b/front/models/Piano.ts new file mode 100644 index 0000000..cfafb26 --- /dev/null +++ b/front/models/Piano.ts @@ -0,0 +1,38 @@ + + +export type Note = "C" | "D" | "E" | "F" | "G" | "A" | "B"; +export type Accidental = "#" | "b" | "##" | "bb"; + +export type NoteNameBehavior = "always" | "onpress" | "onhighlight" | "never"; +export type KeyPressStyle = "subtle" | "vivid"; + +export class PianoKey { + public note: Note; + public accidental?: Accidental; + public octave?: number; + + constructor(note: Note, accidental?: Accidental, octave?: number) { + this.note = note; + this.accidental = accidental; + this.octave = octave; + }; + + public toString = () => { + return this.note + (this.accidental || "") + (this.octave || ""); + } +}; + +export const octaveKeys: Array = [ + new PianoKey("C", undefined), + new PianoKey("C", "#"), + new PianoKey("D", undefined), + new PianoKey("D", "#"), + new PianoKey("E", undefined), + new PianoKey("F", undefined), + new PianoKey("F", "#"), + new PianoKey("G", undefined), + new PianoKey("G", "#"), + new PianoKey("A", undefined), + new PianoKey("A", "#"), + new PianoKey("B", undefined), +]; \ No newline at end of file