Created
October 13, 2017 08:50
-
-
Save bzamecnik/ee3134ab530bb8fada9f777687d60324 to your computer and use it in GitHub Desktop.
music21 stream (de)serialization via pickle
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
# It's possible to serialize music21 stream to pickle. | |
# But for 1 kB of MIDI the pickle is 89 kB (!), after gzip only 8 kB. | |
# The benefit can be lower loading time compared to parsing MIDI | |
# at the cost of roughly 8x file size. | |
import music21 | |
stream = music21.converter.parse("foo.mid") | |
pickled = music21.converter.freezeStr(stream) | |
with open("foo.pickle", "wb") as f: | |
f.write(pickled) | |
with open("foo.pickle", "rb") as f: | |
pickledFromFile = f.read() | |
streamFromPickle = music21.converter.thawStr(pickledFromFile) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment