Skip to content

Instantly share code, notes, and snippets.

@eruffaldi
Created February 7, 2019 14:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eruffaldi/b30650a7be5092fada384b3981af1aab to your computer and use it in GitHub Desktop.
Save eruffaldi/b30650a7be5092fada384b3981af1aab to your computer and use it in GitHub Desktop.
MIDI to notes
from mido import MidiFile
import mido
import sys
def ntof(n):
return 440*(2**((n-69)/12))
def main():
mid = MidiFile(sys.argv[1])
print("ticks per beat:",mid.ticks_per_beat)
#print("us per beat:",)
tempo = 120
for x in mid:
d = x.dict()
if type(x) is mido.midifiles.meta.MetaMessage:
if d.get("type") == "set_tempo":
print("tempo",d["tempo"],d)
tempo = d["tempo"]
else:
print("other",d)
else:
n = d.get("note")
if n is not None:
print(d["type"],ntof(n),mido.tick2second(d["time"],mid.ticks_per_beat,tempo))
else:
print(x)
if __name__ == '__main__':
print(ntof(127))
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment