Skip to content

Commit

Permalink
Add TrainerDisplay component and improve code quality
Browse files Browse the repository at this point in the history
  • Loading branch information
ZaneH committed Jul 4, 2022
1 parent 287af31 commit 50883c8
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 44 deletions.
3 changes: 2 additions & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import Keyboard from './components/Keyboard'
import { TrainerPiano } from './components/Trainer'
import { TrainerDisplay, TrainerPiano } from './components/Trainer'
import TrainerProvider from './components/TrainerProvider'

function App() {
return (
<div className='App'>
<TrainerProvider>
<TrainerPiano />
<TrainerDisplay />
<Keyboard />
</TrainerProvider>
</div>
Expand Down
55 changes: 31 additions & 24 deletions src/components/Keyboard/Keyboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@ import { listen } from '@tauri-apps/api/event'
import { useCallback, useContext, useEffect, useState } from 'react'
import { KeyboardShortcuts, MidiNumbers, Piano } from 'react-piano'
import 'react-piano/dist/styles.css'
import styled from 'styled-components'
import SoundfontProvider from '../SoundfontProvider'
import { TrainerContext } from '../TrainerProvider'

const KeyboardContainer = styled.div`
height: 25vh;
`

const Keyboard = () => {
const { nextTargetNote, setNoteCounter } = useContext(TrainerContext)
const [activeNotes, setActiveNotes] = useState<{ [note: string]: boolean }>(
Expand Down Expand Up @@ -61,31 +66,33 @@ const Keyboard = () => {
onLoad={() => {}}
render={({ playNote, stopNote }) => {
return (
<Piano
noteRange={{ first: firstNote, last: lastNote }}
playNote={(midiNumber: number) => {
if (midiNumber === nextTargetNote) {
setNoteCounter?.((nc) => nc + 1)
}
<KeyboardContainer>
<Piano
noteRange={{ first: firstNote, last: lastNote }}
playNote={(midiNumber: number) => {
if (midiNumber === nextTargetNote) {
setNoteCounter?.((nc) => nc + 1)
}

playNote(midiNumber)
}}
stopNote={(midiNumber: number) => {
stopNote(midiNumber)
}}
keyboardShortcuts={keyboardShortcuts}
renderNoteLabel={({ midiNumber }: { midiNumber: number }) => (
<p className='ReactPiano__NoteLabel'>
{MidiNumbers.getAttributes(midiNumber).note.replace(
/[0-9]/,
''
)}
</p>
)}
activeNotes={Object.keys(activeNotes)
.filter((v: string) => activeNotes[v])
.map((s: string) => Number(s))}
/>
playNote(midiNumber)
}}
stopNote={(midiNumber: number) => {
stopNote(midiNumber)
}}
keyboardShortcuts={keyboardShortcuts}
renderNoteLabel={({ midiNumber }: { midiNumber: number }) => (
<p className='ReactPiano__NoteLabel'>
{MidiNumbers.getAttributes(midiNumber).note.replace(
/[0-9]/,
''
)}
</p>
)}
activeNotes={Object.keys(activeNotes)
.filter((v: string) => activeNotes[v])
.map((s: string) => Number(s))}
/>
</KeyboardContainer>
)
}}
/>
Expand Down
9 changes: 9 additions & 0 deletions src/components/Trainer/TrainerDisplay.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { useContext } from 'react'
import { TrainerContext } from '../TrainerProvider'

const TrainerDisplay = () => {
const { scale } = useContext(TrainerContext)
return <h1>{scale?.name}</h1>
}

export default TrainerDisplay
20 changes: 12 additions & 8 deletions src/components/Trainer/TrainerPiano.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import { ignoreOctave } from '../../utils'
import { TrainerContext } from '../TrainerProvider'

const PianoContainer = styled.div`
height: 50vh;
width: 60%;
height: 35vh;
`

const InKeyMarker = styled.div`
Expand All @@ -31,19 +30,24 @@ const TrainerPiano = () => {
<Keyboard
noteRange={{
first: MidiNumbers.fromNote('c3'),
last: MidiNumbers.fromNote('g4'),
last: MidiNumbers.fromNote('c5'),
}}
activeNotes={[nextTargetNote]}
onPlayNoteInput={() => {}}
onStopNoteInput={() => {}}
keyWidthToHeight={0.33}
renderNoteLabel={({ midiNumber }: { midiNumber: number }) => {
const modScale = ignoreOctave(scale || [])
return (
modScale[midiNumber % 12] && (
<InKeyMarker>{modScale[midiNumber % 12]}</InKeyMarker>
const isMidiNumbers = false
const modScale = ignoreOctave(scale || {})
if (isMidiNumbers) {
return <InKeyMarker>{midiNumber}</InKeyMarker>
} else {
return (
modScale[midiNumber % 12] && (
<InKeyMarker>{modScale[midiNumber % 12]}</InKeyMarker>
)
)
)
}
}}
/>
</PianoContainer>
Expand Down
1 change: 1 addition & 0 deletions src/components/Trainer/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { default as TrainerPiano } from './TrainerPiano'
export { default as TrainerDisplay } from './TrainerDisplay'
14 changes: 5 additions & 9 deletions src/index.css
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
html {
height: 100vh;
min-height: 100vh;
margin: 0 auto;
}

.ReactPiano__Keyboard {
height: 48vh !important;
height: 100vh;
min-height: 100vh;
margin: 0 auto;
}

.ReactPiano__NoteLabel {
font-family: Arial, Helvetica, sans-serif;
}
font-family: Arial, Helvetica, sans-serif;
}
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export type ScaleType = {
name?: string
[midi: number]: string
}

Expand Down
77 changes: 75 additions & 2 deletions src/utils/scales/Majors.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ScaleType } from '..'

const CMajor: ScaleType = {
name: 'C Major',
48: 'I',
50: 'ii',
52: 'iii',
Expand All @@ -9,7 +10,79 @@ const CMajor: ScaleType = {
57: 'vi',
59: 'viiº',
60: 'I',
}
} as const

const DMajor: ScaleType = {
name: 'D Major',
50: 'I',
52: 'ii',
54: 'iii',
55: 'IV',
57: 'V',
59: 'vi',
61: 'viiº',
62: 'I',
} as const

const EMajor: ScaleType = {
name: 'E Major',
52: 'I',
54: 'ii',
56: 'iii',
57: 'IV',
59: 'V',
61: 'vi',
63: 'viiº',
64: 'I',
} as const

const FMajor: ScaleType = {
name: 'F Major',
53: 'I',
55: 'ii',
57: 'iii',
58: 'IV',
60: 'V',
62: 'vi',
64: 'viiº',
65: 'I',
} as const

const GMajor: ScaleType = {
name: 'G Major',
55: 'I',
57: 'ii',
59: 'iii',
60: 'IV',
62: 'V',
64: 'vi',
66: 'viiº',
67: 'I',
} as const

const AMajor: ScaleType = {
name: 'A Major',
57: 'I',
59: 'ii',
61: 'iii',
62: 'IV',
64: 'V',
66: 'vi',
68: 'viiº',
69: 'I',
} as const

const BMajor: ScaleType = {
name: 'B Major',
59: 'I',
61: 'ii',
63: 'iii',
64: 'IV',
66: 'V',
68: 'vi',
70: 'viiº',
71: 'I',
} as const

const ignoreOctave = (scale: ScaleType): ScaleType => {
const scaleKeys: string[] = Object.keys(scale)
Expand All @@ -22,4 +95,4 @@ const ignoreOctave = (scale: ScaleType): ScaleType => {
return modKeys
}

export { CMajor, ignoreOctave }
export { CMajor, DMajor, EMajor, FMajor, GMajor, AMajor, BMajor, ignoreOctave }

0 comments on commit 50883c8

Please sign in to comment.