From 6d3c8556838660b9cc3901dac6f6f9341c899d22 Mon Sep 17 00:00:00 2001 From: esbudylin Date: Wed, 11 Sep 2024 11:15:37 +0400 Subject: [PATCH] Chord alterations handling 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) --- modest/chord.fnl | 13 ++++++++----- tests/chords.lua | 5 +++-- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/modest/chord.fnl b/modest/chord.fnl index 5894d48..b143308 100644 --- a/modest/chord.fnl +++ b/modest/chord.fnl @@ -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)) @@ -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 diff --git a/tests/chords.lua b/tests/chords.lua index 04ff7c5..7803e24 100644 --- a/tests/chords.lua +++ b/tests/chords.lua @@ -152,15 +152,16 @@ end function test_chord_tostring() local chords = { - { chord = "Cø", to_string = "C7b5" }, + { chord = "Cø", 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