music21.metadata

Classes and functions for creating and processing metadata associated with scores, works, and fragments, such as titles, movements, authors, publishers, and regions.

The Metadata object is the main public interface to metadata components. A Metadata object can be added to a Stream and used to set common score attributes, such as title and composer. A Metadata object found at offset zero can be accessed through a Stream’s metadata property.

The following example creates a Stream object, adds a Note object, and configures and adds the title and composer properties of a Metadata object.

>>> s = stream.Score()
>>> p = stream.Part()
>>> m = stream.Measure()
>>> m.append(note.Note())
>>> p.append(m)
>>> s.append(p)
>>> s.insert(0, metadata.Metadata())
>>> s.metadata.title = 'title'
>>> s.metadata.composer = 'composer'
>>> s.show()
../_images/moduleMetadata-01.png

Metadata

class music21.metadata.Metadata(*args, **keywords)

Metadata represent data for a work or fragment, including title, composer, dates, and other relevant information.

Metadata is a Music21Object subclass, meaing that it can be positioned on a Stream by offset and have a Duration.

In many cases, each Stream will have a single Metadata object at the zero offset position.

>>> md = metadata.Metadata(title='Concerto in F')
>>> md.title
'Concerto in F'
>>> md = metadata.Metadata(otl='Concerto in F') # can use abbreviations
>>> md.title
'Concerto in F'
>>> md.setWorkId('otl', 'Rhapsody in Blue')
>>> md.otl
'Rhapsody in Blue'
>>> md.title
'Rhapsody in Blue'
>>> md.composer = 'Gershwin, George'

These are used by .search() methods to determine what attributes are made available by default.

>>> md.searchAttributes
('alternativeTitle', 'composer', 'date', 'localeOfComposition', 
 'movementName', 'movementNumber', 'number', 'opusNumber', 'title')

All contributors are stored in a .contributors list:

>>> md.contributors
[<music21.metadata.primitives.Contributor composer:Gershwin, George>]

Metadata bases

Metadata read-only properties

Metadata.composers

Get a list of all Contributor objects defined as composer of this work.

Read-only properties inherited from Music21Object:

Metadata read/write properties

Metadata.alternativeTitle

Get or set the alternative title.

>>> md = metadata.Metadata(popularTitle='Eroica')
>>> md.alternativeTitle = 'Heroic Symphony'
>>> md.alternativeTitle
'Heroic Symphony'
Metadata.composer

Get or set the composer of this work. More than one composer may be specified.

The composer attribute does not live in Metadata, but creates a Contributor object in the .contributors object.

>>> md = metadata.Metadata(
...     title='Third Symphony',
...     popularTitle='Eroica',
...     composer='Beethoven, Ludwig van',
...     )
>>> md.composer
'Beethoven, Ludwig van'
Metadata.date

Get or set the date of this work as one of the following date objects:

DateSingle, DateRelative, DateBetween, DateSelection,

>>> md = metadata.Metadata(
...     title='Third Symphony',
...     popularTitle='Eroica',
...     composer='Beethoven, Ludwig van',
...     )
>>> md.date = '2010'
>>> md.date
'2010/--/--'
>>> md.date = metadata.DateBetween(['2009/12/31', '2010/1/28'])
>>> md.date
'2009/12/31 to 2010/01/28'
Metadata.localeOfComposition

Get or set the locale of composition, or origin, of the work.

Metadata.movementName

Get or set the movement title.

Note that a number of pieces from various MusicXML datasets have the piece title as the movement title. For instance, the Bach Chorales, since they are technically movements of larger cantatas.

Metadata.movementNumber

Get or set the movement number.

Metadata.number

Get or set the number of the work within a collection of pieces. (for instance, the number within a collection of ABC files)

Metadata.opusNumber

Get or set the opus number.

Metadata.title

Get the title of the work, or the next-matched title string available from a related parameter fields.

>>> md = metadata.Metadata(title='Third Symphony')
>>> md.title
'Third Symphony'
>>> md = metadata.Metadata(popularTitle='Eroica')
>>> md.title
'Eroica'
>>> md = metadata.Metadata(
...     title='Third Symphony',
...     popularTitle='Eroica',
...     )
>>> md.title
'Third Symphony'
>>> md.popularTitle
'Eroica'
>>> md.otp
'Eroica'

Read/write properties inherited from Music21Object:

Metadata methods

static Metadata.abbreviationToWorkId(abbreviation)

Get work id abbreviations.

>>> metadata.Metadata.abbreviationToWorkId('otl')
'title'
>>> for id in metadata.Metadata.workIdAbbreviationDict.keys():
...    result = metadata.Metadata.abbreviationToWorkId(id)
...
Metadata.addContributor(c)

Assign a Contributor object to this Metadata.

>>> md = metadata.Metadata(title='Gaelic Symphony')
>>> c = metadata.Contributor()
>>> c.name = 'Beach, Amy'
>>> c.role = 'composer'
>>> md.addContributor(c)
>>> md.composer
'Beach, Amy'

Add maiden name as an alternative composer name:

>>> md.composer = 'Cheney, Amy Marcy'
>>> md.composers
['Beach, Amy', 'Cheney, Amy Marcy']
Metadata.all(skipContributors=False)

Returns all values (as strings) stored in this metadata as a sorted list of tuples.

>>> c = corpus.parse('corelli/opus3no1/1grave')
>>> c.metadata.all()
[('arranger', 'Michael Scott Cuthbert'), 
 ('composer', 'Arcangelo Corelli'), 
 ('movementName', 'Sonata da Chiesa, No. I (opus 3, no. 1)')]

Skip contributors is there to help with musicxml parsing – there’s no reason for it except that we haven’t exposed enough functionality yet:

>>> c.metadata.date = metadata.DateRelative('1689', 'onOrBefore')
>>> c.metadata.localeOfComposition = 'Rome'
>>> c.metadata.all(skipContributors=True)
[('date', '1689/--/-- or earlier'), 
 ('localeOfComposition', 'Rome'), 
 ('movementName', 'Sonata da Chiesa, No. I (opus 3, no. 1)')]
Metadata.getContributorsByRole(value)

Return a Contributor if defined for a provided role.

>>> md = metadata.Metadata(title='Third Symphony')
>>> c = metadata.Contributor()
>>> c.name = 'Beethoven, Ludwig van'
>>> c.role = 'composer'
>>> md.addContributor(c)
>>> cList = md.getContributorsByRole('composer')
>>> cList[0].name
'Beethoven, Ludwig van'

Some musicxml files have contributors with no role defined. To get these contributors, search for getContributorsByRole(None). N.B. upon output to MusicXML, music21 gives these contributors the generic role of “creator”

>>> c2 = metadata.Contributor()
>>> c2.name = 'Beth Hadley'
>>> md.addContributor(c2)
>>> noRoleList = md.getContributorsByRole(None)
>>> len(noRoleList)
1
>>> noRoleList[0].role
>>> noRoleList[0].name
'Beth Hadley'
Metadata.search(query, field=None)

Search one or all fields with a query, given either as a string or a regular expression match.

>>> md = metadata.Metadata()
>>> md.composer = 'Joplin, Scott'
>>> md.title = 'Maple Leaf Rag'
>>> md.search(
...     'joplin',
...     field='composer',
...     )
(True, 'composer')

Note how the incomplete field name in the following example is still matched:

>>> md.search(
...     'joplin',
...     field='compos',
...     )
(True, 'composer')

These don’t work (Richard W. didn’t have the rhythm...)

>>> md.search(
...     'Wagner',
...     field='composer',
...     )
(False, None)
>>> md.search('Wagner')
(False, None)
>>> md.search('leaf')
(True, 'title')
>>> md.search(
...     'leaf',
...     field='composer',
...     )
(False, None)
>>> md.search(
...     'leaf',
...     field='title',
...     )
(True, 'title')
>>> md.search('leaf|entertainer')
(True, 'title')
>>> md.search('opl(.*)cott')
(True, 'composer')
Metadata.setWorkId(idStr, value)

Directly set a workd id, given either as a full string name or as a three character abbreviation. The following work id abbreviations and their full id string are given as follows. In many cases the Metadata object support properties for convenient access to these work ids.

Id abbreviations and strings: otl / title, otp / popularTitle, ota / alternativeTitle, opr / parentTitle, oac / actNumber, osc / sceneNumber, omv / movementNumber, omd / movementName, ops / opusNumber, onm / number, ovm / volume, ode / dedication, oco / commission, gtl / groupTitle, gaw / associatedWork, gco / collectionDesignation, txo / textOriginalLanguage, txl / textLanguage, ocy / countryOfComposition, opc / localeOfComposition.

>>> md = metadata.Metadata(title='Quartet')
>>> md.title
'Quartet'
>>> md.setWorkId('otl', 'Trio')
>>> md.title
'Trio'
>>> md.setWorkId('sdf', None)
Traceback (most recent call last):
music21.exceptions21.MetadataException: no work id available with id: sdf
static Metadata.workIdToAbbreviation(value)

Get a work abbreviation from a string representation.

>>> metadata.Metadata.workIdToAbbreviation('localeOfComposition')
'opc'
>>> for n in metadata.Metadata.workIdAbbreviationDict.values():
...     result = metadata.Metadata.workIdToAbbreviation(n)
...

Methods inherited from Music21Object:

Metadata instance variables

Instance variables inherited from Music21Object:

RichMetadata

class music21.metadata.RichMetadata(*args, **keywords)

RichMetadata adds to Metadata information about the contents of the Score it is attached to. TimeSignature, KeySignature and related analytical is stored. RichMetadata are generally only created in the process of creating stored JSON metadata.

>>> richMetadata = metadata.RichMetadata(title='Concerto in F')
>>> richMetadata.title
'Concerto in F'
>>> richMetadata.keySignatureFirst = key.KeySignature(-1)
>>> 'keySignatureFirst' in richMetadata.searchAttributes
True
>>> richMetadata.searchAttributes
('alternativeTitle', 'ambitus', 'composer', 'date', 
 'keySignatureFirst', 'keySignatures', 'localeOfComposition', 
 'movementName', 'movementNumber', 'noteCount', 'number', 
 'opusNumber', 'pitchHighest', 'pitchLowest', 'quarterLength', 
 'tempoFirst', 'tempos', 'timeSignatureFirst', 'timeSignatures', 'title')

RichMetadata bases

RichMetadata read-only properties

Read-only properties inherited from Metadata:

Read-only properties inherited from Music21Object:

RichMetadata read/write properties

Read/write properties inherited from Metadata:

Read/write properties inherited from Music21Object:

RichMetadata methods

RichMetadata.merge(other, favorSelf=False)

Given another Metadata or RichMetadata object, combine all attributes and return a new object.

>>> md = metadata.Metadata(title='Concerto in F')
>>> md.title
'Concerto in F'
>>> richMetadata = metadata.RichMetadata()
>>> richMetadata.merge(md)
>>> richMetadata.title
'Concerto in F'
RichMetadata.update(streamObj)

Given a Stream object, update attributes with stored objects.

Methods inherited from Metadata:

Methods inherited from Music21Object:

RichMetadata instance variables

Instance variables inherited from Music21Object: