Mercurial > ~darius > hgwebdir.cgi > musiccutter
annotate musiccutter.py @ 29:767ba8ec90e6
Don't crap out if the input midi note is out of range
author | Daniel O'Connor <darius@dons.net.au> |
---|---|
date | Tue, 03 May 2016 09:09:27 +0930 |
parents | 657bc32a0dfd |
children | f46cc9401e79 |
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 |
22
65e8298f5800
Remove extension and path name from title.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
7 import os.path |
11
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 |
3
49a33c431b45
Accept filename for test function
Daniel O'Connor <darius@dons.net.au>
parents:
2
diff
changeset
|
16 def test(filename = None): |
49a33c431b45
Accept filename for test function
Daniel O'Connor <darius@dons.net.au>
parents:
2
diff
changeset
|
17 if filename == None: |
49a33c431b45
Accept filename for test function
Daniel O'Connor <darius@dons.net.au>
parents:
2
diff
changeset
|
18 filename = 'test.midi' |
7 | 19 # Card layout from http://www.orgues-de-barbarie.com/wp-content/uploads/2014/09/format-cartons.pdf |
19
ffaf97818ce3
- Music needs to run right to left.
Daniel O'Connor <darius@dons.net.au>
parents:
18
diff
changeset
|
20 # Notes are read from right to left |
24
71b78f06ff03
Highest notes are at the bottom not the top.
Daniel O'Connor <darius@dons.net.au>
parents:
23
diff
changeset
|
21 # Highest note is at the bottom (closest to the crank) |
71b78f06ff03
Highest notes are at the bottom not the top.
Daniel O'Connor <darius@dons.net.au>
parents:
23
diff
changeset
|
22 m = Midi2PDF('notes', 120, 155, 5.5, 3.3, 6.0, 50, False, False, False, False, 0, 10, 'Helvetica', 12) |
11
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) |
22
65e8298f5800
Remove extension and path name from title.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
24 base = os.path.basename(base) |
11
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
25 m.processMidi(filename, base + '-%02d.pdf') |
0 | 26 |
11
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
27 class Midi2PDF(object): |
24
71b78f06ff03
Highest notes are at the bottom not the top.
Daniel O'Connor <darius@dons.net.au>
parents:
23
diff
changeset
|
28 def __init__(self, notefile, pagewidth, pageheight, pitch, slotsize, heel, leadin, timemarks, drawrect, notenames, notelines, noteoffset, timescale, fontname, fontsize): |
27
87cf66e04ef9
Handle offsetting note pitch in a better way.
Daniel O'Connor <darius@dons.net.au>
parents:
26
diff
changeset
|
29 self.midi2note, self.note2midi = Midi2PDF.genmidi2note() |
13
5f4c21bb5140
Add lines and notes engraved on the paper.
Daniel O'Connor <darius@dons.net.au>
parents:
12
diff
changeset
|
30 self.note2slot, self.slot2note = Midi2PDF.loadnote2slot(notefile, self.note2midi) |
11
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
31 self.pagewidth = pagewidth # Dimensions are in millimetres |
7 | 32 self.pageheight = pageheight |
13
5f4c21bb5140
Add lines and notes engraved on the paper.
Daniel O'Connor <darius@dons.net.au>
parents:
12
diff
changeset
|
33 self.pitch = pitch # Distance between each slot |
16
20337b22977d
Add left margin and slot size support.
Daniel O'Connor <darius@dons.net.au>
parents:
14
diff
changeset
|
34 self.slotsize = slotsize # Size of each slot cut out |
24
71b78f06ff03
Highest notes are at the bottom not the top.
Daniel O'Connor <darius@dons.net.au>
parents:
23
diff
changeset
|
35 self.heel = heel # Bottom margin (from bottom of page to centre of slot) |
19
ffaf97818ce3
- Music needs to run right to left.
Daniel O'Connor <darius@dons.net.au>
parents:
18
diff
changeset
|
36 self.leadin = leadin # Extra at the start |
ffaf97818ce3
- Music needs to run right to left.
Daniel O'Connor <darius@dons.net.au>
parents:
18
diff
changeset
|
37 self.timemarks = timemarks # Draw vertical time lines |
ffaf97818ce3
- Music needs to run right to left.
Daniel O'Connor <darius@dons.net.au>
parents:
18
diff
changeset
|
38 self.drawrect = drawrect # Draw rectangle around each page |
20
9bb72ae63651
- Make various things optional.
Daniel O'Connor <darius@dons.net.au>
parents:
19
diff
changeset
|
39 self.notenames = notenames # Draw note names on the right edge |
9bb72ae63651
- Make various things optional.
Daniel O'Connor <darius@dons.net.au>
parents:
19
diff
changeset
|
40 self.notelines = notelines # Draw line rulers |
27
87cf66e04ef9
Handle offsetting note pitch in a better way.
Daniel O'Connor <darius@dons.net.au>
parents:
26
diff
changeset
|
41 self.noteoffset = noteoffset # Amount to adjust note pitches by (+12 = 1 octave) |
16
20337b22977d
Add left margin and slot size support.
Daniel O'Connor <darius@dons.net.au>
parents:
14
diff
changeset
|
42 self.timescale = timescale # Width per second |
11
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
43 self.fontname = fontname |
16
20337b22977d
Add left margin and slot size support.
Daniel O'Connor <darius@dons.net.au>
parents:
14
diff
changeset
|
44 self.fontsize = fontsize # Points |
0 | 45 |
7 | 46 def processMidi(self, midifile, outpat): |
47 playablecount = 0 | |
48 unplayablecount = 0 | |
28
657bc32a0dfd
Try transposing unplayable notes down or up an octave (in that order).
Daniel O'Connor <darius@dons.net.au>
parents:
27
diff
changeset
|
49 transposeupcount = 0 |
657bc32a0dfd
Try transposing unplayable notes down or up an octave (in that order).
Daniel O'Connor <darius@dons.net.au>
parents:
27
diff
changeset
|
50 transposedowncount = 0 |
7 | 51 midi = mido.MidiFile(midifile) |
52 ctime = 0 | |
53 channels = [] | |
54 for i in range(16): | |
55 channels.append({}) | |
0 | 56 |
19
ffaf97818ce3
- Music needs to run right to left.
Daniel O'Connor <darius@dons.net.au>
parents:
18
diff
changeset
|
57 npages = int(math.ceil(((midi.length * self.timescale) + self.leadin) / self.pagewidth)) |
7 | 58 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
|
59 pdfs = [] |
7 | 60 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
|
61 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
|
62 pdfs.append(pdf) |
0 | 63 |
22
65e8298f5800
Remove extension and path name from title.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
64 title = os.path.basename(midifile) |
65e8298f5800
Remove extension and path name from title.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
65 title, ext = os.path.splitext(title) |
7 | 66 for ev in midi: |
27
87cf66e04ef9
Handle offsetting note pitch in a better way.
Daniel O'Connor <darius@dons.net.au>
parents:
26
diff
changeset
|
67 # Adjust pitch of note (if it's a note) |
87cf66e04ef9
Handle offsetting note pitch in a better way.
Daniel O'Connor <darius@dons.net.au>
parents:
26
diff
changeset
|
68 if hasattr(ev, 'note'): |
87cf66e04ef9
Handle offsetting note pitch in a better way.
Daniel O'Connor <darius@dons.net.au>
parents:
26
diff
changeset
|
69 ev.note += self.noteoffset |
11
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
70 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
|
71 title = ev.text |
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
72 |
7 | 73 ctime += ev.time |
74 if ev.type == 'note_on' or ev.type == 'note_off': | |
29
767ba8ec90e6
Don't crap out if the input midi note is out of range
Daniel O'Connor <darius@dons.net.au>
parents:
28
diff
changeset
|
75 if ev.note not in self.midi2note: |
767ba8ec90e6
Don't crap out if the input midi note is out of range
Daniel O'Connor <darius@dons.net.au>
parents:
28
diff
changeset
|
76 print 'Input MIDI number %d out of range' % (ev.note) |
767ba8ec90e6
Don't crap out if the input midi note is out of range
Daniel O'Connor <darius@dons.net.au>
parents:
28
diff
changeset
|
77 unplayablecount += 1 |
767ba8ec90e6
Don't crap out if the input midi note is out of range
Daniel O'Connor <darius@dons.net.au>
parents:
28
diff
changeset
|
78 continue |
767ba8ec90e6
Don't crap out if the input midi note is out of range
Daniel O'Connor <darius@dons.net.au>
parents:
28
diff
changeset
|
79 else: |
767ba8ec90e6
Don't crap out if the input midi note is out of range
Daniel O'Connor <darius@dons.net.au>
parents:
28
diff
changeset
|
80 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
|
81 #print ctime, ev |
7 | 82 if ev.type == 'note_on' and ev.velocity > 0: |
83 if ev.note in channels[ev.channel]: | |
84 print 'Duplicate note_on message %d (%s)' % (ev.note, note) | |
0 | 85 else: |
7 | 86 channels[ev.channel][ev.note] = ctime |
87 elif ev.type == 'note_off' or (ev.type == 'note_on' and ev.velocity == 0): | |
88 if ev.note not in channels[ev.channel]: | |
89 print 'note_off with no corresponding note_on for channel %d note %d' % (ev.channel, ev.note) | |
0 | 90 else: |
28
657bc32a0dfd
Try transposing unplayable notes down or up an octave (in that order).
Daniel O'Connor <darius@dons.net.au>
parents:
27
diff
changeset
|
91 start = channels[ev.channel][ev.note] |
657bc32a0dfd
Try transposing unplayable notes down or up an octave (in that order).
Daniel O'Connor <darius@dons.net.au>
parents:
27
diff
changeset
|
92 notelen = ctime - start |
657bc32a0dfd
Try transposing unplayable notes down or up an octave (in that order).
Daniel O'Connor <darius@dons.net.au>
parents:
27
diff
changeset
|
93 playable = True |
657bc32a0dfd
Try transposing unplayable notes down or up an octave (in that order).
Daniel O'Connor <darius@dons.net.au>
parents:
27
diff
changeset
|
94 |
657bc32a0dfd
Try transposing unplayable notes down or up an octave (in that order).
Daniel O'Connor <darius@dons.net.au>
parents:
27
diff
changeset
|
95 if note in self.note2slot: |
657bc32a0dfd
Try transposing unplayable notes down or up an octave (in that order).
Daniel O'Connor <darius@dons.net.au>
parents:
27
diff
changeset
|
96 slot = self.note2slot[note] |
657bc32a0dfd
Try transposing unplayable notes down or up an octave (in that order).
Daniel O'Connor <darius@dons.net.au>
parents:
27
diff
changeset
|
97 elif ev.note - 12 in self.midi2note and self.note2slot[self.midi2note[ev.note - 12]]: |
657bc32a0dfd
Try transposing unplayable notes down or up an octave (in that order).
Daniel O'Connor <darius@dons.net.au>
parents:
27
diff
changeset
|
98 print 'Transposing note %d (%s) down' % (ev.note, note) |
657bc32a0dfd
Try transposing unplayable notes down or up an octave (in that order).
Daniel O'Connor <darius@dons.net.au>
parents:
27
diff
changeset
|
99 slot = self.note2slot[self.midi2note[ev.note - 12]] |
657bc32a0dfd
Try transposing unplayable notes down or up an octave (in that order).
Daniel O'Connor <darius@dons.net.au>
parents:
27
diff
changeset
|
100 transposedowncount += 1 |
657bc32a0dfd
Try transposing unplayable notes down or up an octave (in that order).
Daniel O'Connor <darius@dons.net.au>
parents:
27
diff
changeset
|
101 elif ev.note + 12 in self.midi2note and self.note2slot[self.midi2note[ev.note + 12]]: |
657bc32a0dfd
Try transposing unplayable notes down or up an octave (in that order).
Daniel O'Connor <darius@dons.net.au>
parents:
27
diff
changeset
|
102 print 'Transposing note %d (%s) up' % (ev.note, note) |
657bc32a0dfd
Try transposing unplayable notes down or up an octave (in that order).
Daniel O'Connor <darius@dons.net.au>
parents:
27
diff
changeset
|
103 slot = self.note2slot[self.midi2note[ev.note + 12]] |
657bc32a0dfd
Try transposing unplayable notes down or up an octave (in that order).
Daniel O'Connor <darius@dons.net.au>
parents:
27
diff
changeset
|
104 transposeupcount += 1 |
7 | 105 else: |
28
657bc32a0dfd
Try transposing unplayable notes down or up an octave (in that order).
Daniel O'Connor <darius@dons.net.au>
parents:
27
diff
changeset
|
106 unplayablecount += 1 |
657bc32a0dfd
Try transposing unplayable notes down or up an octave (in that order).
Daniel O'Connor <darius@dons.net.au>
parents:
27
diff
changeset
|
107 playable = False |
657bc32a0dfd
Try transposing unplayable notes down or up an octave (in that order).
Daniel O'Connor <darius@dons.net.au>
parents:
27
diff
changeset
|
108 |
657bc32a0dfd
Try transposing unplayable notes down or up an octave (in that order).
Daniel O'Connor <darius@dons.net.au>
parents:
27
diff
changeset
|
109 if playable: |
657bc32a0dfd
Try transposing unplayable notes down or up an octave (in that order).
Daniel O'Connor <darius@dons.net.au>
parents:
27
diff
changeset
|
110 #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
|
111 self.emitnote(pdfs, slot, start, notelen) |
7 | 112 playablecount += 1 |
28
657bc32a0dfd
Try transposing unplayable notes down or up an octave (in that order).
Daniel O'Connor <darius@dons.net.au>
parents:
27
diff
changeset
|
113 |
7 | 114 del channels[ev.channel][ev.note] |
115 elif ev.type == 'end_of_track': | |
116 print 'EOT, not flushing, check for missed notes' | |
117 for chan in channels: | |
118 for ev in chan: | |
119 print ev | |
120 | |
121 print 'Playable count:', playablecount | |
122 print 'Unplayable count:', unplayablecount | |
28
657bc32a0dfd
Try transposing unplayable notes down or up an octave (in that order).
Daniel O'Connor <darius@dons.net.au>
parents:
27
diff
changeset
|
123 print 'Transpose down:', transposedowncount |
657bc32a0dfd
Try transposing unplayable notes down or up an octave (in that order).
Daniel O'Connor <darius@dons.net.au>
parents:
27
diff
changeset
|
124 print 'Transpose up:', transposeupcount |
7 | 125 |
17
c50427f1da2d
- Draw vertical time lines.
Daniel O'Connor <darius@dons.net.au>
parents:
16
diff
changeset
|
126 for pindx in range(len(pdfs)): |
c50427f1da2d
- Draw vertical time lines.
Daniel O'Connor <darius@dons.net.au>
parents:
16
diff
changeset
|
127 pdf = pdfs[pindx] |
13
5f4c21bb5140
Add lines and notes engraved on the paper.
Daniel O'Connor <darius@dons.net.au>
parents:
12
diff
changeset
|
128 # Add title and page number |
19
ffaf97818ce3
- Music needs to run right to left.
Daniel O'Connor <darius@dons.net.au>
parents:
18
diff
changeset
|
129 Midi2PDF.textHelper(pdf, 0 * mm, 1 * mm, ENGRAVE_COLOUR, True, self.fontname, self.fontsize, '%s (%d / %d)' % (title, pindx + 1, npages)) |
13
5f4c21bb5140
Add lines and notes engraved on the paper.
Daniel O'Connor <darius@dons.net.au>
parents:
12
diff
changeset
|
130 |
5f4c21bb5140
Add lines and notes engraved on the paper.
Daniel O'Connor <darius@dons.net.au>
parents:
12
diff
changeset
|
131 pdf.setLineWidth(0) |
17
c50427f1da2d
- Draw vertical time lines.
Daniel O'Connor <darius@dons.net.au>
parents:
16
diff
changeset
|
132 |
18
cf93a76eda92
The laser cutter software barfs on these lines and stops processing
Daniel O'Connor <darius@dons.net.au>
parents:
17
diff
changeset
|
133 # Draw time marks |
19
ffaf97818ce3
- Music needs to run right to left.
Daniel O'Connor <darius@dons.net.au>
parents:
18
diff
changeset
|
134 if self.timemarks: |
ffaf97818ce3
- Music needs to run right to left.
Daniel O'Connor <darius@dons.net.au>
parents:
18
diff
changeset
|
135 tstart = self.leadin / self.timescale |
ffaf97818ce3
- Music needs to run right to left.
Daniel O'Connor <darius@dons.net.au>
parents:
18
diff
changeset
|
136 tend = self.pagewidth / self.timescale |
18
cf93a76eda92
The laser cutter software barfs on these lines and stops processing
Daniel O'Connor <darius@dons.net.au>
parents:
17
diff
changeset
|
137 if pindx > 0: |
cf93a76eda92
The laser cutter software barfs on these lines and stops processing
Daniel O'Connor <darius@dons.net.au>
parents:
17
diff
changeset
|
138 tsize = self.pagewidth / self.timescale |
cf93a76eda92
The laser cutter software barfs on these lines and stops processing
Daniel O'Connor <darius@dons.net.au>
parents:
17
diff
changeset
|
139 tstart = tend + tsize * pindx |
cf93a76eda92
The laser cutter software barfs on these lines and stops processing
Daniel O'Connor <darius@dons.net.au>
parents:
17
diff
changeset
|
140 tend = tend + tsize * (pindx + 1) |
cf93a76eda92
The laser cutter software barfs on these lines and stops processing
Daniel O'Connor <darius@dons.net.au>
parents:
17
diff
changeset
|
141 for s in range(tstart, tend, 5): |
19
ffaf97818ce3
- Music needs to run right to left.
Daniel O'Connor <darius@dons.net.au>
parents:
18
diff
changeset
|
142 x = self.pagewidth - (float(s * self.timescale + self.leadin) % self.pagewidth) |
24
71b78f06ff03
Highest notes are at the bottom not the top.
Daniel O'Connor <darius@dons.net.au>
parents:
23
diff
changeset
|
143 pdf.line(x * mm, self.heel, x * mm, self.pageheight * mm) |
18
cf93a76eda92
The laser cutter software barfs on these lines and stops processing
Daniel O'Connor <darius@dons.net.au>
parents:
17
diff
changeset
|
144 Midi2PDF.textHelper(pdf, x * mm, 1 * mm, ENGRAVE_COLOUR, False, self.fontname, self.fontsize, str(s) + 's') |
cf93a76eda92
The laser cutter software barfs on these lines and stops processing
Daniel O'Connor <darius@dons.net.au>
parents:
17
diff
changeset
|
145 |
25 | 146 # Draw rectangle around page (upper and right hand ends don't seem to render though) |
19
ffaf97818ce3
- Music needs to run right to left.
Daniel O'Connor <darius@dons.net.au>
parents:
18
diff
changeset
|
147 if self.drawrect: |
18
cf93a76eda92
The laser cutter software barfs on these lines and stops processing
Daniel O'Connor <darius@dons.net.au>
parents:
17
diff
changeset
|
148 pdf.rect(0, 0, self.pagewidth * mm, self.pageheight * mm, fill = False, stroke = True) |
cf93a76eda92
The laser cutter software barfs on these lines and stops processing
Daniel O'Connor <darius@dons.net.au>
parents:
17
diff
changeset
|
149 |
13
5f4c21bb5140
Add lines and notes engraved on the paper.
Daniel O'Connor <darius@dons.net.au>
parents:
12
diff
changeset
|
150 # Draw lines per note |
5f4c21bb5140
Add lines and notes engraved on the paper.
Daniel O'Connor <darius@dons.net.au>
parents:
12
diff
changeset
|
151 for slot in sorted(self.slot2note.keys()): |
26
f492b70f5e49
Fix notename/line drawing.
Daniel O'Connor <darius@dons.net.au>
parents:
25
diff
changeset
|
152 ofs = self.pageheight - (self.heel + slot * self.pitch) - self.slotsize / 2 |
20
9bb72ae63651
- Make various things optional.
Daniel O'Connor <darius@dons.net.au>
parents:
19
diff
changeset
|
153 if self.notelines: |
26
f492b70f5e49
Fix notename/line drawing.
Daniel O'Connor <darius@dons.net.au>
parents:
25
diff
changeset
|
154 pdf.line(0, ofs * mm, self.pagewidth * mm, ofs * mm) |
13
5f4c21bb5140
Add lines and notes engraved on the paper.
Daniel O'Connor <darius@dons.net.au>
parents:
12
diff
changeset
|
155 # Note name |
20
9bb72ae63651
- Make various things optional.
Daniel O'Connor <darius@dons.net.au>
parents:
19
diff
changeset
|
156 if self.notenames: |
26
f492b70f5e49
Fix notename/line drawing.
Daniel O'Connor <darius@dons.net.au>
parents:
25
diff
changeset
|
157 Midi2PDF.textHelper(pdf, (self.pagewidth - 10) * mm, (ofs + 0.5) * mm, ENGRAVE_COLOUR, False, self.fontname, self.fontsize, self.slot2note[slot]) |
25 | 158 |
26
f492b70f5e49
Fix notename/line drawing.
Daniel O'Connor <darius@dons.net.au>
parents:
25
diff
changeset
|
159 # Save PDF |
f492b70f5e49
Fix notename/line drawing.
Daniel O'Connor <darius@dons.net.au>
parents:
25
diff
changeset
|
160 pdf.save() |
7 | 161 |
12
6e46ceee57a7
Use correct MIDI note generation, previous version did not agree with
Daniel O'Connor <darius@dons.net.au>
parents:
11
diff
changeset
|
162 # http://newt.phys.unsw.edu.au/jw/notes.html |
7 | 163 @staticmethod |
27
87cf66e04ef9
Handle offsetting note pitch in a better way.
Daniel O'Connor <darius@dons.net.au>
parents:
26
diff
changeset
|
164 def genmidi2note(): |
87cf66e04ef9
Handle offsetting note pitch in a better way.
Daniel O'Connor <darius@dons.net.au>
parents:
26
diff
changeset
|
165 '''Create forward & reverse tables for midi number to note name (assuming 69 = A4 = A440)''' |
7 | 166 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'] |
167 midi2note = {} | |
168 note2midi = {} | |
13
5f4c21bb5140
Add lines and notes engraved on the paper.
Daniel O'Connor <darius@dons.net.au>
parents:
12
diff
changeset
|
169 for midi in range(21, 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
|
170 octave = midi / len(names) - 1 |
7 | 171 index = midi % len(names) |
172 name = names[index] % (octave) | |
27
87cf66e04ef9
Handle offsetting note pitch in a better way.
Daniel O'Connor <darius@dons.net.au>
parents:
26
diff
changeset
|
173 midi2note[midi] = name |
87cf66e04ef9
Handle offsetting note pitch in a better way.
Daniel O'Connor <darius@dons.net.au>
parents:
26
diff
changeset
|
174 note2midi[name] = midi |
4
9f4fa5f231e6
Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents:
3
diff
changeset
|
175 |
7 | 176 return midi2note, note2midi |
177 | |
178 @staticmethod | |
179 def loadnote2slot(fname, note2midi): | |
180 note2slot = {} | |
13
5f4c21bb5140
Add lines and notes engraved on the paper.
Daniel O'Connor <darius@dons.net.au>
parents:
12
diff
changeset
|
181 slot2note = {} |
7 | 182 index = 0 |
183 | |
184 for note in file(fname): | |
185 note = note.strip() | |
186 if note[0] == '#': | |
187 continue | |
188 if note not in note2midi: | |
189 raise exceptions.ValueError('Note \'%s\' not valid' % note) | |
190 note2slot[note] = index | |
13
5f4c21bb5140
Add lines and notes engraved on the paper.
Daniel O'Connor <darius@dons.net.au>
parents:
12
diff
changeset
|
191 slot2note[index] = note |
7 | 192 index += 1 |
193 | |
13
5f4c21bb5140
Add lines and notes engraved on the paper.
Daniel O'Connor <darius@dons.net.au>
parents:
12
diff
changeset
|
194 return note2slot, slot2note |
4
9f4fa5f231e6
Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents:
3
diff
changeset
|
195 |
11
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
196 def emitnote(self, pdfs, slot, start, notelen): |
19
ffaf97818ce3
- Music needs to run right to left.
Daniel O'Connor <darius@dons.net.au>
parents:
18
diff
changeset
|
197 x = start * self.timescale + self.leadin # Convert start time to distance |
ffaf97818ce3
- Music needs to run right to left.
Daniel O'Connor <darius@dons.net.au>
parents:
18
diff
changeset
|
198 pageidx = int(x / self.pagewidth) # Determine which page |
ffaf97818ce3
- Music needs to run right to left.
Daniel O'Connor <darius@dons.net.au>
parents:
18
diff
changeset
|
199 x = x % self.pagewidth # and where on that page |
16
20337b22977d
Add left margin and slot size support.
Daniel O'Connor <darius@dons.net.au>
parents:
14
diff
changeset
|
200 h = self.slotsize |
24
71b78f06ff03
Highest notes are at the bottom not the top.
Daniel O'Connor <darius@dons.net.au>
parents:
23
diff
changeset
|
201 y = self.pageheight - (self.heel + slot * self.pitch - self.slotsize / 2) - self.slotsize |
19
ffaf97818ce3
- Music needs to run right to left.
Daniel O'Connor <darius@dons.net.au>
parents:
18
diff
changeset
|
202 w = notelen * self.timescale # Convert note length in time to distance |
7 | 203 |
11
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
204 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
|
205 w1 = w |
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
206 # 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
|
207 if x + w > self.pagewidth: |
19
ffaf97818ce3
- Music needs to run right to left.
Daniel O'Connor <darius@dons.net.au>
parents:
18
diff
changeset
|
208 w1 = self.pagewidth - x # Crop first note |
ffaf97818ce3
- Music needs to run right to left.
Daniel O'Connor <darius@dons.net.au>
parents:
18
diff
changeset
|
209 w2 = w - w1 # Calculate length of second note |
ffaf97818ce3
- Music needs to run right to left.
Daniel O'Connor <darius@dons.net.au>
parents:
18
diff
changeset
|
210 assert w2 <= self.pagewidth, 'note extends for more than a page' |
11
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
211 # 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
|
212 print 'split note, page %d w2 = %.3f' % (pageidx + 1, w2) |
19
ffaf97818ce3
- Music needs to run right to left.
Daniel O'Connor <darius@dons.net.au>
parents:
18
diff
changeset
|
213 Midi2PDF._emitnote(pdfs[pageidx + 1], self.pagewidth - w2, y, w2, h) |
11
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
214 |
19
ffaf97818ce3
- Music needs to run right to left.
Daniel O'Connor <darius@dons.net.au>
parents:
18
diff
changeset
|
215 Midi2PDF._emitnote(pdfs[pageidx], self.pagewidth - x - w1, y, w1, h) |
7 | 216 |
217 @staticmethod | |
11
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
218 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
|
219 pdf.saveState() |
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
220 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
|
221 pdf.setLineWidth(0) |
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
222 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
|
223 pdf.restoreState() |
0 | 224 |
17
c50427f1da2d
- Draw vertical time lines.
Daniel O'Connor <darius@dons.net.au>
parents:
16
diff
changeset
|
225 @staticmethod |
c50427f1da2d
- Draw vertical time lines.
Daniel O'Connor <darius@dons.net.au>
parents:
16
diff
changeset
|
226 def textHelper(pdf, x, y, colour, fill, fontname, fontsize, text): |
c50427f1da2d
- Draw vertical time lines.
Daniel O'Connor <darius@dons.net.au>
parents:
16
diff
changeset
|
227 tobj = pdf.beginText() |
c50427f1da2d
- Draw vertical time lines.
Daniel O'Connor <darius@dons.net.au>
parents:
16
diff
changeset
|
228 tobj.setTextOrigin(x, y) |
c50427f1da2d
- Draw vertical time lines.
Daniel O'Connor <darius@dons.net.au>
parents:
16
diff
changeset
|
229 tobj.setFont(fontname, fontsize) |
c50427f1da2d
- Draw vertical time lines.
Daniel O'Connor <darius@dons.net.au>
parents:
16
diff
changeset
|
230 tobj.setStrokeColor(colour) |
c50427f1da2d
- Draw vertical time lines.
Daniel O'Connor <darius@dons.net.au>
parents:
16
diff
changeset
|
231 tobj.setFillColor(colour) |
c50427f1da2d
- Draw vertical time lines.
Daniel O'Connor <darius@dons.net.au>
parents:
16
diff
changeset
|
232 if fill: |
c50427f1da2d
- Draw vertical time lines.
Daniel O'Connor <darius@dons.net.au>
parents:
16
diff
changeset
|
233 tobj.setTextRenderMode(0) |
c50427f1da2d
- Draw vertical time lines.
Daniel O'Connor <darius@dons.net.au>
parents:
16
diff
changeset
|
234 else: |
c50427f1da2d
- Draw vertical time lines.
Daniel O'Connor <darius@dons.net.au>
parents:
16
diff
changeset
|
235 tobj.setTextRenderMode(1) |
c50427f1da2d
- Draw vertical time lines.
Daniel O'Connor <darius@dons.net.au>
parents:
16
diff
changeset
|
236 tobj.textLine(text) |
c50427f1da2d
- Draw vertical time lines.
Daniel O'Connor <darius@dons.net.au>
parents:
16
diff
changeset
|
237 pdf.drawText(tobj) |
c50427f1da2d
- Draw vertical time lines.
Daniel O'Connor <darius@dons.net.au>
parents:
16
diff
changeset
|
238 |
0 | 239 if __name__ == '__main__': |
240 main() |