diff musiccutter.py @ 13:5f4c21bb5140

Add lines and notes engraved on the paper. Add left margin for page one. Add comments about what each thing does.
author Daniel O'Connor <darius@dons.net.au>
date Tue, 12 Apr 2016 21:54:30 +0930
parents 6e46ceee57a7
children 43b5d8d62df6
line wrap: on
line diff
--- a/musiccutter.py	Sun Apr 10 22:24:56 2016 +0930
+++ b/musiccutter.py	Tue Apr 12 21:54:30 2016 +0930
@@ -19,18 +19,19 @@
     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, 10, 'Helvetica', 12)
+    m = Midi2PDF('notes', 200, 155, 5.5, 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, timescale, fontname, fontsize):
+    def __init__(self, notefile, pagewidth, pageheight, pitch, offset, margin, timescale, fontname, fontsize):
         self.midi2note, self.note2midi = Midi2PDF.genmidi2note()
-        self.note2slot = Midi2PDF.loadnote2slot(notefile, self.note2midi)
+        self.note2slot, self.slot2note = Midi2PDF.loadnote2slot(notefile, self.note2midi)
         self.pagewidth = pagewidth # Dimensions are in millimetres
         self.pageheight = pageheight
-        self.pitch = pitch
-        self.offset = offset
+        self.pitch = pitch # Distance between each slot
+        self.offset = offset # Bottom margin
+        self.margin = margin # Left margin
         self.timescale = timescale
         self.fontname = fontname
         self.fontsize = fontsize
@@ -91,13 +92,32 @@
 
         for i in range(len(pdfs)):
             pdf = pdfs[i]
+            # Add title and page number
             tobj = pdf.beginText()
-            tobj.setTextOrigin(2 * mm, 1 * mm)
+            tobj.setTextOrigin(50 * mm, 1 * mm)
             tobj.setFont(self.fontname, self.fontsize)
             tobj.setFillColor(ENGRAVE_COLOUR)
             tobj.setStrokeColor(ENGRAVE_COLOUR)
             tobj.textLine('%s (%d / %d)' % (title, i + 1, npages))
             pdf.drawText(tobj)
+
+            # Draw rect around page
+            pdf.saveState()
+            pdf.setLineWidth(0)
+            #pdf.rect(0, 0, self.pagewidth * mm, self.pageheight * mm, fill = False, stroke = True)
+            # Draw lines per note
+            for slot in sorted(self.slot2note.keys()):
+                ofs = (self.offset + slot * self.pitch) * mm
+                pdf.line(0, ofs, self.pagewidth * mm, ofs)
+                # Note name
+                tobj = pdf.beginText()
+                tobj.setTextOrigin(0 * mm, ofs + 1 * mm)
+                tobj.setFont(self.fontname, self.fontsize)
+                tobj.setFillColor(ENGRAVE_COLOUR)
+                tobj.setStrokeColor(ENGRAVE_COLOUR)
+                tobj.textLine('%s' % (self.slot2note[slot]))
+                pdf.drawText(tobj)
+            pdf.restoreState()
             pdf.save()
 
     # http://newt.phys.unsw.edu.au/jw/notes.html
@@ -107,7 +127,7 @@
         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']
         midi2note = {}
         note2midi = {}
-        for midi in range(128):
+        for midi in range(21, 128):
             octave = midi / len(names) - 1
             index = midi % len(names)
             name = names[index] % (octave)
@@ -119,6 +139,7 @@
     @staticmethod
     def loadnote2slot(fname, note2midi):
         note2slot = {}
+        slot2note = {}
         index = 0
 
         for note in file(fname):
@@ -128,12 +149,13 @@
             if note not in note2midi:
                 raise exceptions.ValueError('Note \'%s\' not valid' % note)
             note2slot[note] = index
+            slot2note[index] = note
             index += 1
 
-        return note2slot
+        return note2slot, slot2note
 
     def emitnote(self, pdfs, slot, start, notelen):
-        x = start * self.timescale
+        x = start * self.timescale + self.margin
         pageidx = int(x / self.pagewidth)
         x = x % self.pagewidth
         y = self.offset + slot * self.pitch