mkiv.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  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. import math
  16. TRACKS = object()
  17. PROGRAMS = object()
  18. parser = optparse.OptionParser()
  19. parser.add_option('-s', '--channel-split', dest='chansplit', action='store_true', help='Split MIDI channels into independent tracks (as far as -T is concerned)')
  20. parser.add_option('-S', '--split-out', dest='chansfname', help='Store the split-format MIDI back into the specified file')
  21. 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)')
  22. parser.add_option('-T', '--track-split', dest='tracks', action='append_const', const=TRACKS, help='Ensure all tracks are on non-mutual streams')
  23. parser.add_option('-t', '--track', dest='tracks', action='append', help='Reserve an exclusive set of streams for certain conditions (try --help-conds)')
  24. parser.add_option('--help-conds', dest='help_conds', action='store_true', help='Print help on filter conditions for streams')
  25. parser.add_option('-p', '--program-split', dest='tracks', action='append_const', const=PROGRAMS, help='Ensure all programs are on non-mutual streams (overrides -T presently)')
  26. parser.add_option('-P', '--percussion', dest='perc', help='Which percussion standard to use to automatically filter to "perc" (GM, GM2, or none)')
  27. parser.add_option('-f', '--fuckit', dest='fuckit', action='store_true', help='Use the Python Error Steamroller when importing MIDIs (useful for extended formats)')
  28. parser.add_option('-v', '--verbose', dest='verbose', action='store_true', help='Be verbose; show important parts about the MIDI scheduling process')
  29. parser.add_option('-d', '--debug', dest='debug', action='store_true', help='Debugging output; show excessive output about the MIDI scheduling process (please use less or write to a file)')
  30. parser.add_option('-D', '--deviation', dest='deviation', type='int', help='Amount (in semitones/MIDI pitch units) by which a fully deflected pitchbend modifies the base pitch (0 disables pitchbend processing)')
  31. parser.add_option('-M', '--modwheel-freq-dev', dest='modfdev', type='float', help='Amount (in semitones/MIDI pitch unites) by which a fully-activated modwheel modifies the base pitch')
  32. parser.add_option('--modwheel-freq-freq', dest='modffreq', type='float', help='Frequency of modulation periods (sinusoids) of the modwheel acting on the base pitch')
  33. parser.add_option('--modwheel-amp-dev', dest='modadev', type='float', help='Deviation [0, 1] by which a fully-activated modwheel affects the amplitude as a factor of that amplitude')
  34. parser.add_option('--modwheel-amp-freq', dest='modafreq', type='float', help='Frequency of modulation periods (sinusoids) of the modwheel acting on amplitude')
  35. parser.add_option('--modwheel-res', dest='modres', type='float', help='(Fractional) seconds by which to resolve modwheel events (0 to disable)')
  36. parser.add_option('--tempo', dest='tempo', help='Adjust interpretation of tempo (try "f1"/"global", "f2"/"track")')
  37. parser.add_option('-0', '--keep-empty', dest='keepempty', action='store_true', help='Keep (do not cull) events with 0 duration in the output file')
  38. parser.set_defaults(tracks=[], perc='GM', deviation=2, tempo='global', modres=0.01, modfdev=1.0, modffreq=5.0, modadev=0.5, modafreq=5.0)
  39. options, args = parser.parse_args()
  40. if options.tempo == 'f1':
  41. options.tempo == 'global'
  42. elif options.tempo == 'f2':
  43. options.tempo == 'track'
  44. if options.help_conds:
  45. print '''Filter conditions are used to route events to groups of streams.
  46. Every filter is an expression; internally, this expression is evaluated as the body of a "lambda ev: ".
  47. The "ev" object will be a MergeEvent with the following properties:
  48. -ev.tidx: the originating track index (starting at 0)
  49. -ev.abstime: the real time in seconds of this event relative to the beginning of playback
  50. -ev.bank: the selected bank (all bits)
  51. -ev.prog: the selected program
  52. -ev.mw: the modwheel value
  53. -ev.ev: a midi.NoteOnEvent:
  54. -ev.ev.pitch: the MIDI pitch
  55. -ev.ev.velocity: the MIDI velocity
  56. -ev.ev.channel: the MIDI channel
  57. All valid Python expressions are accepted. Take care to observe proper shell escaping.
  58. Specifying a -t <group>=<filter> will group all streams under a filter; if the <group> part is omitted, no group will be added.
  59. For example:
  60. mkiv -t bass=ev.ev.pitch<35 -t treble=ev.ev.pitch>75 -T -t ev.abstime<10
  61. will cause these groups to be made:
  62. -A group "bass" with all notes with pitch less than 35;
  63. -Of those not in "bass", a group in "treble" with pitch>75;
  64. -Of what is not yet consumed, a series of groups "trkN" where N is the track index (starting at 0), which consumes the rest.
  65. -An (unfortunately empty) unnamed group with events prior to ten real seconds.
  66. As can be seen, order of specification is important. Equally important is the location of -T, which should be at the end.
  67. NoteOffEvents are always matched to the stream which has their corresponding NoteOnEvent (in track, pitch, and channel), and so are
  68. not affected or observed by filters.
  69. If the filters specified are not a complete cover, an anonymous group will be created with no filter to contain the rest. If
  70. it is desired to force this group to have a name, use -t <group>=True. This should be placed at the end.
  71. -T behaves exactly as if:
  72. -t trk0=ev.tidx==0 -t trk1=ev.tidx==1 -t trk2=ev.tidx==2 [...]
  73. had been specified in its place, though it is automatically sized to the number of tracks. Similarly, -P operates as if
  74. -t prg31=ev.prog==31 -t prg81=ev.prog==81 [...]
  75. had been specified, again containing only the programs that were observed in the piece.
  76. Groups for which no streams are generated are not written to the resulting file.'''
  77. exit()
  78. if not args:
  79. parser.print_usage()
  80. exit()
  81. if options.fuckit:
  82. import fuckit
  83. midi.read_midifile = fuckit(midi.read_midifile)
  84. for fname in args:
  85. try:
  86. pat = midi.read_midifile(fname)
  87. except Exception:
  88. import traceback
  89. traceback.print_exc()
  90. print fname, ': Exception occurred, skipping...'
  91. continue
  92. if pat is None:
  93. print fname, ': Too fucked to continue'
  94. continue
  95. iv = ET.Element('iv')
  96. iv.set('version', '1')
  97. iv.set('src', os.path.basename(fname))
  98. print fname, ': MIDI format,', len(pat), 'tracks'
  99. if options.verbose:
  100. print fname, ': MIDI Parameters:', pat.resolution, 'PPQN,', pat.format, 'format'
  101. if options.chansplit:
  102. print 'Splitting channels...'
  103. old_pat = pat
  104. pat = midi.Pattern(resolution=old_pat.resolution)
  105. for track in old_pat:
  106. chan_map = {}
  107. last_abstick = {}
  108. absticks = 0
  109. for ev in track:
  110. absticks += ev.tick
  111. if isinstance(ev, midi.Event):
  112. tick = absticks - last_abstick.get(ev.channel, 0)
  113. last_abstick[ev.channel] = absticks
  114. if options.chanskeep:
  115. newev = ev.copy(tick = tick)
  116. else:
  117. newev = ev.copy(channel=1, tick = tick)
  118. chan_map.setdefault(ev.channel, midi.Track()).append(newev)
  119. else: # MetaEvent
  120. for trk in chan_map.itervalues():
  121. trk.append(ev)
  122. items = chan_map.items()
  123. items.sort(key=lambda pair: pair[0])
  124. for chn, trk in items:
  125. pat.append(trk)
  126. print 'Split', len(old_pat), 'tracks into', len(pat), 'tracks by channel'
  127. if options.chansfname:
  128. midi.write_midifile(options.chansfname, pat)
  129. ##### Merge events from all tracks into one master list, annotated with track and absolute times #####
  130. print 'Merging events...'
  131. class SortEvent(object):
  132. __slots__ = ['ev', 'tidx', 'abstick']
  133. def __init__(self, ev, tidx, abstick):
  134. self.ev = ev
  135. self.tidx = tidx
  136. self.abstick = abstick
  137. sorted_events = []
  138. for tidx, track in enumerate(pat):
  139. absticks = 0
  140. for ev in track:
  141. absticks += ev.tick
  142. sorted_events.append(SortEvent(ev, tidx, absticks))
  143. sorted_events.sort(key=lambda x: x.abstick)
  144. if options.tempo == 'global':
  145. bpm_at = [{0: 120}]
  146. else:
  147. bpm_at = [{0: 120} for i in pat]
  148. print 'Computing tempos...'
  149. for sev in sorted_events:
  150. if isinstance(sev.ev, midi.SetTempoEvent):
  151. if options.debug:
  152. print fname, ': SetTempo at', sev.abstick, 'to', sev.ev.bpm, ':', sev.ev
  153. bpm_at[sev.tidx if options.tempo == 'track' else 0][sev.abstick] = sev.ev.bpm
  154. if options.verbose:
  155. print fname, ': Events:', len(sorted_events)
  156. print fname, ': Resolved global BPM:', bpm_at
  157. if options.debug:
  158. if options.tempo == 'track':
  159. for tidx, bpms in enumerate(bpm_at):
  160. print fname, ': Tempos in track', tidx
  161. btimes = bpms.keys()
  162. for i in range(len(btimes) - 1):
  163. fev = filter(lambda sev: sev.tidx == tidx and sev.abstick >= btimes[i] and sev.abstick < btimes[i+1], sorted_events)
  164. print fname, ': BPM partition', i, 'contains', len(fev), 'events'
  165. else:
  166. btimes = bpm_at[0].keys()
  167. for i in range(len(btimes) - 1):
  168. fev = filter(lambda sev: sev.abstick >= btimes[i] and sev.abstick < btimes[i+1], sorted_events)
  169. print fname, ': BPM partition', i, 'contains', len(fev), 'events'
  170. def at2rt(abstick, bpms):
  171. bpm_segs = bpms.items()
  172. bpm_segs.sort(key=lambda pair: pair[0])
  173. bpm_segs = filter(lambda pair: pair[0] <= abstick, bpm_segs)
  174. rt = 0
  175. atick = 0
  176. if not bpm_segs:
  177. rt = 0
  178. else:
  179. ctick, bpm = bpm_segs[0]
  180. rt = (60.0 * ctick) / (bpm * pat.resolution)
  181. for idx in range(1, len(bpm_segs)):
  182. dt = bpm_segs[idx][0] - bpm_segs[idx-1][0]
  183. bpm = bpm_segs[idx-1][1]
  184. rt += (60.0 * dt) / (bpm * pat.resolution)
  185. if not bpm_segs:
  186. bpm = 120
  187. ctick = 0
  188. else:
  189. ctick, bpm = bpm_segs[-1]
  190. if options.debug:
  191. print 'seg through', bpm_segs, 'final seg', (abstick - ctick, bpm)
  192. rt += (60.0 * (abstick - ctick)) / (bpm * pat.resolution)
  193. return rt
  194. class MergeEvent(object):
  195. __slots__ = ['ev', 'tidx', 'abstime', 'bank', 'prog', 'mw']
  196. def __init__(self, ev, tidx, abstime, bank=0, prog=0, mw=0):
  197. self.ev = ev
  198. self.tidx = tidx
  199. self.abstime = abstime
  200. self.bank = bank
  201. self.prog = prog
  202. self.mw = mw
  203. def copy(self, **kwargs):
  204. args = {'ev': self.ev, 'tidx': self.tidx, 'abstime': self.abstime, 'bank': self.bank, 'prog': self.prog, 'mw': self.mw}
  205. args.update(kwargs)
  206. return MergeEvent(**args)
  207. def __repr__(self):
  208. return '<ME %r in %d on (%d:%d) MW:%d @%f>'%(self.ev, self.tidx, self.bank, self.prog, self.mw, self.abstime)
  209. events = []
  210. cur_mw = [[0 for i in range(16)] for j in range(len(pat))]
  211. cur_bank = [[0 for i in range(16)] for j in range(len(pat))]
  212. cur_prog = [[0 for i in range(16)] for j in range(len(pat))]
  213. chg_mw = [[0 for i in range(16)] for j in range(len(pat))]
  214. chg_bank = [[0 for i in range(16)] for j in range(len(pat))]
  215. chg_prog = [[0 for i in range(16)] for j in range(len(pat))]
  216. ev_cnts = [[0 for i in range(16)] for j in range(len(pat))]
  217. tnames = [''] * len(pat)
  218. progs = set([0])
  219. for tidx, track in enumerate(pat):
  220. abstime = 0
  221. absticks = 0
  222. lastbpm = 120
  223. for ev in track:
  224. absticks += ev.tick
  225. abstime = at2rt(absticks, bpm_at[tidx if options.tempo == 'track' else 0])
  226. if options.debug:
  227. print 'tick', absticks, 'realtime', abstime
  228. if isinstance(ev, midi.TrackNameEvent):
  229. tnames[tidx] = ev.text
  230. if isinstance(ev, midi.ProgramChangeEvent):
  231. cur_prog[tidx][ev.channel] = ev.value
  232. progs.add(ev.value)
  233. chg_prog[tidx][ev.channel] += 1
  234. elif isinstance(ev, midi.ControlChangeEvent):
  235. if ev.control == 0: # Bank -- MSB
  236. cur_bank[tidx][ev.channel] = (0x3F80 & cur_bank[tidx][ev.channel]) | ev.value
  237. chg_bank[tidx][ev.channel] += 1
  238. elif ev.control == 32: # Bank -- LSB
  239. cur_bank[tidx][ev.channel] = (0x3F & cur_bank[tidx][ev.channel]) | (ev.value << 7)
  240. chg_bank[tidx][ev.channel] += 1
  241. elif ev.control == 1: # ModWheel -- MSB
  242. cur_mw[tidx][ev.channel] = (0x3F80 & cur_mw[tidx][ev.channel]) | ev.value
  243. chg_mw[tidx][ev.channel] += 1
  244. elif ev.control == 33: # ModWheel -- LSB
  245. cur_mw[tidx][ev.channel] = (0x3F & cur_mw[tidx][ev.channel]) | (ev.value << 7)
  246. chg_mw[tidx][ev.channel] += 1
  247. events.append(MergeEvent(ev, tidx, abstime, cur_bank[tidx][ev.channel], cur_prog[tidx][ev.channel], cur_mw[tidx][ev.channel]))
  248. ev_cnts[tidx][ev.channel] += 1
  249. elif isinstance(ev, midi.MetaEventWithText):
  250. events.append(MergeEvent(ev, tidx, abstime))
  251. elif isinstance(ev, midi.Event):
  252. if isinstance(ev, midi.NoteOnEvent) and ev.velocity == 0:
  253. ev.__class__ = midi.NoteOffEvent #XXX Oww
  254. events.append(MergeEvent(ev, tidx, abstime, cur_bank[tidx][ev.channel], cur_prog[tidx][ev.channel], cur_mw[tidx][ev.channel]))
  255. ev_cnts[tidx][ev.channel] += 1
  256. if options.verbose:
  257. print 'Track name, event count, final banks, bank changes, final programs, program changes, final modwheel, modwheel changes:'
  258. for tidx, tname in enumerate(tnames):
  259. 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])), ',', ','.join(map(str, cur_mw[tidx])), ',', ','.join(map(str, chg_mw[tidx]))
  260. print 'All programs observed:', progs
  261. print 'Sorting events...'
  262. events.sort(key = lambda ev: ev.abstime)
  263. ##### Use merged events to construct a set of streams with non-overlapping durations #####
  264. print 'Generating streams...'
  265. class DurationEvent(MergeEvent):
  266. __slots__ = ['duration', 'pitch', 'modwheel', 'ampl']
  267. def __init__(self, me, pitch, ampl, dur, modwheel=0):
  268. MergeEvent.__init__(self, me.ev, me.tidx, me.abstime, me.bank, me.prog, me.mw)
  269. self.pitch = pitch
  270. self.ampl = ampl
  271. self.duration = dur
  272. self.modwheel = modwheel
  273. class NoteStream(object):
  274. __slots__ = ['history', 'active', 'bentpitch', 'modwheel']
  275. def __init__(self):
  276. self.history = []
  277. self.active = None
  278. self.bentpitch = None
  279. self.modwheel = 0
  280. def IsActive(self):
  281. return self.active is not None
  282. def Activate(self, mev, bentpitch = None, modwheel = None):
  283. if bentpitch is None:
  284. bentpitch = mev.ev.pitch
  285. self.active = mev
  286. self.bentpitch = bentpitch
  287. if modwheel is not None:
  288. self.modwheel = modwheel
  289. def Deactivate(self, mev):
  290. self.history.append(DurationEvent(self.active, self.bentpitch, self.active.ev.velocity / 127.0, mev.abstime - self.active.abstime, self.modwheel))
  291. self.active = None
  292. self.bentpitch = None
  293. def WouldDeactivate(self, mev):
  294. if not self.IsActive():
  295. return False
  296. if isinstance(mev.ev, midi.NoteOffEvent):
  297. return mev.ev.pitch == self.active.ev.pitch and mev.tidx == self.active.tidx and mev.ev.channel == self.active.ev.channel
  298. if isinstance(mev.ev, midi.PitchWheelEvent):
  299. return mev.tidx == self.active.tidx and mev.ev.channel == self.active.ev.channel
  300. if isinstance(mev.ev, midi.ControlChangeEvent):
  301. return mev.tidx == self.active.tidx and mev.ev.channel == self.active.ev.channel
  302. raise TypeError('Tried to deactivate with bad type %r'%(type(mev.ev),))
  303. class NSGroup(object):
  304. __slots__ = ['streams', 'filter', 'name']
  305. def __init__(self, filter=None, name=None):
  306. self.streams = []
  307. self.filter = (lambda mev: True) if filter is None else filter
  308. self.name = name
  309. def Accept(self, mev):
  310. if not self.filter(mev):
  311. return False
  312. for stream in self.streams:
  313. if not stream.IsActive():
  314. stream.Activate(mev)
  315. break
  316. else:
  317. stream = NoteStream()
  318. self.streams.append(stream)
  319. stream.Activate(mev)
  320. return True
  321. notegroups = []
  322. auxstream = []
  323. textstream = []
  324. if options.perc and options.perc != 'none':
  325. if options.perc == 'GM':
  326. notegroups.append(NSGroup(filter = lambda mev: mev.ev.channel == 9, name='perc'))
  327. elif options.perc == 'GM2':
  328. notegroups.append(NSGroup(filter = lambda mev: mev.bank == 15360, name='perc'))
  329. else:
  330. print 'Unrecognized --percussion option %r; should be GM, GM2, or none'%(options.perc,)
  331. for spec in options.tracks:
  332. if spec is TRACKS:
  333. for tidx in xrange(len(pat)):
  334. notegroups.append(NSGroup(filter = lambda mev, tidx=tidx: mev.tidx == tidx, name = 'trk%d'%(tidx,)))
  335. elif spec is PROGRAMS:
  336. for prog in progs:
  337. notegroups.append(NSGroup(filter = lambda mev, prog=prog: mev.prog == prog, name = 'prg%d'%(prog,)))
  338. else:
  339. if '=' in spec:
  340. name, _, spec = spec.partition('=')
  341. else:
  342. name = None
  343. notegroups.append(NSGroup(filter = eval("lambda ev: "+spec), name = name))
  344. if options.verbose:
  345. print 'Initial group mappings:'
  346. for group in notegroups:
  347. print ('<anonymous>' if group.name is None else group.name)
  348. for mev in events:
  349. if isinstance(mev.ev, midi.MetaEventWithText):
  350. textstream.append(mev)
  351. elif isinstance(mev.ev, midi.NoteOnEvent):
  352. for group in notegroups:
  353. if group.Accept(mev):
  354. break
  355. else:
  356. group = NSGroup()
  357. group.Accept(mev)
  358. notegroups.append(group)
  359. elif isinstance(mev.ev, midi.NoteOffEvent):
  360. for group in notegroups:
  361. found = False
  362. for stream in group.streams:
  363. if stream.WouldDeactivate(mev):
  364. stream.Deactivate(mev)
  365. found = True
  366. break
  367. if found:
  368. break
  369. else:
  370. print 'WARNING: Did not match %r with any stream deactivation.'%(mev,)
  371. if options.verbose:
  372. print ' Current state:'
  373. for group in notegroups:
  374. print ' Group %r:'%(group.name,)
  375. for stream in group.streams:
  376. print ' Stream: %r'%(stream.active,)
  377. elif options.deviation > 0 and isinstance(mev.ev, midi.PitchWheelEvent):
  378. found = False
  379. for group in notegroups:
  380. for stream in group.streams:
  381. if stream.WouldDeactivate(mev):
  382. base = stream.active.copy(abstime=mev.abstime)
  383. stream.Deactivate(mev)
  384. stream.Activate(base, base.ev.pitch + options.deviation * (mev.ev.pitch / float(0x2000)))
  385. found = True
  386. if not found:
  387. print 'WARNING: Did not find any matching active streams for %r'%(mev,)
  388. if options.verbose:
  389. print ' Current state:'
  390. for group in notegroups:
  391. print ' Group %r:'%(group.name,)
  392. for stream in group.streams:
  393. print ' Stream: %r'%(stream.active,)
  394. elif options.modres > 0 and isinstance(mev.ev, midi.ControlChangeEvent):
  395. found = False
  396. for group in notegroups:
  397. for stream in group.streams:
  398. if stream.WouldDeactivate(mev):
  399. base = stream.active.copy(abstime=mev.abstime)
  400. stream.Deactivate(mev)
  401. stream.Activate(base, stream.bentpitch, mev.mw)
  402. found = True
  403. if not found:
  404. print 'WARNING: Did not find any matching active streams for %r'%(mev,)
  405. if options.verbose:
  406. print ' Current state:'
  407. for group in notegroups:
  408. print ' Group %r:'%(group.name,)
  409. for stream in group.streams:
  410. print ' Stream: %r'%(stream.active,)
  411. else:
  412. auxstream.append(mev)
  413. lastabstime = events[-1].abstime
  414. for group in notegroups:
  415. for ns in group.streams:
  416. if ns.IsActive():
  417. print 'WARNING: Active notes at end of playback.'
  418. ns.Deactivate(MergeEvent(ns.active, ns.active.tidx, lastabstime))
  419. if options.modres > 0:
  420. print 'Resolving modwheel events...'
  421. ev_cnt = 0
  422. for group in notegroups:
  423. for ns in group.streams:
  424. i = 0
  425. while i < len(ns.history):
  426. dev = ns.history[i]
  427. if dev.modwheel > 0:
  428. realpitch = dev.pitch
  429. realamp = dev.ampl
  430. dt = 0.0
  431. events = []
  432. while dt < dev.duration:
  433. events.append(DurationEvent(dev, realpitch + options.modfdev * math.sin(options.modffreq * (dev.abstime + dt)), realamp + options.modadev * (math.sin(options.modafreq * (dev.abstime + dt)) - 1.0) / 2.0, dev.duration, dev.modwheel))
  434. dt += options.modres
  435. ns.history[i:i+1] = events
  436. i += len(events)
  437. ev_cnt += len(events)
  438. else:
  439. i += 1
  440. print '...resolved', ev_cnt, 'events'
  441. if not options.keepempty:
  442. print 'Culling empty events...'
  443. for group in notegroups:
  444. for ns in group.streams:
  445. i = 0
  446. while i < len(ns.history):
  447. if ns.history[i].duration == 0.0:
  448. del ns.history[i]
  449. else:
  450. i += 1
  451. if options.verbose:
  452. print 'Final group mappings:'
  453. for group in notegroups:
  454. print ('<anonymous>' if group.name is None else group.name), '<=', '(', len(group.streams), 'streams)'
  455. print 'Generated %d streams in %d groups'%(sum(map(lambda x: len(x.streams), notegroups)), len(notegroups))
  456. print 'Playtime:', lastabstime, 'seconds'
  457. ##### Write to XML and exit #####
  458. ivmeta = ET.SubElement(iv, 'meta')
  459. abstime = 0
  460. prevticks = 0
  461. prev_bpm = 120
  462. for tidx, bpms in enumerate(bpm_at):
  463. ivbpms = ET.SubElement(ivmeta, 'bpms', track=str(tidx))
  464. for absticks, bpm in sorted(bpms.items(), key = lambda pair: pair[0]):
  465. abstime += ((absticks - prevticks) * 60.0) / (prev_bpm * pat.resolution)
  466. prevticks = absticks
  467. ivbpm = ET.SubElement(ivbpms, 'bpm')
  468. ivbpm.set('bpm', str(bpm))
  469. ivbpm.set('ticks', str(absticks))
  470. ivbpm.set('time', str(abstime))
  471. ivstreams = ET.SubElement(iv, 'streams')
  472. for group in notegroups:
  473. for ns in group.streams:
  474. ivns = ET.SubElement(ivstreams, 'stream')
  475. ivns.set('type', 'ns')
  476. if group.name is not None:
  477. ivns.set('group', group.name)
  478. for note in ns.history:
  479. ivnote = ET.SubElement(ivns, 'note')
  480. ivnote.set('pitch', str(note.pitch))
  481. ivnote.set('vel', str(int(note.ampl * 127.0)))
  482. ivnote.set('ampl', str(note.ampl))
  483. ivnote.set('time', str(note.abstime))
  484. ivnote.set('dur', str(note.duration))
  485. ivtext = ET.SubElement(ivstreams, 'stream', type='text')
  486. for tev in textstream:
  487. ivev = ET.SubElement(ivtext, 'text', time=str(tev.abstime), type=type(tev.ev).__name__, text=tev.ev.text)
  488. ivaux = ET.SubElement(ivstreams, 'stream')
  489. ivaux.set('type', 'aux')
  490. fw = midi.FileWriter()
  491. fw.RunningStatus = None # XXX Hack
  492. for mev in auxstream:
  493. ivev = ET.SubElement(ivaux, 'ev')
  494. ivev.set('time', str(mev.abstime))
  495. ivev.set('data', repr(fw.encode_midi_event(mev.ev)))
  496. print 'Done.'
  497. open(os.path.splitext(os.path.basename(fname))[0]+'.iv', 'w').write(ET.tostring(iv))