Skip to content

Commit

Permalink
Implemented seventh chord practice
Browse files Browse the repository at this point in the history
  • Loading branch information
djakhada committed Jul 22, 2022
1 parent 69a243f commit e71896c
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 1 deletion.
21 changes: 21 additions & 0 deletions src/components/Keyboard/Keyboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import styled, { css } from 'styled-components'
import {
getFifthFromMidiNumber,
getTriadChordFromMidiNumber,
getSeventhChordFromMidiNumber,
MidiDevice,
midiNumberToNote,
} from '../../utils'
Expand Down Expand Up @@ -126,6 +127,26 @@ const Keyboard = () => {
// turn the target numbers into target letters to ignore octave for matching
const targetChordNotes = targetChord.map((n) => midiNumberToNote(n))

const matches = targetChordNotes.every((e) =>
chordStack?.map((cs) => midiNumberToNote(cs)).includes(e)
)
if (matches) {
setNoteTracker?.((nt) => ({
...nt,
noteCounter: nt.noteCounter + 1,
currentMidiNumber: targetChord[0],
}))
setChordStack?.([])
}
} else if (practiceMode === 'seventhChords') {
const targetChord = getSeventhChordFromMidiNumber(
noteTracker?.nextTargetMidiNumber!,
scale!
)

// turn the target numbers into target letters to ignore octave for matching
const targetChordNotes = targetChord.map((n) => midiNumberToNote(n))

const matches = targetChordNotes.every((e) =>
chordStack?.map((cs) => midiNumberToNote(cs)).includes(e)
)
Expand Down
7 changes: 7 additions & 0 deletions src/components/Trainer/TrainerPiano.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
AVAILABLE_MAJOR_SCALES,
getFifthFromMidiNumber,
getTriadChordFromMidiNumber,
getSeventhChordFromMidiNumber,
ignoreOctave,
OCTAVE_LENGTH,
} from '../../utils'
Expand Down Expand Up @@ -54,6 +55,12 @@ const TrainerPiano = () => {
} else {
return getTriadChordFromMidiNumber(nextNote, scale)
}
} else if (practiceMode === 'seventhChords') {
if (isHardModeEnabled) {
return getSeventhChordFromMidiNumber(noteTracker!.prevNote!, scale)
} else {
return getSeventhChordFromMidiNumber(nextNote, scale)
}
} else if (practiceMode === 'fifths') {
if (isHardModeEnabled) {
return [
Expand Down
46 changes: 46 additions & 0 deletions src/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,52 @@ export const getTriadChordFromMidiNumber = (
return triadChordMidi
}

/**
* Given a midi number, return four midi numbers that make up a seventh chord with the
* provided scale.
* @param midiNumber A midi number to start the seventh chord from
* @param scale The scale to follow for this chord
* @returns 4 midi numbers in an array that make up a seventh chord starting from midiNumber
*/
export const getSeventhChordFromMidiNumber = (
midiNumber: number,
scale: ScaleType
): number[] => {
const scaleKeys = Object.keys(scale.keys)
const firstFingerIdx = scaleKeys.indexOf(midiNumber.toString())
const firstFinger = Number(scaleKeys[firstFingerIdx])
let secondFinger: number
let thirdFinger: number
let fourthFinger: number
const seventhChordMidi: number[] = []

if (firstFingerIdx < 0) {
return seventhChordMidi
} else {
secondFinger = Number(scaleKeys[(firstFingerIdx + 2) % (SCALE_LENGTH - 1)])
const secondFingerIdx = scaleKeys.indexOf(secondFinger.toString())
thirdFinger = Number(scaleKeys[(secondFingerIdx + 2) % (SCALE_LENGTH - 1)])
const thirdFingerIdx = scaleKeys.indexOf(thirdFinger.toString())
fourthFinger = Number(scaleKeys[(thirdFingerIdx + 2) % (SCALE_LENGTH - 1)])
}

if (secondFinger < firstFinger) {
secondFinger += OCTAVE_LENGTH
}

if (thirdFinger < secondFinger) {
thirdFinger += OCTAVE_LENGTH
}

if (fourthFinger < thirdFinger) {
fourthFinger += OCTAVE_LENGTH
}

seventhChordMidi.push(firstFinger, secondFinger, thirdFinger, fourthFinger)

return seventhChordMidi
}

/**
* Returns a random piano note as a string.
* @returns A random note (ex. C#, Db, F#, Gb)
Expand Down
10 changes: 9 additions & 1 deletion src/utils/modes/practice.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
export type AvailableScreensType = 'practice' | 'quiz'
export type AvailablePracticeModesType = 'scales' | 'chords' | 'fifths'
export type AvailablePracticeModesType =
| 'scales'
| 'chords'
| 'seventhChords'
| 'fifths'
const AVAILABLE_MODES: {
[key in AvailablePracticeModesType]: { value: string; label: string }
} = {
Expand All @@ -11,6 +15,10 @@ const AVAILABLE_MODES: {
label: 'Chords',
value: 'chords',
},
seventhChords: {
label: 'Seventh Chords',
value: 'seventhChords',
},
fifths: {
label: 'Fifths',
value: 'fifths',
Expand Down

0 comments on commit e71896c

Please sign in to comment.