music21.common.formats

Utilities for working with file formats.

almost everything here is deprecated.

Functions

music21.common.formats.findFormat(fmt)

Given a format defined either by a format name, abbreviation, or an extension, return the regularized format name as well as the output extensions.

DEPRECATED May 2014 – moving to converter

All but the first element of the tuple are deprecated for use, since the extension can vary by subconverter (e.g., lily.png)

Note that .mxl and .mx are only considered MusicXML input formats.

>>> common.findFormat('mx')
('musicxml', '.xml')
>>> common.findFormat('.mxl')
('musicxml', '.xml')
>>> common.findFormat('musicxml')
('musicxml', '.xml')
>>> common.findFormat('lily')
('lilypond', '.ly')
>>> common.findFormat('lily.png')
('lilypond', '.ly')
>>> common.findFormat('humdrum')
('humdrum', '.krn')
>>> common.findFormat('txt')
('text', '.txt')
>>> common.findFormat('textline')
('textline', '.txt')
>>> common.findFormat('midi')
('midi', '.mid')
>>> common.findFormat('abc')
('abc', '.abc')
>>> common.findFormat('scl')
('scala', '.scl')
>>> common.findFormat('braille')
('braille', '.txt')
>>> common.findFormat('vexflow')
('vexflow', '.html')
>>> common.findFormat('capx')
('capella', '.capx')
>>> common.findFormat('mx')
('musicxml', '.xml')

Works the same whether you have a leading dot or not:

>>> common.findFormat('md')
('musedata', '.md')
>>> common.findFormat('.md')
('musedata', '.md')

If you give something we can’t deal with, returns a Tuple of None, None:

>>> common.findFormat('wpd')
(None, None)

These don’t work but should eventually:

# >>> common.findFormat(‘png’) # (‘musicxml.png’, ‘.png’)

# >>> common.findFormat(‘ipython’) # (‘ipython’, ‘.png’) # >>> common.findFormat(‘ipython.png’) # (‘ipython’, ‘.png’)

# >>> common.findFormat(‘musicxml.png’) # (‘musicxml.png’, ‘.png’)

music21.common.formats.findFormatExtFile(fp)

Given a file path (relative or absolute) find format and extension used (not the output extension)

DEPRECATED May 2014 – moving to converter

>>> common.findFormatExtFile('test.mx')
('musicxml', '.mx')
>>> common.findFormatExtFile('long/file/path/test-2009.03.02.xml')
('musicxml', '.xml')
>>> common.findFormatExtFile('long/file/path.intermediate.png/test-2009.03.xml')
('musicxml', '.xml')
>>> common.findFormatExtFile('test')
(None, None)

Windows drive >>> common.findFormatExtFile(‘d:/long/file/path/test.xml’) (‘musicxml’, ‘.xml’)

On a windows networked filesystem >>> common.findFormatExtFile(r’\longfilepathtest.krn’) (‘humdrum’, ‘.krn’)

music21.common.formats.findFormatExtURL(url)

Given a URL, attempt to find the extension. This may scrub arguments in a URL, or simply look at the last characters.

DEPRECATED May 2014 – moving to converter

>>> urlA = 'http://somesite.com/?l=cc/schubert/piano/d0576&file=d0576-06.krn&f=xml'
>>> urlB = 'http://somesite.com/cgi-bin/ksdata?l=cc/schubert/d0576&file=d0576-06.krn&f=kern'
>>> urlC = 'http://somesite.com/cgi-bin/ksdata?l=cc/bach/cello&file=bwv1007-01.krn&f=xml'
>>> urlF = 'http://junk'
>>> common.findFormatExtURL(urlA)
('musicxml', '.xml')
>>> common.findFormatExtURL(urlB)
('humdrum', '.krn')
>>> common.findFormatExtURL(urlC)
('musicxml', '.xml')
>>> common.findFormatExtURL(urlF)
(None, None)
music21.common.formats.findFormatFile(fp)

Given a file path (relative or absolute) return the format

DEPRECATED May 2014 – moving to converter

>>> common.findFormatFile('test.xml')
'musicxml'
>>> common.findFormatFile('long/file/path/test-2009.03.02.xml')
'musicxml'
>>> common.findFormatFile('long/file/path.intermediate.png/test-2009.03.xml')
'musicxml'

On a windows networked filesystem >>> common.findFormatFile(r’\longfilepathtest.krn’) ‘humdrum’

music21.common.formats.findInputExtension(fmt)

Will be fully deprecated when there’s an exact equivalent in converter…

Given an input format or music21 format, find and return all possible input extensions.

>>> a = common.findInputExtension('musicxml')
>>> a
('.xml', '.mxl', '.mx', '.musicxml')
>>> a = common.findInputExtension('humdrum')
>>> a
('.krn',)
>>> common.findInputExtension('musedata')
('.md', '.musedata', '.zip')

mx is not a music21 format but it is a file format

>>> common.findInputExtension('mx')
('.xml', '.mxl', '.mx', '.musicxml')

Leading dots don’t matter…

>>> common.findInputExtension('.mx')
('.xml', '.mxl', '.mx', '.musicxml')

blah is neither

>>> common.findInputExtension('blah') is None
True
music21.common.formats.findSubConverterForFormat(fmt)

return a converter.subConverter.SubConverter subclass for a given format – this is a music21 format name, not a file extension. Or returns None

>>> common.findSubConverterForFormat('musicxml')
<class 'music21.converter.subConverters.ConverterMusicXML'>
>>> common.findSubConverterForFormat('text')
<class 'music21.converter.subConverters.ConverterText'>

Some subconverters have format aliases

>>> common.findSubConverterForFormat('t')
<class 'music21.converter.subConverters.ConverterText'>