Mercurial > ~darius > hgwebdir.cgi > musiccutter
annotate musiccutter.py @ 31:ea98c9507f47
Preliminary multi-page support. Breaks time marks though.
author | Daniel O'Connor <darius@dons.net.au> |
---|---|
date | Tue, 03 May 2016 18:04:41 +0930 |
parents | f46cc9401e79 |
children | 1d5dcaa3b07d |
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) |
30
f46cc9401e79
Make transposition optional and add note scale parameter (to get rearticulation)
Daniel O'Connor <darius@dons.net.au>
parents:
29
diff
changeset
|
22 m = Midi2PDF('notes', 120, 155, 5.5, 3.3, 6.0, 50, False, False, False, False, False, 0, 30, 0.9, '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): |
31
ea98c9507f47
Preliminary multi-page support. Breaks time marks though.
Daniel O'Connor <darius@dons.net.au>
parents:
30
diff
changeset
|
28 def __init__(self, notefile, pagewidth, pageheight, pitch, slotsize, heel, leadin, timemarks, trytranspose, drawrect, notenames, notelines, noteoffset, pagesperpdf, timescale, notescale, 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 |
30
f46cc9401e79
Make transposition optional and add note scale parameter (to get rearticulation)
Daniel O'Connor <darius@dons.net.au>
parents:
29
diff
changeset
|
38 self.trytranspose = trytranspose # Attempt to tranpose unplayable notes |
19
ffaf97818ce3
- Music needs to run right to left.
Daniel O'Connor <darius@dons.net.au>
parents:
18
diff
changeset
|
39 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
|
40 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
|
41 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
|
42 self.noteoffset = noteoffset # Amount to adjust note pitches by (+12 = 1 octave) |
31
ea98c9507f47
Preliminary multi-page support. Breaks time marks though.
Daniel O'Connor <darius@dons.net.au>
parents:
30
diff
changeset
|
43 self.pagesperpdf = pagesperpdf # Number of pages to emit per PDF |
16
20337b22977d
Add left margin and slot size support.
Daniel O'Connor <darius@dons.net.au>
parents:
14
diff
changeset
|
44 self.timescale = timescale # Width per second |
30
f46cc9401e79
Make transposition optional and add note scale parameter (to get rearticulation)
Daniel O'Connor <darius@dons.net.au>
parents:
29
diff
changeset
|
45 self.notescale = notescale # Multiply all note lengths by this (to get rearticulation) |
11
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
46 self.fontname = fontname |
16
20337b22977d
Add left margin and slot size support.
Daniel O'Connor <darius@dons.net.au>
parents:
14
diff
changeset
|
47 self.fontsize = fontsize # Points |
0 | 48 |
31
ea98c9507f47
Preliminary multi-page support. Breaks time marks though.
Daniel O'Connor <darius@dons.net.au>
parents:
30
diff
changeset
|
49 self.pdfwidth = self.pagewidth * self.pagesperpdf |
ea98c9507f47
Preliminary multi-page support. Breaks time marks though.
Daniel O'Connor <darius@dons.net.au>
parents:
30
diff
changeset
|
50 |
7 | 51 def processMidi(self, midifile, outpat): |
52 playablecount = 0 | |
53 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
|
54 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
|
55 transposedowncount = 0 |
7 | 56 midi = mido.MidiFile(midifile) |
57 ctime = 0 | |
58 channels = [] | |
59 for i in range(16): | |
60 channels.append({}) | |
0 | 61 |
19
ffaf97818ce3
- Music needs to run right to left.
Daniel O'Connor <darius@dons.net.au>
parents:
18
diff
changeset
|
62 npages = int(math.ceil(((midi.length * self.timescale) + self.leadin) / self.pagewidth)) |
31
ea98c9507f47
Preliminary multi-page support. Breaks time marks though.
Daniel O'Connor <darius@dons.net.au>
parents:
30
diff
changeset
|
63 npdfs = int(math.ceil(float(npages) / self.pagesperpdf)) |
ea98c9507f47
Preliminary multi-page support. Breaks time marks though.
Daniel O'Connor <darius@dons.net.au>
parents:
30
diff
changeset
|
64 print 'npages %d, npdfs %d' % (npages, npdfs) |
ea98c9507f47
Preliminary multi-page support. Breaks time marks though.
Daniel O'Connor <darius@dons.net.au>
parents:
30
diff
changeset
|
65 |
11
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
66 pdfs = [] |
31
ea98c9507f47
Preliminary multi-page support. Breaks time marks though.
Daniel O'Connor <darius@dons.net.au>
parents:
30
diff
changeset
|
67 for i in range(npdfs): |
ea98c9507f47
Preliminary multi-page support. Breaks time marks though.
Daniel O'Connor <darius@dons.net.au>
parents:
30
diff
changeset
|
68 pdf = reportlab.pdfgen.canvas.Canvas(file(outpat % (i + 1), 'w'), |
ea98c9507f47
Preliminary multi-page support. Breaks time marks though.
Daniel O'Connor <darius@dons.net.au>
parents:
30
diff
changeset
|
69 pagesize = (self.pdfwidth * mm, self.pageheight * mm)) |
11
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
70 pdfs.append(pdf) |
0 | 71 |
22
65e8298f5800
Remove extension and path name from title.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
72 title = os.path.basename(midifile) |
65e8298f5800
Remove extension and path name from title.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
73 title, ext = os.path.splitext(title) |
7 | 74 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
|
75 # 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
|
76 if hasattr(ev, 'note'): |
87cf66e04ef9
Handle offsetting note pitch in a better way.
Daniel O'Connor <darius@dons.net.au>
parents:
26
diff
changeset
|
77 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
|
78 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
|
79 title = ev.text |
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
80 |
7 | 81 ctime += ev.time |
82 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
|
83 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
|
84 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
|
85 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
|
86 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
|
87 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
|
88 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
|
89 #print ctime, ev |
7 | 90 if ev.type == 'note_on' and ev.velocity > 0: |
91 if ev.note in channels[ev.channel]: | |
92 print 'Duplicate note_on message %d (%s)' % (ev.note, note) | |
0 | 93 else: |
7 | 94 channels[ev.channel][ev.note] = ctime |
95 elif ev.type == 'note_off' or (ev.type == 'note_on' and ev.velocity == 0): | |
96 if ev.note not in channels[ev.channel]: | |
97 print 'note_off with no corresponding note_on for channel %d note %d' % (ev.channel, ev.note) | |
0 | 98 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
|
99 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
|
100 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
|
101 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
|
102 |
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 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
|
104 slot = self.note2slot[note] |
30
f46cc9401e79
Make transposition optional and add note scale parameter (to get rearticulation)
Daniel O'Connor <darius@dons.net.au>
parents:
29
diff
changeset
|
105 elif self.trytranspose and (ev.note - 12 in self.midi2note and self.note2slot[self.midi2note[ev.note - 12]]): |
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 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
|
107 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
|
108 transposedowncount += 1 |
30
f46cc9401e79
Make transposition optional and add note scale parameter (to get rearticulation)
Daniel O'Connor <darius@dons.net.au>
parents:
29
diff
changeset
|
109 elif self.trytranspose and (ev.note + 12 in self.midi2note and self.note2slot[self.midi2note[ev.note + 12]]): |
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
|
110 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
|
111 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
|
112 transposeupcount += 1 |
7 | 113 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
|
114 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
|
115 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
|
116 |
657bc32a0dfd
Try transposing unplayable notes down or up an octave (in that order).
Daniel O'Connor <darius@dons.net.au>
parents:
27
diff
changeset
|
117 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
|
118 #print 'Note %s (%d) at %.2f length %.2f' % (note, slot, start, notelen) |
30
f46cc9401e79
Make transposition optional and add note scale parameter (to get rearticulation)
Daniel O'Connor <darius@dons.net.au>
parents:
29
diff
changeset
|
119 self.emitnote(pdfs, slot, start, notelen * self.notescale) |
7 | 120 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
|
121 |
7 | 122 del channels[ev.channel][ev.note] |
123 elif ev.type == 'end_of_track': | |
124 print 'EOT, not flushing, check for missed notes' | |
125 for chan in channels: | |
126 for ev in chan: | |
127 print ev | |
128 | |
129 print 'Playable count:', playablecount | |
130 print 'Unplayable count:', unplayablecount | |
30
f46cc9401e79
Make transposition optional and add note scale parameter (to get rearticulation)
Daniel O'Connor <darius@dons.net.au>
parents:
29
diff
changeset
|
131 if self.trytranspose: |
f46cc9401e79
Make transposition optional and add note scale parameter (to get rearticulation)
Daniel O'Connor <darius@dons.net.au>
parents:
29
diff
changeset
|
132 print 'Transpose down:', transposedowncount |
f46cc9401e79
Make transposition optional and add note scale parameter (to get rearticulation)
Daniel O'Connor <darius@dons.net.au>
parents:
29
diff
changeset
|
133 print 'Transpose up:', transposeupcount |
7 | 134 |
31
ea98c9507f47
Preliminary multi-page support. Breaks time marks though.
Daniel O'Connor <darius@dons.net.au>
parents:
30
diff
changeset
|
135 for pindx in range(npages): |
ea98c9507f47
Preliminary multi-page support. Breaks time marks though.
Daniel O'Connor <darius@dons.net.au>
parents:
30
diff
changeset
|
136 pdf = pdfs[pindx / self.pagesperpdf] # PDF for this page |
ea98c9507f47
Preliminary multi-page support. Breaks time marks though.
Daniel O'Connor <darius@dons.net.au>
parents:
30
diff
changeset
|
137 # Offset into PDF where the page starts |
ea98c9507f47
Preliminary multi-page support. Breaks time marks though.
Daniel O'Connor <darius@dons.net.au>
parents:
30
diff
changeset
|
138 pageofs = self.pagewidth * (self.pagesperpdf - (pindx % self.pagesperpdf) - 1) |
13
5f4c21bb5140
Add lines and notes engraved on the paper.
Daniel O'Connor <darius@dons.net.au>
parents:
12
diff
changeset
|
139 # Add title and page number |
31
ea98c9507f47
Preliminary multi-page support. Breaks time marks though.
Daniel O'Connor <darius@dons.net.au>
parents:
30
diff
changeset
|
140 Midi2PDF.textHelper(pdf, pageofs * mm, 1 * mm, |
ea98c9507f47
Preliminary multi-page support. Breaks time marks though.
Daniel O'Connor <darius@dons.net.au>
parents:
30
diff
changeset
|
141 ENGRAVE_COLOUR, True, self.fontname, self.fontsize, |
ea98c9507f47
Preliminary multi-page support. Breaks time marks though.
Daniel O'Connor <darius@dons.net.au>
parents:
30
diff
changeset
|
142 '%s (%d / %d)' % (title, pindx + 1, npages)) |
ea98c9507f47
Preliminary multi-page support. Breaks time marks though.
Daniel O'Connor <darius@dons.net.au>
parents:
30
diff
changeset
|
143 pdf.saveState() |
13
5f4c21bb5140
Add lines and notes engraved on the paper.
Daniel O'Connor <darius@dons.net.au>
parents:
12
diff
changeset
|
144 pdf.setLineWidth(0) |
17
c50427f1da2d
- Draw vertical time lines.
Daniel O'Connor <darius@dons.net.au>
parents:
16
diff
changeset
|
145 |
31
ea98c9507f47
Preliminary multi-page support. Breaks time marks though.
Daniel O'Connor <darius@dons.net.au>
parents:
30
diff
changeset
|
146 # Draw time marks every 5 seconds |
19
ffaf97818ce3
- Music needs to run right to left.
Daniel O'Connor <darius@dons.net.au>
parents:
18
diff
changeset
|
147 if self.timemarks: |
31
ea98c9507f47
Preliminary multi-page support. Breaks time marks though.
Daniel O'Connor <darius@dons.net.au>
parents:
30
diff
changeset
|
148 pdfidx = pindx / self.pagesperpdf |
ea98c9507f47
Preliminary multi-page support. Breaks time marks though.
Daniel O'Connor <darius@dons.net.au>
parents:
30
diff
changeset
|
149 # Work out start and end times (pdf 1 is special due to leadin) |
19
ffaf97818ce3
- Music needs to run right to left.
Daniel O'Connor <darius@dons.net.au>
parents:
18
diff
changeset
|
150 tstart = self.leadin / self.timescale |
31
ea98c9507f47
Preliminary multi-page support. Breaks time marks though.
Daniel O'Connor <darius@dons.net.au>
parents:
30
diff
changeset
|
151 tend = (self.pagewidth * self.pagesperpdf) / 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
|
152 if pindx > 0: |
31
ea98c9507f47
Preliminary multi-page support. Breaks time marks though.
Daniel O'Connor <darius@dons.net.au>
parents:
30
diff
changeset
|
153 tsize = self.pagewidth / self.timescale # Amount of time per pdf |
ea98c9507f47
Preliminary multi-page support. Breaks time marks though.
Daniel O'Connor <darius@dons.net.au>
parents:
30
diff
changeset
|
154 tstart = tend + tsize * pdfidx |
ea98c9507f47
Preliminary multi-page support. Breaks time marks though.
Daniel O'Connor <darius@dons.net.au>
parents:
30
diff
changeset
|
155 tend = tend + tsize * (pdfidx + 1) |
18
cf93a76eda92
The laser cutter software barfs on these lines and stops processing
Daniel O'Connor <darius@dons.net.au>
parents:
17
diff
changeset
|
156 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
|
157 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
|
158 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
|
159 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
|
160 |
25 | 161 # 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
|
162 if self.drawrect: |
31
ea98c9507f47
Preliminary multi-page support. Breaks time marks though.
Daniel O'Connor <darius@dons.net.au>
parents:
30
diff
changeset
|
163 pdf.rect((pindx % self.pagesperpdf) * self.pagewidth * mm, 0, |
ea98c9507f47
Preliminary multi-page support. Breaks time marks though.
Daniel O'Connor <darius@dons.net.au>
parents:
30
diff
changeset
|
164 self.pagewidth * mm, self.pageheight * mm, fill = False, stroke = True) |
18
cf93a76eda92
The laser cutter software barfs on these lines and stops processing
Daniel O'Connor <darius@dons.net.au>
parents:
17
diff
changeset
|
165 |
13
5f4c21bb5140
Add lines and notes engraved on the paper.
Daniel O'Connor <darius@dons.net.au>
parents:
12
diff
changeset
|
166 # Draw lines per note |
5f4c21bb5140
Add lines and notes engraved on the paper.
Daniel O'Connor <darius@dons.net.au>
parents:
12
diff
changeset
|
167 for slot in sorted(self.slot2note.keys()): |
26
f492b70f5e49
Fix notename/line drawing.
Daniel O'Connor <darius@dons.net.au>
parents:
25
diff
changeset
|
168 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
|
169 if self.notelines: |
31
ea98c9507f47
Preliminary multi-page support. Breaks time marks though.
Daniel O'Connor <darius@dons.net.au>
parents:
30
diff
changeset
|
170 pdf.line(0, ofs * mm, self.pdfwidth * mm, ofs * mm) |
13
5f4c21bb5140
Add lines and notes engraved on the paper.
Daniel O'Connor <darius@dons.net.au>
parents:
12
diff
changeset
|
171 # Note name |
20
9bb72ae63651
- Make various things optional.
Daniel O'Connor <darius@dons.net.au>
parents:
19
diff
changeset
|
172 if self.notenames: |
31
ea98c9507f47
Preliminary multi-page support. Breaks time marks though.
Daniel O'Connor <darius@dons.net.au>
parents:
30
diff
changeset
|
173 Midi2PDF.textHelper(pdf, (self.pdfwidth - 10) * mm, (ofs + 0.5) * mm, ENGRAVE_COLOUR, False, self.fontname, self.fontsize, self.slot2note[slot]) |
ea98c9507f47
Preliminary multi-page support. Breaks time marks though.
Daniel O'Connor <darius@dons.net.au>
parents:
30
diff
changeset
|
174 pdf.restoreState() |
ea98c9507f47
Preliminary multi-page support. Breaks time marks though.
Daniel O'Connor <darius@dons.net.au>
parents:
30
diff
changeset
|
175 for pdf in pdfs: |
26
f492b70f5e49
Fix notename/line drawing.
Daniel O'Connor <darius@dons.net.au>
parents:
25
diff
changeset
|
176 pdf.save() |
7 | 177 |
12
6e46ceee57a7
Use correct MIDI note generation, previous version did not agree with
Daniel O'Connor <darius@dons.net.au>
parents:
11
diff
changeset
|
178 # http://newt.phys.unsw.edu.au/jw/notes.html |
7 | 179 @staticmethod |
27
87cf66e04ef9
Handle offsetting note pitch in a better way.
Daniel O'Connor <darius@dons.net.au>
parents:
26
diff
changeset
|
180 def genmidi2note(): |
87cf66e04ef9
Handle offsetting note pitch in a better way.
Daniel O'Connor <darius@dons.net.au>
parents:
26
diff
changeset
|
181 '''Create forward & reverse tables for midi number to note name (assuming 69 = A4 = A440)''' |
7 | 182 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'] |
183 midi2note = {} | |
184 note2midi = {} | |
13
5f4c21bb5140
Add lines and notes engraved on the paper.
Daniel O'Connor <darius@dons.net.au>
parents:
12
diff
changeset
|
185 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
|
186 octave = midi / len(names) - 1 |
7 | 187 index = midi % len(names) |
188 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
|
189 midi2note[midi] = name |
87cf66e04ef9
Handle offsetting note pitch in a better way.
Daniel O'Connor <darius@dons.net.au>
parents:
26
diff
changeset
|
190 note2midi[name] = midi |
4
9f4fa5f231e6
Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents:
3
diff
changeset
|
191 |
7 | 192 return midi2note, note2midi |
193 | |
194 @staticmethod | |
195 def loadnote2slot(fname, note2midi): | |
196 note2slot = {} | |
13
5f4c21bb5140
Add lines and notes engraved on the paper.
Daniel O'Connor <darius@dons.net.au>
parents:
12
diff
changeset
|
197 slot2note = {} |
7 | 198 index = 0 |
199 | |
200 for note in file(fname): | |
201 note = note.strip() | |
202 if note[0] == '#': | |
203 continue | |
204 if note not in note2midi: | |
205 raise exceptions.ValueError('Note \'%s\' not valid' % note) | |
206 note2slot[note] = index | |
13
5f4c21bb5140
Add lines and notes engraved on the paper.
Daniel O'Connor <darius@dons.net.au>
parents:
12
diff
changeset
|
207 slot2note[index] = note |
7 | 208 index += 1 |
209 | |
13
5f4c21bb5140
Add lines and notes engraved on the paper.
Daniel O'Connor <darius@dons.net.au>
parents:
12
diff
changeset
|
210 return note2slot, slot2note |
4
9f4fa5f231e6
Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents:
3
diff
changeset
|
211 |
11
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
212 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
|
213 x = start * self.timescale + self.leadin # Convert start time to distance |
31
ea98c9507f47
Preliminary multi-page support. Breaks time marks though.
Daniel O'Connor <darius@dons.net.au>
parents:
30
diff
changeset
|
214 pdfidx = int(x / self.pdfwidth) # Determine which pdf |
ea98c9507f47
Preliminary multi-page support. Breaks time marks though.
Daniel O'Connor <darius@dons.net.au>
parents:
30
diff
changeset
|
215 x = x % self.pdfwidth # and where on that pdf |
16
20337b22977d
Add left margin and slot size support.
Daniel O'Connor <darius@dons.net.au>
parents:
14
diff
changeset
|
216 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
|
217 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
|
218 w = notelen * self.timescale # Convert note length in time to distance |
7 | 219 |
31
ea98c9507f47
Preliminary multi-page support. Breaks time marks though.
Daniel O'Connor <darius@dons.net.au>
parents:
30
diff
changeset
|
220 print 'pdf = %d x = %.3f y = %.3f w = %.3f h = %.3f' % (pdfidx, x, y, w, h) |
11
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
221 w1 = w |
31
ea98c9507f47
Preliminary multi-page support. Breaks time marks though.
Daniel O'Connor <darius@dons.net.au>
parents:
30
diff
changeset
|
222 # Check if the note crosses a pdf |
ea98c9507f47
Preliminary multi-page support. Breaks time marks though.
Daniel O'Connor <darius@dons.net.au>
parents:
30
diff
changeset
|
223 if x + w > self.pdfwidth: |
ea98c9507f47
Preliminary multi-page support. Breaks time marks though.
Daniel O'Connor <darius@dons.net.au>
parents:
30
diff
changeset
|
224 w1 = self.pdfwidth - x # Crop first note |
19
ffaf97818ce3
- Music needs to run right to left.
Daniel O'Connor <darius@dons.net.au>
parents:
18
diff
changeset
|
225 w2 = w - w1 # Calculate length of second note |
31
ea98c9507f47
Preliminary multi-page support. Breaks time marks though.
Daniel O'Connor <darius@dons.net.au>
parents:
30
diff
changeset
|
226 assert w2 <= self.pdfwidth, 'note extends for more than a pdf' |
11
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
227 # Emit second half of note |
31
ea98c9507f47
Preliminary multi-page support. Breaks time marks though.
Daniel O'Connor <darius@dons.net.au>
parents:
30
diff
changeset
|
228 print 'split note, pdf %d w2 = %.3f' % (pdfidx + 1, w2) |
ea98c9507f47
Preliminary multi-page support. Breaks time marks though.
Daniel O'Connor <darius@dons.net.au>
parents:
30
diff
changeset
|
229 Midi2PDF._emitnote(pdfs[pdfidx + 1], self.pdfwidth - 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
|
230 |
31
ea98c9507f47
Preliminary multi-page support. Breaks time marks though.
Daniel O'Connor <darius@dons.net.au>
parents:
30
diff
changeset
|
231 Midi2PDF._emitnote(pdfs[pdfidx], self.pdfwidth - x - w1, y, w1, h) |
7 | 232 |
233 @staticmethod | |
11
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
234 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
|
235 pdf.saveState() |
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
236 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
|
237 pdf.setLineWidth(0) |
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
238 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
|
239 pdf.restoreState() |
0 | 240 |
17
c50427f1da2d
- Draw vertical time lines.
Daniel O'Connor <darius@dons.net.au>
parents:
16
diff
changeset
|
241 @staticmethod |
c50427f1da2d
- Draw vertical time lines.
Daniel O'Connor <darius@dons.net.au>
parents:
16
diff
changeset
|
242 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
|
243 tobj = pdf.beginText() |
c50427f1da2d
- Draw vertical time lines.
Daniel O'Connor <darius@dons.net.au>
parents:
16
diff
changeset
|
244 tobj.setTextOrigin(x, y) |
c50427f1da2d
- Draw vertical time lines.
Daniel O'Connor <darius@dons.net.au>
parents:
16
diff
changeset
|
245 tobj.setFont(fontname, fontsize) |
c50427f1da2d
- Draw vertical time lines.
Daniel O'Connor <darius@dons.net.au>
parents:
16
diff
changeset
|
246 tobj.setStrokeColor(colour) |
c50427f1da2d
- Draw vertical time lines.
Daniel O'Connor <darius@dons.net.au>
parents:
16
diff
changeset
|
247 tobj.setFillColor(colour) |
c50427f1da2d
- Draw vertical time lines.
Daniel O'Connor <darius@dons.net.au>
parents:
16
diff
changeset
|
248 if fill: |
c50427f1da2d
- Draw vertical time lines.
Daniel O'Connor <darius@dons.net.au>
parents:
16
diff
changeset
|
249 tobj.setTextRenderMode(0) |
c50427f1da2d
- Draw vertical time lines.
Daniel O'Connor <darius@dons.net.au>
parents:
16
diff
changeset
|
250 else: |
c50427f1da2d
- Draw vertical time lines.
Daniel O'Connor <darius@dons.net.au>
parents:
16
diff
changeset
|
251 tobj.setTextRenderMode(1) |
c50427f1da2d
- Draw vertical time lines.
Daniel O'Connor <darius@dons.net.au>
parents:
16
diff
changeset
|
252 tobj.textLine(text) |
c50427f1da2d
- Draw vertical time lines.
Daniel O'Connor <darius@dons.net.au>
parents:
16
diff
changeset
|
253 pdf.drawText(tobj) |
c50427f1da2d
- Draw vertical time lines.
Daniel O'Connor <darius@dons.net.au>
parents:
16
diff
changeset
|
254 |
0 | 255 if __name__ == '__main__': |
256 main() |