Skip to content

Commit

Permalink
feat(ChordDisplay): detectOnRelease option
Browse files Browse the repository at this point in the history
  • Loading branch information
ArTiSTiX committed Dec 19, 2023
1 parent 24a8682 commit 83ce5d7
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/main/store/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export const defaultChordDisplaySettings: ChordDisplaySettings = {
chordNotation: 'short',
allowOmissions: true,
useSustain: true,
detectOnRelease: true,
highlightAlterations: false,
displayKeyboard: true,
displayChord: true,
Expand Down
3 changes: 3 additions & 0 deletions src/main/types/Settings.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
],
"type": "string"
},
"detectOnRelease": {
"type": "boolean"
},
"displayAltChords": {
"type": "boolean"
},
Expand Down
1 change: 1 addition & 0 deletions src/main/types/Settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export type ChordDisplaySettings = {
chordNotation: 'long' | 'short' | 'symbol';
allowOmissions: boolean;
useSustain: boolean;
detectOnRelease: boolean;
highlightAlterations: boolean;
displayKeyboard: boolean;
displayChord: boolean;
Expand Down
21 changes: 19 additions & 2 deletions src/renderer/hooks/useNotes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ interface Parameters {
midiChannel: number;
allowOmissions: boolean;
useSustain: boolean;
detectOnRelease: boolean;
}

enum MidiActionTypes {
Expand All @@ -69,6 +70,7 @@ enum ParametersActionTypes {
KEY_SIGNATURE_CHANGED = 'KEY_SIGNATURE_CHANGED',
ALLOW_OMISSIONS_CHANGED = 'ALLOW_OMISSIONS_CHANGED',
USE_SUSTAIN_CHANGED = 'USE_SUSTAIN_CHANGED',
DETECT_ON_RELEASE_CHANGED = 'DETECT_ON_RELEASE_CHANGED',
}

interface ParametersAction {
Expand All @@ -83,6 +85,7 @@ interface State {
keySignature: KeySignatureConfig;
allowOmissions: boolean;
useSustain: boolean;
detectOnRelease: boolean;
};
sustainedMidiNotes: number[];
playedMidiNotes: number[];
Expand Down Expand Up @@ -196,7 +199,9 @@ function reducer(state: State, action: Action): State {
midiNotes.sort(midiSortCompareFn);
const notes = midiNotes.map(fromMidi);
const pitchClasses = notes.map(Note.pitchClass);
const chords = getChords(notes, keySignatureNotes, state.params.allowOmissions);
const chords = state.params.detectOnRelease
? getChords(notes, keySignatureNotes, state.params.allowOmissions)
: state.chords;

return {
...state,
Expand Down Expand Up @@ -230,7 +235,9 @@ function reducer(state: State, action: Action): State {
midiNotes.sort(midiSortCompareFn);
const notes = midiNotes.map(fromMidi);
const pitchClasses = notes.map(Note.pitchClass);
const chords = getChords(notes, keySignatureNotes, state.params.allowOmissions);
const chords = state.params.detectOnRelease
? getChords(notes, keySignatureNotes, state.params.allowOmissions)
: state.chords;

return {
...state,
Expand All @@ -253,6 +260,7 @@ const defaultState: State = {
keySignature: getKeySignature('C'),
allowOmissions: false,
useSustain: true,
detectOnRelease: true,
},
sustainedMidiNotes: [],
playedMidiNotes: [],
Expand All @@ -269,13 +277,15 @@ export default function useNotes({
midiChannel = MIDI_CHANNEL_ALL,
allowOmissions = false,
useSustain = true,
detectOnRelease = true,
}: Partial<Parameters> = {}) {
const [state, dispatch] = useReducer(reducer, {
...defaultState,
params: {
keySignature: getKeySignature(key, accidentals === 'sharp'),
allowOmissions,
useSustain,
detectOnRelease,
},
});

Expand All @@ -300,6 +310,13 @@ export default function useNotes({
});
}, [useSustain]);

useEffect(() => {
dispatch({
type: ParametersActionTypes.DETECT_ON_RELEASE_CHANGED,
value: detectOnRelease,
});
}, [detectOnRelease]);

const onMidiMessage = useCallback(
(message: MidiMessage) => {
const cmd = getMidiCommand(message);
Expand Down
1 change: 1 addition & 0 deletions src/renderer/views/ChordDisplay/ChordDisplayModule.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const ChordDisplayModule: React.FC<Props> = ({ moduleId }) => {
midiChannel: 0,
allowOmissions: moduleSettings.allowOmissions,
useSustain: moduleSettings.useSustain,
detectOnRelease: moduleSettings.detectOnRelease,
});

if (!settings || !moduleSettings) return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,17 @@ const ChordDisplayModuleSettings: React.FC<Props> = ({ parentPath }) => {
checked={moduleSettings.useSustain}
/>
</FormControlLabel>

<FormControlLabel
label="Detect on release"
hint="Refresh chord detection when releasing notes"
reverse
>
<Switch
onChange={(value) => updateModuleSetting('detectOnRelease', value)}
checked={moduleSettings.detectOnRelease}
/>
</FormControlLabel>
</FormFieldset>

<FormFieldset label="Additional Info">
Expand Down

0 comments on commit 83ce5d7

Please sign in to comment.