changeset 16:20337b22977d

Add left margin and slot size support.
author Daniel O'Connor <darius@dons.net.au>
date Thu, 14 Apr 2016 09:48:08 +0930
parents b9fb9dee39e4
children c50427f1da2d
files musiccutter.py
diffstat 1 files changed, 10 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/musiccutter.py	Wed Apr 13 09:07:01 2016 +0930
+++ b/musiccutter.py	Thu Apr 14 09:48:08 2016 +0930
@@ -13,28 +13,27 @@
 CUT_COLOUR = reportlab.lib.colors.red
 ENGRAVE_COLOUR = reportlab.lib.colors.black
 
-
-## XXX: PDF origin bottom left, SVG origin top left
 def test(filename = None):
     if filename == None:
         filename = 'test.midi'
     # Card layout from http://www.orgues-de-barbarie.com/wp-content/uploads/2014/09/format-cartons.pdf
-    m = Midi2PDF('notes', 200, 155, 5.5, 6.0, 20, 10, 'Helvetica', 12)
+    m = Midi2PDF('notes', 200, 155, 5.5, 3.0, 6.0, 20, 10, 'Helvetica', 12)
     base, ext = os.path.splitext(filename)
     m.processMidi(filename, base + '-%02d.pdf')
 
 class Midi2PDF(object):
-    def __init__(self, notefile, pagewidth, pageheight, pitch, offset, margin, timescale, fontname, fontsize):
+    def __init__(self, notefile, pagewidth, pageheight, pitch, slotsize, offset, lmargin, timescale, fontname, fontsize):
         self.midi2note, self.note2midi = Midi2PDF.genmidi2note()
         self.note2slot, self.slot2note = Midi2PDF.loadnote2slot(notefile, self.note2midi)
         self.pagewidth = pagewidth # Dimensions are in millimetres
         self.pageheight = pageheight
         self.pitch = pitch # Distance between each slot
+        self.slotsize = slotsize # Size of each slot cut out
         self.offset = offset # Bottom margin
-        self.margin = margin # Left margin
-        self.timescale = timescale
+        self.lmargin = lmargin # Left margin
+        self.timescale = timescale # Width per second
         self.fontname = fontname
-        self.fontsize = fontsize
+        self.fontsize = fontsize # Points
 
     def processMidi(self, midifile, outpat):
         playablecount = 0
@@ -45,7 +44,7 @@
         for i in range(16):
             channels.append({})
 
-        npages = int(math.ceil(midi.length * self.timescale / self.pagewidth))
+        npages = int(math.ceil(((midi.length * self.timescale) + self.lmargin) / self.pagewidth))
         print 'npages', npages
         pdfs = []
         for i in range(npages):
@@ -156,12 +155,12 @@
         return note2slot, slot2note
 
     def emitnote(self, pdfs, slot, start, notelen):
-        x = start * self.timescale + self.margin
+        x = start * self.timescale + self.lmargin
         pageidx = int(x / self.pagewidth)
         x = x % self.pagewidth
-        y = self.offset + slot * self.pitch
+        y = self.offset + slot * self.pitch + (self.pitch - self.slotsize) / 2
         w = notelen * self.timescale
-        h = self.pitch
+        h = self.slotsize
 
         print 'page = %d x = %.3f y = %.3f w = %.3f h = %.3f' % (pageidx, x, y, w, h)
         w1 = w