Mercurial > ~darius > hgwebdir.cgi > musiccutter
comparison musiccutter.py @ 5:af683606184e
Paginate with a view port, a lot slower and wastes space but handling
notes between pages is trivial.
author | Daniel O'Connor <darius@dons.net.au> |
---|---|
date | Tue, 08 Mar 2016 00:43:30 +1030 |
parents | 9f4fa5f231e6 |
children | 31db42ce72b8 |
comparison
equal
deleted
inserted
replaced
4:9f4fa5f231e6 | 5:af683606184e |
---|---|
47 note2slot[note] = index | 47 note2slot[note] = index |
48 index += 1 | 48 index += 1 |
49 | 49 |
50 return note2slot | 50 return note2slot |
51 | 51 |
52 def emitnote(pages, outpat, slot, start, notelen, pagewidth, pageheight, pitch, offset, timescale): | 52 def emitnote(svg, slot, start, notelen, pagewidth, pageheight, pitch, offset, timescale): |
53 x = start / timescale | 53 x = start / timescale |
54 pageidx = int(x / pagewidth) | |
55 x = x % pagewidth | |
56 y = pageheight - (offset + slot * pitch) | 54 y = pageheight - (offset + slot * pitch) |
57 w = notelen / timescale | 55 w = notelen / timescale |
58 h = pitch | 56 h = pitch |
59 print 'pageidx = %d x = %.3f y = %.3f w = %.3f h = %.3f' % (pageidx, x, y, w, h) | 57 print 'x = %.3f y = %.3f w = %.3f h = %.3f' % (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] | |
67 svg.add(svgwrite.shapes.Rect(insert = ('%.3fcm' % (x), '%.3fcm' % (y)), | 58 svg.add(svgwrite.shapes.Rect(insert = ('%.3fcm' % (x), '%.3fcm' % (y)), |
68 size = ('%.3fcm' % (w), '%.3fcm' % (h)), | 59 size = ('%.3fcm' % (w), '%.3fcm' % (h)), |
69 stroke = 'red', stroke_width = '1px', fill = 'red')) | 60 stroke = 'red', stroke_width = '1px', fill = 'red')) |
70 | 61 |
71 def midi2svg(inf, outpat, midi2note, note2midi, note2slot, pagewidth, pageheight, pitch, offset, timescale): | 62 def midi2svg(inf, outpat, midi2note, note2midi, note2slot, pagewidth, pageheight, pitch, offset, timescale): |
72 playablecount = 0 | 63 playablecount = 0 |
73 unplayablecount = 0 | 64 unplayablecount = 0 |
74 midi = mido.MidiFile(inf) | 65 midi = mido.MidiFile(inf) |
75 ctime = 0 | 66 ctime = 0 |
76 channels = [] | 67 channels = [] |
68 svg = svgwrite.Drawing(outpat % (0), size = ('%.3fcm' % (pagewidth), '%.3fcm' % (pageheight))) | |
77 for i in range(16): | 69 for i in range(16): |
78 channels.append({}) | 70 channels.append({}) |
79 | 71 |
80 pages = [] | 72 pages = [] |
81 for ev in midi: | 73 for ev in midi: |
97 else: | 89 else: |
98 start = channels[ev.channel][ev.note] | 90 start = channels[ev.channel][ev.note] |
99 notelen = ctime - start | 91 notelen = ctime - start |
100 slot = note2slot[note] | 92 slot = note2slot[note] |
101 #print 'Note %s (%d) at %d length %d' % (note, slot, start, notelen) | 93 #print 'Note %s (%d) at %d length %d' % (note, slot, start, notelen) |
102 emitnote(pages,outpat, slot, start, notelen, pagewidth, pageheight, pitch, offset, timescale) | 94 emitnote(svg, slot, start, notelen, pagewidth, pageheight, pitch, offset, timescale) |
103 playablecount += 1 | 95 playablecount += 1 |
104 del channels[ev.channel][ev.note] | 96 del channels[ev.channel][ev.note] |
105 elif ev.type == 'end_of_track': | 97 elif ev.type == 'end_of_track': |
106 print 'EOT, not flushing, check for missed notes' | 98 print 'EOT, not flushing, check for missed notes' |
107 for chan in channels: | 99 for chan in channels: |
108 for ev in chan: | 100 for ev in chan: |
109 print ev | 101 print ev |
110 | 102 |
111 for svg in pages: | 103 npages = int(midi.length / timescale / pagewidth + 0.5) |
112 if not svg: | 104 print 'npages', npages |
113 asdasd | 105 for i in range(npages): |
114 continue | 106 svg.viewbox(pagewidth / 2.54 * 96.0 * i, 0, pagewidth / 2.54 * 96.0, pageheight / 2.54 * 96.0) |
115 svg.save() | 107 svg.saveas(outpat % i) |
116 | 108 |
117 print 'Playable count:', playablecount | 109 print 'Playable count:', playablecount |
118 print 'Unplayable count:', unplayablecount | 110 print 'Unplayable count:', unplayablecount |
119 | 111 |
120 if __name__ == '__main__': | 112 if __name__ == '__main__': |