Table Of Contents

music21.musicxml.translate

Low-level conversion routines between MusicXML and music21.

music21.musicxml.translate.mxToScore(mxScore, spannerBundle=None, inputM21=None)

Translate an mxScore into a music21 Score object or puts it into the given inputM21 object (which does not necessarily have to be a Score object. It can be any Stream object)

All spannerBundles accumulated at all lower levels are inserted here.

music21.musicxml.translate.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.

>>> from music21 import *
>>> n1 = note.Note()
>>> measure1 = stream.Measure()
>>> measure1.insert(n1)
>>> s1 = stream.Stream()
>>> s1.insert(measure1)
>>> mxScore = musicxml.translate.streamToMx(s1)
>>> mxPartList = mxScore.get('partList')
music21.musicxml.translate.accidentalToMx(a)

returns a musicxml Accidental object from a music21 pitch Accidental object

>>> from music21 import *
>>> a = pitch.Accidental()
>>> a.set('half-sharp')
>>> a.alter == .5
True
>>> mxAccidental = musicxml.translate.accidentalToMx(a)
>>> mxAccidental.get('content')
'quarter-sharp'
music21.musicxml.translate.articulationToMxArticulation(articulationMark)

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

>>> from music21 import *
>>> a = articulations.Accent()
>>> a.placement = 'below'
>>> mxArticulationMark = musicxml.translate.articulationToMxArticulation(a)
>>> mxArticulationMark
<accent placement=below>
music21.musicxml.translate.articulationsAndExpressionsToMx(target, mxNoteList)

The target parameter is the music21 object.

music21.musicxml.translate.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.

>>> from music21 import *
>>> b = bar.Barline('final')
>>> mxBarline = musicxml.translate.barlineToMx(b)
>>> mxBarline.get('barStyle')
'light-heavy'
music21.musicxml.translate.beamToMx(beamObject)
>>> from music21 import *
>>> a = beam.Beam()
>>> a.type = 'start'
>>> a.number = 1
>>> b = musicxml.translate.beamToMx(a)
>>> b.get('charData')
'begin'
>>> b.get('number')
1

>>> a.type = 'continue'
>>> b = musicxml.translate.beamToMx(a)
>>> b.get('charData')
'continue'
>>> a.type = 'stop'
>>> b = musicxml.translate.beamToMx(a)
>>> b.get('charData')
'end'

>>> a.type = 'partial'
>>> a.direction = 'left'
>>> b = musicxml.translate.beamToMx(a)
>>> b.get('charData')
'backward hook'
>>> a.direction = 'right'
>>> b = musicxml.translate.beamToMx(a)
>>> b.get('charData')
'forward hook'

>>> a.direction = None
>>> b = musicxml.translate.beamToMx(a)
Traceback (most recent call last):
TranslateException: partial beam defined without a proper direction set (set to None)
>>> a.type = 'crazy'
>>> b = musicxml.translate.beamToMx(a)
Traceback (most recent call last):
TranslateException: unexpected beam type encountered (crazy)
music21.musicxml.translate.beamsToMx(beamsObj)

Returns a list of mxBeam objects from a Beams object

>>> from music21 import *
>>> a = beam.Beams()
>>> a.fill(2, type='start')
>>> mxBeamList = musicxml.translate.beamsToMx(a)
>>> len(mxBeamList)
2
>>> mxBeamList[0]
<beam number=1 charData=begin>
>>> mxBeamList[1]
<beam number=2 charData=begin>
music21.musicxml.translate.chordSymbolToMx(cs)
>>> from music21 import *
>>> cs = harmony.ChordSymbol()
>>> cs.root('E-')
>>> cs.bass('B-')
>>> cs.inversion(2)
>>> cs.romanNumeral = 'I64'
>>> cs.chordKind = 'major'
>>> cs.chordKindStr = 'M'
>>> cs
<music21.harmony.ChordSymbol E-/B->
>>> mxHarmony = musicxml.translate.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.type = 'alter'
>>> hd.interval = -1
>>> hd.degree = 3
>>> cs.addChordStepModification(hd)
>>> mxHarmony = musicxml.translate.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>>>
music21.musicxml.translate.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

>>> from music21 import *
>>> 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.translate.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.translate.chordToMx(i)
>>> listOfMxNotes[0].get('chord')
False
>>> listOfMxNotes[1].noteheadObj.get('charData')
'diamond'
music21.musicxml.translate.clefToMxClef(clefObj)

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

>>> from music21 import *
>>> gc = clef.GClef()
>>> gc
<music21.clef.GClef>
>>> mxc = musicxml.translate.clefToMxClef(gc)
>>> mxc.get('sign')
'G'

>>> b = clef.Treble8vbClef()
>>> b.octaveChange
-1
>>> mxc2 = musicxml.translate.clefToMxClef(b)
>>> mxc2.get('sign')
'G'
>>> mxc2.get('clefOctaveChange')
-1
music21.musicxml.translate.codaToMx(rm)

Returns a musicxml.Direction object

>>> from music21 import *
music21.musicxml.translate.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.translate.configureStaffGroupFromMxPartGroup(staffGroup, mxPartGroup)

Given an already instantiated spanner.StaffGroup, configure it with parameters from an mxPartGroup.

music21.musicxml.translate.contributorToMxCreator(contribObj)

Return a mxCreator object from a Contributor object.

>>> from music21 import *
>>> md = metadata.Metadata()
>>> md.composer = 'frank'
>>> contrib = md._contributors[0]
>>> contrib
<music21.metadata.Contributor object at 0x...>
>>> mxCreator = musicxml.translate.contributorToMxCreator(contrib)
>>> mxCreator.get('charData')
'frank'
>>> mxCreator.get('type')
'composer'
music21.musicxml.translate.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.

>>> from music21 import *
>>> a = duration.Duration()
>>> a.quarterLength = 3
>>> b = musicxml.translate.durationToMx(a)
>>> len(b) == 1
True
>>> isinstance(b[0], musicxmlMod.Note)
True

>>> a = duration.Duration()
>>> a.quarterLength = .33333333
>>> b = musicxml.translate.durationToMx(a)
>>> len(b) == 1
True
>>> isinstance(b[0], musicxmlMod.Note)
True
>>> a = duration.Duration()
>>> a.quarterLength = .625
>>> b = musicxml.translate.durationToMx(a)
>>> len(b) == 2
True
>>> isinstance(b[0], musicxmlMod.Note)
True

>>> a = duration.Duration()
>>> a.type = 'half'
>>> a.dotGroups = [1,1]
>>> b = musicxml.translate.durationToMx(a)
>>> len(b) == 2
True
>>> isinstance(b[0], musicxmlMod.Note)
True
music21.musicxml.translate.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.translate.dynamicToMx(d)

Return an mx direction returns a musicxml.Direction object

>>> from music21 import *
>>> a = dynamics.Dynamic('ppp')
>>> a.volumeScalar
0.15
>>> a._positionRelativeY = -10
>>> b = musicxml.translate.dynamicToMx(a)
>>> b[0][0][0].get('tag')
'ppp'
>>> b[1].get('tag')
'sound'
>>> b[1].get('dynamics')
'19'
music21.musicxml.translate.emptyObjectToMx()

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

>>> from music21 import *
>>> musicxml.translate.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.translate.expressionToMx(orn)

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

music21.musicxml.translate.fermataToMxFermata(fermata)

Convert an expressions.Fermata object to a musicxml.Fermata object.

>>> from music21 import *
>>> fermata = expressions.Fermata()
>>> fermata.type
'upright'
>>> mxFermata = musicxml.translate.fermataToMxFermata(fermata)
>>> mxFermata.get('type')
'upright'
music21.musicxml.translate.instrumentToMx(i)
>>> from music21 import *
>>> i = instrument.Celesta()
>>> mxScorePart = musicxml.translate.instrumentToMx(i)
>>> len(mxScorePart.scoreInstrumentList)
1
>>> mxScorePart.scoreInstrumentList[0].instrumentName
'Celesta'
>>> mxScorePart.midiInstrumentList[0].midiProgram
9
music21.musicxml.translate.intervalToMXTranspose(int)

Convert a music21 Interval into a musicxml transposition specification

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

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

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

Translate a music21 Lyric object to a MusicXML Lyric object.

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

Translate a Measure to a MusicXML Measure object.

music21.musicxml.translate.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.translate.musicXMLTypeToType(value)

Convert a MusicXML type to an music21 type.

>>> from music21 import *
>>> musicxml.translate.musicXMLTypeToType('long')
'longa'
>>> musicxml.translate.musicXMLTypeToType('quarter')
'quarter'
>>> musicxml.translate.musicXMLTypeToType(None)
Traceback (most recent call last):
TranslateException...
music21.musicxml.translate.mxArticulationToArticulation(mxArticulationMark, inputM21=None)

Convert an mxArticulationMark to a music21.articulations.Articulation object or one of its subclasses.

Example: Provided an musicxml.ArticulationMark object (not an mxArticulations object) configure the music21 object.

Create both a musicxml.ArticulationMark object and a conflicting music21 object:

>>> from music21 import *
>>> mxArticulationMark = musicxml.ArticulationMark('accent')
>>> mxArticulationMark.set('placement', 'below')
>>> a = articulations.Tenuto()
>>> a.placement = 'above'

Now override the music21 object with the mxArticulationMark object’s characteristics

>>> musicxml.translate.mxArticulationToArticulation(mxArticulationMark, inputM21 = a)
>>> 'Tenuto' in a.classes
False
>>> 'Accent' in a.classes
True
>>> a.placement
'below'
music21.musicxml.translate.mxClefToClef(mxClefList, inputM21=None)

Given a MusicXML Clef object, return a music21 Clef object

>>> from music21 import *
>>> a = musicxml.Clef()
>>> a.set('sign', 'G')
>>> a.set('line', 2)
>>> b = clef.Clef()
>>> b
<music21.clef.Clef>
>>> 'TrebleClef' in b.classes
False
>>> musicxml.translate.mxClefToClef(a, b)
>>> b.sign
'G'
>>> 'TrebleClef' in b.classes
True
>>> b
<music21.clef.TrebleClef>

Create a new clef from thin air:

>>> a = musicxml.Clef()
>>> a.set('sign', 'TAB')
>>> c = musicxml.translate.mxClefToClef(a)
>>> c
<music21.clef.TabClef>
music21.musicxml.translate.mxCreatorToContributor(mxCreator, inputM21=None)

Given an mxCreator, fill the necessary parameters of a Contributor.

>>> from music21 import *
>>> mxCreator = musicxml.Creator()
>>> mxCreator.set('type', 'composer')
>>> mxCreator.set('charData', 'Beethoven, Ludwig van')

>>> c = mxCreatorToContributor(mxCreator)
>>> c
<music21.metadata.Contributor object at 0x...>
>>> c.role
'composer'
>>> c.name
'Beethoven, Ludwig van'
music21.musicxml.translate.mxCreditToTextBox(mxCredit)

Convert a MusicXML credit to a music21 TextBox

>>> from music21 import *
>>> c = musicxml.Credit()
>>> c.append(musicxml.CreditWords('testing'))
>>> c.set('page', 2)
>>> tb = musicxml.translate.mxCreditToTextBox(c)
>>> tb.page
2
>>> tb.content
'testing'
music21.musicxml.translate.mxDirectionToSpanners(targetLast, mxDirection, spannerBundle)

Some spanners, such as MusicXML octave-shift, are encoded as MusicXML directions.

music21.musicxml.translate.mxFermataToFermata(mxFermata, inputM21=None)

Convert an mxFermata object to a music21 expressions.Fermata object.

If inputM21 is None, creates a new Fermata object and returns it. Otherwise changes the current Fermata object and returns nothing.

>>> from music21 import *
>>> mxFermata = musicxml.Fermata()
>>> mxFermata.set('type', 'inverted')
>>> fermata = musicxml.translate.mxFermataToFermata(mxFermata)
>>> fermata.type
'inverted'
music21.musicxml.translate.mxGraceToGrace(noteOrChord, mxGrace=None)

Given a completely formed, non-grace Note or Chord, create and return a m21 grace version of the same.

If mxGrace is None, no change is made and the same object is returned.

music21.musicxml.translate.mxKeyListToKeySignature(mxKeyList, inputM21=None)

Given a mxKey object or keyList, return a music21.key.KeySignature object and return it, or if inputM21 is None, change its attributes and return nothing.

>>> from music21 import *
>>> mxk = musicxml.Key()
>>> mxk.set('fifths', 5)
>>> ks = key.KeySignature()
>>> musicxml.translate.mxKeyListToKeySignature(mxk, ks)
>>> ks.sharps
5

Or just get a new KeySignature object from scratch:

>>> mxk.set('fifths', -2)
>>> ks2 = musicxml.translate.mxKeyListToKeySignature(mxk)
>>> ks2
<music21.key.KeySignature of 2 flats>
music21.musicxml.translate.mxNotationsToSpanners(target, mxNotations, spannerBundle)

General routines for gathering spanners from notes via mxNotations objects and placing them in a spanner bundle.

Spanners may be found in musicXML notations and directions objects.

The passed-in spannerBundle will be edited in-place; existing spanners may be completed, or new spanners may be added.

The target object is a reference to the relevant music21 object this spanner is associated with.

music21.musicxml.translate.mxOrnamentToExpressionOrArticulation(mxOrnament)

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.

music21.musicxml.translate.mxPrintToPageLayout(mxPrint, inputM21=None)

Given an mxPrint object, set object data for the print section of a layout.PageLayout object

>>> from music21 import *
>>> mxPrint = musicxml.Print()
>>> mxPrint.set('new-page', 'yes')
>>> mxPrint.set('page-number', 5)
>>> mxPageLayout = musicxml.PageLayout()
>>> mxPageLayout.pageHeight = 4000
>>> mxPageMargins = musicxml.PageMargins()
>>> mxPageMargins.set('leftMargin', 20)
>>> mxPageMargins.set('rightMargin', 30.2)
>>> mxPageLayout.append(mxPageMargins)
>>> mxPrint.append(mxPageLayout)

>>> pl = musicxml.translate.mxPrintToPageLayout(mxPrint)
>>> pl.isNew
True
>>> pl.rightMargin > 30.1 and pl.rightMargin < 30.3
True
>>> pl.leftMargin
20.0
>>> pl.pageNumber
5

Alternatively, pass a music21 object into this routine.

>>> plAlt = layout.PageLayout()
>>> musicxml.translate.mxPrintToPageLayout(mxPrint, plAlt)
>>> plAlt.pageNumber
5
>>> plAlt.pageHeight
4000.0
>>> plAlt.isNew
True
music21.musicxml.translate.mxPrintToSystemLayout(mxPrint, inputM21=None)

Given an mxPrint object, set object data

>>> from music21 import *
>>> mxPrint = musicxml.Print()
>>> mxPrint.set('new-system', 'yes')
>>> mxSystemLayout = musicxml.SystemLayout()
>>> mxSystemLayout.systemDistance = 55
>>> mxSystemMargins = musicxml.SystemMargins()
>>> mxSystemMargins.set('leftMargin', 20)
>>> mxSystemMargins.set('rightMargin', 30.2)
>>> mxSystemLayout.append(mxSystemMargins)
>>> mxPrint.append(mxSystemLayout)

>>> sl = musicxml.translate.mxPrintToSystemLayout(mxPrint)
>>> sl.isNew
True
>>> sl.rightMargin > 30.1 and sl.rightMargin <= 30.2
True
>>> sl.leftMargin
20.0
>>> sl.distance
55.0
music21.musicxml.translate.mxScoreToMetadata(mxScore, inputM21=None)

Use an mxScore, to fill in parameters of a Metadata object.

if inputM21 is None, a new Metadata object is created and returned at the end.

Otherwise, the parameters of this Metadata object are changed and nothing is returned.

music21.musicxml.translate.mxToAccidental(mxAccidental, inputM21Object=None)
>>> from music21 import *
>>> a = musicxml.Accidental()
>>> a.set('content', 'half-flat')
>>> a.get('content')
'half-flat'

>>> b = pitch.Accidental()
>>> bReference = musicxml.translate.mxToAccidental(a, b)
>>> b is bReference
True
>>> b.name
'half-flat'
music21.musicxml.translate.mxToBarline(mxBarline, inputM21=None)

Given an mxBarline, fill the necessary parameters

>>> from music21 import *
>>> mxBarline = musicxml.Barline()
>>> mxBarline.set('barStyle', 'light-light')
>>> mxBarline.set('location', 'right')
>>> b = musicxml.translate.mxToBarline(mxBarline)
>>> b.style  # different in music21 than musicxml
'double'
>>> b.location
'right'
music21.musicxml.translate.mxToBeam(mxBeam, inputM21=None)

given an mxBeam object return a Beam object

>>> from music21 import *
>>> mxBeam = musicxml.Beam()
>>> mxBeam.set('charData', 'begin')
>>> a = musicxml.translate.mxToBeam(mxBeam)
>>> a.type
'start'

>>> mxBeam.set('charData', 'continue')
>>> a = musicxml.translate.mxToBeam(mxBeam)
>>> a.type
'continue'
>>> mxBeam.set('charData', 'end')
>>> a = musicxml.translate.mxToBeam(mxBeam)
>>> a.type
'stop'

>>> mxBeam.set('charData', 'forward hook')
>>> a = musicxml.translate.mxToBeam(mxBeam)
>>> a.type
'partial'
>>> a.direction
'right'
>>> mxBeam.set('charData', 'backward hook')
>>> a = musicxml.translate.mxToBeam(mxBeam)
>>> a.type
'partial'
>>> a.direction
'left'

>>> mxBeam.set('charData', 'crazy')
>>> a = musicxml.translate.mxToBeam(mxBeam)
Traceback (most recent call last):
TranslateException: unexpected beam type encountered (crazy)
music21.musicxml.translate.mxToBeams(mxBeamList, inputM21=None)

given a list of mxBeam objects, sets the beamsList

>>> from music21 import *
>>> a = beam.Beams()
>>> a.fill(2, type='start')
>>> mxBeamList = musicxml.translate.beamsToMx(a)
>>> b = mxToBeams(mxBeamList)
>>> b
<music21.beam.Beams <music21.beam.Beam None/start>/<music21.beam.Beam None/start>>
music21.musicxml.translate.mxToChord(mxNoteList, inputM21=None, spannerBundle=None)

Given an a list of mxNotes, fill the necessary parameters

>>> from music21 import *
>>> a = musicxml.Note()
>>> p = musicxml.Pitch()
>>> p.set('step', 'A')
>>> p.set('octave', 3)
>>> a.setDefaults()
>>> a.set('pitch', p)
>>> b = musicxml.Note()
>>> b.setDefaults()
>>> b.set('chord', True)
>>> m = musicxml.Measure()
>>> m.setDefaults()
>>> a.external['measure'] = m # assign measure for divisions ref
>>> a.external['divisions'] = m.external['divisions']
>>> b.external['measure'] = m # assign measure for divisions ref
>>> b.external['divisions'] = m.external['divisions']
>>> c = musicxml.translate.mxToChord([a, b])
>>> len(c.pitches)
2
>>> c.pitches[0]
<music21.pitch.Pitch A3>

>>> from music21 import *
>>> a = musicxml.Note()
>>> a.setDefaults()
>>> nh1 = musicxml.Notehead()
>>> nh1.set('charData', 'diamond')
>>> a.noteheadObj = nh1
>>> b = musicxml.Note()
>>> b.setDefaults()
>>> b.set('chord', True)
>>> m = musicxml.Measure()
>>> m.setDefaults()
>>> a.external['measure'] = m # assign measure for divisions ref
>>> a.external['divisions'] = m.external['divisions']
>>> b.external['measure'] = m # assign measure for divisions ref
>>> b.external['divisions'] = m.external['divisions']
>>> c = musicxml.translate.mxToChord([a, b])
>>> c.getNotehead(c.pitches[0])
'diamond'
music21.musicxml.translate.mxToChordSymbol(mxHarmony)
music21.musicxml.translate.mxToCoda(mxCoda)

Translate a MusicXML Coda object to a music21 Coda object.

music21.musicxml.translate.mxToDuration(mxNote, inputM21=None)

Translate a MusicXML Note object to a music21 Duration object.

>>> from music21 import *
>>> a = musicxml.Note()
>>> a.setDefaults()
>>> m = musicxml.Measure()
>>> m.setDefaults()
>>> a.external['measure'] = m # assign measure for divisions ref
>>> a.external['divisions'] = m.external['divisions']

>>> c = duration.Duration()
>>> musicxml.translate.mxToDuration(a, c)
<music21.duration.Duration 1.0>
>>> c.quarterLength
1.0
music21.musicxml.translate.mxToDynamicList(mxDirection)

Given an mxDirection, load instance

>>> from music21 import *
>>> mxDirection = musicxml.Direction()
>>> mxDirectionType = musicxml.DirectionType()
>>> mxDynamicMark = musicxml.DynamicMark('ff')
>>> mxDynamics = musicxml.Dynamics()
>>> mxDynamics.set('default-y', -20)
>>> mxDynamics.append(mxDynamicMark)
>>> mxDirectionType.append(mxDynamics)
>>> mxDirection.append(mxDirectionType)

>>> a = dynamics.Dynamic()
>>> a = musicxml.translate.mxToDynamicList(mxDirection)[0]
>>> a.value
'ff'
>>> a.englishName
'very loud'
>>> a._positionDefaultY
-20
music21.musicxml.translate.mxToInstrument(mxScorePart, inputM21=None)
music21.musicxml.translate.mxToLyric(mxLyric, inputM21=None)

Translate a MusicXML Lyric object to a music21 Lyric object.

>>> from music21 import *
>>> mxLyric = musicxml.Lyric()
>>> mxLyric.set('text', 'word')
>>> mxLyric.set('number', 4)
>>> mxLyric.set('syllabic', 'single')
>>> lyricObj = note.Lyric()
>>> mxToLyric(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 = mxToLyric(mxLyric)
>>> l2
<music21.note.Lyric number=0 identifier="part2verse1" syllabic=single text="word">
music21.musicxml.translate.mxToMeasure(mxMeasure, spannerBundle=None, inputM21=None)

Translate an mxMeasure (a MusicXML Measure object) into a music21 Measure.

If an inputM21 object reference is provided, this object will be configured and returned; otherwise, a new Measure object is created.

The spannerBundle that is passed in is used to accumulate any created Spanners. This Spanners are not inserted into the Stream here.

music21.musicxml.translate.mxToNote(mxNote, spannerBundle=None, inputM21=None)

Translate a MusicXML Note 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.

Returns a note.Note object.

>>> from music21 import *
>>> mxNote = musicxml.Note()
>>> mxNote.setDefaults()
>>> mxMeasure = musicxml.Measure()
>>> mxMeasure.setDefaults()
>>> mxMeasure.append(mxNote)
>>> mxNote.external['measure'] = mxMeasure # manually create ref
>>> mxNote.external['divisions'] = mxMeasure.external['divisions']
>>> n = musicxml.translate.mxToNote(mxNote)
>>> n
<music21.note.Note C>
music21.musicxml.translate.mxToOffset(mxDirection, mxDivisions)

Translate a MusicXML Direction with an offset value to an offset in music21.

music21.musicxml.translate.mxToPitch(mxNote, inputM21=None)

Given a MusicXML Note object, set this Pitch object to its values.

>>> from music21 import *
>>> b = musicxml.Pitch()
>>> b.set('octave', 3)
>>> b.set('step', 'E')
>>> b.set('alter', -1)
>>> c = musicxml.Note()
>>> c.set('pitch', b)
>>> a = pitch.Pitch('g#4')
>>> a = musicxml.translate.mxToPitch(c)
>>> print(a)
E-3
music21.musicxml.translate.mxToRepeat(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

>>> from music21 import *
>>> mxRepeat = musicxml.Repeat()
>>> mxRepeat.set('direction', 'backward')
>>> mxRepeat.get('times') == None
True
>>> mxBarline = musicxml.Barline()
>>> mxBarline.set('barStyle', 'light-heavy')
>>> mxBarline.set('repeatObj', mxRepeat)
>>> b = musicxml.translate.mxToRepeat(mxBarline)
>>> b
<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.

>>> b.style
'final'
>>> b.direction
'end'
>>> mxBarline2 = musicxml.translate.repeatToMx(b)
>>> mxBarline2.get('barStyle')
'light-heavy'
music21.musicxml.translate.mxToRepeatExpression(mxDirection)

Given an mxDirection that may define a coda, segno, or other repeat expression statement, realize the appropriate music21 object.

music21.musicxml.translate.mxToRest(mxNote, inputM21=None)

Translate a MusicXML Note object to a Rest.

If an inputM21 object reference is provided, this object will be configured; otherwise, a new Rest object is created and returned.

music21.musicxml.translate.mxToSegno(mxCoda)

Translate a MusicXML Coda object to a music21 Coda object.

music21.musicxml.translate.mxToStreamPart(mxScore, partId, spannerBundle=None, inputM21=None)

Load a part into a new Stream or one provided by inputM21 given an mxScore and a part name.

The spannerBundle reference, when passed in, is used to accumulate Spanners. These are not inserted here.

Though it is incorrect MusicXML, PDFtoMusic creates empty measures when it should create full measures of rests (possibly hidden). This routine fixes that bug. See http://musescore.org/en/node/15129

music21.musicxml.translate.mxToTempoIndication(mxMetronome, mxWords=None)

Given an mxMetronome, convert to either a TempoIndication subclass, either a tempo.MetronomeMark or tempo.MetricModulation.

>>> from music21 import *
>>> m = musicxml.Metronome()
>>> bu = musicxml.BeatUnit('half')
>>> pm = musicxml.PerMinute(125)
>>> m.append(bu)
>>> m.append(pm)
>>> musicxml.translate.mxToTempoIndication(m)
<music21.tempo.MetronomeMark Half=125.0>
music21.musicxml.translate.mxToTextExpression(mxDirection)

Given an mxDirection, create one or more TextExpressions

music21.musicxml.translate.mxToTie(mxNote, inputM21=None)

Translate a MusicXML Note (!) to a music21 Tie object according to its <tieList> parameter.

music21.musicxml.translate.mxToTimeSignature(mxTimeList, inputM21=None)

Given an mxTimeList, load this object

if inputM21 is None, create a new TimeSignature and return it.

>>> from music21 import *
>>> mxTime = musicxml.Time()
>>> mxTime.setDefaults()
>>> mxAttributes = musicxml.Attributes()
>>> mxAttributes.timeList.append(mxTime)
>>> ts = meter.TimeSignature()
>>> mxToTimeSignature(mxAttributes.timeList, ts)
>>> ts.numerator
4
music21.musicxml.translate.mxToTuplet(mxNote, inputM21Object=None)

Given an mxNote, based on mxTimeModification and mxTuplet objects, return a Tuplet object (or alter the input object and then return it)

music21.musicxml.translate.mxTransposeToInterval(mxTranspose)

Convert a MusicXML Transpose object to a music21 Interval object.

>>> from music21 import *
>>> t = musicxml.Transpose()
>>> t.diatonic = -1
>>> t.chromatic = -2
>>> musicxml.translate.mxTransposeToInterval(t)
<music21.interval.Interval M-2>

>>> t = musicxml.Transpose()
>>> t.diatonic = -5
>>> t.chromatic = -9
>>> musicxml.translate.mxTransposeToInterval(t)
<music21.interval.Interval M-6>
>>> t = musicxml.Transpose()
>>> t.diatonic = 3 # a type of 4th
>>> t.chromatic = 6
>>> musicxml.translate.mxTransposeToInterval(t)
<music21.interval.Interval A4>
music21.musicxml.translate.noteToMxNotes(n, spannerBundle=None)

Translate a music21 Note into a list of Note objects.

Because of “complex” durations, the number of musicxml.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.

>>> from music21 import *
>>> n = note.Note('D#5')
>>> n.quarterLength = 2.25
>>> musicxmlNoteList = musicxml.translate.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>>>
music21.musicxml.translate.noteheadToMxNotehead(obj, defaultColor=None)

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

>>> from music21 import *
>>> n = note.Note('C#4')
>>> n.notehead = 'diamond'
>>> mxN = musicxml.translate.noteheadToMxNotehead(n)
>>> mxN.get('charData')
'diamond'

>>> n1 = note.Note('c3')
>>> n1.notehead = 'diamond'
>>> n1.noteheadParenthesis = 'yes'
>>> n1.noteheadFill = 'no'
>>> mxN4 = musicxml.translate.noteheadToMxNotehead(n1)
>>> mxN4._attr['filled']
'no'
>>> mxN4._attr['parentheses']
'yes'
music21.musicxml.translate.pageLayoutToMxPrint(pageLayout)

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

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

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

Returns a musicxml.Note() object

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

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

music21.musicxml.translate.segnoToMx(rm)

Returns a musicxml.Direction object

>>> from music21 import *
music21.musicxml.translate.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.translate.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.translate.systemLayoutToMxPrint(systemLayout)

Return a mxPrint object

>>> from music21 import *
>>> sl = layout.SystemLayout(leftmargin=234, rightmargin=124, distance=3, isNew=True)
>>> mxPrint = musicxml.translate.systemLayoutToMxPrint(sl)

Test conversion back using the parallel routine

>>> slAlt = layout.SystemLayout()
>>> musicxml.translate.mxPrintToSystemLayout(mxPrint, slAlt)
>>> slAlt.leftMargin
234.0
>>> slAlt.rightMargin
124.0
>>> slAlt.distance
3.0
>>> slAlt.isNew
True
music21.musicxml.translate.tempoIndicationToMx(ti)

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

>>> from music21 import *
>>> mm = tempo.MetronomeMark("slow", 40, note.HalfNote())
>>> mxList = musicxml.translate.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.translate.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.translate.textBoxToMxCredit(textBox)

Convert a music21 TextBox to a MusicXML Credit.

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

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

music21.musicxml.translate.tieToMx(t)

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

music21.musicxml.translate.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

>>> from music21 import *
>>> a = meter.TimeSignature('3/4')
>>> b = musicxml.translate.timeSignatureToMx(a)
>>> b
<time <beats charData=3> <beat-type charData=4>>

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

return a tuple of mxTimeModification and mxTuplet from a Tuplet object

>>> from music21 import *
>>> a = duration.Tuplet(6, 4, "16th")
>>> a.type = 'start'
>>> a.bracket = True
>>> b, c = musicxml.translate.tupletToMx(a)
music21.musicxml.translate.typeToMusicXMLType(value)

Convert a music21 type to a MusicXML type.

>>> from music21 import *
>>> musicxml.translate.typeToMusicXMLType('longa')
'long'
>>> musicxml.translate.typeToMusicXMLType('quarter')
'quarter'