music21.musicxml.xmlToM21¶
Functions¶
-
music21.musicxml.xmlToM21.
musicXMLTypeToType
(value)¶ Utility function to convert a MusicXML duration type to an music21 duration type.
Changes ‘long’ to ‘longa’ and deals with a Guitar Pro 5.2 bug in MusicXML export, that exports a 32nd note with the type ‘32th’.
>>> musicxml.xmlToM21.musicXMLTypeToType('long') 'longa' >>> musicxml.xmlToM21.musicXMLTypeToType('32th') '32nd' >>> musicxml.xmlToM21.musicXMLTypeToType('quarter') 'quarter' >>> musicxml.xmlToM21.musicXMLTypeToType(None) Traceback (most recent call last): MusicXMLImportException...
MeasureParser¶
-
class
music21.musicxml.xmlToM21.
MeasureParser
(mxMeasure=None, parent=None)¶ parser to work with a single <measure> tag.
called out for simplicity.
>>> from xml.etree.ElementTree import fromstring as EL
>>> scoreMeasure = '<measure><note><rest/><duration>40320</duration></note></measure>' >>> mxMeasure = EL(scoreMeasure) >>> mp = musicxml.xmlToM21.MeasureParser(mxMeasure) >>> mp.parse() >>> mp.restAndNoteCount['rest'] 1 >>> mp.restAndNoteCount['note'] 0
>>> mp.fullMeasureRest True
MeasureParser
bases
MeasureParser
methods
-
MeasureParser.
addNoteToMeasureOrVoice
(mxNote, n)¶
-
MeasureParser.
addToStaffReference
(mxObjectOrNumber, m21Object)¶ Utility routine for importing musicXML objects; here, we store a reference to the music21 object in a dictionary, where keys are the staff values. Staff values may be None, 1, 2, etc.
-
MeasureParser.
getStaffNumberStr
(mxObjectOrNumber)¶ gets a string representing a staff number, or None from an mxObject or a number...
>>> mp = musicxml.xmlToM21.MeasureParser() >>> from xml.etree.ElementTree import fromstring as EL
>>> gsn = mp.getStaffNumberStr >>> gsn(1) '1' >>> gsn('2') '2' >>> gsn(EL('<note><staff>2</staff></note>')) '2' >>> gsn(EL('<note><pitch><step>C</step><octave>4</octave></pitch></note>')) is None True >>> gsn(EL('<clef number="2"/>')) '2' >>> print(gsn(None)) None
-
MeasureParser.
handleClef
(mxClef)¶ Handles a clef object, appending it to the core, and setting self.lastClefs for the staff number.
>>> import xml.etree.ElementTree as ET >>> mxClef = ET.fromstring('<clef><sign>G</sign><line>2</line></clef>')
>>> MP = musicxml.xmlToM21.MeasureParser() >>> MP.handleClef(mxClef) >>> MP.lastClefs {None: <music21.clef.TrebleClef>}
>>> mxClefBC = ET.fromstring('<clef number="2"><sign>F</sign><line>4</line></clef>') >>> MP.handleClef(mxClefBC) >>> MP.lastClefs['2'] <music21.clef.BassClef> >>> MP.lastClefs[None] <music21.clef.TrebleClef>
-
MeasureParser.
handleKeySignature
(mxKey)¶
-
MeasureParser.
handleStaffDetails
(mxDetails)¶ StaffDetails (staff-details) handles attributes about the staff itself – its size, number of lines, tuning, frets, etc.
It is different from StaffLayout (staff-layout) which only handles relationship of one staff to another (the distance)
Rather than returning a StaffLayout object, it adds it to self.staffLayoutObjects checking to see if there is already an incomplete StaffLayout object for this staff.
-
MeasureParser.
handleTimeSignature
(mxTime)¶
-
MeasureParser.
insertCoreAndRef
(offset, mxObjectOrNumber, m21Object)¶
-
MeasureParser.
parse
()¶
-
MeasureParser.
parseAttributesTag
(mxAttributes)¶
-
MeasureParser.
parseMeasureAttributes
()¶
-
MeasureParser.
parseMeasureNumbers
()¶
-
MeasureParser.
updateLyricsFromList
(n, lyricList)¶ Takes a list of <lyric> elements and update the note’s lyrics from that list.
>>> import xml.etree.ElementTree as ET >>> MP = musicxml.xmlToM21.MeasureParser()
>>> mxLyric1 = ET.fromstring('<lyric><text>Hi</text></lyric>') >>> mxLyric2 = ET.fromstring('<lyric><text>Hi</text></lyric>') >>> n = note.Note() >>> MP.updateLyricsFromList(n, [mxLyric1, mxLyric2]) >>> n.lyrics [<music21.note.Lyric number=1 syllabic=single text="Hi">, <music21.note.Lyric number=2 syllabic=single text="Hi">]
-
MeasureParser.
updateVoiceInformation
()¶
-
MeasureParser.
xmlBackup
(mxObj)¶
-
MeasureParser.
xmlBarline
(mxBarline)¶ Handles everything for putting a barline into a Stream and updating repeat characteristics.
-
MeasureParser.
xmlDirection
(mxDirection)¶ convert a <direction> tag to an expression, metronome mark, etc. and add it to the core and staff direction.
-
MeasureParser.
xmlDirectionTypeToSpanners
(mxObj)¶ Some spanners, such as MusicXML wedge, bracket, and dashes, are encoded as MusicXML directions.
>>> from xml.etree.ElementTree import fromstring as EL >>> MP = musicxml.xmlToM21.MeasureParser() >>> n1 = note.Note("D4") >>> MP.nLast = n1
>>> len(MP.spannerBundle) 0 >>> mxDirectionType = EL('<wedge type="crescendo" number="2"/>') >>> MP.xmlDirectionTypeToSpanners(mxDirectionType) >>> len(MP.spannerBundle) 1 >>> sp = MP.spannerBundle[0] >>> sp <music21.spanner.Crescendo >
>>> mxDirectionType2 = EL('<wedge type="stop" number="2"/>') >>> MP.xmlDirectionTypeToSpanners(mxDirectionType2) >>> len(MP.spannerBundle) 1 >>> sp = MP.spannerBundle[0] >>> sp <music21.spanner.Crescendo <music21.note.Note D>>
-
MeasureParser.
xmlForward
(mxObj)¶
-
MeasureParser.
xmlGraceToGrace
(mxGrace, noteOrChord)¶ Given a completely formed, non-grace Note or Chord that should become one create and return a m21 grace version of the same.
-
MeasureParser.
xmlHarmony
(mxHarmony)¶
-
MeasureParser.
xmlNotations
(mxNotations, n)¶
-
MeasureParser.
xmlNotationsToSpanners
(mxNotations, n)¶
-
MeasureParser.
xmlNoteToGeneralNoteHelper
(n, mxNote, freeSpanners=True)¶
-
MeasureParser.
xmlNotehead
(n, mxNotehead)¶
-
MeasureParser.
xmlOneSpanner
(mxObj, target, spannerClass, allowDuplicateIds=False)¶ Some spanner types do not have an id necessarily, we allow duplicates of them if allowDuplicateIds is True. Wedges are one.
-
MeasureParser.
xmlOrnamentToExpression
(mxObj)¶ Convert mxOrnament into a music21 ornament.
This only processes non-spanner ornaments. Many mxOrnaments are spanners: these are handled elsewhere.
Returns None if cannot be converted or not defined.
Return an articulation from an mxObj, setting placement
>>> from xml.etree.ElementTree import fromstring as EL >>> MP = musicxml.xmlToM21.MeasureParser()
>>> mxOrn = EL('<inverted-turn placement="above"/>') >>> a = MP.xmlOrnamentToExpression(mxOrn) >>> a <music21.expressions.InvertedTurn> >>> a.placement 'above'
If it can’t be converted, return None
>>> mxOrn = EL('<crazy-slide placement="above"/>') >>> a = MP.xmlOrnamentToExpression(mxOrn) >>> a is None True
Not supported currently: ‘accidental-mark’, ‘vertical-turn’, ‘delayed-turn’, ‘delayed-inverted-turn’
-
MeasureParser.
xmlPrint
(mxPrint)¶ <print> handles changes in pages, numbering, layout, etc. so can generate PageLayout, SystemLayout, or StaffLayout objects.
Should also be able to set measure attributes on self.stream
-
MeasureParser.
xmlStaffLayoutFromStaffDetails
(mxDetails)¶ Returns a new StaffLayout object from staff-details.
-
MeasureParser.
xmlTechnicalToArticulation
(mxObj)¶ Convert an mxArticulationMark to a music21.articulations.Articulation object or one of its subclasses.
>>> from xml.etree.ElementTree import fromstring as EL >>> MP = musicxml.xmlToM21.MeasureParser()
>>> mxTech = EL('<down-bow placement="below"/>') >>> a = MP.xmlTechnicalToArticulation(mxTech) >>> a <music21.articulations.DownBow> >>> a.placement 'below'
-
MeasureParser.
xmlToAccidental
(mxAccidental, inputM21=None)¶ >>> from xml.etree.ElementTree import fromstring as EL >>> MP = musicxml.xmlToM21.MeasureParser()
>>> a = EL('<accidental>half-flat</accidental>') >>> b = pitch.Accidental() >>> MP.xmlToAccidental(a, b) >>> b.name 'half-flat' >>> b.alter -0.5
-
MeasureParser.
xmlToArticulation
(mxObj)¶ Return an articulation from an mxObj, setting placement
>>> from xml.etree.ElementTree import fromstring as EL >>> MP = musicxml.xmlToM21.MeasureParser()
>>> mxArt = EL('<spiccato placement="above"/>') >>> a = MP.xmlToArticulation(mxArt) >>> a <music21.articulations.Spiccato> >>> a.placement 'above'
-
MeasureParser.
xmlToBarline
(mxBarline, inputM21=None)¶ Given an mxBarline, fill the necessary parameters
>>> import xml.etree.ElementTree as ET >>> MP = musicxml.xmlToM21.MeasureParser()
>>> mxBarline = ET.fromstring( ... '<barline location="right"><bar-style>light-light</bar-style></barline>') >>> b = MP.xmlToBarline(mxBarline) >>> b <music21.bar.Barline style=double> >>> b.style # different in music21 than musicxml 'double' >>> b.location 'right'
-
MeasureParser.
xmlToBeam
(mxBeam, inputM21=None)¶ given an mxBeam object return a
Beam
object>>> from xml.etree.ElementTree import fromstring as EL >>> MP = musicxml.xmlToM21.MeasureParser()
>>> mxBeam = EL('<beam>begin</beam>') >>> a = MP.xmlToBeam(mxBeam) >>> a.type 'start'
>>> mxBeam = EL('<beam>continue</beam>') >>> a = MP.xmlToBeam(mxBeam) >>> a.type 'continue'
>>> mxBeam = EL('<beam>end</beam>') >>> a = MP.xmlToBeam(mxBeam) >>> a.type 'stop'
>>> mxBeam = EL('<beam>forward hook </beam>') >>> a = MP.xmlToBeam(mxBeam) >>> a.type 'partial' >>> a.direction 'right'
>>> mxBeam = EL('<beam>backward hook</beam>') >>> a = MP.xmlToBeam(mxBeam) >>> a.type 'partial' >>> a.direction 'left'
>>> mxBeam = EL('<beam>crazy</beam>') >>> a = MP.xmlToBeam(mxBeam) Traceback (most recent call last): MusicXMLImportException: unexpected beam type encountered (crazy)
-
MeasureParser.
xmlToBeams
(mxBeamList, inputM21=None)¶ given a list of mxBeam objects, sets the beamsList
>>> from xml.etree.ElementTree import fromstring as EL >>> MP = musicxml.xmlToM21.MeasureParser()
>>> mxBeam1 = EL('<beam>begin</beam>') >>> mxBeam2 = EL('<beam>begin</beam>') >>> mxBeamList = [mxBeam1, mxBeam2] >>> b = MP.xmlToBeams(mxBeamList) >>> b <music21.beam.Beams <music21.beam.Beam 1/start>/<music21.beam.Beam 2/start>>
-
MeasureParser.
xmlToChord
(mxNoteList)¶ Given an a list of mxNotes, fill the necessary parameters
>>> from xml.etree.ElementTree import fromstring as EL >>> MP = musicxml.xmlToM21.MeasureParser() >>> MP.divisions = 10080
>>> qnDuration = r'<duration>7560</duration><type>quarter</type>'
>>> a = EL(r'<note><pitch><step>A</step><octave>3</octave></pitch>' ... + qnDuration + '</note>') >>> b = EL(r'<note><chord/><pitch><step>B</step><octave>3</octave></pitch>' ... + qnDuration + '</note>')
>>> c = MP.xmlToChord([a, b]) >>> len(c.pitches) 2 >>> c.pitches[0] <music21.pitch.Pitch A3> >>> c.pitches[1] <music21.pitch.Pitch B3>
>>> a = EL(r'<note><pitch><step>A</step><octave>3</octave></pitch>' + qnDuration + ... '<notehead>diamond</notehead></note>') >>> c = MP.xmlToChord([a, b]) >>> c.getNotehead(c.pitches[0]) 'diamond'
-
MeasureParser.
xmlToChordSymbol
(mxHarmony)¶ Convert a <harmony> tag to a harmony.ChordSymbol object:
>>> from xml.etree.ElementTree import fromstring as EL >>> MP = musicxml.xmlToM21.MeasureParser()
>>> elStr = '<harmony><root><root-step>D</root-step><root-alter>-1</root-alter>' >>> elStr += '</root><kind>major-seventh</kind></harmony>' >>> mxHarmony = EL(elStr)
>>> cs = MP.xmlToChordSymbol(mxHarmony) >>> cs <music21.harmony.ChordSymbol D-maj7>
>>> cs.figure 'D-maj7'
>>> cs.pitches (<music21.pitch.Pitch D-3>, <music21.pitch.Pitch F3>, <music21.pitch.Pitch A-3>, <music21.pitch.Pitch C4>)
>>> cs.root() <music21.pitch.Pitch D-3>
TODO: this is very classically-oriented. Make more Jazz/Rock like possible/default?.
>>> mxHarmony.find('kind').text = 'major-sixth' >>> cs = MP.xmlToChordSymbol(mxHarmony) >>> cs <music21.harmony.ChordSymbol D-6>
>>> cs.figure 'D-6'
>>> cs.pitches (<music21.pitch.Pitch D-3>, <music21.pitch.Pitch F3>, <music21.pitch.Pitch A-3>, <music21.pitch.Pitch B-3>)
>>> cs.root() <music21.pitch.Pitch D-3>
-
MeasureParser.
xmlToClef
(mxClef)¶ >>> import xml.etree.ElementTree as ET >>> mxClef = ET.fromstring('<clef><sign>G</sign><line>2</line></clef>')
>>> MP = musicxml.xmlToM21.MeasureParser() >>> MP.xmlToClef(mxClef) <music21.clef.TrebleClef>
>>> mxClef = ET.fromstring('<clef><sign>TAB</sign></clef>') >>> MP.xmlToClef(mxClef) <music21.clef.TabClef>
-
MeasureParser.
xmlToDuration
(mxNote, inputM21=None)¶ Translate a MusicXML <note> object’s <duration>, <type>, <dot>, tuplets, etc. to a music21
Duration
object.>>> from xml.etree.ElementTree import fromstring as EL >>> MP = musicxml.xmlToM21.MeasureParser()
>>> MP.divisions = 10080
>>> mxNote = EL('<note><pitch><step>D</step>' + ... '<alter>-1</alter><octave>6</octave></pitch>' + ... '<duration>7560</duration>' + ... '<type>eighth</type><dot/></note>')
>>> c = duration.Duration() >>> MP.xmlToDuration(mxNote, c) >>> c <music21.duration.Duration 0.75> >>> c.quarterLength 0.75 >>> c.type 'eighth' >>> c.dots 1
-
MeasureParser.
xmlToKeySignature
(mxKey)¶ >>> import xml.etree.ElementTree as ET >>> mxKey = ET.fromstring('<key><fifths>-4</fifths><mode>minor</mode></key>')
>>> MP = musicxml.xmlToM21.MeasureParser() >>> MP.xmlToKeySignature(mxKey) <music21.key.KeySignature of 4 flats, mode minor>
-
MeasureParser.
xmlToLyric
(mxLyric, inputM21=None)¶ Translate a MusicXML <lyric> tag to a music21
Lyric
object.If inputM21 is a
Lyric
object, then the values of the mxLyric are transfered there and nothing returned.Otherwise, a new Lyric object is created and returned.
>>> import xml.etree.ElementTree as ET >>> MP = musicxml.xmlToM21.MeasureParser()
>>> mxLyric = ET.fromstring('<lyric number="4"><syllabic>single</syllabic>' + ... '<text>word</text></lyric>') >>> lyricObj = note.Lyric() >>> MP.xmlToLyric(mxLyric, lyricObj) >>> lyricObj <music21.note.Lyric number=4 syllabic=single text="word">
Non-numeric MusicXML lyric “number”s are converted to identifiers:
>>> mxLyric.set('number', 'part2verse1') >>> l2 = MP.xmlToLyric(mxLyric) >>> l2 <music21.note.Lyric number=0 identifier="part2verse1" syllabic=single text="word">
-
MeasureParser.
xmlToNote
(mxNote)¶ handles everything for creating a Note or Rest or Chord
-
MeasureParser.
xmlToOffset
(mxObj)¶ Finds an <offset> inside the mxObj and returns it as a music21 offset (in quarterLengths)
>>> from xml.etree.ElementTree import fromstring as EL >>> MP = musicxml.xmlToM21.MeasureParser() >>> MP.divisions = 40 >>> off = EL(r'<direction><offset>100</offset></direction>') >>> MP.xmlToOffset(off) 2.5
-
MeasureParser.
xmlToPitch
(mxNote, inputM21=None)¶ Given a MusicXML Note object, set this Pitch object to its values.
>>> from xml.etree.ElementTree import fromstring as EL >>> MP = musicxml.xmlToM21.MeasureParser()
>>> b = EL('<note><pitch><step>E</step><alter>-1</alter><octave>3</octave></pitch></note>') >>> a = MP.xmlToPitch(b) >>> print(a) E-3
Conflicting alter and accidental – accidental wins:
>>> b = EL('<note><pitch><step>E</step><alter>-1</alter><octave>3</octave></pitch>' + ... '<accidental>sharp</accidental></note>') >>> a = MP.xmlToPitch(b) >>> print(a) E#3
-
MeasureParser.
xmlToRepeat
(mxBarline, inputM21=None)¶ Given an mxBarline (not an mxRepeat object) with repeatObj as a parameter, file the necessary parameters and return a bar.Repeat() object
>>> import xml.etree.ElementTree as ET >>> MP = musicxml.xmlToM21.MeasureParser()
>>> mxBarline = ET.fromstring('<barline><bar-style>light-heavy</bar-style>' + ... '<repeat direction="backward"/></barline>') >>> r = MP.xmlToRepeat(mxBarline) >>> r <music21.bar.Repeat direction=end>
Test that the music21 style for a backwards repeat is called “final” (because it resembles a final barline) but that the musicxml style is called light-heavy.
>>> r.style 'final' >>> r.direction 'end'
# TODO: replace after changing output not to use toMxObjects
>>> mxBarline2 = musicxml.toMxObjects.repeatToMx(r) >>> mxBarline2.get('barStyle') 'light-heavy'
-
MeasureParser.
xmlToRest
(mxRest)¶ Takes a <note> tag that has been shown to have a <rest> tag in it and return a rest.
>>> from xml.etree.ElementTree import fromstring as EL >>> MP = musicxml.xmlToM21.MeasureParser()
>>> mxr = EL('<note><rest/><duration>5040</duration><type>eighth</type></note>') >>> r = MP.xmlToRest(mxr) >>> r <music21.note.Rest rest> >>> r.duration.quarterLength 0.5
>>> mxr = EL('<note><rest><display-step>G</display-step>' + ... '<display-octave>4</display-octave>' + ... '</rest><duration>5040</duration><type>eighth</type></note>') >>> r = MP.xmlToRest(mxr) >>> r <music21.note.Rest rest>
A rest normally lies at B4 in treble clef, but here we have put it at G4, so we’ll shift it down two steps.
>>> r.stepShift -2
Clef context matters, here we will set it for notes that don’t specify a staff:
>>> MP.lastClefs[None] = clef.BassClef() >>> r = MP.xmlToRest(mxr)
Now this is a high rest:
>>> r.stepShift 10
Test full measure rest.
>>> mxr = EL('<note><rest measure="yes"/><duration>40320</duration></note>') >>> r = MP.xmlToRest(mxr) >>> MP.fullMeasureRest True
Note that we do NOT set r‘s .fullMeasure to True or always, but keep it as “auto” so that changes in time signature, etc. will affect output.
-
MeasureParser.
xmlToSimpleNote
(mxNote, freeSpanners=True)¶ Translate a MusicXML <note> (without <chord/>) to a
Note
.The spannerBundle parameter can be a list or a Stream for storing and processing Spanner objects.
If inputM21 is not None then that object is used for translating. Otherwise a new Note is created.
if freeSpanners is False then pending spanners will not be freed
Returns a note.Note object.
>>> from xml.etree.ElementTree import fromstring as EL >>> MP = musicxml.xmlToM21.MeasureParser()
>>> MP.divisions = 10080
>>> mxNote = EL('<note><pitch><step>D</step>' + ... '<alter>-1</alter><octave>6</octave></pitch>' + ... '<duration>7560</duration>' + ... '<type>eighth</type><dot/></note>')
>>> n = MP.xmlToSimpleNote(mxNote) >>> n <music21.note.Note D-> >>> n.octave 6 >>> n.duration <music21.duration.Duration 0.75>
>>> beams = EL('<beam>begin</beam>') >>> mxNote.append(beams) >>> n = MP.xmlToSimpleNote(mxNote) >>> n.beams <music21.beam.Beams <music21.beam.Beam 1/start>>
>>> stem = EL('<stem>up</stem>') >>> mxNote.append(stem) >>> n = MP.xmlToSimpleNote(mxNote) >>> n.stemDirection 'up'
# TODO: beams over rests?
-
MeasureParser.
xmlToTempoIndication
(mxMetronome, mxWords=None)¶ Given an mxMetronome, convert to either a TempoIndication subclass, either a tempo.MetronomeMark or tempo.MetricModulation.
>>> from xml.etree.ElementTree import fromstring as EL >>> MP = musicxml.xmlToM21.MeasureParser()
>>> m = EL(r'<metronome><per-minute>125</per-minute>' + ... '<beat-unit>half</beat-unit></metronome>') >>> MP.xmlToTempoIndication(m) <music21.tempo.MetronomeMark Half=125.0>
Metric modulation:
>>> m = EL(r'<metronome><beat-unit>long</beat-unit><beat-unit>32nd</beat-unit>' + ... '<beat-unit-dot/></metronome>') >>> MP.xmlToTempoIndication(m) <music21.tempo.MetricModulation <music21.tempo.MetronomeMark Imperfect Longa=None>=<music21.tempo.MetronomeMark Dotted 32nd=None>>
-
MeasureParser.
xmlToTextExpression
(mxWords)¶ Given an mxDirection, create a textExpression
-
MeasureParser.
xmlToTimeSignature
(mxTime)¶ >>> import xml.etree.ElementTree as ET >>> mxTime = ET.fromstring('<time><beats>3</beats><beat-type>8</beat-type></time>')
>>> MP = musicxml.xmlToM21.MeasureParser() >>> MP.xmlToTimeSignature(mxTime) <music21.meter.TimeSignature 3/8>
-
MeasureParser.
xmlToTremolo
(mxTremolo, n)¶
-
MeasureParser.
xmlToTuplet
(mxNote, inputM21=None)¶ Given an mxNote, based on mxTimeModification and mxTuplet objects, return a Tuplet object (or alter the input object and then return it)
>>> import xml.etree.ElementTree as ET >>> MP = musicxml.xmlToM21.MeasureParser()
>>> mxNote = ET.fromstring('<note><type>16th</type>' + ... '<time-modification><actual-notes>5</actual-notes>' + ... '<normal-notes>4</normal-notes></time-modification></note>') >>> t = MP.xmlToTuplet(mxNote) >>> t <music21.duration.Tuplet 5/4/16th>
-
MeasureParser.
xmlTransposeToInterval
(mxTranspose)¶ Convert a MusicXML Transpose object to a music21 Interval object. >>> import xml.etree.ElementTree as ET >>> MP = musicxml.xmlToM21.MeasureParser()
>>> t = ET.fromstring('<transpose><diatonic>-1</diatonic>' + ... '<chromatic>-2</chromatic></transpose>') >>> MP.xmlTransposeToInterval(t) <music21.interval.Interval M-2>
>>> t = ET.fromstring('<transpose><diatonic>-5</diatonic>' + ... '<chromatic>-9</chromatic></transpose>') >>> MP.xmlTransposeToInterval(t) <music21.interval.Interval M-6>
It should be like this:
>>> t = ET.fromstring('<transpose><diatonic>-8</diatonic><chromatic>-2</chromatic>' + ... '<octave-change>-1</octave-change></transpose>') >>> MP.xmlTransposeToInterval(t) <music21.interval.Interval M-9>
but it is sometimes encoded this way (Finale; MuseScore), so we will deal...
>>> t = ET.fromstring('<transpose><diatonic>-1</diatonic><chromatic>-2</chromatic>' + ... '<octave-change>-1</octave-change></transpose>') >>> MP.xmlTransposeToInterval(t) <music21.interval.Interval M-9>
Methods inherited from XMLParserBase
:
MusicXMLImporter¶
-
class
music21.musicxml.xmlToM21.
MusicXMLImporter
¶ Object for importing .xml, .mxl, .musicxml, MusicXML files into music21.
MusicXMLImporter
bases
MusicXMLImporter
methods
-
MusicXMLImporter.
creatorToContributor
(creator, inputM21=None)¶ Given an <creator> tag, fill the necessary parameters of a Contributor.
>>> import xml.etree.ElementTree as ET >>> creator = ET.fromstring('<creator type="composer">Beethoven, Ludwig van</creator>')
>>> MI = musicxml.xmlToM21.MusicXMLImporter() >>> c = MI.creatorToContributor(creator) >>> c <music21.metadata.primitives.Contributor object at 0x...> >>> c.role 'composer' >>> c.name 'Beethoven, Ludwig van'
Pass in a Contributor object and set it...
>>> c2 = metadata.Contributor() >>> MI.creatorToContributor(creator, c2) >>> c2.role 'composer'
-
MusicXMLImporter.
parsePartList
(mxScore)¶ Parses the <part-list> tag and adds <score-part> entries into self.partIdDict[partId] and adds them to any open <part-group> entries, stored as PartGroup objects in self.partGroupList
-
MusicXMLImporter.
parseXMLText
()¶
-
MusicXMLImporter.
partGroups
()¶ set StaffGroup objects from the <part-group> tags.
-
MusicXMLImporter.
readFile
(filename)¶
-
MusicXMLImporter.
scoreFromFile
(filename)¶ main program: opens a file given by filename and returns a complete music21 Score from it.
-
MusicXMLImporter.
xmlCreditToTextBox
(mxCredit)¶ Convert a MusicXML credit to a music21 TextBox
>>> import xml.etree.ElementTree as ET >>> credit = ET.fromstring( ... '<credit page="2"><credit-words>Testing</credit-words></credit>')
>>> MI = musicxml.xmlToM21.MusicXMLImporter() >>> tb = MI.xmlCreditToTextBox(credit) >>> tb.page 2 >>> tb.content 'Testing'
-
MusicXMLImporter.
xmlDefaultsToScoreLayout
(mxDefaults, inputM21=None)¶ Convert a <defaults> tag to a
ScoreLayout
object
-
MusicXMLImporter.
xmlMetadata
(el=None, inputM21=None)¶ Converts part of the root element into a metadata object
Supported: work-title, work-number, opus, movement-number, movement-title, identification (creator)
Not supported: rights, encoding, source, relation, miscellaneous
-
MusicXMLImporter.
xmlPartToPart
(mxPart, partIdDict)¶
-
MusicXMLImporter.
xmlRootToScore
(mxScore, inputM21=None)¶ parse an xml file into a Score() object.
Methods inherited from XMLParserBase
:
PartGroup¶
-
class
music21.musicxml.xmlToM21.
PartGroup
(mxPartGroup)¶ Small helper class for keeping track of part-groups from XML since they are converted to StaffGroup spanners much later.
PartGroup
methods
-
PartGroup.
add
(partGroupId)¶
PartParser¶
-
class
music21.musicxml.xmlToM21.
PartParser
(mxPart=None, mxPartInfo=None, parent=None)¶ parser to work with a single <part> tag.
called out for multiprocessing potential in future
PartParser
bases
PartParser
read-only properties
-
PartParser.
parent
¶
PartParser
methods
-
PartParser.
adjustTimeAttributesFromMeasure
(m)¶
-
PartParser.
getDefaultInstrument
(mxPartInfo=None)¶ >>> scorePart = (r'<score-part id="P4"><part-name>Bass</part-name>' + ... '<part-abbreviation>B.</part-abbreviation>' + ... '<score-instrument id="P4-I4">' + ... ' <instrument-name>Instrument 4</instrument-name>' + ... '</score-instrument>' + ... '<midi-instrument id="P4-I4">' + ... ' <midi-channel>4</midi-channel>' + ... '<midi-program>1</midi-program>' + ... '</midi-instrument>' + ... '</score-part>') >>> from xml.etree.ElementTree import fromstring as EL >>> PP = musicxml.xmlToM21.PartParser()
>>> mxPartInfo = EL(scorePart) >>> i = PP.getDefaultInstrument(mxPartInfo) >>> i.partName 'Bass' >>> i.partAbbreviation 'B.'
-
PartParser.
measureParsingError
(mxMeasure, e)¶
-
PartParser.
parse
()¶
-
PartParser.
parseMeasures
()¶
-
PartParser.
parsePartInfo
()¶
-
PartParser.
separateOutPartStaves
()¶ Take a Part with multiple staves and make them a set of PartStaff objects.
-
PartParser.
xmlMeasureToMeasure
(mxMeasure)¶ Convert a measure element to a Measure, using
MeasureParser
>>> from xml.etree.ElementTree import fromstring as EL
Testing full measure rest parsing:
Here is a measure with a rest that lasts 4 beats but we will put it in a 3/4 context.
>>> scoreMeasure = '<measure><note><rest/><duration>40320</duration></note></measure>' >>> mxMeasure = EL(scoreMeasure) >>> pp = musicxml.xmlToM21.PartParser() >>> pp.lastDivisions 10080 >>> 40320 / 10080 4.0 >>> pp.lastTimeSignature = meter.TimeSignature('3/4') >>> m = pp.xmlMeasureToMeasure(mxMeasure)
Test that the rest lasts three, not four beats:
>>> m.notesAndRests[0] <music21.note.Rest rest> >>> m.notesAndRests[0].duration.quarterLength 3.0
Methods inherited from XMLParserBase
:
XMLParserBase¶
-
class
music21.musicxml.xmlToM21.
XMLParserBase
¶ contains functions that could be called at multiple levels of parsing (Score, Part, Measure).
XMLParserBase
methods
-
XMLParserBase.
setPosition
(mxObject, m21Object)¶ get positioning information for an object from x-position
-
XMLParserBase.
xmlPageLayoutToPageLayout
(mxPageLayout, inputM21=None)¶ get a PageLayout object from an mxPageLayout
Called out from mxPrintToPageLayout because it is also used in the <defaults> tag
-
XMLParserBase.
xmlPrintToPageLayout
(mxPrint, inputM21=None)¶ Given an mxPrint object, set object data for the print section of a layout.PageLayout object
>>> from xml.etree.ElementTree import fromstring as El >>> MP = musicxml.xmlToM21.MeasureParser()
>>> mxPrint = El('<print new-page="yes" page-number="5">' + ... ' <page-layout><page-height>4000</page-height>' + ... ' <page-margins><left-margin>20</left-margin>' + ... ' <right-margin>30.25</right-margin></page-margins>' + ... '</page-layout></print>')
>>> pl = MP.xmlPrintToPageLayout(mxPrint) >>> pl.isNew True >>> pl.rightMargin 30.25 >>> pl.leftMargin 20 >>> pl.pageNumber 5 >>> pl.pageHeight 4000
-
XMLParserBase.
xmlPrintToSystemLayout
(mxPrint, inputM21=None)¶ Given an mxPrint object, set object data
>>> from xml.etree.ElementTree import fromstring as El >>> MP = musicxml.xmlToM21.MeasureParser()
>>> mxPrint = El('<print new-system="yes">' + ... ' <system-layout><system-distance>55</system-distance>' + ... ' <system-margins><left-margin>20</left-margin>' + ... ' <right-margin>30.25</right-margin></system-margins>' + ... '</system-layout></print>') >>> sl = MP.xmlPrintToSystemLayout(mxPrint) >>> sl.isNew True >>> sl.rightMargin 30.25 >>> sl.leftMargin 20 >>> sl.distance 55
-
XMLParserBase.
xmlStaffLayoutToStaffLayout
(mxStaffLayout, inputM21=None)¶ get a StaffLayout object from an <staff-layout> tag
In music21, the <staff-layout> and <staff-details> are intertwined in a StaffLayout object.
-
XMLParserBase.
xmlSystemLayoutToSystemLayout
(mxSystemLayout, inputM21=None)¶ get a SystemLayout object from an <system-layout> element
Called out from xmlPrintToSystemLayout because it is also used in the <defaults> tag