Skip to content

Commit

Permalink
Chord alterations handling
Browse files Browse the repository at this point in the history
Chord alterations are always presented in parenthesis when transforming a Chord object into a string. It is done to avoid ambiguity: e.g. F(#11)
  • Loading branch information
esbudylin committed Sep 11, 2024
1 parent 81b7abb commit 6d3c855
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
13 changes: 8 additions & 5 deletions modest/chord.fnl
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
:min :m
:power :5
[:sus step] (.. :sus step)
; half-diminished chord is handled as a 7b5 chord due to the lack of an ascii symbol for it
; half-diminished chord is handled as a 7(b5) chord due to the lack of an ascii symbol for it
(where (or :maj :half-dim)) ""
_ triad))

Expand All @@ -80,10 +80,13 @@
(let [alterations (-> (or alterations [])
(copy)
(conj (when (= triad :half-dim) [-1 5]))
(sort #(. $1 2)))]
(accumulate [res ""
_ [acc interval-size] (ipairs alterations)]
(.. res (accidental-to-string acc ascii) interval-size))))
(sort #(. $1 2)))
alteration-string (accumulate [res ""
_ [acc interval-size] (ipairs alterations)]
(.. res (accidental-to-string acc ascii) interval-size))]
(if (= alteration-string "")
alteration-string
(.. "(" alteration-string ")"))))

(fn add-to-string [{: add : ext}]
(when add
Expand Down
5 changes: 3 additions & 2 deletions tests/chords.lua
Original file line number Diff line number Diff line change
Expand Up @@ -152,15 +152,16 @@ end

function test_chord_tostring()
local chords = {
{ chord = "", to_string = "C7b5" },
{ chord = "", to_string = "C7(b5)" },
{ chord = "Ab6/9" },
{ chord = "C6" },
{ chord = "D13#5b9" },
{ chord = "D13#5b9", to_string = "D13(#5b9)" },
{ chord = "C/Bb" },
{ chord = "CM7" },
{ chord = "CmM7" },
{ chord = "Eaug" },
{ chord = "F5" },
{ chord = "F(#11)" },
}

for _, test in ipairs(chords) do
Expand Down

0 comments on commit 6d3c855

Please sign in to comment.