mkiv.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. '''
  2. itl_chorus -- ITL Chorus Suite
  3. mkiv -- Make Intervals
  4. This simple script (using python-midi) reads a MIDI file and makes an interval
  5. (.iv) file (actually XML) that contains non-overlapping notes.
  6. TODO:
  7. -MIDI Control events
  8. -Percussion
  9. '''
  10. import xml.etree.ElementTree as ET
  11. import midi
  12. import sys
  13. import os
  14. import optparse
  15. TRACKS = object()
  16. parser = optparse.OptionParser()
  17. parser.add_option('-s', '--channel-split', dest='chansplit', action='store_true', help='Split MIDI channels into independent tracks (as far as -T is concerned)')
  18. parser.add_option('-S', '--split-out', dest='chansfname', help='Store the split-format MIDI back into the specified file')
  19. parser.add_option('-c', '--preserve-channels', dest='chanskeep', action='store_true', help='Keep the channel number when splitting channels to tracks (default is to set it to 1)')
  20. parser.add_option('-T', '--track-split', dest='tracks', action='append_const', const=TRACKS, help='Ensure all tracks are on non-mutual streams')
  21. parser.add_option('-t', '--track', dest='tracks', action='append', help='Reserve an exclusive set of streams for certain conditions (try --help-conds)')
  22. parser.add_option('--help-conds', dest='help_conds', action='store_true', help='Print help on filter conditions for streams')
  23. parser.add_option('-P', '--percussion', dest='perc', help='Which percussion standard to use to automatically filter to "perc" (GM, GM2, or none)')
  24. parser.add_option('-f', '--fuckit', dest='fuckit', action='store_true', help='Use the Python Error Steamroller when importing MIDIs (useful for extended formats)')
  25. parser.add_option('-n', '--target-num', dest='repeaterNumber', type='int', help='Target count of devices')
  26. parser.add_option('-v', '--verbose', dest='verbose', action='store_true', help='Be verbose; show important parts about the MIDI scheduling process')
  27. parser.add_option('-d', '--debug', dest='debug', action='store_true', help='Debugging output; show excessive output about the MIDI scheduling process')
  28. parser.set_defaults(tracks=[], repeaterNumber=1, perc='GM')
  29. options, args = parser.parse_args()
  30. if options.help_conds:
  31. print '''Filter conditions are used to route events to groups of streams.
  32. Every filter is an expression; internally, this expression is evaluated as the body of a "lambda ev: ".
  33. The "ev" object will be a MergeEvent with the following properties:
  34. -ev.tidx: the originating track index (starting at 0)
  35. -ev.abstime: the real time in seconds of this event relative to the beginning of playback
  36. -ev.bank: the selected bank (all bits)
  37. -ev.prog: the selected program
  38. -ev.ev: a midi.NoteOnEvent:
  39. -ev.ev.pitch: the MIDI pitch
  40. -ev.ev.velocity: the MIDI velocity
  41. -ev.ev.channel: the MIDI channel
  42. All valid Python expressions are accepted. Take care to observe proper shell escaping.
  43. Specifying a -t <group>=<filter> will group all streams under a filter; if the <group> part is omitted, no group will be added.
  44. For example:
  45. mkiv -t bass=ev.ev.pitch<35 -t treble=ev.ev.pitch>75 -T -t ev.abstime<10
  46. will cause these groups to be made:
  47. -A group "bass" with all notes with pitch less than 35;
  48. -Of those not in "bass", a group in "treble" with pitch>75;
  49. -Of what is not yet consumed, a series of groups "trkN" where N is the track index (starting at 0), which consumes the rest.
  50. -An (unfortunately empty) unnamed group with events prior to ten real seconds.
  51. As can be seen, order of specification is important. Equally important is the location of -T, which should be at the end.
  52. NoteOffEvents are always matched to the stream which has their corresponding NoteOnEvent (in track and pitch), and so are
  53. not affected or observed by filters.
  54. If the filters specified are not a complete cover, an anonymous group will be created with no filter to contain the rest. If
  55. it is desired to force this group to have a name, use -t <group>=True.'''
  56. exit()
  57. if not args:
  58. parser.print_usage()
  59. exit()
  60. if options.fuckit:
  61. import fuckit
  62. midi.read_midifile = fuckit(midi.read_midifile)
  63. for fname in args:
  64. try:
  65. pat = midi.read_midifile(fname)
  66. except Exception:
  67. import traceback
  68. traceback.print_exc()
  69. print fname, ': Exception occurred, skipping...'
  70. continue
  71. if pat is None:
  72. print fname, ': Too fucked to continue'
  73. continue
  74. iv = ET.Element('iv')
  75. iv.set('version', '1')
  76. iv.set('src', os.path.basename(fname))
  77. print fname, ': MIDI format,', len(pat), 'tracks'
  78. if options.chansplit:
  79. print 'Splitting channels...'
  80. old_pat = pat
  81. pat = midi.Pattern(resolution=old_pat.resolution)
  82. for track in old_pat:
  83. chan_map = {}
  84. last_abstick = {}
  85. absticks = 0
  86. for ev in track:
  87. absticks += ev.tick
  88. if isinstance(ev, midi.Event):
  89. tick = absticks - last_abstick.get(ev.channel, 0)
  90. last_abstick[ev.channel] = absticks
  91. if options.chanskeep:
  92. newev = ev.copy(tick = tick)
  93. else:
  94. newev = ev.copy(channel=1, tick = tick)
  95. chan_map.setdefault(ev.channel, midi.Track()).append(newev)
  96. else: # MetaEvent
  97. for trk in chan_map.itervalues():
  98. trk.append(ev)
  99. items = chan_map.items()
  100. items.sort(key=lambda pair: pair[0])
  101. for chn, trk in items:
  102. pat.append(trk)
  103. print 'Split', len(old_pat), 'tracks into', len(pat), 'tracks by channel'
  104. if options.chansfname:
  105. midi.write_midifile(options.chansfname, pat)
  106. ##### Merge events from all tracks into one master list, annotated with track and absolute times #####
  107. print 'Merging events...'
  108. class MergeEvent(object):
  109. __slots__ = ['ev', 'tidx', 'abstime', 'bank', 'prog']
  110. def __init__(self, ev, tidx, abstime, bank, prog):
  111. self.ev = ev
  112. self.tidx = tidx
  113. self.abstime = abstime
  114. self.bank = bank
  115. self.prog = prog
  116. def __repr__(self):
  117. return '<ME %r in %d @%f>'%(self.ev, self.tidx, self.abstime)
  118. events = []
  119. bpm_at = {0: 120}
  120. cur_bank = [[0 for i in range(16)] for j in range(len(pat))]
  121. cur_prog = [[0 for i in range(16)] for j in range(len(pat))]
  122. chg_bank = [[0 for i in range(16)] for j in range(len(pat))]
  123. chg_prog = [[0 for i in range(16)] for j in range(len(pat))]
  124. ev_cnts = [[0 for i in range(16)] for j in range(len(pat))]
  125. tnames = [''] * len(pat)
  126. for tidx, track in enumerate(pat):
  127. abstime = 0
  128. absticks = 0
  129. for ev in track:
  130. if options.debug:
  131. print ev
  132. if isinstance(ev, midi.SetTempoEvent):
  133. absticks += ev.tick
  134. bpm_at[absticks] = ev.bpm
  135. elif isinstance(ev, midi.ProgramChangeEvent):
  136. cur_prog[tidx][ev.channel] = ev.value
  137. chg_prog[tidx][ev.channel] += 1
  138. elif isinstance(ev, midi.ControlChangeEvent):
  139. if ev.control == 0:
  140. cur_bank[tidx][ev.channel] = (0x3F80 & cur_bank[tidx][ev.channel]) | ev.value
  141. chg_bank[tidx][ev.channel] += 1
  142. elif ev.control == 32:
  143. cur_bank[tidx][ev.channel] = (0x3F & cur_bank[tidx][ev.channel]) | (ev.value << 7)
  144. chg_bank[tidx][ev.channel] += 1
  145. elif isinstance(ev, midi.TrackNameEvent):
  146. tnames[tidx] = ev.text
  147. elif isinstance(ev, midi.Event):
  148. if isinstance(ev, midi.NoteOnEvent) and ev.velocity == 0:
  149. ev.__class__ = midi.NoteOffEvent #XXX Oww
  150. bpm = filter(lambda pair: pair[0] <= absticks, sorted(bpm_at.items(), key=lambda pair: pair[0]))[-1][1]
  151. abstime += (60.0 * ev.tick) / (bpm * pat.resolution)
  152. absticks += ev.tick
  153. events.append(MergeEvent(ev, tidx, abstime, cur_bank[tidx][ev.channel], cur_prog[tidx][ev.channel]))
  154. ev_cnts[tidx][ev.channel] += 1
  155. if options.verbose:
  156. print 'Track name, event count, final banks, bank changes, final programs, program changes:'
  157. for tidx, tname in enumerate(tnames):
  158. print tidx, ':', tname, ',', ','.join(map(str, ev_cnts[tidx])), ',', ','.join(map(str, cur_bank[tidx])), ',', ','.join(map(str, chg_bank[tidx])), ',', ','.join(map(str, cur_prog[tidx])), ',', ','.join(map(str, chg_prog[tidx]))
  159. print 'Sorting events...'
  160. events.sort(key = lambda ev: ev.abstime)
  161. ##### Use merged events to construct a set of streams with non-overlapping durations #####
  162. print 'Generating streams...'
  163. class DurationEvent(MergeEvent):
  164. __slots__ = ['duration']
  165. def __init__(self, me, dur):
  166. MergeEvent.__init__(self, me.ev, me.tidx, me.abstime, me.bank, me.prog)
  167. self.duration = dur
  168. class NoteStream(object):
  169. __slots__ = ['history', 'active']
  170. def __init__(self):
  171. self.history = []
  172. self.active = None
  173. def IsActive(self):
  174. return self.active is not None
  175. def Activate(self, mev):
  176. self.active = mev
  177. def Deactivate(self, mev):
  178. self.history.append(DurationEvent(self.active, mev.abstime - self.active.abstime))
  179. self.active = None
  180. def WouldDeactivate(self, mev):
  181. if not self.IsActive():
  182. return False
  183. return mev.ev.pitch == self.active.ev.pitch and mev.tidx == self.active.tidx
  184. class NSGroup(object):
  185. __slots__ = ['streams', 'filter', 'name']
  186. def __init__(self, filter=None, name=None):
  187. self.streams = []
  188. self.filter = (lambda mev: True) if filter is None else filter
  189. self.name = name
  190. def Accept(self, mev):
  191. if not self.filter(mev):
  192. return False
  193. for stream in self.streams:
  194. if not stream.IsActive():
  195. stream.Activate(mev)
  196. break
  197. else:
  198. stream = NoteStream()
  199. self.streams.append(stream)
  200. stream.Activate(mev)
  201. return True
  202. notegroups = []
  203. auxstream = []
  204. if options.perc and options.perc != 'none':
  205. if options.perc == 'GM':
  206. notegroups.append(NSGroup(filter = lambda mev: mev.ev.channel == 9, name='perc'))
  207. elif options.perc == 'GM2':
  208. notegroups.append(NSGroup(filter = lambda mev: mev.bank == 15360, name='perc'))
  209. else:
  210. print 'Unrecognized --percussion option %r; should be GM, GM2, or none'%(options.perc,)
  211. for spec in options.tracks:
  212. if spec is TRACKS:
  213. for tidx in xrange(len(pat)):
  214. notegroups.append(NSGroup(filter = lambda mev, tidx=tidx: mev.tidx == tidx, name = 'trk%d'%(tidx,)))
  215. else:
  216. if '=' in spec:
  217. name, _, spec = spec.partition('=')
  218. else:
  219. name = None
  220. notegroups.append(NSGroup(filter = eval("lambda ev: "+spec), name = name))
  221. if options.verbose:
  222. print 'Initial group mappings:'
  223. for group in notegroups:
  224. print ('<anonymous>' if group.name is None else group.name)
  225. for mev in events:
  226. if isinstance(mev.ev, midi.NoteOnEvent):
  227. for group in notegroups:
  228. if group.Accept(mev):
  229. break
  230. else:
  231. group = NSGroup()
  232. group.Accept(mev)
  233. notegroups.append(group)
  234. elif isinstance(mev.ev, midi.NoteOffEvent):
  235. for group in notegroups:
  236. found = False
  237. for stream in group.streams:
  238. if stream.WouldDeactivate(mev):
  239. stream.Deactivate(mev)
  240. found = True
  241. break
  242. if found:
  243. break
  244. else:
  245. print 'WARNING: Did not match %r with any stream deactivation.'%(mev,)
  246. else:
  247. auxstream.append(mev)
  248. lastabstime = events[-1].abstime
  249. for group in notegroups:
  250. for ns in group.streams:
  251. if ns.IsActive():
  252. print 'WARNING: Active notes at end of playback.'
  253. ns.Deactivate(MergeEvent(ns.active, ns.active.tidx, lastabstime))
  254. if options.verbose:
  255. print 'Final group mappings:'
  256. for group in notegroups:
  257. print ('<anonymous>' if group.name is None else group.name), '<=', '(', len(group.streams), 'streams)'
  258. print 'Generated %d streams in %d groups'%(sum(map(lambda x: len(x.streams), notegroups)), len(notegroups))
  259. print 'Playtime:', lastabstime, 'seconds'
  260. ##### Write to XML and exit #####
  261. ivmeta = ET.SubElement(iv, 'meta')
  262. ivbpms = ET.SubElement(ivmeta, 'bpms')
  263. abstime = 0
  264. prevticks = 0
  265. prev_bpm = 120
  266. for absticks, bpm in sorted(bpm_at.items(), key = lambda pair: pair[0]):
  267. abstime += ((absticks - prevticks) * 60.0) / (prev_bpm * pat.resolution)
  268. prevticks = absticks
  269. ivbpm = ET.SubElement(ivbpms, 'bpm')
  270. ivbpm.set('bpm', str(bpm))
  271. ivbpm.set('ticks', str(absticks))
  272. ivbpm.set('time', str(abstime))
  273. ivstreams = ET.SubElement(iv, 'streams')
  274. x = 0
  275. while(x<options.repeaterNumber):
  276. for group in notegroups:
  277. for ns in group.streams:
  278. ivns = ET.SubElement(ivstreams, 'stream')
  279. ivns.set('type', 'ns')
  280. if group.name is not None:
  281. ivns.set('group', group.name)
  282. for note in ns.history:
  283. ivnote = ET.SubElement(ivns, 'note')
  284. ivnote.set('pitch', str(note.ev.pitch))
  285. ivnote.set('vel', str(note.ev.velocity))
  286. ivnote.set('time', str(note.abstime))
  287. ivnote.set('dur', str(note.duration))
  288. x+=1
  289. if(x>=options.repeaterNumber and options.repeaterNumber!=1):
  290. break
  291. if(x>=options.repeaterNumber and options.repeaterNumber!=1):
  292. break
  293. if(x>=options.repeaterNumber and options.repeaterNumber!=1):
  294. break
  295. ivaux = ET.SubElement(ivstreams, 'stream')
  296. ivaux.set('type', 'aux')
  297. fw = midi.FileWriter()
  298. fw.RunningStatus = None # XXX Hack
  299. for mev in auxstream:
  300. ivev = ET.SubElement(ivaux, 'ev')
  301. ivev.set('time', str(mev.abstime))
  302. ivev.set('data', repr(fw.encode_midi_event(mev.ev)))
  303. print 'Done.'
  304. open(os.path.splitext(os.path.basename(fname))[0]+'.iv', 'w').write(ET.tostring(iv))