Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support CLI notes -> chord, support some inversions in Chord::from_string #36

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

spongechameleon
Copy link

@spongechameleon spongechameleon commented Apr 16, 2022

First of all, hi everybody, thanks for making such a cool app 😀

FYI please let me know if there's any problems and I'll be glad to fix 'er up. I'm just getting into both Rust and Music Theory so I'm sure I've botched a couple things here.

Support entering notes in the CLI to get the chord

Entering the chord command followed by notes will return the chord name, or an error if it's an invalid/unknown chord.

$ rustmt chord C E G
C Major Triad

$ rustmt chord E G C
C Major Triad, 1st Inversion

$ rustmt chord C Cs D
Invalid/Unknown Chord!

This change is three parts:

  1. Implementing the Display trait for Chord
    • Prints "Root Quality Number, Inversion"
  2. Changing Chord::from_string to return a Result instead of panicking
  3. Checking the length of CLI arguments for the chord command
    • If any argument is >2 characters long it can't be a note, so the user is entering a chord name and is asking for notes
    • Thus, if all the arguments are <=2 characters they're all notes and the user is asking for a chord name

Inversion support in Chord::from_string

Can't be sure that the first note in a string of notes is the root- for example, somebody could enter E G C. So we should check for inversions, if possible.

I did this by first checking against interval patterns for chords in their root position (using the existing code). For instance, the interval [4, 3] means Major Triad in the root position, e.g. C E G. If there are no matches for root position chords, then we check some interval patterns for inversions. For instance, the interval [3, 5] means Major Triad 1st inversion, e.g. E G C.

Unfortunately the inversion support is pretty sparse so far. We can check Major and Minor Triads and that's about it. I plan on finding more patterns, but some chord positions cannot be deciphered from interval patterns alone. For instance, Augmented Triads have a [4, 4] interval, so you cannot distinguish between the root position or either of the inversions by using the interval pattern alone since it's the same for every position. I'm wondering what the best behavior is in a case like this: just list off all the possible chords?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant