Previous topic

music21.musicxml.fromMxObjects

Next topic

music21.musicxml.mxObjects

Table Of Contents

Table Of Contents

This Page

music21.musicxml.m21ToString

Medium-level conversion routines to take music21 Streams and other objects and get a full MusicXML String from them. In general do not use this module. Instead call:

>>> s = converter.parse('tinyNotation: 3/4 C4 D E r2.').makeMeasures()
>>> s.show('musicxml')

But you might need this routine if you want to check that the musicxml string returned has certain features:

>>> musicxmlStr = musicxml.m21ToString.fromMusic21Object(s)
>>> '<rest' in musicxmlStr
True

This module is an intermediate level of complexity for working with musicxml between the standard music21 objects and musicxml.toMxObjects

Functions

music21.musicxml.m21ToString.fromDiatonicScale(diatonicScaleObject)

Return a complete musicxml of the DiatonicScale

Overrides the general scale behavior to highlight the tonic and dominant.

music21.musicxml.m21ToString.fromDuration(d)

Translate a music21 Duration into a complete MusicXML representation.

Rarely rarely used. Only if you call .show() on a duration object

>>> d = duration.Duration(4.0)
>>> dxml = musicxml.m21ToString.fromDuration(d)
>>> print(dxml)
<?xml version="1.0" ...?>
<!DOCTYPE score-partwise
  PUBLIC '-//Recordare//DTD MusicXML 2.0 Partwise//EN'
  'http://www.musicxml.org/dtds/partwise.dtd'>
<score-partwise>
  <movement-title>Music21 Fragment</movement-title>
  <identification>
    <creator type="composer">Music21</creator>
  </identification>
  <defaults>
    <scaling>
      <millimeters>7</millimeters>
      <tenths>40</tenths>
    </scaling>
  </defaults>
  <part-list>
  ...
  </part-list>
  <part id="...">
    <measure number="1">
      <attributes>
        <divisions>10080</divisions>
        <time>
          <beats>4</beats>
          <beat-type>4</beat-type>
        </time>
        <clef>
          <sign>G</sign>
          <line>2</line>
        </clef>
      </attributes>
      <note>
        <pitch>
          <step>C</step>
          <octave>4</octave>
        </pitch>
        <duration>40320</duration>
        <type>whole</type>
        <notations/>
      </note>
      <barline location="right">
        <bar-style>light-heavy</bar-style>
      </barline>
    </measure>
  </part>
</score-partwise>

Or, more simply

>>> d.show('musicxml')
music21.musicxml.m21ToString.fromDynamic(dynamicObject)

Provide a complete MusicXML string from a single dynamic by putting it into a Stream first.

music21.musicxml.m21ToString.fromGeneralNote(n)

Translate a music21 Note into a complete MusicXML representation.

>>> n = note.Note('c3')
>>> n.quarterLength = 3
>>> post = musicxml.m21ToString.fromGeneralNote(n)
>>> #print post
music21.musicxml.m21ToString.fromMeasure(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.

>>> m = stream.Measure()
>>> m.repeatAppend(note.Note('g3'), 4)
>>> post = musicxml.m21ToString.fromMeasure(m)
>>> len(post) > 1000
True
music21.musicxml.m21ToString.fromMusic21Object(m21Object)

Translate an arbitrary music21 object to a musicxml string and return it

This function is called by music21.base.write() and is the most important function here.

music21.musicxml.m21ToString.fromPitch(p)
music21.musicxml.m21ToString.fromScale(scaleObject)

Generate the pitches from this scale and put it into a stream.Measure, then call fromMeasure on it

music21.musicxml.m21ToString.fromStream(streamObject)

return a complete musicxml string from a music21 Stream object

music21.musicxml.m21ToString.fromTimeSignature(ts)

return a single TimeSignature as a musicxml document