Previous topic

music21.corpus.chorales

Next topic

music21.corpus.virtual

Table Of Contents

Table Of Contents

This Page

music21.corpus.corpora

Corpus

class music21.corpus.corpora.Corpus

Abstract base class of all corpora subclasses.

Corpus read-only properties

Corpus.metadataBundle

The metadata bundle for a corpus:

>>> from music21 import corpus
>>> corpus.CoreCorpus().metadataBundle
<music21.metadata.bundles.MetadataBundle 'core': {149... entries}>
Corpus.name

The name of a given corpus.

Corpus methods

static Corpus.fromName(name)

Instantiate a specific corpus based on name:

>>> from music21 import corpus
>>> corpus.Corpus.fromName('core')
<music21.corpus.corpora.CoreCorpus>
>>> corpus.Corpus.fromName('virtual')
<music21.corpus.corpora.VirtualCorpus>
>>> corpus.Corpus.fromName('local')
<music21.corpus.corpora.LocalCorpus: 'local'>
>>> corpus.Corpus.fromName('test')
<music21.corpus.corpora.LocalCorpus: 'test'>
Corpus.getPaths()

The paths of the files in a given corpus.

static Corpus.listSearchFields()

List all available search field names:

>>> for field in corpus.Corpus.listSearchFields():
...     field
...
'alternativeTitle'
'ambitus'
'composer'
'date'
'keySignatureFirst'
'keySignatures'
'localeOfComposition'
'movementName'
'movementNumber'
'noteCount'
'number'
'opusNumber'
'pitchHighest'
'pitchLowest'
'quarterLength'
'tempoFirst'
'tempos'
'timeSignatureFirst'
'timeSignatures'
'title'
static Corpus.parse(workName, movementNumber=None, number=None, fileExtensions=None, forceSource=False)

The most important method call for corpus.

Similar to the parse() method of converter (which takes in a filepath on the local hard drive), this method searches the corpus (including the virtual corpus) for a work fitting the workName description and returns a music21.stream.Stream.

If movementNumber is defined, and a movement is included in the corpus, that movement will be returned.

If number is defined, and the work is a collection with multiple components, that work number will be returned. For instance, some of our ABC documents contain dozens of folk songs within a single file.

Advanced: if forceSource is True, the original file will always be loaded freshly and pickled (e.g., pre-parsed) files will be ignored. This should not be needed if the file has been changed, since the filetime of the file and the filetime of the pickled version are compared. But it might be needed if the music21 parsing routine has changed.

Example, get a chorale by Bach. Note that the source type does not need to be specified, nor does the name Bach even (since it’s the only piece with the title BWV 66.6)

>>> from music21 import corpus
>>> bachChorale = corpus.Corpus.parse('bwv66.6')
>>> len(bachChorale.parts)
4

After parsing, the file path within the corpus is stored as .corpusFilePath

>>> bachChorale.corpusFilepath
u'bach/bwv66.6.mxl'
static Corpus.search(query, field=None, corpusNames=None, fileExtensions=None)

Search all stored metadata bundles and return a list of file paths.

The names parameter can be used to specify which corpora to search, for example:

>>> corpus.Corpus.search(
...     'bach',
...     corpusNames=('core', 'virtual'),
...     )
<music21.metadata.bundles.MetadataBundle {150 entries}>

If names is None, all corpora known to music21 will be searched.

This method uses stored metadata and thus, on first usage, will incur a performance penalty during metadata loading.

Corpus.updateMetadataBundle()

Update a corpus’ metadata bundle from its stored JSON file on disk.

CoreCorpus

class music21.corpus.corpora.CoreCorpus

A model of the core corpus.

>>> coreCorpus = corpus.CoreCorpus()

CoreCorpus bases

CoreCorpus read-only properties

CoreCorpus.name
CoreCorpus.noCorpus

Return True or False if this is a corpus or noCoprus distribution.

>>> from music21 import corpus
>>> corpus.CoreCorpus().noCorpus
False

Read-only properties inherited from Corpus:

CoreCorpus read/write properties

CoreCorpus.manualCoreCorpusPath

Set music21’s core corpus to a directory, and save that information in the user settings.

This is specifically for use with “no corpus” music21 packages, where the core corpus was not included with the rest of the package functionality, and had to be installed separately.

Set it to a directory:

>>> coreCorpus = corpus.CoreCorpus()
>>> coreCorpus.manualCoreCorpusPath = '~/Desktop'

Unset it:

>>> coreCorpus.manualCoreCorpusPath = None
>>> coreCorpus.manualCoreCorpusPath is None
True

CoreCorpus methods

CoreCorpus.getBachChorales(fileExtensions='xml')

Return the file name of all Bach chorales.

By default, only Bach Chorales in xml format are returned, because the quality of the encoding and our parsing of those is superior.

N.B. Look at the module corpus.chorales for many better ways to work with the chorales.

>>> from music21 import corpus
>>> coreCorpus = corpus.CoreCorpus()
>>> a = coreCorpus.getBachChorales()
>>> len(a) > 400
True
>>> a = coreCorpus.getBachChorales('krn')
>>> len(a) > 10
False
>>> a = coreCorpus.getBachChorales('xml')
>>> len(a) > 400
True
>>> a[0]
u'/Users/cuthbert/Documents/music21/corpus/bach/bwv1.6.mxl'
CoreCorpus.getBeethovenStringQuartets(fileExtensions=None)

Return a list of all Beethoven String Quartet filenames.

>>> from music21 import corpus
>>> coreCorpus = corpus.CoreCorpus()
>>> a = coreCorpus.getBeethovenStringQuartets()
>>> len(a) > 10
True
>>> a = coreCorpus.getBeethovenStringQuartets('krn')
>>> len(a) < 10 and len(a) > 0
True
>>> a = coreCorpus.getBeethovenStringQuartets('xml')
>>> len(a) > 400
False
CoreCorpus.getComposer(composerName, fileExtensions=None)

Return all filenames in the corpus that match a composer’s or a collection’s name. An fileExtensions, if provided, defines which extensions are returned. An fileExtensions of None (default) returns all extensions.

Note that xml and mxl are treated equivalently.

>>> from music21 import corpus
>>> coreCorpus = corpus.CoreCorpus()
>>> a = coreCorpus.getComposer('beethoven')
>>> len(a) > 10
True
>>> a = coreCorpus.getComposer('mozart')
>>> len(a) > 10
True
>>> a = coreCorpus.getComposer('bach', 'krn')
>>> len(a) < 10
True
>>> a = coreCorpus.getComposer('bach', 'xml')
>>> len(a) > 10
True
CoreCorpus.getComposerDirectoryPath(composerName)

Given the name of a composer, get the path to the top-level directory of that composer:

>>> import os
>>> from music21 import corpus
>>> coreCorpus = corpus.CoreCorpus()
>>> a = coreCorpus.getComposerDirectoryPath('beethoven')
>>> a.endswith(os.path.join('corpus', os.sep, 'beethoven'))
True
>>> a = coreCorpus.getComposerDirectoryPath('bach')
>>> a.endswith(os.path.join('corpus', os.sep, 'bach'))
True
>>> a = coreCorpus.getComposerDirectoryPath('mozart')
>>> a.endswith(os.path.join('corpus', os.sep, 'mozart'))
True
CoreCorpus.getHandelMessiah(fileExtensions='md')

Return a list of the filenames of all parts of Handel’s Messiah:

>>> from music21 import corpus
>>> coreCorpus = corpus.CoreCorpus()
>>> a = coreCorpus.getHandelMessiah()
>>> len(a)
43
CoreCorpus.getMonteverdiMadrigals(fileExtensions='xml')

Return a list of the filenames of all Monteverdi madrigals.

>>> from music21 import corpus
>>> coreCorpus = corpus.CoreCorpus()
>>> a = coreCorpus.getMonteverdiMadrigals()
>>> len(a) > 40
True
CoreCorpus.getPaths(fileExtensions=None, expandExtensions=True)

Get all paths in the core corpus that match a known extension, or an extenion provided by an argument.

If expandExtensions is True, a format for an extension, and related extensions, will replaced by all known input extensions.

This is convenient when an input format might match for multiple extensions.

>>> from music21 import corpus
>>> coreCorpus = corpus.CoreCorpus()
>>> corpusFilePaths = coreCorpus.getPaths()
>>> len(corpusFilePaths)
3044
>>> kernFilePaths = coreCorpus.getPaths('krn')
>>> len(kernFilePaths) >= 500
True
>>> abcFilePaths = coreCorpus.getPaths('abc')
>>> len(abcFilePaths) >= 100
True
CoreCorpus.getWorkList(workName, movementNumber=None, fileExtensions=None)

Search the corpus and return a list of filenames of works, always in a list.

If no matches are found, an empty list is returned.

>>> from music21 import corpus
>>> coreCorpus = corpus.CoreCorpus()
>>> len(coreCorpus.getWorkList('beethoven/opus18no1'))
8
>>> len(coreCorpus.getWorkList('beethoven/opus18no1', 1))
2
>>> len(coreCorpus.getWorkList('beethoven/opus18no1', 1, '.krn'))
1
>>> len(coreCorpus.getWorkList('beethoven/opus18no1', 1, '.xml'))
1
>>> len(coreCorpus.getWorkList('beethoven/opus18no1', 0, '.xml'))
0
>>> len(coreCorpus.getWorkList('handel/hwv56', '1-02', '.md'))
1
>>> len(coreCorpus.getWorkList('handel/hwv56', (2, 1), '.md'))
1
>>> len(coreCorpus.getWorkList(
...     'bach/artOfFugue_bwv1080', 2, '.md'))
1

Make sure that ‘verdi’ just gets the single Verdi piece and not the Monteverdi pieces:

>>> len(coreCorpus.getWorkList('verdi'))
1
CoreCorpus.search(query, field=None, fileExtensions=None)

Search the core corpus for metadata entries:

>>> corpus.CoreCorpus().search('3/4')
<music21.metadata.bundles.MetadataBundle {2012 entries}>
>>> corpus.CoreCorpus().search(
...      'bach',
...      field='composer',
...      )
<music21.metadata.bundles.MetadataBundle {21 entries}>
>>> predicate = lambda noteCount: noteCount < 20
>>> corpus.CoreCorpus().search(
...     predicate,
...     field='noteCount',
...     )
<music21.metadata.bundles.MetadataBundle {131 entries}>

Methods inherited from Corpus:

LocalCorpus

class music21.corpus.corpora.LocalCorpus(name=None)

A model of a local corpus.

>>> localCorpus = corpus.LocalCorpus()

The default local corpus is unnamed, but an arbitrary number of independent, named local corpora can be defined and persisted:

>>> namedLocalCorpus = corpus.LocalCorpus('with a name')

LocalCorpus bases

LocalCorpus read-only properties

LocalCorpus.directoryPaths

The directory paths in use by a given local corpus.

LocalCorpus.existsInSettings

True if this local corpus has a corresponding entry in music21’s user settings, otherwise false.

LocalCorpus.name

The name of a given local corpus.

>>> from music21 import corpus
>>> corpus.LocalCorpus().name
'local'
>>> corpus.LocalCorpus(name='Bach Chorales').name
'Bach Chorales'

Read-only properties inherited from Corpus:

LocalCorpus methods

LocalCorpus.addPath(directoryPath)

Add a directory path to a local corpus:

>>> localCorpus = corpus.LocalCorpus('a new corpus')
>>> localCorpus.addPath('~/Desktop')

Paths added in this way will not be persisted from session to session unless explicitly saved by a call to LocalCorpus.save().

LocalCorpus.delete()

Delete a non-default local corpus from the user settings.

LocalCorpus.getPaths(fileExtensions=None, expandExtensions=True)

Access files in additional directories supplied by the user and defined in environment settings in the ‘localCorpusSettings’ list.

If additional paths are added on a per-session basis with the addPath() function, these paths are also returned with this method.

static LocalCorpus.listLocalCorporaNames()

List the names of all user-defined local corpora.

The entry for None refers to the default local corpus.

LocalCorpus.removePath(directoryPath)

Remove a directory path from a local corpus.

If that path is included in the list of persisted paths for the given corpus, it will be removed permanently.

LocalCorpus.save()

Save the current list of directory paths in use by a given corpus in the user settings.

LocalCorpus.search(query, field=None, fileExtensions=None)

Search a local corpus.

See music21.corpus.corpora.Corpus.search for examples, as the interface is the same.

Methods inherited from Corpus:

VirtualCorpus

class music21.corpus.corpora.VirtualCorpus

A model of the virtual corpus.

>>> virtualCorpus = corpus.VirtualCorpus()

VirtualCorpus bases

VirtualCorpus read-only properties

VirtualCorpus.name

The name of the virtual corpus:

>>> corpus.VirtualCorpus().name
'virtual'

Read-only properties inherited from Corpus:

VirtualCorpus methods

VirtualCorpus.getPaths(fileExtensions=None, expandExtensions=True)

Get all paths in the virtual corpus that match a known extension.

An extension of None will return all known extensions.

>>> from music21 import corpus
>>> len(corpus.VirtualCorpus().getPaths()) > 6
True
VirtualCorpus.getWorkList(workName, movementNumber=None, fileExtensions=None)

Given a work name, search all virtual works and return a list of URLs for any matches.

>>> from music21 import corpus
>>> virtualCorpus = corpus.VirtualCorpus()
>>> virtualCorpus.getWorkList('bach/bwv1007/prelude')
['http://kern.ccarh.org/cgi-bin/ksdata?l=cc/bach/cello&file=bwv1007-01.krn&f=xml']
>>> virtualCorpus.getWorkList('junk')
[]
VirtualCorpus.search(query, field=None, fileExtensions=None)

Search the virtual corpus.

See music21.corpus.corpora.Corpus.search for examples, as the interface is the same.

Methods inherited from Corpus: