Skip to content

Commit

Permalink
Merge pull request cuthbertLab#1128 from cuthbertLab/multiple-figures
Browse files Browse the repository at this point in the history
  • Loading branch information
mscuthbert authored Sep 22, 2021
2 parents 546683a + 9221e6b commit 3d3bee6
Showing 1 changed file with 55 additions and 2 deletions.
57 changes: 55 additions & 2 deletions music21/figuredBass/realizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ def figuredBassFromStream(streamPart):
.. image:: images/figuredBass/fbRealizer_fbStreamPart.*
:width: 500
Changed in v7.3: multiple figures in same lyric (e.g. '64') now supported.
'''
sf = streamPart.flatten()
sfn = sf.notes
Expand Down Expand Up @@ -116,9 +117,41 @@ def figuredBassFromStream(streamPart):
if paddingLeft != 0.0:
fb._paddingLeft = paddingLeft

def updateAnnotationString(annotationString: str, inputText: str) -> str:
'''
Continue building the working `annotationString` based on some `inputText`
that has yet to be processed. Called recursively until `inputText` is exhausted
or contains unexpected characters.
'''
# "64" and "#6#42" but not necessarily "4-3" or "sus4"
stop_index_exclusive: int = 0
if inputText[0] in '#-' and len(inputText) > 1 and inputText[1].isnumeric():
stop_index_exclusive = 2
elif inputText[0].isnumeric():
stop_index_exclusive = 1
annotationString += inputText[:stop_index_exclusive]
# Is there more?
if inputText[stop_index_exclusive:]:
annotationString += ', '
annotationString = updateAnnotationString(
annotationString, inputText[stop_index_exclusive:])
return annotationString

for n in sfn:
if n.lyrics:
annotationString = ', '.join([x.text for x in n.lyrics])
annotationString: str = ''
for i, lyric_line in enumerate(n.lyrics):
if lyric_line.text in (None, ''):
continue
if ',' in lyric_line.text:
# presence of comma suggests we already have a separated
# sequence of figures, e.g. "#6, 4, 2"
annotationString = lyric_line.text
else:
# parse it more carefully
annotationString = updateAnnotationString(annotationString, lyric_line.text)
if i + 1 < len(n.lyrics):
annotationString += ', '
fb.addElement(n, annotationString)
else:
fb.addElement(n)
Expand Down Expand Up @@ -793,7 +826,27 @@ class FiguredBassLineException(exceptions21.Music21Exception):


class Test(unittest.TestCase):
pass
def testMultipleFiguresInLyric(self):
from music21 import converter

s = converter.parse('tinynotation: 4/4 C4 F4 G4_64 G4 C1', makeNotation=False)
third_note = s[note.Note][2]
self.assertEqual(third_note.lyric, '64')
unused_fb = figuredBassFromStream(s)
self.assertEqual(third_note.notationString, '6, 4')

third_note.lyric = '#6#42'
unused_fb = figuredBassFromStream(s)
self.assertEqual(third_note.notationString, '#6, #4, 2')

third_note.lyric = '#64#2'
unused_fb = figuredBassFromStream(s)
self.assertEqual(third_note.notationString, '#6, 4, #2')

# original case
third_note.lyric = '6\n4'
unused_fb = figuredBassFromStream(s)
self.assertEqual(third_note.notationString, '6, 4')


if __name__ == '__main__':
Expand Down

0 comments on commit 3d3bee6

Please sign in to comment.