Music21 can import and export a variety of musical data formats. Many of these formats are distributed with music21 as part of the corpus module (see music21.corpus.base).
The converter module, as well as the module function music21.converter.parse() handle importing all supported formats. For complete documentation on file and data formats, see music21.converter.
We can parse a MusicXML file by providing the music21.converter.parse() function with a local file path or an URL to a file path. The function will determine the file format. An appropriate Stream or Stream subclass will be returned. For example, given a MusicXML file stored at the file path “/Volumes/xdisc/_scratch/bwv1007-01.xml”, a Stream can be created from the file with the following:
>>> from music21 import *
>>> sBach = converter.parse('/Volumes/xdisc/_scratch/bwv1007-01.xml')
>>> sBach.show()
Alternative, we can provide a URL to the music21.converter.parse() function that points to the desired file. Assuming proper system configuration (see Setting up and Configuring Environment), the file will be downloaded and parsed.
>>> url = 'http://kern.ccarh.org/cgi-bin/ksdata?l=cc/bach/cello&file=bwv1007-01.krn&f=xml'
>>> sAlt = converter.parse(url)
Note that presently music21 offers limited support for compressed .mxl MusicXML files; this feature will be expanded in the future.
Numerous MusicXML files can be found at the following URLs.
Parsing Humdrum functions exactly as parsing other data formats. Simply call the music21.converter.parse() function on the desired file path or URL.
>>> sLassus = converter.parse('/Volumes/xdisc/_scratch/matona.krn')
Over one hundred thousand Kern files can be found at the following URL.
Presently, MIDI input and output is not yet supported. We hope to have this feature available soon.