diff --git a/Code/romanUmpire.py b/Code/romanUmpire.py index 0e0025dce..b5634b533 100644 --- a/Code/romanUmpire.py +++ b/Code/romanUmpire.py @@ -137,6 +137,9 @@ def __init__(self, source=None): self.bassFeedbackMessage: Optional[str] = None self.bassSuggestions: Optional[list] = [] + # Shared by the score marker and the printed feedback for this range. + self.feedbackNumber: Optional[int] = None + if source: if ('NotRest' in source.classes) and ('GeneralNote' in source.classes): @@ -1064,6 +1067,8 @@ def printFeedback(self, if (not pitches) and (not rare) and (not metre) and (not bass): raise ValueError('Please select at least one of the feedback options.') + self.numberFeedback() + if pitches: self.comparePitches() pitchToPrint = ['PITCH COVERAGE =====================\n'] @@ -1080,7 +1085,7 @@ def printFeedback(self, for hr in self.harmonicRanges: if hr.pitchFeedbackMessage: if len(hr.pitchSuggestions) > 0: - pitchToPrint.append(hr.pitchFeedbackMessage) + pitchToPrint.append(f'[{hr.feedbackNumber}] {hr.pitchFeedbackMessage}') pitchToPrint.append(f'Pitch match: {hr.pitchMatchStrength}%') pitchToPrint.append('How about:') for x in hr.pitchSuggestions: @@ -1088,7 +1093,7 @@ def printFeedback(self, pitchToPrint.append('\n') else: # no suggestions if not constructiveOnly: - pitchToPrint.append(hr.pitchFeedbackMessage) + pitchToPrint.append(f'[{hr.feedbackNumber}] {hr.pitchFeedbackMessage}') pitchToPrint.append(f'Pitch match: {hr.pitchMatchStrength}%') pitchToPrint.append('Sorry, no suggestions - ' "I can't find any triads or sevenths.") @@ -1101,7 +1106,7 @@ def printFeedback(self, for hr in self.harmonicRanges: if hr.rareRnFeedbackMessage: self.totalRareRnFeedback += 1 - rareToPrint.append(hr.rareRnFeedbackMessage + '\n') + rareToPrint.append(f'[{hr.feedbackNumber}] {hr.rareRnFeedbackMessage}\n') if self.totalRareRnFeedback == 0: msg = 'No rare or unconventional Roman Numerals in this analysis.\n' @@ -1126,7 +1131,7 @@ def printFeedback(self, 'at an unusually weak metrical position:\n') for hr in self.harmonicRanges: if hr.metricalFeedbackMessage: - metreToPrint.append(hr.metricalFeedbackMessage + '\n') + metreToPrint.append(f'[{hr.feedbackNumber}] {hr.metricalFeedbackMessage}\n') if bass: self.compareBass() @@ -1145,14 +1150,14 @@ def printFeedback(self, for hr in self.harmonicRanges: if hr.bassFeedbackMessage: if len(hr.bassSuggestions) > 0: - bassToPrint.append(hr.bassFeedbackMessage) + bassToPrint.append(f'[{hr.feedbackNumber}] {hr.bassFeedbackMessage}') bassToPrint.append('How about:') for x in hr.bassSuggestions: bassToPrint.append(x) bassToPrint.append('\n') else: # no suggestions if not constructiveOnly: - bassToPrint.append(hr.bassFeedbackMessage) + bassToPrint.append(f'[{hr.feedbackNumber}] {hr.bassFeedbackMessage}') bassToPrint.append('Sorry, no inversion suggestions - ' 'none of the bass note(s) are in the chord.') bassToPrint.append('\n') @@ -1182,49 +1187,71 @@ def printFeedback(self, text_file.close() + def numberFeedback(self): + """ + Numbers the harmonic ranges with feedback of any type, in score order, + setting each one's feedbackNumber. + The score (writeScoreWithAnalysis) marks each of these ranges with its number, + and printFeedback starts each message with the same number in [brackets], + so that a marker on the score leads to the corresponding text. + All types count, whichever ones are selected for display, + so that a range keeps the same number either way. + """ + + self.runComparisons() + + number = 0 + for hr in self.harmonicRanges: + if hr.pitchFeedbackMessage or hr.rareRnFeedbackMessage or \ + hr.metricalFeedbackMessage or hr.bassFeedbackMessage: + number += 1 + hr.feedbackNumber = number + else: + hr.feedbackNumber = None + def _feedbackOnScore(self, pitches: bool = True, rare: bool = True, metre: bool = True, bass: bool = True): """ - Inserts comments on the score for moments where there is feedback available. + Marks the score for moments where there is feedback available: + a warning sign and the number of that feedback (as in printFeedback) + above the analysis, and the chord in red. As with the printFeedback method, you can chose to any or all of the feedback types: 'pitches', 'rare', 'metre'; and 'bass'. """ - if pitches: - self.comparePitches() - if metre: - self.metricalPositions() - if bass: - self.compareBass() + self.numberFeedback() for hr in self.harmonicRanges: - if pitches and hr.pitchFeedbackMessage: - self.insertFeedback(hr, '* Pitch') - if rare and hr.rareRnFeedbackMessage: - self.insertFeedback(hr, '* Rare') - if metre and hr.metricalFeedbackMessage: - self.insertFeedback(hr, '* Beat') - if bass and hr.bassFeedbackMessage: - self.insertFeedback(hr, '* Bass') + if (pitches and hr.pitchFeedbackMessage) or \ + (rare and hr.rareRnFeedbackMessage) or \ + (metre and hr.metricalFeedbackMessage) or \ + (bass and hr.bassFeedbackMessage): + # A warning sign (in text, not emoji, style) as well as colour, for + # colour-blind readers and black-and-white printing. + self.insertFeedback(hr, f'\u26a0\ufe0e{hr.feedbackNumber}') def insertFeedback(self, hr: HarmonicRange, message: str): """ - Shared method for inserting feedback of each type in to the score. + Inserts a message above the analysis at the start of a harmonic range + and colours the chord there red. """ te = expressions.TextExpression(message) # NB: Have to make a new one each time te.placement = 'above' + te.style.fontWeight = 'bold' te.style.color = 'red' - # Known issue: ^ This works within musicXML but readers like MuseScore don't show it. p = self.scoreWithAnalysis.parts[-1] m = p.measure(hr.startMeasure) mOffset = m.getOffsetInHierarchy(p) offsetInMeasure = hr.startOffset - mOffset # TODO compress, but note not offset nor beat m.insert(offsetInMeasure, te) + # Unlike text colour, MuseScore and other readers show note colour. + for c in m.flatten().getElementsByOffset(offsetInMeasure).getElementsByClass(chord.Chord): + c.style.color = 'red' # ------------------------------------------------------------------------------ diff --git a/Tests/test_Roman_umpire.py b/Tests/test_Roman_umpire.py index 6f991358d..9923693dc 100644 --- a/Tests/test_Roman_umpire.py +++ b/Tests/test_Roman_umpire.py @@ -1,7 +1,8 @@ import os +import tempfile import unittest -from music21 import converter +from music21 import chord, converter, expressions from Code.romanUmpire import ScoreAndAnalysis from . import TEST_RESOURCES_FOLDER @@ -47,3 +48,36 @@ def testScoreAndAnalysis(self): "Measure 9, beat 1, iiø7 in bb, " "indicating the bass C for lowest note(s) of: ['Eb2'].",) self.assertEqual(sa.totalMetricalFeedback, 19) + + def testFeedbackNumbers(self): + """ + Each harmonic range with feedback gets a number, + shown both on the score and in the printed feedback. + """ + + basePath = TEST_RESOURCES_FOLDER / "Example" + sa = ScoreAndAnalysis(os.path.join(basePath, "score.mxl"), + analysisLocation=os.path.join(basePath, "analysis.txt"), + tolerance=60) + + with tempfile.TemporaryDirectory() as outPath: + sa.writeScoreWithAnalysis(outPath=outPath, outFile="annotated.mxl") + sa.printFeedback(outPath=outPath, outFile="feedback") + with open(os.path.join(outPath, "feedback.txt")) as f: + feedback = f.read() + + numbered = [hr for hr in sa.harmonicRanges if hr.feedbackNumber] + self.assertGreater(len(numbered), 0) + self.assertEqual([hr.feedbackNumber for hr in numbered], + list(range(1, len(numbered) + 1))) + + analysis = sa.scoreWithAnalysis.parts[-1] + markers = analysis.recurse().getElementsByClass(expressions.TextExpression) + self.assertEqual([te.content for te in markers], + [f"\u26a0\ufe0e{hr.feedbackNumber}" for hr in numbered]) + redChords = [c for c in analysis.recurse().getElementsByClass(chord.Chord) + if c.style.color == "red"] + self.assertEqual(len(redChords), len(numbered)) + + hr = sa.harmonicRanges[20] + self.assertIn(f"[{hr.feedbackNumber}] {hr.bassFeedbackMessage}", feedback)