comparison musiccutter.py @ 4:9f4fa5f231e6

Rework to use mido iterator and paginate. Pagination doesn't handle notes extending over a page yet.
author Daniel O'Connor <darius@dons.net.au>
date Mon, 07 Mar 2016 21:23:59 +1030
parents 49a33c431b45
children af683606184e
comparison
equal deleted inserted replaced
3:49a33c431b45 4:9f4fa5f231e6
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 2
3 import exceptions 3 import exceptions
4 import itertools
4 import mido 5 import mido
5 import svgwrite 6 import svgwrite
6 import sys 7 import sys
7 8
8 def test(filename = None): 9 def test(filename = None):
9 # http://www.orgues-de-barbarie.com/wp-content/uploads/2014/09/format-cartons.pdf 10 # http://www.orgues-de-barbarie.com/wp-content/uploads/2014/09/format-cartons.pdf
10 conf = { 'notefile' : 'notes', 'pitch' : 5.5 , 'pagewidth' : 20, 'pageheight' : 15.5 } 11 conf = { 'notefile' : 'notes', 'pagewidth' : 20, 'pageheight' : 15.5,
12 'pitch' : 0.55, 'offset' : 0.60, 'timescale' : 1.0 }
11 midi2note, note2midi = genmidi2note() 13 midi2note, note2midi = genmidi2note()
12 note2slot = loadnote2slot(conf['notefile'], note2midi) 14 note2slot = loadnote2slot(conf['notefile'], note2midi)
13 if filename == None: 15 if filename == None:
14 filename = 'test.midi' 16 filename = 'test.midi'
15 midi2svg(filename, 'test%d.svg', conf['pitch'], midi2note, note2midi, note2slot, conf['pagewidth'], conf['pageheight']) 17 midi2svg(filename, 'test%02d.svg', midi2note, note2midi, note2slot, conf['pagewidth'], conf['pageheight'],
18 conf['pitch'], conf['offset'], conf['timescale'])
16 19
17 # http://www.electronics.dit.ie/staff/tscarff/Music_technology/midi/midi_note_numbers_for_octaves.htm 20 # http://www.electronics.dit.ie/staff/tscarff/Music_technology/midi/midi_note_numbers_for_octaves.htm
18 def genmidi2note(): 21 def genmidi2note():
19 '''Create forward & reverse tables for midi number to note name (assuming 69 == A440)''' 22 '''Create forward & reverse tables for midi number to note name (assuming 69 == A440)'''
20 names = ['C%d', 'C%d#', 'D%d', 'D%d#', 'E%d', 'F%d', 'F%d#', 'G%d', 'G%d#', 'A%d', 'A%d#', 'B%d'] 23 names = ['C%d', 'C%d#', 'D%d', 'D%d#', 'E%d', 'F%d', 'F%d#', 'G%d', 'G%d#', 'A%d', 'A%d#', 'B%d']
44 note2slot[note] = index 47 note2slot[note] = index
45 index += 1 48 index += 1
46 49
47 return note2slot 50 return note2slot
48 51
49 def emitnote(svg, slot, start, notelen, pageheight): 52 def emitnote(pages, outpat, slot, start, notelen, pagewidth, pageheight, pitch, offset, timescale):
50 # Let us say 185 ticks for a quaver and that is 0.5cm long 53 x = start / timescale
51 tickscale = 185.0 / 0.5 54 pageidx = int(x / pagewidth)
52 pitch = 0.55 55 x = x % pagewidth
53 offset = 0.60
54 x = start / tickscale
55 y = pageheight - (offset + slot * pitch) 56 y = pageheight - (offset + slot * pitch)
56 w = notelen / tickscale 57 w = notelen / timescale
57 h = pitch 58 h = pitch
58 print 'x = %.3f y = %.3f w = %.3f h = %.3f' % (x, y, w, h) 59 print 'pageidx = %d x = %.3f y = %.3f w = %.3f h = %.3f' % (pageidx, x, y, w, h)
60 if len(pages) <= pageidx:
61 pages.extend(itertools.repeat(None, len(pages) - pageidx + 1))
62 if not pages[pageidx]:
63 svg = svgwrite.Drawing(outpat % (pageidx), size = ('%.3fcm' % (pagewidth), '%.3fcm' % (pageheight)))
64 pages[pageidx] = svg
65 else:
66 svg = pages[pageidx]
59 svg.add(svgwrite.shapes.Rect(insert = ('%.3fcm' % (x), '%.3fcm' % (y)), 67 svg.add(svgwrite.shapes.Rect(insert = ('%.3fcm' % (x), '%.3fcm' % (y)),
60 size = ('%.3fcm' % (w), '%.3fcm' % (h)), 68 size = ('%.3fcm' % (w), '%.3fcm' % (h)),
61 stroke = 'red', stroke_width = '1px', fill = 'red')) 69 stroke = 'red', stroke_width = '1px', fill = 'red'))
62 70
63 def midi2svg(inf, outpat, pitch, midi2note, note2midi, note2slot, pagewidth, pageheight): 71 def midi2svg(inf, outpat, midi2note, note2midi, note2slot, pagewidth, pageheight, pitch, offset, timescale):
64 playablecount = 0 72 playablecount = 0
65 unplayablecount = 0 73 unplayablecount = 0
66 midi = mido.MidiFile(inf) 74 midi = mido.MidiFile(inf)
67 svg = svgwrite.Drawing(outpat % (0), size = ('%.3fcm' % (pagewidth), '%.3fcm' % (pageheight))) 75 ctime = 0
68 tnum = 0 76 channels = []
77 for i in range(16):
78 channels.append({})
69 79
70 for t in midi.tracks: 80 pages = []
71 print "Track", tnum 81 for ev in midi:
72 tnum += 1 82 ctime += ev.time
73 channels = [] 83 #print ctime, ev
74 for i in range(16): 84 if ev.type == 'note_on' and ev.velocity > 0:
75 channels.append({}) 85 if ev.note in channels[ev.channel]:
76 ctime = 0 86 print 'Duplicate note_on message %d (%s)' % (ev.note, midi2note[ev.note])
77 for ev in t: 87 else:
78 ctime += ev.time 88 channels[ev.channel][ev.note] = ctime
79 print ctime, ev 89 elif ev.type == 'note_off' or (ev.type == 'note_on' and ev.velocity == 0):
80 if ev.type == 'note_on' and ev.velocity > 0: 90 if ev.note not in channels[ev.channel]:
81 if ev.note in channels[ev.channel]: 91 print 'note_off with no corresponding note_on for channel %d note %d' % (ev.channel, ev.note)
82 print "Duplicate channel on message"
83 else: 92 else:
84 channels[ev.channel][ev.note] = ctime 93 note = midi2note[ev.note]
85 elif ev.type == 'note_off' or (ev.type == 'note_on' and ev.velocity == 0): 94 if note not in note2slot:
86 if ev.note not in channels[ev.channel]: 95 print 'Skipping unplayable note %s' % (note)
87 print 'Note_off with no corresponding note_on for track %d channel %d note %d' % (tnum, ev.channel, ev.note) 96 unplayablecount += 1
88 else: 97 else:
89 note = midi2note[ev.note] 98 start = channels[ev.channel][ev.note]
90 if note not in note2slot: 99 notelen = ctime - start
91 print 'Skipping unplayable note %s' % (note) 100 slot = note2slot[note]
92 unplayablecount += 1 101 #print 'Note %s (%d) at %d length %d' % (note, slot, start, notelen)
93 else: 102 emitnote(pages,outpat, slot, start, notelen, pagewidth, pageheight, pitch, offset, timescale)
94 start = channels[ev.channel][ev.note] 103 playablecount += 1
95 notelen = ctime - start 104 del channels[ev.channel][ev.note]
96 slot = note2slot[note] 105 elif ev.type == 'end_of_track':
97 print 'Note %s (%d) at %d length %d' % (note, slot, start, notelen) 106 print 'EOT, not flushing, check for missed notes'
98 emitnote(svg, slot, start, notelen, pageheight) 107 for chan in channels:
99 print 108 for ev in chan:
100 playablecount += 1 109 print ev
101 del channels[ev.channel][ev.note] 110
102 elif ev.type == 'end_of_track': 111 for svg in pages:
103 print 'EOT, flushing' 112 if not svg:
104 for chan in channels: 113 asdasd
105 for ev in chan: 114 continue
106 print ev 115 svg.save()
107 print 116
108 print
109 print
110 svg.save()
111 print 'Playable count:', playablecount 117 print 'Playable count:', playablecount
112 print 'Unplayable count:', unplayablecount 118 print 'Unplayable count:', unplayablecount
113 119
114 if __name__ == '__main__': 120 if __name__ == '__main__':
115 main() 121 main()