Previous topic

music21.musedata.base40

Next topic

music21.note

music21.musicxml.translate

Low-level conversion routines between MusicXML and music21.

music21.musicxml.translate.chordToMx(c)

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 = a.mx
>>> len(mxNoteList) # get three mxNotes
3
>>> mxNoteList[0].get('chord')
False
>>> mxNoteList[1].get('chord')
True
>>> mxNoteList[2].get('chord')
True
music21.musicxml.translate.codaToMx(rm)

Returns a musicxml.Direction object

>>> from music21 import *
music21.musicxml.translate.durationToMusicXML(d)
Translate a music21 Duration into a complete MusicXML representation.
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.dynamicToMx(d)

Return an mx direction returns a musicxml.Direction object

>>> from music21 import *
>>> a = dynamics.Dynamic('ppp')
>>> a._positionRelativeY = -10
>>> b = a.mx
>>> b[0][0][0].get('tag')
'ppp'
music21.musicxml.translate.generalNoteToMusicXML(n)

Translate a music21 Note into a complete MusicXML representation.

>>> from music21 import *
>>> n = note.Note('c3')
>>> n.quarterLength = 3
>>> post = musicxml.translate.generalNoteToMusicXML(n)
>>> post[-100:].replace('\n', '')
'/type>        <dot/>        <notations/>      </note>    </measure>  </part></score-partwise>'
music21.musicxml.translate.instrumentToMx(i)
>>> from music21 import *
>>> i = instrument.Celesta()
>>> mxScorePart = i.mx
>>> len(mxScorePart.scoreInstrumentList)
1
>>> mxScorePart.scoreInstrumentList[0].instrumentName
'Celesta'
>>> mxScorePart.midiInstrumentList[0].midiProgram
9
music21.musicxml.translate.lyricToMx(l)
Translate a music21 Lyric object to a MusicXML Lyric object.
music21.musicxml.translate.measureToMusicXML(m)

Translate a music21 Measure into a complete MusicXML string representation. Note: this method is called for complete MusicXML representation of a Measure, not for partial solutions in Part or Stream production.

>>> from music21 import *
>>> m = stream.Measure()
>>> m.repeatAppend(note.Note('g3'), 4)
>>> post = musicxml.translate.measureToMusicXML(m)
>>> post[-100:].replace('\n', '')
' <type>quarter</type>        <notations/>      </note>    </measure>  </part></score-partwise>'
music21.musicxml.translate.measureToMx(m, spannerBundle=None)
Translate a Measure to a MusicXML Measure object.
music21.musicxml.translate.mxToChord(mxNoteList, inputM21=None)

Given an a list of mxNotes, fill the necessary parameters

>>> from music21 import *
>>> a = musicxml.Note()
>>> a.setDefaults()
>>> 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 = chord.Chord()
>>> c.mx = [a, b]
>>> len(c.pitches)
2
music21.musicxml.translate.mxToCoda(mxCoda)
Translate a MusicXML Coda object to a music21 Coda object.
music21.musicxml.translate.mxToDuration(mxNote, inputM21)

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.mx = mxDirection
>>> 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.
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 spanners parameter can be a list or a Stream for storing and processing Spanner objects.
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.mx = c
>>> print(a)
E-3
music21.musicxml.translate.mxToRepeat(mxBarline, inputM21=None)

Given an mxBarline, file the necessary parameters

>>> from music21 import *
>>> mxRepeat = musicxml.Repeat()
>>> mxRepeat.set('direction', 'forward')
>>> mxRepeat.get('times') == None
True
>>> mxBarline = musicxml.Barline()
>>> mxBarline.set('barStyle', 'light-heavy')
>>> mxBarline.set('repeatObj', mxRepeat)
>>> b = bar.Repeat()
>>> b.mx = mxBarline
>>> b.style
'final'
>>> b.direction
'start'
>>> b.mx.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.mxToStream(mxScore, spannerBundle=None, inputM21=None)
Translate an mxScore into a music21 Score object. All spannerBundles accumulated at all lower levels are inserted here.
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.
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 Quarter=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.
music21.musicxml.translate.mxToTimeSignature(mxTimeList, inputM21=None)

Given an mxTimeList, load this object

>>> from music21 import *
>>> a = musicxml.Time()
>>> a.setDefaults()
>>> b = musicxml.Attributes()
>>> b.timeList.append(a)
>>> c = meter.TimeSignature()
>>> c.mx = b.timeList
>>> c.numerator
4
music21.musicxml.translate.noteToMxNotes(n, spannerBundle=None)
Translate a music21 Note into a list of Note objects.
music21.musicxml.translate.noteheadToMxNotehead(n, spannerBundle=None)
Translate a music21 Note object into a musicxml.Notehead object.
music21.musicxml.translate.pitchToMusicXML(p)
music21.musicxml.translate.pitchToMx(p)

Returns a musicxml.Note() object

>>> from music21 import *
>>> a = pitch.Pitch('g#4')
>>> c = a.mx
>>> c.get('pitch').get('step')
'G'
music21.musicxml.translate.repeatToMx(r)
>>> from music21 import *
>>> b = bar.Repeat('light-heavy')
>>> mxBarline = b.mx
>>> 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.streamPartToMx(part, instObj=None, meterStream=None, refStreamOrTimeRange=None, spannerBundle=None)

If there are Measures within this stream, use them to create and return an MX Part and ScorePart.

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.streamToMx(s, spannerBundle=None)

Create and return a musicxml Score object.

This is the most common entry point for conversion of a Stream to MusicXML. This method is called on Stream from the musicxml property.

>>> 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.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.textExpressionToMx(te)

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

>>> from music21 import *
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.timeSignatureToMusicXML(ts)
music21.musicxml.translate.timeSignatureToMx(ts)

Returns a single mxTime object. Compound meters are represented as multiple pairs of beat and beat-type elements

>>> from music21 import *
>>> a = meter.TimeSignature('3/4')
>>> b = a.mx
>>> a = meter.TimeSignature('3/4+2/4')
>>> b = a.mx