Last active
December 20, 2015 14:39
-
-
Save eleisinger/6148438 to your computer and use it in GitHub Desktop.
Investigation of music21 Haydn data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import music21 | |
import os | |
from collections import defaultdict | |
HAYDN = music21.corpus.getWork('haydn') | |
def title(h): | |
path, mvt = os.path.split(h) | |
mvt, ext = os.path.splitext(mvt) | |
qrt = os.path.basename(path) | |
return qrt, mvt | |
def parse_path(h): | |
"""Given a path, returns Opus, Number, Mvmt #""" | |
if not 'opus' in h: | |
return # fuck the non-quartets | |
path, mvt = os.path.split(h) | |
mvt, ext = os.path.splitext(mvt) | |
qrt = os.path.basename(path) | |
# e.g. opus71no2 | |
# movement2 | |
mvt = int(mvt[-1]) | |
qrt = qrt.replace("opus", "") | |
if "no" in qrt: | |
opus = qrt[:qrt.index('no')] | |
number = int(qrt[-1]) | |
return int(opus), number, mvt | |
else: | |
opus = int(qrt) | |
return opus, mvt | |
def v(i): | |
s = music21.corpus.parse(HAYDN[i]) | |
# s.show(app=musescore) | |
return s.fileFormat | |
def show_formats(): | |
for i, path in enumerate(HAYDN): | |
format = v(i) | |
qrt, mvt = title(path) | |
print qrt, mvt, format | |
def show_incomplete(): | |
""" | |
9/3, 33/2 missing 1 mvt each. | |
Also missing complete qrts: | |
1/5 (not qrt -- symphony) | |
all opus 2 | |
9/1, 9/4, 9/5, 9/6 | |
17/4 | |
""" | |
counts = defaultdict(int) | |
for i, path in enumerate(HAYDN): | |
qrt, mvt = title(path) | |
counts[qrt] += 1 | |
for q, i in counts.iteritems(): | |
if i < 4: | |
print q, i | |
def save_xml(directory='/Users/elaineleisinger/Dropbox/haydn/musicxml'): | |
for path in HAYDN: | |
parsed = parse_path(path) | |
if parsed: | |
try: | |
quartet, number, mvmt = parsed | |
desired_name = '%03d_%dm%d.xml' % (quartet, number, mvmt) | |
except ValueError: | |
quartet, mvmt = parsed | |
desired_name = '%03dm%d.xml' % (quartet, mvmt) | |
desired_path = os.path.join(directory, desired_name) | |
score = music21.corpus.parse(path) | |
tmp_path = score.write() | |
os.rename(tmp_path, desired_path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment