music21 Examples

Code snippets for common music manipulation tasks in the music21 python module.

Fetching a tune from URL

Note that unless you specify a type, the type will be assumed *not* from the actual extension of the file but from the presence of an extension–like string at the end of the URL, and can therefore be faked! For example:

  • http://waterpigs.co.uk/wpsm/tune.php?id=38 will not work, but
  • http://waterpigs.co.uk/wpsm/tune.php?id=38&fmt=.abc will.

tok = music21.converter.parse('http://waterpigs.co.uk/wpsm/tune.php?id=38&fmt=.abc')
bourree = music21.converter.parse('http://static.wikifonia.org/13271/musicxml.mxl')

Transposing an entire tune (stream) up or down

The built–in transpose method by default only transposes Note and Chord objects, if you wish to actually transpose the entire tune you’ll have to add KeySignature to the list of transposed classnames


# score_c is a Score object, e.g. a tune in C
# transpose(int num_semitones, bool in-place, list classnames)
score_g = score_c.transpose(7, False, ['Note', 'Chord', 'KeySignature'])