Sunday, February 16, 2014

Plotting pitches and durations continuously in music21

With music21, it's not hard to plot discrete data (pitches, durations, etc.) as continuous data. There isn't a built in tool for doing this, but since music21 is written in Python, it is easy to take advantage of the tools from matplotlib, numpy, and scipy to create "cubic bezier-curve splines" that show these points in an easily visualized format.

In music21 you can easily plot the position of notes as a piano roll:

from music21 import corpus

bach = corpus.parse('bwv66.6')

bach.plot('pianoroll')



which preserves pitch names, measure numbers, etc.  But the case we're asking for requires a plot more like this:




















The numbers at the left are midi numbers while the bottom is number of quarter notes from the beginning. Here's some code to help you achieve this:

import numpy as np
import matplotlib.pyplot as plt
from scipy import interpolate
from music21 import corpus

bach = corpus.parse('bwv66.6')

fig = plt.figure()

for i in range(len(bach.parts)):
    top = bach.parts[i].flat.notes
    y = [n.ps for n in top]
    x = [n.offset + n.quarterLength/2.0 for n in top]

    tck = interpolate.splrep(x,y,s=0)
    xnew = np.arange(0,max(x),0.01)
    ynew = interpolate.splev(xnew,tck,der=0)
    
    subplot = fig.add_subplot(111) # semi-arbitrary three-digit number
    subplot.plot(xnew,ynew)
plt.title('Bach motion')

plt.show()

With this sort of graph it's easy to isolate each voice (not much overlap of voices in this chorale) and to see the preponderance of similar motion among the Soprano, Alto, and Tenor, but lack of coordination with the Bass (which would create forbidden parallels if it coordinated).  More sophisticated examples with better labels are easily created by those with knowledge of matplotlib, but this simple demonstration will suffice to get things started.

Monday, November 4, 2013

Music21 on GitHub

Just a quick note that music21's code is now hosted on GitHub at https://github.com/cuthbertLab/music21 .  For people who have been using SVN, we will have instructions on how to make a Fork of the music21 Git repository and to begin contributing pull requests soon.  Please hang tight.

Friday, November 1, 2013

Music21 v.1.7 Released

The newest version of music21, v. 1.7 has been released and is available for download at https://code.google.com/p/music21/downloads/list.

In the three months since v. 1.6 some good changes and improvements have been introduced.  We focused primarily on stabilizing features that were already in music21 in some form but were too experimental to advertise widely.

A noCorpus version of music21 has also been released, the first since v.1.0.  This version can be used in pure Free/Libre projects since files that were licensed for music21 only or non-commercial use have been removed.  If you are not sure which version to download, definitely get the full version. But maintainers of Debian Linux and others can update to 1.7 noCorpus.

Music21 v 1.7 (actually 1.7.1) will be the last version to support Python 2.6. Python 2.7 is over three years old and is supported by other flavors of Python including Jython (which skipped 2.6), PyPy, IronPython and is an easy upgrade for Python on Windows.  Mac users have had 2.7 since Mountain Lion and we're happy to report that with Mavericks being free and supporting systems that can run Snow Leopard, we're happy to be able to use this opportunity to take advantage of the latest features and start a roadmap to supporting Python 3.3 as well.  This is also the last release to use SVN.  We are moving to GitHub.  Updates soon.

The most important improvement for users is a much improved system of metadata searching (thanks to Josiah Oberholtzer). See:

http://web.mit.edu/music21/doc/usersGuide/usersGuide_11_corpusSearching.html

for more details.  LocalCorpus objects are elevated to equal status as the Core corpus so you can now build searchable indexes on any data you have and find the file you want much faster.  Try corpus.search('haydn') and read the docs above to see what's possible.

Among the other 150+ changes since 1.6 include:

  • Chord.inversion(2) will take a root position chord and put it in second inversion. (this is a change of behavior from before, where .inversion(2) would specify that the chord was in second inversion and override default inversion reporting (for things like Jazz 6 chords). To get the old behavior, use .inversion(2, transposeOnSet=False)
  • Fixes for abc parsing (N.B. the next version will rename the "abc" module to "abcNotation" to avoid the occasional name clash with the python AbstractBaseClass (abc) module).
  • Stream.getElementsByOffset(4.0) can now find a zero-length object at 4.0 -- bug fix.
  • Many modules are now packages (Stream, for instance).  This should not affect your code.  Existing packages with X/base.py can now find their files in X/__init__.py.  Again, this should not affect your code.
  • Page break support in Lilypond.
  • MIDI translate works better with instruments (thanks to Christopher Antilla)
  • Improvements to Braille Music Code output (thanks to Mario Lang; more to come)
  • Bug fixes in measure copying involving pivot chords and secondary dominants in RomanText
  • Roman numerals for VII, VI, viio/vii, vi/vio in minor are made more robust.  It6, Ger65, Fr43, are now supported.
  • Many many many bug fixes.  Thanks to community help!
Thanks as always to the NEH, Digging into Data program, the Seaver Institute, and MIT for their support of the project.

Thursday, August 8, 2013

music21 v.1.6.0 Released

Version 1.6.0 of music21 was released today.  This is the first new release in three months and is available at: http://code.google.com/p/music21/downloads/  .  The release also inaugurates a new documentation system, http://web.mit.edu/music21/doc/ designed by new music21 Lead Programmer, Josiah Wolf Oberholtzer and featuring an expanded User's Guide using the iPython Notebook, which allows for more examples, more graphics, and quicker development.  Expect to see more there soon.

Improved and updated docs were the main focuses of the new release, but you will also find:
  • Many improvements and fixes to the harmony.Harmony object.
  • Freezing/Thawing of Streams that were originally MIDI files now works.
  • Better handling of repeat endings.
  • Measure suffixes are handled more "musically"
  • Easier setup on UNIX systems and using PIP and other tools.
  • Improvements in Lilypond output
  • Documentation on using music21 with iPython in a multi-core, cluster environment.
This version of music21 is a larger download than before (68MB as opposed to 30MB) because all of the documentation in both old and new formats is included.  The next version will return to normal size.

One backwards incompatible change: Stream.elements now returns a tuple instead of a List.  This is done to emphasize that .elements should not be changed.  Instead use Stream.append, .remove, .insert, or subscript the Stream object.  Chord.pitches also returns a tuple instead of a List as well.  

Thanks to everyone for their contributions and comments that make music21 work well! 

Saturday, May 11, 2013

music21 v. 1.5 Released

A new version of music21 has been released, the first public release since January.  This release focuses primarily on speed and stability improvements so it is worth an upgrade even if none of the new features is of use to you.  A summary of what's new:


  1. Many improvements to Layout -- information soon, but pixel level positioning of measures can now be extracted from MusicXML files.
  2. Chordify addPartIdAsGroup=True works better.
  3. Better triplet handling in Humdrum SpineParser.
  4. Some .nwc files (not just .nwctxt) files can now be parsed.
  5. Improvements for PartStaffs (Piano scores, etc.)
  6. .transpose works on Key objects.
Thanks to everyone for support and new ideas on where to take the project.

Friday, January 11, 2013

Music21 version 1.4.0 Released

Version 1.4.0 of music21 is released.  This is a minor release on the outside, but incorporates a number of changes that will let us do more substantial changes in the upcoming next release.  Among the most substantial changes that people will notice are:

  1. Better documentation and more chapters in the User's Guide
  2. Ability to import Capella XML (.capx) files
  3. Articulations, grace notes, crescendo, and diminuendo now import in ABC (and the code is in place to bring in many more !exclamation! tags).  Thanks to Dylan Nagler, Harvard Research Partner for the code.
  4. analysis.neoRiemannian allows for analyzing the effect of smooth voice leading on chords.  Thanks to Maura Church, Harvard Research Partner for the code.
  5. Humdrum parsing improvements, including comments and better handling of multiple voices and importing of instruments.
  6. Lilypond now supports different numbers of stafflines in output
  7. Percussion support for MIDI and the basics of an Unpitched object type.  Thanks to Ben Houge for commits.
  8. Improvements to chords, including a better .root() algorithm for incomplete chords, geometricNormalForm -- implements Dmitri Tymoczko's algorithm for normal form.
  9. More useful errors when parsing incorrect or unsupported features in several formats.
  10. New files in the corpus, including many 14th century scores, and the 2nd movement of Clara Schumann's Piano Trio.  Female composers are hugely underrepresented in computer-readable music repertories (we couldn't find any substantial piece that was available), so we're proud to add an important work (known to many of us from the Norton Anthology of Western Music) by a great compose.
  11. Serialization of Streams via Pickle is much better tested and works even on large scores.  See the freezeThaw module.  If you're going to work with the same pieces over and over again, freeze them once and you'll load them over 3x as fast the next time you need them.
  12. MusicXML improvements: bowing marks are now supported as are pizzicato, etc.  A bug on piano staves where one used multiple voices but the other did not has been fixed.
  13. Improvements to the harmony module.  Thanks to Beth Hadley, MIT Undergraduate Research Assistant.
  14. Big speed increase on startup for people who have installed additional modules: numpy, scipy, and matplotlib are only loaded when first needed.

The next release will include support for virtualenv installs (I know people have been waiting for it, but this release included switching the entire development/commit platform, so we didn't want to change too much at once) and will have optional support for Rational number durations and offsets for perfect work with complex tuplets, etc.

In the meantime, I've been using music21 to explore similarity in fourteenth-century music to great success, so I hope to be able to share my own experiences as a user, not just a developer, of the toolkit very soon.  -- Michael

Sunday, October 28, 2012

New Orleans news and Josquin Research Project

Several items of interest:

1) The joint meeting of the American Musicological Society, Society for Music Theory, and Society for Ethnomusicology will take place on Nov 1-4 in New Orleans.  There are several sessions of interest for music21 users: a panel on Corpus Research by ELVIS collaborator Ian Quinn on Saturday morning, a discussion of MEI the day before the conference on Oct 31, a panel on musical databases for medieval and Renaissance music on Thursday afternoon (concurrent with my less digital paper on Italian influence in early fifteenth-century music), the meeting of the Computational Music Theory group, and several other meetings that are slipping my mind right now -- all in all an important place for digital musicology!

2) The New England Chapter of the American Musicological Society will host its Winter meeting on Saturday, Feb 2 at Tufts University in Medford, MA (a Boston suburb).  The call for papers has just gone out requesting:
abstracts of up to 300 words for papers and roundtable sessions. Submissions in the area of digital musicology are of particular interest, but proposals on all musicological topics are welcome. Abstracts should be submitted by Monday, 26 November 2012 via email to jsholes at bu.edu (Jacquelyn Sholes)
I hope that people working on digital musicology will choose to apply.   
3) Craig Sapp's recent blog post details the work being done by our friends in the Josquin Research Project at Stanford University.  In addition to creating a number of wonderful tools for analyzing their data online or with Humdrum, the JRP has made all their data available, primarily in Humdrum's KERN format.  Music21 reads these files, including the new KERN rhythm extension.  The results of the JRP so far will be part of the Thursday afternoon AMS session in New Orleans.

Happy analyzing!