Mercurial > ~darius > hgwebdir.cgi > musiccutter
annotate musiccutter.py @ 11:9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
author | Daniel O'Connor <darius@dons.net.au> |
---|---|
date | Thu, 07 Apr 2016 23:35:44 +0930 |
parents | e9760224e992 |
children | 6e46ceee57a7 |
rev | line source |
---|---|
0 | 1 #!/usr/bin/env python |
2 | |
3 import exceptions | |
4
9f4fa5f231e6
Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents:
3
diff
changeset
|
4 import itertools |
7 | 5 import math |
0 | 6 import mido |
11
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
7 import os |
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
8 import reportlab.lib.colors |
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
9 from reportlab.lib.units import mm |
0 | 10 import sys |
11 | |
11
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
12 CUT_COLOUR = reportlab.lib.colors.red |
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
13 ENGRAVE_COLOUR = reportlab.lib.colors.black |
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
14 |
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
15 |
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
16 ## XXX: PDF origin bottom left, SVG origin top left |
3
49a33c431b45
Accept filename for test function
Daniel O'Connor <darius@dons.net.au>
parents:
2
diff
changeset
|
17 def test(filename = None): |
49a33c431b45
Accept filename for test function
Daniel O'Connor <darius@dons.net.au>
parents:
2
diff
changeset
|
18 if filename == None: |
49a33c431b45
Accept filename for test function
Daniel O'Connor <darius@dons.net.au>
parents:
2
diff
changeset
|
19 filename = 'test.midi' |
7 | 20 # Card layout from http://www.orgues-de-barbarie.com/wp-content/uploads/2014/09/format-cartons.pdf |
11
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
21 m = Midi2PDF('notes', 200, 155, 5.5, 6.0, 10, 'Helvetica', 12) |
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
22 base, ext = os.path.splitext(filename) |
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
23 m.processMidi(filename, base + '-%02d.pdf') |
0 | 24 |
11
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
25 class Midi2PDF(object): |
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
26 def __init__(self, notefile, pagewidth, pageheight, pitch, offset, timescale, fontname, fontsize): |
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
27 self.midi2note, self.note2midi = Midi2PDF.genmidi2note() |
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
28 self.note2slot = Midi2PDF.loadnote2slot(notefile, self.note2midi) |
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
29 self.pagewidth = pagewidth # Dimensions are in millimetres |
7 | 30 self.pageheight = pageheight |
31 self.pitch = pitch | |
32 self.offset = offset | |
33 self.timescale = timescale | |
11
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
34 self.fontname = fontname |
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
35 self.fontsize = fontsize |
0 | 36 |
7 | 37 def processMidi(self, midifile, outpat): |
38 playablecount = 0 | |
39 unplayablecount = 0 | |
40 midi = mido.MidiFile(midifile) | |
41 ctime = 0 | |
42 channels = [] | |
43 for i in range(16): | |
44 channels.append({}) | |
0 | 45 |
11
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
46 npages = int(math.ceil(midi.length * self.timescale / self.pagewidth)) |
7 | 47 print 'npages', npages |
11
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
48 pdfs = [] |
7 | 49 for i in range(npages): |
11
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
50 pdf = reportlab.pdfgen.canvas.Canvas(file(outpat % (i + 1), 'w'), pagesize = (self.pagewidth * mm, self.pageheight * mm)) |
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
51 pdfs.append(pdf) |
0 | 52 |
11
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
53 title = midifile |
7 | 54 for ev in midi: |
11
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
55 if ev.type == 'text' and ctime == 0: |
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
56 title = ev.text |
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
57 |
7 | 58 ctime += ev.time |
59 if ev.type == 'note_on' or ev.type == 'note_off': | |
60 note = self.midi2note[ev.note] | |
11
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
61 #print ctime, ev |
7 | 62 if ev.type == 'note_on' and ev.velocity > 0: |
63 if ev.note in channels[ev.channel]: | |
64 print 'Duplicate note_on message %d (%s)' % (ev.note, note) | |
0 | 65 else: |
7 | 66 channels[ev.channel][ev.note] = ctime |
67 elif ev.type == 'note_off' or (ev.type == 'note_on' and ev.velocity == 0): | |
68 if ev.note not in channels[ev.channel]: | |
69 print 'note_off with no corresponding note_on for channel %d note %d' % (ev.channel, ev.note) | |
0 | 70 else: |
7 | 71 if note not in self.note2slot: |
72 print 'Skipping unplayable note %s' % (note) | |
73 unplayablecount += 1 | |
74 else: | |
75 start = channels[ev.channel][ev.note] | |
76 notelen = ctime - start | |
77 slot = self.note2slot[note] | |
78 print 'Note %s (%d) at %.2f length %.2f' % (note, slot, start, notelen) | |
11
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
79 self.emitnote(pdfs, slot, start, notelen) |
7 | 80 playablecount += 1 |
81 del channels[ev.channel][ev.note] | |
82 elif ev.type == 'end_of_track': | |
83 print 'EOT, not flushing, check for missed notes' | |
84 for chan in channels: | |
85 for ev in chan: | |
86 print ev | |
87 | |
88 print 'Playable count:', playablecount | |
89 print 'Unplayable count:', unplayablecount | |
90 | |
11
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
91 for i in range(len(pdfs)): |
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
92 pdf = pdfs[i] |
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
93 tobj = pdf.beginText() |
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
94 tobj.setTextOrigin(2 * mm, 1 * mm) |
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
95 tobj.setFont(self.fontname, self.fontsize) |
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
96 tobj.setFillColor(ENGRAVE_COLOUR) |
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
97 tobj.setStrokeColor(ENGRAVE_COLOUR) |
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
98 tobj.textLine('%s (%d / %d)' % (title, i + 1, npages)) |
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
99 pdf.drawText(tobj) |
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
100 pdf.save() |
7 | 101 |
102 # http://www.electronics.dit.ie/staff/tscarff/Music_technology/midi/midi_note_numbers_for_octaves.htm | |
103 @staticmethod | |
104 def genmidi2note(): | |
105 '''Create forward & reverse tables for midi number to note name (assuming 69 == A440)''' | |
106 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'] | |
107 midi2note = {} | |
108 note2midi = {} | |
109 for midi in range(128): | |
110 octave = midi / len(names) | |
111 index = midi % len(names) | |
112 name = names[index] % (octave) | |
113 midi2note[midi] = name | |
114 note2midi[name] = midi | |
4
9f4fa5f231e6
Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents:
3
diff
changeset
|
115 |
7 | 116 return midi2note, note2midi |
117 | |
118 @staticmethod | |
119 def loadnote2slot(fname, note2midi): | |
120 note2slot = {} | |
121 index = 0 | |
122 | |
123 for note in file(fname): | |
124 note = note.strip() | |
125 if note[0] == '#': | |
126 continue | |
127 if note not in note2midi: | |
128 raise exceptions.ValueError('Note \'%s\' not valid' % note) | |
129 note2slot[note] = index | |
130 index += 1 | |
131 | |
132 return note2slot | |
4
9f4fa5f231e6
Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents:
3
diff
changeset
|
133 |
11
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
134 def emitnote(self, pdfs, slot, start, notelen): |
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
135 x = start * self.timescale |
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
136 pageidx = int(x / self.pagewidth) |
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
137 x = x % self.pagewidth |
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
138 y = self.offset + slot * self.pitch |
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
139 w = notelen * self.timescale |
7 | 140 h = self.pitch |
141 | |
11
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
142 print 'page = %d x = %.3f y = %.3f w = %.3f h = %.3f' % (pageidx, x, y, w, h) |
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
143 w1 = w |
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
144 # Check if the note crosses a page |
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
145 if x + w > self.pagewidth: |
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
146 w1 = x - self.pagewidth # Crop first note |
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
147 w2 = w - w1 |
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
148 assert w1 <= self.pagewidth, 'note extends for more than a page' |
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
149 # Emit second half of note |
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
150 print 'split note, page %d w2 = %.3f' % (pageidx + 1, w2) |
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
151 Midi2PDF._emitnote(pdfs[pageidx + 1], 0, y, w2, h) |
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
152 |
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
153 Midi2PDF._emitnote(pdfs[pageidx], x, y, w1, h) |
7 | 154 |
155 @staticmethod | |
11
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
156 def _emitnote(pdf, x, y, w, h): |
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
157 pdf.saveState() |
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
158 pdf.setStrokeColor(CUT_COLOUR) |
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
159 pdf.setLineWidth(0) |
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
160 pdf.rect(x * mm, y * mm, w * mm, h * mm, fill = False, stroke = True) |
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
161 pdf.restoreState() |
0 | 162 |
163 if __name__ == '__main__': | |
164 main() |