Skip to content

Commit

Permalink
feat(ChordDictionary): disabled chord
Browse files Browse the repository at this point in the history
  • Loading branch information
ArTiSTiX committed Jan 4, 2024
1 parent 472241e commit a6c50a9
Show file tree
Hide file tree
Showing 27 changed files with 647 additions and 207 deletions.
12 changes: 12 additions & 0 deletions src/main/store/defaults.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
ChordDictionarySettings,
ChordDisplaySettings,
ChordQuizSettings,
CircleOfFifthsSettings,
Expand Down Expand Up @@ -75,6 +76,16 @@ export const defaultCircleOfFifthsSettings: CircleOfFifthsSettings = {
displayDegreeLabels: false,
};

export const defaultChordDictionarySettings: ChordDictionarySettings = {
interactive: 'play',
hideDisabled: false,
filterInKey: false,
groupBy: 'none',
defaultNotation: 'short',
disabled: [],
aliases: [['maj', '']],
};

export const defaultNotationSettings: NotationSettings = {
key: 'C',
accidentals: 'flat' as const,
Expand Down Expand Up @@ -105,6 +116,7 @@ export const defaults: StoreType = {
chordDisplay: [defaultChordDisplaySettings],
chordQuiz: defaultChordQuizSettings,
circleOfFifths: defaultCircleOfFifthsSettings,
chordDictionary: defaultChordDictionarySettings,
notation: defaultNotationSettings,
server: {
enabled: true,
Expand Down
56 changes: 56 additions & 0 deletions src/main/types/Settings.schema.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,62 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"properties": {
"chordDictionary": {
"properties": {
"aliases": {
"items": {
"items": [
{
"type": "string"
},
{
"type": "string"
}
],
"maxItems": 2,
"minItems": 2,
"type": "array"
},
"type": "array"
},
"defaultNotation": {
"enum": [
"long",
"short",
"symbol"
],
"type": "string"
},
"disabled": {
"items": {
"type": "string"
},
"type": "array"
},
"filterInKey": {
"type": "boolean"
},
"groupBy": {
"enum": [
"intervals",
"none",
"quality"
],
"type": "string"
},
"hideDisabled": {
"type": "boolean"
},
"interactive": {
"enum": [
"detect",
"play"
],
"type": "string"
}
},
"type": "object"
},
"chordDisplay": {
"items": {
"properties": {
Expand Down
11 changes: 11 additions & 0 deletions src/main/types/Settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@ export type CircleOfFifthsSettings = {
displayDegreeLabels: boolean;
};

export type ChordDictionarySettings = {
interactive: 'detect' | 'play';
hideDisabled: boolean;
filterInKey: boolean;
groupBy: 'none' | 'quality' | 'intervals';
defaultNotation: 'long' | 'short' | 'symbol';
disabled: string[];
aliases: Array<[key: string, value: string]>;
};

export type NotationSettings = {
key: string;
accidentals: 'flat' | 'sharp';
Expand All @@ -88,6 +98,7 @@ export type Settings = {
chordDisplay: ChordDisplaySettings[];
chordQuiz: ChordQuizSettings;
circleOfFifths: CircleOfFifthsSettings;
chordDictionary: ChordDictionarySettings;
notation: NotationSettings;
server: ServerSettings;
};
4 changes: 4 additions & 0 deletions src/renderer/components/Icon/icons/hidden.react.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions src/renderer/components/Icon/icons/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export const ICON_NAMES = [
'exclamation',
'github',
'heart',
'hidden',
'info',
'loading',
'midi',
Expand All @@ -38,6 +39,7 @@ export const ICON_NAMES = [
'server',
'settings',
'trash',
'visible',
'window',
] as const;

Expand Down
4 changes: 4 additions & 0 deletions src/renderer/components/Icon/icons/visible.react.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions src/renderer/helpers/chord-detect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ function withOmissions(chroma: string, omissionChroma: string): string {

type FindMatchesOptions = {
allowOmissions: boolean;
disabledChords: string[];
};
function findMatches(
notes: string[],
Expand All @@ -63,6 +64,10 @@ function findMatches(
allModes.forEach((mode, index) => {
// some chords could have the same chroma but different interval spelling
const chordTypes = chordTypesWithOmissions.filter((chordType) => {
if (options.disabledChords && options.disabledChords.includes(chordType.aliases[0])) {
return false;
}

if (options.allowOmissions) {
const modeWithOmissions = withOmissions(mode, chordType.omissionChroma);
return chordType.chroma === modeWithOmissions;
Expand Down Expand Up @@ -100,6 +105,7 @@ function findMatches(

type DetectOptions = {
allowOmissions: boolean;
disabledChords: string[];
};
export function detect(source: string[], options: Partial<DetectOptions> = {}): string[] {
const notes = source.map((n) => note(n).pc).filter((x) => x);
Expand Down
84 changes: 76 additions & 8 deletions src/renderer/hooks/useNotes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,15 @@ const getChordInfo = (chord: string, keySignatureNotes: string[]) => {
return null;
};

const getChords = (notes: string[], keySignatureNotes: string[], allowOmissions: boolean) => {
const chords = detect(notes, { allowOmissions }).map((n) => getChordInfo(n, keySignatureNotes));
const getChords = (
notes: string[],
keySignatureNotes: string[],
allowOmissions: boolean,
disabledChords: string[] = []
) => {
const chords = detect(notes, { allowOmissions, disabledChords }).map((n) =>
getChordInfo(n, keySignatureNotes)
);

return chords;
};
Expand All @@ -52,6 +59,7 @@ interface Parameters {
allowOmissions: boolean;
useSustain: boolean;
detectOnRelease: boolean;
disabledChords: string[];
}

enum MidiActionTypes {
Expand All @@ -71,6 +79,7 @@ enum ParametersActionTypes {
ALLOW_OMISSIONS_CHANGED = 'ALLOW_OMISSIONS_CHANGED',
USE_SUSTAIN_CHANGED = 'USE_SUSTAIN_CHANGED',
DETECT_ON_RELEASE_CHANGED = 'DETECT_ON_RELEASE_CHANGED',
DISABLED_CHORDS_CHANGED = 'DISABLED_CHORDS_CHANGED',
}

interface ParametersAction {
Expand All @@ -86,6 +95,7 @@ interface State {
allowOmissions: boolean;
useSustain: boolean;
detectOnRelease: boolean;
disabledChords?: string[];
};
sustainedMidiNotes: number[];
playedMidiNotes: number[];
Expand All @@ -110,7 +120,12 @@ function reducer(state: State, action: Action): State {
getNoteInKeySignature(Note.fromMidi(m), keySignature.notes)
);
const pitchClasses = notes.map(Note.pitchClass);
const chords = getChords(notes, keySignature.notes, state.params.allowOmissions);
const chords = getChords(
notes,
keySignature.notes,
state.params.allowOmissions,
state.params.disabledChords
);

return {
...state,
Expand All @@ -125,7 +140,12 @@ function reducer(state: State, action: Action): State {
}
case ParametersActionTypes.ALLOW_OMISSIONS_CHANGED: {
const allowOmissions = action.value as typeof state.params.allowOmissions;
const chords = getChords(state.notes, keySignatureNotes, allowOmissions);
const chords = getChords(
state.notes,
keySignatureNotes,
allowOmissions,
state.params.disabledChords
);

return {
...state,
Expand All @@ -136,6 +156,24 @@ function reducer(state: State, action: Action): State {
chords,
};
}
case ParametersActionTypes.DISABLED_CHORDS_CHANGED: {
const disabledChords = action.value as typeof state.params.disabledChords;
const chords = getChords(
state.notes,
keySignatureNotes,
state.params.allowOmissions,
disabledChords
);

return {
...state,
params: {
...state.params,
disabledChords,
},
chords,
};
}
case ParametersActionTypes.USE_SUSTAIN_CHANGED: {
const useSustain = action.value as typeof state.params.useSustain;

Expand All @@ -144,7 +182,12 @@ 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 = getChords(
notes,
keySignatureNotes,
state.params.allowOmissions,
state.params.disabledChords
);

return {
...state,
Expand Down Expand Up @@ -177,7 +220,12 @@ 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 = getChords(
notes,
keySignatureNotes,
state.params.allowOmissions,
state.params.disabledChords
);

return {
...state,
Expand All @@ -200,7 +248,12 @@ function reducer(state: State, action: Action): State {
const notes = midiNotes.map(fromMidi);
const pitchClasses = notes.map(Note.pitchClass);
const chords = state.params.detectOnRelease
? getChords(notes, keySignatureNotes, state.params.allowOmissions)
? getChords(
notes,
keySignatureNotes,
state.params.allowOmissions,
state.params.disabledChords
)
: state.chords;

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

return {
Expand All @@ -261,6 +319,7 @@ const defaultState: State = {
allowOmissions: false,
useSustain: true,
detectOnRelease: true,
disabledChords: undefined,
},
sustainedMidiNotes: [],
playedMidiNotes: [],
Expand All @@ -276,6 +335,7 @@ export default function useNotes({
key = 'C',
midiChannel = MIDI_CHANNEL_ALL,
allowOmissions = false,
disabledChords = undefined,
useSustain = true,
detectOnRelease = true,
}: Partial<Parameters> = {}) {
Expand All @@ -286,6 +346,7 @@ export default function useNotes({
allowOmissions,
useSustain,
detectOnRelease,
disabledChords,
},
});

Expand All @@ -303,6 +364,13 @@ export default function useNotes({
});
}, [allowOmissions]);

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

useEffect(() => {
dispatch({
type: ParametersActionTypes.USE_SUSTAIN_CHANGED,
Expand Down
Loading

0 comments on commit a6c50a9

Please sign in to comment.