Example #1
0
    def testAbc21Chords(self):
        '''
        Translation of ABC Chord variations
        '''
        from music21 import abcFormat
        from music21 import chord
        from music21.abcFormat import translate

        af = abcFormat.ABCFile()
        # default length of this test
        abc_dl = 'L:1/8\n'

        # Empty Chords should be skipped at all
        for abc_chord in ['[]', '[z]']:
            ah = af.readstr(abc_dl + '[]')
            s = translate.abcToStreamScore(ah)
            part = s.parts[0]
            self.assertFalse(part.getElementsByClass(chord.Chord),
                             f'Empty chord "{abc_chord}" in Score')

        # list of test abc chords and their quarter lengths at the default length of 1/8
        # list[tuple(str, int)] = of abc chords and= [( abc_chord: str)]
        abc_chords = [
            ('[c_eg]', 0.5, ['C', 'E-', 'G']),
            ('[ceg]', 0.5, 'CEG'),
            ('[ceg]2', 1.0, 'CEG'),
            ('[c2e2^g2]', 1.0, ['C', 'E', 'G#']),
            ("[c'e2g]", 0.5, 'CEG'),
            ('[ce^g2]', 0.5, ['C', 'E', 'G#']),
            ('[c,2e2g2]/2', 0.5, 'CEG'),
            ("[c/2e'/2=g/2]", 0.25, 'CEG'),
            ('[c2_e,,/2g/2]/2', 0.5, ['C', 'E-', 'G']),
            ('[c/2e/2g/2]2', 0.5, 'CEG'),
            ('[^c/2e/2g/2]/2', 0.125, ['C#', 'E', 'G']),
            ('[ceg]', 0.5, 'CEG'),
        ]

        for abc_chord, quarter_length, chord_pitches in abc_chords:
            ah = af.readstr(abc_dl + abc_chord)
            s = translate.abcToStreamScore(ah)
            self.assertEqual(s.duration.quarterLength, quarter_length,
                             f'invalid duration of chord "{abc_chord}"')

            notes = s.parts[0].notes
            chord0 = notes[0]
            self.assertEqual(len(notes), 1, 'Wrong number of chords found,')
            self.assertIsInstance(chord0, chord.Chord, 'Not a Chord!')
            for pitch_name in chord_pitches:
                self.assertIn(pitch_name, chord0.pitchNames,
                              f'Pitch not in Chord "{abc_chord}"')
Example #2
0
    def testAbc21DirectiveCarryOctave(self):
        from music21 import abcFormat, note
        from music21.abcFormat import translate

        af = abcFormat.ABCFile()
        ah = af.readstr(directiveCarryOctave)
        s = translate.abcToStreamScore(ah)
        notes = s.flat.getElementsByClass(note.Note)
        gSharp = notes[1]
        g8va = notes[3]
        self.assertTrue(gSharp.pitch.midi % 12 > g8va.pitch.midi % 12,
                        "Sharp carries beyond its octave")
        aFlat = notes[2]
        a = notes[4]
        self.assertEqual(aFlat.pitch.midi, a.pitch.midi,
                         "Flat does not carry through measure")
        fNat = notes[5]
        f = notes[6]
        f8ba = notes[7]
        self.assertEqual(fNat.pitch.midi, f.pitch.midi,
                         "Natural does not carry through measure")
        self.assertTrue(fNat.pitch.midi % 12 < f8ba.pitch.midi % 12,
                        "Natural carries beyond its octave")
        self.assertEqual(notes[8].pitch.midi, 65, "Natural is ignored")
        self.assertEqual(notes[12].pitch.midi, 72, "Natural is ignored")
Example #3
0
    def testAbc21DirectiveCarryPitch(self):
        from music21 import abcFormat
        from music21 import note
        from music21.abcFormat import translate

        af = abcFormat.ABCFile()
        ah = af.readstr(directiveCarryPitch)
        s = translate.abcToStreamScore(ah)
        notes = s.flatten().getElementsByClass(note.Note)
        gSharp = notes[1]
        g8va = notes[3]
        self.assertEqual(gSharp.pitch.midi % 12, g8va.pitch.midi % 12,
                         'Sharp does not carry through measure')
        aFlat = notes[2]
        a = notes[4]
        self.assertEqual(aFlat.pitch.midi, a.pitch.midi,
                         'Flat does not carry through measure')
        fNat = notes[5]
        f = notes[6]
        f8ba = notes[7]
        self.assertEqual(fNat.pitch.midi, f.pitch.midi,
                         'Natural does not carry through measure')
        self.assertEqual(fNat.pitch.midi % 12, f8ba.pitch.midi % 12,
                         'Natural does not carry through measure')
        self.assertEqual(notes[8].pitch.midi, 65, 'Natural is ignored')
        self.assertEqual(notes[12].pitch.midi, 72, 'Natural is ignored')
Example #4
0
    def testAbc21DirectiveCarryNot(self):
        from music21 import abcFormat, note
        from music21.abcFormat import translate

        af = abcFormat.ABCFile()
        ah = af.readstr(directiveCarryNot)
        s = translate.abcToStreamScore(ah)
        notes = s.flat.getElementsByClass(note.Note)
        gSharp = notes[1]
        g8va = notes[3]
        self.assertGreater(gSharp.pitch.midi % 12, g8va.pitch.midi % 12,
                           'Sharp carries beyond its octave')
        aFlat = notes[2]
        a = notes[4]
        self.assertLess(aFlat.pitch.midi, a.pitch.midi,
                        'Flat carries through measure')
        fNat = notes[5]
        f = notes[6]
        f8ba = notes[7]
        self.assertLess(fNat.pitch.midi, f.pitch.midi,
                        'Natural carries through measure')
        self.assertLess(fNat.pitch.midi % 12, f8ba.pitch.midi % 12,
                        'Natural carries beyond its octave')
        self.assertEqual(notes[8].pitch.midi, 65, 'Natural is ignored')
        self.assertEqual(notes[12].pitch.midi, 72, 'Natural is ignored')
Example #5
0
    def testAbc21(self):
        from music21 import abcFormat, note
        from music21.abcFormat import translate

        af = abcFormat.ABCFile(abcVersion=(2,1,0))
        ah = af.readstr(carryThrough)
        title = ah.getTitle()
        environLocal.printDebug([title])
        s = translate.abcToStreamScore(ah)
        notes = s.flat.getElementsByClass(note.Note)
        cSharp = notes[3]
        cThrough = notes[5]
        self.assertEqual(cSharp.pitch.midi, cThrough.pitch.midi, 'Sharp does not carry through measure')
        bFlat = notes[4]
        bLast = notes[7]
        self.assertEqual(bFlat.pitch.midi, bLast.pitch.midi, 'Flat does not carry through measure')
        bNat = notes[10]
        bNatNext = notes[11]
        self.assertEqual(bNat.pitch.midi, bNatNext.pitch.midi, 'Natural does not carry through measure')
        self.assertEqual(notes[12].pitch.midi, 73, 'Sharp does not carry through measure')
        self.assertEqual(notes[13].pitch.midi, 72, 'Natural is ignored')
        self.assertEqual(notes[14].pitch.midi, 72, 'Natural does not carry through measure')
        self.assertEqual(notes[16].pitch.midi, 72, 'Sharp carries over measure incorrectly')
        self.assertEqual(notes[17].pitch.midi, 74, 'Sharp (D5) carries over measure incorrectly')
        self.assertEqual(notes[18].pitch.midi, 78, 'Natural (F5) carries over measure incorrectly')
Example #6
0
    def testBasic(self):
        from music21 import abcFormat
        from music21.abcFormat import translate
        from music21.musicxml import m21ToXml

        af = abcFormat.ABCFile()

        GEX = m21ToXml.GeneralObjectExporter()

        for i, tf in enumerate(ALL):
            ah = af.readstr(tf)
            title = ah.getTitle()
            environLocal.printDebug([title])
            s = translate.abcToStreamScore(ah)
            # run musicxml processing to look for internal errors
            #print(repr(s.metadata._workIds['localeOfComposition']._data))
            #print(s.metadata.all())
            try:
                unused_out = GEX.parse(s)
            except UnicodeDecodeError as ude:
                environLocal.warn('About to fail on ABC file #{}'.format(i))
                raise ude
            
            if title == 'D Fragment':
                sharps = s.parts[0].keySignature.sharps
                self.assertEqual(sharps, 7, 'C# key signature should be parsed as 7 sharps')
Example #7
0
    def testAbc21(self):
        from music21 import abcFormat, note
        from music21.abcFormat import translate

        af = abcFormat.ABCFile(abcVersion=(2,1,0))
        ah = af.readstr(carryThrough)
        title = ah.getTitle()
        environLocal.printDebug([title])
        s = translate.abcToStreamScore(ah)
        notes = s.flat.getElementsByClass(note.Note)
        cSharp = notes[3]
        cThrough = notes[5]
        self.assertEqual(cSharp.pitch.midi, cThrough.pitch.midi, "Sharp does not carry through measure")
        bFlat = notes[4]
        bLast = notes[7]
        self.assertEqual(bFlat.pitch.midi, bLast.pitch.midi, "Flat does not carry through measure")
        bNat = notes[10]
        bNatNext = notes[11]
        self.assertEqual(bNat.pitch.midi, bNatNext.pitch.midi, "Natural does not carry through measure")
        self.assertEqual(notes[12].pitch.midi, 73, "Sharp does not carry through measure")
        self.assertEqual(notes[13].pitch.midi, 72, "Natural is ignored")
        self.assertEqual(notes[14].pitch.midi, 72, "Natural does not carry through measure")
        self.assertEqual(notes[16].pitch.midi, 72, "Sharp carries over measure incorrectly")
        self.assertEqual(notes[17].pitch.midi, 74, "Sharp (D5) carries over measure incorrectly")
        self.assertEqual(notes[18].pitch.midi, 78, "Natural (F5) carries over measure incorrectly")
Example #8
0
    def testBasic(self):
        from music21 import abcFormat
        from music21.abcFormat import translate
        from music21.musicxml import m21ToXml

        af = abcFormat.ABCFile()

        GEX = m21ToXml.GeneralObjectExporter()

        for i, tf in enumerate(ALL):
            ah = af.readstr(tf)
            title = ah.getTitle()
            environLocal.printDebug([title])
            s = translate.abcToStreamScore(ah)
            # run musicxml processing to look for internal errors
            #print(repr(s.metadata._workIds['localeOfComposition']._data))
            #print(s.metadata.all())
            try:
                unused_out = GEX.parse(s)
            except UnicodeDecodeError as ude:
                environLocal.warn('About to fail on ABC file #{}'.format(i))
                raise ude

            if title == 'D Fragment':
                sharps = s.parts[0].keySignature.sharps
                self.assertEqual(
                    sharps, 7, 'C# key signature should be parsed as 7 sharps')
Example #9
0
 def testKeySignatures(self):      
     from music21 import abcFormat
     from music21.abcFormat import translate
     af = abcFormat.ABCFile()
     ah = af.readstr(czernyCsharp)
     s = translate.abcToStreamScore(ah)
     sharps = s.parts[0].keySignature.sharps
     self.assertEqual(sharps, 7, 'C# key signature should be parsed as 7 sharps')
Example #10
0
 def testKeySignatures(self):
     from music21 import abcFormat
     from music21.abcFormat import translate
     af = abcFormat.ABCFile()
     ah = af.readstr(czernyCsharp)
     s = translate.abcToStreamScore(ah)
     sharps = s.parts[0].keySignature.sharps
     self.assertEqual(sharps, 7, 'C# key signature should be parsed as 7 sharps')
Example #11
0
    def testAbc21BrokenRhythm(self):
        # Test the chord symbol for note and chord
        from music21 import abcFormat
        from music21 import note
        from music21.abcFormat import translate

        # default length of this test
        abc_dl = 'L:1/4\n'

        # test abc strings of broken rhythm between 2 notes and/or chords and their
        # quarter lengths at the default length of 1/4
        # list[tuple(abc: str, value1: int, value2: int)]
        data = [
            ('[ceg]<f', 0.5, 1.5),
            ('f<[ceg]', 0.5, 1.5),
            ('c>g', 1.5, 0.5),
            ('c<g', 0.5, 1.5),
            ('c>>=g', 1.75, 0.25),
            ('c<<g', 0.25, 1.75),
            ('c>>>g', 1.875, 0.125),
            ('c<<<_g', 0.125, 1.875),
            ("[ceg]>^f", 1.5, 0.5),
            ('[ce^g]>>f', 1.75, 0.25),
            ("[ceg]<<f", 0.25, 1.75),
            ('[ceg]>>>f', 1.875, 0.125),
            ("[ceg]<<<f", 0.125, 1.875),
            ('f>[ceg]', 1.5, 0.5),
            ('f>>[_ceg]', 1.75, 0.25),
            ("f'<<[ceg]", 0.25, 1.75),
            ('f,>>>[ceg]', 1.875, 0.125),
            ('f<<<[ce_g]', 0.125, 1.875),
            ('f<<<[ceg]', 0.125, 1.875),
            ('f2>[ceg]', 3, 0.5),
            ('[ceg]>f2', 1.5, 1),
            ('f>[c_eg]2', 1.5, 1),
            ('[c^eg]2>f', 3, 0.5),
            ('f2<[ceg]', 1.0, 1.5),
            ('[ceg]<f2', 0.5, 3),
            ('f<[ceg]2', 0.5, 3),
            ('[ceg]2<f', 1.0, 1.5),
        ]

        af = abcFormat.ABCFile()
        for abc, soll_left, soll_right in data:
            ah = af.readstr(abc_dl + abc)
            part = translate.abcToStreamScore(ah).parts[0]
            general_notes = part.getElementsByClass(note.GeneralNote)
            self.assertEqual(len(general_notes), 2,
                             f'Wrong numbers of Notes found in abc: {abc}!')
            ist_left, ist_right = general_notes
            self.assertEqual(
                ist_left.duration.quarterLength, soll_left,
                f'Invalid left note/chord length of abc broken rhythm: {abc}')
            self.assertEqual(
                ist_right.duration.quarterLength, soll_right,
                f'Invalid right note/chord length of abc broken rhythm: {abc}')
Example #12
0
    def testBasic(self):
        from music21 import abcFormat
        from music21.abcFormat import translate
        from music21.musicxml import m21ToString

        af = abcFormat.ABCFile()

        for tf in ALL:
            ah = af.readstr(tf)
            environLocal.printDebug([ah.getTitle()])
            s = translate.abcToStreamScore(ah)
            # run musicxml processing to look for internal errors
            unused_out = m21ToString.fromMusic21Object(s)
Example #13
0
    def testBasic(self):
        from music21 import abcFormat
        from music21.abcFormat import translate
        from music21.musicxml import m21ToString

        af = abcFormat.ABCFile()

        for tf in ALL:
            ah = af.readstr(tf)
            environLocal.printDebug([ah.getTitle()])
            s = translate.abcToStreamScore(ah)
            # run musicxml processing to look for internal errors
            unused_out = m21ToString.fromMusic21Object(s)
Example #14
0
    def testAbc21ChordSymbol(self):
        # Test the chord symbol for note and chord
        from music21 import abcFormat, harmony
        from music21.abcFormat import translate

        # default length of this test
        abc_dl = 'L:1/8\n'

        af = abcFormat.ABCFile()
        for abc_text in ('"C"C', '"C"[ceg]'):
            ah = af.readstr(abc_dl + abc_text)
            part = translate.abcToStreamScore(ah).parts[0]
            chord_symbol = part.getElementsByClass(harmony.ChordSymbol)
            self.assertTrue(chord_symbol,
                            'No ChordSymbol found in abc: "%s"' % abc_text)
            for pitch_name in 'CEG':
                self.assertIn(
                    pitch_name, chord_symbol[0].pitchNames,
                    'Pitch not in ChordSymbol of abc: "%s"' % abc_text)
Example #15
0
    def testBasic(self):
        from music21 import abcFormat
        from music21.abcFormat import translate
        from music21.musicxml import m21ToXml

        af = abcFormat.ABCFile()

        GEX = m21ToXml.GeneralObjectExporter()
        
        for i, tf in enumerate(ALL):
            ah = af.readstr(tf)
            environLocal.printDebug([ah.getTitle()])
            s = translate.abcToStreamScore(ah)
            # run musicxml processing to look for internal errors
            #print(repr(s.metadata._workIds['localeOfComposition']._data))
            #print(s.metadata.all())
            try:
                unused_out = GEX.parse(s)
            except UnicodeDecodeError as ude:
                environLocal.warn("About to fail on ABC file #{}".format(i))
                raise ude
Example #16
0
    def testAbc21DirectiveCarryPitch(self):
        from music21 import abcFormat, note
        from music21.abcFormat import translate

        af = abcFormat.ABCFile()
        ah = af.readstr(directiveCarryPitch)
        s = translate.abcToStreamScore(ah)
        notes = s.flat.getElementsByClass(note.Note)
        gSharp = notes[1]
        g8va = notes[3]
        self.assertEqual(gSharp.pitch.midi % 12, g8va.pitch.midi % 12, "Sharp does not carry through measure")
        aFlat = notes[2]
        a = notes[4]
        self.assertEqual(aFlat.pitch.midi, a.pitch.midi, "Flat does not carry through measure")
        fNat = notes[5]
        f = notes[6]
        f8ba = notes[7]
        self.assertEqual(fNat.pitch.midi, f.pitch.midi, "Natural does not carry through measure")
        self.assertEqual(fNat.pitch.midi % 12, f8ba.pitch.midi % 12, "Natural does not carry through measure")
        self.assertEqual(notes[8].pitch.midi, 65, "Natural is ignored")
        self.assertEqual(notes[12].pitch.midi, 72, "Natural is ignored")
Example #17
0
    def testAbc21DirectiveCarryNot(self):
        from music21 import abcFormat, note
        from music21.abcFormat import translate

        af = abcFormat.ABCFile()
        ah = af.readstr(directiveCarryNot)
        s = translate.abcToStreamScore(ah)
        notes = s.flat.getElementsByClass(note.Note)
        gSharp = notes[1]
        g8va = notes[3]
        self.assertTrue(gSharp.pitch.midi % 12 > g8va.pitch.midi % 12, "Sharp carries beyond its octave")
        aFlat = notes[2]
        a = notes[4]
        self.assertTrue(aFlat.pitch.midi < a.pitch.midi, "Flat carries through measure")
        fNat = notes[5]
        f = notes[6]
        f8ba = notes[7]
        self.assertTrue(fNat.pitch.midi < f.pitch.midi, "Natural carries through measure")
        self.assertTrue(fNat.pitch.midi % 12 < f8ba.pitch.midi % 12, "Natural carries beyond its octave")
        self.assertEqual(notes[8].pitch.midi, 65, "Natural is ignored")
        self.assertEqual(notes[12].pitch.midi, 72, "Natural is ignored")