Mercurial > ~darius > hgwebdir.cgi > musiccutter
annotate musiccutter.py @ 12:6e46ceee57a7
Use correct MIDI note generation, previous version did not agree with
MUP (hmmmm).
author | Daniel O'Connor <darius@dons.net.au> |
---|---|
date | Sun, 10 Apr 2016 22:24:56 +0930 |
parents | 9faad813e39e |
children | 5f4c21bb5140 |
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 |
12
6e46ceee57a7
Use correct MIDI note generation, previous version did not agree with
Daniel O'Connor <darius@dons.net.au>
parents:
11
diff
changeset
|
9 import reportlab.pdfgen.canvas |
11
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
10 from reportlab.lib.units import mm |
0 | 11 import sys |
12 | |
11
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
13 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
|
14 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
|
15 |
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
16 |
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
17 ## 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
|
18 def test(filename = None): |
49a33c431b45
Accept filename for test function
Daniel O'Connor <darius@dons.net.au>
parents:
2
diff
changeset
|
19 if filename == None: |
49a33c431b45
Accept filename for test function
Daniel O'Connor <darius@dons.net.au>
parents:
2
diff
changeset
|
20 filename = 'test.midi' |
7 | 21 # 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
|
22 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
|
23 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
|
24 m.processMidi(filename, base + '-%02d.pdf') |
0 | 25 |
11
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
26 class Midi2PDF(object): |
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
27 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
|
28 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
|
29 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
|
30 self.pagewidth = pagewidth # Dimensions are in millimetres |
7 | 31 self.pageheight = pageheight |
32 self.pitch = pitch | |
33 self.offset = offset | |
34 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
|
35 self.fontname = fontname |
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
36 self.fontsize = fontsize |
0 | 37 |
7 | 38 def processMidi(self, midifile, outpat): |
39 playablecount = 0 | |
40 unplayablecount = 0 | |
41 midi = mido.MidiFile(midifile) | |
42 ctime = 0 | |
43 channels = [] | |
44 for i in range(16): | |
45 channels.append({}) | |
0 | 46 |
11
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
47 npages = int(math.ceil(midi.length * self.timescale / self.pagewidth)) |
7 | 48 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
|
49 pdfs = [] |
7 | 50 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
|
51 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
|
52 pdfs.append(pdf) |
0 | 53 |
11
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
54 title = midifile |
7 | 55 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
|
56 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
|
57 title = ev.text |
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
58 |
7 | 59 ctime += ev.time |
60 if ev.type == 'note_on' or ev.type == 'note_off': | |
61 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
|
62 #print ctime, ev |
7 | 63 if ev.type == 'note_on' and ev.velocity > 0: |
64 if ev.note in channels[ev.channel]: | |
65 print 'Duplicate note_on message %d (%s)' % (ev.note, note) | |
0 | 66 else: |
7 | 67 channels[ev.channel][ev.note] = ctime |
68 elif ev.type == 'note_off' or (ev.type == 'note_on' and ev.velocity == 0): | |
69 if ev.note not in channels[ev.channel]: | |
70 print 'note_off with no corresponding note_on for channel %d note %d' % (ev.channel, ev.note) | |
0 | 71 else: |
7 | 72 if note not in self.note2slot: |
73 print 'Skipping unplayable note %s' % (note) | |
74 unplayablecount += 1 | |
75 else: | |
76 start = channels[ev.channel][ev.note] | |
77 notelen = ctime - start | |
78 slot = self.note2slot[note] | |
79 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
|
80 self.emitnote(pdfs, slot, start, notelen) |
7 | 81 playablecount += 1 |
82 del channels[ev.channel][ev.note] | |
83 elif ev.type == 'end_of_track': | |
84 print 'EOT, not flushing, check for missed notes' | |
85 for chan in channels: | |
86 for ev in chan: | |
87 print ev | |
88 | |
89 print 'Playable count:', playablecount | |
90 print 'Unplayable count:', unplayablecount | |
91 | |
11
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
92 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
|
93 pdf = pdfs[i] |
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
94 tobj = pdf.beginText() |
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
95 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
|
96 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
|
97 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
|
98 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
|
99 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
|
100 pdf.drawText(tobj) |
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
101 pdf.save() |
7 | 102 |
12
6e46ceee57a7
Use correct MIDI note generation, previous version did not agree with
Daniel O'Connor <darius@dons.net.au>
parents:
11
diff
changeset
|
103 # http://newt.phys.unsw.edu.au/jw/notes.html |
7 | 104 @staticmethod |
105 def genmidi2note(): | |
12
6e46ceee57a7
Use correct MIDI note generation, previous version did not agree with
Daniel O'Connor <darius@dons.net.au>
parents:
11
diff
changeset
|
106 '''Create forward & reverse tables for midi number to note name (assuming 69 = A4 = A440)''' |
7 | 107 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'] |
108 midi2note = {} | |
109 note2midi = {} | |
110 for midi in range(128): | |
12
6e46ceee57a7
Use correct MIDI note generation, previous version did not agree with
Daniel O'Connor <darius@dons.net.au>
parents:
11
diff
changeset
|
111 octave = midi / len(names) - 1 |
7 | 112 index = midi % len(names) |
113 name = names[index] % (octave) | |
114 midi2note[midi] = name | |
115 note2midi[name] = midi | |
4
9f4fa5f231e6
Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents:
3
diff
changeset
|
116 |
7 | 117 return midi2note, note2midi |
118 | |
119 @staticmethod | |
120 def loadnote2slot(fname, note2midi): | |
121 note2slot = {} | |
122 index = 0 | |
123 | |
124 for note in file(fname): | |
125 note = note.strip() | |
126 if note[0] == '#': | |
127 continue | |
128 if note not in note2midi: | |
129 raise exceptions.ValueError('Note \'%s\' not valid' % note) | |
130 note2slot[note] = index | |
131 index += 1 | |
132 | |
133 return note2slot | |
4
9f4fa5f231e6
Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents:
3
diff
changeset
|
134 |
11
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
135 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
|
136 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
|
137 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
|
138 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
|
139 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
|
140 w = notelen * self.timescale |
7 | 141 h = self.pitch |
142 | |
11
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
143 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
|
144 w1 = w |
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
145 # 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
|
146 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
|
147 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
|
148 w2 = w - w1 |
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
149 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
|
150 # 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
|
151 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
|
152 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
|
153 |
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
154 Midi2PDF._emitnote(pdfs[pageidx], x, y, w1, h) |
7 | 155 |
156 @staticmethod | |
11
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
157 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
|
158 pdf.saveState() |
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
159 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
|
160 pdf.setLineWidth(0) |
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
161 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
|
162 pdf.restoreState() |
0 | 163 |
164 if __name__ == '__main__': | |
165 main() |