Previous topic

music21.musicxml.mxObjects

Next topic

music21.musicxml.xmlHandler

Table Of Contents

Table Of Contents

This Page

music21.musicxml.toMxObjects

Low-level conversion routines from music21 objects to musicxml.mxObject representation.

Functions

music21.musicxml.toMxObjects.streamToMx(s, spannerBundle=None)

Create and return a musicxml Score object from a Stream or Score

This is the most common entry point for conversion of a Stream to MusicXML.

>>> n1 = note.Note()
>>> measure1 = stream.Measure()
>>> measure1.insert(n1)
>>> s1 = stream.Stream()
>>> s1.insert(measure1)
>>> mxScore = musicxml.toMxObjects.streamToMx(s1)
>>> mxPartList = mxScore.get('partList')
music21.musicxml.toMxObjects.streamPartToMx(part, instStream=None, meterStream=None, refStreamOrTimeRange=None, spannerBundle=None)

Convert a Part object (or any Stream representing a Part) to musicxml

If there are Measures within this Stream, use them to create and return an mxPart and mxScorePart.

An instObj may be assigned from caller; this Instrument is pre-collected from this Stream in order to configure id and midi-channel values.

The meterStream, if given, provides a template of meters.

music21.musicxml.toMxObjects.measureToMx(m, spannerBundle=None, mxTranspose=None)

Translate a Measure to a MusicXML Measure object.

music21.musicxml.toMxObjects.accidentalToMx(a)

returns a musicxml Accidental object from a music21 pitch Accidental object

>>> a = pitch.Accidental()
>>> a.set('half-sharp')
>>> a.alter == .5
True
>>> mxAccidental = musicxml.toMxObjects.accidentalToMx(a)
>>> mxAccidental.get('content')
'quarter-sharp'
music21.musicxml.toMxObjects.addSupportsToMxScore(s, mxScore)

add information about what this score actually supports

Currently, if Stream.definesExplicitSystemBreaks is set to True then a supports new-system tag is added.

music21.musicxml.toMxObjects.articulationToMxArticulation(articulationMark)

Returns a class (mxArticulationMark) that represents the MusicXML structure of an articulation mark.

>>> a = articulations.Accent()
>>> a.placement = 'below'
>>> mxArticulationMark = musicxml.toMxObjects.articulationToMxArticulation(a)
>>> mxArticulationMark
<accent placement=below>
music21.musicxml.toMxObjects.articulationToMxTechnical(articulationMark)

Returns a class (mxTechnicalMark) that represents the MusicXML structure of an articulation mark that is primarily a TechnicalIndication.

>>> a = articulations.UpBow()
>>> a.placement = 'below'
>>> mxTechnicalMark = musicxml.toMxObjects.articulationToMxTechnical(a)
>>> mxTechnicalMark
<up-bow placement=below>
music21.musicxml.toMxObjects.articulationsAndExpressionsToMx(target, mxNoteList)

The target parameter is the music21 object (Note, Chord, etc.) that will have the articulation or expression applied.

mxNoteList is a list of mxNotes to which these articulations or expressions apply. Only the first of the component notes (e.g., of a chord) get the articulations.

music21.musicxml.toMxObjects.barlineToMx(barObject)

Translate a music21 bar.Bar object to an mxBar while making two substitutions: double -> light-light and final -> light-heavy as shown below.

>>> b = bar.Barline('final')
>>> mxBarline = musicxml.toMxObjects.barlineToMx(b)
>>> mxBarline.get('barStyle')
'light-heavy'
music21.musicxml.toMxObjects.beamToMx(beamObject)
>>> a = beam.Beam()
>>> a.type = 'start'
>>> a.number = 1
>>> b = musicxml.toMxObjects.beamToMx(a)
>>> b.get('charData')
'begin'
>>> b.get('number')
1
>>> a.type = 'continue'
>>> b = musicxml.toMxObjects.beamToMx(a)
>>> b.get('charData')
'continue'
>>> a.type = 'stop'
>>> b = musicxml.toMxObjects.beamToMx(a)
>>> b
<beam number=1 charData=end>
>>> b.get('charData')
'end'
>>> a.type = 'partial'
>>> a.direction = 'left'
>>> b = musicxml.toMxObjects.beamToMx(a)
>>> b.get('charData')
'backward hook'
>>> a.direction = 'right'
>>> b = musicxml.toMxObjects.beamToMx(a)
>>> b.get('charData')
'forward hook'
>>> a.direction = None
>>> b = musicxml.toMxObjects.beamToMx(a)
Traceback (most recent call last):
ToMxObjectsException: partial beam defined without a proper direction set (set to None)
>>> a.type = 'crazy'
>>> b = musicxml.toMxObjects.beamToMx(a)
Traceback (most recent call last):
ToMxObjectsException: unexpected beam type encountered (crazy)
music21.musicxml.toMxObjects.beamsToMx(beamsObj)

Returns a list of mxBeam objects from a Beams object

>>> a = beam.Beams()
>>> a.fill(2, type='start')
>>> mxBeamList = musicxml.toMxObjects.beamsToMx(a)
>>> len(mxBeamList)
2
>>> mxBeamList[0]
<beam number=1 charData=begin>
>>> mxBeamList[1]
<beam number=2 charData=begin>
music21.musicxml.toMxObjects.chordSymbolToMx(cs)

Convert a ChordSymbol object to an mxHarmony object.

>>> cs = harmony.ChordSymbol()
>>> cs.root('E-')
>>> cs.bass('B-')
>>> cs.inversion(2, transposeOnSet = False)
>>> cs.romanNumeral = 'I64'
>>> cs.chordKind = 'major'
>>> cs.chordKindStr = 'M'
>>> cs
<music21.harmony.ChordSymbol E-/B->
>>> mxHarmony = musicxml.toMxObjects.chordSymbolToMx(cs)
>>> mxHarmony
<harmony <root root-step=E root-alter=-1> function=I64 <kind text=M charData=major> inversion=2 <bass bass-step=B bass-alter=-1>>
>>> hd = harmony.ChordStepModification()
>>> hd.modType = 'alter'
>>> hd.interval = -1
>>> hd.degree = 3
>>> cs.addChordStepModification(hd)
>>> mxHarmony = musicxml.toMxObjects.chordSymbolToMx(cs)
>>> mxHarmony
<harmony <root root-step=E root-alter=-1> function=I64 <kind text=M charData=major> inversion=2 <bass bass-step=B bass-alter=-1> <degree <degree-value charData=3> <degree-alter charData=-1> <degree-type charData=alter>>>

Test altered chords:

Is this correct?

>>> f = harmony.ChordSymbol('F sus add 9')
>>> mxF = musicxml.toMxObjects.chordSymbolToMx(f)
>>> mxF
<harmony <root root-step=G> <kind text= charData=suspended-fourth> inversion=3 <bass bass-step=F> <degree <degree-value charData=9> <degree-alter > <degree-type charData=add>>>
music21.musicxml.toMxObjects.chordToMx(c, spannerBundle=None)

Returns a List of mxNotes Attributes of notes are merged from different locations: first from the duration objects, then from the pitch objects. Finally, GeneralNote attributes are added

>>> a = chord.Chord()
>>> a.quarterLength = 2
>>> b = pitch.Pitch('A-')
>>> c = pitch.Pitch('D-')
>>> d = pitch.Pitch('E-')
>>> e = a.pitches = [b, c, d]
>>> len(e)
3
>>> mxNoteList = musicxml.toMxObjects.chordToMx(a)
>>> len(mxNoteList) # get three mxNotes
3
>>> mxNoteList[0].get('chord')
False
>>> mxNoteList[1].get('chord')
True
>>> mxNoteList[2].get('chord')
True
>>> mxNoteList[0].get('pitch')
<pitch step=A alter=-1 octave=4>
>>> mxNoteList[1].get('pitch')
<pitch step=D alter=-1 octave=4>
>>> mxNoteList[2].get('pitch')
<pitch step=E alter=-1 octave=4>

Test that notehead translation works:

>>> g = note.Note('c4')
>>> g.notehead = 'diamond'
>>> h = pitch.Pitch('g3')
>>> i = chord.Chord([h, g])
>>> i.quarterLength = 2
>>> listOfMxNotes = musicxml.toMxObjects.chordToMx(i)
>>> listOfMxNotes[0].get('chord')
False
>>> listOfMxNotes[1].noteheadObj.get('charData')
'diamond'
music21.musicxml.toMxObjects.clefToMxClef(clefObj)

Given a music21 Clef object, return a MusicXML Clef object.

>>> gc = clef.GClef()
>>> gc
<music21.clef.GClef>
>>> mxc = musicxml.toMxObjects.clefToMxClef(gc)
>>> mxc.get('sign')
'G'
>>> b = clef.Treble8vbClef()
>>> b.octaveChange
-1
>>> mxc2 = musicxml.toMxObjects.clefToMxClef(b)
>>> mxc2.get('sign')
'G'
>>> mxc2.get('clefOctaveChange')
-1
music21.musicxml.toMxObjects.codaToMx(rm)

Returns a musicxml.mxObjects.Direction object with a musicxml.mxObjects.Coda mark in it

music21.musicxml.toMxObjects.configureMxPartGroupFromStaffGroup(staffGroup)

Create and configure an mxPartGroup object from a staff group spanner. Note that this object is not completely formed by this procedure.

music21.musicxml.toMxObjects.contributorToMxCreator(contribObj)

Return a mxCreator object from a Contributor object.

>>> md = metadata.Metadata()
>>> md.composer = 'frank'
>>> contrib = md._contributors[0]
>>> contrib
<music21.metadata.primitives.Contributor object at 0x...>
>>> mxCreator = musicxml.toMxObjects.contributorToMxCreator(contrib)
>>> mxCreator.get('charData')
'frank'
>>> mxCreator.get('type')
'composer'
music21.musicxml.toMxObjects.durationToMx(d)

Translate a music21 Duration object to a list of one or more MusicXML Note objects.

All rhythms and ties necessary in the MusicXML Notes are configured. The returned mxNote objects are incompletely specified, lacking full representation and information on pitch, etc.

>>> a = duration.Duration()
>>> a.quarterLength = 3
>>> b = musicxml.toMxObjects.durationToMx(a)
>>> len(b) == 1
True
>>> isinstance(b[0], musicxml.mxObjects.Note)
True
>>> a = duration.Duration()
>>> a.quarterLength = .33333333
>>> b = musicxml.toMxObjects.durationToMx(a)
>>> len(b) == 1
True
>>> isinstance(b[0], musicxml.mxObjects.Note)
True
>>> a = duration.Duration()
>>> a.quarterLength = .625
>>> b = musicxml.toMxObjects.durationToMx(a)
>>> len(b) == 2
True
>>> isinstance(b[0], musicxml.mxObjects.Note)
True
>>> a = duration.Duration()
>>> a.type = 'half'
>>> a.dotGroups = [1,1]
>>> b = musicxml.toMxObjects.durationToMx(a)
>>> len(b) == 2
True
>>> isinstance(b[0], musicxml.mxObjects.Note)
True
music21.musicxml.toMxObjects.durationToMxGrace(d, mxNoteList)

Given a music21 duration and a list of mxNotes, edit the mxNotes in place if the duration is a GraceDuration

music21.musicxml.toMxObjects.dynamicToMx(d)

Return an mx direction returns a musicxml.mxObjects.Direction object

>>> a = dynamics.Dynamic('ppp')
>>> print('%.2f' % a.volumeScalar)
0.15
>>> a._positionRelativeY = -10
>>> b = musicxml.toMxObjects.dynamicToMx(a)
>>> b[0][0][0].get('tag')
'ppp'
>>> b[1].get('tag')
'sound'
>>> b[1].get('dynamics')
'19'
music21.musicxml.toMxObjects.emptyObjectToMx()

Create a blank score with ‘This Page Intentionally Left blank’ as a gag...

>>> musicxml.toMxObjects.emptyObjectToMx()
<score-partwise <work work-title=This Page Intentionally Left Blank>...<measure number=0 <attributes divisions=10080> <note <rest > duration=40320 type=whole...>>>>    
music21.musicxml.toMxObjects.expressionToMx(orn)

Convert a music21 Expression (expression or ornament) to a musicxml object; return None if no conversion is possible.

music21.musicxml.toMxObjects.fermataToMxFermata(fermata)

Convert an expressions.Fermata object to a musicxml.mxObject.Fermata object. Note that the default musicxml is inverted fermatas – those are what most of us think of as ‘upright’ fermatas

>>> fermata = expressions.Fermata()
>>> fermata.type
'inverted'
>>> mxFermata = musicxml.toMxObjects.fermataToMxFermata(fermata)
>>> mxFermata.get('type')
'inverted'
music21.musicxml.toMxObjects.instrumentToMx(i)
>>> i = instrument.Celesta()
>>> mxScorePart = musicxml.toMxObjects.instrumentToMx(i)
>>> len(mxScorePart.scoreInstrumentList)
1
>>> mxScorePart.scoreInstrumentList[0].instrumentName
'Celesta'
>>> mxScorePart.midiInstrumentList[0].midiProgram
9
music21.musicxml.toMxObjects.intervalToMXTranspose(intervalObj)

Convert a music21 Interval into a musicxml transposition specification

>>> musicxml.toMxObjects.intervalToMXTranspose(interval.Interval('m6'))
<transpose diatonic=5 chromatic=8>
>>> musicxml.toMxObjects.intervalToMXTranspose(interval.Interval('-M6'))
<transpose diatonic=-5 chromatic=-9>
music21.musicxml.toMxObjects.keySignatureToMx(keySignature)

Returns a musicxml.mxObjects.KeySignature object from a music21 key.KeySignature or key.Key object

>>> ks = key.KeySignature(-3)
>>> ks
<music21.key.KeySignature of 3 flats>
>>> mxKey = musicxml.toMxObjects.keySignatureToMx(ks)
>>> mxKey.get('fifths')
-3
music21.musicxml.toMxObjects.lyricToMx(l)

Translate a music21 Lyric object to a MusicXML Lyric object.

music21.musicxml.toMxObjects.metadataToMxScore(mdObj)

Return a mxScore object from a Metadata object.

Note that an mxScore object is also where all the music is stored, so normally we call mxScore.merge(otherMxScore) where otherMxScore comes from converting the rest of the stream.

music21.musicxml.toMxObjects.noteToMxNotes(n, spannerBundle=None)

Translate a music21 Note into a list of Note objects.

Because of “complex” durations, the number of musicxml.mxObjects.Note objects could be more than one.

Note that, some note-attached spanners, such as octave shifts, produce direction (and direction types) in this method.

>>> n = note.Note('D#5')
>>> n.quarterLength = 2.25
>>> musicxmlNoteList = musicxml.toMxObjects.noteToMxNotes(n)
>>> len(musicxmlNoteList)
2
>>> musicxmlHalf = musicxmlNoteList[0]
>>> musicxmlHalf
<note <pitch step=D alter=1 octave=5> duration=20160 <tie type=start> type=half <accidental charData=sharp> <notations <tied type=start>>> 

TODO: Test with spannerBundle != None

music21.musicxml.toMxObjects.noteheadToMxNotehead(obj, defaultColor=None)

Translate a music21 Note object or Pitch object to a into a musicxml.mxObjects.Notehead object.

>>> n = note.Note('C#4')
>>> n.notehead = 'diamond'
>>> mxN = musicxml.toMxObjects.noteheadToMxNotehead(n)
>>> mxN.get('charData')
'diamond'
>>> n1 = note.Note('c3')
>>> n1.notehead = 'diamond'
>>> n1.noteheadParenthesis = 'yes'
>>> n1.noteheadFill = 'no'
>>> mxN4 = musicxml.toMxObjects.noteheadToMxNotehead(n1)
>>> mxN4._attr['filled']
'no'
>>> mxN4._attr['parentheses']
'yes'
music21.musicxml.toMxObjects.pageLayoutToMxPageLayout(pageLayout)

returns either an mxPageLayout object from a layout.PageLayout object, or None if there’s nothing to set.

>>> pl = layout.PageLayout(pageNumber = 5, leftMargin=234, rightMargin=124, pageHeight=4000, pageWidth=3000, isNew=True)
>>> mxPageLayout = musicxml.toMxObjects.pageLayoutToMxPageLayout(pl)
>>> mxPageLayout.get('page-height')
4000
music21.musicxml.toMxObjects.pageLayoutToMxPrint(pageLayout)

Return a Print (mxPrint) object for a PageLayout object.

the mxPrint object includes an PageLayout (mxPageLayout) object inside it.

>>> pl = layout.PageLayout(pageNumber = 5, leftMargin=234, rightMargin=124, pageHeight=4000, pageWidth=3000, isNew=True)
>>> mxPrint = musicxml.toMxObjects.pageLayoutToMxPrint(pl)
>>> mxPrint.get('new-page')
'yes'
>>> mxPrint.get('page-number')
5
music21.musicxml.toMxObjects.pitchToMx(p)

Returns a musicxml.mxObjects.Note() object

>>> a = pitch.Pitch('g#4')
>>> c = musicxml.toMxObjects.pitchToMx(a)
>>> c.get('pitch').get('step')
'G'
music21.musicxml.toMxObjects.repeatToMx(r)
>>> b = bar.Repeat(direction='end')
>>> mxBarline = musicxml.toMxObjects.repeatToMx(b)
>>> mxBarline.get('barStyle')
'light-heavy'
music21.musicxml.toMxObjects.restToMxNotes(r)

Translate a Rest to a MusicXML Note object configured with a Rest.

music21.musicxml.toMxObjects.scoreLayoutToMxDefaults(scoreLayout)

Return a Defaults (‘mxDefaults’) object for a ScoreLayout object.

the mxDefaults object might include an mxScaling object.

>>> sl = layout.ScoreLayout()
>>> sl.scalingMillimeters = 7.24342
>>> sl.scalingTenths = 40
>>> mxDefaults = musicxml.toMxObjects.scoreLayoutToMxDefaults(sl)
>>> mxDefaults
<defaults <scaling millimeters=7.24342 tenths=40>>
music21.musicxml.toMxObjects.segnoToMx(rm)

Returns a musicxml.mxObjects.Direction object with a musicxml.mxObjects.Segno mark in it

music21.musicxml.toMxObjects.spannersToMx(target, mxNoteList, mxDirectionPre, mxDirectionPost, spannerBundle)

Convenience routine to create and add MusicXML objects from music21 objects provided as a target and as a SpannerBundle.

The target parameter here may be music21 Note or Chord. This may edit the mxNoteList and direction lists in place, and thus returns None.

TODO: Improve docs and show a test...

music21.musicxml.toMxObjects.staffLayoutToMxPrint(staffLayout)
music21.musicxml.toMxObjects.staffLayoutToMxStaffLayout(staffLayout)
>>> sl = layout.StaffLayout(distance=34.0, staffNumber=2)
>>> mxStaffLayout = musicxml.toMxObjects.staffLayoutToMxStaffLayout(sl)    
>>> mxStaffLayout
<staff-layout number=2 staff-distance=34.0>
music21.musicxml.toMxObjects.systemLayoutToMxPrint(systemLayout)

Return a mxPrint object

>>> sl = layout.SystemLayout(leftmargin=234, rightmargin=124, distance=3, isNew=True)
>>> mxPrint = musicxml.toMxObjects.systemLayoutToMxPrint(sl)

Test conversion back using the parallel routine

>>> slAlt = layout.SystemLayout()
>>> musicxml.fromMxObjects.mxPrintToSystemLayout(mxPrint, slAlt)
>>> slAlt.leftMargin
234.0
>>> slAlt.rightMargin
124.0
>>> slAlt.distance
3.0
>>> slAlt.isNew
True
music21.musicxml.toMxObjects.systemLayoutToMxSystemLayout(systemLayout)

returns either an mxSystemLayout object from a layout.SystemLayout object, or None if there’s nothing to set.

>>> sl = layout.SystemLayout(leftmargin=234, rightmargin=124, distance=3, isNew=True)
>>> mxSystemLayout = musicxml.toMxObjects.systemLayoutToMxSystemLayout(sl)
>>> mxSystemLayout.get('system-distance')
3
music21.musicxml.toMxObjects.tempoIndicationToMx(ti)

Given a music21 MetronomeMark or MetricModulation, produce a musicxml Metronome tag wrapped in a <direction> tag.

>>> mm = tempo.MetronomeMark("slow", 40, note.Note(type='half'))
>>> mxList = musicxml.toMxObjects.tempoIndicationToMx(mm)
>>> mxList
[<direction <direction-type <metronome parentheses=no <beat-unit charData=half> <per-minute charData=40>>> <sound tempo=80.0>>, <direction <direction-type <words default-y=45.0 font-weight=bold justify=left charData=slow>>>]
>>> mm = tempo.MetronomeMark("slow", 40, duration.Duration(quarterLength=1.5))
>>> mxList = musicxml.toMxObjects.tempoIndicationToMx(mm)
>>> mxList
[<direction <direction-type <metronome parentheses=no <beat-unit charData=quarter> <beat-unit-dot > <per-minute charData=40>>> <sound tempo=60.0>>, <direction <direction-type <words default-y=45.0 font-weight=bold justify=left charData=slow>>>]
music21.musicxml.toMxObjects.textBoxToMxCredit(textBox)

Convert a music21 TextBox to a MusicXML Credit.

>>> tb = text.TextBox('testing')
>>> tb.positionVertical = 500
>>> tb.positionHorizontal = 300
>>> tb.page = 3
>>> mxCredit = musicxml.toMxObjects.textBoxToMxCredit(tb)
>>> print(mxCredit)
<credit page=3 <credit-words default-x=300 default-y=500 halign=center valign=top charData=testing>>
music21.musicxml.toMxObjects.textExpressionToMx(te)

Convert a TextExpression to a MusicXML mxDirection type. returns a musicxml.mxObjects.Direction object

music21.musicxml.toMxObjects.tieToMx(t)

Translate a music21 Tie object to MusicXML Tie (representing sound) and Tied (representing notation) objects as two component lists.

music21.musicxml.toMxObjects.timeSignatureToMx(ts)

Returns a single mxTime object from a meter.TimeSignature object.

Compound meters are represented as multiple pairs of beat and beat-type elements

>>> a = meter.TimeSignature('3/4')
>>> b = musicxml.toMxObjects.timeSignatureToMx(a)
>>> b
<time <beats charData=3> <beat-type charData=4>>
>>> a = meter.TimeSignature('3/4+2/4')
>>> b = musicxml.toMxObjects.timeSignatureToMx(a)
>>> b
<time <beats charData=3> <beat-type charData=4> <beats charData=2> <beat-type charData=4>>
>>> a.setDisplay('5/4')
>>> b = musicxml.toMxObjects.timeSignatureToMx(a)
>>> b
<time <beats charData=5> <beat-type charData=4>>
music21.musicxml.toMxObjects.tupletToMx(tuplet)

return a tuple of mxTimeModification and mxTuplet from a Tuplet object

>>> a = duration.Tuplet(6, 4, "16th")
>>> a.type = 'start'
>>> a.bracket = True
>>> b, c = musicxml.toMxObjects.tupletToMx(a)
>>> b
<time-modification actual-notes=6 normal-notes=4 normal-type=16th>
>>> c
[<tuplet bracket=yes placement=above type=start>]
music21.musicxml.toMxObjects.typeToMusicXMLType(value)

Convert a music21 type to a MusicXML type.

>>> musicxml.toMxObjects.typeToMusicXMLType('longa')
'long'
>>> musicxml.toMxObjects.typeToMusicXMLType('quarter')
'quarter'