# HG changeset patch # User Daniel O'Connor # Date 1460463870 -34200 # Node ID 5f4c21bb5140e72506a9a15507d0f140e233d20c # Parent 6e46ceee57a7f931fef0ca6dda44774ca16f4284 Add lines and notes engraved on the paper. Add left margin for page one. Add comments about what each thing does. diff -r 6e46ceee57a7 -r 5f4c21bb5140 Makefile --- a/Makefile Sun Apr 10 22:24:56 2016 +0930 +++ b/Makefile Tue Apr 12 21:54:30 2016 +0930 @@ -6,11 +6,7 @@ MVOPTS= MPOPTS= -DPIANO -DNOVOICE -all: test.midi test.ps -#all: test.ps test-piano.ps test.midi - -test-piano.ps: test.mup - $(MUP) $(MOPTS) $(MPOPTS) $> > $@ +all: test.midi scale.midi clean: $(RM) test.ps test-piano.ps test.midi diff -r 6e46ceee57a7 -r 5f4c21bb5140 musiccutter.py --- 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