#!/usr/bin/env python2 import midi import sys RATE = 16384 #RATE = 8192 def array(type, name, data): data = list(data) print "const %s %s[] = {" % (type, name) print " " + ", ".join(str(int(x)) for x in data) print "}; // " + str(len(data)) + " elements\n" array("uint16_t", "note_fstep", [int((2 ** ((n - 69) / 12.0) * 440) * 12) for n in range(0, 106)]) mid = midi.read_midifile(sys.argv[1]) combined = [] for track in mid: t = 0 for ev in track: t += ev.tick ev.tick = t combined.append(ev) combined.sort(lambda a, b: cmp(a.tick, b.tick)) #print "\n".join(str(x) for x in combined[0:200] if x.name == "Note On" and x.channel == 0) #print combined for i in range(len(combined) - 1, -1, -1): if i > 0: combined[i].tick = combined[i].tick - combined[i-1].tick #print combined notes = [] #chans = [0, 1, 2, 3, 4, 5] #chans = [0, 1, 2, 3, 4, 5, 6] chans = None for ev in combined: if ev.tick: #print "// sleep: " + str(ev.tick) tick = ev.tick while tick > 0: notes.append(min(tick, 0x7F)) tick -= 0x7F #if ev.name == "Note On" and ev.channel in chans: # print "// " + str(ev) if ev.name == "Note On" and ev.data[1] and ev.channel != 10 and (not chans or ev.channel in chans): n = ev.data[0] #print "// TOOK" # if ev.channel == 1: # n += 12 notes.append(0x80 | n) array("uint8_t", "notes", notes)