Skip to content

Commit

Permalink
Cleanup on TSV-DCML Converter
Browse files Browse the repository at this point in the history
a little cleanup of typing and fix docs
  • Loading branch information
mscuthbert committed Jun 13, 2024
1 parent 3fae7a1 commit c9350c5
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions music21/romanText/tsvConverter.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ def _changeRepresentation(self) -> None:
self.chord = re.sub(
r'''
(\d+) # match one or more digits
(?![\]\d]) # without a digit or a ']' to the right
(?![]\d]) # without a digit or a ']' to the right
''',
r'd\1',
self.chord,
Expand Down Expand Up @@ -521,7 +521,7 @@ class TsvHandler:
'I'
'''
def __init__(self, tsvFile: str, dcml_version: int = 1):
def __init__(self, tsvFile: str|pathlib.Path, dcml_version: int = 1):
if dcml_version == 1:
self.heading_names = HEADERS[1]
self._tab_chord_cls: type[TabChordBase] = TabChord
Expand Down Expand Up @@ -653,6 +653,7 @@ def prepStream(self) -> stream.Score:
'''
s = stream.Score()
p = stream.Part()
m: stream.Measure|None = None
if self.dcml_version == 1:
# This sort of metadata seems to have been removed altogether from the
# v2 files
Expand Down Expand Up @@ -762,7 +763,6 @@ class M21toTSV:
>>> tsvData[1][DCML_V2_HEADERS.index('chord')]
'I'
'''

def __init__(self, m21Stream: stream.Score, dcml_version: int = 2):
self.version = dcml_version
self.m21Stream = m21Stream
Expand Down Expand Up @@ -871,6 +871,8 @@ def _m21ToTsv_v2(self) -> list[list[str]]:
thisEntry.numeral = '@none'
thisEntry.chord = '@none'
else:
if t.TYPE_CHECKING:
assert isinstance(thisRN, roman.RomanNumeral)
local_key = localKeyAsRn(thisRN.key, global_key_obj)
relativeroot = None
if thisRN.secondaryRomanNumeral:
Expand Down Expand Up @@ -911,7 +913,7 @@ def _m21ToTsv_v2(self) -> list[list[str]]:
tsvData.append(thisInfo)
return tsvData

def write(self, filePathAndName: str):
def write(self, filePathAndName: str|pathlib.Path):
'''
Writes a list of lists (e.g. from m21ToTsv()) to a tsv file.
'''
Expand Down Expand Up @@ -974,16 +976,19 @@ def handleAddedTones(dcmlChord: str) -> str:
'Viio7[no3][no5][addb4]/V'
When in root position, 7 does not replace 8:
>>> romanText.tsvConverter.handleAddedTones('vi(#74)')
'vi[no3][add#7][add4]'
When not in root position, 7 does replace 8:
>>> romanText.tsvConverter.handleAddedTones('ii6(11#7b6)')
'ii6[no8][no5][add11][add#7][addb6]'
'0' can be used to indicate root-replacement by 7 in a root-position chord.
We need to change '0' to '7' because music21 changes the 0 to 'o' (i.e.,
a diminished chord).
>>> romanText.tsvConverter.handleAddedTones('i(#0)')
'i[no1][add#7]'
'''
Expand All @@ -1001,8 +1006,8 @@ def handleAddedTones(dcmlChord: str) -> str:
return 'Cad64' + secondary
added_tone_tuples: list[tuple[str, str, str, str]] = re.findall(
r'''
(\+|-)? # indicates whether to add or remove chord factor
(\^|v)? # indicates whether tone replaces chord factor above/below
([+\-])? # indicates whether to add or remove chord factor
([\^v])? # indicates whether tone replaces chord factor above/below
(\#+|b+)? # alteration
(1\d|\d) # figures 0-19, in practice 0-14
''',
Expand Down Expand Up @@ -1134,7 +1139,6 @@ def getLocalKey(local_key: str, global_key: str, convertDCMLToM21: bool = False)
>>> romanText.tsvConverter.getLocalKey('vii', 'a', convertDCMLToM21=True)
'g'
'''
if convertDCMLToM21:
local_key = characterSwaps(local_key, minor=isMinor(global_key[0]), direction='DCML-m21')
Expand Down

0 comments on commit c9350c5

Please sign in to comment.