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:

  • will not work, but
  • will.

tok = music21.converter.parse('')
bourree = music21.converter.parse('')

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'])