#!/usr/bin/python import sys, string, math import mido min_note = 21 max_note = 21+63 max_voices = 3 one_voice_per_channel = 1 tempo = 48 compress = 0 transpose = 0 fn = sys.argv[1] mid = mido.MidiFile(fn) def hex2(n): return '0x%02x'%n def only_notes(s): for ch in s: if ord(ch) == 0xff: return False return True def find_common_substrings(s): results = {} for l in range(64, 6, -1): for i in range(0,len(s)-l*2): match = s[i:i+l] if not only_notes(match): continue count = 0 j = i+l while j < len(s): p = s.find(match, j) if p > 0: count += 1 j = p+l else: break if count: n = count*(l-1)-1 if not results.get(i) or n > results[i][0]: results[i] = (n,l) return results def get_best_substring(ss): best = (0,0,0) for k,v in ss.items(): if v[0] > best[2]: best = (k,v[1],v[0]) return best def offset2str(ofs): return chr(ofs & 0xff) + chr((ofs >> 8) & 0xff) #return chr(0xc0 | (ofs & 0x3f)) + chr(0xc0 | ((ofs >> 6) & 0x3f)) g_code = 0xc1 g_subs = [] def replace_substrings(s, bss): global g_code i,l,n = bss count = (n+1)/(l-1) match = s[i:i+l] print i,l,n,count,repr(match) r = s[0:i] while i 3: transpose = int(sys.argv[3]) gtime = 0 curtime = 0 nnotes = 0 nvoices = 0 curchans = 0 channels = [int(x) for x in string.split(sys.argv[2], ',')] print print "// %s %s" % (mid, channels) output = [] for msg in mid: gtime += msg.time * tempo if msg.type == 'note_on' and msg.channel in channels: note = msg.note + transpose vel = msg.velocity t = int(math.ceil(gtime)) if vel > 0: while curtime < t: dt = min(63, t-curtime) curtime += dt if nnotes > 0: nvoices = 0 curchans = 0 output.append(dt+128) if note >= min_note and note <= max_note and nvoices < max_voices: if not (one_voice_per_channel and (curchans & (1< %d bytes" % (len(output), len(zout))