Skip to content

Instantly share code, notes, and snippets.

@bzamecnik
Created October 13, 2017 08:50
Show Gist options
  • Save bzamecnik/ee3134ab530bb8fada9f777687d60324 to your computer and use it in GitHub Desktop.
Save bzamecnik/ee3134ab530bb8fada9f777687d60324 to your computer and use it in GitHub Desktop.
music21 stream (de)serialization via pickle
# 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