コード例 #1
0
    def testMultipleInstrumentsPiano(self):
        ps1 = stream.PartStaff([
            stream.Measure(
                [instrument.ElectricPiano(),
                 note.Note(type='whole')]),
            stream.Measure(
                [instrument.ElectricOrgan(),
                 note.Note(type='whole')]),
            stream.Measure([instrument.Piano(),
                            note.Note(type='whole')]),
        ])
        ps2 = stream.PartStaff([
            stream.Measure([instrument.Vocalist(),
                            note.Note(type='whole')]),
            stream.Measure([note.Note(type='whole')]),
            stream.Measure([note.Note(type='whole')]),
        ])
        sg = layout.StaffGroup([ps1, ps2])
        s = stream.Score([ps1, ps2, sg])
        scEx = ScoreExporter(s)
        tree = scEx.parse()

        self.assertEqual(
            [el.text for el in tree.findall('.//instrument-name')],
            ['Electric Piano', 'Voice', 'Electric Organ', 'Piano'])
        self.assertEqual(len(tree.findall('.//measure/note/instrument')), 6)
コード例 #2
0
 def testFromScoreNoMeasures(self):
     s = stream.Score()
     s.append(note.Note())
     scExporter = ScoreExporter(s)
     tree = scExporter.parse()
     # Measures should have been made
     self.assertIsNotNone(tree.find('.//measure'))
コード例 #3
0
 def testSetPartsAndRefStreamMeasure(self):
     p = converter.parse('tinynotation: 4/4 c1 d1')
     sc = stream.Score([p])
     sx = ScoreExporter(sc)  # substreams are measures
     sx.setPartsAndRefStream()
     measuresAtOffsetZero = [m for m in p if m.offset == 0]
     self.assertSequenceEqual(measuresAtOffsetZero, p.elements[:1])
コード例 #4
0
    def getET(self, obj):
        from music21.musicxml.m21ToXml import ScoreExporter

        SX = ScoreExporter(obj)
        mxScore = SX.parse()
        helpers.indent(mxScore)
        return mxScore
コード例 #5
0
 def getET(self, obj):
     '''
     Return a <score-partwise> ElementTree.
     Does NOT call makeNotation() like most calls to show() and write().
     '''
     SX = ScoreExporter(obj)
     mxScore = SX.parse()
     helpers.indent(mxScore)
     return mxScore
コード例 #6
0
 def testJoinPartStaffsAgain(self):
     '''
     Regression test for side effects on the stream passed to ScoreExporter
     preventing it from being written out again.
     '''
     from music21 import corpus
     from music21.musicxml.m21ToXml import ScoreExporter
     b = corpus.parse('cpebach')
     SX = ScoreExporter(b)
     SX.parse()
     SX.parse()
コード例 #7
0
    def testMidiInstrumentNoName(self):
        i = instrument.Instrument()
        i.midiProgram = 42
        s = converter.parse('tinyNotation: c1')
        s.measure(1).insert(i)
        sc = stream.Score(s)
        scExporter = ScoreExporter(sc)

        tree = scExporter.parse()
        mxScoreInstrument = tree.findall('.//score-instrument')[0]
        mxMidiInstrument = tree.findall('.//midi-instrument')[0]
        self.assertEqual(mxScoreInstrument.get('id'),
                         mxMidiInstrument.get('id'))
コード例 #8
0
    def testSimple(self):
        # b = converter.parse(corpus.corpora.CoreCorpus().getWorkList('cpebach')[0],
        #    format='musicxml', forceSource=True)
        b = corpus.parse('cpebach')
        # b.show('text')
        # n = b[note.NotRest].first()
        # print(n.expressions)
        # return

        SX = ScoreExporter(b)
        mxScore = SX.parse()

        helpers.indent(mxScore)

        sio = io.BytesIO()

        sio.write(SX.xmlHeader())

        et = ElementTree(mxScore)
        et.write(sio, encoding='utf-8', xml_declaration=False)
        v = sio.getvalue()
        sio.close()

        v = v.decode('utf-8')
        # v = v.replace(' />', '/>')  # normalize

        # b2 = converter.parse(v)
        fp = b.write('musicxml')
        if self.show:
            print(fp)

        with io.open(fp, encoding='utf-8') as f:
            v2 = f.read()
        differ = list(difflib.ndiff(v.splitlines(), v2.splitlines()))
        for i, l in enumerate(differ):
            if l.startswith('-') or l.startswith('?') or l.startswith('+'):
                if 'id=' in l:
                    continue
                if self.show:
                    print(l)
                    # for j in range(i - 1,i + 1):
                    #    print(differ[j])
                    # print('------------------')
        import os
        os.remove(fp)
コード例 #9
0
 def testMultipleInstruments(self):
     '''
     This is a score for two woodwind players both doubling on
     flute and oboe. They both switch to flute and then back to oboe.
     There are six m21 instruments to represent this, but the
     <score-instrument> tags need just four, since no
     musicXML <part> needs two oboes in it, etc., unless
     there is a patch change/MIDI instrument change.
     '''
     p1 = stream.Part([
         stream.Measure([instrument.Oboe(),
                         note.Note(type='whole')]),
         stream.Measure([instrument.Flute(),
                         note.Note(type='whole')]),
         stream.Measure([instrument.Oboe(),
                         note.Note(type='whole')]),
     ])
     p2 = stream.Part([
         stream.Measure([instrument.Oboe(),
                         note.Note(type='whole')]),
         stream.Measure([instrument.Flute(),
                         note.Note(type='whole')]),
         stream.Measure([instrument.Oboe(),
                         note.Note(type='whole')]),
     ])
     s = stream.Score([p1, p2])
     scEx = ScoreExporter(s)
     tree = scEx.parse()
     self.assertEqual(len(tree.findall('.//score-instrument')), 4)
     self.assertEqual(len(tree.findall('.//measure/note/instrument')), 6)
     self.assertEqual(
         tree.find('.//score-instrument').get('id'),
         tree.find('.//measure/note/instrument').get('id'))
     self.assertNotEqual(
         tree.find('.//score-instrument').get('id'),
         tree.findall('.//measure/note/instrument')[-1].get('id'))