annotate musiccutter.py @ 14:43b5d8d62df6

Just stroke the note names to make things faster.
author Daniel O'Connor <darius@dons.net.au>
date Wed, 13 Apr 2016 08:43:54 +0930
parents 5f4c21bb5140
children 20337b22977d
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
11
9faad813e39e switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents: 9
diff changeset
7 import os
9faad813e39e switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents: 9
diff changeset
8 import reportlab.lib.colors
12
6e46ceee57a7 Use correct MIDI note generation, previous version did not agree with
Daniel O'Connor <darius@dons.net.au>
parents: 11
diff changeset
9 import reportlab.pdfgen.canvas
11
9faad813e39e switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents: 9
diff changeset
10 from reportlab.lib.units import mm
0
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
9faad813e39e switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents: 9
diff changeset
16
9faad813e39e switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents: 9
diff changeset
17 ## XXX: PDF origin bottom left, SVG origin top left
3
49a33c431b45 Accept filename for test function
Daniel O'Connor <darius@dons.net.au>
parents: 2
diff changeset
18 def test(filename = None):
49a33c431b45 Accept filename for test function
Daniel O'Connor <darius@dons.net.au>
parents: 2
diff changeset
19 if filename == None:
49a33c431b45 Accept filename for test function
Daniel O'Connor <darius@dons.net.au>
parents: 2
diff changeset
20 filename = 'test.midi'
7
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
21 # Card layout from http://www.orgues-de-barbarie.com/wp-content/uploads/2014/09/format-cartons.pdf
13
5f4c21bb5140 Add lines and notes engraved on the paper.
Daniel O'Connor <darius@dons.net.au>
parents: 12
diff changeset
22 m = Midi2PDF('notes', 200, 155, 5.5, 6.0, 20, 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)
9faad813e39e switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents: 9
diff changeset
24 m.processMidi(filename, base + '-%02d.pdf')
0
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
25
11
9faad813e39e switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents: 9
diff changeset
26 class Midi2PDF(object):
13
5f4c21bb5140 Add lines and notes engraved on the paper.
Daniel O'Connor <darius@dons.net.au>
parents: 12
diff changeset
27 def __init__(self, notefile, pagewidth, pageheight, pitch, offset, margin, timescale, fontname, fontsize):
11
9faad813e39e switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents: 9
diff changeset
28 self.midi2note, self.note2midi = Midi2PDF.genmidi2note()
13
5f4c21bb5140 Add lines and notes engraved on the paper.
Daniel O'Connor <darius@dons.net.au>
parents: 12
diff changeset
29 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
30 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
31 self.pageheight = pageheight
13
5f4c21bb5140 Add lines and notes engraved on the paper.
Daniel O'Connor <darius@dons.net.au>
parents: 12
diff changeset
32 self.pitch = pitch # Distance between each slot
5f4c21bb5140 Add lines and notes engraved on the paper.
Daniel O'Connor <darius@dons.net.au>
parents: 12
diff changeset
33 self.offset = offset # Bottom margin
5f4c21bb5140 Add lines and notes engraved on the paper.
Daniel O'Connor <darius@dons.net.au>
parents: 12
diff changeset
34 self.margin = margin # Left margin
7
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
35 self.timescale = timescale
11
9faad813e39e switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents: 9
diff changeset
36 self.fontname = fontname
9faad813e39e switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents: 9
diff changeset
37 self.fontsize = fontsize
0
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
38
7
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
39 def processMidi(self, midifile, outpat):
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
40 playablecount = 0
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
41 unplayablecount = 0
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
42 midi = mido.MidiFile(midifile)
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
43 ctime = 0
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
44 channels = []
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
45 for i in range(16):
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
46 channels.append({})
0
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
47
11
9faad813e39e switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents: 9
diff changeset
48 npages = int(math.ceil(midi.length * self.timescale / self.pagewidth))
7
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
49 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
50 pdfs = []
7
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
51 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
52 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
53 pdfs.append(pdf)
0
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
54
11
9faad813e39e switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents: 9
diff changeset
55 title = midifile
7
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
56 for ev in midi:
11
9faad813e39e switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents: 9
diff changeset
57 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
58 title = ev.text
9faad813e39e switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents: 9
diff changeset
59
7
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
60 ctime += ev.time
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
61 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
62 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
63 #print ctime, ev
7
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
64 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
65 if ev.note in channels[ev.channel]:
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
66 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
67 else:
7
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
68 channels[ev.channel][ev.note] = ctime
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
69 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
70 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
71 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
72 else:
7
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
73 if note not in self.note2slot:
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
74 print 'Skipping unplayable note %s' % (note)
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
75 unplayablecount += 1
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
76 else:
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
77 start = channels[ev.channel][ev.note]
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
78 notelen = ctime - start
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
79 slot = self.note2slot[note]
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
80 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
81 self.emitnote(pdfs, slot, start, notelen)
7
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
82 playablecount += 1
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
83 del channels[ev.channel][ev.note]
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
84 elif ev.type == 'end_of_track':
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
85 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
86 for chan in channels:
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
87 for ev in chan:
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
88 print ev
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
89
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
90 print 'Playable count:', playablecount
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
91 print 'Unplayable count:', unplayablecount
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
92
11
9faad813e39e switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents: 9
diff changeset
93 for i in range(len(pdfs)):
9faad813e39e switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents: 9
diff changeset
94 pdf = pdfs[i]
13
5f4c21bb5140 Add lines and notes engraved on the paper.
Daniel O'Connor <darius@dons.net.au>
parents: 12
diff changeset
95 # Add title and page number
11
9faad813e39e switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents: 9
diff changeset
96 tobj = pdf.beginText()
13
5f4c21bb5140 Add lines and notes engraved on the paper.
Daniel O'Connor <darius@dons.net.au>
parents: 12
diff changeset
97 tobj.setTextOrigin(50 * mm, 1 * mm)
11
9faad813e39e switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents: 9
diff changeset
98 tobj.setFont(self.fontname, self.fontsize)
9faad813e39e switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents: 9
diff changeset
99 tobj.setFillColor(ENGRAVE_COLOUR)
9faad813e39e switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents: 9
diff changeset
100 tobj.setStrokeColor(ENGRAVE_COLOUR)
9faad813e39e switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents: 9
diff changeset
101 tobj.textLine('%s (%d / %d)' % (title, i + 1, npages))
9faad813e39e switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents: 9
diff changeset
102 pdf.drawText(tobj)
13
5f4c21bb5140 Add lines and notes engraved on the paper.
Daniel O'Connor <darius@dons.net.au>
parents: 12
diff changeset
103
5f4c21bb5140 Add lines and notes engraved on the paper.
Daniel O'Connor <darius@dons.net.au>
parents: 12
diff changeset
104 # Draw rect around page
5f4c21bb5140 Add lines and notes engraved on the paper.
Daniel O'Connor <darius@dons.net.au>
parents: 12
diff changeset
105 pdf.saveState()
5f4c21bb5140 Add lines and notes engraved on the paper.
Daniel O'Connor <darius@dons.net.au>
parents: 12
diff changeset
106 pdf.setLineWidth(0)
5f4c21bb5140 Add lines and notes engraved on the paper.
Daniel O'Connor <darius@dons.net.au>
parents: 12
diff changeset
107 #pdf.rect(0, 0, self.pagewidth * mm, self.pageheight * mm, fill = False, stroke = True)
5f4c21bb5140 Add lines and notes engraved on the paper.
Daniel O'Connor <darius@dons.net.au>
parents: 12
diff changeset
108 # Draw lines per note
5f4c21bb5140 Add lines and notes engraved on the paper.
Daniel O'Connor <darius@dons.net.au>
parents: 12
diff changeset
109 for slot in sorted(self.slot2note.keys()):
5f4c21bb5140 Add lines and notes engraved on the paper.
Daniel O'Connor <darius@dons.net.au>
parents: 12
diff changeset
110 ofs = (self.offset + slot * self.pitch) * mm
5f4c21bb5140 Add lines and notes engraved on the paper.
Daniel O'Connor <darius@dons.net.au>
parents: 12
diff changeset
111 pdf.line(0, ofs, self.pagewidth * mm, ofs)
5f4c21bb5140 Add lines and notes engraved on the paper.
Daniel O'Connor <darius@dons.net.au>
parents: 12
diff changeset
112 # Note name
5f4c21bb5140 Add lines and notes engraved on the paper.
Daniel O'Connor <darius@dons.net.au>
parents: 12
diff changeset
113 tobj = pdf.beginText()
14
43b5d8d62df6 Just stroke the note names to make things faster.
Daniel O'Connor <darius@dons.net.au>
parents: 13
diff changeset
114 tobj.setTextRenderMode(1)
13
5f4c21bb5140 Add lines and notes engraved on the paper.
Daniel O'Connor <darius@dons.net.au>
parents: 12
diff changeset
115 tobj.setTextOrigin(0 * mm, ofs + 1 * mm)
5f4c21bb5140 Add lines and notes engraved on the paper.
Daniel O'Connor <darius@dons.net.au>
parents: 12
diff changeset
116 tobj.setFont(self.fontname, self.fontsize)
5f4c21bb5140 Add lines and notes engraved on the paper.
Daniel O'Connor <darius@dons.net.au>
parents: 12
diff changeset
117 tobj.setFillColor(ENGRAVE_COLOUR)
5f4c21bb5140 Add lines and notes engraved on the paper.
Daniel O'Connor <darius@dons.net.au>
parents: 12
diff changeset
118 tobj.setStrokeColor(ENGRAVE_COLOUR)
5f4c21bb5140 Add lines and notes engraved on the paper.
Daniel O'Connor <darius@dons.net.au>
parents: 12
diff changeset
119 tobj.textLine('%s' % (self.slot2note[slot]))
5f4c21bb5140 Add lines and notes engraved on the paper.
Daniel O'Connor <darius@dons.net.au>
parents: 12
diff changeset
120 pdf.drawText(tobj)
5f4c21bb5140 Add lines and notes engraved on the paper.
Daniel O'Connor <darius@dons.net.au>
parents: 12
diff changeset
121 pdf.restoreState()
11
9faad813e39e switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents: 9
diff changeset
122 pdf.save()
7
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
123
12
6e46ceee57a7 Use correct MIDI note generation, previous version did not agree with
Daniel O'Connor <darius@dons.net.au>
parents: 11
diff changeset
124 # 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
125 @staticmethod
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
126 def genmidi2note():
12
6e46ceee57a7 Use correct MIDI note generation, previous version did not agree with
Daniel O'Connor <darius@dons.net.au>
parents: 11
diff changeset
127 '''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
128 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
129 midi2note = {}
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
130 note2midi = {}
13
5f4c21bb5140 Add lines and notes engraved on the paper.
Daniel O'Connor <darius@dons.net.au>
parents: 12
diff changeset
131 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
132 octave = midi / len(names) - 1
7
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
133 index = midi % len(names)
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
134 name = names[index] % (octave)
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
135 midi2note[midi] = name
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
136 note2midi[name] = midi
4
9f4fa5f231e6 Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
137
7
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
138 return midi2note, note2midi
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
139
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
140 @staticmethod
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
141 def loadnote2slot(fname, note2midi):
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
142 note2slot = {}
13
5f4c21bb5140 Add lines and notes engraved on the paper.
Daniel O'Connor <darius@dons.net.au>
parents: 12
diff changeset
143 slot2note = {}
7
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
144 index = 0
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
145
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
146 for note in file(fname):
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
147 note = note.strip()
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
148 if note[0] == '#':
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
149 continue
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
150 if note not in note2midi:
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
151 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
152 note2slot[note] = index
13
5f4c21bb5140 Add lines and notes engraved on the paper.
Daniel O'Connor <darius@dons.net.au>
parents: 12
diff changeset
153 slot2note[index] = note
7
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
154 index += 1
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
155
13
5f4c21bb5140 Add lines and notes engraved on the paper.
Daniel O'Connor <darius@dons.net.au>
parents: 12
diff changeset
156 return note2slot, slot2note
4
9f4fa5f231e6 Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
157
11
9faad813e39e switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents: 9
diff changeset
158 def emitnote(self, pdfs, slot, start, notelen):
13
5f4c21bb5140 Add lines and notes engraved on the paper.
Daniel O'Connor <darius@dons.net.au>
parents: 12
diff changeset
159 x = start * self.timescale + self.margin
11
9faad813e39e switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents: 9
diff changeset
160 pageidx = int(x / self.pagewidth)
9faad813e39e switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents: 9
diff changeset
161 x = x % self.pagewidth
9faad813e39e switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents: 9
diff changeset
162 y = self.offset + slot * self.pitch
9faad813e39e switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents: 9
diff changeset
163 w = notelen * self.timescale
7
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
164 h = self.pitch
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
165
11
9faad813e39e switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents: 9
diff changeset
166 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
167 w1 = w
9faad813e39e switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents: 9
diff changeset
168 # 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
169 if x + w > self.pagewidth:
9faad813e39e switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents: 9
diff changeset
170 w1 = x - self.pagewidth # Crop first note
9faad813e39e switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents: 9
diff changeset
171 w2 = w - w1
9faad813e39e switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents: 9
diff changeset
172 assert w1 <= self.pagewidth, 'note extends for more than a page'
9faad813e39e switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents: 9
diff changeset
173 # 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
174 print 'split note, page %d w2 = %.3f' % (pageidx + 1, w2)
9faad813e39e switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents: 9
diff changeset
175 Midi2PDF._emitnote(pdfs[pageidx + 1], 0, y, w2, h)
9faad813e39e switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents: 9
diff changeset
176
9faad813e39e switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents: 9
diff changeset
177 Midi2PDF._emitnote(pdfs[pageidx], x, y, w1, h)
7
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
178
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
179 @staticmethod
11
9faad813e39e switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents: 9
diff changeset
180 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
181 pdf.saveState()
9faad813e39e switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents: 9
diff changeset
182 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
183 pdf.setLineWidth(0)
9faad813e39e switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents: 9
diff changeset
184 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
185 pdf.restoreState()
0
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
186
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
187 if __name__ == '__main__':
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
188 main()