comparison musiccutter.py @ 24:71b78f06ff03

Highest notes are at the bottom not the top. Rename margin to heel to match the builders nomenclature.
author Daniel O'Connor <darius@dons.net.au>
date Sat, 30 Apr 2016 16:37:14 +0930
parents 63d13efa040f
children ce367392806c
comparison
equal deleted inserted replaced
23:63d13efa040f 24:71b78f06ff03
16 def test(filename = None): 16 def test(filename = None):
17 if filename == None: 17 if filename == None:
18 filename = 'test.midi' 18 filename = 'test.midi'
19 # Card layout from http://www.orgues-de-barbarie.com/wp-content/uploads/2014/09/format-cartons.pdf 19 # Card layout from http://www.orgues-de-barbarie.com/wp-content/uploads/2014/09/format-cartons.pdf
20 # Notes are read from right to left 20 # Notes are read from right to left
21 m = Midi2PDF('notes', 120, 155, 5.5, 3.0, 6.0, 50, False, False, False, False, 12, 10, 'Helvetica', 12) 21 # Highest note is at the bottom (closest to the crank)
22 m = Midi2PDF('notes', 120, 155, 5.5, 3.3, 6.0, 50, False, False, False, False, 0, 10, 'Helvetica', 12)
22 base, ext = os.path.splitext(filename) 23 base, ext = os.path.splitext(filename)
23 base = os.path.basename(base) 24 base = os.path.basename(base)
24 m.processMidi(filename, base + '-%02d.pdf') 25 m.processMidi(filename, base + '-%02d.pdf')
25 26
26 class Midi2PDF(object): 27 class Midi2PDF(object):
27 def __init__(self, notefile, pagewidth, pageheight, pitch, slotsize, offset, leadin, timemarks, drawrect, notenames, notelines, noteoffset, timescale, fontname, fontsize): 28 def __init__(self, notefile, pagewidth, pageheight, pitch, slotsize, heel, leadin, timemarks, drawrect, notenames, notelines, noteoffset, timescale, fontname, fontsize):
28 self.midi2note, self.note2midi = Midi2PDF.genmidi2note(noteoffset) 29 self.midi2note, self.note2midi = Midi2PDF.genmidi2note(noteoffset)
29 self.note2slot, self.slot2note = Midi2PDF.loadnote2slot(notefile, self.note2midi) 30 self.note2slot, self.slot2note = Midi2PDF.loadnote2slot(notefile, self.note2midi)
30 self.pagewidth = pagewidth # Dimensions are in millimetres 31 self.pagewidth = pagewidth # Dimensions are in millimetres
31 self.pageheight = pageheight 32 self.pageheight = pageheight
32 self.pitch = pitch # Distance between each slot 33 self.pitch = pitch # Distance between each slot
33 self.slotsize = slotsize # Size of each slot cut out 34 self.slotsize = slotsize # Size of each slot cut out
34 self.offset = offset # Bottom margin 35 self.heel = heel # Bottom margin (from bottom of page to centre of slot)
35 self.leadin = leadin # Extra at the start 36 self.leadin = leadin # Extra at the start
36 self.timemarks = timemarks # Draw vertical time lines 37 self.timemarks = timemarks # Draw vertical time lines
37 self.drawrect = drawrect # Draw rectangle around each page 38 self.drawrect = drawrect # Draw rectangle around each page
38 self.notenames = notenames # Draw note names on the right edge 39 self.notenames = notenames # Draw note names on the right edge
39 self.notelines = notelines # Draw line rulers 40 self.notelines = notelines # Draw line rulers
112 tsize = self.pagewidth / self.timescale 113 tsize = self.pagewidth / self.timescale
113 tstart = tend + tsize * pindx 114 tstart = tend + tsize * pindx
114 tend = tend + tsize * (pindx + 1) 115 tend = tend + tsize * (pindx + 1)
115 for s in range(tstart, tend, 5): 116 for s in range(tstart, tend, 5):
116 x = self.pagewidth - (float(s * self.timescale + self.leadin) % self.pagewidth) 117 x = self.pagewidth - (float(s * self.timescale + self.leadin) % self.pagewidth)
117 pdf.line(x * mm, self.offset, x * mm, self.pageheight * mm) 118 pdf.line(x * mm, self.heel, x * mm, self.pageheight * mm)
118 Midi2PDF.textHelper(pdf, x * mm, 1 * mm, ENGRAVE_COLOUR, False, self.fontname, self.fontsize, str(s) + 's') 119 Midi2PDF.textHelper(pdf, x * mm, 1 * mm, ENGRAVE_COLOUR, False, self.fontname, self.fontsize, str(s) + 's')
119 120
120 # Draw rectangle around page 121 # Draw rectangle around page
121 if self.drawrect: 122 if self.drawrect:
122 pdf.rect(0, 0, self.pagewidth * mm, self.pageheight * mm, fill = False, stroke = True) 123 pdf.rect(0, 0, self.pagewidth * mm, self.pageheight * mm, fill = False, stroke = True)
123 124
124 # Draw lines per note 125 # Draw lines per note
125 for slot in sorted(self.slot2note.keys()): 126 for slot in sorted(self.slot2note.keys()):
126 ofs = (self.offset + slot * self.pitch) * mm 127 ofs = (self.heel - self.slotsize / 2 + slot * self.pitch) * mm
127 if self.notelines: 128 if self.notelines:
128 pdf.line(0, ofs, self.pagewidth * mm, ofs) 129 pdf.line(0, ofs, self.pagewidth * mm, ofs)
129 # Note name 130 # Note name
130 if self.notenames: 131 if self.notenames:
131 Midi2PDF.textHelper(pdf, (self.pagewidth - 10) * mm, ofs + 1 * mm, ENGRAVE_COLOUR, False, self.fontname, self.fontsize, self.slot2note[slot]) 132 Midi2PDF.textHelper(pdf, (self.pagewidth - 10) * mm, ofs + 1 * mm, ENGRAVE_COLOUR, False, self.fontname, self.fontsize, self.slot2note[slot])
169 def emitnote(self, pdfs, slot, start, notelen): 170 def emitnote(self, pdfs, slot, start, notelen):
170 x = start * self.timescale + self.leadin # Convert start time to distance 171 x = start * self.timescale + self.leadin # Convert start time to distance
171 pageidx = int(x / self.pagewidth) # Determine which page 172 pageidx = int(x / self.pagewidth) # Determine which page
172 x = x % self.pagewidth # and where on that page 173 x = x % self.pagewidth # and where on that page
173 h = self.slotsize 174 h = self.slotsize
174 y = self.offset + slot * self.pitch + (self.pitch - h) / 2 175 y = self.pageheight - (self.heel + slot * self.pitch - self.slotsize / 2) - self.slotsize
175 w = notelen * self.timescale # Convert note length in time to distance 176 w = notelen * self.timescale # Convert note length in time to distance
176 177
177 print 'page = %d x = %.3f y = %.3f w = %.3f h = %.3f' % (pageidx, x, y, w, h) 178 print 'page = %d x = %.3f y = %.3f w = %.3f h = %.3f' % (pageidx, x, y, w, h)
178 w1 = w 179 w1 = w
179 # Check if the note crosses a page 180 # Check if the note crosses a page