annotate musiccutter.py @ 27:87cf66e04ef9

Handle offsetting note pitch in a better way.
author Daniel O'Connor <darius@dons.net.au>
date Tue, 03 May 2016 08:31:28 +0930
parents f492b70f5e49
children 657bc32a0dfd
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1 #!/usr/bin/env python
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
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
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
5 import math
0
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
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
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
11 import sys
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
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
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
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
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
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
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
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
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
45
7
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
46 def processMidi(self, midifile, outpat):
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
47 playablecount = 0
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
48 unplayablecount = 0
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
49 midi = mido.MidiFile(midifile)
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
50 ctime = 0
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
51 channels = []
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
52 for i in range(16):
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
53 channels.append({})
0
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
54
19
ffaf97818ce3 - Music needs to run right to left.
Daniel O'Connor <darius@dons.net.au>
parents: 18
diff changeset
55 npages = int(math.ceil(((midi.length * self.timescale) + self.leadin) / self.pagewidth))
7
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
56 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
57 pdfs = []
7
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
58 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
59 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
60 pdfs.append(pdf)
0
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
61
22
65e8298f5800 Remove extension and path name from title.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
62 title = os.path.basename(midifile)
65e8298f5800 Remove extension and path name from title.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
63 title, ext = os.path.splitext(title)
7
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
64 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
65 # 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
66 if hasattr(ev, 'note'):
87cf66e04ef9 Handle offsetting note pitch in a better way.
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
67 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
68 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
69 title = ev.text
9faad813e39e switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents: 9
diff changeset
70
7
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
71 ctime += ev.time
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
72 if ev.type == 'note_on' or ev.type == 'note_off':
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
73 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
74 #print ctime, ev
7
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
75 if ev.type == 'note_on' and ev.velocity > 0:
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
76 if ev.note in channels[ev.channel]:
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
77 print 'Duplicate note_on message %d (%s)' % (ev.note, note)
0
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
78 else:
7
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
79 channels[ev.channel][ev.note] = ctime
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
80 elif ev.type == 'note_off' or (ev.type == 'note_on' and ev.velocity == 0):
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
81 if ev.note not in channels[ev.channel]:
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
82 print 'note_off with no corresponding note_on for channel %d note %d' % (ev.channel, ev.note)
0
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
83 else:
7
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
84 if note not in self.note2slot:
21
c710c4c3f44f Add pitch offset (number of midi notes to move up/down)
Daniel O'Connor <darius@dons.net.au>
parents: 20
diff changeset
85 print 'Skipping unplayable note %d (%s)' % (ev.note, note)
7
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
86 unplayablecount += 1
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
87 else:
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
88 start = channels[ev.channel][ev.note]
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
89 notelen = ctime - start
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
90 slot = self.note2slot[note]
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
91 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
92 self.emitnote(pdfs, slot, start, notelen)
7
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
93 playablecount += 1
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
94 del channels[ev.channel][ev.note]
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
95 elif ev.type == 'end_of_track':
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
96 print 'EOT, not flushing, check for missed notes'
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
97 for chan in channels:
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
98 for ev in chan:
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
99 print ev
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
100
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
101 print 'Playable count:', playablecount
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
102 print 'Unplayable count:', unplayablecount
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
103
17
c50427f1da2d - Draw vertical time lines.
Daniel O'Connor <darius@dons.net.au>
parents: 16
diff changeset
104 for pindx in range(len(pdfs)):
c50427f1da2d - Draw vertical time lines.
Daniel O'Connor <darius@dons.net.au>
parents: 16
diff changeset
105 pdf = pdfs[pindx]
13
5f4c21bb5140 Add lines and notes engraved on the paper.
Daniel O'Connor <darius@dons.net.au>
parents: 12
diff changeset
106 # 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
107 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
108
5f4c21bb5140 Add lines and notes engraved on the paper.
Daniel O'Connor <darius@dons.net.au>
parents: 12
diff changeset
109 pdf.setLineWidth(0)
17
c50427f1da2d - Draw vertical time lines.
Daniel O'Connor <darius@dons.net.au>
parents: 16
diff changeset
110
18
cf93a76eda92 The laser cutter software barfs on these lines and stops processing
Daniel O'Connor <darius@dons.net.au>
parents: 17
diff changeset
111 # Draw time marks
19
ffaf97818ce3 - Music needs to run right to left.
Daniel O'Connor <darius@dons.net.au>
parents: 18
diff changeset
112 if self.timemarks:
ffaf97818ce3 - Music needs to run right to left.
Daniel O'Connor <darius@dons.net.au>
parents: 18
diff changeset
113 tstart = self.leadin / self.timescale
ffaf97818ce3 - Music needs to run right to left.
Daniel O'Connor <darius@dons.net.au>
parents: 18
diff changeset
114 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
115 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
116 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
117 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
118 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
119 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
120 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
121 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
122 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
123
25
ce367392806c comment tweaks
Daniel O'Connor <darius@dons.net.au>
parents: 24
diff changeset
124 # 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
125 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
126 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
127
13
5f4c21bb5140 Add lines and notes engraved on the paper.
Daniel O'Connor <darius@dons.net.au>
parents: 12
diff changeset
128 # Draw lines per note
5f4c21bb5140 Add lines and notes engraved on the paper.
Daniel O'Connor <darius@dons.net.au>
parents: 12
diff changeset
129 for slot in sorted(self.slot2note.keys()):
26
f492b70f5e49 Fix notename/line drawing.
Daniel O'Connor <darius@dons.net.au>
parents: 25
diff changeset
130 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
131 if self.notelines:
26
f492b70f5e49 Fix notename/line drawing.
Daniel O'Connor <darius@dons.net.au>
parents: 25
diff changeset
132 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
133 # Note name
20
9bb72ae63651 - Make various things optional.
Daniel O'Connor <darius@dons.net.au>
parents: 19
diff changeset
134 if self.notenames:
26
f492b70f5e49 Fix notename/line drawing.
Daniel O'Connor <darius@dons.net.au>
parents: 25
diff changeset
135 Midi2PDF.textHelper(pdf, (self.pagewidth - 10) * mm, (ofs + 0.5) * mm, ENGRAVE_COLOUR, False, self.fontname, self.fontsize, self.slot2note[slot])
25
ce367392806c comment tweaks
Daniel O'Connor <darius@dons.net.au>
parents: 24
diff changeset
136
26
f492b70f5e49 Fix notename/line drawing.
Daniel O'Connor <darius@dons.net.au>
parents: 25
diff changeset
137 # Save PDF
f492b70f5e49 Fix notename/line drawing.
Daniel O'Connor <darius@dons.net.au>
parents: 25
diff changeset
138 pdf.save()
7
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
139
12
6e46ceee57a7 Use correct MIDI note generation, previous version did not agree with
Daniel O'Connor <darius@dons.net.au>
parents: 11
diff changeset
140 # http://newt.phys.unsw.edu.au/jw/notes.html
7
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
141 @staticmethod
27
87cf66e04ef9 Handle offsetting note pitch in a better way.
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
142 def genmidi2note():
87cf66e04ef9 Handle offsetting note pitch in a better way.
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
143 '''Create forward & reverse tables for midi number to note name (assuming 69 = A4 = A440)'''
7
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
144 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']
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
145 midi2note = {}
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
146 note2midi = {}
13
5f4c21bb5140 Add lines and notes engraved on the paper.
Daniel O'Connor <darius@dons.net.au>
parents: 12
diff changeset
147 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
148 octave = midi / len(names) - 1
7
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
149 index = midi % len(names)
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
150 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
151 midi2note[midi] = name
87cf66e04ef9 Handle offsetting note pitch in a better way.
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
152 note2midi[name] = midi
4
9f4fa5f231e6 Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
153
7
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
154 return midi2note, note2midi
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
155
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
156 @staticmethod
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
157 def loadnote2slot(fname, note2midi):
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
158 note2slot = {}
13
5f4c21bb5140 Add lines and notes engraved on the paper.
Daniel O'Connor <darius@dons.net.au>
parents: 12
diff changeset
159 slot2note = {}
7
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
160 index = 0
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
161
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
162 for note in file(fname):
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
163 note = note.strip()
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
164 if note[0] == '#':
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
165 continue
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
166 if note not in note2midi:
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
167 raise exceptions.ValueError('Note \'%s\' not valid' % note)
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
168 note2slot[note] = index
13
5f4c21bb5140 Add lines and notes engraved on the paper.
Daniel O'Connor <darius@dons.net.au>
parents: 12
diff changeset
169 slot2note[index] = note
7
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
170 index += 1
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
171
13
5f4c21bb5140 Add lines and notes engraved on the paper.
Daniel O'Connor <darius@dons.net.au>
parents: 12
diff changeset
172 return note2slot, slot2note
4
9f4fa5f231e6 Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
173
11
9faad813e39e switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents: 9
diff changeset
174 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
175 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
176 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
177 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
178 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
179 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
180 w = notelen * self.timescale # Convert note length in time to distance
7
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
181
11
9faad813e39e switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents: 9
diff changeset
182 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
183 w1 = w
9faad813e39e switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents: 9
diff changeset
184 # 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
185 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
186 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
187 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
188 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
189 # 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
190 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
191 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
192
19
ffaf97818ce3 - Music needs to run right to left.
Daniel O'Connor <darius@dons.net.au>
parents: 18
diff changeset
193 Midi2PDF._emitnote(pdfs[pageidx], self.pagewidth - x - w1, y, w1, h)
7
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
194
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
195 @staticmethod
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(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
197 pdf.saveState()
9faad813e39e switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents: 9
diff changeset
198 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
199 pdf.setLineWidth(0)
9faad813e39e switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents: 9
diff changeset
200 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
201 pdf.restoreState()
0
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
202
17
c50427f1da2d - Draw vertical time lines.
Daniel O'Connor <darius@dons.net.au>
parents: 16
diff changeset
203 @staticmethod
c50427f1da2d - Draw vertical time lines.
Daniel O'Connor <darius@dons.net.au>
parents: 16
diff changeset
204 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
205 tobj = pdf.beginText()
c50427f1da2d - Draw vertical time lines.
Daniel O'Connor <darius@dons.net.au>
parents: 16
diff changeset
206 tobj.setTextOrigin(x, y)
c50427f1da2d - Draw vertical time lines.
Daniel O'Connor <darius@dons.net.au>
parents: 16
diff changeset
207 tobj.setFont(fontname, fontsize)
c50427f1da2d - Draw vertical time lines.
Daniel O'Connor <darius@dons.net.au>
parents: 16
diff changeset
208 tobj.setStrokeColor(colour)
c50427f1da2d - Draw vertical time lines.
Daniel O'Connor <darius@dons.net.au>
parents: 16
diff changeset
209 tobj.setFillColor(colour)
c50427f1da2d - Draw vertical time lines.
Daniel O'Connor <darius@dons.net.au>
parents: 16
diff changeset
210 if fill:
c50427f1da2d - Draw vertical time lines.
Daniel O'Connor <darius@dons.net.au>
parents: 16
diff changeset
211 tobj.setTextRenderMode(0)
c50427f1da2d - Draw vertical time lines.
Daniel O'Connor <darius@dons.net.au>
parents: 16
diff changeset
212 else:
c50427f1da2d - Draw vertical time lines.
Daniel O'Connor <darius@dons.net.au>
parents: 16
diff changeset
213 tobj.setTextRenderMode(1)
c50427f1da2d - Draw vertical time lines.
Daniel O'Connor <darius@dons.net.au>
parents: 16
diff changeset
214 tobj.textLine(text)
c50427f1da2d - Draw vertical time lines.
Daniel O'Connor <darius@dons.net.au>
parents: 16
diff changeset
215 pdf.drawText(tobj)
c50427f1da2d - Draw vertical time lines.
Daniel O'Connor <darius@dons.net.au>
parents: 16
diff changeset
216
0
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
217 if __name__ == '__main__':
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
218 main()